You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.2 KiB
145 lines
4.2 KiB
8 months ago
|
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||
|
#pragma warning disable
|
||
|
using System;
|
||
|
using System.IO;
|
||
|
|
||
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
|
||
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032;
|
||
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
|
||
|
|
||
|
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
|
||
|
{
|
||
|
public class Ed448Signer
|
||
|
: ISigner
|
||
|
{
|
||
|
private readonly Buffer buffer = new Buffer();
|
||
|
private readonly byte[] context;
|
||
|
|
||
|
private bool forSigning;
|
||
|
private Ed448PrivateKeyParameters privateKey;
|
||
|
private Ed448PublicKeyParameters publicKey;
|
||
|
|
||
|
public Ed448Signer(byte[] context)
|
||
|
{
|
||
|
this.context = Arrays.Clone(context);
|
||
|
}
|
||
|
|
||
|
public virtual string AlgorithmName
|
||
|
{
|
||
|
get { return "Ed448"; }
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
buffer.WriteByte(b);
|
||
|
}
|
||
|
|
||
|
public virtual void BlockUpdate(byte[] buf, int off, int len)
|
||
|
{
|
||
|
buffer.Write(buf, off, len);
|
||
|
}
|
||
|
|
||
|
public virtual byte[] GenerateSignature()
|
||
|
{
|
||
|
if (!forSigning || null == privateKey)
|
||
|
throw new InvalidOperationException("Ed448Signer not initialised for signature generation.");
|
||
|
|
||
|
return buffer.GenerateSignature(privateKey, context);
|
||
|
}
|
||
|
|
||
|
public virtual bool VerifySignature(byte[] signature)
|
||
|
{
|
||
|
if (forSigning || null == publicKey)
|
||
|
throw new InvalidOperationException("Ed448Signer not initialised for verification");
|
||
|
|
||
|
return buffer.VerifySignature(publicKey, context, signature);
|
||
|
}
|
||
|
|
||
|
public virtual void Reset()
|
||
|
{
|
||
|
buffer.Reset();
|
||
|
}
|
||
|
|
||
|
private class Buffer : MemoryStream
|
||
|
{
|
||
|
internal byte[] GenerateSignature(Ed448PrivateKeyParameters privateKey, byte[] ctx)
|
||
|
{
|
||
|
lock (this)
|
||
|
{
|
||
|
#if PORTABLE || NETFX_CORE
|
||
|
byte[] buf = ToArray();
|
||
|
int count = buf.Length;
|
||
|
#else
|
||
|
byte[] buf = GetBuffer();
|
||
|
int count = (int)Position;
|
||
|
#endif
|
||
|
byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
|
||
|
privateKey.Sign(Ed448.Algorithm.Ed448, ctx, buf, 0, count, signature, 0);
|
||
|
Reset();
|
||
|
return signature;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal bool VerifySignature(Ed448PublicKeyParameters publicKey, byte[] ctx, byte[] signature)
|
||
|
{
|
||
|
if (Ed448.SignatureSize != signature.Length)
|
||
|
{
|
||
|
Reset();
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
lock (this)
|
||
|
{
|
||
|
#if PORTABLE || NETFX_CORE
|
||
|
byte[] buf = ToArray();
|
||
|
int count = buf.Length;
|
||
|
#else
|
||
|
byte[] buf = GetBuffer();
|
||
|
int count = (int)Position;
|
||
|
#endif
|
||
|
byte[] pk = publicKey.GetEncoded();
|
||
|
bool result = Ed448.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
|
||
|
Reset();
|
||
|
return result;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal void Reset()
|
||
|
{
|
||
|
lock (this)
|
||
|
{
|
||
|
long count = Position;
|
||
|
#if PORTABLE || NETFX_CORE
|
||
|
this.Position = 0L;
|
||
|
Streams.WriteZeroes(this, count);
|
||
|
#else
|
||
|
Array.Clear(GetBuffer(), 0, (int)count);
|
||
|
#endif
|
||
|
this.Position = 0L;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#pragma warning restore
|
||
|
#endif
|