Skip to content

Commit 28d7001

Browse files
committed
Fix some TODOs for PORTABLE and Streams
1 parent 8b8a625 commit 28d7001

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

crypto/src/crypto/digests/NullDigest.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
33

4+
using Org.BouncyCastle.Utilities.IO;
5+
46
namespace Org.BouncyCastle.Crypto.Digests
57
{
68
public class NullDigest : IDigest
@@ -20,7 +22,7 @@ public int GetByteLength()
2022

2123
public int GetDigestSize()
2224
{
23-
return (int) bOut.Length;
25+
return (int)bOut.Length;
2426
}
2527

2628
public void Update(byte b)
@@ -33,15 +35,19 @@ public void BlockUpdate(byte[] inBytes, int inOff, int len)
3335
bOut.Write(inBytes, inOff, len);
3436
}
3537

36-
public int DoFinal(byte[] outBytes, int outOff)
38+
public int DoFinal(byte[] outBytes, int outOff)
3739
{
38-
byte[] res = bOut.ToArray();
39-
res.CopyTo(outBytes, outOff);
40-
Reset();
41-
return res.Length;
42-
}
43-
44-
public void Reset()
40+
try
41+
{
42+
return Streams.WriteBufTo(bOut, outBytes, outOff);
43+
}
44+
finally
45+
{
46+
Reset();
47+
}
48+
}
49+
50+
public void Reset()
4551
{
4652
bOut.SetLength(0);
4753
}

crypto/src/crypto/signers/Ed25519Signer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Org.BouncyCastle.Crypto.Parameters;
66
using Org.BouncyCastle.Math.EC.Rfc8032;
77
using Org.BouncyCastle.Utilities;
8+
using Org.BouncyCastle.Utilities.IO;
89

910
namespace Org.BouncyCastle.Crypto.Signers
1011
{
@@ -114,12 +115,12 @@ internal bool VerifySignature(Ed25519PublicKeyParameters publicKey, byte[] signa
114115
[MethodImpl(MethodImplOptions.Synchronized)]
115116
internal void Reset()
116117
{
118+
long count = Position;
117119
#if PORTABLE
118120
this.Position = 0L;
119-
120-
// TODO Clear using Write method
121+
Streams.WriteZeroes(this, count);
121122
#else
122-
Array.Clear(GetBuffer(), 0, (int)Position);
123+
Array.Clear(GetBuffer(), 0, (int)count);
123124
#endif
124125
this.Position = 0L;
125126
}

crypto/src/crypto/signers/Ed25519ctxSigner.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Org.BouncyCastle.Crypto.Parameters;
66
using Org.BouncyCastle.Math.EC.Rfc8032;
77
using Org.BouncyCastle.Utilities;
8+
using Org.BouncyCastle.Utilities.IO;
89

910
namespace Org.BouncyCastle.Crypto.Signers
1011
{
@@ -116,12 +117,12 @@ internal bool VerifySignature(Ed25519PublicKeyParameters publicKey, byte[] ctx,
116117
[MethodImpl(MethodImplOptions.Synchronized)]
117118
internal void Reset()
118119
{
120+
long count = Position;
119121
#if PORTABLE
120122
this.Position = 0L;
121-
122-
// TODO Clear using Write method
123+
Streams.WriteZeroes(this, count);
123124
#else
124-
Array.Clear(GetBuffer(), 0, (int)Position);
125+
Array.Clear(GetBuffer(), 0, (int)count);
125126
#endif
126127
this.Position = 0L;
127128
}

crypto/src/crypto/signers/Ed448Signer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Org.BouncyCastle.Crypto.Parameters;
66
using Org.BouncyCastle.Math.EC.Rfc8032;
77
using Org.BouncyCastle.Utilities;
8+
using Org.BouncyCastle.Utilities.IO;
89

910
namespace Org.BouncyCastle.Crypto.Signers
1011
{
@@ -116,12 +117,12 @@ internal bool VerifySignature(Ed448PublicKeyParameters publicKey, byte[] ctx, by
116117
[MethodImpl(MethodImplOptions.Synchronized)]
117118
internal void Reset()
118119
{
120+
long count = Position;
119121
#if PORTABLE
120122
this.Position = 0L;
121-
122-
// TODO Clear using Write method
123+
Streams.WriteZeroes(this, count);
123124
#else
124-
Array.Clear(GetBuffer(), 0, (int)Position);
125+
Array.Clear(GetBuffer(), 0, (int)count);
125126
#endif
126127
this.Position = 0L;
127128
}

crypto/src/util/io/Streams.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,30 @@ public static void WriteBufTo(MemoryStream buf, Stream output)
9696
{
9797
buf.WriteTo(output);
9898
}
99+
100+
/// <exception cref="IOException"></exception>
101+
public static int WriteBufTo(MemoryStream buf, byte[] output, int offset)
102+
{
103+
#if PORTABLE
104+
byte[] bytes = buf.ToArray();
105+
bytes.CopyTo(output, offset);
106+
return bytes.Length;
107+
#else
108+
int size = (int)buf.Length;
109+
buf.WriteTo(new MemoryStream(output, offset, size, true));
110+
return size;
111+
#endif
112+
}
113+
114+
public static void WriteZeroes(Stream outStr, long count)
115+
{
116+
byte[] zeroes = new byte[BufferSize];
117+
while (count > BufferSize)
118+
{
119+
outStr.Write(zeroes, 0, BufferSize);
120+
count -= BufferSize;
121+
}
122+
outStr.Write(zeroes, 0, (int)count);
123+
}
99124
}
100125
}

0 commit comments

Comments
 (0)