diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-04 17:34:24 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-04 17:34:24 +0700 |
commit | 189b49dc3f2087de852757832435c4dc11aa4e25 (patch) | |
tree | 0e2053889c16934ab52b483d5b437f90f00efabe /crypto/src | |
parent | BIKE refactoring (diff) | |
download | BouncyCastle.NET-ed25519-189b49dc3f2087de852757832435c4dc11aa4e25.tar.xz |
Length property and internal Span accessors
Diffstat (limited to 'crypto/src')
-rw-r--r-- | crypto/src/crypto/engines/AesEngine_X86.cs | 14 | ||||
-rw-r--r-- | crypto/src/crypto/engines/AsconEngine.cs | 17 | ||||
-rw-r--r-- | crypto/src/crypto/parameters/KeyParameter.cs | 6 | ||||
-rw-r--r-- | crypto/src/crypto/parameters/ParametersWithIV.cs | 6 |
4 files changed, 36 insertions, 7 deletions
diff --git a/crypto/src/crypto/engines/AesEngine_X86.cs b/crypto/src/crypto/engines/AesEngine_X86.cs index 51080ec52..ba1d77ec7 100644 --- a/crypto/src/crypto/engines/AesEngine_X86.cs +++ b/crypto/src/crypto/engines/AesEngine_X86.cs @@ -18,7 +18,7 @@ namespace Org.BouncyCastle.Crypto.Engines { public static bool IsSupported => Aes.IsSupported; - private static Vector128<byte>[] CreateRoundKeys(byte[] key, bool forEncryption) + private static Vector128<byte>[] CreateRoundKeys(ReadOnlySpan<byte> key, bool forEncryption) { Vector128<byte>[] K; @@ -30,7 +30,7 @@ namespace Org.BouncyCastle.Crypto.Engines K = new Vector128<byte>[11]; - var s = Load128(key.AsSpan(0, 16)); + var s = Load128(key[..16]); K[0] = s; for (int round = 0; round < 10;) @@ -49,8 +49,8 @@ namespace Org.BouncyCastle.Crypto.Engines { K = new Vector128<byte>[13]; - var s1 = Load128(key.AsSpan(0, 16)); - var s2 = Load64(key.AsSpan(16, 8)).ToVector128(); + var s1 = Load128(key[..16]); + var s2 = Load64(key[16..24]).ToVector128(); K[0] = s1; byte rcon = 0x01; @@ -95,8 +95,8 @@ namespace Org.BouncyCastle.Crypto.Engines { K = new Vector128<byte>[15]; - var s1 = Load128(key.AsSpan(0, 16)); - var s2 = Load128(key.AsSpan(16, 16)); + var s1 = Load128(key[..16]); + var s2 = Load128(key[16..32]); K[0] = s1; K[1] = s2; @@ -163,7 +163,7 @@ namespace Org.BouncyCastle.Crypto.Engines throw new ArgumentException("invalid type: " + Platform.GetTypeName(parameters), nameof(parameters)); } - m_roundKeys = CreateRoundKeys(keyParameter.GetKey(), forEncryption); + m_roundKeys = CreateRoundKeys(keyParameter.Key, forEncryption); if (m_roundKeys.Length == 11) { diff --git a/crypto/src/crypto/engines/AsconEngine.cs b/crypto/src/crypto/engines/AsconEngine.cs index 870d0e57f..3ca5ede60 100644 --- a/crypto/src/crypto/engines/AsconEngine.cs +++ b/crypto/src/crypto/engines/AsconEngine.cs @@ -142,22 +142,39 @@ namespace Org.BouncyCastle.Crypto.Engines if (npub == null || npub.Length != CRYPTO_ABYTES) throw new ArgumentException(asconParameters + " requires exactly " + CRYPTO_ABYTES + " bytes of IV"); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + var k = key.Key; +#else byte[] k = key.GetKey(); +#endif + if (k.Length != CRYPTO_KEYBYTES) throw new ArgumentException(asconParameters + " key must be " + CRYPTO_KEYBYTES + " bytes long"); N0 = Pack.BE_To_UInt64(npub, 0); N1 = Pack.BE_To_UInt64(npub, 8); + if (CRYPTO_KEYBYTES == 16) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + K1 = Pack.BE_To_UInt64(k); + K2 = Pack.BE_To_UInt64(k[8..]); +#else K1 = Pack.BE_To_UInt64(k, 0); K2 = Pack.BE_To_UInt64(k, 8); +#endif } else if (CRYPTO_KEYBYTES == 20) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + K0 = Pack.BE_To_UInt32(k); + K1 = Pack.BE_To_UInt64(k[4..]); + K2 = Pack.BE_To_UInt64(k[12..]); +#else K0 = Pack.BE_To_UInt32(k, 0); K1 = Pack.BE_To_UInt64(k, 4); K2 = Pack.BE_To_UInt64(k, 12); +#endif } else { diff --git a/crypto/src/crypto/parameters/KeyParameter.cs b/crypto/src/crypto/parameters/KeyParameter.cs index 8d35a19f1..7ee47de9a 100644 --- a/crypto/src/crypto/parameters/KeyParameter.cs +++ b/crypto/src/crypto/parameters/KeyParameter.cs @@ -39,5 +39,11 @@ namespace Org.BouncyCastle.Crypto.Parameters { return (byte[])m_key.Clone(); } + + public int KeyLength => m_key.Length; + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + internal ReadOnlySpan<byte> Key => m_key; +#endif } } diff --git a/crypto/src/crypto/parameters/ParametersWithIV.cs b/crypto/src/crypto/parameters/ParametersWithIV.cs index ea1773d54..e642e828a 100644 --- a/crypto/src/crypto/parameters/ParametersWithIV.cs +++ b/crypto/src/crypto/parameters/ParametersWithIV.cs @@ -49,6 +49,12 @@ namespace Org.BouncyCastle.Crypto.Parameters return (byte[])m_iv.Clone(); } + public int IVLength => m_iv.Length; + public ICipherParameters Parameters => m_parameters; + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + internal ReadOnlySpan<byte> IV => m_iv; +#endif } } |