David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 1 | /* test_ctr_mode.c - TinyCrypt implementation of some AES-CTR tests */ |
| 2 | |
| 3 | /* |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 4 | * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of Intel Corporation nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | DESCRIPTION |
| 35 | This module tests the following AES-CTR Mode routines: |
| 36 | |
| 37 | Scenarios tested include: |
| 38 | - AES128 CTR mode encryption SP 800-38a tests |
| 39 | */ |
| 40 | |
| 41 | #include <tinycrypt/ctr_mode.h> |
| 42 | #include <tinycrypt/aes.h> |
| 43 | #include <tinycrypt/constants.h> |
| 44 | #include <test_utils.h> |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <string.h> |
| 49 | |
| 50 | /* |
| 51 | * NIST SP 800-38a CTR Test for encryption and decryption. |
| 52 | */ |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 53 | unsigned int test_1_and_2(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 54 | { |
| 55 | const uint8_t key[16] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 56 | 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, |
| 57 | 0x09, 0xcf, 0x4f, 0x3c |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 58 | }; |
| 59 | uint8_t ctr[16] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 60 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, |
| 61 | 0xfc, 0xfd, 0xfe, 0xff |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 62 | }; |
| 63 | const uint8_t plaintext[64] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 64 | 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, |
| 65 | 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
| 66 | 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46, |
| 67 | 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
| 68 | 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, |
| 69 | 0xe6, 0x6c, 0x37, 0x10 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 70 | }; |
| 71 | const uint8_t ciphertext[80] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 72 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, |
| 73 | 0xfc, 0xfd, 0xfe, 0xff, 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, |
| 74 | 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x98, 0x06, 0xf6, 0x6b, |
| 75 | 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff, |
| 76 | 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02, |
| 77 | 0x0d, 0xb0, 0x3e, 0xab, 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, |
| 78 | 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 79 | }; |
| 80 | struct tc_aes_key_sched_struct sched; |
| 81 | uint8_t out[80]; |
| 82 | uint8_t decrypted[64]; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 83 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 84 | |
| 85 | TC_PRINT("CTR test #1 (encryption SP 800-38a tests):\n"); |
| 86 | (void)tc_aes128_set_encrypt_key(&sched, key); |
| 87 | |
| 88 | (void)memcpy(out, ctr, sizeof(ctr)); |
| 89 | if (tc_ctr_mode(&out[TC_AES_BLOCK_SIZE], sizeof(plaintext), plaintext, |
| 90 | sizeof(plaintext), ctr, &sched) == 0) { |
| 91 | TC_ERROR("CTR test #1 (encryption SP 800-38a tests) failed in %s.\n", __func__); |
| 92 | result = TC_FAIL; |
| 93 | goto exitTest1; |
| 94 | } |
| 95 | |
| 96 | result = check_result(1, ciphertext, sizeof(out), out, sizeof(out)); |
| 97 | TC_END_RESULT(result); |
| 98 | |
| 99 | TC_PRINT("CTR test #2 (decryption SP 800-38a tests):\n"); |
| 100 | (void) memcpy(ctr, out, sizeof(ctr)); |
| 101 | if (tc_ctr_mode(decrypted, sizeof(decrypted), &out[TC_AES_BLOCK_SIZE], |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 102 | sizeof(decrypted), ctr, &sched) == 0) { |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 103 | TC_ERROR("CTR test #2 (decryption SP 800-38a tests) failed in %s.\n", __func__); |
| 104 | result = TC_FAIL; |
| 105 | goto exitTest1; |
| 106 | } |
| 107 | |
| 108 | result = check_result(2, plaintext, sizeof(plaintext), |
| 109 | decrypted, sizeof(plaintext)); |
| 110 | |
| 111 | exitTest1: |
| 112 | TC_END_RESULT(result); |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Main task to test AES |
| 118 | */ |
| 119 | |
| 120 | int main(void) |
| 121 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 122 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 123 | |
| 124 | TC_START("Performing AES128-CTR mode tests:"); |
| 125 | |
| 126 | TC_PRINT("Performing CTR tests:\n"); |
| 127 | result = test_1_and_2(); |
| 128 | if (result == TC_FAIL) { /* terminate test */ |
| 129 | TC_ERROR("CBC test #1 failed.\n"); |
| 130 | goto exitTest; |
| 131 | } |
| 132 | |
| 133 | TC_PRINT("All CTR tests succeeded!\n"); |
| 134 | |
| 135 | exitTest: |
| 136 | TC_END_RESULT(result); |
| 137 | TC_END_REPORT(result); |
| 138 | } |