1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 #include "qat_utils.h"
4
5 #define AES_128_KEY_LEN_BYTES 16
6 #define AES_192_KEY_LEN_BYTES 24
7 #define AES_256_KEY_LEN_BYTES 32
8
9 CpaStatus
qatUtilsHashMD5(uint8_t * in,uint8_t * out)10 qatUtilsHashMD5(uint8_t *in, uint8_t *out)
11 {
12 MD5_CTX ctx;
13
14 MD5Init(&ctx);
15 MD5Update(&ctx, in, MD5_BLOCK_LENGTH);
16 bcopy(&ctx, out, MD5_DIGEST_LENGTH);
17
18 return CPA_STATUS_SUCCESS;
19 }
20
21 CpaStatus
qatUtilsHashSHA1(uint8_t * in,uint8_t * out)22 qatUtilsHashSHA1(uint8_t *in, uint8_t *out)
23 {
24 SHA1_CTX ctx;
25
26 SHA1Init(&ctx);
27 SHA1Update(&ctx, in, SHA1_BLOCK_LEN);
28 bcopy(&ctx, out, SHA1_HASH_LEN);
29
30 return CPA_STATUS_SUCCESS;
31 }
32
33 CpaStatus
qatUtilsHashSHA224(uint8_t * in,uint8_t * out)34 qatUtilsHashSHA224(uint8_t *in, uint8_t *out)
35 {
36 SHA224_CTX ctx;
37
38 SHA224_Init(&ctx);
39 SHA224_Update(&ctx, in, SHA224_BLOCK_LENGTH);
40 bcopy(&ctx, out, SHA256_DIGEST_LENGTH);
41
42 return CPA_STATUS_SUCCESS;
43 }
44
45 CpaStatus
qatUtilsHashSHA256(uint8_t * in,uint8_t * out)46 qatUtilsHashSHA256(uint8_t *in, uint8_t *out)
47 {
48 SHA256_CTX ctx;
49
50 SHA256_Init(&ctx);
51 SHA256_Update(&ctx, in, SHA256_BLOCK_LENGTH);
52 bcopy(&ctx, out, SHA256_DIGEST_LENGTH);
53
54 return CPA_STATUS_SUCCESS;
55 }
56
57 CpaStatus
qatUtilsHashSHA384(uint8_t * in,uint8_t * out)58 qatUtilsHashSHA384(uint8_t *in, uint8_t *out)
59 {
60 SHA384_CTX ctx;
61
62 SHA384_Init(&ctx);
63 SHA384_Update(&ctx, in, SHA384_BLOCK_LENGTH);
64 bcopy(&ctx, out, SHA512_DIGEST_LENGTH);
65
66 return CPA_STATUS_SUCCESS;
67 }
68
69 CpaStatus
qatUtilsHashSHA512(uint8_t * in,uint8_t * out)70 qatUtilsHashSHA512(uint8_t *in, uint8_t *out)
71 {
72 SHA512_CTX ctx;
73
74 SHA512_Init(&ctx);
75 SHA512_Update(&ctx, in, SHA512_BLOCK_LENGTH);
76 bcopy(&ctx, out, SHA512_DIGEST_LENGTH);
77
78 return CPA_STATUS_SUCCESS;
79 }
80
81 CpaStatus
qatUtilsHashMD5Full(uint8_t * in,uint8_t * out,uint32_t len)82 qatUtilsHashMD5Full(uint8_t *in, uint8_t *out, uint32_t len)
83 {
84 MD5_CTX ctx;
85
86 MD5Init(&ctx);
87 MD5Update(&ctx, in, len);
88 MD5Final(out, &ctx);
89
90 return CPA_STATUS_SUCCESS;
91 }
92
93 CpaStatus
qatUtilsHashSHA1Full(uint8_t * in,uint8_t * out,uint32_t len)94 qatUtilsHashSHA1Full(uint8_t *in, uint8_t *out, uint32_t len)
95 {
96 SHA1_CTX ctx;
97
98 SHA1Init(&ctx);
99 SHA1Update(&ctx, in, len);
100 SHA1Final((caddr_t)out, &ctx);
101
102 return CPA_STATUS_SUCCESS;
103 }
104
105 CpaStatus
qatUtilsHashSHA256Full(uint8_t * in,uint8_t * out,uint32_t len)106 qatUtilsHashSHA256Full(uint8_t *in, uint8_t *out, uint32_t len)
107 {
108 SHA256_CTX ctx;
109
110 SHA256_Init(&ctx);
111 SHA256_Update(&ctx, in, len);
112 SHA256_Final(out, &ctx);
113
114 return CPA_STATUS_SUCCESS;
115 }
116
117 CpaStatus
qatUtilsHashSHA384Full(uint8_t * in,uint8_t * out,uint32_t len)118 qatUtilsHashSHA384Full(uint8_t *in, uint8_t *out, uint32_t len)
119 {
120 SHA384_CTX ctx;
121
122 SHA384_Init(&ctx);
123 SHA384_Update(&ctx, in, len);
124 SHA384_Final(out, &ctx);
125
126 return CPA_STATUS_SUCCESS;
127 }
128
129 CpaStatus
qatUtilsHashSHA512Full(uint8_t * in,uint8_t * out,uint32_t len)130 qatUtilsHashSHA512Full(uint8_t *in, uint8_t *out, uint32_t len)
131 {
132 SHA512_CTX ctx;
133
134 SHA512_Init(&ctx);
135 SHA512_Update(&ctx, in, len);
136 SHA512_Final(out, &ctx);
137
138 return CPA_STATUS_SUCCESS;
139 }
140
141 #define BYTE_TO_BITS_SHIFT 3
142
143 CpaStatus
qatUtilsAESEncrypt(uint8_t * key,uint32_t keyLenInBytes,uint8_t * in,uint8_t * out)144 qatUtilsAESEncrypt(uint8_t *key,
145 uint32_t keyLenInBytes,
146 uint8_t *in,
147 uint8_t *out)
148 {
149 rijndael_ctx ctx;
150
151 rijndael_set_key(&ctx, key, keyLenInBytes << BYTE_TO_BITS_SHIFT);
152 rijndael_encrypt(&ctx, in, out);
153
154 return CPA_STATUS_SUCCESS;
155 }
156
157 CpaStatus
qatUtilsAESKeyExpansionForward(uint8_t * key,uint32_t keyLenInBytes,uint32_t * out)158 qatUtilsAESKeyExpansionForward(uint8_t *key,
159 uint32_t keyLenInBytes,
160 uint32_t *out)
161 {
162 rijndael_ctx ctx;
163 uint32_t i = 0, j = 0;
164 uint32_t lw_per_round = 4;
165 int32_t lw_left_to_copy = keyLenInBytes / lw_per_round;
166 uint32_t *key_pointer = NULL;
167
168 /* Error check for wrong input key len */
169 if (AES_128_KEY_LEN_BYTES != keyLenInBytes &&
170 AES_192_KEY_LEN_BYTES != keyLenInBytes &&
171 AES_256_KEY_LEN_BYTES != keyLenInBytes) {
172 return CPA_STATUS_INVALID_PARAM;
173 }
174
175 rijndael_set_key(&ctx, key, keyLenInBytes << BYTE_TO_BITS_SHIFT);
176
177 /* Pointer to the last round of expanded key. */
178 key_pointer = &ctx.ek[lw_per_round * ctx.Nr];
179
180 while (lw_left_to_copy > 0) {
181 for (i = 0; i < MIN(lw_left_to_copy, lw_per_round); i++, j++) {
182 out[j] = __builtin_bswap32(key_pointer[i]);
183 }
184
185 lw_left_to_copy -= lw_per_round;
186 key_pointer -= lw_left_to_copy;
187 }
188
189 return CPA_STATUS_SUCCESS;
190 }
191