1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
using System;
using System.IO;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Signers
{
public class Ed448phSigner
: ISigner
{
private readonly IXof prehash = Ed448.CreatePrehash();
private readonly byte[] context;
private bool forSigning;
private Ed448PrivateKeyParameters privateKey;
private Ed448PublicKeyParameters publicKey;
public Ed448phSigner(byte[] context)
{
if (null == context)
throw new ArgumentNullException(nameof(context));
this.context = (byte[])context.Clone();
}
public virtual string AlgorithmName
{
get { return "Ed448ph"; }
}
public virtual void Init(bool forSigning, ICipherParameters parameters)
{
this.forSigning = forSigning;
if (forSigning)
{
this.privateKey = (Ed448PrivateKeyParameters)parameters;
this.publicKey = null;
}
else
{
this.privateKey = null;
this.publicKey = (Ed448PublicKeyParameters)parameters;
}
Reset();
}
public virtual void Update(byte b)
{
prehash.Update(b);
}
public virtual void BlockUpdate(byte[] buf, int off, int len)
{
prehash.BlockUpdate(buf, off, len);
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public virtual void BlockUpdate(ReadOnlySpan<byte> input)
{
prehash.BlockUpdate(input);
}
#endif
public virtual int GetMaxSignatureSize() => Ed448.SignatureSize;
public virtual byte[] GenerateSignature()
{
if (!forSigning || null == privateKey)
throw new InvalidOperationException("Ed448phSigner not initialised for signature generation.");
byte[] msg = new byte[Ed448.PrehashSize];
if (Ed448.PrehashSize != prehash.OutputFinal(msg, 0, Ed448.PrehashSize))
throw new InvalidOperationException("Prehash digest failed");
byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
privateKey.Sign(Ed448.Algorithm.Ed448ph, context, msg, 0, Ed448.PrehashSize, signature, 0);
return signature;
}
public virtual bool VerifySignature(byte[] signature)
{
if (forSigning || null == publicKey)
throw new InvalidOperationException("Ed448phSigner not initialised for verification");
if (Ed448.SignatureSize != signature.Length)
{
prehash.Reset();
return false;
}
byte[] msg = new byte[Ed448.PrehashSize];
if (Ed448.PrehashSize != prehash.OutputFinal(msg, 0, Ed448.PrehashSize))
throw new InvalidOperationException("Prehash digest failed");
return publicKey.Verify(Ed448.Algorithm.Ed448ph, context, msg, 0, Ed448.PrehashSize, signature, 0);
}
public void Reset()
{
prehash.Reset();
}
}
}
|