blob: c68e3601147cd8ef93afef1f57feeb060f3f3a90 [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
2 * \file aesni.h
3 *
4 * \brief AES-NI for hardware AES acceleration on some Intel processors
5 */
6/*
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * 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.
21 *
22 * This file is part of Mbed Crypto (https://tls.mbed.org)
23 */
24#ifndef MBEDCRYPTO_AESNI_H
25#define MBEDCRYPTO_AESNI_H
26
27#include "aes.h"
28
29#define MBEDCRYPTO_AESNI_AES 0x02000000u
30#define MBEDCRYPTO_AESNI_CLMUL 0x00000002u
31
32#if defined(MBEDCRYPTO_HAVE_ASM) && defined(__GNUC__) && \
33 ( defined(__amd64__) || defined(__x86_64__) ) && \
34 ! defined(MBEDCRYPTO_HAVE_X86_64)
35#define MBEDCRYPTO_HAVE_X86_64
36#endif
37
38#if defined(MBEDCRYPTO_HAVE_X86_64)
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \brief AES-NI features detection routine
46 *
47 * \param what The feature to detect
48 * (MBEDCRYPTO_AESNI_AES or MBEDCRYPTO_AESNI_CLMUL)
49 *
50 * \return 1 if CPU has support for the feature, 0 otherwise
51 */
52int mbedcrypto_aesni_has_support( unsigned int what );
53
54/**
55 * \brief AES-NI AES-ECB block en(de)cryption
56 *
57 * \param ctx AES context
58 * \param mode MBEDCRYPTO_AES_ENCRYPT or MBEDCRYPTO_AES_DECRYPT
59 * \param input 16-byte input block
60 * \param output 16-byte output block
61 *
62 * \return 0 on success (cannot fail)
63 */
64int mbedcrypto_aesni_crypt_ecb( mbedcrypto_aes_context *ctx,
65 int mode,
66 const unsigned char input[16],
67 unsigned char output[16] );
68
69/**
70 * \brief GCM multiplication: c = a * b in GF(2^128)
71 *
72 * \param c Result
73 * \param a First operand
74 * \param b Second operand
75 *
76 * \note Both operands and result are bit strings interpreted as
77 * elements of GF(2^128) as per the GCM spec.
78 */
79void mbedcrypto_aesni_gcm_mult( unsigned char c[16],
80 const unsigned char a[16],
81 const unsigned char b[16] );
82
83/**
84 * \brief Compute decryption round keys from encryption round keys
85 *
86 * \param invkey Round keys for the equivalent inverse cipher
87 * \param fwdkey Original round keys (for encryption)
88 * \param nr Number of rounds (that is, number of round keys minus one)
89 */
90void mbedcrypto_aesni_inverse_key( unsigned char *invkey,
91 const unsigned char *fwdkey, int nr );
92
93/**
94 * \brief Perform key expansion (for encryption)
95 *
96 * \param rk Destination buffer where the round keys are written
97 * \param key Encryption key
98 * \param bits Key size in bits (must be 128, 192 or 256)
99 *
100 * \return 0 if successful, or MBEDCRYPTO_ERR_AES_INVALID_KEY_LENGTH
101 */
102int mbedcrypto_aesni_setkey_enc( unsigned char *rk,
103 const unsigned char *key,
104 size_t bits );
105
106#ifdef __cplusplus
107}
108#endif
109
110#endif /* MBEDCRYPTO_HAVE_X86_64 */
111
112#endif /* MBEDCRYPTO_AESNI_H */