From 1f63639cc3e55ab4d02b7cf6fc645cdbb1ea8e08 Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Sun, 26 Mar 2023 03:34:25 +0900 Subject: [PATCH 1/3] Add interop testing with t_cose. --- tests/test_cose_hpke.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_cose_hpke.py b/tests/test_cose_hpke.py index 8b5e46f3..1190da69 100644 --- a/tests/test_cose_hpke.py +++ b/tests/test_cose_hpke.py @@ -7,9 +7,11 @@ Tests for COSE. """ +from cryptography.hazmat.primitives.asymmetric import ec import pytest from cwt import COSE, COSEKey +from cwt.algs.ec2 import EC2Key class TestCOSE_HPKE: @@ -300,3 +302,31 @@ def test_cose_hpke_kem_0x0021(self, kdf, aead): ) recipient = COSE.new() assert b"This is the content." == recipient.decode(encoded, rsk) + + def test_cose_hpke_with_t_cose_example(self): + + pkr_buf = bytes(bytearray([ + 0x04, 0x6d, 0x35, 0xe7, 0xa0, 0x75, 0x42, 0xc1, 0x2c, 0x6d, 0x2a, 0x0d, + 0x2d, 0x45, 0xa4, 0xe9, 0x46, 0x68, 0x95, 0x27, 0x65, 0xda, 0x9f, 0x68, + 0xb4, 0x7c, 0x75, 0x5f, 0x38, 0x00, 0xfb, 0x95, 0x85, 0xdd, 0x7d, 0xed, + 0xa7, 0xdb, 0xfd, 0x2d, 0xf0, 0xd1, 0x2c, 0xf3, 0xcc, 0x3d, 0xb6, 0xa0, + 0x75, 0xd6, 0xb9, 0x35, 0xa8, 0x2a, 0xac, 0x3c, 0x38, 0xa5, 0xb7, 0xe8, + 0x62, 0x80, 0x93, 0x84, 0x55 + ])) + pkr = COSEKey.new(EC2Key.to_cose_key(ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP256R1(), pkr_buf))) + + skr_buf = bytes(bytearray([ + 0x37, 0x0b, 0xaf, 0x20, 0x45, 0x17, 0x01, 0xf6, 0x64, 0xe1, 0x28, 0x57, + 0x4e, 0xb1, 0x7a, 0xd3, 0x5b, 0xdd, 0x96, 0x65, 0x0a, 0xa8, 0xa3, 0xcd, + 0xbd, 0xd6, 0x6f, 0x57, 0xa8, 0xcc, 0xe8, 0x09 + ])) + params = EC2Key.to_cose_key(ec.derive_private_key(int.from_bytes(skr_buf, byteorder="big"), ec.SECP256R1())) + params[2] = b"fixed_test_key_p256r1" + skr = COSEKey.new(params) + + msg = bytes.fromhex("d8608443a10101a10550ff96274fe513d6d5556bd5149e954c53582305126370fc3fbfc1d1bc1710d58e6de52be729d30db2497a10f83e52edd29438fb9809818343a10120a2238410010158410403be32ff3820be4f311e0112d2bd58b907d5a33141a5d112f6729669f9f310a11774246487d404b96e32300eb4b18de88d32bb85458cc126fc8a6f2f59ce888e045566697865645f746573745f6b65795f7032353672315820bc7b8684ba8b5170f04fa6feccc338266cfe7b278bd95b1c03bad0023f58f880") + + pt = bytes.fromhex("5468697320697320746865207061796c6f6164") + + recipient = COSE.new() + assert pt == recipient.decode(msg, skr) From a8c561c6d91458cc31f35f4584318fa3ddb6194d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Mar 2023 01:25:24 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_cose_hpke.py | 129 +++++++++++++++++++++++++++++++++++----- 1 file changed, 113 insertions(+), 16 deletions(-) diff --git a/tests/test_cose_hpke.py b/tests/test_cose_hpke.py index 1190da69..30c5c672 100644 --- a/tests/test_cose_hpke.py +++ b/tests/test_cose_hpke.py @@ -7,8 +7,8 @@ Tests for COSE. """ -from cryptography.hazmat.primitives.asymmetric import ec import pytest +from cryptography.hazmat.primitives.asymmetric import ec from cwt import COSE, COSEKey from cwt.algs.ec2 import EC2Key @@ -304,27 +304,124 @@ def test_cose_hpke_kem_0x0021(self, kdf, aead): assert b"This is the content." == recipient.decode(encoded, rsk) def test_cose_hpke_with_t_cose_example(self): - - pkr_buf = bytes(bytearray([ - 0x04, 0x6d, 0x35, 0xe7, 0xa0, 0x75, 0x42, 0xc1, 0x2c, 0x6d, 0x2a, 0x0d, - 0x2d, 0x45, 0xa4, 0xe9, 0x46, 0x68, 0x95, 0x27, 0x65, 0xda, 0x9f, 0x68, - 0xb4, 0x7c, 0x75, 0x5f, 0x38, 0x00, 0xfb, 0x95, 0x85, 0xdd, 0x7d, 0xed, - 0xa7, 0xdb, 0xfd, 0x2d, 0xf0, 0xd1, 0x2c, 0xf3, 0xcc, 0x3d, 0xb6, 0xa0, - 0x75, 0xd6, 0xb9, 0x35, 0xa8, 0x2a, 0xac, 0x3c, 0x38, 0xa5, 0xb7, 0xe8, - 0x62, 0x80, 0x93, 0x84, 0x55 - ])) + pkr_buf = bytes( + bytearray( + [ + 0x04, + 0x6D, + 0x35, + 0xE7, + 0xA0, + 0x75, + 0x42, + 0xC1, + 0x2C, + 0x6D, + 0x2A, + 0x0D, + 0x2D, + 0x45, + 0xA4, + 0xE9, + 0x46, + 0x68, + 0x95, + 0x27, + 0x65, + 0xDA, + 0x9F, + 0x68, + 0xB4, + 0x7C, + 0x75, + 0x5F, + 0x38, + 0x00, + 0xFB, + 0x95, + 0x85, + 0xDD, + 0x7D, + 0xED, + 0xA7, + 0xDB, + 0xFD, + 0x2D, + 0xF0, + 0xD1, + 0x2C, + 0xF3, + 0xCC, + 0x3D, + 0xB6, + 0xA0, + 0x75, + 0xD6, + 0xB9, + 0x35, + 0xA8, + 0x2A, + 0xAC, + 0x3C, + 0x38, + 0xA5, + 0xB7, + 0xE8, + 0x62, + 0x80, + 0x93, + 0x84, + 0x55, + ] + ) + ) pkr = COSEKey.new(EC2Key.to_cose_key(ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP256R1(), pkr_buf))) - skr_buf = bytes(bytearray([ - 0x37, 0x0b, 0xaf, 0x20, 0x45, 0x17, 0x01, 0xf6, 0x64, 0xe1, 0x28, 0x57, - 0x4e, 0xb1, 0x7a, 0xd3, 0x5b, 0xdd, 0x96, 0x65, 0x0a, 0xa8, 0xa3, 0xcd, - 0xbd, 0xd6, 0x6f, 0x57, 0xa8, 0xcc, 0xe8, 0x09 - ])) + skr_buf = bytes( + bytearray( + [ + 0x37, + 0x0B, + 0xAF, + 0x20, + 0x45, + 0x17, + 0x01, + 0xF6, + 0x64, + 0xE1, + 0x28, + 0x57, + 0x4E, + 0xB1, + 0x7A, + 0xD3, + 0x5B, + 0xDD, + 0x96, + 0x65, + 0x0A, + 0xA8, + 0xA3, + 0xCD, + 0xBD, + 0xD6, + 0x6F, + 0x57, + 0xA8, + 0xCC, + 0xE8, + 0x09, + ] + ) + ) params = EC2Key.to_cose_key(ec.derive_private_key(int.from_bytes(skr_buf, byteorder="big"), ec.SECP256R1())) params[2] = b"fixed_test_key_p256r1" skr = COSEKey.new(params) - msg = bytes.fromhex("d8608443a10101a10550ff96274fe513d6d5556bd5149e954c53582305126370fc3fbfc1d1bc1710d58e6de52be729d30db2497a10f83e52edd29438fb9809818343a10120a2238410010158410403be32ff3820be4f311e0112d2bd58b907d5a33141a5d112f6729669f9f310a11774246487d404b96e32300eb4b18de88d32bb85458cc126fc8a6f2f59ce888e045566697865645f746573745f6b65795f7032353672315820bc7b8684ba8b5170f04fa6feccc338266cfe7b278bd95b1c03bad0023f58f880") + msg = bytes.fromhex( + "d8608443a10101a10550ff96274fe513d6d5556bd5149e954c53582305126370fc3fbfc1d1bc1710d58e6de52be729d30db2497a10f83e52edd29438fb9809818343a10120a2238410010158410403be32ff3820be4f311e0112d2bd58b907d5a33141a5d112f6729669f9f310a11774246487d404b96e32300eb4b18de88d32bb85458cc126fc8a6f2f59ce888e045566697865645f746573745f6b65795f7032353672315820bc7b8684ba8b5170f04fa6feccc338266cfe7b278bd95b1c03bad0023f58f880" + ) pt = bytes.fromhex("5468697320697320746865207061796c6f6164") From cc67c718afd71112cf7cec32808072a869ee9b30 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Mar 2023 01:25:24 +0000 Subject: [PATCH 3/3] Add interop test code with t_cose. --- tests/test_cose_hpke.py | 132 +++++++++++++++++++++++++++++++++++----- 1 file changed, 117 insertions(+), 15 deletions(-) diff --git a/tests/test_cose_hpke.py b/tests/test_cose_hpke.py index 1190da69..12a56a47 100644 --- a/tests/test_cose_hpke.py +++ b/tests/test_cose_hpke.py @@ -7,8 +7,8 @@ Tests for COSE. """ -from cryptography.hazmat.primitives.asymmetric import ec import pytest +from cryptography.hazmat.primitives.asymmetric import ec from cwt import COSE, COSEKey from cwt.algs.ec2 import EC2Key @@ -305,27 +305,129 @@ def test_cose_hpke_kem_0x0021(self, kdf, aead): def test_cose_hpke_with_t_cose_example(self): - pkr_buf = bytes(bytearray([ - 0x04, 0x6d, 0x35, 0xe7, 0xa0, 0x75, 0x42, 0xc1, 0x2c, 0x6d, 0x2a, 0x0d, - 0x2d, 0x45, 0xa4, 0xe9, 0x46, 0x68, 0x95, 0x27, 0x65, 0xda, 0x9f, 0x68, - 0xb4, 0x7c, 0x75, 0x5f, 0x38, 0x00, 0xfb, 0x95, 0x85, 0xdd, 0x7d, 0xed, - 0xa7, 0xdb, 0xfd, 0x2d, 0xf0, 0xd1, 0x2c, 0xf3, 0xcc, 0x3d, 0xb6, 0xa0, - 0x75, 0xd6, 0xb9, 0x35, 0xa8, 0x2a, 0xac, 0x3c, 0x38, 0xa5, 0xb7, 0xe8, - 0x62, 0x80, 0x93, 0x84, 0x55 - ])) + # t_cose/examples/keys/init_keys_psa.c:fixed_test_p256r1_public_key + pkr_buf = bytes( + bytearray( + [ + 0x04, + 0x6D, + 0x35, + 0xE7, + 0xA0, + 0x75, + 0x42, + 0xC1, + 0x2C, + 0x6D, + 0x2A, + 0x0D, + 0x2D, + 0x45, + 0xA4, + 0xE9, + 0x46, + 0x68, + 0x95, + 0x27, + 0x65, + 0xDA, + 0x9F, + 0x68, + 0xB4, + 0x7C, + 0x75, + 0x5F, + 0x38, + 0x00, + 0xFB, + 0x95, + 0x85, + 0xDD, + 0x7D, + 0xED, + 0xA7, + 0xDB, + 0xFD, + 0x2D, + 0xF0, + 0xD1, + 0x2C, + 0xF3, + 0xCC, + 0x3D, + 0xB6, + 0xA0, + 0x75, + 0xD6, + 0xB9, + 0x35, + 0xA8, + 0x2A, + 0xAC, + 0x3C, + 0x38, + 0xA5, + 0xB7, + 0xE8, + 0x62, + 0x80, + 0x93, + 0x84, + 0x55, + ] + ) + ) pkr = COSEKey.new(EC2Key.to_cose_key(ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP256R1(), pkr_buf))) - skr_buf = bytes(bytearray([ - 0x37, 0x0b, 0xaf, 0x20, 0x45, 0x17, 0x01, 0xf6, 0x64, 0xe1, 0x28, 0x57, - 0x4e, 0xb1, 0x7a, 0xd3, 0x5b, 0xdd, 0x96, 0x65, 0x0a, 0xa8, 0xa3, 0xcd, - 0xbd, 0xd6, 0x6f, 0x57, 0xa8, 0xcc, 0xe8, 0x09 - ])) + # t_cose/examples/keys/init_keys_psa.c:fixed_test_p256r1_private_key + skr_buf = bytes( + bytearray( + [ + 0x37, + 0x0B, + 0xAF, + 0x20, + 0x45, + 0x17, + 0x01, + 0xF6, + 0x64, + 0xE1, + 0x28, + 0x57, + 0x4E, + 0xB1, + 0x7A, + 0xD3, + 0x5B, + 0xDD, + 0x96, + 0x65, + 0x0A, + 0xA8, + 0xA3, + 0xCD, + 0xBD, + 0xD6, + 0x6F, + 0x57, + 0xA8, + 0xCC, + 0xE8, + 0x09, + ] + ) + ) params = EC2Key.to_cose_key(ec.derive_private_key(int.from_bytes(skr_buf, byteorder="big"), ec.SECP256R1())) params[2] = b"fixed_test_key_p256r1" skr = COSEKey.new(params) - msg = bytes.fromhex("d8608443a10101a10550ff96274fe513d6d5556bd5149e954c53582305126370fc3fbfc1d1bc1710d58e6de52be729d30db2497a10f83e52edd29438fb9809818343a10120a2238410010158410403be32ff3820be4f311e0112d2bd58b907d5a33141a5d112f6729669f9f310a11774246487d404b96e32300eb4b18de88d32bb85458cc126fc8a6f2f59ce888e045566697865645f746573745f6b65795f7032353672315820bc7b8684ba8b5170f04fa6feccc338266cfe7b278bd95b1c03bad0023f58f880") + # from the output of t_cose/examples/encryption_examples + msg = bytes.fromhex( + "d8608443a10101a10550d146ace746b2a860b273f03f0534d0bb58231405f78e061fb13f400df1bfd156c9b9f7460325dfe9adc762e9d208196a3bdfd8dc19818343a10120a22384100101584104371c3c31426b7f84340757509836ab7ed60bca356cf9570a2858473827297959633fd597bc4202b409552ada2c81825897eafb0a402d6ea2f74fbe0dcea2b47e045566697865645f746573745f6b65795f7032353672315820c452a5bc660a268fa04c05744d16f17dc28d7593897d2c3dbc54223d9ca5eec6" + ) + # from the output of t_cose/examples/encryption_examples pt = bytes.fromhex("5468697320697320746865207061796c6f6164") recipient = COSE.new()