Beispiel #1
0
    def setUp(self):
        comps = "Crypto.SelfTest.Signature.test_vectors.wycheproof".split(".")
        with open(pycryptodome_filename(comps, "dsa_test.json"), "rt") as file_in:
            tv_tree = json.load(file_in)

        self.tv = []

        for group in tv_tree['testGroups']:
            key = DSA.import_key(group['keyPem'])
            hash_name = group['sha']
            if hash_name == "SHA-256":
                hash_module = SHA256
            elif hash_name == "SHA-224":
                hash_module = SHA224
            elif hash_name == "SHA-1":
                hash_module = SHA1
            else:
                assert False
            assert group['type'] == "DSAVer"
            
            from collections import namedtuple
            TestVector = namedtuple('TestVector', 'id comment msg sig key hash_module valid warning')

            for test in group['tests']:
                tv = TestVector(
                    test['tcId'],
                    test['comment'],
                    unhexlify(test['msg']),
                    unhexlify(test['sig']),
                    key,
                    hash_module,
                    test['result'] != "invalid",
                    test['result'] == "acceptable"
                )
                self.tv.append(tv)
Beispiel #2
0
def verify_signature(transaction):
    verifier = DSS.new(DSA.import_key(transaction.sender_public_key), 'fips-186-3')
    try:
        verifier.verify(transaction.get_hash(), transaction.signature)
        print('Correct signature')
    except ValueError:
        print('Incorrect signature')
Beispiel #3
0
    def test_import_key(self):
        """Verify importKey is an alias to import_key"""

        key_obj = DSA.import_key(self.der_public)
        self.assertFalse(key_obj.has_private())
        self.assertEqual(self.y, key_obj.y)
        self.assertEqual(self.p, key_obj.p)
        self.assertEqual(self.q, key_obj.q)
        self.assertEqual(self.g, key_obj.g)
Beispiel #4
0
 def varify_message(self, text, signature, key):
     pubKey = DSA.import_key(bytes(key, 'utf-8'))
     hash_obj = SHA256.new(bytes(text, 'utf-8'))
     verifier = DSS.new(pubKey, 'fips-186-3')
     signature = base64.b64decode(signature)
     try:
         verifier.verify(hash_obj, signature)
         return True
     except ValueError:
         return False
Beispiel #5
0
def DSA_verifier(message, signature, client):
    # Load the public key
    f = open(client + "_DSA_public_key.pem", "rb")
    hash_obj = SHA256.new(message.encode())
    pub_key = DSA.import_key(f.read())
    verifier = DSS.new(pub_key, 'fips-186-3')
    # Verify the authenticity of the message
    try:
        verifier.verify(hash_obj, signature)
        print("The message is authentic.")
    except ValueError:
        print("The message is not authentic.")
Beispiel #6
0
def validate_private_key(ssh_private_key_data):
    try:
        RSA.import_key(from_base64(ssh_private_key_data))
        return
    except ValueError:
        try:
            ECC.import_key(from_base64(ssh_private_key_data))
            return
        except ValueError:
            try:
                DSA.import_key(from_base64(ssh_private_key_data))
                return
            except ValueError:
                try:
                    key_obj = io.StringIO(
                        from_base64(ssh_private_key_data).decode('utf-8'))
                    Ed25519Key(file_obj=key_obj)
                    return
                except SSHException as ex:
                    raise InvalidArgumentValueError(
                        consts.SSH_PRIVATE_KEY_ERROR,
                        consts.SSH_PRIVATE_KEY_HELP) from ex
Beispiel #7
0
def verify(signature):
    file = open("public_key.pem", "r")
    flag = False
    pub_key = DSA.import_key(file.read())
    verifier = DSS.new(pub_key, 'fips-186-3')
    for password in in_memory_password:
        hash_obj = SHA256.new(password.encode('utf-8'))
        try:
            verifier.verify(hash_obj, signature)
            flag = True
            break
        except ValueError:
            pass
    if flag:
        print("You are authenticated!")
    else:
        print("You are not authenticated!")
Beispiel #8
0
def verifysig(message, signature, pubkey):
    # get r and s out of the ASN-1
    decoder = asn1.Decoder()
    decoder.start(signature)
    tag, seq = decoder.read()
    decoder.start(seq)
    tag, r = decoder.read()
    tag, s = decoder.read()

    rbytes = Integer(r).to_bytes()
    sbytes = Integer(s).to_bytes()

    verifykey = DSA.import_key(base64.b64decode(pubkey))

    h = SHA1.new(message)
    verifier = DSS.new(verifykey, 'fips-186-3')

    try:
        verifier.verify(h, rbytes + sbytes)
        print("Signature is valid.")
        return True
    except ValueError:
        print("Signature NOT valid.")
        return False
Beispiel #9
0
 def sign_message(self, text, key):
     privKey = DSA.import_key(bytes(key, 'utf-8'))
     hash_obj = SHA256.new(bytes(text, 'utf-8'))
     signer = DSS.new(privKey, 'fips-186-3')
     signature = signer.sign(hash_obj)
     return base64.b64encode(signature)