def test_encrypt_key_agreement_key_wrap_decoding( test_encrypt_key_agreement_key_wrap_files): test_input = test_encrypt_key_agreement_key_wrap_files['input'] test_output = test_encrypt_key_agreement_key_wrap_files['output'] msg = CoseMessage.decode(cbor2.dumps((test_output['result']))) assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] assert msg._enc_structure == test_output['structure'] # test intermediate results for i, (r, r_output) in enumerate( zip(msg.recipients, test_output['recipients'])): # this should actually be static_receiver_key but the test vector is wrong and messed it up r.key = test_input['recipients'][i]['static_receiver_key'] assert r.phdr_encoded == r_output['protected'] assert r.uhdr_encoded == r_output['unprotected'] assert r.payload == r_output['ciphertext'] assert r.get_kdf_context( (r.get_attr(headers.Algorithm) ).get_key_wrap_func()).encode() == r_output['context'] assert r.decrypt((r.get_attr(headers.Algorithm)).get_key_wrap_func()) == \ test_encrypt_key_agreement_key_wrap_files['random_key'].k for r in msg.recipients: assert msg.decrypt(r) == test_input['plaintext']
def test_fail_on_illegal_keyops_verifying(ops): msg = Sign1Message(phdr={'ALG': 'ES256'}, payload="signed message".encode('utf-8')) ec2_key = EC2Key.generate_key(crv='P_256') msg.key = ec2_key msg = msg.encode() msg = CoseMessage.decode(msg) # set an illegal key op if ops in { 'ENCRYPT', 'DECRYPT', 'WRAP', 'UNWRAP', 'MAC_CREATE', 'MAC_VERIFY' }: with pytest.raises(CoseIllegalKeyOps) as excinfo: ec2_key.key_ops = [ops] assert "Invalid COSE key operation" in str(excinfo.value) return else: ec2_key.key_ops = [ops] msg.key = ec2_key with pytest.raises(CoseIllegalKeyOps) as excinfo: msg.verify_signature() assert "Illegal key operations specified." in str(excinfo.value)
def test_sign1_decoding(test_sign): test_input = test_sign['input'] test_output = test_sign['output'] msg = CoseMessage.decode(cbor2.dumps(test_output['result'])) for s, s_input, s_output in zip(msg.signers, test_input['signers'], test_output['signers']): s.external_aad = unhexlify(s_input['external_aad']) s.key = s_input['signing_key'] assert s._sig_structure == s_output['structure'] assert s.verify_signature()
def test_encrypt0_decoding(test_encrypt0): test_input = test_encrypt0['input'] test_output = test_encrypt0['output'] msg = CoseMessage.decode(cbor2.dumps(test_output['result'])) assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] key = CoseKey.from_dict(test_encrypt0["cek"]) key.key_ops = [DecryptOp] msg.key = key assert msg.decrypt() == test_input['plaintext']
def test_encrypt0_decoding(test_mac0): test_input = test_mac0['input'] test_output = test_mac0['output'] msg = CoseMessage.decode(cbor2.dumps(test_output['result'])) msg.external_aad = test_input['external_aad'] key = CoseKey.from_dict(test_mac0["cek"]) key.key_ops = [MacVerifyOp] msg.key = key assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] assert msg.verify_tag()
def test_sign1_decoding(test_sign1): test_input = test_sign1['input'] test_output = test_sign1['output'] msg = CoseMessage.decode(cbor2.dumps(test_output['result'])) msg.external_aad = test_input['external_aad'] key = CoseKey.from_dict(test_sign1["cek"]) key.key_ops = [VerifyOp] msg.key = key assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] assert msg.verify_signature()
def test_encrypt_direct_encryption_decoding(test_encrypt_direct_encryption_files): test_output = test_encrypt_direct_encryption_files['output'] test_input = test_encrypt_direct_encryption_files['input'] msg = CoseMessage.decode(cbor2.dumps((test_output['result']))) key = CoseKey.from_dict(test_encrypt_direct_encryption_files["cek"]) key.key_ops = [DecryptOp] msg.key = key assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] for r in msg.recipients: assert msg.decrypt(r) == test_input['plaintext']
def test_mac_direct_encryption_decoding(test_mac_direct_encryption_files): test_output = test_mac_direct_encryption_files['output'] test_input = test_mac_direct_encryption_files['input'] msg = CoseMessage.decode(cbor2.dumps((test_output['result']))) msg.external_aad = test_input['external_aad'] key = CoseKey.from_dict(test_mac_direct_encryption_files["cek"]) key.key_ops = [MacVerifyOp] msg.key = key assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] for r in msg.recipients: assert msg.verify_tag(r)
def test_encrypt_direct_key_agreement_decoding(test_encrypt_direct_key_agreement_files): test_output = test_encrypt_direct_key_agreement_files['output'] test_input = test_encrypt_direct_key_agreement_files['input'] msg = CoseMessage.decode(cbor2.dumps((test_output['result']))) assert msg._enc_structure == test_output['structure'] assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] # test intermediate results for k, (r, r_output) in enumerate(zip(msg.recipients, test_output['recipients'])): assert r.phdr_encoded == r_output['protected'] assert r.uhdr_encoded == r_output['unprotected'] assert r.get_kdf_context(msg.get_attr(headers.Algorithm)).encode() == r_output['context'] r.key = test_input['recipients'][k]['static_receiver_key'] for r in msg.recipients: assert msg.decrypt(r) == test_input['plaintext']
def test_encrypt0_round_trip(test_encrypt0): test_input = test_encrypt0['input'] test_output = test_encrypt0['output'] key = CoseKey.from_dict(test_encrypt0["cek"]) key.key_ops = [DecryptOp, EncryptOp] msg = Enc0Message( phdr=test_input['protected'], uhdr=test_input['unprotected'], payload=test_input['plaintext'], external_aad=test_input['external_aad'], key=key) assert msg.encrypt() == test_output['ciphertext'] assert cbor2.loads(msg.encode()) == test_output['result'] msg = msg.encode() msg = CoseMessage.decode(msg) msg.key = key assert msg.decrypt() == test_input['plaintext']
def test_encrypt_key_wrap_decoding(test_encrypt_key_wrap_files): test_input = test_encrypt_key_wrap_files['input'] test_output = test_encrypt_key_wrap_files['output'] msg = CoseMessage.decode(cbor2.dumps((test_output['result']))) assert msg._enc_structure == test_output['structure'] assert msg.phdr == test_input['protected'] assert msg.uhdr == test_input['unprotected'] # test intermediate results for i, (r, r_output) in enumerate(zip(msg.recipients, test_output['recipients'])): assert r.phdr_encoded == r_output['protected'] assert r.uhdr_encoded == r_output['unprotected'] assert r.payload == r_output['ciphertext'] # set the key r.key = test_input['recipients'][i]['secret_key'] assert r.decrypt(msg.get_attr(headers.Algorithm)) == test_encrypt_key_wrap_files['random_key'].k for r in msg.recipients: assert msg.decrypt(r) == test_input['plaintext']