1 /* 2 * Copyright The Mbed TLS Contributors 3 * SPDX-License-Identifier: Apache-2.0 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may 6 * not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * Copyright (c) 2023 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 17 */ 18 #ifndef MBEDTLS_AESNI_H 19 #define MBEDTLS_AESNI_H 20 21 #include "mbedtls/build_info.h" 22 23 #include "mbedtls/aes.h" 24 25 #define MBEDTLS_AESNI_AES 0x02000000u 26 #define MBEDTLS_AESNI_CLMUL 0x00000002u 27 28 #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__)) && \ 29 !defined(MBEDTLS_HAVE_X86_64) 30 #define MBEDTLS_HAVE_X86_64 31 #endif 32 33 #if defined(MBEDTLS_HAVE_X86_64) 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /** 40 * \brief Internal function to detect the AES-NI feature in CPUs. 41 * 42 * \note This function is only for internal use by other library 43 * functions; you must not call it directly. 44 * 45 * \param what The feature to detect 46 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) 47 * 48 * \return 1 if CPU has support for the feature, 0 otherwise 49 */ 50 int mbedtls_aesni_has_support(unsigned int what); 51 52 /** 53 * \brief Internal AES-NI AES-ECB block encryption and decryption 54 * 55 * \note This function is only for internal use by other library 56 * functions; you must not call it directly. 57 * 58 * \param ctx AES context 59 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 60 * \param input 16-byte input block 61 * \param output 16-byte output block 62 * 63 * \return 0 on success (cannot fail) 64 */ 65 int mbedtls_aesni_crypt_ecb( 66 mbedtls_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16]); 67 68 /** 69 * \brief Internal GCM multiplication: c = a * b in GF(2^128) 70 * 71 * \note This function is only for internal use by other library 72 * functions; you must not call it directly. 73 * 74 * \param c Result 75 * \param a First operand 76 * \param b Second operand 77 * 78 * \note Both operands and result are bit strings interpreted as 79 * elements of GF(2^128) as per the GCM spec. 80 */ 81 void mbedtls_aesni_gcm_mult(unsigned char c[16], const unsigned char a[16], const unsigned char b[16]); 82 83 /** 84 * \brief Internal round key inversion. This function computes 85 * decryption round keys from the encryption round keys. 86 * 87 * \note This function is only for internal use by other library 88 * functions; you must not call it directly. 89 * 90 * \param invkey Round keys for the equivalent inverse cipher 91 * \param fwdkey Original round keys (for encryption) 92 * \param nr Number of rounds (that is, number of round keys minus one) 93 */ 94 void mbedtls_aesni_inverse_key(unsigned char *invkey, const unsigned char *fwdkey, int nr); 95 96 /** 97 * \brief Internal key expansion for encryption 98 * 99 * \note This function is only for internal use by other library 100 * functions; you must not call it directly. 101 * 102 * \param rk Destination buffer where the round keys are written 103 * \param key Encryption key 104 * \param bits Key size in bits (must be 128, 192 or 256) 105 * 106 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH 107 */ 108 int mbedtls_aesni_setkey_enc(unsigned char *rk, const unsigned char *key, size_t bits); 109 110 #ifdef __cplusplus 111 } 112 #endif 113 114 #endif /* MBEDTLS_HAVE_X86_64 */ 115 116 #endif /* MBEDTLS_AESNI_H */ 117