Esempio n. 1
0
def solve():
    with open("public.pem", "rb") as public:
        public_dsa = DSA.importKey(public.read())
    with open('signed', 'rb') as f:
        signed_msg = f.read()
    with open('signature', 'rb') as f:
        r, s = eval(f.read())
    with open('sign_me', 'rb') as f:
        to_sign = f.read()

    k = nonce(signed_msg, public_dsa.q)
    hashed_message = int.from_bytes(SHA.new(signed_msg).digest(),
                                    byteorder='big')
    x = (((s * k) - hashed_message) * modinv(r, public_dsa.q)) % public_dsa.q
    if x:
        print('Found a potential private key !')
    else:
        raise Exception('Problem when calculating the private key')

    private_dsa = DSA.construct(
        (public_dsa.y, public_dsa.g, public_dsa.p, public_dsa.q, x))
    # verify the private key is the right one:
    new_r, new_s = private_dsa.sign(hashed_message, k)
    if new_r != r or new_s != s:
        print(
            "Error when getting the private key, the generated signature doesn't coincide with the known one"
        )
        return
    else:
        print("The private key found is the right one")
    to_sign_hashed = SHA.new(to_sign).digest()
    solution = private_dsa.sign(to_sign_hashed, nonce(to_sign, public_dsa.q))
    print('Here is the signed message: {}'.format(solution))
Esempio n. 2
0
 def testImportKey1(self):
     key_obj = DSA.importKey(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)
Esempio n. 3
0
    def is_private_key_unprotected(self, key_content_encoded, key_algorithm):
        """
        Check if the private key can be loaded without specifying any passphrase.

        PyCrypto >= 2.6.1 required in order to have the method importKey() in DSA class.

        :param key_content_encoded: Encoded content of the private key to test
        :param key_algorithm: Algorithm of the key (RSA or DSA)
        :return: True only if the key can be successfuly loaded and is usable
        """
        state = False
        try:
            # Try to load it
            if key_algorithm == "RSA":
                key = RSA.importKey(key_content_encoded)
            else:
                key = DSA.importKey(key_content_encoded)
            # Validate loading
            state = (key is not None and key.can_sign() and key.has_private())
        except Exception as e:
            print_debug("ERROR", "Cannot validate key protection '%s'" % e)
            state = False
            pass

        return state
Esempio n. 4
0
    def is_private_key_unprotected(self, key_content_encoded, key_algorithm):
        """
        Check if the private key can be loaded without specifying any passphrase.

        PyCrypto >= 2.6.1 required in order to have the method importKey() in DSA class.

        :param key_content_encoded: Encoded content of the private key to test
        :param key_algorithm: Algorithm of the key (RSA or DSA)
        :return: True only if the key can be successfuly loaded and is usable
        """
        state = False
        try:
            # Try to load it
            if key_algorithm == "RSA":
                key = RSA.importKey(key_content_encoded)
            else:
                key = DSA.importKey(key_content_encoded)
            # Validate loading
            state = (key is not None and key.can_sign() and key.has_private())
        except Exception as e:
            print_debug("ERROR", "Cannot validate key protection '%s'" % e)
            state = False
            pass

        return state
Esempio n. 5
0
 def testImportKey3(self):
     key_obj = DSA.importKey(self.der_private)
     self.failUnless(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)
     self.assertEqual(self.x, key_obj.x)
Esempio n. 6
0
 def testImportKey2(self):
     for pem in (self.pem_public, tostr(self.pem_public)):
         key_obj = DSA.importKey(pem)
         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)
Esempio n. 7
0
 def testImportKey7(self):
     for ssh in (self.ssh_pub, tostr(self.ssh_pub)):
         key_obj = DSA.importKey(ssh)
         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)
