# 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
from pygost.gost3410 import GOST3410Curve
from pygost.gost3410 import public_key
from pygost.gost3410 import sign
+from pygost.gost3410 import uv2xy
from pygost.gost3410 import verify
+from pygost.gost3410 import xy2uv
from pygost.utils import bytes2long
from pygost.utils import hexdec
+from pygost.utils import hexenc
from pygost.utils import long2bytes
class Test34102012(TestCase):
+ def test_1(self):
+ """Test vector from 34.10-2012 standard itself
+ """
+ curve = CURVES["id-GostR3410-2001-TestParamSet"]
+ prv = bytes2long(hexdec("7A929ADE789BB9BE10ED359DD39A72C11B60961F49397EEE1D19CE9891EC3B28"))
+ digest = hexdec("2DFBC1B372D89A1188C09C52E0EEC61FCE52032AB1022E8E67ECE6672B043EE5")
+ rand = hexdec("77105C9B20BCD3122823C8CF6FCC7B956DE33814E95B7FE64FED924594DCEAB3")
+ signature = sign(curve, prv, digest, rand)
+ r = "41aa28d2f1ab148280cd9ed56feda41974053554a42767b83ad043fd39dc0493"
+ s = "01456c64ba4642a1653c235a98a60249bcd6d3f746b631df928014f6c5bf9c40"
+ self.assertSequenceEqual(hexenc(signature), s + r)
+
+ 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)
+
def test_gcl3(self):
""" Test vector from libgcl3
"""
self.assertNotIn(b"\x00" * 8, s)
+class TestUVXYConversion(TestCase):
+ """Twisted Edwards to Weierstrass coordinates conversion and vice versa
+ """
+ def test_curve1(self):
+ c = CURVES["id-tc26-gost-3410-2012-256-paramSetA"]
+ u, v = (0x0D, bytes2long(hexdec("60CA1E32AA475B348488C38FAB07649CE7EF8DBE87F22E81F92B2592DBA300E7")))
+ self.assertEqual(uv2xy(c, u, v), (c.x, c.y))
+ self.assertEqual(xy2uv(c, c.x, c.y), (u, v))
+
+ def test_curve2(self):
+ c = CURVES["id-tc26-gost-3410-2012-512-paramSetC"]
+ u, v = (0x12, bytes2long(hexdec("469AF79D1FB1F5E16B99592B77A01E2A0FDFB0D01794368D9A56117F7B38669522DD4B650CF789EEBF068C5D139732F0905622C04B2BAAE7600303EE73001A3D")))
+ self.assertEqual(uv2xy(c, u, v), (c.x, c.y))
+ self.assertEqual(xy2uv(c, c.x, c.y), (u, v))
+
+
class Test34102012SESPAKE(TestCase):
"""Test vectors for multiplication from :rfc:`8133`
"""