Files
OpenCellular/test/tpm_test/genvectors.py
nagendra modadugu 9c69337050 CR50: add tests for AES CBC, CFB and OFB
Add tests for CBC, CFB and OFB AES modes.

Also convert tests to use word unligned
input parameters, to ensure that the api's
are unalignment agnostic.

Also add the program used for generating
test vectors.

BRANCH=none
BUG=chrome-os-partner:56413
TEST=tpmtest.py passes

Change-Id: I92c9ffece797aa7134d9cdad6ea32e6fe50feef1
Signed-off-by: nagendra modadugu <ngm@google.com>
Reviewed-on: https://chromium-review.googlesource.com/374663
Commit-Ready: Nagendra Modadugu <ngm@google.com>
Tested-by: Nagendra Modadugu <ngm@google.com>
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
2016-08-25 01:46:12 -07:00

78 lines
2.1 KiB
Python

#!/usr/bin/python
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Module for generating AES test vectors."""
from binascii import b2a_hex as b2a
from Crypto.Cipher import AES
from itertools import izip_longest
import os
modes = {
AES.MODE_CBC: 'CBC',
AES.MODE_CFB: 'CFB',
AES.MODE_OFB: 'OFB',
}
template = \
'''
<crypto_test name="AES:{mode}{key_bits} {test_num}">
<clear_text format="hex">
{pt}
</clear_text>
<key>
{key}
</key>
<cipher_text>
{ct}
</cipher_text>
<iv>
{iv}
</iv>
</crypto_test>
'''
def h2be(v):
# Convert input big-endian byte-string to 4-byte segmented
# little-endian words. Pad-bytes (if necessary) are the empty string.
word = [iter(v)] * 4
return ''.join([
''.join(b[::-1]) for b in izip_longest(*word, fillvalue='')
])
for mode in [AES.MODE_CBC, AES.MODE_CFB, AES.MODE_OFB]:
for key_bytes in [16, 24, 32]:
test_num = 0
for pt_len in [5, 16, 21, 32]:
# CBC mode requires block sized inputs.
if mode == AES.MODE_CBC and pt_len % 16:
continue
test_num += 1
actual_pt_len = pt_len
if pt_len % 16:
pt_len = 16 * ((pt_len / 16) + 1)
key = os.urandom(key_bytes)
iv = os.urandom(16)
pt = os.urandom(pt_len)
obj = AES.new(key, mode=mode, IV=iv, segment_size=128)
ct = obj.encrypt(pt)
obj = AES.new(key, mode=mode, IV=iv, segment_size=128)
assert obj.decrypt(ct)[:pt_len] == pt
print template.format(mode=modes[mode],
key_bits=str(key_bytes * 8),
test_num=str(test_num),
pt=b2a(h2be(pt[:actual_pt_len])),
key=b2a(h2be(key)),
ct=b2a(h2be(ct[:actual_pt_len])),
iv=b2a(h2be(iv))),