Esempio n. 8
0
 def testImportKey5(self):
     key_obj = DSA.importKey(self.der_pkcs8)
     self.assertTrue(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)
     self.assertEqual(self.x, key_obj.x)
 def testImportKey3(self):
     key_obj = DSA.importKey(self.der_private)
     self.failUnless(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)
     self.assertEqual(self.x, key_obj.x)
Esempio n. 10
0
 def testImportKey10(self):
     key_obj = DSA.importKey(self.der_pkcs8_encrypted, "PWDTEST")
     self.assertTrue(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)
     self.assertEqual(self.x, key_obj.x)
Esempio n. 11
0
 def testImportKey6(self):
     for pem in (self.pem_pkcs8, tostr(self.pem_pkcs8)):
         key_obj = DSA.importKey(pem)
         self.assertTrue(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)
         self.assertEqual(self.x, key_obj.x)
 def testImportKey4(self):
     for pem in (self.pem_private, tostr(self.pem_private)):
         key_obj = DSA.importKey(pem)
         self.failUnless(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)
         self.assertEqual(self.x, key_obj.x)
Esempio n. 13
0
 def testImportKey9(self):
     for pem in (self.pem_pkcs8_encrypted, tostr(self.pem_pkcs8_encrypted)):
         key_obj = DSA.importKey(pem, "PWDTEST")
         self.assertTrue(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)
         self.assertEqual(self.x, key_obj.x)
def sign_message(message, signature_file):
    hash_object = SHA1.new(message)
    with open('private_key.pem', 'rb') as f:
        key = DSA.importKey(f.read())
    signer = DSS.new(key, 'fips-186-3')
    sign = signer.sign(hash_object)
    print("Signature: ", sign)
    with open(signature_file, 'wb') as signature:
        signature.write(sign)
Esempio n. 15
0
 def testImportKey4(self):
     for pem in (self.pem_private, tostr(self.pem_private)):
         key_obj = DSA.importKey(pem)
         self.failUnless(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)
         self.assertEqual(self.x, key_obj.x)
Esempio n. 16
0
 def testExportKey8(self):
     tup = (self.y, self.g, self.p, self.q, self.x)
     key = DSA.construct(tup)
     encoded = key.export_key('PEM', pkcs8=False, passphrase="PWDTEST")
     key = DSA.importKey(encoded, "PWDTEST")
     self.assertEqual(self.y, key.y)
     self.assertEqual(self.p, key.p)
     self.assertEqual(self.q, key.q)
     self.assertEqual(self.g, key.g)
     self.assertEqual(self.x, key.x)
def verify_message(message, signature_file):
    with open(signature_file, 'rb') as signature:
        sign = signature.read()
    with open('public_key.pem', 'rb') as f:
        public_key = DSA.importKey(f.read())
    hash_object = SHA1.new(message)
    verifier = DSS.new(public_key, 'fips-186-3')

    try:
        verifier.verify(hash_object, sign)
        return "Verified"
    except ValueError:
        return "Not verified"
def sign_file(input_file, size, signature_file):
    hash_object = SHA1.new()
    with open(input_file, 'rb') as file:
        chunk = 0
        while chunk != b'':
            chunk = file.read(size)
            hash_object.update(chunk)
    with open('private_key.pem', 'rb') as f:
        key = DSA.importKey(f.read())
    signer = DSS.new(key, 'fips-186-3')
    sign = signer.sign(hash_object)
    print("Signature: ", sign)
    with open(signature_file, 'wb') as signature:
        signature.write(sign)
def verify_file(input_file, size, signature_file):
    signature = open(signature_file, 'rb').read()
    hash_object = SHA1.new()
    with open(input_file, 'rb') as file:
        chunk = 0
        while chunk != b'':
            chunk = file.read(size)
            hash_object.update(chunk)
    with open('public_key.pem', 'rb') as f:
        public_key = DSA.importKey(f.read())
    verifier = DSS.new(public_key, 'fips-186-3')

    try:
        verifier.verify(hash_object, signature)
        return "Verified"
    except ValueError:
        return "Not verified"
