]>
git.proxmox.com Git - ceph.git/blob - 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.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
31 * Run list of standard CBC test vectors through encode and decode checks.
40 #include "cbc_std_vectors.h"
42 typedef void (*aes_cbc_generic
) (uint8_t * in
, uint8_t * IV
, uint8_t * keys
, uint8_t * out
,
45 int check_data(uint8_t * test
, uint8_t * expected
, uint64_t len
, char *data_name
)
51 mismatch
= memcmp(test
, expected
, len
);
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
);
69 int check_vector(struct cbc_vector
*vector
)
71 uint8_t *pt_test
= NULL
;
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
));
81 switch (vector
->K_LEN
) {
83 enc
= (aes_cbc_generic
) & aes_cbc_enc_128
;
84 dec
= (aes_cbc_generic
) & aes_cbc_dec_128
;
85 DEBUG_PRINT((" CBC128 "));
88 enc
= (aes_cbc_generic
) & aes_cbc_enc_192
;
89 dec
= (aes_cbc_generic
) & aes_cbc_dec_192
;
90 DEBUG_PRINT((" CBC192 "));
93 enc
= (aes_cbc_generic
) & aes_cbc_enc_256
;
94 dec
= (aes_cbc_generic
) & aes_cbc_dec_256
;
95 DEBUG_PRINT((" CBC256 "));
98 printf("Invalid key length: %d\n", vector
->K_LEN
);
102 // Allocate space for the calculated ciphertext
103 pt_test
= malloc(vector
->P_LEN
);
105 if (pt_test
== NULL
) {
106 fprintf(stderr
, "Can't allocate ciphertext memory\n");
110 aes_cbc_precomp(vector
->K
, vector
->K_LEN
, vector
->KEYS
);
115 enc(vector
->P
, vector
->IV
, vector
->KEYS
->enc_keys
, vector
->C
, vector
->P_LEN
);
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)");
121 memcpy(pt_test
, vector
->P
, vector
->P_LEN
);
122 memset(vector
->P
, 0, vector
->P_LEN
);
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"));
135 int test_std_combinations(void)
137 int const vectors_cnt
= sizeof(cbc_vectors
) / sizeof(cbc_vectors
[0]);
141 printf("AES CBC standard test vectors: ");
143 posix_memalign((void **)&iv
, 16, (CBC_IV_DATA_LEN
));
147 for (i
= 0; (i
< vectors_cnt
); i
++) {
148 struct cbc_vector vect
= cbc_vectors
[i
];
150 posix_memalign((void **)&(vect
.KEYS
), 16, sizeof(*vect
.KEYS
));
151 if (NULL
== vect
.KEYS
)
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
);
158 vect
.C
= malloc(vect
.P_LEN
);
162 DEBUG_PRINT(("vector[%d of %d] ", i
, vectors_cnt
));
164 if (0 != check_vector(&vect
))
167 aligned_free(vect
.KEYS
);
179 OK
= test_std_combinations();
181 printf(0 == OK
? "Pass\n" : "Fail\n");