blob: 7b16b4bad078bec5d2ded8fb7972caac9007e43d [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_AESNI_H
25#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010026
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020027#if !defined(MBEDTLS_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010033#include "aes.h"
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define MBEDTLS_AESNI_AES 0x02000000u
36#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039 ( defined(__amd64__) || defined(__x86_64__) ) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 ! defined(MBEDTLS_HAVE_X86_64)
41#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000046#ifdef __cplusplus
47extern "C" {
48#endif
49
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050/**
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010051 * \brief AES-NI features detection routine
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/**
61 * \brief AES-NI AES-ECB block en(de)cryption
62 *
63 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010065 * \param input 16-byte input block
66 * \param output 16-byte output block
67 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010068 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010071 int mode,
72 const unsigned char input[16],
73 unsigned char output[16] );
74
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010075/**
76 * \brief GCM multiplication: c = a * b in GF(2^128)
77 *
78 * \param c Result
79 * \param a First operand
80 * \param b Second operand
81 *
82 * \note Both operands and result are bit strings interpreted as
83 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +010086 const unsigned char a[16],
87 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010088
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010089/**
90 * \brief Compute decryption round keys from encryption round keys
91 *
92 * \param invkey Round keys for the equivalent inverse cipher
93 * \param fwdkey Original round keys (for encryption)
94 * \param nr Number of rounds (that is, number of round keys minus one)
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010097 const unsigned char *fwdkey, int nr );
98
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +010099/**
100 * \brief Perform key expansion (for encryption)
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 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100109 const unsigned char *key,
110 size_t bits );
111
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000112#ifdef __cplusplus
113}
114#endif
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#endif /* MBEDTLS_AESNI_H */