]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/xts_256_expanded_key_test.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / aes / xts_256_expanded_key_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 #include <stdlib.h>
31 #include <stdio.h>
32 #include <aes_keyexp.h>
33 #include "xts_256_vect.h"
34
35 int main(void)
36 {
37
38 // Temporary array for the calculated vectors
39 uint8_t *ct_test;
40 uint8_t *pt_test;
41 // Arrays for expanded keys, null_key is a dummy vector (decrypt key not
42 // needed for the tweak part of the decryption)
43 uint8_t expkey1_enc[16 * 15], expkey2_enc[16 * 15];
44 uint8_t expkey1_dec[16 * 15], null_key[16 * 15];
45
46 int i, j;
47
48 // --- Encryption test ---
49
50 // Loop over the vectors
51 for (i = 0; i < NVEC; i++) {
52
53 // Allocate space for the calculated ciphertext
54 ct_test = malloc(vlist[i].ptlen);
55 if (ct_test == NULL) {
56 printf("Can't allocate ciphertext memory\n");
57 return -1;
58 }
59 // Pre-expand our keys (will only use the encryption ones here)
60 aes_keyexp_256(vlist[i].key1, expkey1_enc, expkey1_dec);
61 aes_keyexp_256(vlist[i].key2, expkey2_enc, null_key);
62
63 XTS_AES_256_enc_expanded_key(expkey2_enc, expkey1_enc, vlist[i].TW,
64 vlist[i].ptlen, vlist[i].PTX, ct_test);
65
66 // Carry out comparison of the calculated ciphertext with
67 // the reference
68 for (j = 0; j < vlist[i].ptlen; j++) {
69
70 if (ct_test[j] != vlist[i].CTX[j]) {
71 printf("\nXTS_AES_256_enc: Vector %d: ", i + 10);
72 printf("failed at byte %d! \n", j);
73 return -1;
74 }
75 }
76 printf(".");
77 }
78
79 // --- Decryption test ---
80
81 // Loop over the vectors
82 for (i = 0; i < NVEC; i++) {
83
84 // Allocate space for the calculated plaintext
85 pt_test = malloc(vlist[i].ptlen);
86 if (pt_test == NULL) {
87 printf("Can't allocate plaintext memory\n");
88 return -1;
89 }
90 // Pre-expand keys for the decryption
91 aes_keyexp_256(vlist[i].key1, expkey1_enc, expkey1_dec);
92 aes_keyexp_256(vlist[i].key2, expkey2_enc, null_key);
93
94 // Note, encryption key is re-used for the tweak decryption step
95 XTS_AES_256_dec_expanded_key(expkey2_enc, expkey1_dec, vlist[i].TW,
96 vlist[i].ptlen, vlist[i].CTX, pt_test);
97
98 // Carry out comparison of the calculated ciphertext with
99 // the reference
100 for (j = 0; j < vlist[i].ptlen; j++) {
101
102 if (pt_test[j] != vlist[i].PTX[j]) {
103 printf("\nXTS_AES_256_dec: Vector %d: ", i + 10);
104 printf("failed at byte %d! \n", j);
105 return -1;
106 }
107 }
108 printf(".");
109 }
110 printf("Pass\n");
111
112 return 0;
113 }