/// <summary> /// Updates the instance with the given data. /// </summary> /// <param name="b">The b.</param> public void Update(byte b) { if (_signatureType == CanonicalTextDocument) { DoCanonicalUpdateByte(b); } else { _sig.Update(b); } }
public void Update(byte b) { if (signatureType == 1) { doCanonicalUpdateByte(b); } else { sig.Update(b); } }
public void Update( byte b) { if (signatureType == PgpSignature.CanonicalTextDocument) { doCanonicalUpdateByte(b); } else { sig.Update(b); } }
public override void WriteByte( byte b) { CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "SignatureStream"); signer.Update(b); }
public override void WriteByte(byte b) { if (outSigner != null) { outSigner.Update(b); } stream.WriteByte(b); }
/// <summary><inheritDoc/></summary> /// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> public virtual byte[] Sign(byte[] message) { String algorithm = hashAlgorithm + "with" + encryptionAlgorithm; ISigner sig = SignUtils.GetSignatureHelper(algorithm); sig.InitSign(pk); sig.Update(message); return(sig.GenerateSignature()); }
public override int ReadByte() { int num = stream.ReadByte(); if (inSigner != null && num >= 0) { inSigner.Update((byte)num); } return(num); }
public override void WriteByte(byte b) { try { sig.Update(b); } catch (SignatureException e) { throw new CmsStreamException("signature problem: " + e); } }
public override void WriteByte( byte b) { CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "SignerStream"); if (outSigner != null) { outSigner.Update(b); } stream.WriteByte(b); }
public override void WriteByte(byte b) { try { sig.Update(b); } catch (SignatureException ex) { throw new CmsStreamException(string.Concat((object)"signature problem: ", (object)ex)); } }
public override int ReadByte() { int b = stream.ReadByte(); if (inSigner != null) { if (b >= 0) { inSigner.Update((byte)b); } } return(b); }
public override int ReadByte() { CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "SignerStream"); int b = stream.ReadByte(); if (inSigner != null) { if (b >= 0) { inSigner.Update((byte)b); } } return(b); }
/** * signature with a "forged signature" (sig block not at end of plain text) */ private void DoTestBadSig( IAsymmetricKeyParameter priv, IAsymmetricKeyParameter pub) { IDigest sha1 = DigestUtilities.GetDigest("SHA1"); IBufferedCipher signer = CipherUtilities.GetCipher("RSA//PKCS1Padding"); signer.Init(true, priv); byte[] block = new byte[signer.GetBlockSize()]; sha1.Update((byte)0); byte[] sigHeader = Hex.Decode("3021300906052b0e03021a05000414"); Array.Copy(sigHeader, 0, block, 0, sigHeader.Length); // byte[] dig = sha1.digest(); byte[] dig = DigestUtilities.DoFinal(sha1); Array.Copy(dig, 0, block, sigHeader.Length, dig.Length); Array.Copy(sigHeader, 0, block, sigHeader.Length + dig.Length, sigHeader.Length); byte[] sig = signer.DoFinal(block); ISigner verifier = SignerUtilities.GetSigner("SHA1WithRSA"); verifier.Init(false, pub); verifier.Update((byte)0); if (verifier.VerifySignature(sig)) { Fail("bad signature passed"); } }
private void doUpdateByte( byte b) { sig.Update(b); dig.Update(b); }
public override void WriteByte(byte b) { s.Update(b); }
public static void Main(String[] args) { using (EzQuoteMain main = new EzQuoteMain()) { /*foreach (String tpm_name in main.ctx_.TPMClient.TPMDevices) * { * Console.WriteLine("TPM DEVICE {0}", tpm_name); * * }*/ TPMSession tpm0 = main.ctx_.TPMClient.SelectTPMDevice("ibm0"); IDictionary <string, string> opts = new Dictionary <string, string>(); opts.Add("file", "/tmp/mystore"); tpm0.Keystore = TPMKeystoreProviders.Create("SQLiteKeystore", opts); tpm0.SetRequestSecretCallback(mycallback); /* * ProtectedPasswordStorage pws = new ProtectedPasswordStorage(); * pws.AppendPasswordChar('i'); * pws.AppendPasswordChar('a'); * pws.AppendPasswordChar('i'); * pws.AppendPasswordChar('k'); * * tpm0.AdministrationClient.TakeOwnership(pws, pws); */ ClientKeyHandle kh_srk = tpm0.KeyClient.GetSrkKeyHandle(); ClientKeyHandle kh_sig1 = kh_srk.CreateKey("sigkey5" + tpm0.CreateRNG().Next(), 2048, TPMKeyUsage.TPM_KEY_SIGNING, TPMKeyFlags.None); TPMPCRSelection pcrs = tpm0.CreateEmptyPCRSelection(); pcrs.PcrSelection.SetBit(0, true); pcrs.PcrSelection.SetBit(1, true); pcrs.PcrSelection.SetBit(16, true); foreach (int pcr in pcrs.SelectedPCRs) { Console.Write(" PCR {0:D2}: 0x"); foreach (byte b in tpm0.IntegrityClient.PCRValue((uint)pcr)) { Console.Write("{0:X2}", b); } Console.WriteLine(); } ISigner signer = kh_sig1.CreateQuoter(pcrs); signer.Init(true, null); signer.Update((byte)'i'); signer.Update((byte)'a'); signer.Update((byte)'i'); signer.Update((byte)'k'); byte[] signature = signer.GenerateSignature(); Console.Write("QUOTE: "); foreach (byte b in signature) { Console.Write(" {0:X2}", b); } Console.WriteLine(); ISigner verifier = kh_sig1.CreateQuoter(pcrs); verifier.Init(false, null); verifier.Update((byte)'i'); verifier.Update((byte)'a'); verifier.Update((byte)'i'); verifier.Update((byte)'k'); if (verifier.VerifySignature(signature)) { Console.WriteLine("JO IT WORKED"); } else { Console.WriteLine("NA IT FAILED"); } } }
public void TestAlgorithms() { // // RSA parameters // BigInteger rsaMod = new BigInteger("a7295693155b1813bb84877fb45343556e0568043de5910872a3a518cc11e23e2db74eaf4545068c4e3d258a2718fbacdcc3eafa457695b957e88fbf110aed049a992d9c430232d02f3529c67a3419935ea9b569f85b1bcd37de6b899cd62697e843130ff0529d09c97d813cb15f293751ff56f943fbdabb63971cc7f4f6d5bff1594416b1f5907bde5a84a44f9802ef29b43bda1960f948f8afb8766c1ab80d32eec88ed66d0b65aebe44a6d0b3c5e0ab051aaa1b912fbcc17b8e751ddecc5365b6db6dab0020c3057db4013a51213a5798a3aab67985b0f4d88627a54a0f3f0285fbcb4afdfeb65cb153af66825656d43238b75503231500753f4e421e3c57", 16); BigInteger rsaPubExp = new BigInteger("10001", 16); BigInteger rsaPrivExp = new BigInteger("65dad56ac7df7abb434e4cb5eeadb16093aa6da7f0033aad3815289b04757d32bfee6ade7749c8e4a323b5050a2fb9e2a99e23469e1ed4ba5bab54336af20a5bfccb8b3424cc6923db2ffca5787ed87aa87aa614cd04cedaebc8f623a2d2063017910f436dff18bb06f01758610787f8b258f0a8efd8bd7de30007c47b2a1031696c7d6523bc191d4d918927a7e0b09584ed205bd2ff4fc4382678df82353f7532b3bbb81d69e3f39070aed3fb64fce032a089e8e64955afa5213a6eb241231bd98d702fba725a9b205952fda186412d9e0d9344d2998c455ad8c2bae85ee672751466d5288304032b5b7e02f7e558c7af82c7fbf58eea0bb4ef0f001e6cd0a9", 16); BigInteger rsaPrivP = new BigInteger("d4fd9ac3474fb83aaf832470643609659e511b322632b239b688f3cd2aad87527d6cf652fb9c9ca67940e84789444f2e99b0cb0cfabbd4de95396106c865f38e2fb7b82b231260a94df0e01756bf73ce0386868d9c41645560a81af2f53c18e4f7cdf3d51d80267372e6e0216afbf67f655c9450769cca494e4f6631b239ce1b", 16); BigInteger rsaPrivQ = new BigInteger("c8eaa0e2a1b3a4412a702bccda93f4d150da60d736c99c7c566fdea4dd1b401cbc0d8c063daaf0b579953d36343aa18b33dbf8b9eae94452490cc905245f8f7b9e29b1a288bc66731a29e1dd1a45c9fd7f8238ff727adc49fff73991d0dc096206b9d3a08f61e7462e2b804d78cb8c5eccdb9b7fbd2ad6a8fea46c1053e1be75", 16); BigInteger rsaPrivDP = new BigInteger("10edcb544421c0f9e123624d1099feeb35c72a8b34e008ac6fa6b90210a7543f293af4e5299c8c12eb464e70092805c7256e18e5823455ba0f504d36f5ccacac1b7cd5c58ff710f9c3f92646949d88fdd1e7ea5fed1081820bb9b0d2a8cd4b093fecfdb96dabd6e28c3a6f8c186dc86cddc89afd3e403e0fcf8a9e0bcb27af0b", 16); BigInteger rsaPrivDQ = new BigInteger("97fc25484b5a415eaa63c03e6efa8dafe9a1c8b004d9ee6e80548fefd6f2ce44ee5cb117e77e70285798f57d137566ce8ea4503b13e0f1b5ed5ca6942537c4aa96b2a395782a4cb5b58d0936e0b0fa63b1192954d39ced176d71ef32c6f42c84e2e19f9d4dd999c2151b032b97bd22aa73fd8c5bcd15a2dca4046d5acc997021", 16); BigInteger rsaPrivQinv = new BigInteger("4bb8064e1eff7e9efc3c4578fcedb59ca4aef0993a8312dfdcb1b3decf458aa6650d3d0866f143cbf0d3825e9381181170a0a1651eefcd7def786b8eb356555d9fa07c85b5f5cbdd74382f1129b5e36b4166b6cc9157923699708648212c484958351fdc9cf14f218dbe7fbf7cbd93a209a4681fe23ceb44bab67d66f45d1c9d", 16); RsaKeyParameters rsaPublic = new RsaKeyParameters(false, rsaMod, rsaPubExp); RsaPrivateCrtKeyParameters rsaPrivate = new RsaPrivateCrtKeyParameters( rsaMod, rsaPubExp, rsaPrivExp, rsaPrivP, rsaPrivQ, rsaPrivDP, rsaPrivDQ, rsaPrivQinv); // // ECDSA parameters // BigInteger ECParraGX = new BigInteger(Base64.Decode("D/qWPNyogWzMM7hkK+35BcPTWFc9Pyf7vTs8uaqv")); BigInteger ECParraGY = new BigInteger(Base64.Decode("AhQXGxb1olGRv6s1LPRfuatMF+cx3ZTGgzSE/Q5R")); BigInteger ECParraH = new BigInteger(Base64.Decode("AQ==")); BigInteger ECParraN = new BigInteger(Base64.Decode("f///////////////f///nl6an12QcfvRUiaIkJ0L")); BigInteger ECPubQX = new BigInteger(Base64.Decode("HWWi17Yb+Bm3PYr/DMjLOYNFhyOwX1QY7ZvqqM+l")); BigInteger ECPubQY = new BigInteger(Base64.Decode("JrlJfxu3WGhqwtL/55BOs/wsUeiDFsvXcGhB8DGx")); BigInteger ECPrivD = new BigInteger(Base64.Decode("GYQmd/NF1B+He1iMkWt3by2Az6Eu07t0ynJ4YCAo")); FpCurve curve = new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECDomainParameters ecDomain = new ECDomainParameters(curve, new FpPoint(curve, curve.FromBigInteger(ECParraGX), curve.FromBigInteger(ECParraGY)), ECParraN); ECPublicKeyParameters ecPub = new ECPublicKeyParameters( new FpPoint(curve, curve.FromBigInteger(ECPubQX), curve.FromBigInteger(ECPubQY)), ecDomain); ECPrivateKeyParameters ecPriv = new ECPrivateKeyParameters(ECPrivD, ecDomain); // // DSA parameters // BigInteger DSAParaG = new BigInteger(Base64.Decode("AL0fxOTq10OHFbCf8YldyGembqEu08EDVzxyLL29Zn/t4It661YNol1rnhPIs+cirw+yf9zeCe+KL1IbZ/qIMZM=")); BigInteger DSAParaP = new BigInteger(Base64.Decode("AM2b/UeQA+ovv3dL05wlDHEKJ+qhnJBsRT5OB9WuyRC830G79y0R8wuq8jyIYWCYcTn1TeqVPWqiTv6oAoiEeOs=")); BigInteger DSAParaQ = new BigInteger(Base64.Decode("AIlJT7mcKL6SUBMmvm24zX1EvjNx")); BigInteger DSAPublicY = new BigInteger(Base64.Decode("TtWy2GuT9yGBWOHi1/EpCDa/bWJCk2+yAdr56rAcqP0eHGkMnA9s9GJD2nGU8sFjNHm55swpn6JQb8q0agrCfw==")); BigInteger DsaPrivateX = new BigInteger(Base64.Decode("MMpBAxNlv7eYfxLTZ2BItJeD31A=")); DsaParameters para = new DsaParameters(DSAParaP, DSAParaQ, DSAParaG); DsaPrivateKeyParameters dsaPriv = new DsaPrivateKeyParameters(DsaPrivateX, para); DsaPublicKeyParameters dsaPub = new DsaPublicKeyParameters(DSAPublicY, para); // // GOST3410 parameters // IAsymmetricCipherKeyPairGenerator gostKpg = GeneratorUtilities.GetKeyPairGenerator("GOST3410"); gostKpg.Init( new Gost3410KeyGenerationParameters( new SecureRandom(), CryptoProObjectIdentifiers.GostR3410x94CryptoProA)); AsymmetricCipherKeyPair gostPair = gostKpg.GenerateKeyPair(); // // signer loop // byte[] shortMsg = new byte[] { 1, 4, 5, 6, 8, 8, 4, 2, 1, 3 }; byte[] longMsg = new byte[100]; new SecureRandom().NextBytes(longMsg); foreach (string algorithm in SignerUtilities.Algorithms) { ISigner signer = SignerUtilities.GetSigner(algorithm); string upper = algorithm.ToUpper(CultureInfo.InvariantCulture); int withPos = upper.LastIndexOf("WITH"); string cipherName = withPos < 0 ? upper : upper.Substring(withPos + "WITH".Length); ICipherParameters signParams = null, verifyParams = null; if (cipherName == "RSA" || cipherName == "RSAANDMGF1") { signParams = rsaPrivate; verifyParams = rsaPublic; } else if (cipherName == "ECDSA") { signParams = ecPriv; verifyParams = ecPub; } else if (cipherName == "DSA") { signParams = dsaPriv; verifyParams = dsaPub; } else if (cipherName == "ECGOST3410") { AsymmetricCipherKeyPair ecGostPair = GetEcCipherKeyPair( "ECGOST3410", CryptoProObjectIdentifiers.GostR3410x2001CryptoProA); signParams = ecGostPair.Private; verifyParams = ecGostPair.Public; } else if (cipherName == "GOST3410") { signParams = gostPair.Private; verifyParams = gostPair.Public; } else if (cipherName == "GOST3410_2012_256") { AsymmetricCipherKeyPair gost2012_256Pair = GetEcCipherKeyPair( "GOST3410_2012_256", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA); signParams = gost2012_256Pair.Private; verifyParams = gost2012_256Pair.Public; } else if (cipherName == "GOST3410_2012_512") { AsymmetricCipherKeyPair gost2012_512Pair = GetEcCipherKeyPair( "GOST3410_2012_512", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA); signParams = gost2012_512Pair.Private; verifyParams = gost2012_512Pair.Public; } else { Assert.Fail("Unknown algorithm encountered: " + cipherName); } signer.Init(true, signParams); foreach (byte b in shortMsg) { signer.Update(b); } signer.BlockUpdate(longMsg, 0, longMsg.Length); byte[] sig = signer.GenerateSignature(); signer.Init(false, verifyParams); foreach (byte b in shortMsg) { signer.Update(b); } signer.BlockUpdate(longMsg, 0, longMsg.Length); Assert.IsTrue(signer.VerifySignature(sig), cipherName + " signer " + algorithm + " failed."); } }
// TODO Move this when other JCE tests are ported from Java /** * signature with a "forged signature" (sig block not at end of plain text) */ private void doTestBadSig()//PrivateKey priv, PublicKey pub) { // Signature sig = Signature.getInstance("SHA1WithRSAEncryption", "BC"); ISigner sig = SignerUtilities.GetSigner("SHA1WithRSAEncryption"); // KeyPairGenerator fact; // KeyPair keyPair; // byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; // fact = KeyPairGenerator.getInstance("RSA", "BC"); RsaKeyPairGenerator fact = new RsaKeyPairGenerator(); // fact.initialize(768, new SecureRandom()); RsaKeyGenerationParameters factParams = new RsaKeyGenerationParameters( // BigInteger.ValueOf(0x11), new SecureRandom(), 768, 25); BigInteger.ValueOf(3), new SecureRandom(), 768, 25); fact.Init(factParams); // keyPair = fact.generateKeyPair(); // // PrivateKey signingKey = keyPair.getPrivate(); // PublicKey verifyKey = keyPair.getPublic(); AsymmetricCipherKeyPair keyPair = fact.GenerateKeyPair(); AsymmetricKeyParameter priv = keyPair.Private; AsymmetricKeyParameter pub = keyPair.Public; // testBadSig(signingKey, verifyKey); // MessageDigest sha1 = MessageDigest.getInstance("SHA1", "BC"); IDigest sha1 = DigestUtilities.GetDigest("SHA1"); // Cipher signer = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); // IBufferedCipher signer = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding"); IAsymmetricBlockCipher signer = new Pkcs1Encoding(new RsaEngine()); // signer.init(Cipher.ENCRYPT_MODE, priv); signer.Init(true, priv); // byte[] block = new byte[signer.getBlockSize()]; // byte[] block = new byte[signer.GetBlockSize()]; byte[] block = new byte[signer.GetInputBlockSize()]; // sha1.update((byte)0); sha1.Update(0); // byte[] sigHeader = Hex.decode("3021300906052b0e03021a05000414"); byte[] sigHeader = Hex.Decode("3021300906052b0e03021a05000414"); // System.arraycopy(sigHeader, 0, block, 0, sigHeader.length); Array.Copy(sigHeader, 0, block, 0, sigHeader.Length); // sha1.digest(block, sigHeader.length, sha1.getDigestLength()); sha1.DoFinal(block, sigHeader.Length); // System.arraycopy(sigHeader, 0, block, // sigHeader.length + sha1.getDigestLength(), sigHeader.length); Array.Copy(sigHeader, 0, block, sigHeader.Length + sha1.GetDigestSize(), sigHeader.Length); // byte[] sigBytes = signer.doFinal(block); byte[] sigBytes = signer.ProcessBlock(block, 0, block.Length); // Signature verifier = Signature.getInstance("SHA1WithRSA", "BC"); ISigner verifier = SignerUtilities.GetSigner("SHA1WithRSA"); // verifier.initVerify(pub); verifier.Init(false, pub); // verifier.update((byte)0); verifier.Update(0); // if (verifier.verify(sig)) if (verifier.VerifySignature(sigBytes)) { // fail("bad signature passed"); Fail("bad signature passed"); } }
private void DoUpdateByte( byte b) { _sig.Update(b); _dig.Update(b); }
public override void WriteByte(byte b) { mSigner.Update(b); }