def test_ecdh_direct_hkdf_apply_with_invalid_recipient_key( self, sender_key_es, recipient_private_key): rec = Recipient.new(protected={"alg": "ECDH-ES+HKDF-256"}, sender_key=sender_key_es) with pytest.raises(ValueError) as err: rec.apply(recipient_key=recipient_private_key, context={"alg": "A128GCM"}) pytest.fail("apply() should fail.") assert "public_key should be elliptic curve public key." in str( err.value)
def test_ecdh_aes_key_wrap_apply_with_invalid_key_to_wrap( self, sender_key_es, recipient_public_key): mac_key = COSEKey.from_symmetric_key(key="xxx", alg="HS256") rec = Recipient.new(protected={"alg": "ECDH-ES+A128KW"}, sender_key=sender_key_es) with pytest.raises(EncodeError) as err: rec.apply(mac_key, recipient_key=recipient_public_key, context={"alg": "A128GCM"}) pytest.fail("apply() should fail.") assert "Failed to wrap key." in str(err.value)
def test_ecdh_aes_key_wrap_apply_with_invalid_recipient_key( self, sender_key_es, recipient_private_key): enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305") rec = Recipient.new(protected={"alg": "ECDH-ES+A128KW"}, sender_key=sender_key_es) with pytest.raises(ValueError) as err: rec.apply(enc_key, recipient_key=recipient_private_key, context={"alg": "A128GCM"}) pytest.fail("apply() should fail.") assert "public_key should be elliptic curve public key." 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"})