[#26] All: Remove V2 from naming
Rename project, namespaces and class names Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
parent
c406df1a78
commit
766f61a5f7
219 changed files with 219 additions and 974 deletions
126
src/FrostFS.SDK.Client/Tools/RequestSigner.cs
Normal file
126
src/FrostFS.SDK.Client/Tools/RequestSigner.cs
Normal file
|
@ -0,0 +1,126 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using FrostFS.Refs;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
using FrostFS.SDK.Proto.Interfaces;
|
||||
using FrostFS.Session;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
using Org.BouncyCastle.Asn1.Sec;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Crypto.Signers;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace FrostFS.SDK.Client;
|
||||
|
||||
public static class RequestSigner
|
||||
{
|
||||
internal const int RFC6979SignatureSize = 64;
|
||||
|
||||
internal static byte[] SignRFC6979(this ECDsa key, byte[] data)
|
||||
{
|
||||
if (key is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (data is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
|
||||
var digest = new Sha256Digest();
|
||||
var secp256R1 = SecNamedCurves.GetByName("secp256r1");
|
||||
var ecParameters = new ECDomainParameters(secp256R1.Curve, secp256R1.G, secp256R1.N);
|
||||
var privateKey = new ECPrivateKeyParameters(new BigInteger(1, key.PrivateKey()), ecParameters);
|
||||
var signer = new ECDsaSigner(new HMacDsaKCalculator(digest));
|
||||
var hash = new byte[digest.GetDigestSize()];
|
||||
|
||||
digest.BlockUpdate(data, 0, data.Length);
|
||||
digest.DoFinal(hash, 0);
|
||||
signer.Init(true, privateKey);
|
||||
|
||||
var rs = signer.GenerateSignature(hash);
|
||||
var signature = new byte[RFC6979SignatureSize];
|
||||
var rbytes = rs[0].ToByteArrayUnsigned();
|
||||
var sbytes = rs[1].ToByteArrayUnsigned();
|
||||
var index = RFC6979SignatureSize / 2 - rbytes.Length;
|
||||
|
||||
rbytes.CopyTo(signature, index);
|
||||
index = RFC6979SignatureSize - sbytes.Length;
|
||||
sbytes.CopyTo(signature, index);
|
||||
|
||||
return signature;
|
||||
}
|
||||
|
||||
internal static SignatureRFC6979 SignRFC6979(this ECDsa key, IMessage message)
|
||||
{
|
||||
return new SignatureRFC6979
|
||||
{
|
||||
Key = ByteString.CopyFrom(key.PublicKey()),
|
||||
Sign = ByteString.CopyFrom(key.SignRFC6979(message.ToByteArray())),
|
||||
};
|
||||
}
|
||||
|
||||
internal static SignatureRFC6979 SignRFC6979(this ECDsa key, ByteString data)
|
||||
{
|
||||
return new SignatureRFC6979
|
||||
{
|
||||
Key = ByteString.CopyFrom(key.PublicKey()),
|
||||
Sign = ByteString.CopyFrom(key.SignRFC6979(data.ToByteArray())),
|
||||
};
|
||||
}
|
||||
|
||||
public static byte[] SignData(this ECDsa key, byte[] data)
|
||||
{
|
||||
if (key is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
var hash = new byte[65];
|
||||
hash[0] = 0x04;
|
||||
|
||||
key.SignHash(data.Sha512()).CopyTo(hash, 1);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
internal static Signature SignMessagePart(this ECDsa key, IMessage? data)
|
||||
{
|
||||
var data2Sign = data is null ? [] : data.ToByteArray();
|
||||
var sig = new Signature
|
||||
{
|
||||
Key = ByteString.CopyFrom(key.PublicKey()),
|
||||
Sign = ByteString.CopyFrom(key.SignData(data2Sign)),
|
||||
};
|
||||
|
||||
return sig;
|
||||
}
|
||||
|
||||
internal static void Sign(this IVerifiableMessage message, ECDsa key)
|
||||
{
|
||||
var meta = message.GetMetaHeader();
|
||||
IVerificationHeader verify = message switch
|
||||
{
|
||||
IRequest => new RequestVerificationHeader(),
|
||||
IResponse => new ResponseVerificationHeader(),
|
||||
_ => throw new InvalidOperationException("Unsupported message type")
|
||||
};
|
||||
|
||||
var verifyOrigin = message.GetVerificationHeader();
|
||||
|
||||
if (verifyOrigin is null)
|
||||
verify.BodySignature = key.SignMessagePart(message.GetBody());
|
||||
else
|
||||
verify.SetOrigin(verifyOrigin);
|
||||
|
||||
verify.MetaSignature = key.SignMessagePart(meta);
|
||||
verify.OriginSignature = key.SignMessagePart(verifyOrigin);
|
||||
|
||||
message.SetVerificationHeader(verify);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue