Example #1
0
    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))
Example #2
0
def verifySignature(m, signature, publicKey):
    """
    Verify a type 0 signature over m

    @param m: binary message
    @type m: string
    @param signature: The OpenPGP signature over m.
    @type signature: SignatureMessage
    @param publicKey: The public key of the signer.
    @type publicKey: PublicKeyMessage
    @return: Returns true if the signature is valid.
    """

    data = (m + signature.packets[TAG_SIGNATURE].hashdata())
    plainhash = crypto.hash(
        data,
        signature.packets[TAG_SIGNATURE].hashAlgorithm.value)
    if signature.packets[TAG_SIGNATURE].hashLeftTwo != plainhash[0:2]:
        return False

    codedhash = encoding.pkcs15(
        plainhash,
        publicKey.packets[TAG_PUBKEY].n.bits(),
        signature.packets[TAG_SIGNATURE].hashAlgorithm.value)
    codedhashInt = elements.ScalarElement(codedhash).value
    
    rsaN = publicKey.packets[TAG_PUBKEY].n.value
    rsaE = publicKey.packets[TAG_PUBKEY].e.value
    return crypto.rsaVerify(signature.packets[TAG_SIGNATURE].sig.value,
                            codedhashInt, rsaE, rsaN)
Example #3
0
    def testForge(self):
        e = 17
        n = 3233
        d = 2753

        m1 = 11
        m2 = 19

        bm1 = crypto.rsaBlind(m1, 11, e, n)
        bs1 = pow(bm1, d, n)
        s1 = crypto.rsaUnblind(11, n, bs1)
        self.assertTrue(crypto.rsaVerify(s1, m1, e, n))

        bm2 = crypto.rsaBlind(m2, 13, e, n)
        bs2 = pow(bm2, d, n)
        s2 = crypto.rsaUnblind(13, n, bs2)
        self.assertTrue(crypto.rsaVerify(s2, m2, e, n))
        
        self.assertTrue(crypto.rsaVerify((s1 * s2) % n, (m1 * m2) % n, e, n))
        self.assertTrue(crypto.rsaVerify(crypto.modInverse(s1, n),
                                         crypto.modInverse(m1, n), e, n))
Example #4
0
    def testSign(self):
        message = 'The quick brown fox jumps over the lazy dog\n'

        packet = packets.BlindMessagePacket()
        packet.m = elements.MPIElement(elements.ScalarElement(message).value)
        blindMessage = messages.BlindMessageMessage()
        blindMessage.packets[packet.TAG] = packet

        sigMessage = self.ca.sign(blindMessage)

        self.assertTrue(crypto.rsaVerify(
                sigMessage.packets[TAG_BLINDSIG].s.value,
                packet.m.value,
                self.ca.secretKey.packets[TAG_SECKEY].e.value,
                self.ca.secretKey.packets[TAG_SECKEY].n.value))