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