Example #1
0
 def test_2(self):
     """Test vector from 34.10-2012 standard itself
     """
     curve = GOST3410Curve(
         p=
         3623986102229003635907788753683874306021320925534678605086546150450856166624002482588482022271496854025090823603058735163734263822371964987228582907372403,
         q=
         3623986102229003635907788753683874306021320925534678605086546150450856166623969164898305032863068499961404079437936585455865192212970734808812618120619743,
         a=7,
         b=
         1518655069210828534508950034714043154928747527740206436194018823352809982443793732829756914785974674866041605397883677596626326413990136959047435811826396,
         x=
         1928356944067022849399309401243137598997786635459507974357075491307766592685835441065557681003184874819658004903212332884252335830250729527632383493573274,
         y=
         2288728693371972859970012155529478416353562327329506180314497425931102860301572814141997072271708807066593850650334152381857347798885864807605098724013854,
     )
     prv = bytes2long(
         hexdec(
             "0BA6048AADAE241BA40936D47756D7C93091A0E8514669700EE7508E508B102072E8123B2200A0563322DAD2827E2714A2636B7BFD18AADFC62967821FA18DD4"
         ))
     digest = hexdec(
         "3754F3CFACC9E0615C4F4A7C4D8DAB531B09B6F9C170C533A71D147035B0C5917184EE536593F4414339976C647C5D5A407ADEDB1D560C4FC6777D2972075B8C"
     )
     rand = hexdec(
         "0359E7F4B1410FEACC570456C6801496946312120B39D019D455986E364F365886748ED7A44B3E794434006011842286212273A6D14CF70EA3AF71BB1AE679F1"
     )
     signature = sign(curve, prv, digest, rand, mode=2012)
     r = "2f86fa60a081091a23dd795e1e3c689ee512a3c82ee0dcc2643c78eea8fcacd35492558486b20f1c9ec197c90699850260c93bcbcd9c5c3317e19344e173ae36"
     s = "1081b394696ffe8e6585e7a9362d26b6325f56778aadbc081c0bfbe933d52ff5823ce288e8c4f362526080df7f70ce406a6eeb1f56919cb92a9853bde73e5b4a"
     self.assertSequenceEqual(hexenc(signature), s + r)
Example #2
0
 def test_sequence(self):
     c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2001_TestParamSet"])
     prv = bytes2long(urandom(32))
     pubX, pubY = public_key(c, prv)
     for _ in range(20):
         digest = urandom(32)
         s = sign(c, prv, digest, mode=2001)
         self.assertTrue(verify(c, (pubX, pubY), digest, s, mode=2001))
Example #3
0
 def test_sequence(self):
     c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
     prv = bytes2long(urandom(64))
     pubX, pubY = public_key(c, prv)
     for _ in range(20):
         digest = urandom(64)
         s = sign(c, prv, digest, mode=2012)
         self.assertTrue(verify(c, (pubX, pubY), digest, s, mode=2012))
         self.assertNotIn(b"\x00" * 8, s)
Example #4
0
 def test_vector(self):
     curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
     ukm = ukm_unmarshal(hexdec("1d80603c8544c727"))
     prvA = prv_unmarshal(hexdec("c990ecd972fce84ec4db022778f50fcac726f46708384b8d458304962d7147f8c2db41cef22c90b102f2968404f9b9be6d47c79692d81826b32b8daca43cb667"))
     pubA = pub_unmarshal(hexdec("aab0eda4abff21208d18799fb9a8556654ba783070eba10cb9abb253ec56dcf5d3ccba6192e464e6e5bcb6dea137792f2431f6c897eb1b3c0cc14327b1adc0a7914613a3074e363aedb204d38d3563971bd8758e878c9db11403721b48002d38461f92472d40ea92f9958c0ffa4c93756401b97f89fdbe0b5e46e4a4631cdb5a"), mode=2012)
     prvB = prv_unmarshal(hexdec("48c859f7b6f11585887cc05ec6ef1390cfea739b1a18c0d4662293ef63b79e3b8014070b44918590b4b996acfea4edfbbbcccc8c06edd8bf5bda92a51392d0db"))
     pubB = pub_unmarshal(hexdec("192fe183b9713a077253c72c8735de2ea42a3dbc66ea317838b65fa32523cd5efca974eda7c863f4954d1147f1f2b25c395fce1c129175e876d132e94ed5a65104883b414c9b592ec4dc84826f07d0b6d9006dda176ce48c391e3f97d102e03bb598bf132a228a45f7201aba08fc524a2d77e43a362ab022ad4028f75bde3b79"), mode=2012)
     vko = hexdec("79f002a96940ce7bde3259a52e015297adaad84597a0d205b50e3e1719f97bfa7ee1d2661fa9979a5aa235b558a7e6d9f88f982dd63fc35a8ec0dd5e242d3bdf")
     self.assertEqual(kek_34102012512(curve, prvA, pubB, ukm), vko)
     self.assertEqual(kek_34102012512(curve, prvB, pubA, ukm), vko)
