diff options
author | David Hook <dgh@cryptoworkshop.com> | 2022-07-24 19:46:56 +1000 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2022-07-24 19:46:56 +1000 |
commit | 060b2b6c2fa5c41eb774db896abb2f6d282a48e4 (patch) | |
tree | 5df8121f24cf43ebca16a9ecfca72501e48e1ba7 /crypto/src/pqc | |
parent | fixed exposure of engine class (diff) | |
download | BouncyCastle.NET-ed25519-060b2b6c2fa5c41eb774db896abb2f6d282a48e4.tar.xz |
moved ntrup to ntruprime, reduced accessors on keys, marked key fields internal
Diffstat (limited to 'crypto/src/pqc')
13 files changed, 159 insertions, 162 deletions
diff --git a/crypto/src/pqc/crypto/ntrup/NtruPKeyParameters.cs b/crypto/src/pqc/crypto/ntrup/NtruPKeyParameters.cs deleted file mode 100644 index 96bd4ca8e..000000000 --- a/crypto/src/pqc/crypto/ntrup/NtruPKeyParameters.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Org.BouncyCastle.Crypto; - -namespace Org.BouncyCastle.pqc.crypto.NtruP -{ - public class NtruPKeyParameters : AsymmetricKeyParameter - { - private NtruPParameters _pParameters; - - public NtruPKeyParameters(bool isPrivate, NtruPParameters pParameters) : base(isPrivate) - { - this._pParameters = pParameters; - } - - public NtruPParameters PParameters => _pParameters; - - } -} \ No newline at end of file diff --git a/crypto/src/pqc/crypto/ntrup/NtruPParameters.cs b/crypto/src/pqc/crypto/ntrup/NtruPParameters.cs deleted file mode 100644 index 82c7c8e13..000000000 --- a/crypto/src/pqc/crypto/ntrup/NtruPParameters.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.ComponentModel; -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Crypto.Modes; - -namespace Org.BouncyCastle.pqc.crypto.NtruP -{ - public class NtruPParameters : ICipherParameters - { - - private String name; - private int p; - private int q; - private int _roundedBytes; - private bool LPR; - private int _w; - private int _rqBytes; - private int _tau0; - private int _tau1; - private int _tau2; - private int _tau3; - private int _skBytes; - private int _pkBytes; - private int _ctBytes; - private NtruPEngine _pEngine; - - public NtruPParameters(String name, int p, int q, bool LPR, int w, int tau0, - int tau1, int tau2, int tau3, int skBytes, int pkBytes, int ctBytes, int roundedBytes, int rqBytes) - { - this.name = name; - this.p = p; - this.q = q; - this.LPR = LPR; - this._w = w; - this._tau0 = tau0; - this._tau1 = tau1; - this._tau2 = tau2; - this._tau3 = tau3; - - // KEM Parameters - this._roundedBytes = roundedBytes; - this._rqBytes = rqBytes; - this._skBytes = skBytes; - this._pkBytes = pkBytes; - this._ctBytes = ctBytes; - this._pEngine = new NtruPEngine(p,q, LPR, w, tau0, tau1, tau2, tau3, skBytes, pkBytes, ctBytes, roundedBytes, rqBytes); - } - - public static NtruPParameters ntrulpr653 = new NtruPParameters("NTRU_LPRime_653", 653, 4621, true, 252, 2175,113,2031,290,1125,897,1025, 865, -1); - public static NtruPParameters ntrulpr761 = new NtruPParameters("NTRU_LPRime_761", 761, 4591, true, 250, 2156,114,2007,287,1294,1039,1167, 1007, -1); - public static NtruPParameters ntrulpr857 = new NtruPParameters("NTRU_LPRime_857", 857, 5167, true, 281, 2433,101,2265,324,1463,1184,1312, 1152, -1); - public static NtruPParameters ntrulpr953 = new NtruPParameters("NTRU_LPRime_953", 953, 6343, true, 345, 2997,82,2798,400,1652,1349,1477, 1317, -1); - public static NtruPParameters ntrulpr1013 = new NtruPParameters("NTRU_LPRime_1013", 1013, 7177, true, 392, 3367,73,3143,449,1773,1455,1583, 1423, -1); - public static NtruPParameters ntrulpr1277 = new NtruPParameters("NTRU_LPRime_1277", 1277, 7879, true, 429, 3724,66,3469,496,2231,1847,1975, 1815, -1); - - public static NtruPParameters sntrup653 = new NtruPParameters("SNTRU_Prime_653", 653, 4621, false, 288, -1,-1,-1,-1,1518,994,897, 865, 994); - public static NtruPParameters sntrup761 = new NtruPParameters("SNTRU_Prime_761", 761, 4591, false, 286, -1,-1,-1,-1,1763,1158,1039, 1007, 1158); - public static NtruPParameters sntrup857 = new NtruPParameters("SNTRU_Prime_857", 857, 5167, false, 322, -1,-1,-1,-1,1999,1322,1184, 1152, 1322); - public static NtruPParameters sntrup953 = new NtruPParameters("SNTRU_Prime_953", 953, 6343, false, 396, -1,-1,-1,-1,2254,1505,1349, 1317, 1505); - public static NtruPParameters sntrup1013 = new NtruPParameters("SNTRU_Prime_1013", 1013, 7177, false, 448, -1,-1,-1,-1,2417,1623,1455, 1423, 1623); - public static NtruPParameters sntrup1277 = new NtruPParameters("SNTRU_Prime_1277", 1277, 7879, false, 492, -1,-1,-1,-1,3059,2067,1847, 1815, 2067); - - public int P => p; - public bool lpr => LPR; - - public int Q => q; - - internal NtruPEngine PEngine => _pEngine; - - } -} \ No newline at end of file diff --git a/crypto/src/pqc/crypto/ntrup/NtruPPrivateKeyParameters.cs b/crypto/src/pqc/crypto/ntrup/NtruPPrivateKeyParameters.cs deleted file mode 100644 index ff70aa241..000000000 --- a/crypto/src/pqc/crypto/ntrup/NtruPPrivateKeyParameters.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.pqc.crypto.NtruP -{ - public class NtruPPrivateKeyParameters : NtruPKeyParameters - { - private byte[] privKey; - public byte[] PrivateKey => Arrays.Clone(privKey); - - public NtruPPrivateKeyParameters(NtruPParameters pParameters, byte[] privKey) : base(true, pParameters) - { - this.privKey = Arrays.Clone(privKey); - } - - public byte[] GetEncoded() - { - return PrivateKey; - } - } -} \ No newline at end of file diff --git a/crypto/src/pqc/crypto/ntrup/NtruPPublicKeyParameters.cs b/crypto/src/pqc/crypto/ntrup/NtruPPublicKeyParameters.cs deleted file mode 100644 index c7a00d9f3..000000000 --- a/crypto/src/pqc/crypto/ntrup/NtruPPublicKeyParameters.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.pqc.crypto.NtruP -{ - public class NtruPPublicKeyParameters : NtruPKeyParameters - { - public byte[] pubKey; - - public byte[] PublicKey => Arrays.Clone(pubKey); - - public byte[] GetEncoded() - { - return PublicKey; - } - - public NtruPPublicKeyParameters(NtruPParameters pParameters, byte[] pubKey) : base(false,pParameters) - { - this.pubKey = Arrays.Clone(pubKey); - } - } -} \ No newline at end of file diff --git a/crypto/src/pqc/crypto/ntrup/NtruPEngine.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeEngine.cs index f78abbf7c..701a53cee 100644 --- a/crypto/src/pqc/crypto/ntrup/NtruPEngine.cs +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeEngine.cs @@ -8,9 +8,9 @@ using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Parameters; -namespace Org.BouncyCastle.pqc.crypto.NtruP +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime { - internal class NtruPEngine + internal class NtruPrimeEngine { // Key Sizes private readonly int _skBytes; // [KEM] Size of secret key @@ -53,7 +53,7 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP public int CipherTextSize => _ctBytes; public int SessionKeySize => SessionKeyBytes; - public NtruPEngine(int p, int q, bool lpr, int w, int tau0, + public NtruPrimeEngine(int p, int q, bool lpr, int w, int tau0, int tau1, int tau2, int tau3, int skBytes, int pkBytes, int ctBytes, int roundedBytes, int rqBytes) { this._p = p; @@ -1281,4 +1281,4 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP } } -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/ntrup/NtruPKemExtractor.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKemExtractor.cs index 6149e1112..9e3c65b10 100644 --- a/crypto/src/pqc/crypto/ntrup/NtruPKemExtractor.cs +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKemExtractor.cs @@ -1,20 +1,20 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Pqc.Crypto.Frodo; -namespace Org.BouncyCastle.pqc.crypto.NtruP +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime { - public class NtruPKEMExtractor : IEncapsulatedSecretExtractor + public class NtruPrimeKEMExtractor : IEncapsulatedSecretExtractor { - private NtruPEngine _pEngine; - private NtruPKeyParameters _pKey; + private NtruPrimeEngine _pEngine; + private NtruPrimeKeyParameters _pKey; - public NtruPKEMExtractor(NtruPKeyParameters privParams) + public NtruPrimeKEMExtractor(NtruPrimeKeyParameters privParams) { this._pKey = privParams; - InitCipher(_pKey.PParameters); + InitCipher(_pKey.Parameters); } - private void InitCipher(NtruPParameters param) + private void InitCipher(NtruPrimeParameters param) { _pEngine = param.PEngine; } @@ -22,7 +22,7 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP public byte[] ExtractSecret(byte[] encapsulation) { byte[] session_key = new byte[_pEngine.SessionKeySize]; - _pEngine.kem_dec(session_key, encapsulation, ((NtruPPrivateKeyParameters)_pKey).PrivateKey); + _pEngine.kem_dec(session_key, encapsulation, ((NtruPrimePrivateKeyParameters)_pKey).privKey); return session_key; } @@ -32,4 +32,4 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP } } -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/ntrup/NtruPKemGenerator.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKemGenerator.cs index 0fb2633e8..9c2e0461b 100644 --- a/crypto/src/pqc/crypto/ntrup/NtruPKemGenerator.cs +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKemGenerator.cs @@ -3,25 +3,25 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; -namespace Org.BouncyCastle.pqc.crypto.NtruP +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime { - public class NtruPKemGenerator : IEncapsulatedSecretGenerator + public class NtruPrimeKemGenerator : IEncapsulatedSecretGenerator { private SecureRandom sr; - public NtruPKemGenerator(SecureRandom sr) + public NtruPrimeKemGenerator(SecureRandom sr) { this.sr = sr; } public ISecretWithEncapsulation GenerateEncapsulated(AsymmetricKeyParameter recipientKey) { - NtruPPublicKeyParameters key = (NtruPPublicKeyParameters)recipientKey; - NtruPEngine pEngine = key.PParameters.PEngine; + NtruPrimePublicKeyParameters key = (NtruPrimePublicKeyParameters)recipientKey; + NtruPrimeEngine pEngine = key.Parameters.PEngine; byte[] cipherText = new byte[pEngine.CipherTextSize]; byte[] sessionKey = new byte[pEngine.SessionKeySize]; - pEngine.kem_enc(cipherText, sessionKey,key.PublicKey, sr); - return new NtruPKemGenerator.SecretWithEncapsulationImpl(sessionKey, cipherText); + pEngine.kem_enc(cipherText, sessionKey,key.pubKey, sr); + return new NtruPrimeKemGenerator.SecretWithEncapsulationImpl(sessionKey, cipherText); } public class SecretWithEncapsulationImpl : ISecretWithEncapsulation @@ -74,4 +74,4 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP } } -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/ntrup/NtruPKeyGenerationParameters.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyGenerationParameters.cs index 7f96dfce6..56e7315ae 100644 --- a/crypto/src/pqc/crypto/ntrup/NtruPKeyGenerationParameters.cs +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyGenerationParameters.cs @@ -1,18 +1,18 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; -namespace Org.BouncyCastle.pqc.crypto.NtruP +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime { public class NtruKeyGenerationParameters : KeyGenerationParameters { - private NtruPParameters _pParameters; + private NtruPrimeParameters _pParameters; - public NtruKeyGenerationParameters(SecureRandom random, NtruPParameters ntruPParameters) : base(random,256) + public NtruKeyGenerationParameters(SecureRandom random, NtruPrimeParameters ntruPParameters) : base(random,256) { this._pParameters = ntruPParameters; } - public NtruPParameters PParameters => _pParameters; + public NtruPrimeParameters PParameters => _pParameters; } -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/ntrup/NtruPKeyPairGenerator.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyPairGenerator.cs index fbbd66281..eff4828f8 100644 --- a/crypto/src/pqc/crypto/ntrup/NtruPKeyPairGenerator.cs +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyPairGenerator.cs @@ -1,7 +1,7 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; -namespace Org.BouncyCastle.pqc.crypto.NtruP +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime { public class NtruKeyPairGenerator { @@ -26,13 +26,13 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP private AsymmetricCipherKeyPair GenKeyPair() { - NtruPEngine pEngine = ntruParams.PParameters.PEngine; + NtruPrimeEngine pEngine = ntruParams.PParameters.PEngine; byte[] sk = new byte[pEngine.PrivateKeySize]; byte[] pk = new byte[pEngine.PublicKeySize]; pEngine.kem_keypair( pk,sk,random); - NtruPPublicKeyParameters pubKey = new NtruPPublicKeyParameters(ntruParams.PParameters, pk); - NtruPPrivateKeyParameters privKey = new NtruPPrivateKeyParameters(ntruParams.PParameters, sk); + NtruPrimePublicKeyParameters pubKey = new NtruPrimePublicKeyParameters(ntruParams.PParameters, pk); + NtruPrimePrivateKeyParameters privKey = new NtruPrimePrivateKeyParameters(ntruParams.PParameters, sk); return new AsymmetricCipherKeyPair(pubKey, privKey); } @@ -66,4 +66,4 @@ namespace Org.BouncyCastle.pqc.crypto.NtruP // } } -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyParameters.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyParameters.cs new file mode 100644 index 000000000..fb77d8567 --- /dev/null +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeKeyParameters.cs @@ -0,0 +1,17 @@ +using Org.BouncyCastle.Crypto; + +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime +{ + public class NtruPrimeKeyParameters : AsymmetricKeyParameter + { + private NtruPrimeParameters _pParameters; + + public NtruPrimeKeyParameters(bool isPrivate, NtruPrimeParameters pParameters) : base(isPrivate) + { + this._pParameters = pParameters; + } + + public NtruPrimeParameters Parameters => _pParameters; + + } +} diff --git a/crypto/src/pqc/crypto/ntruprime/NtruPrimeParameters.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimeParameters.cs new file mode 100644 index 000000000..3cf691565 --- /dev/null +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimeParameters.cs @@ -0,0 +1,72 @@ +using System; +using System.ComponentModel; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Digests; +using Org.BouncyCastle.Crypto.Modes; + +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime +{ + public class NtruPrimeParameters : ICipherParameters + { + + private String name; + private int p; + private int q; + private int _roundedBytes; + private bool LPR; + private int _w; + private int _rqBytes; + private int _tau0; + private int _tau1; + private int _tau2; + private int _tau3; + private int _skBytes; + private int _pkBytes; + private int _ctBytes; + private NtruPrimeEngine _pEngine; + + public NtruPrimeParameters(String name, int p, int q, bool LPR, int w, int tau0, + int tau1, int tau2, int tau3, int skBytes, int pkBytes, int ctBytes, int roundedBytes, int rqBytes) + { + this.name = name; + this.p = p; + this.q = q; + this.LPR = LPR; + this._w = w; + this._tau0 = tau0; + this._tau1 = tau1; + this._tau2 = tau2; + this._tau3 = tau3; + + // KEM Parameters + this._roundedBytes = roundedBytes; + this._rqBytes = rqBytes; + this._skBytes = skBytes; + this._pkBytes = pkBytes; + this._ctBytes = ctBytes; + this._pEngine = new NtruPrimeEngine(p,q, LPR, w, tau0, tau1, tau2, tau3, skBytes, pkBytes, ctBytes, roundedBytes, rqBytes); + } + + public static NtruPrimeParameters ntrulpr653 = new NtruPrimeParameters("NTRU_LPRime_653", 653, 4621, true, 252, 2175,113,2031,290,1125,897,1025, 865, -1); + public static NtruPrimeParameters ntrulpr761 = new NtruPrimeParameters("NTRU_LPRime_761", 761, 4591, true, 250, 2156,114,2007,287,1294,1039,1167, 1007, -1); + public static NtruPrimeParameters ntrulpr857 = new NtruPrimeParameters("NTRU_LPRime_857", 857, 5167, true, 281, 2433,101,2265,324,1463,1184,1312, 1152, -1); + public static NtruPrimeParameters ntrulpr953 = new NtruPrimeParameters("NTRU_LPRime_953", 953, 6343, true, 345, 2997,82,2798,400,1652,1349,1477, 1317, -1); + public static NtruPrimeParameters ntrulpr1013 = new NtruPrimeParameters("NTRU_LPRime_1013", 1013, 7177, true, 392, 3367,73,3143,449,1773,1455,1583, 1423, -1); + public static NtruPrimeParameters ntrulpr1277 = new NtruPrimeParameters("NTRU_LPRime_1277", 1277, 7879, true, 429, 3724,66,3469,496,2231,1847,1975, 1815, -1); + + public static NtruPrimeParameters sntrup653 = new NtruPrimeParameters("SNTRU_Prime_653", 653, 4621, false, 288, -1,-1,-1,-1,1518,994,897, 865, 994); + public static NtruPrimeParameters sntrup761 = new NtruPrimeParameters("SNTRU_Prime_761", 761, 4591, false, 286, -1,-1,-1,-1,1763,1158,1039, 1007, 1158); + public static NtruPrimeParameters sntrup857 = new NtruPrimeParameters("SNTRU_Prime_857", 857, 5167, false, 322, -1,-1,-1,-1,1999,1322,1184, 1152, 1322); + public static NtruPrimeParameters sntrup953 = new NtruPrimeParameters("SNTRU_Prime_953", 953, 6343, false, 396, -1,-1,-1,-1,2254,1505,1349, 1317, 1505); + public static NtruPrimeParameters sntrup1013 = new NtruPrimeParameters("SNTRU_Prime_1013", 1013, 7177, false, 448, -1,-1,-1,-1,2417,1623,1455, 1423, 1623); + public static NtruPrimeParameters sntrup1277 = new NtruPrimeParameters("SNTRU_Prime_1277", 1277, 7879, false, 492, -1,-1,-1,-1,3059,2067,1847, 1815, 2067); + + public int P => p; + public bool lpr => LPR; + + public int Q => q; + + internal NtruPrimeEngine PEngine => _pEngine; + + } +} diff --git a/crypto/src/pqc/crypto/ntruprime/NtruPrimePrivateKeyParameters.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimePrivateKeyParameters.cs new file mode 100644 index 000000000..69cfc4744 --- /dev/null +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimePrivateKeyParameters.cs @@ -0,0 +1,20 @@ +using System; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime +{ + public class NtruPrimePrivateKeyParameters : NtruPrimeKeyParameters + { + internal byte[] privKey; + + public NtruPrimePrivateKeyParameters(NtruPrimeParameters pParameters, byte[] privKey) : base(true, pParameters) + { + this.privKey = Arrays.Clone(privKey); + } + + public byte[] GetEncoded() + { + return Arrays.Clone(privKey); + } + } +} diff --git a/crypto/src/pqc/crypto/ntruprime/NtruPrimePublicKeyParameters.cs b/crypto/src/pqc/crypto/ntruprime/NtruPrimePublicKeyParameters.cs new file mode 100644 index 000000000..265382455 --- /dev/null +++ b/crypto/src/pqc/crypto/ntruprime/NtruPrimePublicKeyParameters.cs @@ -0,0 +1,19 @@ +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime +{ + public class NtruPrimePublicKeyParameters : NtruPrimeKeyParameters + { + internal byte[] pubKey; + + public byte[] GetEncoded() + { + return Arrays.Clone(pubKey); + } + + public NtruPrimePublicKeyParameters(NtruPrimeParameters pParameters, byte[] pubKey) : base(false,pParameters) + { + this.pubKey = Arrays.Clone(pubKey); + } + } +} |