summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/05_crypto/02_ecdsa
blob: 194a5ce6e6e6b8c187958ea632b09a9c2695a3a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Some test vectors from RFC 6979

-- Testcase --
{%
	import { pk as pk_mbedtls } from 'crypto_mbedtls';
	import { pk as pk_openssl } from 'crypto_openssl';
	import { test_ecdsa } from './files/ecdsa.uc';

	const tests = {
		p192: {
			alg: 'SHA1',
			data: 'sample',
			// Ux = 'AC2C77F529F91689FEA0EA5EFEC7F210D8EEA0B9E047ED56'
			// Uy = '3BC723E57670BD4887EBC732C523063D0A7C957BC97C1C43'
			// DER encoded public key
			key: '3049301306072a8648ce3d020106082a8648ce3d03010103320004ac2c77f529f91689fea0ea5efec7f210d8eea0b9e047ed563bc723e57670bd4887ebc732c523063d0a7c957bc97c1c43',
			// r = '98C6BD12B23EAF5E2A2045132086BE3EB8EBD62ABF6698FF'
			// s = '57A22B07DEA9530F8DE9471B1DC6624472E8E2844BC25B64'
			// DER encoded signature
			sig: '303502190098c6bd12b23eaf5e2a2045132086be3eb8ebd62abf6698ff021857a22b07dea9530f8de9471b1dc6624472e8e2844bc25b64',
		},
		p224: {
			alg: 'SHA224',
			data: 'sample',
			// Ux = '00CF08DA5AD719E42707FA431292DEA11244D64FC51610D94B130D6C'
			// Uy = 'EEAB6F3DEBE455E3DBF85416F7030CBD94F34F2D6F232C69F3C1385A'
			// DER encoded public key
			 key: '304e301006072a8648ce3d020106052b81040021033a000400cf08da5ad719e42707fa431292dea11244d64fc51610d94b130d6ceeab6f3debe455e3dbf85416f7030cbd94f34f2d6f232c69f3c1385a',
			// r = '1CDFE6662DDE1E4A1EC4CDEDF6A1F5A2FB7FBD9145C12113E6ABFD3E'
			// s = 'A6694FD7718A21053F225D3F46197CA699D45006C06F871808F43EBC'
			// DER encoded signature
			sig: '303d021c1cdfe6662dde1e4a1ec4cdedf6a1f5a2fb7fbd9145c12113e6abfd3e021d00a6694fd7718a21053f225d3f46197ca699d45006c06f871808f43ebc',
		},
		p256: {
			alg: 'SHA256',
			data: 'sample',
			// Ux = '60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6'
			// Uy = '7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299'
			// DER encoded public key
			key: '3059301306072a8648ce3d020106082a8648ce3d0301070342000460fed4ba255a9d31c961eb74c6356d68c049b8923b61fa6ce669622e60f29fb67903fe1008b8bc99a41ae9e95628bc64f2f1b20c2d7e9f5177a3c294d4462299',
			// r = 'EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716'
			// s = 'F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8'
			// DER encoded signature
			sig: '3046022100efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716022100f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8',
		},
		p384: {
			alg: 'SHA384',
			data: 'sample',
			// Ux = 'EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64DEF8F0EA9055866064A254515480BC13'
			// Uy = '8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1288B231C3AE0D4FE7344FD2533264720'
			// DER encoded public key
			key: '3076301006072a8648ce3d020106052b8104002203620004ec3a4e415b4e19a4568618029f427fa5da9a8bc4ae92e02e06aae5286b300c64def8f0ea9055866064a254515480bc138015d9b72d7d57244ea8ef9ac0c621896708a59367f9dfb9f54ca84b3f1c9db1288b231c3ae0d4fe7344fd2533264720',
			// r = '94EDBB92A5ECB8AAD4736E56C691916B3F88140666CE9FA73D64C4EA95AD133C81A648152E44ACF96E36DD1E80FABE46'
			// s = '99EF4AEB15F178CEA1FE40DB2603138F130E740A19624526203B6351D0A3A94FA329C145786E679E7B82C71A38628AC8'
			// DER encoded signature
			sig: '306602310094edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fabe4602310099ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38628ac8',
		},
		p521: {
			alg: 'SHA512',
			data: 'sample',
			// Ux = '01894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD371123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F5023A4'
			// Uy = '00493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A28A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDFCF5'
			// DER encoded public key
			key: '30819b301006072a8648ce3d020106052b81040023038186000401894550d0785932e00eaa23b694f213f8c3121f86dc97a04e5a7167db4e5bcd371123d46e45db6b5d5370a7f20fb633155d38ffa16d2bd761dcac474b9a2f5023a400493101c962cd4d2fddf782285e64584139c2f91b47f87ff82354d6630f746a28a0db25741b5b34a828008b22acc23f924faafbd4d33f81ea66956dfeaa2bfdfcf5',
			// r = 'C328FAFCBD79DD77850370C46325D987CB525569FB63C5D3BC53950E6D4C5F174E25A1EE9017B5D450606ADD152B534931D7D4E8455CC91F9B15BF05EC36E377FA'
			// s = '617CCE7CF5064806C467F678D3B4080D6F1CC50AF26CA209417308281B68AF282623EAA63E5B5C0723D8B8C37FF0777B1A20F8CCB1DCCC43997F1EE0E44DA4A67A'
			// DER encoded signature
			sig: '308187024200c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add152b534931d7d4e8455cc91f9b15bf05ec36e377fa0241617cce7cf5064806c467f678d3b4080d6f1cc50af26ca209417308281b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20f8ccb1dccc43997f1ee0e44da4a67a',
		},
		p192_fail: {
			// Test signature (from 04_keygen) against wrong key.
			alg: 'SHA1',
			data: 'Message',
			key: '3049301306072a8648ce3d020106082a8648ce3d03010103320004ac2c77f529f91689fea0ea5efec7f210d8eea0b9e047ed563bc723e57670bd4887ebc732c523063d0a7c957bc97c1c43',
			sig: '303402181e2dbd03ca5a4dbbdda18a967969c0ac246ef6b0562f572e021838e05d8ba04345299188147b2636015b777223016f32cde2',
		},
	};

	for (test in tests) {
		test_ecdsa(pk_mbedtls(), tests[test]);
		test_ecdsa(pk_openssl(), tests[test]);
	}
%}
-- End --

-- Expect stdout --
true
true
true
true
true
true
true
true
true
true
exception
exception
-- End --

-- Expect stderr --
-- End --

-- File ecdsa.uc --
export
function test_ecdsa(pk, test)
{
	const mdAlg = test.alg;
	const data = test.data;
	const key = test.key;
	const sig = test.sig;

	const pubkeyDer = hexdec(key);
	pk.set_public_key(pubkeyDer);
	const pkey = pk.get_public_key();
	assert(pkey == pubkeyDer);
	let verify = 'undefined';
	try {
		verify = pk.verify(mdAlg, data, hexdec(sig));
	} catch {
		verify = 'exception';
	}
	print(verify, '\n');
};
-- End --