Example #5
0
 def test_vector(self):
     curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2001_TestParamSet"])
     ukm = ukm_unmarshal(hexdec("5172be25f852a233"))
     prv1 = prv_unmarshal(hexdec("1df129e43dab345b68f6a852f4162dc69f36b2f84717d08755cc5c44150bf928"))
     prv2 = prv_unmarshal(hexdec("5b9356c6474f913f1e83885ea0edd5df1a43fd9d799d219093241157ac9ed473"))
     kek = hexdec("ee4618a0dbb10cb31777b4b86a53d9e7ef6cb3e400101410f0c0f2af46c494a6")
     pub1 = public_key(curve, prv1)
     pub2 = public_key(curve, prv2)
     self.assertEqual(kek_34102001(curve, prv1, pub2, ukm), kek)
     self.assertEqual(kek_34102001(curve, prv2, pub1, ukm), kek)
Example #6
0
 def test_sequence(self):
     curve = GOST3410Curve(*CURVE_PARAMS["GostR3410_2012_TC26_ParamSetA"])
     for _ in range(10):
         ukm = ukm_unmarshal(urandom(8))
         prv1 = bytes2long(urandom(32))
         prv2 = bytes2long(urandom(32))
         pub1 = public_key(curve, prv1)
         pub2 = public_key(curve, prv2)
         kek1 = kek_34102012256(curve, prv1, pub2, ukm)
         kek2 = kek_34102012256(curve, prv2, pub1, ukm)
         self.assertEqual(kek1, kek2)
         kek1 = kek_34102012256(curve, prv1, pub1, ukm)
         kek2 = kek_34102012256(curve, prv2, pub2, ukm)
         self.assertNotEqual(kek1, kek2)
Example #7
0
def sign(private_key, data):
    """ Sign data

    :param private_key: private key to sign with
    :type private_key: bytes, 32 bytes
    :param bytes data: arbitrary data
    :return: signature
    :rtype: bytes, 64 bytes
    """
    curve = GOST3410Curve(*CURVE_PARAMS[DEFAULT_CURVE])
    return _sign(
        curve,
        bytes2long(private_key[::-1]),
        GOST341194(data, GOST341194_SBOX).digest(),
    )
Example #8
0
def keypair_gen(seed):
    """ Generate keypair

    :param seed: random data used as an entropy source
    :type seed: bytes, 32 bytes
    :return: private and public keys
    :rtype: (bytes, bytes), 32 and 64 bytes
    """
    if len(seed) != 32:
        raise ValueError("Invalid seed size")
    curve = GOST3410Curve(*CURVE_PARAMS[DEFAULT_CURVE])
    private_key = seed
    public_key_x, public_key_y = _public_key(curve, bytes2long(private_key))
    public_key = (long2bytes(public_key_y) + long2bytes(public_key_x))[::-1]
    return private_key[::-1], public_key
