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_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_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_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_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_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_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_ec2_key_derive_key_with_invalid_private_key(self): 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: -7, # signature algorithm. }) with open(key_path("public_key_es256.pem")) as key_file: public_key = COSEKey.from_pem(key_file.read()) with pytest.raises(ValueError) as err: private_key.derive_key({"alg": "A128GCM"}, public_key=public_key) assert "Invalid alg for key derivation: -7." in str(err.value)
def test_okp_key_derive_key_with_ed25519_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") pub_key = COSEKey.from_jwk({ "kty": "EC", "kid": "01", "crv": "P-256", "x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8", "y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4", # "d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM", }) with pytest.raises(ValueError) as err: private_key.derive_key({"alg": "A128GCM"}, public_key=pub_key) assert "public_key should be x25519/x448 public key." in str(err.value)
def test_ecdh_direct_hkdf_apply_with_supp_pub_other(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") enc_key = rec.apply( recipient_key=pub_key, context=[ 1, [None, None, None], [None, nonce, None], [128, cbor2.dumps({1: -25}), b"other"], ], ) assert enc_key.alg == 1 assert nonce == rec._unprotected[-25]
def test_okp_key_derive_key_with_public_key(self): with open(key_path("public_key_x25519.pem")) as key_file: public_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", }) with pytest.raises(ValueError) as err: public_key.derive_key({"alg": "A128GCM"}, public_key=pub_key) assert "Public key cannot be used for key derivation." in str( err.value)
def test_okp_key_to_cose_key_with_invalid_key(self): with open(key_path("private_key_es256.pem")) as key_file: private_key = COSEKey.from_pem(key_file.read()) with pytest.raises(ValueError) as err: OKPKey.to_cose_key(private_key.key) assert "Unsupported or unknown key for OKP." in str(err.value)