The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/qat/qat_api/qat_utils/src/QatUtilsCrypto.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 }

Cache object: 1c391eb3c010300286de678a3375eb4e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.