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 |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | b66e7db | 2018-12-18 09:57:18 +0100 | [diff] [blame] | 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 | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 10 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 12 | * |
| 13 | * This file is provided under the Apache License 2.0, or the |
| 14 | * GNU General Public License v2.0 or later. |
| 15 | * |
| 16 | * ********** |
| 17 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 30 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 31 | * ********** |
| 32 | * |
| 33 | * ********** |
| 34 | * GNU General Public License v2.0 or later: |
| 35 | * |
| 36 | * This program is free software; you can redistribute it and/or modify |
| 37 | * it under the terms of the GNU General Public License as published by |
| 38 | * the Free Software Foundation; either version 2 of the License, or |
| 39 | * (at your option) any later version. |
| 40 | * |
| 41 | * This program is distributed in the hope that it will be useful, |
| 42 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 43 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 44 | * GNU General Public License for more details. |
| 45 | * |
| 46 | * You should have received a copy of the GNU General Public License along |
| 47 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 48 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 49 | * |
| 50 | * ********** |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 51 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #ifndef MBEDTLS_AESNI_H |
| 53 | #define MBEDTLS_AESNI_H |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 54 | |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 56 | #include "config.h" |
| 57 | #else |
| 58 | #include MBEDTLS_CONFIG_FILE |
| 59 | #endif |
| 60 | |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 61 | #include "aes.h" |
| 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #define MBEDTLS_AESNI_AES 0x02000000u |
| 64 | #define MBEDTLS_AESNI_CLMUL 0x00000002u |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 67 | ( defined(__amd64__) || defined(__x86_64__) ) && \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | ! defined(MBEDTLS_HAVE_X86_64) |
| 69 | #define MBEDTLS_HAVE_X86_64 |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 70 | #endif |
| 71 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_HAVE_X86_64) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 74 | #ifdef __cplusplus |
| 75 | extern "C" { |
| 76 | #endif |
| 77 | |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 78 | /** |
Manuel Pégourié-Gonnard | b66e7db | 2018-12-18 09:57:18 +0100 | [diff] [blame] | 79 | * \brief Internal function to detect the AES-NI feature in CPUs. |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 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 | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 83 | * |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 84 | * \param what The feature to detect |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 86 | * |
| 87 | * \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] | 88 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 89 | int mbedtls_aesni_has_support( unsigned int what ); |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 90 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 91 | /** |
Manuel Pégourié-Gonnard | b66e7db | 2018-12-18 09:57:18 +0100 | [diff] [blame] | 92 | * \brief Internal AES-NI AES-ECB block encryption and decryption |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 93 | * |
| 94 | * \note This function is only for internal use by other library |
| 95 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 96 | * |
| 97 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 99 | * \param input 16-byte input block |
| 100 | * \param output 16-byte output block |
| 101 | * |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 102 | * \return 0 on success (cannot fail) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 103 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx, |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 105 | int mode, |
| 106 | const unsigned char input[16], |
| 107 | unsigned char output[16] ); |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 109 | /** |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 110 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) |
| 111 | * |
| 112 | * \note This function is only for internal use by other library |
| 113 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 114 | * |
| 115 | * \param c Result |
| 116 | * \param a First operand |
| 117 | * \param b Second operand |
| 118 | * |
| 119 | * \note Both operands and result are bit strings interpreted as |
| 120 | * elements of GF(2^128) as per the GCM spec. |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 121 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | void mbedtls_aesni_gcm_mult( unsigned char c[16], |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 123 | const unsigned char a[16], |
| 124 | const unsigned char b[16] ); |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 125 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 126 | /** |
Manuel Pégourié-Gonnard | b66e7db | 2018-12-18 09:57:18 +0100 | [diff] [blame] | 127 | * \brief Internal round key inversion. This function computes |
| 128 | * decryption round keys from the encryption round keys. |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 129 | * |
| 130 | * \note This function is only for internal use by other library |
| 131 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 132 | * |
| 133 | * \param invkey Round keys for the equivalent inverse cipher |
| 134 | * \param fwdkey Original round keys (for encryption) |
| 135 | * \param nr Number of rounds (that is, number of round keys minus one) |
| 136 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | void mbedtls_aesni_inverse_key( unsigned char *invkey, |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 138 | const unsigned char *fwdkey, |
| 139 | int nr ); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 141 | /** |
Manuel Pégourié-Gonnard | b66e7db | 2018-12-18 09:57:18 +0100 | [diff] [blame] | 142 | * \brief Internal key expansion for encryption |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 143 | * |
| 144 | * \note This function is only for internal use by other library |
| 145 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 146 | * |
| 147 | * \param rk Destination buffer where the round keys are written |
| 148 | * \param key Encryption key |
| 149 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 150 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | * \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] | 152 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | int mbedtls_aesni_setkey_enc( unsigned char *rk, |
Manuel Pégourié-Gonnard | ad54c49 | 2018-12-13 11:15:26 +0100 | [diff] [blame] | 154 | const unsigned char *key, |
| 155 | size_t bits ); |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 156 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 157 | #ifdef __cplusplus |
| 158 | } |
| 159 | #endif |
| 160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #endif /* MBEDTLS_HAVE_X86_64 */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #endif /* MBEDTLS_AESNI_H */ |