Example #9
0
    def test_rfc(self):
        """ Test vector from :rfc:`5832`
        """
        prv = bytes(bytearray((
            0x7A, 0x92, 0x9A, 0xDE, 0x78, 0x9B, 0xB9, 0xBE,
            0x10, 0xED, 0x35, 0x9D, 0xD3, 0x9A, 0x72, 0xC1,
            0x1B, 0x60, 0x96, 0x1F, 0x49, 0x39, 0x7E, 0xEE,
            0x1D, 0x19, 0xCE, 0x98, 0x91, 0xEC, 0x3B, 0x28
        )))
        pub_x = bytes(bytearray((
            0x7F, 0x2B, 0x49, 0xE2, 0x70, 0xDB, 0x6D, 0x90,
            0xD8, 0x59, 0x5B, 0xEC, 0x45, 0x8B, 0x50, 0xC5,
            0x85, 0x85, 0xBA, 0x1D, 0x4E, 0x9B, 0x78, 0x8F,
            0x66, 0x89, 0xDB, 0xD8, 0xE5, 0x6F, 0xD8, 0x0B
        )))
        pub_y = bytes(bytearray((
            0x26, 0xF1, 0xB4, 0x89, 0xD6, 0x70, 0x1D, 0xD1,
            0x85, 0xC8, 0x41, 0x3A, 0x97, 0x7B, 0x3C, 0xBB,
            0xAF, 0x64, 0xD1, 0xC5, 0x93, 0xD2, 0x66, 0x27,
            0xDF, 0xFB, 0x10, 0x1A, 0x87, 0xFF, 0x77, 0xDA
        )))
        digest = bytes(bytearray((
            0x2D, 0xFB, 0xC1, 0xB3, 0x72, 0xD8, 0x9A, 0x11,
            0x88, 0xC0, 0x9C, 0x52, 0xE0, 0xEE, 0xC6, 0x1F,
            0xCE, 0x52, 0x03, 0x2A, 0xB1, 0x02, 0x2E, 0x8E,
            0x67, 0xEC, 0xE6, 0x67, 0x2B, 0x04, 0x3E, 0xE5
        )))
        signature = bytes(bytearray((
            0x41, 0xAA, 0x28, 0xD2, 0xF1, 0xAB, 0x14, 0x82,
            0x80, 0xCD, 0x9E, 0xD5, 0x6F, 0xED, 0xA4, 0x19,
            0x74, 0x05, 0x35, 0x54, 0xA4, 0x27, 0x67, 0xB8,
            0x3A, 0xD0, 0x43, 0xFD, 0x39, 0xDC, 0x04, 0x93,
            0x01, 0x45, 0x6C, 0x64, 0xBA, 0x46, 0x42, 0xA1,
            0x65, 0x3C, 0x23, 0x5A, 0x98, 0xA6, 0x02, 0x49,
            0xBC, 0xD6, 0xD3, 0xF7, 0x46, 0xB6, 0x31, 0xDF,
            0x92, 0x80, 0x14, 0xF6, 0xC5, 0xBF, 0x9C, 0x40
        )))
        prv = bytes2long(prv)
        signature = signature[32:] + signature[:32]

        c = GOST3410Curve(*CURVE_PARAMS["GostR3410_2001_TestParamSet"])
        pubX, pubY = public_key(c, prv)
        self.assertEqual(long2bytes(pubX), pub_x)
        self.assertEqual(long2bytes(pubY), pub_y)
        s = sign(c, prv, digest)
        self.assertTrue(verify(c, (pubX, pubY), digest, s))
        self.assertTrue(verify(c, (pubX, pubY), digest, signature))
Example #10
0
def verify(public_key, data, signature):
    """ Verify signature

    :param public_key: public key to verify with
    :type public_key: bytes, 64 bytes
    :param bytes data: arbitrary data
    :type signature: bytes, 64 bytes
    :rtype: bool
    """
    curve = GOST3410Curve(*CURVE_PARAMS[DEFAULT_CURVE])
    public_key = public_key[::-1]
    return _verify(
        curve,
        bytes2long(public_key[32:]),
        bytes2long(public_key[:32]),
        GOST341194(data, GOST341194_SBOX).digest(),
        signature,
    )
