blob: 6b64f45d0ac03308940198659bd3dd8039bb8bd1 [file] [log] [blame]
Jerry Yu49231312023-01-10 16:57:21 +08001/**
2 * \file aesce.h
3 *
Dave Rodgmanf918d422023-03-17 17:52:23 +00004 * \brief Support hardware AES acceleration on Armv8-A processors with
Dave Rodgmanece803b2023-10-08 20:24:48 +01005 * the Armv8-A Cryptographic Extension.
Jerry Yu49231312023-01-10 16:57:21 +08006 *
7 * \warning These functions are only for internal use by other library
8 * functions; you must not call them directly.
9 */
10/*
11 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jerry Yu49231312023-01-10 16:57:21 +080013 */
14#ifndef MBEDTLS_AESCE_H
15#define MBEDTLS_AESCE_H
16
17#include "mbedtls/build_info.h"
Dave Rodgman12d1c3a2023-11-30 09:38:38 +000018#include "common.h"
Jerry Yu49231312023-01-10 16:57:21 +080019
20#include "mbedtls/aes.h"
21
Jerry Yu07d28d82023-03-20 18:12:36 +080022
Dave Rodgman12d1c3a2023-11-30 09:38:38 +000023#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_ARCH_IS_ARMV8_A) && \
24 defined(MBEDTLS_HAVE_NEON_INTRINSICS)
Jerry Yu72fd0bd2023-08-18 16:31:01 +080025
26#define MBEDTLS_AESCE_HAVE_CODE
Jerry Yu49231312023-01-10 16:57:21 +080027
28#ifdef __cplusplus
29extern "C" {
30#endif
Jerry Yub95c7762023-01-10 16:59:51 +080031
Dave Rodgman45661322023-08-04 12:31:58 +010032#if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
33
Dave Rodgmanb30adce2023-08-04 12:52:51 +010034extern signed char mbedtls_aesce_has_support_result;
Dave Rodgman45661322023-08-04 12:31:58 +010035
Jerry Yub95c7762023-01-10 16:59:51 +080036/**
Jerry Yuc8bcdc82023-02-21 14:49:02 +080037 * \brief Internal function to detect the crypto extension in CPUs.
Jerry Yub95c7762023-01-10 16:59:51 +080038 *
39 * \return 1 if CPU has support for the feature, 0 otherwise
40 */
Dave Rodgman45661322023-08-04 12:31:58 +010041int mbedtls_aesce_has_support_impl(void);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080042
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010043#define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \
Dave Rodgman45661322023-08-04 12:31:58 +010044 mbedtls_aesce_has_support_impl() : \
45 mbedtls_aesce_has_support_result)
46
47#else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
48
49/* If we are not on Linux, we can't detect support so assume that it's supported.
50 * Similarly, assume support if MBEDTLS_AES_USE_HARDWARE_ONLY is set.
51 */
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010052#define MBEDTLS_AESCE_HAS_SUPPORT() 1
Dave Rodgman45661322023-08-04 12:31:58 +010053
54#endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
Jerry Yub95c7762023-01-10 16:59:51 +080055
Jerry Yu2bb3d812023-01-10 17:38:26 +080056/**
57 * \brief Internal AES-ECB block encryption and decryption
58 *
Dave Rodgman48fd2ab2023-06-16 09:36:50 +010059 * \warning This assumes that the context specifies either 10, 12 or 14
60 * rounds and will behave incorrectly if this is not the case.
Dave Rodgman96fdfb82023-06-15 16:21:31 +010061 *
Jerry Yu2bb3d812023-01-10 17:38:26 +080062 * \param ctx AES context
63 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
64 * \param input 16-byte input block
65 * \param output 16-byte output block
66 *
67 * \return 0 on success (cannot fail)
68 */
69int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
70 int mode,
71 const unsigned char input[16],
72 unsigned char output[16]);
73
Jerry Yu3f2fb712023-01-10 17:05:42 +080074/**
Jerry Yudf87a122023-01-10 18:17:15 +080075 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
76 *
77 * \note This function is only for internal use by other library
78 * functions; you must not call it directly.
79 *
80 * \param c Result
81 * \param a First operand
82 * \param b Second operand
83 *
84 * \note Both operands and result are bit strings interpreted as
85 * elements of GF(2^128) as per the GCM spec.
86 */
87void mbedtls_aesce_gcm_mult(unsigned char c[16],
88 const unsigned char a[16],
89 const unsigned char b[16]);
90
91
Yanray Wangb67b4742023-10-31 17:10:32 +080092#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Jerry Yudf87a122023-01-10 18:17:15 +080093/**
Jerry Yue096da12023-01-10 17:07:01 +080094 * \brief Internal round key inversion. This function computes
95 * decryption round keys from the encryption round keys.
96 *
97 * \param invkey Round keys for the equivalent inverse cipher
98 * \param fwdkey Original round keys (for encryption)
99 * \param nr Number of rounds (that is, number of round keys minus one)
100 */
101void mbedtls_aesce_inverse_key(unsigned char *invkey,
102 const unsigned char *fwdkey,
103 int nr);
Yanray Wangb67b4742023-10-31 17:10:32 +0800104#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Jerry Yue096da12023-01-10 17:07:01 +0800105
106/**
Jerry Yu3f2fb712023-01-10 17:05:42 +0800107 * \brief Internal key expansion for encryption
108 *
109 * \param rk Destination buffer where the round keys are written
110 * \param key Encryption key
111 * \param bits Key size in bits (must be 128, 192 or 256)
112 *
113 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
114 */
115int mbedtls_aesce_setkey_enc(unsigned char *rk,
116 const unsigned char *key,
117 size_t bits);
118
Jerry Yu49231312023-01-10 16:57:21 +0800119#ifdef __cplusplus
120}
121#endif
122
Dave Rodgmanece803b2023-10-08 20:24:48 +0100123#else
124
Dave Rodgman9fd1b522023-10-10 15:23:44 +0100125#if defined(MBEDTLS_AES_USE_HARDWARE_ONLY) && defined(MBEDTLS_ARCH_IS_ARMV8_A)
Dave Rodgmanece803b2023-10-08 20:24:48 +0100126#error "AES hardware acceleration not supported on this platform"
127#endif
128
Dave Rodgman12d1c3a2023-11-30 09:38:38 +0000129#endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARMV8_A && MBEDTLS_HAVE_NEON_INTRINSICS */
Jerry Yu49231312023-01-10 16:57:21 +0800130
131#endif /* MBEDTLS_AESCE_H */