blob: 0196f49b872f5e9c7b6063dfe48c875fbfbe2664 [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/**
2 * \file aesni.h
3 *
4 * \brief AES-NI for hardware AES acceleration on some Intel processors
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +01005 *
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +01006 * \warning These functions are only for internal use by other library
7 * functions; you must not call them directly.
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +020010 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020011 * 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é-Gonnard92ac76f2013-12-16 17:12:53 +010024 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000025 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#ifndef MBEDTLS_AESNI_H
28#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010029
30#include "aes.h"
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define MBEDTLS_AESNI_AES 0x02000000u
33#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010036 ( defined(__amd64__) || defined(__x86_64__) ) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 ! defined(MBEDTLS_HAVE_X86_64)
38#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010042
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000043#ifdef __cplusplus
44extern "C" {
45#endif
46
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010047/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +010048 * \brief Internal function to detect the AES-NI feature in CPUs.
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010049 *
50 * \note This function is only for internal use by other library
51 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010052 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010053 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010055 *
56 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010057 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010058int mbedtls_aesni_has_support( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010059
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010060/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +010061 * \brief Internal AES-NI AES-ECB block encryption and decryption
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010062 *
63 * \note This function is only for internal use by other library
64 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010065 *
66 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010068 * \param input 16-byte input block
69 * \param output 16-byte output block
70 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010071 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010074 int mode,
75 const unsigned char input[16],
76 unsigned char output[16] );
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010077
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010078/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010079 * \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é-Gonnardd333f672013-12-26 11:44:46 +010083 *
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é-Gonnardd333f672013-12-26 11:44:46 +010090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010092 const unsigned char a[16],
93 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010094
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010095/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +010096 * \brief Internal round key inversion. This function computes
97 * decryption round keys from the encryption round keys.
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010098 *
99 * \note This function is only for internal use by other library
100 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100101 *
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100107 const unsigned char *fwdkey,
108 int nr );
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100109
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100110/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +0100111 * \brief Internal key expansion for encryption
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100112 *
113 * \note This function is only for internal use by other library
114 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100115 *
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100123 const unsigned char *key,
124 size_t bits );
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100125
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000126#ifdef __cplusplus
127}
128#endif
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_AESNI_H */