1 /* test_ctr_mode.c - TinyCrypt implementation of some AES-CTR tests */
2 
3 /*
4  *  Copyright (C) 2015 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  * Scenarios tested include:
37  * - AES128 CTR mode encryption SP 800-38a tests
38  */
39 
40 #include <tinycrypt/ctr_mode.h>
41 #include <tinycrypt/constants.h>
42 #include <zephyr/test_utils.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <zephyr/ztest.h>
48 
49 /*
50  * NIST SP 800-38a CTR Test for encryption and decryption.
51  */
test_ctr_sp_800_38a_encrypt_decrypt(void)52 void test_ctr_sp_800_38a_encrypt_decrypt(void)
53 {
54 
55 	TC_PRINT("Performing CTR tests:\n");
56 
57 	const uint8_t key[16] = {
58 		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7,
59 		0x15, 0x88,
60 		0x09, 0xcf, 0x4f, 0x3c
61 	};
62 	uint8_t ctr[16] = {
63 		0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
64 		0xfa, 0xfb,
65 		0xfc, 0xfd, 0xfe, 0xff
66 	};
67 	const uint8_t plaintext[64] = {
68 		0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d,
69 		0x7e, 0x11,
70 		0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03,
71 		0xac, 0x9c,
72 		0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8,
73 		0x1c, 0x46,
74 		0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a,
75 		0x52, 0xef,
76 		0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b,
77 		0x41, 0x7b,
78 		0xe6, 0x6c, 0x37, 0x10
79 	};
80 	const uint8_t ciphertext[80] = {
81 		0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
82 		0xfa, 0xfb,
83 		0xfc, 0xfd, 0xfe, 0xff, 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20,
84 		0xe3, 0x26,
85 		0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x98, 0x06,
86 		0xf6, 0x6b,
87 		0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff,
88 		0xfd, 0xff,
89 		0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f,
90 		0x09, 0x02,
91 		0x0d, 0xb0, 0x3e, 0xab, 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe,
92 		0x03, 0xd1,
93 		0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee
94 	};
95 	struct tc_aes_key_sched_struct sched;
96 	uint8_t out[80];
97 	uint8_t decrypted[64];
98 	uint32_t result = TC_PASS;
99 
100 	TC_PRINT("CTR test #1 (encryption SP 800-38a tests):\n");
101 	(void)tc_aes128_set_encrypt_key(&sched, key);
102 
103 	(void)memcpy(out, ctr, sizeof(ctr));
104 
105 	/**TESTPOINT: Check test 1 result*/
106 	zassert_true(tc_ctr_mode(&out[TC_AES_BLOCK_SIZE],
107 			sizeof(plaintext), plaintext, sizeof(plaintext),
108 			ctr, &sched),
109 			"CTR test #1 (encryption SP 800-38a tests) failed");
110 
111 	result = check_result(1, ciphertext, sizeof(out), out, sizeof(out), 1);
112 
113 	TC_PRINT("CTR test #2 (decryption SP 800-38a tests):\n");
114 	(void)memcpy(ctr, out, sizeof(ctr));
115 
116 	/**TESTPOINT: Check test 2 result*/
117 	zassert_true(tc_ctr_mode(decrypted, sizeof(decrypted),
118 			&out[TC_AES_BLOCK_SIZE], sizeof(decrypted),
119 			ctr, &sched),
120 			"CTR test #2 (decryption SP 800-38a tests) failed");
121 
122 	result = check_result(2, plaintext, sizeof(plaintext),
123 			      decrypted, sizeof(plaintext), 1);
124 
125 	/**TESTPOINT: Check result*/
126 	zassert_false(result, "CBC test #1 failed");
127 	TC_PRINT("All CTR tests succeeded!\n");
128 }
129