/// <summary> /// Constructs a new key pair from a byte array /// </summary> /// /// <param name="KeyPair">An encoded key pair</param> public NTRUKeyPair(byte[] KeyPair) { using (MemoryStream keyStream = new MemoryStream(KeyPair)) { _publicKey = new NTRUPublicKey(keyStream); _privateKey = new NTRUPrivateKey(keyStream); } }
/// <summary> /// Constructs a new key pair /// </summary> /// /// <param name="PublicKey">The Public key</param> /// <param name="PrivateKey">The Private Key</param> /// /// <exception cref="NTRUException">Thrown if an invalid key is used</exception> public NTRUKeyPair(IAsymmetricKey PublicKey, IAsymmetricKey PrivateKey) { if (!(PublicKey is NTRUPublicKey)) throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Public key!", new InvalidDataException()); if (!(PrivateKey is NTRUPrivateKey)) throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Private key!", new InvalidDataException()); _publicKey = (NTRUPublicKey)PublicKey; _privateKey = (NTRUPrivateKey)PrivateKey; }
/// <summary> /// Constructs a new key pair /// </summary> /// /// <param name="PublicKey">The Public key</param> /// <param name="PrivateKey">The Private Key</param> /// /// <exception cref="NTRUException">Thrown if an invalid key is used</exception> public NTRUKeyPair(IAsymmetricKey PublicKey, IAsymmetricKey PrivateKey) { if (!(PublicKey is NTRUPublicKey)) { throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Public key!", new InvalidDataException()); } if (!(PrivateKey is NTRUPrivateKey)) { throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Private key!", new InvalidDataException()); } _publicKey = (NTRUPublicKey)PublicKey; _privateKey = (NTRUPrivateKey)PrivateKey; }
/// <summary> /// Constructs a new key pair /// </summary> /// /// <param name="Key">The public or private key</param> /// /// <exception cref="NTRUException">Thrown if an invalid name is used</exception> public NTRUKeyPair(IAsymmetricKey Key) { if (Key is NTRUPublicKey) { _publicKey = (NTRUPublicKey)PublicKey; } else if (Key is NTRUPrivateKey) { _privateKey = (NTRUPrivateKey)PrivateKey; } else { throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU key!", new InvalidDataException()); } }
/// <summary> /// Generates a new encryption key pair /// </summary> /// /// <param name="RngF">The random number generator to use for generating the secret polynomial f</param> /// <param name="RngG">The random number generator to use for generating the secret polynomial g</param> /// /// <returns>A key pair</returns> private IAsymmetricKeyPair GenerateKeyPair(IRandom RngF, IRandom RngG) { int N = _encParams.N; int q = _encParams.Q; bool fastFp = _encParams.FastFp; bool sparse = _encParams.Sparse; TernaryPolynomialType polyType = _encParams.PolyType; IPolynomial t = null; IntegerPolynomial fq = null; IntegerPolynomial fp = null; IntegerPolynomial g = null; if (ParallelUtils.IsParallel && _isParallel) { Action[] gA = new Action[] { new Action(() => g = GenerateG(RngG)), new Action(() => GenerateFQ(RngF, out t, out fq, out fp)) }; Parallel.Invoke(gA); } else { // Choose a random g that is invertible mod q. g = GenerateG(RngG); // choose a random f that is invertible mod 3 and q GenerateFQ(RngF, out t, out fq, out fp); } // if fastFp=true, fp=1 if (fastFp) { fp = new IntegerPolynomial(N); fp.Coeffs[0] = 1; } IntegerPolynomial h = g.Multiply(fq, q); h.Mult3(q); h.EnsurePositive(q); NTRUPrivateKey priv = new NTRUPrivateKey(t, fp, N, q, sparse, fastFp, polyType); NTRUPublicKey pub = new NTRUPublicKey(h, N, q); return(new NTRUKeyPair(pub, priv)); }
/// <summary> /// Compare this object instance with another /// </summary> /// /// <param name="Obj">Object to compare</param> /// /// <returns>True if equal, otherwise false</returns> public override bool Equals(Object Obj) { if (this == Obj) { return(true); } if (Obj == null) { return(false); } NTRUPublicKey other = (NTRUPublicKey)Obj; if (N != other.N) { return(false); } if (H == null) { if (other.H != null) { return(false); } } else if (!H.Equals(other.H)) { return(false); } if (Q != other.Q) { return(false); } return(true); }
/// <summary> /// Constructs a new key pair /// </summary> /// /// <param name="Key">The public or private key</param> /// /// <exception cref="NTRUException">Thrown if an invalid name is used</exception> public NTRUKeyPair(IAsymmetricKey Key) { if (Key is NTRUPublicKey) _publicKey = (NTRUPublicKey)PublicKey; else if (Key is NTRUPrivateKey) _privateKey = (NTRUPrivateKey)PrivateKey; else throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU key!", new InvalidDataException()); }
/// <summary> /// Constructs a new key pair from an input stream /// </summary> /// /// <param name="KeyStream">An input stream containing an encoded key pair</param> public NTRUKeyPair(MemoryStream KeyStream) { _publicKey = new NTRUPublicKey(KeyStream); _privateKey = new NTRUPrivateKey(KeyStream); }
/// <summary> /// Generates a new encryption key pair /// </summary> /// /// <param name="RngF">The random number generator to use for generating the secret polynomial f</param> /// <param name="RngG">The random number generator to use for generating the secret polynomial g</param> /// /// <returns>A key pair</returns> private IAsymmetricKeyPair GenerateKeyPair(IRandom RngF, IRandom RngG) { int N = _ntruParams.N; int q = _ntruParams.Q; bool fastFp = _ntruParams.FastFp; bool sparse = _ntruParams.Sparse; TernaryPolynomialType polyType = _ntruParams.PolyType; IPolynomial t = null; IntegerPolynomial fq = null; IntegerPolynomial fp = null; IntegerPolynomial g = null; if (ParallelUtils.IsParallel && _isParallel) { Action[] gA = new Action[] { new Action(()=> g = GenerateG(RngG)), new Action(()=> GenerateFQ(RngF, out t, out fq, out fp)) }; Parallel.Invoke(gA); } else { // Choose a random g that is invertible mod q. g = GenerateG(RngG); // choose a random f that is invertible mod 3 and q GenerateFQ(RngF, out t, out fq, out fp); } // if fastFp=true, fp=1 if (fastFp) { fp = new IntegerPolynomial(N); fp.Coeffs[0] = 1; } IntegerPolynomial h = g.Multiply(fq, q); h.Mult3(q); h.EnsurePositive(q); NTRUPrivateKey priv = new NTRUPrivateKey(t, fp, N, q, sparse, fastFp, polyType); NTRUPublicKey pub = new NTRUPublicKey(h, N, q); return new NTRUKeyPair(pub, priv); }