]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/cbc_std_vectors_test.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / aes / cbc_std_vectors_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 /*
31 * Run list of standard CBC test vectors through encode and decode checks.
32 */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <aes_cbc.h>
39 #include "types.h"
40 #include "cbc_std_vectors.h"
41
42 typedef void (*aes_cbc_generic) (uint8_t * in, uint8_t * IV, uint8_t * keys, uint8_t * out,
43 uint64_t len_bytes);
44
45 int check_data(uint8_t * test, uint8_t * expected, uint64_t len, char *data_name)
46 {
47 int mismatch;
48 int OK = 0;
49 uint64_t a;
50
51 mismatch = memcmp(test, expected, len);
52 if (!mismatch) {
53 return OK;
54
55 } else {
56 OK = 1;
57 printf(" failed %s \t\t", data_name);
58 for (a = 0; a < len; a++) {
59 if (test[a] != expected[a]) {
60 printf(" '%x' != '%x' at %lx of %lx\n",
61 test[a], expected[a], a, len);
62 break;
63 }
64 }
65 }
66 return OK;
67 }
68
69 int check_vector(struct cbc_vector *vector)
70 {
71 uint8_t *pt_test = NULL;
72 int OK = 0;
73 aes_cbc_generic enc;
74 aes_cbc_generic dec;
75
76 DEBUG_PRINT((" Keylen:%d PLen:%d ", (int)vector->K_LEN, (int)vector->P_LEN));
77 DEBUG_PRINT((" K:%p P:%p C:%p IV:%p expC:%p Keys:%p ", vector->K, vector->P, vector->C,
78 vector->IV, vector->EXP_C, vector->KEYS));
79 printf(".");
80
81 switch (vector->K_LEN) {
82 case CBC_128_BITS:
83 enc = (aes_cbc_generic) & aes_cbc_enc_128;
84 dec = (aes_cbc_generic) & aes_cbc_dec_128;
85 DEBUG_PRINT((" CBC128 "));
86 break;
87 case CBC_192_BITS:
88 enc = (aes_cbc_generic) & aes_cbc_enc_192;
89 dec = (aes_cbc_generic) & aes_cbc_dec_192;
90 DEBUG_PRINT((" CBC192 "));
91 break;
92 case CBC_256_BITS:
93 enc = (aes_cbc_generic) & aes_cbc_enc_256;
94 dec = (aes_cbc_generic) & aes_cbc_dec_256;
95 DEBUG_PRINT((" CBC256 "));
96 break;
97 default:
98 printf("Invalid key length: %d\n", vector->K_LEN);
99 return 1;
100 }
101
102 // Allocate space for the calculated ciphertext
103 pt_test = malloc(vector->P_LEN);
104
105 if (pt_test == NULL) {
106 fprintf(stderr, "Can't allocate ciphertext memory\n");
107 return 1;
108 }
109
110 aes_cbc_precomp(vector->K, vector->K_LEN, vector->KEYS);
111
112 ////
113 // ISA-l Encrypt
114 ////
115 enc(vector->P, vector->IV, vector->KEYS->enc_keys, vector->C, vector->P_LEN);
116
117 if (NULL != vector->EXP_C) { //when the encrypted text is known verify correct
118 OK |= check_data(vector->EXP_C, vector->C, vector->P_LEN,
119 "ISA-L expected cypher text (C)");
120 }
121 memcpy(pt_test, vector->P, vector->P_LEN);
122 memset(vector->P, 0, vector->P_LEN);
123
124 ////
125 // ISA-l Decrypt
126 ////
127 dec(vector->C, vector->IV, vector->KEYS->dec_keys, vector->P, vector->P_LEN);
128 OK |= check_data(vector->P, pt_test, vector->P_LEN, "ISA-L decrypted plain text (P)");
129 DEBUG_PRINT((OK ? "Failed\n" : "Passed\n"));
130
131 free(pt_test);
132 return OK;
133 }
134
135 int test_std_combinations(void)
136 {
137 int const vectors_cnt = sizeof(cbc_vectors) / sizeof(cbc_vectors[0]);
138 int i;
139 uint8_t *iv = NULL;
140
141 printf("AES CBC standard test vectors: ");
142
143 posix_memalign((void **)&iv, 16, (CBC_IV_DATA_LEN));
144 if (NULL == iv)
145 return 1;
146
147 for (i = 0; (i < vectors_cnt); i++) {
148 struct cbc_vector vect = cbc_vectors[i];
149
150 posix_memalign((void **)&(vect.KEYS), 16, sizeof(*vect.KEYS));
151 if (NULL == vect.KEYS)
152 return 1;
153
154 // IV data must be aligned to 16 byte boundary so move data in
155 // aligned buffer and change out the pointer
156 memcpy(iv, vect.IV, CBC_IV_DATA_LEN);
157 vect.IV = iv;
158 vect.C = malloc(vect.P_LEN);
159 if (NULL == vect.C)
160 return 1;
161
162 DEBUG_PRINT(("vector[%d of %d] ", i, vectors_cnt));
163
164 if (0 != check_vector(&vect))
165 return 1;
166
167 aligned_free(vect.KEYS);
168 free(vect.C);
169 }
170
171 aligned_free(iv);
172 return 0;
173 }
174
175 int main(void)
176 {
177 uint32_t OK = 0;
178
179 OK = test_std_combinations();
180
181 printf(0 == OK ? "Pass\n" : "Fail\n");
182 return OK;
183 }