def test_ecdh_direct_hkdf_apply_with_ecdh_ss_p256(self): rec = Recipient.from_jwk({ "kty": "EC", "alg": "ECDH-SS+HKDF-256", "d": "kwibx3gas6Kz1V2fyQHKSnr-ybflddSjN0eOnbmLmyo", "crv": "P-256", "kid": "01", "x": "-eZXC6nV-xgthy8zZMCN8pcYSeE2XfWWqckA2fsxHPc", "y": "BGU5soLgsu_y7GN2I3EPUXS9EZ7Sw0qif-V70JtInFI", }) with open(key_path("public_key_es256.pem")) as key_file: pub_key = COSEKey.from_pem(key_file.read(), kid="01") enc_key = rec.apply(recipient_key=pub_key, salt=token_bytes(32), context={"alg": "A128GCM"}) ctx = COSE.new(alg_auto_inclusion=True) encoded = ctx.encode_and_encrypt(b"Hello world!", enc_key, recipients=[rec]) with open(key_path("private_key_es256.pem")) as key_file: priv_key = COSEKey.from_pem(key_file.read(), kid="01", alg="ECDH-SS+HKDF-256") assert b"Hello world!" == ctx.decode(encoded, priv_key, context={"alg": "A128GCM"})
def test_ecdh_direct_hkdf_apply_without_salt(self): rec = Recipient.from_jwk({ "kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256" }) with open(key_path("public_key_es256.pem")) as key_file: pub_key = COSEKey.from_pem(key_file.read(), kid="01") enc_key = rec.apply( recipient_key=pub_key, context=[ 1, [None, None, None], [None, None, None], [128, cbor2.dumps({1: -25})], ], ) ctx = COSE.new(alg_auto_inclusion=True) encoded = ctx.encode_and_encrypt(b"Hello world!", enc_key, recipients=[rec]) with open(key_path("private_key_es256.pem")) as key_file: priv_key = COSEKey.from_pem(key_file.read(), kid="01", alg="ECDH-ES+HKDF-256") assert b"Hello world!" == ctx.decode(encoded, priv_key, context={"alg": "A128GCM"})
def test_okp_key_derive_key_with_raw_context(self): with open(key_path("private_key_x25519.pem")) as key_file: private_key = COSEKey.from_pem(key_file.read(), alg="ECDH-SS+HKDF-256") pub_key = COSEKey.from_jwk({ "kty": "OKP", "alg": "ECDH-ES+HKDF-256", "kid": "01", "crv": "X25519", "x": "y3wJq3uXPHeoCO4FubvTc7VcBuqpvUrSvU6ZMbHDTCI", # "d": "vsJ1oX5NNi0IGdwGldiac75r-Utmq3Jq4LGv48Q_Qc4", }) context = [ 1, [None, None, None], [None, None, None], [128, cbor2.dumps({1: -25})], ] try: private_key.derive_key(context, public_key=pub_key) except Exception: pytest.fail("derive_key() should not fail.")
def test_ecdh_direct_hkdf_apply_with_party_u_nonce(self): nonce = token_bytes(32) rec = Recipient.from_jwk({ "kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256" }) with open(key_path("public_key_es256.pem")) as key_file: pub_key = COSEKey.from_pem(key_file.read(), kid="01") context = [ 1, [None, nonce, None], [None, None, None], [128, cbor2.dumps({1: -25})], ] enc_key = rec.apply(recipient_key=pub_key, context=context) assert nonce == rec._unprotected[-22] ctx = COSE.new(alg_auto_inclusion=True) encoded = ctx.encode_and_encrypt(b"Hello world!", enc_key, recipients=[rec]) with open(key_path("private_key_es256.pem")) as key_file: priv_key = COSEKey.from_pem(key_file.read(), kid="01", alg="ECDH-ES+HKDF-256") assert b"Hello world!" == ctx.decode(encoded, priv_key, context=context)
def test_ecdh_direct_hkdf_apply_with_ecdh_ss_p521_with_default_salt(self): rec = Recipient.from_jwk({ "kty": "EC", "alg": "ECDH-SS+HKDF-512", "d": "ADYyo73ZKicOjwGDYQ_ybZKnVzdAcxGm9OVAxQjzgVM4jaS-Iwtkz90oLdDz3shgKlDgtRK2Aa9lMhqR94hBo4IE", "crv": "P-521", "kid": "01", "x": "APkZitSJMJUMB-iPCt47sWu_CrnUHg6IAR4qjmHON-2u41Rjg6DNOS0LZYJJt-AVH5NgGVi8ElIfjo71b9HXCTOc", "y": "ASx-Cb--149HJ-e1KlSaY-1BOhwOdcTkxSt8BGbW7_hnGfzHsoXM3ywwNcp1Yad-FHUKwmCyMelMQEn2Rh4V2l3I", "salt": "aabbccddeeff", }) with open(key_path("public_key_es512.pem")) as key_file: pub_key = COSEKey.from_pem(key_file.read(), kid="01") enc_key = rec.apply(recipient_key=pub_key, context={"alg": "A128GCM"}) ctx = COSE.new(alg_auto_inclusion=True) encoded = ctx.encode_and_encrypt(b"Hello world!", enc_key, recipients=[rec]) with open(key_path("private_key_es512.pem")) as key_file: priv_key = COSEKey.from_pem(key_file.read(), kid="01", alg="ECDH-SS+HKDF-512") assert b"Hello world!" == ctx.decode(encoded, priv_key, context={"alg": "A128GCM"})
def test_aes_key_wrap_apply_with_invalid_key(self): key = COSEKey.from_symmetric_key(key="xxx", alg="HS256", kid="01") ctx = AESKeyWrap({1: -3}, {}, sender_key=COSEKey.from_symmetric_key(alg="A128KW")) with pytest.raises(EncodeError) as err: ctx.apply(key, context={"alg": "A128GCM"}) pytest.fail("apply() should fail.") assert "Failed to wrap key." in str(err.value)
def test_aes_key_wrap_wrap_key_without_alg(self): key = COSEKey.from_symmetric_key(alg="A128GCM", kid="01") ctx = AESKeyWrap({1: -3}, {}, sender_key=COSEKey.from_symmetric_key(alg="A128KW")) with pytest.raises(ValueError) as err: ctx.extract(key=key) pytest.fail("extract() should fail.") assert "alg should be set." in str(err.value)
def test_aes_key_wrap_wrap_key_without_ciphertext(self): key = COSEKey.from_symmetric_key(alg="A128GCM", kid="01") ctx = AESKeyWrap({1: -3}, {}, sender_key=COSEKey.from_symmetric_key(alg="A128KW")) with pytest.raises(DecodeError) as err: ctx.extract(key=key, alg="A128GCM") pytest.fail("extract() should fail.") assert "Failed to decode key." in str(err.value)
def test_ecdh_aes_key_wrap_extract_without_context(self): enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305") ctx = ECDH_AESKeyWrap({1: -29}, {4: b"01"}) with pytest.raises(ValueError) as err: ctx.extract(enc_key, alg="ChaCha20/Poly1305") pytest.fail("extract() should fail.") assert "context should be set." in str(err.value)
def test_ec2_key_derive_key_with_invalid_alg(self, invalid_alg): private_key = EC2Key({ 1: 2, -2: b"\xa7\xddc*\xff\xc2?\x8b\xf8\x9c:\xad\xccDF\x9cZ \x04P\xef\x99\x0c=\xe6 w1\x08&\xba\xd9", -3: b"\xe2\xdb\xef\xfe\xb8\x8a\x12\xf27\xcb\x15:\x8a\xb9\x1a90B\x1a\x19^\xbc\xdc\xde\r\xb9s\xc1P\xf3\xaa\xdd", -4: b'\xe9\x16\x0c\xa96\x8d\xfa\xbc\xd5\xda"ua\xec\xf7\x96\r\x15\xf7_\xf3rb{\xb1\xde;\x99\x88\xafNh', -1: 1, 3: -25, }) pub_key = COSEKey.from_jwk({ "kty": "EC", "kid": "01", "crv": "P-256", "x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8", "y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4", }) with pytest.raises(ValueError) as err: private_key.derive_key({"alg": invalid_alg}, public_key=pub_key) pytest.fail("derive_key() should fail.") assert f"Unsupported or unknown alg for context information: {invalid_alg}." in str( err.value)
def test_ec2_key_derive_key(self, alg): private_key = EC2Key({ 1: 2, -2: b"\xa7\xddc*\xff\xc2?\x8b\xf8\x9c:\xad\xccDF\x9cZ \x04P\xef\x99\x0c=\xe6 w1\x08&\xba\xd9", -3: b"\xe2\xdb\xef\xfe\xb8\x8a\x12\xf27\xcb\x15:\x8a\xb9\x1a90B\x1a\x19^\xbc\xdc\xde\r\xb9s\xc1P\xf3\xaa\xdd", -4: b'\xe9\x16\x0c\xa96\x8d\xfa\xbc\xd5\xda"ua\xec\xf7\x96\r\x15\xf7_\xf3rb{\xb1\xde;\x99\x88\xafNh', -1: 1, 3: -25, }) pub_key = COSEKey.from_jwk({ "kty": "EC", "kid": "01", "crv": "P-256", "x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8", "y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4", }) try: derived_key = private_key.derive_key({"alg": alg}, public_key=pub_key) assert derived_key.kty == 4 except Exception: pytest.fail("derive_key() should not fail.")
def test_ecdh_direct_hkdf_extract_with_invalid_private_key( self, recipient_public_key): rec = Recipient.from_jwk({ "kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256" }) enc_key = rec.apply(recipient_key=recipient_public_key, context={"alg": "A128GCM"}) ctx = COSE.new(alg_auto_inclusion=True) encoded = ctx.encode_and_encrypt(b"Hello world!", enc_key, recipients=[rec]) another_priv_key = COSEKey.from_jwk({ "kty": "EC", "alg": "ECDH-ES+HKDF-256", "kid": "01", "crv": "P-256", "x": "-eZXC6nV-xgthy8zZMCN8pcYSeE2XfWWqckA2fsxHPc", "y": "BGU5soLgsu_y7GN2I3EPUXS9EZ7Sw0qif-V70JtInFI", "d": "kwibx3gas6Kz1V2fyQHKSnr-ybflddSjN0eOnbmLmyo", }) with pytest.raises(DecodeError) as err: ctx.decode(encoded, another_priv_key, context={"alg": "A128GCM"}) pytest.fail("decode() should fail.") assert "Failed to decrypt." in str(err.value)
def test_aes_key_wrap_constructor_with_invalid_alg(self): with pytest.raises(ValueError) as err: AESKeyWrap({1: -1}, {}, sender_key=COSEKey.from_symmetric_key(alg="A128KW")) pytest.fail("AESKeyWrap() should fail.") assert "algs in protected and sender_key do not match." in str( err.value)
def test_direct_hkdf_verify_key_with_invalid_material(self): material = COSEKey.from_symmetric_key( key=base64url_decode( "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg"), alg="A256GCM", ) ctx = DirectHKDF({1: -10}, {-20: b"aabbccddeeff"}) context = { "alg": "AES-CCM-16-64-128", "party_u": { "identity": "lighting-client", }, "party_v": { "identity": "lighting-server", }, "supp_pub": { "other": "Encryption Example 02", }, } key = ctx.apply( material, context=context, ) with pytest.raises(VerifyError) as err: ctx.verify_key( b"xxxxxxxxxx", key.key, context=context, ) pytest.fail("verify_key() should fail.") assert "Failed to verify key." in str(err.value)
def test_aes_key_wrap_apply_without_key(self): ctx = AESKeyWrap({1: -3}, {}, sender_key=COSEKey.from_symmetric_key(alg="A128KW")) with pytest.raises(ValueError) as err: ctx.apply() pytest.fail("apply() should fail.") assert "key should be set." in str(err.value)
def test_direct_hkdf_extract_with_invalid_key(self): key = COSEKey.from_symmetric_key(key="a", alg="HS256") ctx = DirectHKDF({1: -10}, {-20: b"aabbccddeeff"}) with pytest.raises(ValueError) as err: ctx.extract(key, alg="A128GCM") pytest.fail("extract() should fail.") assert "context should be set." in str(err.value)
def test_direct_hkdf_verify_key(self): material = COSEKey.from_symmetric_key( key=base64url_decode( "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg"), alg="A256GCM", ) ctx = DirectHKDF({1: -10}, {-20: b"aabbccddeeff"}) context = { "alg": "AES-CCM-16-64-128", "party_u": { "identity": "lighting-client", }, "party_v": { "identity": "lighting-server", }, "supp_pub": { "other": "Encryption Example 02", }, } key = ctx.apply(material, context=context) ctx.verify_key( base64url_decode("hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg"), key.key, context=context, )
def test_direct_hkdf_apply_without_alg(self): material = COSEKey.from_symmetric_key(token_bytes(16)) ctx = DirectHKDF({1: -10}, {4: b"01"}) with pytest.raises(ValueError) as err: ctx.apply(material) pytest.fail("apply() should fail.") assert "context should be set." in str(err.value)
def test_direct_hkdf_extract_with_invalid_context(self): key = COSEKey.from_symmetric_key(alg="A128GCM") ctx = DirectHKDF({1: -10}, {-20: b"aabbccddeeff"}) with pytest.raises(ValueError) as err: ctx.extract(key, alg="A128GCM", context=[None, None, None]) pytest.fail("extract() should fail.") assert "Invalid context information." in str(err.value)
def test_okp_key_derive_key_without_public_key(self): with open(key_path("private_key_x25519.pem")) as key_file: private_key = COSEKey.from_pem(key_file.read(), alg="ECDH-SS+HKDF-256") with pytest.raises(ValueError) as err: private_key.derive_key({"alg": "A128GCM"}) assert "public_key should be set." in str(err.value)
def test_ecdh_direct_hkdf_through_cose_api_with_ecdh_es( self, alg, crv, private_key_path, public_key_path): sender_key = COSEKey.new({1: 2, -1: crv, 3: alg}) rec = Recipient.new(protected={1: alg}, sender_key=sender_key) with open(key_path(public_key_path)) as key_file: pub_key = COSEKey.from_pem(key_file.read(), kid="01") enc_key = rec.apply(recipient_key=pub_key, context={"alg": "A128GCM"}) ctx = COSE.new(alg_auto_inclusion=True) encoded = ctx.encode_and_encrypt(b"Hello world!", enc_key, recipients=[rec]) with open(key_path(private_key_path)) as key_file: priv_key = COSEKey.from_pem(key_file.read(), kid="01", alg=alg) assert b"Hello world!" == ctx.decode(encoded, priv_key, context={"alg": "A128GCM"})
def test_ecdh_aes_key_wrap_apply_without_context(self, sender_key_es): enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305") sender = ECDH_AESKeyWrap({1: -29}, {4: b"01"}, sender_key=sender_key_es) with pytest.raises(ValueError) as err: sender.apply(enc_key, recipient_key=recipient_public_key) pytest.fail("apply() should fail.") assert "context should be set." in str(err.value)
def recipient_public_key(): return COSEKey.from_jwk({ "kty": "EC", "kid": "01", "crv": "P-256", "x": "Ze2loSV3wrroKUN_4zhwGhCqo3Xhu1td4QjeQ5wIVR0", "y": "HlLtdXARY_f55A3fnzQbPcm6hgr34Mp8p-nuzQCE0Zw", })
def test_aes_key_wrap_constructor_a256kw_invalid_key_length(self): with pytest.raises(ValueError) as err: AESKeyWrap( {1: -5}, {}, sender_key=COSEKey.from_symmetric_key(key="xxx", alg="A256KW"), ) pytest.fail("AESKeyWrap() should fail.") assert "Invalid key length: 3." in str(err.value)
def test_aes_key_wrap_constructor_a256kw_with_key(self): ctx = AESKeyWrap( {1: -5}, {}, sender_key=COSEKey.from_symmetric_key(alg="A256KW", key=token_bytes(32)), ) assert isinstance(ctx, AESKeyWrap) assert ctx.alg == -5
def test_ecdh_aes_key_wrap_apply_without_recipient_key( self, sender_key_es): enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305") sender = ECDH_AESKeyWrap({1: -29}, {4: b"01"}, sender_key=sender_key_es) with pytest.raises(ValueError) as err: sender.apply(enc_key, context={"alg": "A128GCM"}) pytest.fail("apply() should fail.") assert "recipient_key should be set in advance." in str(err.value)
def test_aes_key_wrap_constructor_a128kw_with_invalid_alg_in_sender_key( self): with pytest.raises(ValueError) as err: AESKeyWrap( {1: -3}, {}, sender_key=COSEKey.from_symmetric_key(alg="A128GCM"), ) pytest.fail("AESKeyWrap() should fail.") assert "Invalid alg in sender_key: 1." in str(err.value)
def recipient_private_key(): return COSEKey.from_jwk({ "kty": "EC", "alg": "ECDH-ES+A128KW", "kid": "01", "crv": "P-256", "x": "Ze2loSV3wrroKUN_4zhwGhCqo3Xhu1td4QjeQ5wIVR0", "y": "HlLtdXARY_f55A3fnzQbPcm6hgr34Mp8p-nuzQCE0Zw", "d": "r_kHyZ-a06rmxM3yESK84r1otSg-aQcVStkRhA-iCM8", })
def test_direct_hkdf_apply_with_invalid_context(self, invalid, msg): material = COSEKey.from_symmetric_key( key=base64url_decode( "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg"), alg="A256GCM", ) ctx = DirectHKDF({1: -10}, {-20: b"aabbccddeeff"}) with pytest.raises(ValueError) as err: ctx.apply(key=material, context=invalid) pytest.fail("apply() should fail.") assert msg in str(err.value)
def test_direct_constructor(self): k = COSEKey.from_symmetric_key(alg="HS256") ctx = Direct({1: -6}, {}) assert isinstance(ctx, Direct) assert ctx.alg == -6 with pytest.raises(NotImplementedError): ctx.apply(k) pytest.fail("apply() should fail.") with pytest.raises(NotImplementedError): ctx.extract(k) pytest.fail("extract() should fail.")