Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wycheproof tests for AES CMAC #4344

Merged
merged 2 commits into from Jul 17, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/wycheproof/test_cmac.py
@@ -0,0 +1,36 @@
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

import binascii

import pytest

from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends.interfaces import CipherBackend
from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.cmac import CMAC


@pytest.mark.requires_backend_interface(interface=CipherBackend)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMACBackend

@pytest.mark.wycheproof_tests("aes_cmac_test.json")
def test_keywrap_with_padding(backend, wycheproof):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not right

key = binascii.unhexlify(wycheproof.testcase["key"])
msg = binascii.unhexlify(wycheproof.testcase["msg"])
tag = binascii.unhexlify(wycheproof.testcase["tag"])

# skip truncated tags, which we don't support in the API
if wycheproof.valid and not len(tag) != 16:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simplies to wycheproof.valid and len(tag) == 16

ctx = CMAC(AES(key), backend)
ctx.update(msg)
ctx.verify(tag)
elif len(key) not in [16, 24, 32]:
with pytest.raises(ValueError):
CMAC(AES(key), backend)
else:
ctx = CMAC(AES(key), backend)
ctx.update(msg)
with pytest.raises(InvalidSignature):
ctx.verify(tag)