def sign_bytes_dsa(byte_array, path_to_private_key_pem_file):
    # Use this method for DSA keys
    key = open(path_to_private_key_pem_file, 'r').read()
    # Import the key
    dsa_key = DSA.importKey(key)

    # Create a digest of nonce + cnonce
    # This only seems to work with SHA1 (SHA256 gives us a 401 error)
    buf = buffer(byte_array)
    digest = SHA1.new(buf).digest()

    # Digitally sign the digest with our private key
    # The corresponding public key is in our admin handle on the server
    k = random.StrongRandom().randint(1, dsa_key.q-1)
    sign = dsa_key.sign(digest, k)

    # Signature bytes from a DSA key need to be DER-encoded
    # This signature is in two parts (r and s)
    seq = DerSequence()
    seq.append(sign[0])
    seq.append(sign[1])

    return seq.encode()
def sign_bytes_dsa(byte_array, path_to_private_key_pem_file):
    # Use this method for DSA keys
    key = open(path_to_private_key_pem_file, 'r').read()
    # Import the key
    dsa_key = DSA.importKey(key)

    # Create a digest of nonce + cnonce
    # This only seems to work with SHA1 (SHA256 gives us a 401 error)
    buf = buffer(byte_array)
    digest = SHA1.new(buf).digest()

    # Digitally sign the digest with our private key
    # The corresponding public key is in our admin handle on the server
    k = random.StrongRandom().randint(1, dsa_key.q - 1)
    sign = dsa_key.sign(digest, k)

    # Signature bytes from a DSA key need to be DER-encoded
    # This signature is in two parts (r and s)
    seq = DerSequence()
    seq.append(sign[0])
    seq.append(sign[1])

    return seq.encode()
Esempio n. 22
0
"""This module helps user sign a file using DSS standard."""

from Crypto.Signature import DSS
from Crypto.Hash import SHA256
from Crypto.PublicKey import DSA

message = 'To be signed'
key = DSA.importKey(open('dsa_key.bin').read())
h = SHA256.new(message)
signer = DSS.new(key, 'fips-186-3')
signature = signer.sign(h)
print signature
f = open("signature.bin", "wb")
f.write(signature)
Esempio n. 23
0
from Crypto.PublicKey import DSA
from Crypto.Hash import SHA

with open("private.pem", "rb") as priv:
    priv_dsa = DSA.importKey(priv.read())
with open("public.pem", "rb") as public:
    public_dsa = DSA.importKey(public.read())


def nonce(msg, max_int):
    n = 0
    for i in msg:
        n += i % max_int
        n = 53 * n % max_int
    n = (n - len(msg)) % max_int
    return n


def new_sign(msg):
    n = nonce(msg, public_dsa.q)
    h = SHA.new(msg).digest()
    sig = priv_dsa.sign(h, n)
    return sig


def new_verify(msg, s):
    h = SHA.new(msg).digest()
    return public_dsa.verify(h, s)