Example #11
0
 def process_cert(self, curve_name, mode, hasher, prv_key_raw, cert_raw):
     cert, tail = Certificate().decode(cert_raw)
     self.assertSequenceEqual(tail, b"")
     curve = GOST3410Curve(*CURVE_PARAMS[curve_name])
     prv_key = prv_unmarshal(prv_key_raw)
     pub_key_raw, tail = OctetString().decode(
         bytes(cert["tbsCertificate"]["subjectPublicKeyInfo"]
               ["subjectPublicKey"]))
     pub_key = pub_unmarshal(bytes(pub_key_raw), mode=mode)
     self.assertSequenceEqual(tail, b"")
     self.assertSequenceEqual(pub_key, public_key(curve, prv_key))
     self.assertTrue(
         verify(
             curve,
             pub_key,
             hasher(cert["tbsCertificate"].encode()).digest()[::-1],
             bytes(cert["signatureValue"]),
             mode=mode,
         ))
Example #12
0
 def process_cms(
     self,
     content_info_raw,
     prv_key_our,
     curve_name,
     keker,
     plaintext_expected,
 ):
     sbox = "Gost28147_tc26_ParamZ"
     content_info, tail = ContentInfo().decode(content_info_raw)
     self.assertSequenceEqual(tail, b"")
     enveloped_data, tail = EnvelopedData().decode(
         bytes(content_info["content"]))
     self.assertSequenceEqual(tail, b"")
     eci = enveloped_data["encryptedContentInfo"]
     ri = enveloped_data["recipientInfos"][0]
     encrypted_key, tail = GostR3410KeyTransport().decode(
         bytes(ri["ktri"]["encryptedKey"]))
     self.assertSequenceEqual(tail, b"")
     ukm = bytes(encrypted_key["transportParameters"]["ukm"])
     spk = bytes(encrypted_key["transportParameters"]["ephemeralPublicKey"]
                 ["subjectPublicKey"])
     pub_key_their, tail = OctetString().decode(spk)
     self.assertSequenceEqual(tail, b"")
     curve = GOST3410Curve(*CURVE_PARAMS[curve_name])
     kek = keker(curve, prv_key_our, bytes(pub_key_their), ukm)
     key_wrapped = bytes(
         encrypted_key["sessionEncryptedKey"]["encryptedKey"])
     mac = bytes(encrypted_key["sessionEncryptedKey"]["macKey"])
     cek = unwrap_cryptopro(kek, ukm + key_wrapped + mac, sbox=sbox)
     ciphertext = bytes(eci["encryptedContent"])
     encryption_params, tail = Gost2814789Parameters().decode(
         bytes(eci["contentEncryptionAlgorithm"]["parameters"]))
     self.assertSequenceEqual(tail, b"")
     iv = bytes(encryption_params["iv"])
     self.assertSequenceEqual(
         cfb_decrypt(cek, ciphertext, iv, sbox=sbox, mesh=True),
         plaintext_expected,
     )
Example #13
0
 def process_cms(
     self,
     content_info_raw,
     prv_key_our,
     curve_name,
     keker,
     plaintext_expected,
 ):
     sbox = "Gost28147_tc26_ParamZ"
     content_info, tail = ContentInfo().decode(content_info_raw)
     self.assertSequenceEqual(tail, b"")
     enveloped_data, tail = EnvelopedData().decode(
         bytes(content_info["content"]))
     self.assertSequenceEqual(tail, b"")
     eci = enveloped_data["encryptedContentInfo"]
     kari = enveloped_data["recipientInfos"][0]["kari"]
     pub_key_their, tail = OctetString().decode(
         bytes(kari["originator"]["originatorKey"]["publicKey"]), )
     self.assertSequenceEqual(tail, b"")
     ukm = bytes(kari["ukm"])
     rek = kari["recipientEncryptedKeys"][0]
     curve = GOST3410Curve(*CURVE_PARAMS[curve_name])
     kek = keker(curve, prv_key_our, bytes(pub_key_their), ukm)
     encrypted_key, tail = Gost2814789EncryptedKey().decode(
         bytes(rek["encryptedKey"]), )
     self.assertSequenceEqual(tail, b"")
     key_wrapped = bytes(encrypted_key["encryptedKey"])
     mac = bytes(encrypted_key["macKey"])
     cek = unwrap_gost(kek, ukm + key_wrapped + mac, sbox=sbox)
     ciphertext = bytes(eci["encryptedContent"])
     encryption_params, tail = Gost2814789Parameters().decode(
         bytes(eci["contentEncryptionAlgorithm"]["parameters"]))
     self.assertSequenceEqual(tail, b"")
     iv = bytes(encryption_params["iv"])
     self.assertSequenceEqual(
         cfb_decrypt(cek, ciphertext, iv, sbox=sbox, mesh=True),
         plaintext_expected,
     )
