1 /* test_ctr_mode.c - TinyCrypt implementation of some AES-CTR tests */
2 
3 /*
4  *  Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
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  */
test_1_and_2(void)53 unsigned int test_1_and_2(void)
54 {
55         const uint8_t key[16] = {
56 		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88,
57 		0x09, 0xcf, 0x4f, 0x3c
58         };
59         uint8_t ctr[16] = {
60 		0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
61 		0xfc, 0xfd, 0xfe, 0xff
62         };
63         const uint8_t plaintext[64] = {
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
70         };
71         const uint8_t ciphertext[80] = {
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
79         };
80         struct tc_aes_key_sched_struct sched;
81         uint8_t out[80];
82         uint8_t decrypted[64];
83         unsigned int result = TC_PASS;
84         uint32_t off = 0;
85 
86         TC_PRINT("CTR test #1 (encryption SP 800-38a tests):\n");
87         (void)tc_aes128_set_encrypt_key(&sched, key);
88 
89         (void)memcpy(out, ctr, sizeof(ctr));
90         if (tc_ctr_mode(&out[TC_AES_BLOCK_SIZE], sizeof(plaintext), plaintext,
91 			sizeof(plaintext), ctr, &off, &sched) == 0) {
92                 TC_ERROR("CTR test #1 (encryption SP 800-38a tests) failed in %s.\n", __func__);
93                 result = TC_FAIL;
94                 goto exitTest1;
95         }
96 
97         if (off != 0) {
98             TC_ERROR("CTR test #1 invalid block offset (%u).\n", off);
99             result = TC_FAIL;
100             goto exitTest1;
101         }
102         result = check_result(1, ciphertext, sizeof(out), out, sizeof(out));
103         TC_END_RESULT(result);
104 
105         TC_PRINT("CTR test #2 (decryption SP 800-38a tests):\n");
106         (void) memcpy(ctr, out, sizeof(ctr));
107         off = 0;
108         if (tc_ctr_mode(decrypted, sizeof(decrypted), &out[TC_AES_BLOCK_SIZE],
109                         sizeof(decrypted), ctr, &off, &sched) == 0) {
110                 TC_ERROR("CTR test #2 (decryption SP 800-38a tests) failed in %s.\n", __func__);
111                 result = TC_FAIL;
112                 goto exitTest1;
113         }
114 
115         if (off != 0) {
116             TC_ERROR("CTR test #2 invalid block offset (%u).\n", off);
117             result = TC_FAIL;
118             goto exitTest1;
119         }
120         result = check_result(2, plaintext, sizeof(plaintext),
121 			      decrypted, sizeof(plaintext));
122 
123  exitTest1:
124         TC_END_RESULT(result);
125         return result;
126 }
127 
128 /*
129  * Main task to test AES
130  */
131 
main(void)132 int main(void)
133 {
134         unsigned int result = TC_PASS;
135 
136         TC_START("Performing AES128-CTR mode tests:");
137 
138         TC_PRINT("Performing CTR tests:\n");
139         result = test_1_and_2();
140         if (result == TC_FAIL) { /* terminate test */
141                 TC_ERROR("CTR test #1 failed.\n");
142                 goto exitTest;
143         }
144 
145         TC_PRINT("All CTR tests succeeded!\n");
146 
147  exitTest:
148         TC_END_RESULT(result);
149         TC_END_REPORT(result);
150 }
151