with open('signed', 'rb') as f:
Esempio n. 24
0
    def test_x509v3(self):

        # Sample V3 certificate with a 1024 bit DSA key
        x509_v3_cert = """
-----BEGIN CERTIFICATE-----
MIIFhjCCA26gAwIBAgIBAzANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJVUzEL
MAkGA1UECAwCTUQxEjAQBgNVBAcMCUJhbHRpbW9yZTEQMA4GA1UEAwwHVGVzdCBD
QTEfMB0GCSqGSIb3DQEJARYQdGVzdEBleGFtcGxlLmNvbTAeFw0xNDA3MTMyMDUz
MjBaFw0xNzA0MDgyMDUzMjBaMEAxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJNRDES
MBAGA1UEBwwJQmFsdGltb3JlMRAwDgYDVQQDDAdhdXN0cmlhMIIBtjCCASsGByqG
SM44BAEwggEeAoGBALfd8gyEpVPA0ZI69Kp3nyJcu5N0ZZ3K1K9hleQLNqKEcZOh
7a/C2J1TPdmHTLJ0rAwBZ1nWxnARSgRphziGDFspKCYQwYcSMz8KoFgvXbXpuchy
oFACiQ2LqZnc5MakuLQtLcQciSYGYj3zmZdYMoa904F1aDWr+DxQI6DVC3/bAhUA
hqXMCJ6fQK3G2O9S3/CC/yVZXCsCgYBRXROl3R2khX7l10LQjDEgo3B1IzjXU/jP
McMBl6XO+nBJXxr/scbq8Ajiv7LTnGpSjgryHtvfj887kfvo8QbSS3kp3vq5uSqI
ui7E7r3jguWaLj616AG1HWOctXJUjqsiabZwsp2h09gHTzmHEXBOmiARu8xFxKAH
xsuo7onAbwOBhAACgYBylWjWSnKHE8mHx1A5m/0GQx6xnhWIe3+MJAnEhRGxA2J4
SCsfWU0OwglIQToh1z5uUU9oDi9cYgNPBevOFRnDhc2yaJY6VAYnI+D+6J5IU6Yd
0iaG/iSc4sV4bFr0axcPpse3SN0XaQxiKeSFBfFnoMqL+dd9Gb3QPZSllBcVD6OB
1TCB0jAdBgNVHQ4EFgQUx5wN0Puotv388M9Tp/fsPbZpzAUwHwYDVR0jBBgwFoAU
a0hkif3RMaraiWtsOOZZlLu9wJwwCQYDVR0TBAIwADALBgNVHQ8EBAMCBeAwSgYD
VR0RBEMwQYILZXhhbXBsZS5jb22CD3d3dy5leGFtcGxlLmNvbYIQbWFpbC5leGFt
cGxlLmNvbYIPZnRwLmV4YW1wbGUuY29tMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NM
IEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTANBgkqhkiG9w0BAQsFAAOCAgEAyWf1TiJI
aNEIA9o/PG8/JiGASTS2/HBVTJbkq03k6NkJVk/GxC1DPziTUJ+CdWlHWcAi1EOW
Ach3QxNDRrVfCOfCMDgElIO1094/reJgdFYG00LRi8QkRJuxANV7YS4tLudhyHJC
kR2lhdMNmEuzWK+s2y+5cLrdm7qdvdENQCcV67uvGPx4sc+EaE7x13SczKjWBtbo
QCs6JTOW+EkPRl4Zo27K4OIZ43/J+GxvwU9QUVH3wPVdbbLNw+QeTFBYMTEcxyc4
kv50HPBFaithziXBFyvdIs19FjkFzu0Uz/e0zb1+vMzQlJMD94HVOrMnIj5Sb2cL
KKdYXS4uhxFJmdV091Xur5JkYYwEzuaGav7J3zOzYutrIGTgDluLCvA+VQkRcTsy
jZ065SkY/v+38QHp+cmm8WRluupJTs8wYzVp6Fu0iFaaK7ztFmaZmHpiPIfDFjva
aCIgzzT5NweJd/b71A2SyzHXJ14zBXsr1PMylMp2TpHIidhuuNuQL6I0HaollB4M
Z3FsVBMhVDw4Z76qnFPr8mZE2tar33hSlJI/3pS/bBiukuBk8U7VB0X8OqaUnP3C
7b2Z4G8GtqDVcKGMzkvMjT4n9rKd/Le+qHSsQOGO9W/0LB7UDAZSwUsfAPnoBgdS
5t9tIomLCOstByXi+gGZue1TcdCa3Ph4kO0=
-----END CERTIFICATE-----
        """.strip()

        # DSA public key as dumped by openssl
        y_str = """
72:95:68:d6:4a:72:87:13:c9:87:c7:50:39:9b:fd:
06:43:1e:b1:9e:15:88:7b:7f:8c:24:09:c4:85:11:
b1:03:62:78:48:2b:1f:59:4d:0e:c2:09:48:41:3a:
21:d7:3e:6e:51:4f:68:0e:2f:5c:62:03:4f:05:eb:
ce:15:19:c3:85:cd:b2:68:96:3a:54:06:27:23:e0:
fe:e8:9e:48:53:a6:1d:d2:26:86:fe:24:9c:e2:c5:
78:6c:5a:f4:6b:17:0f:a6:c7:b7:48:dd:17:69:0c:
62:29:e4:85:05:f1:67:a0:ca:8b:f9:d7:7d:19:bd:
d0:3d:94:a5:94:17:15:0f
        """
        p_str = """
00:b7:dd:f2:0c:84:a5:53:c0:d1:92:3a:f4:aa:77:
9f:22:5c:bb:93:74:65:9d:ca:d4:af:61:95:e4:0b:
36:a2:84:71:93:a1:ed:af:c2:d8:9d:53:3d:d9:87:
4c:b2:74:ac:0c:01:67:59:d6:c6:70:11:4a:04:69:
87:38:86:0c:5b:29:28:26:10:c1:87:12:33:3f:0a:
a0:58:2f:5d:b5:e9:b9:c8:72:a0:50:02:89:0d:8b:
a9:99:dc:e4:c6:a4:b8:b4:2d:2d:c4:1c:89:26:06:
62:3d:f3:99:97:58:32:86:bd:d3:81:75:68:35:ab:
f8:3c:50:23:a0:d5:0b:7f:db
        """
        q_str = """
00:86:a5:cc:08:9e:9f:40:ad:c6:d8:ef:52:df:f0:
82:ff:25:59:5c:2b
        """
        g_str = """
51:5d:13:a5:dd:1d:a4:85:7e:e5:d7:42:d0:8c:31:
20:a3:70:75:23:38:d7:53:f8:cf:31:c3:01:97:a5:
ce:fa:70:49:5f:1a:ff:b1:c6:ea:f0:08:e2:bf:b2:
d3:9c:6a:52:8e:0a:f2:1e:db:df:8f:cf:3b:91:fb:
e8:f1:06:d2:4b:79:29:de:fa:b9:b9:2a:88:ba:2e:
c4:ee:bd:e3:82:e5:9a:2e:3e:b5:e8:01:b5:1d:63:
9c:b5:72:54:8e:ab:22:69:b6:70:b2:9d:a1:d3:d8:
07:4f:39:87:11:70:4e:9a:20:11:bb:cc:45:c4:a0:
07:c6:cb:a8:ee:89:c0:6f
        """

        key = DSA.importKey(x509_v3_cert)
        for comp_name in ('y', 'p', 'q', 'g'):
            comp_str = locals()[comp_name + "_str"]
            comp = int(re.sub("[^0-9a-f]", "", comp_str), 16)
            self.assertEqual(getattr(key, comp_name), comp)
        self.assertFalse(key.has_private())
