From 739c0024b2677dcf16cac9c268a2124de47e8efb Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 27 Nov 2022 23:03:19 +0700 Subject: EdDSA: match API to bc-java --- .../parameters/Ed25519PrivateKeyParameters.cs | 5 +- .../parameters/Ed25519PublicKeyParameters.cs | 26 +++-- .../crypto/parameters/Ed448PrivateKeyParameters.cs | 5 +- .../crypto/parameters/Ed448PublicKeyParameters.cs | 26 +++-- crypto/src/math/ec/rfc8032/Ed25519.cs | 110 +++++++++------------ crypto/src/math/ec/rfc8032/Ed448.cs | 110 +++++++++------------ 6 files changed, 134 insertions(+), 148 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/crypto/parameters/Ed25519PrivateKeyParameters.cs b/crypto/src/crypto/parameters/Ed25519PrivateKeyParameters.cs index 161dedb6d..6d2e44937 100644 --- a/crypto/src/crypto/parameters/Ed25519PrivateKeyParameters.cs +++ b/crypto/src/crypto/parameters/Ed25519PrivateKeyParameters.cs @@ -77,11 +77,10 @@ namespace Org.BouncyCastle.Crypto.Parameters if (null == cachedPublicKey) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - Ed25519.GeneratePublicKey(data, out var publicPoint); + cachedPublicKey = new Ed25519PublicKeyParameters(Ed25519.GeneratePublicKey(data)); #else - Ed25519.GeneratePublicKey(data, 0, out var publicPoint); + cachedPublicKey = new Ed25519PublicKeyParameters(Ed25519.GeneratePublicKey(data, 0)); #endif - cachedPublicKey = new Ed25519PublicKeyParameters(publicPoint); } return cachedPublicKey; diff --git a/crypto/src/crypto/parameters/Ed25519PublicKeyParameters.cs b/crypto/src/crypto/parameters/Ed25519PublicKeyParameters.cs index 57c63e624..8a89a2db2 100644 --- a/crypto/src/crypto/parameters/Ed25519PublicKeyParameters.cs +++ b/crypto/src/crypto/parameters/Ed25519PublicKeyParameters.cs @@ -21,8 +21,7 @@ namespace Org.BouncyCastle.Crypto.Parameters public Ed25519PublicKeyParameters(byte[] buf, int off) : base(false) { - if (!Ed25519.ValidatePublicKeyPartial(buf, off, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(buf, off); } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -32,8 +31,7 @@ namespace Org.BouncyCastle.Crypto.Parameters if (buf.Length != KeySize) throw new ArgumentException("must have length " + KeySize, nameof(buf)); - if (!Ed25519.ValidatePublicKeyPartial(buf, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(buf); } #endif @@ -50,11 +48,9 @@ namespace Org.BouncyCastle.Crypto.Parameters throw new EndOfStreamException("EOF encountered in middle of Ed25519 public key"); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - if (!Ed25519.ValidatePublicKeyPartial(data, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(data); #else - if (!Ed25519.ValidatePublicKeyPartial(data, 0, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(data, 0); #endif } @@ -122,6 +118,20 @@ namespace Org.BouncyCastle.Crypto.Parameters } } + private static Ed25519.PublicPoint Parse(byte[] buf, int off) + { + return Ed25519.ValidatePublicKeyPartialExport(buf, off) + ?? throw new ArgumentException("invalid public key"); + } + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + private static Ed25519.PublicPoint Parse(ReadOnlySpan buf) + { + return Ed25519.ValidatePublicKeyPartialExport(buf) + ?? throw new ArgumentException("invalid public key"); + } +#endif + private static byte[] Validate(byte[] buf) { if (buf.Length != KeySize) diff --git a/crypto/src/crypto/parameters/Ed448PrivateKeyParameters.cs b/crypto/src/crypto/parameters/Ed448PrivateKeyParameters.cs index b80b68529..a9d1d0e93 100644 --- a/crypto/src/crypto/parameters/Ed448PrivateKeyParameters.cs +++ b/crypto/src/crypto/parameters/Ed448PrivateKeyParameters.cs @@ -77,11 +77,10 @@ namespace Org.BouncyCastle.Crypto.Parameters if (null == cachedPublicKey) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - Ed448.GeneratePublicKey(data, out var publicPoint); + cachedPublicKey = new Ed448PublicKeyParameters(Ed448.GeneratePublicKey(data)); #else - Ed448.GeneratePublicKey(data, 0, out var publicPoint); + cachedPublicKey = new Ed448PublicKeyParameters(Ed448.GeneratePublicKey(data, 0)); #endif - cachedPublicKey = new Ed448PublicKeyParameters(publicPoint); } return cachedPublicKey; diff --git a/crypto/src/crypto/parameters/Ed448PublicKeyParameters.cs b/crypto/src/crypto/parameters/Ed448PublicKeyParameters.cs index 0f07fa783..cf165751a 100644 --- a/crypto/src/crypto/parameters/Ed448PublicKeyParameters.cs +++ b/crypto/src/crypto/parameters/Ed448PublicKeyParameters.cs @@ -21,8 +21,7 @@ namespace Org.BouncyCastle.Crypto.Parameters public Ed448PublicKeyParameters(byte[] buf, int off) : base(false) { - if (!Ed448.ValidatePublicKeyPartial(buf, off, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(buf, off); } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -32,8 +31,7 @@ namespace Org.BouncyCastle.Crypto.Parameters if (buf.Length != KeySize) throw new ArgumentException("must have length " + KeySize, nameof(buf)); - if (!Ed448.ValidatePublicKeyPartial(buf, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(buf); } #endif @@ -50,11 +48,9 @@ namespace Org.BouncyCastle.Crypto.Parameters throw new EndOfStreamException("EOF encountered in middle of Ed448 public key"); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - if (!Ed448.ValidatePublicKeyPartial(data, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(data); #else - if (!Ed448.ValidatePublicKeyPartial(data, 0, out m_publicPoint)) - throw new ArgumentException("invalid public key"); + m_publicPoint = Parse(data, 0); #endif } @@ -115,6 +111,20 @@ namespace Org.BouncyCastle.Crypto.Parameters } } + private static Ed448.PublicPoint Parse(byte[] buf, int off) + { + return Ed448.ValidatePublicKeyPartialExport(buf, off) + ?? throw new ArgumentException("invalid public key"); + } + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + private static Ed448.PublicPoint Parse(ReadOnlySpan buf) + { + return Ed448.ValidatePublicKeyPartialExport(buf) + ?? throw new ArgumentException("invalid public key"); + } +#endif + private static byte[] Validate(byte[] buf) { if (buf.Length != KeySize) diff --git a/crypto/src/math/ec/rfc8032/Ed25519.cs b/crypto/src/math/ec/rfc8032/Ed25519.cs index cde61b621..d5b035734 100644 --- a/crypto/src/math/ec/rfc8032/Ed25519.cs +++ b/crypto/src/math/ec/rfc8032/Ed25519.cs @@ -442,13 +442,13 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - private static void ExportPoint(ref PointAffine p, out PublicPoint publicPoint) + private static PublicPoint ExportPoint(ref PointAffine p) { int[] data = new int[F.Size * 2]; F.Copy(p.x, 0, data, 0); F.Copy(p.y, 0, data, F.Size); - publicPoint = new PublicPoint(data); + return new PublicPoint(data); } public static void GeneratePrivateKey(SecureRandom random, byte[] k) @@ -503,10 +503,10 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - public static void GeneratePublicKey(byte[] sk, int skOff, out PublicPoint publicPoint) + public static PublicPoint GeneratePublicKey(byte[] sk, int skOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - GeneratePublicKey(sk.AsSpan(skOff), out publicPoint); + return GeneratePublicKey(sk.AsSpan(skOff)); #else IDigest d = CreateDigest(); byte[] h = new byte[64]; @@ -526,12 +526,12 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 if (0 == CheckPoint(ref q)) throw new InvalidOperationException(); - ExportPoint(ref q, out publicPoint); + return ExportPoint(ref q); #endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public static void GeneratePublicKey(ReadOnlySpan sk, out PublicPoint publicPoint) + public static PublicPoint GeneratePublicKey(ReadOnlySpan sk) { IDigest d = CreateDigest(); Span h = stackalloc byte[64]; @@ -551,7 +551,7 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 if (0 == CheckPoint(ref q)) throw new InvalidOperationException(); - ExportPoint(ref q, out publicPoint); + return ExportPoint(ref q); } #endif @@ -1865,52 +1865,44 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - public static bool ValidatePublicKeyFull(byte[] pk, int pkOff, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyFullExport(byte[] pk, int pkOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - return ValidatePublicKeyFull(pk.AsSpan(pkOff), out publicPoint); + return ValidatePublicKeyFullExport(pk.AsSpan(pkOff)); #else byte[] A = Copy(pk, pkOff, PublicKeySize); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - if (CheckPointOrderVar(ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + if (!CheckPointOrderVar(ref pA)) + return null; + + return ExportPoint(ref pA); #endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public static bool ValidatePublicKeyFull(ReadOnlySpan pk, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyFullExport(ReadOnlySpan pk) { Span A = stackalloc byte[PublicKeySize]; A.CopyFrom(pk); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - if (CheckPointOrderVar(ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + if (!CheckPointOrderVar(ref pA)) + return null; + + return ExportPoint(ref pA); } #endif @@ -1943,46 +1935,38 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - public static bool ValidatePublicKeyPartial(byte[] pk, int pkOff, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyPartialExport(byte[] pk, int pkOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - return ValidatePublicKeyPartial(pk.AsSpan(pkOff), out publicPoint); + return ValidatePublicKeyPartialExport(pk.AsSpan(pkOff)); #else byte[] A = Copy(pk, pkOff, PublicKeySize); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + return ExportPoint(ref pA); #endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public static bool ValidatePublicKeyPartial(ReadOnlySpan pk, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyPartialExport(ReadOnlySpan pk) { Span A = stackalloc byte[PublicKeySize]; A.CopyFrom(pk); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + return ExportPoint(ref pA); } #endif diff --git a/crypto/src/math/ec/rfc8032/Ed448.cs b/crypto/src/math/ec/rfc8032/Ed448.cs index c592fcdcc..22fe79960 100644 --- a/crypto/src/math/ec/rfc8032/Ed448.cs +++ b/crypto/src/math/ec/rfc8032/Ed448.cs @@ -414,13 +414,13 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - private static void ExportPoint(ref PointAffine p, out PublicPoint publicPoint) + private static PublicPoint ExportPoint(ref PointAffine p) { uint[] data = new uint[F.Size * 2]; F.Copy(p.x, 0, data, 0); F.Copy(p.y, 0, data, F.Size); - publicPoint = new PublicPoint(data); + return new PublicPoint(data); } public static void GeneratePrivateKey(SecureRandom random, byte[] k) @@ -475,10 +475,10 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - public static void GeneratePublicKey(byte[] sk, int skOff, out PublicPoint publicPoint) + public static PublicPoint GeneratePublicKey(byte[] sk, int skOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - GeneratePublicKey(sk.AsSpan(skOff), out publicPoint); + return GeneratePublicKey(sk.AsSpan(skOff)); #else IXof d = CreateXof(); byte[] h = new byte[ScalarBytes * 2]; @@ -498,12 +498,12 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 if (0 == CheckPoint(ref q)) throw new InvalidOperationException(); - ExportPoint(ref q, out publicPoint); + return ExportPoint(ref q); #endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public static void GeneratePublicKey(ReadOnlySpan sk, out PublicPoint publicPoint) + public static PublicPoint GeneratePublicKey(ReadOnlySpan sk) { IXof d = CreateXof(); Span h = stackalloc byte[ScalarBytes * 2]; @@ -523,7 +523,7 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 if (0 == CheckPoint(ref q)) throw new InvalidOperationException(); - ExportPoint(ref q, out publicPoint); + return ExportPoint(ref q); } #endif @@ -1714,52 +1714,44 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - public static bool ValidatePublicKeyFull(byte[] pk, int pkOff, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyFullExport(byte[] pk, int pkOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - return ValidatePublicKeyFull(pk.AsSpan(pkOff), out publicPoint); + return ValidatePublicKeyFullExport(pk.AsSpan(pkOff)); #else byte[] A = Copy(pk, pkOff, PublicKeySize); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - if (CheckPointOrderVar(ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + if (!CheckPointOrderVar(ref pA)) + return null; + + return ExportPoint(ref pA); #endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public static bool ValidatePublicKeyFull(ReadOnlySpan pk, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyFullExport(ReadOnlySpan pk) { Span A = stackalloc byte[PublicKeySize]; A.CopyFrom(pk); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - if (CheckPointOrderVar(ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + if (!CheckPointOrderVar(ref pA)) + return null; + + return ExportPoint(ref pA); } #endif @@ -1792,46 +1784,38 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 } #endif - public static bool ValidatePublicKeyPartial(byte[] pk, int pkOff, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyPartialExport(byte[] pk, int pkOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - return ValidatePublicKeyPartial(pk.AsSpan(pkOff), out publicPoint); + return ValidatePublicKeyPartialExport(pk.AsSpan(pkOff)); #else byte[] A = Copy(pk, pkOff, PublicKeySize); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + return ExportPoint(ref pA); #endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public static bool ValidatePublicKeyPartial(ReadOnlySpan pk, out PublicPoint publicPoint) + public static PublicPoint ValidatePublicKeyPartialExport(ReadOnlySpan pk) { Span A = stackalloc byte[PublicKeySize]; A.CopyFrom(pk); - if (CheckPointFullVar(A)) - { - Init(out PointAffine pA); - if (DecodePointVar(A, false, ref pA)) - { - ExportPoint(ref pA, out publicPoint); - return true; - } - } + if (!CheckPointFullVar(A)) + return null; - publicPoint = null; - return false; + Init(out PointAffine pA); + if (!DecodePointVar(A, false, ref pA)) + return null; + + return ExportPoint(ref pA); } #endif -- cgit 1.4.1