Exemplo n.º 1
0
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']
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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']
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
def test_encrypt_direct_encryption_encoding(test_encrypt_direct_encryption_files):
    test_input = test_encrypt_direct_encryption_files['input']
    test_output = test_encrypt_direct_encryption_files['output']

    recipients = _setup_direct_encryption_recipients(test_input['recipients'])

    msg = EncMessage(test_input['protected'], test_input['unprotected'], test_input['plaintext'], recipients=recipients)

    key = CoseKey.from_dict(test_encrypt_direct_encryption_files["cek"])
    key.key_ops = [EncryptOp]

    msg.key = key

    assert msg.phdr_encoded == test_output['protected']
    assert msg.uhdr_encoded == test_output['unprotected']

    assert msg._enc_structure == test_output['structure']

    assert cbor2.loads(msg.encode()) == test_output['result']
Exemplo n.º 7
0
def test_mac0_encoding(test_mac0):
    test_input = test_mac0['input']
    test_output = test_mac0['output']

    msg = Mac0Message(phdr=test_input['protected'],
                      uhdr=test_input['unprotected'],
                      payload=test_input['plaintext'],
                      external_aad=test_input['external_aad'])

    assert msg.phdr_encoded == test_output['protected']
    assert msg.uhdr_encoded == test_output['unprotected']

    assert msg._mac_structure == test_output['structure']

    key = CoseKey.from_dict(test_mac0["cek"])
    key.key_ops = [MacCreateOp, MacVerifyOp]
    msg.key = key

    assert msg.compute_tag() == test_output['tag']
    assert cbor2.loads(
        msg.encode(tag=test_mac0['cbor_tag'])) == test_output['result']
Exemplo n.º 8
0
def test_encrypt0_encoding(test_encrypt0):
    test_input = test_encrypt0['input']
    test_output = test_encrypt0['output']

    msg = Enc0Message(
        phdr=test_input['protected'],
        uhdr=test_input['unprotected'],
        payload=test_input['plaintext'],
        external_aad=test_input['external_aad'])

    assert msg.phdr_encoded == test_output['protected']
    assert msg.uhdr_encoded == test_output['unprotected']

    assert msg._enc_structure == test_output['structure']

    key = CoseKey.from_dict(test_encrypt0["cek"])
    key.key_ops = [EncryptOp, DecryptOp]
    msg.key = key

    assert msg.encrypt() == test_output['ciphertext']
    assert cbor2.loads(msg.encode()) == test_output['result']
Exemplo n.º 9
0
def test_sign1_encoding(test_sign1):
    test_input = test_sign1['input']
    test_output = test_sign1['output']

    msg = Sign1Message(phdr=test_input['protected'],
                       uhdr=test_input['unprotected'],
                       payload=test_input['plaintext'],
                       external_aad=test_input['external_aad'])

    assert msg.phdr_encoded == test_output['protected']
    assert msg.uhdr_encoded == test_output['unprotected']

    assert msg._sig_structure == test_output['structure']

    key = CoseKey.from_dict(test_sign1["cek"])
    key.key_ops = [SignOp, VerifyOp]
    msg.key = key

    assert msg.compute_signature() == test_output['signature']
    assert cbor2.loads(
        msg.encode(tag=test_sign1['cbor_tag'])) == test_output['result']
Exemplo n.º 10
0
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']