# -*- coding: utf-8 -*-
#
# SelfTest/Cipher/AES.py: Self-test for the AES cipher
#
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
#
# ===================================================================
# The contents of this file are dedicated to the public domain. To
# the extent that dedication to the public domain is not available,
# everyone is granted a worldwide, perpetual, royalty-free,
# non-exclusive license to exercise all rights associated with the
# contents of this file for any purpose whatsoever.
# No rights are reserved.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ===================================================================
"""Self-test suite for Crypto.Cipher.AES"""
from __future__ import print_function
import unittest
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
from Crypto.Util.py3compat import *
from binascii import hexlify
# This is a list of (plaintext, ciphertext, key[, description[, params]]) tuples.
test_data = [
# FIPS PUB 197 test vectors
# http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
('00112233445566778899aabbccddeeff', '69c4e0d86a7b0430d8cdb78070b4c55a',
'000102030405060708090a0b0c0d0e0f', 'FIPS 197 C.1 (AES-128)'),
('00112233445566778899aabbccddeeff', 'dda97ca4864cdfe06eaf70a0ec0d7191',
'000102030405060708090a0b0c0d0e0f1011121314151617',
'FIPS 197 C.2 (AES-192)'),
('00112233445566778899aabbccddeeff', '8ea2b7ca516745bfeafc49904b496089',
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
'FIPS 197 C.3 (AES-256)'),
# Rijndael128 test vectors
# Downloaded 2008-09-13 from
# http://www.iaik.tugraz.at/Research/krypto/AES/old/~rijmen/rijndael/testvalues.tar.gz
# ecb_tbl.txt, KEYSIZE=128
('506812a45f08c889b97f5980038b8359', 'd8f532538289ef7d06b506a4fd5be9c9',
'00010203050607080a0b0c0d0f101112',
'ecb-tbl-128: I=1'),
('5c6d71ca30de8b8b00549984d2ec7d4b', '59ab30f4d4ee6e4ff9907ef65b1fb68c',
'14151617191a1b1c1e1f202123242526',
'ecb-tbl-128: I=2'),
('53f3f4c64f8616e4e7c56199f48f21f6', 'bf1ed2fcb2af3fd41443b56d85025cb1',
'28292a2b2d2e2f30323334353738393a',
'ecb-tbl-128: I=3'),
('a1eb65a3487165fb0f1c27ff9959f703', '7316632d5c32233edcb0780560eae8b2',
'3c3d3e3f41424344464748494b4c4d4e',
'ecb-tbl-128: I=4'),
('3553ecf0b1739558b08e350a98a39bfa', '408c073e3e2538072b72625e68b8364b',
'50515253555657585a5b5c5d5f606162',
'ecb-tbl-128: I=5'),
('67429969490b9711ae2b01dc497afde8', 'e1f94dfa776597beaca262f2f6366fea',
'64656667696a6b6c6e6f707173747576',
'ecb-tbl-128: I=6'),
('93385c1f2aec8bed192f5a8e161dd508', 'f29e986c6a1c27d7b29ffd7ee92b75f1',
'78797a7b7d7e7f80828384858788898a',
'ecb-tbl-128: I=7'),
('b5bf946be19beb8db3983b5f4c6e8ddb', '131c886a57f8c2e713aba6955e2b55b5',
'8c8d8e8f91929394969798999b9c9d9e',
'ecb-tbl-128: I=8'),
('41321ee10e21bd907227c4450ff42324', 'd2ab7662df9b8c740210e5eeb61c199d',
'a0a1a2a3a5a6a7a8aaabacadafb0b1b2',
'ecb-tbl-128: I=9'),
('00a82f59c91c8486d12c0a80124f6089', '14c10554b2859c484cab5869bbe7c470',
'b4b5b6b7b9babbbcbebfc0c1c3c4c5c6',
'ecb-tbl-128: I=10'),
('7ce0fd076754691b4bbd9faf8a1372fe', 'db4d498f0a49cf55445d502c1f9ab3b5',
'c8c9cacbcdcecfd0d2d3d4d5d7d8d9da',
'ecb-tbl-128: I=11'),
('23605a8243d07764541bc5ad355b3129', '6d96fef7d66590a77a77bb2056667f7f',
'dcdddedfe1e2e3e4e6e7e8e9ebecedee',
'ecb-tbl-128: I=12'),
('12a8cfa23ea764fd876232b4e842bc44', '316fb68edba736c53e78477bf913725c',
'f0f1f2f3f5f6f7f8fafbfcfdfe010002',
'ecb-tbl-128: I=13'),
('bcaf32415e8308b3723e5fdd853ccc80', '6936f2b93af8397fd3a771fc011c8c37',
'04050607090a0b0c0e0f101113141516',
'ecb-tbl-128: I=14'),
('89afae685d801ad747ace91fc49adde0', 'f3f92f7a9c59179c1fcc2c2ba0b082cd',
'2c2d2e2f31323334363738393b3c3d3e',
'ecb-tbl-128: I=15'),
('f521d07b484357c4a69e76124a634216', '6a95ea659ee3889158e7a9152ff04ebc',
'40414243454647484a4b4c4d4f505152',
'ecb-tbl-128: I=16'),
('3e23b3bc065bcc152407e23896d77783', '1959338344e945670678a5d432c90b93',
'54555657595a5b5c5e5f606163646566',
'ecb-tbl-128: I=17'),
('79f0fba002be1744670e7e99290d8f52', 'e49bddd2369b83ee66e6c75a1161b394',
'68696a6b6d6e6f70727374757778797a',
'ecb-tbl-128: I=18'),
('da23fe9d5bd63e1d72e3dafbe21a6c2a', 'd3388f19057ff704b70784164a74867d',
'7c7d7e7f81828384868788898b8c8d8e',
'ecb-tbl-128: I=19'),
('e3f5698ba90b6a022efd7db2c7e6c823', '23aa03e2d5e4cd24f3217e596480d1e1',
'a4a5a6a7a9aaabacaeafb0b1b3b4b5b6',
'ecb-tbl-128: I=20'),
('bdc2691d4f1b73d2700679c3bcbf9c6e', 'c84113d68b666ab2a50a8bdb222e91b9',
'e0e1e2e3e5e6e7e8eaebecedeff0f1f2',
'ecb-tbl-128: I=21'),
('ba74e02093217ee1ba1b42bd5624349a', 'ac02403981cd4340b507963db65cb7b6',
'08090a0b0d0e0f10121314151718191a',
'ecb-tbl-128: I=22'),
('b5c593b5851c57fbf8b3f57715e8f680', '8d1299236223359474011f6bf5088414',
'6c6d6e6f71727374767778797b7c7d7e',
'ecb-tbl-128: I=23'),
('3da9bd9cec072381788f9387c3bbf4ee', '5a1d6ab8605505f7977e55b9a54d9b90',
'80818283858687888a8b8c8d8f909192',
'ecb-tbl-128: I=24'),
('4197f3051121702ab65d316b3c637374', '72e9c2d519cf555e4208805aabe3b258',
'94959697999a9b9c9e9fa0a1a3a4a5a6',
'ecb-tbl-128: I=25'),
('9f46c62ec4f6ee3f6e8c62554bc48ab7', 'a8f3e81c4a23a39ef4d745dffe026e80',
'a8a9aaabadaeafb0b2b3b4b5b7b8b9ba',
'ecb-tbl-128: I=26'),
('0220673fe9e699a4ebc8e0dbeb6979c8', '546f646449d31458f9eb4ef5483aee6c',
'bcbdbebfc1c2c3c4c6c7c8c9cbcccdce',
'ecb-tbl-128: I=27'),
('b2b99171337ded9bc8c2c23ff6f18867', '4dbe4bc84ac797c0ee4efb7f1a07401c',
'd0d1d2d3d5d6d7d8dadbdcdddfe0e1e2',
'ecb-tbl-128: I=28'),
('a7facf4e301e984e5efeefd645b23505', '25e10bfb411bbd4d625ac8795c8ca3b3',
'e4e5e6e7e9eaebeceeeff0f1f3f4f5f6',
'ecb-tbl-128: I=29'),
('f7c762e4a9819160fd7acfb6c4eedcdd', '315637405054ec803614e43def177579',
'f8f9fafbfdfefe00020304050708090a',
'ecb-tbl-128: I=30'),
('9b64fc21ea08709f4915436faa70f1be', '60c5bc8a1410247295c6386c59e572a8',
'0c0d0e0f11121314161718191b1c1d1e',
'ecb-tbl-128: I=31'),
('52af2c3de07ee6777f55a4abfc100b3f', '01366fc8ca52dfe055d6a00a76471ba6',
'20212223252627282a2b2c2d2f303132',
'ecb-tbl-128: I=32'),
('2fca001224386c57aa3f968cbe2c816f', 'ecc46595516ec612449c3f581e7d42ff',
'34353637393a3b3c3e3f404143444546',
'ecb-tbl-128: I=33'),
('4149c73658a4a9c564342755ee2c132f', '6b7ffe4c602a154b06ee9c7dab5331c9',
'48494a4b4d4e4f50525354555758595a',
'ecb-tbl-128: I=34'),
('af60005a00a1772f7c07a48a923c23d2', '7da234c14039a240dd02dd0fbf84eb67',
'5c5d5e5f61626364666768696b6c6d6e',
'ecb-tbl-128: I=35'),
('6fccbc28363759914b6f0280afaf20c6', 'c7dc217d9e3604ffe7e91f080ecd5a3a',
'70717273757677787a7b7c7d7f808182',
'ecb-tbl-128: I=36'),
('7d82a43ddf4fefa2fc5947499884d386', '37785901863f5c81260ea41e7580cda5',
'84858687898a8b8c8e8f909193949596',
'ecb-tbl-128: I=37'),
('5d5a990eaab9093afe4ce254dfa49ef9', 'a07b9338e92ed105e6ad720fccce9fe4',
'98999a9b9d9e9fa0a2a3a4a5a7a8a9aa',
'ecb-tbl-128: I=38'),
('4cd1e2fd3f4434b553aae453f0ed1a02', 'ae0fb9722418cc21a7da816bbc61322c',
'acadaeafb1b2b3b4b6b7b8b9bbbcbdbe',
'ecb-tbl-128: I=39'),
('5a2c9a9641d4299125fa1b9363104b5e', 'c826a193080ff91ffb21f71d3373c877',
'c0c1c2c3c5c6c7c8cacbcccdcfd0d1d2',
'ecb-tbl-128: I=40'),
('b517fe34c0fa217d341740bfd4fe8dd4', '1181b11b0e494e8d8b0aa6b1d5ac2c48',
'd4d5d6d7d9dadbdcdedfe0e1e3e4e5e6',
'ecb-tbl-128: I=41'),
('014baf2278a69d331d5180103643e99a', '6743c3d1519ab4f2cd9a78ab09a511bd',
'e8e9eaebedeeeff0f2f3f4f5f7f8f9fa',
'ecb-tbl-128: I=42'),
('b529bd8164f20d0aa443d4932116841c', 'dc55c076d52bacdf2eefd952946a439d',
'fcfdfeff01020304060708090b0c0d0e',
'ecb-tbl-128: I=43'),
('2e596dcbb2f33d4216a1176d5bd1e456', '711b17b590ffc72b5c8e342b601e8003',
'10111213151617181a1b1c1d1f202122',
'ecb-tbl-128: I=44'),
('7274a1ea2b7ee2424e9a0e4673689143', '19983bb0950783a537e1339f4aa21c75',
'24252627292a2b2c2e2f303133343536',
'ecb-tbl-128: I=45'),
('ae20020bd4f13e9d90140bee3b5d26af', '3ba7762e15554169c0f4fa39164c410c',
'38393a3b3d3e3f40424344454748494a',
'ecb-tbl-128: I=46'),
('baac065da7ac26e855e79c8849d75a02', 'a0564c41245afca7af8aa2e0e588ea89',
'4c4d4e4f51525354565758595b5c5d5e',
'ecb-tbl-128: I=47'),
('7c917d8d1d45fab9e2540e28832540cc', '5e36a42a2e099f54ae85ecd92e2381ed',
'60616263656667686a6b6c6d6f707172',
'ecb-tbl-128: I=48'),
('bde6f89e16daadb0e847a2a614566a91', '770036f878cd0f6ca2268172f106f2fe',
'74757677797a7b7c7e7f808183848586',
'ecb-tbl-128: I=49'),
('c9de163725f1f5be44ebb1db51d07fbc', '7e4e03908b716116443ccf7c94e7c259',
'88898a8b8d8e8f90929394959798999a',
'ecb-tbl-128: I=50'),
('3af57a58f0c07dffa669572b521e2b92', '482735a48c30613a242dd494c7f9185d',
'9c9d9e9fa1a2a3a4a6a7a8a9abacadae',
'ecb-tbl-128: I=51'),
('3d5ebac306dde4604f1b4fbbbfcdae55', 'b4c0f6c9d4d7079addf9369fc081061d',
'b0b1b2b3b5b6b7b8babbbcbdbfc0c1c2',
'ecb-tbl-128: I=52'),
('c2dfa91bceb76a1183c995020ac0b556', 'd5810fe0509ac53edcd74f89962e6270',
'c4c5c6c7c9cacbcccecfd0d1d3d4d5d6',
'ecb-tbl-128: I=53'),
('c70f54305885e9a0746d01ec56c8596b', '03f17a16b3f91848269ecdd38ebb2165',
'd8d9dadbdddedfe0e2e3e4e5e7e8e9ea',
'ecb-tbl-128: I=54'),
('c4f81b610e98012ce000182050c0c2b2', 'da1248c3180348bad4a93b4d9856c9df',
'ecedeeeff1f2f3f4f6f7f8f9fbfcfdfe',
'ecb-tbl-128: I=55'),
('eaab86b1d02a95d7404eff67489f97d4', '3d10d7b63f3452c06cdf6cce18be0c2c',
'00010203050607080a0b0c0d0f101112',
'ecb-tbl-128: I=56'),
('7c55bdb40b88870b52bec3738de82886', '4ab823e7477dfddc0e6789018fcb6258',
'14151617191a1b1c1e1f202123242526',
'ecb-tbl-128: I=57'),
('ba6eaa88371ff0a3bd875e3f2a975ce0', 'e6478ba56a77e70cfdaa5c843abde30e',
'28292a2b2d2e2f30323334353738393a',
'ecb-tbl-128: I=58'),
('08059130c4c24bd30cf0575e4e0373dc', '1673064895fbeaf7f09c5429ff75772d',
'3c3d3e3f41424344464748494b4c4d4e',
'ecb-tbl-128: I=59'),
('9a8eab004ef53093dfcf96f57e7eda82', '4488033ae9f2efd0ca9383bfca1a94e9',
'50515253555657585a5b5c5d5f606162',
'ecb-tbl-128: I=60'),
('0745b589e2400c25f117b1d796c28129', '978f3b8c8f9d6f46626cac3c0bcb9217',
'64656667696a6b6c6e6f707173747576',
'ecb-tbl-128: I=61'),
('2f1777781216cec3f044f134b1b92bbe', 'e08c8a7e582e15e5527f1d9e2eecb236',
'78797a7b7d7e7f80828384858788898a',
'ecb-tbl-128: I=62'),
('353a779ffc541b3a3805d90ce17580fc', 'cec155b76ac5ffda4cf4f9ca91e49a7a',
'8c8d8e8f91929394969798999b9c9d9e',
'ecb-tbl-128: I=63'),
('1a1eae4415cefcf08c4ac1c8f68bea8f', 'd5ac7165763225dd2a38cdc6862c29ad',
'a0a1a2a3a5a6a7a8aaabacadafb0b1b2',
'ecb-tbl-128: I=64'),
('e6e7e4e5b0b3b2b5d4d5aaab16111013', '03680fe19f7ce7275452020be70e8204',
'b4b5b6b7b9babbbcbebfc0c1c3c4c5c6',
'ecb-tbl-128: I=65'),
('f8f9fafbfbf8f9e677767170efe0e1e2', '461df740c9781c388e94bb861ceb54f6',
'c8c9cacbcdcecfd0d2d3d4d5d7d8d9da',
'ecb-tbl-128: I=66'),
('63626160a1a2a3a445444b4a75727370', '451bd60367f96483042742219786a074',
'dcdddedfe1e2e3e4e6e7e8e9ebecedee',
'ecb-tbl-128: I=67'),
('717073720605040b2d2c2b2a05fafbf9', 'e4dfa42671a02e57ef173b85c0ea9f2b',
'f0f1f2f3f5f6f7f8fafbfcfdfe010002',
'ecb-tbl-128: I=68'),
('78797a7beae9e8ef3736292891969794', 'ed11b89e76274282227d854700a78b9e',
'04050607090a0b0c0e0f101113141516',
'ecb-tbl-128: I=69'),
('838281803231300fdddcdbdaa0afaead', '433946eaa51ea47af33895f2b90b3b75',
'18191a1b1d1e1f20222324252728292a',
'ecb-tbl-128: I=70'),
('18191a1bbfbcbdba75747b7a7f78797a', '6bc6d616a5d7d0284a5910ab35022528',
'2c2d2e2f31323334363738393b3c3d3e',
'ecb-tbl-128: I=71'),
('848586879b989996a3a2a5a4849b9a99', 'd2a920ecfe919d354b5f49eae9719c98',
'40414243454647484a4b4c4d4f505152',
'ecb-tbl-128: I=72'),
('0001020322212027cacbf4f551565754', '3a061b17f6a92885efbd0676985b373d',
'54555657595a5b5c5e5f606163646566',
'ecb-tbl-128: I=73'),
('cecfcccdafacadb2515057564a454447', 'fadeec16e33ea2f4688499d157e20d8f',
'68696a6b6d6e6f70727374757778797a',
'ecb-tbl-128: I=74'),
('92939091cdcecfc813121d1c80878685', '5cdefede59601aa3c3cda36fa6b1fa13',
'7c7d7e7f81828384868788898b8c8d8e',
'ecb-tbl-128: I=75'),
('d2d3d0d16f6c6d6259585f5ed1eeefec', '9574b00039844d92ebba7ee8719265f8',
'90919293959697989a9b9c9d9fa0a1a2',
'ecb-tbl-128: I=76'),
('acadaeaf878485820f0e1110d5d2d3d0', '9a9cf33758671787e5006928188643fa',
'a4a5a6a7a9aaabacaeafb0b1b3b4b5b6',
'ecb-tbl-128: I=77'),
('9091929364676619e6e7e0e1757a7b78', '2cddd634c846ba66bb46cbfea4a674f9',
'b8b9babbbdbebfc0c2c3c4c5c7c8c9ca',
'ecb-tbl-128: I=78'),
('babbb8b98a89888f74757a7b92959497', 'd28bae029393c3e7e26e9fafbbb4b98f',
'cccdcecfd1d2d3d4d6d7d8d9dbdcddde',
'ecb-tbl-128: I=79'),
('8d8c8f8e6e6d6c633b3a3d3ccad5d4d7', 'ec27529b1bee0a9ab6a0d73ebc82e9b7',
'e0e1e2e3e5e6e7e8eaebecedeff0f1f2',
'ecb-tbl-128: I=80'),
('86878485010203040808f7f767606162', '3cb25c09472aff6ee7e2b47ccd7ccb17',
'f4f5f6f7f9fafbfcfefe010103040506',
'ecb-tbl-128: I=81'),
('8e8f8c8d656667788a8b8c8d010e0f0c', 'dee33103a7283370d725e44ca38f8fe5',
'08090a0b0d0e0f10121314151718191a',
'ecb-tbl-128: I=82'),
('c8c9cacb858687807a7b7475e7e0e1e2', '27f9bcd1aac64bffc11e7815702c1a69',
'1c1d1e1f21222324262728292b2c2d2e',
'ecb-tbl-128: I=83'),
('6d6c6f6e5053525d8c8d8a8badd2d3d0', '5df534ffad4ed0749a9988e9849d0021',
'30313233353637383a3b3c3d3f404142',
'ecb-tbl-128: I=84'),
('28292a2b393a3b3c0607181903040506', 'a48bee75db04fb60ca2b80f752a8421b',
'44454647494a4b4c4e4f505153545556',
'ecb-tbl-128: I=85'),
('a5a4a7a6b0b3b28ddbdadddcbdb2b3b0', '024c8cf70bc86ee5ce03678cb7af45f9',
'58595a5b5d5e5f60626364656768696a',
'ecb-tbl-128: I=86'),
('323330316467666130313e3f2c2b2a29', '3c19ac0f8a3a3862ce577831301e166b',
'6c6d6e6f71727374767778797b7c7d7e',
'ecb-tbl-128: I=87'),
('27262524080b0a05171611100b141516', 'c5e355b796a57421d59ca6be82e73bca',
'80818283858687888a8b8c8d8f909192',
'ecb-tbl-128: I=88'),
('040506074142434435340b0aa3a4a5a6', 'd94033276417abfb05a69d15b6e386e2',
'94959697999a9b9c9e9fa0a1a3a4a5a6',
'ecb-tbl-128: I=89'),
('242526271112130c61606766bdb2b3b0', '24b36559ea3a9b9b958fe6da3e5b8d85',
'a8a9aaabadaeafb0b2b3b4b5b7b8b9ba',
'ecb-tbl-128: I=90'),
('4b4a4948252627209e9f9091cec9c8cb', '20fd4feaa0e8bf0cce7861d74ef4cb72',
'bcbdbebfc1c2c3c4c6c7c8c9cbcccdce',
'ecb-tbl-128: I=91'),
('68696a6b6665646b9f9e9998d9e6e7e4', '350e20d5174277b9ec314c501570a11d',
'd0d1d2d3d5d6d7d8dadbdcdddfe0e1e2',
'ecb-tbl-128: I=92'),
('34353637c5c6c7c0f0f1eeef7c7b7a79', '87a29d61b7c604d238fe73045a7efd57',
'e4e5e6e7e9eaebeceeeff0f1f3f4f5f6',
'ecb-tbl-128: I=93'),
('32333031c2c1c13f0d0c0b0a050a0b08', '2c3164c1cc7d0064816bdc0faa362c52',
'f8f9fafbfdfefe00020304050708090a',
'ecb-tbl-128: I=94'),
('cdcccfcebebdbcbbabaaa5a4181f1e1d', '195fe5e8a05a2ed594f6e4400eee10b3',
'0c0d0e0f11121314161718191b1c1d1e',
'ecb-tbl-128: I=95'),
('212023223635343ba0a1a6a7445b5a59', 'e4663df19b9a21a5a284c2bd7f905025',
'20212223252627282a2b2c2d2f303132',
'ecb-tbl-128: I=96'),
('0e0f0c0da8abaaad2f2e515002050407', '21b88714cfb4e2a933bd281a2c4743fd',
'34353637393a3b3c3e3f404143444546',
Loading ...