def testRsa(self): e = 17 n = 3233 d = 2753 m = 3000 s = crypto.rsaSign(m, d, n) self.assertTrue(crypto.rsaVerify(s, m, e, n)) r = 11 bm = crypto.rsaBlind(m, r, e, n) bs = crypto.rsaSign(bm, d, n) s = crypto.rsaUnblind(r, n, bs) self.assertTrue(crypto.rsaVerify(s, m, e, n))
def computeSignature(self, secretKeyMessage): """ Compute self signature """ key = secretKeyMessage.packets[SecretKeyPacket.TAG] m = encoding.hashEncode(self.hashdata(), key.n.bits() - 1, crypto.HASH_SHA256, encoding.ENCODING_PKCSPSS) s = crypto.rsaSign(crypto.b2i(m), key.d.value, key.n.value) self.signature = MPIElement(s)
def sign(self, bm): """ Create the signature. @param bm: The message to be signed. @type bm: openpgp.BlindMessageMessage. @return: The signature as a BlindSigntureMessage. """ packet = packets.BlindSignaturePacket() packet.s = elements.MPIElement(crypto.rsaSign( bm.packets[TAG_BLINDMSG].m.value, self.secretKey.packets[TAG_SECKEY].d.value, self.secretKey.packets[TAG_SECKEY].n.value)) message = messages.BlindSignatureMessage().fromPackets((packet,)) return message
def testBlinding(self): secretKey = messages.fromRadix64( open('testdata/foo-bar.com_secret_openpgp.txt', 'r').read()) publicKey = messages.fromRadix64( open('testdata/foo-bar.com_public_openpgp.txt', 'r').read()) data = 'The quick brown fox jumps over the lazy dog\n' sigTime = elements.TimeElement.now() r, hashTwo, newSigTime, blinded = blinding.blind(publicKey, sigTime, data) self.assertEqual(sigTime.value, newSigTime.value) blindSig = crypto.rsaSign(blinded.packets[TAG_BLINDMSG].m.value, secretKey.packets[TAG_SECKEY].d.value, publicKey.packets[TAG_PUBKEY].n.value) packet = packets.BlindSignaturePacket() packet.s = elements.MPIElement(blindSig) message = messages.BlindSignatureMessage.fromPackets((packet,)) s = blinding.unblind(publicKey, sigTime, r, hashTwo, message) self.assertTrue(verifySignature(data, s, publicKey))
def computeSignature(m, secretKey): """ Compute a type 0 signature. @param m: Message of binary data to sign. @type m: string @param secretKey: Key used for signature. @type secretKey: SecretKeyMessage @return: An OpenPGP signature message. """ sigPacket = packets.SignaturePacket() sigPacket.version = elements.ScalarElement(4) sigPacket.signatureType = elements.ScalarElement(0) sigPacket.pubAlgorithm = elements.ScalarElement(1) sigPacket.hashAlgorithm = elements.ScalarElement(crypto.HASH_SHA256) sigPacket.hashedSubpackets.add(subpackets.CreationTimeSubpacket( elements.TimeElement.now())) sigPacket.subpackets.add( subpackets.IssuerSubpacket(secretKey.packets[TAG_SECKEY].keyID())) data = (m + sigPacket.hashdata()) plainhash = crypto.hash(data, sigPacket.hashAlgorithm.value) sigPacket.hashLeftTwo = plainhash[0:2] codedhash = encoding.pkcs15( plainhash, secretKey.packets[TAG_SECKEY].n.bits(), sigPacket.hashAlgorithm.value) codedhashInt = elements.ScalarElement(codedhash).value rsaN = secretKey.packets[TAG_SECKEY].n.value rsaD = secretKey.packets[TAG_SECKEY].d.value s = crypto.rsaSign(codedhashInt, rsaD, rsaN) sigPacket.sig = elements.MPIElement(s) pgpMessage = messages.SignatureMessage.fromPackets((sigPacket,)) return pgpMessage