Esempio n. 25
0
    def test_x509v1(self):

        # Sample V1 certificate with a 1024 bit DSA key
        x509_v1_cert = """
-----BEGIN CERTIFICATE-----
MIIDUjCCArsCAQIwDQYJKoZIhvcNAQEFBQAwfjENMAsGA1UEChMEQWNtZTELMAkG
A1UECxMCUkQxHDAaBgkqhkiG9w0BCQEWDXNwYW1AYWNtZS5vcmcxEzARBgNVBAcT
Ck1ldHJvcG9saXMxETAPBgNVBAgTCE5ldyBZb3JrMQswCQYDVQQGEwJVUzENMAsG
A1UEAxMEdGVzdDAeFw0xNDA3MTEyMDM4NDNaFw0xNzA0MDYyMDM4NDNaME0xCzAJ
BgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazENMAsGA1UEChMEQWNtZTELMAkG
A1UECxMCUkQxDzANBgNVBAMTBnBvbGFuZDCCAbYwggErBgcqhkjOOAQBMIIBHgKB
gQDOrN4Ox4+t3T6wKeHfhzArhcrNEFMQ4Ss+4PIKyimDy9Bn64WPkL1B/9dvYIga
23GLu6tVJmXo6EdJnVOHEMhr99EeOwuDWWeP7Awq7RSlKEejokr4BEzMTW/tExSD
cO6/GI7xzh0eTH+VTTPDfyrJMYCkh0rJAfCP+5xrmPNetwIVALtXYOV1yoRrzJ2Q
M5uEjidH6GiZAoGAfUqA1SAm5g5U68SILMVX9l5rq0OpB0waBMpJQ31/R/yXNDqo
c3gGWZTOJFU4IzwNpGhrGNADUByz/lc1SAOAdEJIr0JVrhbGewQjB4pWqoLGbBKz
RoavTNDc/zD7SYa12evWDHADwvlXoeQg+lWop1zS8OqaDC7aLGKpWN3/m8kDgYQA
AoGAKoirPAfcp1rbbl4y2FFAIktfW8f4+T7d2iKSg73aiVfujhNOt1Zz1lfC0NI2
eonLWO3tAM4XGKf1TLjb5UXngGn40okPsaA81YE6ZIKm20ywjlOY3QkAEdMaLVY3
9PJvM8RGB9m7pLKxyHfGMfF40MVN4222zKeGp7xhM0CNiCUwDQYJKoZIhvcNAQEF
BQADgYEAfbNZfpYa2KlALEM1FZnwvQDvJHntHz8LdeJ4WM7CXDlKi67wY2HKM30w
s2xej75imkVOFd1kF2d0A8sjfriXLVIt1Hwq9ANZomhu4Edx0xpH8tqdh/bDtnM2
TmduZNY9OWkb07h0CtWD6Zt8fhRllVsSSrlWd/2or7FXNC5weFQ=
-----END CERTIFICATE-----
        """.strip()

        # DSA public key as dumped by openssl
        y_str = """
2a:88:ab:3c:07:dc:a7:5a:db:6e:5e:32:d8:51:40:
22:4b:5f:5b:c7:f8:f9:3e:dd:da:22:92:83:bd:da:
89:57:ee:8e:13:4e:b7:56:73:d6:57:c2:d0:d2:36:
7a:89:cb:58:ed:ed:00:ce:17:18:a7:f5:4c:b8:db:
e5:45:e7:80:69:f8:d2:89:0f:b1:a0:3c:d5:81:3a:
64:82:a6:db:4c:b0:8e:53:98:dd:09:00:11:d3:1a:
2d:56:37:f4:f2:6f:33:c4:46:07:d9:bb:a4:b2:b1:
c8:77:c6:31:f1:78:d0:c5:4d:e3:6d:b6:cc:a7:86:
a7:bc:61:33:40:8d:88:25
        """
        p_str = """
00:ce:ac:de:0e:c7:8f:ad:dd:3e:b0:29:e1:df:87:
30:2b:85:ca:cd:10:53:10:e1:2b:3e:e0:f2:0a:ca:
29:83:cb:d0:67:eb:85:8f:90:bd:41:ff:d7:6f:60:
88:1a:db:71:8b:bb:ab:55:26:65:e8:e8:47:49:9d:
53:87:10:c8:6b:f7:d1:1e:3b:0b:83:59:67:8f:ec:
0c:2a:ed:14:a5:28:47:a3:a2:4a:f8:04:4c:cc:4d:
6f:ed:13:14:83:70:ee:bf:18:8e:f1:ce:1d:1e:4c:
7f:95:4d:33:c3:7f:2a:c9:31:80:a4:87:4a:c9:01:
f0:8f:fb:9c:6b:98:f3:5e:b7
        """
        q_str = """
00:bb:57:60:e5:75:ca:84:6b:cc:9d:90:33:9b:84:
8e:27:47:e8:68:99
        """
        g_str = """
7d:4a:80:d5:20:26:e6:0e:54:eb:c4:88:2c:c5:57:
f6:5e:6b:ab:43:a9:07:4c:1a:04:ca:49:43:7d:7f:
47:fc:97:34:3a:a8:73:78:06:59:94:ce:24:55:38:
23:3c:0d:a4:68:6b:18:d0:03:50:1c:b3:fe:57:35:
48:03:80:74:42:48:af:42:55:ae:16:c6:7b:04:23:
07:8a:56:aa:82:c6:6c:12:b3:46:86:af:4c:d0:dc:
ff:30:fb:49:86:b5:d9:eb:d6:0c:70:03:c2:f9:57:
a1:e4:20:fa:55:a8:a7:5c:d2:f0:ea:9a:0c:2e:da:
2c:62:a9:58:dd:ff:9b:c9
        """

        key = DSA.importKey(x509_v1_cert)
        for comp_name in ('y', 'p', 'q', 'g'):
            comp_str = locals()[comp_name + "_str"]
            comp = int(re.sub("[^0-9a-f]", "", comp_str), 16)
            self.assertEqual(getattr(key, comp_name), comp)
        self.assertFalse(key.has_private())
