# coding: utf-8
# PyGOST -- Pure Python GOST cryptographic functions library
-# Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
+# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
e=0x01,
d=bytes2long(hexdec("0605F6B7C183FA81578BC39CFAD518132B9DF62897009AF7E522C32D6DC7BFFB")),
),
+ "id-tc26-gost-3410-2012-512-paramSetTest": GOST3410Curve(
+ p=bytes2long(hexdec("4531ACD1FE0023C7550D267B6B2FEE80922B14B2FFB90F04D4EB7C09B5D2D15DF1D852741AF4704A0458047E80E4546D35B8336FAC224DD81664BBF528BE6373")),
+ q=bytes2long(hexdec("4531ACD1FE0023C7550D267B6B2FEE80922B14B2FFB90F04D4EB7C09B5D2D15DA82F2D7ECB1DBAC719905C5EECC423F1D86E25EDBE23C595D644AAF187E6E6DF")),
+ a=7,
+ b=bytes2long(hexdec("1CFF0806A31116DA29D8CFA54E57EB748BC5F377E49400FDD788B649ECA1AC4361834013B2AD7322480A89CA58E0CF74BC9E540C2ADD6897FAD0A3084F302ADC")),
+ x=bytes2long(hexdec("24D19CC64572EE30F396BF6EBBFD7A6C5213B3B3D7057CC825F91093A68CD762FD60611262CD838DC6B60AA7EEE804E28BC849977FAC33B4B530F1B120248A9A")),
+ y=bytes2long(hexdec("2BB312A43BD2CE6E0D020613C857ACDDCFBF061E91E5F2C3F32447C259F39B2C83AB156D77F1496BF7EB3351E1EE4E43DC1A18B91B24640B6DBB92CB1ADD371E")),
+ ),
"id-tc26-gost-3410-12-512-paramSetA": GOST3410Curve(
p=bytes2long(hexdec("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7")),
q=bytes2long(hexdec("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF27E69532F48D89116FF22B8D4E0560609B4B38ABFAD2B85DCACDB1411F10B275")),
return curve.exp(prv)
-def sign(curve, prv, digest, mode=2001):
+def sign(curve, prv, digest, rand=None, mode=2001):
""" Calculate signature for provided digest
:param GOST3410Curve curve: curve to use
:param long prv: private key
:param digest: digest for signing
:type digest: bytes, 32 or 64 bytes
- :returns: signature
+ :param rand: optional predefined random data used for k/r generation
+ :type rand: bytes, 32 or 64 bytes
+ :returns: signature, BE(S) || BE(R)
:rtype: bytes, 64 or 128 bytes
"""
size = MODE2SIZE[mode]
if e == 0:
e = 1
while True:
- k = bytes2long(urandom(size)) % q
+ if rand is None:
+ rand = urandom(size)
+ elif len(rand) != size:
+ raise ValueError("rand length != %d" % size)
+ k = bytes2long(rand) % q
if k == 0:
continue
r, _ = curve.exp(k)
def prv_unmarshal(prv):
- """Unmarshal private key
+ """Unmarshal little-endian private key
:param bytes prv: serialized private key
:rtype: long
:type pub: (long, long)
:rtype: bytes
+ :returns: LE(X) || LE(Y)
"""
size = MODE2SIZE[mode]
return (long2bytes(pub[1], size) + long2bytes(pub[0], size))[::-1]
def pub_unmarshal(pub, mode=2001):
"""Unmarshal public key
+ :param pub: LE(X) || LE(Y)
:type pub: bytes
:rtype: (long, long)
"""