1 /*******************************************************************************
2   Copyright (c) 2012-2020, Intel Corporation
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6 
7       * Redistributions of source code must retain the above copyright notice,
8         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 the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of Intel Corporation nor the names of its contributors
13         may be used to endorse or promote products derived from this software
14         without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27 
28 
29 #include <stdio.h>
30 #include "intel-ipsec-mb.h"
31 
32 #include "noaesni.h"
33 #include "asm.h"
34 #include "include/clear_regs_mem.h"
35 
36 static uint32_t in[4*3] = {
37         0x01010101, 0x01010101, 0x01010101, 0x01010101,
38         0x02020202, 0x02020202, 0x02020202, 0x02020202,
39         0x03030303, 0x03030303, 0x03030303, 0x03030303
40 };
41 
42 void
aes_xcbc_expand_key_sse(const void * key,void * k1_exp,void * k2,void * k3)43 aes_xcbc_expand_key_sse(const void *key, void *k1_exp, void *k2, void *k3)
44 {
45 #ifdef SAFE_PARAM
46         if ((key == NULL) || (k1_exp == NULL) ||
47             (k2 == NULL) || (k3 == NULL))
48                 return;
49 #endif
50         DECLARE_ALIGNED(uint32_t keys_exp_enc[11*4], 16);
51 
52         aes_keyexp_128_enc_sse(key, keys_exp_enc);
53 
54         aes128_ecbenc_x3_sse(in, keys_exp_enc, k1_exp, k2, k3);
55 
56         aes_keyexp_128_enc_sse(k1_exp, k1_exp);
57 
58 #ifdef SAFE_DATA
59         clear_mem(&keys_exp_enc, sizeof(keys_exp_enc));
60 #endif
61 }
62 
63 void
aes_xcbc_expand_key_sse_no_aesni(const void * key,void * k1_exp,void * k2,void * k3)64 aes_xcbc_expand_key_sse_no_aesni(const void *key, void *k1_exp,
65                                  void *k2, void *k3)
66 {
67 #ifdef SAFE_PARAM
68         if ((key == NULL) || (k1_exp == NULL) ||
69             (k2 == NULL) || (k3 == NULL))
70                 return;
71 #endif
72         DECLARE_ALIGNED(uint32_t keys_exp_enc[11*4], 16);
73 
74         aes_keyexp_128_enc_sse_no_aesni(key, keys_exp_enc);
75 
76         aes128_ecbenc_x3_sse_no_aesni(in, keys_exp_enc, k1_exp, k2, k3);
77 
78         aes_keyexp_128_enc_sse_no_aesni(k1_exp, k1_exp);
79 
80 #ifdef SAFE_DATA
81         clear_mem(&keys_exp_enc, sizeof(keys_exp_enc));
82 #endif
83 }
84 
85 __forceinline
86 void
aes_xcbc_expand_key_avx_common(const void * key,void * k1_exp,void * k2,void * k3)87 aes_xcbc_expand_key_avx_common(const void *key,
88                                void *k1_exp, void *k2, void *k3)
89 {
90 #ifdef SAFE_PARAM
91         if ((key == NULL) || (k1_exp == NULL) ||
92             (k2 == NULL) || (k3 == NULL))
93                 return;
94 #endif
95         DECLARE_ALIGNED(uint32_t keys_exp_enc[11*4], 16);
96 
97         aes_keyexp_128_enc_avx(key, keys_exp_enc);
98 
99         aes128_ecbenc_x3_avx(in, keys_exp_enc, k1_exp, k2, k3);
100 
101         aes_keyexp_128_enc_avx(k1_exp, k1_exp);
102 
103 #ifdef SAFE_DATA
104         clear_mem(&keys_exp_enc, sizeof(keys_exp_enc));
105 #endif
106 }
107 
108 void
aes_xcbc_expand_key_avx(const void * key,void * k1_exp,void * k2,void * k3)109 aes_xcbc_expand_key_avx(const void *key, void *k1_exp, void *k2, void *k3)
110 {
111 #ifdef SAFE_PARAM
112         if ((key == NULL) || (k1_exp == NULL) ||
113             (k2 == NULL) || (k3 == NULL))
114                 return;
115 #endif
116         aes_xcbc_expand_key_avx_common(key, k1_exp, k2, k3);
117 }
118 
119 void
aes_xcbc_expand_key_avx2(const void * key,void * k1_exp,void * k2,void * k3)120 aes_xcbc_expand_key_avx2(const void *key, void *k1_exp, void *k2, void *k3)
121 {
122 #ifdef SAFE_PARAM
123         if ((key == NULL) || (k1_exp == NULL) ||
124             (k2 == NULL) || (k3 == NULL))
125                 return;
126 #endif
127         aes_xcbc_expand_key_avx_common(key, k1_exp, k2, k3);
128 }
129 
130 void
aes_xcbc_expand_key_avx512(const void * key,void * k1_exp,void * k2,void * k3)131 aes_xcbc_expand_key_avx512(const void *key, void *k1_exp, void *k2, void *k3)
132 {
133 #ifdef SAFE_PARAM
134         if ((key == NULL) || (k1_exp == NULL) ||
135             (k2 == NULL) || (k3 == NULL))
136                 return;
137 #endif
138         aes_xcbc_expand_key_avx_common(key, k1_exp, k2, k3);
139 }
140