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