1 /* SPDX-License-Identifier: BSD-3-Clause */ 2 /* Copyright(c) 2007-2022 Intel Corporation */ 3 /* $FreeBSD$ */ 4 #include "qat_utils.h" 5 6 CpaStatus 7 qatUtilsHashMD5(uint8_t *in, uint8_t *out) 8 { 9 MD5_CTX ctx; 10 11 MD5Init(&ctx); 12 MD5Update(&ctx, in, MD5_BLOCK_LENGTH); 13 bcopy(&ctx, out, MD5_DIGEST_LENGTH); 14 15 return CPA_STATUS_SUCCESS; 16 } 17 18 CpaStatus 19 qatUtilsHashSHA1(uint8_t *in, uint8_t *out) 20 { 21 SHA1_CTX ctx; 22 23 SHA1Init(&ctx); 24 SHA1Update(&ctx, in, SHA1_BLOCK_LEN); 25 bcopy(&ctx, out, SHA1_HASH_LEN); 26 27 return CPA_STATUS_SUCCESS; 28 } 29 30 CpaStatus 31 qatUtilsHashSHA224(uint8_t *in, uint8_t *out) 32 { 33 SHA224_CTX ctx; 34 35 SHA224_Init(&ctx); 36 SHA224_Update(&ctx, in, SHA224_BLOCK_LENGTH); 37 bcopy(&ctx, out, SHA256_DIGEST_LENGTH); 38 39 return CPA_STATUS_SUCCESS; 40 } 41 42 CpaStatus 43 qatUtilsHashSHA256(uint8_t *in, uint8_t *out) 44 { 45 SHA256_CTX ctx; 46 47 SHA256_Init(&ctx); 48 SHA256_Update(&ctx, in, SHA256_BLOCK_LENGTH); 49 bcopy(&ctx, out, SHA256_DIGEST_LENGTH); 50 51 return CPA_STATUS_SUCCESS; 52 } 53 54 CpaStatus 55 qatUtilsHashSHA384(uint8_t *in, uint8_t *out) 56 { 57 SHA384_CTX ctx; 58 59 SHA384_Init(&ctx); 60 SHA384_Update(&ctx, in, SHA384_BLOCK_LENGTH); 61 bcopy(&ctx, out, SHA512_DIGEST_LENGTH); 62 63 return CPA_STATUS_SUCCESS; 64 } 65 66 CpaStatus 67 qatUtilsHashSHA512(uint8_t *in, uint8_t *out) 68 { 69 SHA512_CTX ctx; 70 71 SHA512_Init(&ctx); 72 SHA512_Update(&ctx, in, SHA512_BLOCK_LENGTH); 73 bcopy(&ctx, out, SHA512_DIGEST_LENGTH); 74 75 return CPA_STATUS_SUCCESS; 76 } 77 78 CpaStatus 79 qatUtilsHashMD5Full(uint8_t *in, uint8_t *out, uint32_t len) 80 { 81 MD5_CTX ctx; 82 83 MD5Init(&ctx); 84 MD5Update(&ctx, in, len); 85 MD5Final(out, &ctx); 86 87 return CPA_STATUS_SUCCESS; 88 } 89 90 CpaStatus 91 qatUtilsHashSHA1Full(uint8_t *in, uint8_t *out, uint32_t len) 92 { 93 SHA1_CTX ctx; 94 95 SHA1Init(&ctx); 96 SHA1Update(&ctx, in, len); 97 SHA1Final((caddr_t)out, &ctx); 98 99 return CPA_STATUS_SUCCESS; 100 } 101 102 CpaStatus 103 qatUtilsHashSHA256Full(uint8_t *in, uint8_t *out, uint32_t len) 104 { 105 SHA256_CTX ctx; 106 107 SHA256_Init(&ctx); 108 SHA256_Update(&ctx, in, len); 109 SHA256_Final(out, &ctx); 110 111 return CPA_STATUS_SUCCESS; 112 } 113 114 CpaStatus 115 qatUtilsHashSHA384Full(uint8_t *in, uint8_t *out, uint32_t len) 116 { 117 SHA384_CTX ctx; 118 119 SHA384_Init(&ctx); 120 SHA384_Update(&ctx, in, len); 121 SHA384_Final(out, &ctx); 122 123 return CPA_STATUS_SUCCESS; 124 } 125 126 CpaStatus 127 qatUtilsHashSHA512Full(uint8_t *in, uint8_t *out, uint32_t len) 128 { 129 SHA512_CTX ctx; 130 131 SHA512_Init(&ctx); 132 SHA512_Update(&ctx, in, len); 133 SHA512_Final(out, &ctx); 134 135 return CPA_STATUS_SUCCESS; 136 } 137 138 #define BYTE_TO_BITS_SHIFT 3 139 140 CpaStatus 141 qatUtilsAESEncrypt(uint8_t *key, 142 uint32_t keyLenInBytes, 143 uint8_t *in, 144 uint8_t *out) 145 { 146 rijndael_ctx ctx; 147 148 rijndael_set_key(&ctx, key, keyLenInBytes << BYTE_TO_BITS_SHIFT); 149 rijndael_encrypt(&ctx, in, out); 150 151 return CPA_STATUS_SUCCESS; 152 } 153