internal byte[] PrivateToASN1() { ASN1Builder builder = new ASN1Builder(); PrivateToASN1(builder); return(builder.ToByteArray()); }
internal byte[] PublicToASN1(bool x509) { ASN1Builder builder = new ASN1Builder(); PublicToASN1(builder, x509); return(builder.ToByteArray()); }
// Add a container to this builder. public ASN1Builder AddContainer(ASN1Type type) { ASN1Builder container = new ASN1Builder(type); list.Add(container); return(container); }
// Convert DSA private parameters into an ASN.1 buffer. internal void PrivateToASN1(ASN1Builder builder) { builder.AddBigInt(P); builder.AddBigInt(Q); builder.AddBigInt(G); builder.AddBigInt(Y); builder.AddBigInt(J); builder.AddBigInt(X); builder.AddBigInt(Seed); builder.AddInt32(Counter); }
// Convert RSA private parameters into an ASN.1 buffer. internal void PrivateToASN1(ASN1Builder builder) { builder.AddInt32(0); builder.AddBigInt(Modulus); builder.AddBigInt(Exponent); builder.AddBigInt(D); builder.AddBigInt(P); builder.AddBigInt(Q); builder.AddBigInt(DP); builder.AddBigInt(DQ); builder.AddBigInt(InverseQ); }
// Convert RSA public parameters into an ASN.1 buffer. internal void PublicToASN1(ASN1Builder builder, bool x509) { if (x509) { // Output an X.509 "SubjectPublicKeyInfo" block. ASN1Builder alg = builder.AddSequence(); alg.AddObjectIdentifier(rsaID); alg.AddNull(); ASN1Builder bitString = builder.AddBitStringContents(); ASN1Builder inner = bitString.AddSequence(); inner.AddBigInt(Modulus); inner.AddBigInt(Exponent); } else { // Output a bare list of RSA parameters. builder.AddBigInt(Modulus); builder.AddBigInt(Exponent); } }
// Convert DSA public parameters into an ASN.1 buffer. internal void PublicToASN1(ASN1Builder builder, bool x509) { if (x509) { // Output an X.509 "SubjectPublicKeyInfo" block. ASN1Builder alg = builder.AddSequence(); alg.AddObjectIdentifier(dsaID); ASN1Builder inner = alg.AddSequence(); inner.AddBigInt(P); inner.AddBigInt(Q); inner.AddBigInt(G); ASN1Builder bitString = builder.AddBitStringContents(); bitString.AddBigInt(Y); } else { // Output the raw public parameters. builder.AddBigInt(P); builder.AddBigInt(Q); builder.AddBigInt(G); builder.AddBigInt(Y); } }
internal byte[] PrivateToASN1() { ASN1Builder builder = new ASN1Builder(); PrivateToASN1(builder); return builder.ToByteArray(); }
internal byte[] PublicToASN1(bool x509) { ASN1Builder builder = new ASN1Builder(); PublicToASN1(builder, x509); return builder.ToByteArray(); }
// Convert RSA public parameters into an ASN.1 buffer. internal void PublicToASN1(ASN1Builder builder, bool x509) { if(x509) { // Output an X.509 "SubjectPublicKeyInfo" block. ASN1Builder alg = builder.AddSequence(); alg.AddObjectIdentifier(rsaID); alg.AddNull(); ASN1Builder bitString = builder.AddBitStringContents(); ASN1Builder inner = bitString.AddSequence(); inner.AddBigInt(Modulus); inner.AddBigInt(Exponent); } else { // Output a bare list of RSA parameters. builder.AddBigInt(Modulus); builder.AddBigInt(Exponent); } }
// Create a DSA signature for the specified data. public override byte[] CreateSignature(byte[] rgbHash) { // Validate the parameter. if (rgbHash == null) { throw new ArgumentNullException("rgbHash"); } // Check that we have sufficient DSA parameters to sign. if (dsaParams.G == null) { throw new CryptographicException (_("Crypto_DSAParamsNotSet")); } else if (dsaParams.X == null) { throw new CryptographicException (_("Crypto_CannotSignWithPublic")); } // Generate a random K less than Q to use in // signature generation. We guarantee less than // by setting the high byte of K to at least one // less than the high byte of Q. int len = dsaParams.Q.Length; byte[] K = new byte [len]; CryptoMethods.GenerateRandom(K, 1, K.Length - 1); int index = 0; while (index < len && K[index] >= dsaParams.Q[index]) { if (dsaParams.Q[index] == 0) { K[index] = (byte)0; ++index; } else { K[index] = (byte)(dsaParams.Q[index] - 1); break; } } // Compute R = ((G^K mod P) mod Q) byte[] temp1 = CryptoMethods.NumPow (dsaParams.G, K, dsaParams.P); byte[] R = CryptoMethods.NumMod(temp1, dsaParams.Q); Array.Clear(temp1, 0, temp1.Length); // Compute S = ((K^-1 * (hash + X * R)) mod Q) temp1 = CryptoMethods.NumInv(K, dsaParams.Q); byte[] temp2 = CryptoMethods.NumMul (dsaParams.X, R, dsaParams.Q); byte[] temp3 = CryptoMethods.NumAdd (rgbHash, temp2, dsaParams.Q); byte[] S = CryptoMethods.NumMul(temp1, temp3, dsaParams.Q); Array.Clear(temp1, 0, temp1.Length); Array.Clear(temp2, 0, temp2.Length); Array.Clear(temp3, 0, temp3.Length); Array.Clear(K, 0, K.Length); // Pack R and S into a signature blob and return it. ASN1Builder builder = new ASN1Builder(); builder.AddBigInt(R); builder.AddBigInt(S); byte[] sig = builder.ToByteArray(); Array.Clear(R, 0, R.Length); Array.Clear(S, 0, S.Length); return(sig); }
// Add a container to this builder. public ASN1Builder AddContainer(ASN1Type type) { ASN1Builder container = new ASN1Builder(type); list.Add(container); return container; }
// Convert DSA public parameters into an ASN.1 buffer. internal void PublicToASN1(ASN1Builder builder, bool x509) { if(x509) { // Output an X.509 "SubjectPublicKeyInfo" block. ASN1Builder alg = builder.AddSequence(); alg.AddObjectIdentifier(dsaID); ASN1Builder inner = alg.AddSequence(); inner.AddBigInt(P); inner.AddBigInt(Q); inner.AddBigInt(G); ASN1Builder bitString = builder.AddBitStringContents(); bitString.AddBigInt(Y); } else { // Output the raw public parameters. builder.AddBigInt(P); builder.AddBigInt(Q); builder.AddBigInt(G); builder.AddBigInt(Y); } }
// Create a DSA signature for the specified data. public override byte[] CreateSignature(byte[] rgbHash) { // Validate the parameter. if(rgbHash == null) { throw new ArgumentNullException("rgbHash"); } // Check that we have sufficient DSA parameters to sign. if(dsaParams.G == null) { throw new CryptographicException (_("Crypto_DSAParamsNotSet")); } else if(dsaParams.X == null) { throw new CryptographicException (_("Crypto_CannotSignWithPublic")); } // Generate a random K less than Q to use in // signature generation. We guarantee less than // by setting the high byte of K to at least one // less than the high byte of Q. int len = dsaParams.Q.Length; byte[] K = new byte [len]; CryptoMethods.GenerateRandom(K, 1, K.Length - 1); int index = 0; while(index < len && K[index] >= dsaParams.Q[index]) { if(dsaParams.Q[index] == 0) { K[index] = (byte)0; ++index; } else { K[index] = (byte)(dsaParams.Q[index] - 1); break; } } // Compute R = ((G^K mod P) mod Q) byte[] temp1 = CryptoMethods.NumPow (dsaParams.G, K, dsaParams.P); byte[] R = CryptoMethods.NumMod(temp1, dsaParams.Q); Array.Clear(temp1, 0, temp1.Length); // Compute S = ((K^-1 * (hash + X * R)) mod Q) temp1 = CryptoMethods.NumInv(K, dsaParams.Q); byte[] temp2 = CryptoMethods.NumMul (dsaParams.X, R, dsaParams.Q); byte[] temp3 = CryptoMethods.NumAdd (rgbHash, temp2, dsaParams.Q); byte[] S = CryptoMethods.NumMul(temp1, temp3, dsaParams.Q); Array.Clear(temp1, 0, temp1.Length); Array.Clear(temp2, 0, temp2.Length); Array.Clear(temp3, 0, temp3.Length); Array.Clear(K, 0, K.Length); // Pack R and S into a signature blob and return it. ASN1Builder builder = new ASN1Builder(); builder.AddBigInt(R); builder.AddBigInt(S); byte[] sig = builder.ToByteArray(); Array.Clear(R, 0, R.Length); Array.Clear(S, 0, S.Length); return sig; }