Esempio n. 26
0
#coding=utf8
from Crypto.PublicKey import DSA
from hashlib import sha1
import gmpy2
with open('./dsa_public.pem') as f:
    key = DSA.importKey(f)
    y = key.y
    g = key.g
    p = key.p
    q = key.q
f3 = open(r"packet3/message3", 'r')
f4 = open(r"packet4/message4", 'r')
data3 = f3.read()
data4 = f4.read()
sha = sha1()
sha.update(data3)
m3 = int(sha.hexdigest(), 16)
sha = sha1()
sha.update(data4)
m4 = int(sha.hexdigest(), 16)
print m3, m4
s3 = 0x30EB88E6A4BFB1B16728A974210AE4E41B42677D
s4 = 0x5E10DED084203CCBCEC3356A2CA02FF318FD4123
r = 0x5090DA81FEDE048D706D80E0AC47701E5A9EF1CC
ds = s4 - s3
dm = m4 - m3
k = gmpy2.mul(dm, gmpy2.invert(ds, q))
k = gmpy2.f_mod(k, q)
tmp = gmpy2.mul(k, s3) - m3
x = tmp * gmpy2.invert(r, q)
x = gmpy2.f_mod(x, q)
Esempio n. 27
0
"""This module verifies a signature signed using DSS standard."""

