diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2024-11-18 18:27:39 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2024-11-22 12:59:13 +0100 |
commit | c626e488ba7c1b3c5932dc887402b9ba4c320f29 (patch) | |
tree | 6763abcd3966eae5cb1bee1e7c9ab58da5270c7e /tests/custom | |
parent | be34c719162bd03c235e7cf051bdc8f0ae234637 (diff) |
tests: add crypto tests
Test RSA, ECDSA, and EdDSA signature verification,
and key generation with message signing and verification.
Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
Diffstat (limited to 'tests/custom')
-rw-r--r-- | tests/custom/05_crypto/01_rsa | 85 | ||||
-rw-r--r-- | tests/custom/05_crypto/02_ecdsa | 125 | ||||
-rw-r--r-- | tests/custom/05_crypto/03_eddsa | 70 | ||||
-rw-r--r-- | tests/custom/05_crypto/04_keygen | 80 |
4 files changed, 360 insertions, 0 deletions
diff --git a/tests/custom/05_crypto/01_rsa b/tests/custom/05_crypto/01_rsa new file mode 100644 index 0000000..c07c690 --- /dev/null +++ b/tests/custom/05_crypto/01_rsa @@ -0,0 +1,85 @@ +A few test vectors from SigGen15_186-2.txt in 186-2rsatestvectors.zip + +-- Testcase -- +{% + import { pk as pk_mbedtls } from 'crypto_mbedtls'; + import { pk as pk_openssl } from 'crypto_openssl'; + import { test_rsa } from './files/rsa.uc'; + + const pubkeyPem = `-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4LFLmc1hzT25wgdmaIQT +JPoxdPM85m/9UUOU00F40ppJSTJ2tndyM+fUaj5ovHyn6JnpAdVPbe4HScPkjd9o +aFhn7irmbfiOtWP22xN6n2sXWhEuDtqDaOiORe/hzhS8YBbVJjlicGavGHLHL2C5 +FhwdI37rNLD4QbPwiW+f4OFrD3Q1LRASksxGSn54YbvrhvbfYVHLJlQXxmxWXtiX +S9j8mE1d39TrkaPVI0zhtUZ/Ot43X4AuwHKT8SNu+jBovJGxWFUch1xdwKnW+jIb ++UIfCN6skQ41wcKFSe6O7YMwz3BZX/cLlLSZB+J2mKnZEfesBwavyxpKOf6ziwqA +SQIDAQAB +-----END PUBLIC KEY-----`; + + const pubkeyLines = split(pubkeyPem, '\n'); + const pubkeyB64 = join('', splice(splice(pubkeyLines, length(pubkeyLines)-1), 0, 1)); + const pubkey = b64dec(pubkeyB64); + + const msg1 = '544be0a4044edcdc1240f2182109f826d784ea613486dfd5221d3ba44d1525a934c13c5a81f885f3955da8d168e35d1b909121a89f832d1db232a85647f51c084fc727a4854a737efd0ce72e00091f3617721ae666ad337d3b9d5391e72364b1cde50948b84e8cc472d618f8928328bf95af3fe3300dda3de5e7a21dd5a9e7ec'; + const s1 = 'd0d03ccb3b30b7c9c4d6eeee2ec26d069246e019fb8fa2f3a9b72c9bbe231d93ce053df805a045e2ef6bd8d08bfb0c36922e5a6f10b947b2607f596b6cbd3c9eefef56f5396805e8b28b1ca182c78c0b12b9796aa856af69c35504f8acc7afa74bc0f77a1d61da94944057a9ee72d2f0a96cbaa2f64676f5318b71e56f519d0da1ce8f42db0ebe5045fcc726e39fb0032f2287918f9190f3fb3d4de542030441f6736c6205a2bcd2450eb411085311c7320baa4268fd2fd8bcc8ebfddbb60740cff0b3b00f618777ebcfb3468f309d923c957c8170727a5458ac2c9070f93cfc37d31cf9f1a35d0cc3abf25af8dc9e1590ce59ab39d01cf0c154ab8d0635c5e9'; + + const msg256 = '6504921a97cd57aa8f3863dc32e1f2d0b57aff63106e59f6afc3f9726b459388bae16b3e224f6aa7f4f471f13606eda6e1f1ac2b4df9ef8de921c07c2f4c8598d7a3d6ec4b368cb85ce61a74338221118a303e821c0f277b591af6795f50c40226127a2efacce4662fd7076c109eb59b18005e7165f6294a6976436ee397774e'; + const s256 = '335ffadc0b1b8bd2b1eb670dd246e76dcccdc955a1687a15f74aa3e1596ebd43e607c640525f89dda95809cfd065f1be4e4a249477d24f400d4d4c9438a0af95b26b28b416e42aa950e2a52851b52132048f1b1ce944322fc99c1aabb49b7fae4c2f0fef674b50adee3bbb5c6c33822b608e4b9577275ca20c710af9fc41b1c01d9c0ff6f0d8324dc08e1a76e232d8feaa06c73bbf64053bea35f1c528b2722764822ef1ff06246e75a9a22a10da4ea84fc2441bea24b35506f8447fcf69093c5d21ab0305cce2c7ea9ffac357c664b491fc55f2919ec490c38accbab378c252ac2df3845acff575ec7524cd2f586cca1497c74f24b299d6d6254c8cdb1d227d'; + + const msg384 = 'db2e9fbefd9832d29e6015b976734ff15a5c1177a9153b38e61d22c8a6ca9056ade10de054645da32eab4ad6eab6a4977bc28526771ad951bd301e2f5bec6911af44aab6cc0d30be1e1170615261026170edec3d4a5123a81af24f39674acf642880fda92c3cb30bb9b90ec4741c71378004cd26ea622025458b3c4f918bca37'; + const s384 = '211398c068e60ae49ee2b8b7cd96171373f4664cf45b62415d9bcf119184c03274aec2b3f04c598043d1d8642d53edad68d0e0bc33fdc65ed69334ab52303c839cbbaf0586508007dcf4ad5d297f568039dc16a1e6c08108a0050d5dcf47523e4868e4be72f20d789b06bcb92484efa2fcd903ae280f42a509dadbe62dce1c6d3aef6e4ff78a745c8500ff0572748abb1a220495c1c103a72c940206347c16ae6082dd1e4624525aa8ed8f118d09be2b8535d042b29ea3e2c6ab3c990b4b59501c3bb9d602c7813a55f4efc129f4dfd0fabe0cf6b8c8c1d9126c5440cbec5405722927389b3ca80a0872711388b5b099cce6effedfbd9befc9646ea8a258fdd0'; + + const msg512 = 'e4dba4692a6628b501f776fec7fe973d655154268f669bfd47c624aba3be5311d158619c588ab71aa0ac9accb52f0dbc488df350f77c7520ce67a3050d1e5e722bcd75081c2b0e64d0f3483cfc981eaa1c358fc7b9c2fb7ce78ed19513e96717fb2129d4feb1f63c96b4c77623a092b0ea306eb35da2f7ba9d23f4843d8837a8'; + const s512 = 'dfe58c84ec3ee5f11b265e8e7d99b416d8e7b166a38d2b0f9027be73887fad28947994a2bc227dafcb272d2d410af31afe16b96f51c9ddf62b417d03e2af63ea4a58d41e4649712177c85788d837fadd223c4ea7f635237a93b181fe1c0fd3bf2d8a7997cc9bb6ca099a0a36c4b9e91aa780a3d5edbc283cc316a153a101fc8c33d0035e6c1e1aca731ea765a1e5fe1406ef7ba8bb8f335b8d2e6ce0b9fee2f5416e4536b280fdc40860c17e9da6bb361128ee53754bf68f54c2878a4ac4d349703066a6fb96220e15285fcb41b398b4567c0c167762eb6ceb4f0537c7fb7103487c78a98093209771fc4f4a9821cc8eb11f15a0e35c54f6d5d31d8e8646c229'; + + // Test signature (from 04_keygen) against wrong key. + const s256_fail = '3f602377b1702ba2f8e67f39c5856341512ef0ad4fb9091017c5561b1041d9b6b351218ec457700a0a51b0829a34bb027126d95a2af373313d6026d840d98300f1a1d08c9343403a0e8d765fce97743dfbbaa8c57285e3d1d1d06eb9e7da16a9196908dc87cf1c615535cf7ae7c9c98fced8f1a4a188f1ec1490785076f923ad2f58f655c08de1083cfb6dea21f01cb822b7fd42ba57405a771b487b8aa50db6cd08fc00830c2e5833c0dd471f8280f9a0582870af365848e3d8f42145883c28cea10f401a7e008c497f73595ba8d6787bd21f89b7995a290b31014df281928fbbea4a73bb2989234167d3487ccb919f21993577006519a88a6208b4f70d91b2'; + + test_rsa(pk_mbedtls(), pubkey, 'SHA1', msg1, s1); + test_rsa(pk_mbedtls(), pubkey, 'SHA256', msg256, s256); + test_rsa(pk_mbedtls(), pubkey, 'SHA384', msg384, s384); + test_rsa(pk_mbedtls(), pubkey, 'SHA512', msg512, s512); + test_rsa(pk_mbedtls(), pubkey, 'SHA256', hexenc('Message'), s256_fail); + + test_rsa(pk_openssl(), pubkey, 'SHA1', msg1, s1); + test_rsa(pk_openssl(), pubkey, 'SHA256', msg256, s256); + test_rsa(pk_openssl(), pubkey, 'SHA384', msg384, s384); + test_rsa(pk_openssl(), pubkey, 'SHA512', msg512, s512); + test_rsa(pk_openssl(), pubkey, 'SHA256', hexenc('Message'), s256_fail); +%} +-- End -- + +-- Expect stdout -- +true +true +true +true +exception +true +true +true +true +exception +-- End -- + +-- Expect stderr -- +-- End -- + +-- File rsa.uc -- +export +function test_rsa(pk, pubkey, mdAlg, msg, s) +{ + const data = hexdec(msg); + const signature = hexdec(s); + pk.set_public_key(pubkey); + const pkey = pk.get_public_key(); + assert(pkey == pubkey); + let verify = 'undefined'; + try { + verify = pk.verify(mdAlg, data, signature); + } catch { + verify = 'exception' + } + print(verify, '\n'); +}; +-- End -- diff --git a/tests/custom/05_crypto/02_ecdsa b/tests/custom/05_crypto/02_ecdsa new file mode 100644 index 0000000..194a5ce --- /dev/null +++ b/tests/custom/05_crypto/02_ecdsa @@ -0,0 +1,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 -- diff --git a/tests/custom/05_crypto/03_eddsa b/tests/custom/05_crypto/03_eddsa new file mode 100644 index 0000000..468e3b7 --- /dev/null +++ b/tests/custom/05_crypto/03_eddsa @@ -0,0 +1,70 @@ +-- Testcase -- +{% + import { pk as pk_openssl } from 'crypto_openssl'; + import { test_eddsa } from './files/eddsa.uc'; + + const tests = { + test_1: { + // raw key = 'd75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a', + key: '302a300506032b6570032100d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a', + msg: '', + sig: 'e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b', + }, + test_2: { + // raw key = '3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c' + key: '302a300506032b65700321003d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c', + msg: '72', + sig: '92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00', + }, + test_3: { + // raw key = 'fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025' + key: '302a300506032b6570032100fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025', + msg: 'af82', + sig: '6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a', + }, + test_4_fail: { + key: '302a300506032b6570032100d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a', + msg: hexenc('Message'), + sig: '8e06163289f37c4e8a15cb3ddcf7379247f7820cea8488531579b32bf8aed7dc5dd4839e800ecc1911a8984344ef281db684b6f538d1c38a0a4570849e32fd02', + }, + }; + + for (test in tests) { + test_eddsa(pk_openssl(), tests[test]); + } +%} +-- End -- + +-- Expect stdout -- +true +true +true +exception +-- End -- + +-- Expect stderr -- +-- End -- + +-- File eddsa.uc -- +export +function test_eddsa(pk, test) +{ + const pkey = test.key; + const msg = test.msg; + const s = test.sig; + + const data = hexdec(msg); + const signature = hexdec(s); + + //pk.set_raw_public_key('ED25519', hexdec(pkey)); + pk.set_public_key(hexdec(pkey)); + + let verify = 'undefined'; + try { + verify = pk.verify(null, data, signature); + } catch { + verify = 'exception'; + } + print(verify, '\n'); +}; +-- End -- diff --git a/tests/custom/05_crypto/04_keygen b/tests/custom/05_crypto/04_keygen new file mode 100644 index 0000000..97a94b8 --- /dev/null +++ b/tests/custom/05_crypto/04_keygen @@ -0,0 +1,80 @@ +-- Testcase -- +{% + import { pk as pk_mbedtls } from 'crypto_mbedtls'; + import { pk as pk_openssl } from 'crypto_openssl'; + import { test_keygen } from './files/keygen.uc'; + + curves = [ + 'P-192', + 'P-224', + 'P-256', + 'P-384', + 'P-521', + 'brainpoolP256r1', + 'brainpoolP384r1', + 'brainpoolP512r1', + 'secp192k1', + 'secp224k1', + 'secp256k1', + ]; + + + for (c in curves) { + test_keygen(pk_openssl, pk_mbedtls, "EC", "SHA256", c); + test_keygen(pk_mbedtls, pk_openssl, "EC", "SHA256", c); + } + test_keygen(pk_openssl, pk_mbedtls, "RSA", "SHA256", 2048); + test_keygen(pk_mbedtls, pk_openssl, "RSA", "SHA256", 2048); + + test_keygen(pk_openssl, pk_openssl, "ED25519", null); +%} +-- End -- + +-- Expect stdout -- +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +true +-- End -- + +-- Expect stderr -- +-- End -- + +-- File keygen.uc -- +export +function test_keygen(pk_init, other_pk_init, type, md_alg, ...args) +{ + let pk = pk_init(); + let res = pk.keygen(type, ...args); + let pkey = pk.get_public_key(); + const data = "Message"; + const signature = pk.sign(md_alg, data); + + let other_pk = other_pk_init(); + other_pk.set_public_key(pkey); + + const verify = other_pk.verify(md_alg, data, signature); + print(verify, '\n'); +}; +-- End -- |