Example #14
0
 def process_cms(
     self,
     content_info_raw,
     prv_key_raw,
     curve_name,
     hasher,
     mode,
 ):
     content_info, tail = ContentInfo().decode(content_info_raw)
     self.assertSequenceEqual(tail, b"")
     signed_data, tail = SignedData().decode(bytes(content_info["content"]))
     self.assertSequenceEqual(tail, b"")
     self.assertEqual(len(signed_data["signerInfos"]), 1)
     curve = GOST3410Curve(*CURVE_PARAMS[curve_name])
     self.assertTrue(
         verify(
             curve,
             public_key(curve, prv_unmarshal(prv_key_raw)),
             hasher(bytes(signed_data["encapContentInfo"]
                          ["eContent"])).digest()[::-1],
             bytes(signed_data["signerInfos"][0]["signature"]),
             mode=mode,
         ))
Example #15
0
 def test_gcl3(self):
     """ Test vector from libgcl3
     """
     p = bytes2long(
         bytes(
             bytearray((
                 0x45,
                 0x31,
                 0xAC,
                 0xD1,
                 0xFE,
                 0x00,
                 0x23,
                 0xC7,
                 0x55,
                 0x0D,
                 0x26,
                 0x7B,
                 0x6B,
                 0x2F,
                 0xEE,
                 0x80,
                 0x92,
                 0x2B,
                 0x14,
                 0xB2,
                 0xFF,
                 0xB9,
                 0x0F,
                 0x04,
                 0xD4,
                 0xEB,
                 0x7C,
                 0x09,
                 0xB5,
                 0xD2,
                 0xD1,
                 0x5D,
                 0xF1,
                 0xD8,
                 0x52,
                 0x74,
                 0x1A,
                 0xF4,
                 0x70,
                 0x4A,
                 0x04,
                 0x58,
                 0x04,
                 0x7E,
                 0x80,
                 0xE4,
                 0x54,
                 0x6D,
                 0x35,
                 0xB8,
                 0x33,
                 0x6F,
                 0xAC,
                 0x22,
                 0x4D,
                 0xD8,
                 0x16,
                 0x64,
                 0xBB,
                 0xF5,
                 0x28,
                 0xBE,
                 0x63,
                 0x73,
             ))))
     q = bytes2long(
         bytes(
             bytearray((
                 0x45,
                 0x31,
                 0xAC,
                 0xD1,
                 0xFE,
                 0x00,
                 0x23,
                 0xC7,
                 0x55,
                 0x0D,
                 0x26,
                 0x7B,
                 0x6B,
                 0x2F,
                 0xEE,
                 0x80,
                 0x92,
                 0x2B,
                 0x14,
                 0xB2,
                 0xFF,
                 0xB9,
                 0x0F,
                 0x04,
                 0xD4,
                 0xEB,
                 0x7C,
                 0x09,
                 0xB5,
                 0xD2,
                 0xD1,
                 0x5D,
                 0xA8,
                 0x2F,
                 0x2D,
                 0x7E,
                 0xCB,
                 0x1D,
                 0xBA,
                 0xC7,
                 0x19,
                 0x90,
                 0x5C,
                 0x5E,
                 0xEC,
                 0xC4,
                 0x23,
                 0xF1,
                 0xD8,
                 0x6E,
                 0x25,
                 0xED,
                 0xBE,
                 0x23,
                 0xC5,
                 0x95,
                 0xD6,
                 0x44,
                 0xAA,
                 0xF1,
                 0x87,
                 0xE6,
                 0xE6,
                 0xDF,
             ))))
     a = bytes2long(
         bytes(
             bytearray((
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x00,
                 0x07,
             ))))
     b = bytes2long(
         bytes(
             bytearray((
                 0x1C,
                 0xFF,
                 0x08,
                 0x06,
                 0xA3,
                 0x11,
                 0x16,
                 0xDA,
                 0x29,
                 0xD8,
                 0xCF,
                 0xA5,
                 0x4E,
                 0x57,
                 0xEB,
                 0x74,
                 0x8B,
                 0xC5,
                 0xF3,
                 0x77,
                 0xE4,
                 0x94,
                 0x00,
                 0xFD,
                 0xD7,
                 0x88,
                 0xB6,
                 0x49,
                 0xEC,
                 0xA1,
                 0xAC,
                 0x43,
                 0x61,
                 0x83,
                 0x40,
                 0x13,
                 0xB2,
                 0xAD,
                 0x73,
                 0x22,
                 0x48,
                 0x0A,
                 0x89,
                 0xCA,
                 0x58,
                 0xE0,
                 0xCF,
                 0x74,
                 0xBC,
                 0x9E,
                 0x54,
                 0x0C,
                 0x2A,
                 0xDD,
                 0x68,
                 0x97,
                 0xFA,
                 0xD0,
                 0xA3,
                 0x08,
                 0x4F,
                 0x30,
                 0x2A,
                 0xDC,
             ))))
     x = bytes2long(
         bytes(
             bytearray((
                 0x24,
                 0xD1,
                 0x9C,
                 0xC6,
                 0x45,
                 0x72,
                 0xEE,
                 0x30,
                 0xF3,
                 0x96,
                 0xBF,
                 0x6E,
                 0xBB,
                 0xFD,
                 0x7A,
                 0x6C,
                 0x52,
                 0x13,
                 0xB3,
                 0xB3,
                 0xD7,
                 0x05,
                 0x7C,
                 0xC8,
                 0x25,
                 0xF9,
                 0x10,
                 0x93,
                 0xA6,
                 0x8C,
                 0xD7,
                 0x62,
                 0xFD,
                 0x60,
                 0x61,
                 0x12,
                 0x62,
                 0xCD,
                 0x83,
                 0x8D,
                 0xC6,
                 0xB6,
                 0x0A,
                 0xA7,
                 0xEE,
                 0xE8,
                 0x04,
                 0xE2,
                 0x8B,
                 0xC8,
                 0x49,
                 0x97,
                 0x7F,
                 0xAC,
                 0x33,
                 0xB4,
                 0xB5,
                 0x30,
                 0xF1,
                 0xB1,
                 0x20,
                 0x24,
                 0x8A,
                 0x9A,
             ))))
     y = bytes2long(
         bytes(
             bytearray((
                 0x2B,
                 0xB3,
                 0x12,
                 0xA4,
                 0x3B,
                 0xD2,
                 0xCE,
                 0x6E,
                 0x0D,
                 0x02,
                 0x06,
                 0x13,
                 0xC8,
                 0x57,
                 0xAC,
                 0xDD,
                 0xCF,
                 0xBF,
                 0x06,
                 0x1E,
                 0x91,
                 0xE5,
                 0xF2,
                 0xC3,
                 0xF3,
                 0x24,
                 0x47,
                 0xC2,
                 0x59,
                 0xF3,
                 0x9B,
                 0x2C,
                 0x83,
                 0xAB,
                 0x15,
                 0x6D,
                 0x77,
                 0xF1,
                 0x49,
                 0x6B,
                 0xF7,
                 0xEB,
                 0x33,
                 0x51,
                 0xE1,
                 0xEE,
                 0x4E,
                 0x43,
                 0xDC,
                 0x1A,
                 0x18,
                 0xB9,
                 0x1B,
                 0x24,
                 0x64,
                 0x0B,
                 0x6D,
                 0xBB,
                 0x92,
                 0xCB,
                 0x1A,
                 0xDD,
                 0x37,
                 0x1E,
             ))))
     prv = bytes(
         bytearray((
             0x0B,
             0xA6,
             0x04,
             0x8A,
             0xAD,
             0xAE,
             0x24,
             0x1B,
             0xA4,
             0x09,
             0x36,
             0xD4,
             0x77,
             0x56,
             0xD7,
             0xC9,
             0x30,
             0x91,
             0xA0,
             0xE8,
             0x51,
             0x46,
             0x69,
             0x70,
             0x0E,
             0xE7,
             0x50,
             0x8E,
             0x50,
             0x8B,
             0x10,
             0x20,
             0x72,
             0xE8,
             0x12,
             0x3B,
             0x22,
             0x00,
             0xA0,
             0x56,
             0x33,
             0x22,
             0xDA,
             0xD2,
             0x82,
             0x7E,
             0x27,
             0x14,
             0xA2,
             0x63,
             0x6B,
             0x7B,
             0xFD,
             0x18,
             0xAA,
             0xDF,
             0xC6,
             0x29,
             0x67,
             0x82,
             0x1F,
             0xA1,
             0x8D,
             0xD4,
         )))
     pub_x = bytes(
         bytearray((
             0x11,
             0x5D,
             0xC5,
             0xBC,
             0x96,
             0x76,
             0x0C,
             0x7B,
             0x48,
             0x59,
             0x8D,
             0x8A,
             0xB9,
             0xE7,
             0x40,
             0xD4,
             0xC4,
             0xA8,
             0x5A,
             0x65,
             0xBE,
             0x33,
             0xC1,
             0x81,
             0x5B,
             0x5C,
             0x32,
             0x0C,
             0x85,
             0x46,
             0x21,
             0xDD,
             0x5A,
             0x51,
             0x58,
             0x56,
             0xD1,
             0x33,
             0x14,
             0xAF,
             0x69,
             0xBC,
             0x5B,
             0x92,
             0x4C,
             0x8B,
             0x4D,
             0xDF,
             0xF7,
             0x5C,
             0x45,
             0x41,
             0x5C,
             0x1D,
             0x9D,
             0xD9,
             0xDD,
             0x33,
             0x61,
             0x2C,
             0xD5,
             0x30,
             0xEF,
             0xE1,
         )))
     pub_y = bytes(
         bytearray((
             0x37,
             0xC7,
             0xC9,
             0x0C,
             0xD4,
             0x0B,
             0x0F,
             0x56,
             0x21,
             0xDC,
             0x3A,
             0xC1,
             0xB7,
             0x51,
             0xCF,
             0xA0,
             0xE2,
             0x63,
             0x4F,
             0xA0,
             0x50,
             0x3B,
             0x3D,
             0x52,
             0x63,
             0x9F,
             0x5D,
             0x7F,
             0xB7,
             0x2A,
             0xFD,
             0x61,
             0xEA,
             0x19,
             0x94,
             0x41,
             0xD9,
             0x43,
             0xFF,
             0xE7,
             0xF0,
             0xC7,
             0x0A,
             0x27,
             0x59,
             0xA3,
             0xCD,
             0xB8,
             0x4C,
             0x11,
             0x4E,
             0x1F,
             0x93,
             0x39,
             0xFD,
             0xF2,
             0x7F,
             0x35,
             0xEC,
             0xA9,
             0x36,
             0x77,
             0xBE,
             0xEC,
         )))
     digest = bytes(
         bytearray((
             0x37,
             0x54,
             0xF3,
             0xCF,
             0xAC,
             0xC9,
             0xE0,
             0x61,
             0x5C,
             0x4F,
             0x4A,
             0x7C,
             0x4D,
             0x8D,
             0xAB,
             0x53,
             0x1B,
             0x09,
             0xB6,
             0xF9,
             0xC1,
             0x70,
             0xC5,
             0x33,
             0xA7,
             0x1D,
             0x14,
             0x70,
             0x35,
             0xB0,
             0xC5,
             0x91,
             0x71,
             0x84,
             0xEE,
             0x53,
             0x65,
             0x93,
             0xF4,
             0x41,
             0x43,
             0x39,
             0x97,
             0x6C,
             0x64,
             0x7C,
             0x5D,
             0x5A,
             0x40,
             0x7A,
             0xDE,
             0xDB,
             0x1D,
             0x56,
             0x0C,
             0x4F,
             0xC6,
             0x77,
             0x7D,
             0x29,
             0x72,
             0x07,
             0x5B,
             0x8C,
         )))
     signature = bytes(
         bytearray((
             0x2F,
             0x86,
             0xFA,
             0x60,
             0xA0,
             0x81,
             0x09,
             0x1A,
             0x23,
             0xDD,
             0x79,
             0x5E,
             0x1E,
             0x3C,
             0x68,
             0x9E,
             0xE5,
             0x12,
             0xA3,
             0xC8,
             0x2E,
             0xE0,
             0xDC,
             0xC2,
             0x64,
             0x3C,
             0x78,
             0xEE,
             0xA8,
             0xFC,
             0xAC,
             0xD3,
             0x54,
             0x92,
             0x55,
             0x84,
             0x86,
             0xB2,
             0x0F,
             0x1C,
             0x9E,
             0xC1,
             0x97,
             0xC9,
             0x06,
             0x99,
             0x85,
             0x02,
             0x60,
             0xC9,
             0x3B,
             0xCB,
             0xCD,
             0x9C,
             0x5C,
             0x33,
             0x17,
             0xE1,
             0x93,
             0x44,
             0xE1,
             0x73,
             0xAE,
             0x36,
             0x10,
             0x81,
             0xB3,
             0x94,
             0x69,
             0x6F,
             0xFE,
             0x8E,
             0x65,
             0x85,
             0xE7,
             0xA9,
             0x36,
             0x2D,
             0x26,
             0xB6,
             0x32,
             0x5F,
             0x56,
             0x77,
             0x8A,
             0xAD,
             0xBC,
             0x08,
             0x1C,
             0x0B,
             0xFB,
             0xE9,
             0x33,
             0xD5,
             0x2F,
             0xF5,
             0x82,
             0x3C,
             0xE2,
             0x88,
             0xE8,
             0xC4,
             0xF3,
             0x62,
             0x52,
             0x60,
             0x80,
             0xDF,
             0x7F,
             0x70,
             0xCE,
             0x40,
             0x6A,
             0x6E,
             0xEB,
             0x1F,
             0x56,
             0x91,
             0x9C,
             0xB9,
             0x2A,
             0x98,
             0x53,
             0xBD,
             0xE7,
             0x3E,
             0x5B,
             0x4A,
         )))
     prv = bytes2long(prv)
     signature = signature[64:] + signature[:64]
     c = GOST3410Curve(p, q, a, b, x, y)
     pubX, pubY = public_key(c, prv)
     self.assertSequenceEqual(long2bytes(pubX), pub_x)
     self.assertSequenceEqual(long2bytes(pubY), pub_y)
     s = sign(c, prv, digest, mode=2012)
     self.assertTrue(verify(c, (pubX, pubY), digest, s, mode=2012))
     self.assertTrue(verify(c, (pubX, pubY), digest, signature, mode=2012))
Example #16
0
import base64
from binascii import hexlify, unhexlify
from pygost.gost3410 import CURVE_PARAMS
from pygost.gost3410 import GOST3410Curve
from pygost.gost3410 import bytes2long
curve = GOST3410Curve(*CURVE_PARAMS['GostR3410_2012_TC26_ParamSetA'])
from os import urandom
# prv_raw = urandom(32)
from pygost.gost3410 import prv_unmarshal
# prv = prv_unmarshal(prv_raw)
from pygost.gost3410 import public_key
# pub = public_key(curve, prv)
from pygost.gost3410 import pub_marshal
from pygost.utils import hexenc
from pygost.utils import hexdec
# print('Public key is:', hexenc(pub_marshal(pub)))
from pygost import gost34112012256
# data_for_signing = b'some data'
# dgst = gost34112012256.new(data_for_signing).digest()
from pygost.gost3410 import sign
# signature = sign(curve, prv, dgst, mode=2012)
from pygost.gost3410 import verify
# verify(curve, pub, dgst, signature, mode=2012)

name = 'gost'
bit = '256'


class sk_:
    def __init__(self, prv, pub=0000):
        self.prv = prv