from Crypto.Signature import DSS
from Crypto.Hash import SHA256
from Crypto.PublicKey import DSA

message = 'To be signed'

key = DSA.importKey(open('pubkey.bin').read())
h = SHA256.new(message)
verifier = DSS.new(key, 'fips-186-3')
f = open("signature.bin", "rb")
signature = f.read()
try:
    verifier.verify(h, signature)
    print "The signature is authentic."
except (ValueError, TypeError):
    print "The signature is not authentic."
Esempio n. 28
0
#!/usr/bin/env python
from Crypto.PublicKey import DSA
import libnum
import hashlib
key = DSA.importKey(open("dsa_public.pem").read())

m3 = open("message3").read()
m4 = open("message4").read()
h3 = int(hashlib.sha1(m3).hexdigest(), 16)
h4 = int(hashlib.sha1(m4).hexdigest(), 16)
r = 0x5090DA81FEDE048D706D80E0AC47701E5A9EF1CC
s3 = 0x30EB88E6A4BFB1B16728A974210AE4E41B42677D
s4 = 0x5E10DED084203CCBCEC3356A2CA02FF318FD4123

k = ((h3 - h4) * libnum.invmod(s3 - s4, key.q)) % key.q
x = ((k * s3 - h3) * libnum.invmod(r, key.q)) % key.q
print "CTF{%s}" % (str(x))