mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Prehash digest for safer raw signers
- Loading branch information
1 parent
e253567
commit 5ad15ee
Showing
5 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
|
||
using Org.BouncyCastle.Utilities.IO; | ||
|
||
namespace Org.BouncyCastle.Crypto.Digests | ||
{ | ||
public sealed class Prehash | ||
: IDigest | ||
{ | ||
public static Prehash ForDigest(IDigest digest) => new Prehash(digest); | ||
|
||
private readonly string m_algorithmName; | ||
private readonly LimitedBuffer m_buf; | ||
|
||
private Prehash(IDigest digest) | ||
{ | ||
m_algorithmName = digest.AlgorithmName; | ||
m_buf = new LimitedBuffer(digest.GetDigestSize()); | ||
} | ||
|
||
public string AlgorithmName => m_algorithmName; | ||
|
||
public int GetByteLength() => throw new NotSupportedException(); | ||
|
||
public int GetDigestSize() => m_buf.Limit; | ||
|
||
public void Update(byte input) => m_buf.WriteByte(input); | ||
|
||
public void BlockUpdate(byte[] input, int inOff, int inLen) => m_buf.Write(input, inOff, inLen); | ||
|
||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public void BlockUpdate(ReadOnlySpan<byte> input) => m_buf.Write(input); | ||
#endif | ||
|
||
public int DoFinal(byte[] output, int outOff) | ||
{ | ||
try | ||
{ | ||
if (GetDigestSize() != m_buf.Count) | ||
throw new InvalidOperationException("Incorrect prehash size"); | ||
|
||
return m_buf.CopyTo(output, outOff); | ||
} | ||
finally | ||
{ | ||
Reset(); | ||
} | ||
} | ||
|
||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public int DoFinal(Span<byte> output) | ||
{ | ||
try | ||
{ | ||
if (GetDigestSize() != m_buf.Count) | ||
throw new InvalidOperationException("Incorrect prehash size"); | ||
|
||
return m_buf.CopyTo(output); | ||
} | ||
finally | ||
{ | ||
Reset(); | ||
} | ||
} | ||
#endif | ||
|
||
public void Reset() => m_buf.Reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
|
||
namespace Org.BouncyCastle.Utilities.IO | ||
{ | ||
public sealed class LimitedBuffer | ||
: BaseOutputStream | ||
{ | ||
private readonly byte[] m_buf; | ||
private int m_count; | ||
|
||
public LimitedBuffer(int limit) | ||
{ | ||
m_buf = new byte[limit]; | ||
m_count = 0; | ||
} | ||
|
||
public int CopyTo(byte[] buffer, int offset) | ||
{ | ||
Array.Copy(m_buf, 0, buffer, offset, m_count); | ||
return m_count; | ||
} | ||
|
||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public int CopyTo(Span<byte> buffer) | ||
{ | ||
m_buf.AsSpan(0, m_count).CopyTo(buffer); | ||
return m_count; | ||
} | ||
#endif | ||
|
||
public int Count => m_count; | ||
|
||
public int Limit => m_buf.Length; | ||
|
||
public void Reset() | ||
{ | ||
m_count = 0; | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
Array.Copy(buffer, offset, m_buf, m_count, count); | ||
m_count += count; | ||
} | ||
|
||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public override void Write(ReadOnlySpan<byte> buffer) | ||
{ | ||
buffer.CopyTo(m_buf.AsSpan(m_count)); | ||
} | ||
#endif | ||
|
||
public override void WriteByte(byte value) | ||
{ | ||
m_buf[m_count++] = value; | ||
} | ||
} | ||
} |