Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Arm64 crypto engine support functions |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_AESCE_C) |
| 24 | |
| 25 | #include "aesce.h" |
| 26 | |
| 27 | #if defined(MBEDTLS_HAVE_ARM64) |
| 28 | |
| 29 | #if defined(__clang__) |
| 30 | # if __clang_major__ < 4 |
| 31 | # error "A more recent Clang is required for MBEDTLS_AES_C" |
| 32 | # endif |
| 33 | #elif defined(__GNUC__) |
| 34 | # if __GNUC__ < 6 |
| 35 | # error "A more recent GCC is required for MBEDTLS_AES_C" |
| 36 | # endif |
| 37 | #else |
| 38 | # error "Only GCC and Clang supported for MBEDTLS_AES_C" |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(__ARM_FEATURE_CRYPTO) |
| 42 | # error "`crypto` feature moddifier MUST be enabled for MBEDTLS_AESCE_C." |
| 43 | # error "Typical option for GCC and Clang is `-march=armv8-a+crypto`." |
| 44 | #endif /* !__ARM_FEATURE_CRYPTO */ |
| 45 | |
| 46 | #include <arm_neon.h> |
| 47 | |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 48 | #if defined(__linux__) |
| 49 | #include <asm/hwcap.h> |
| 50 | #include <sys/auxv.h> |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * AES instruction support detection routine |
| 55 | */ |
| 56 | int mbedtls_aesce_has_support(void) |
| 57 | { |
| 58 | #if defined(__linux__) |
| 59 | unsigned long auxval = getauxval(AT_HWCAP); |
| 60 | return (auxval & (HWCAP_ASIMD | HWCAP_AES)) == |
| 61 | (HWCAP_ASIMD | HWCAP_AES); |
| 62 | #else |
| 63 | /* Suppose aes instructions are supported. */ |
| 64 | return 1; |
| 65 | #endif |
| 66 | } |
| 67 | |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame^] | 68 | |
| 69 | static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10, |
| 70 | 0x20, 0x40, 0x80, 0x1b, 0x36 }; |
| 71 | |
| 72 | static inline uint32_t ror32_8(uint32_t word) |
| 73 | { |
| 74 | return (word << (32 - 8)) | (word >> 8); |
| 75 | } |
| 76 | |
| 77 | static inline uint32_t aes_sub(uint32_t in) |
| 78 | { |
| 79 | uint32x4_t _in = vdupq_n_u32(in); |
| 80 | uint32x4_t v; |
| 81 | uint8x16_t zero = vdupq_n_u8(0); |
| 82 | v = vreinterpretq_u32_u8(vaeseq_u8(zero, vreinterpretq_u8_u32(_in))); |
| 83 | return vgetq_lane_u32(v, 0); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Key expansion, 128-bit case |
| 88 | */ |
| 89 | static void aesce_setkey_enc_128(unsigned char *rk, |
| 90 | const unsigned char *key) |
| 91 | { |
| 92 | uint32_t *rki; |
| 93 | uint32_t *rko; |
| 94 | uint32_t *rk_u32 = (uint32_t *) rk; |
| 95 | memcpy(rk, key, (128 / 8)); |
| 96 | |
| 97 | for (size_t i = 0; i < sizeof(rcon); i++) { |
| 98 | rki = rk_u32 + i * (128 / 32); |
| 99 | rko = rki + (128 / 32); |
| 100 | rko[0] = ror32_8(aes_sub(rki[(128 / 32) - 1])) ^ rcon[i] ^ rki[0]; |
| 101 | rko[1] = rko[0] ^ rki[1]; |
| 102 | rko[2] = rko[1] ^ rki[2]; |
| 103 | rko[3] = rko[2] ^ rki[3]; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Key expansion, 192-bit case |
| 109 | */ |
| 110 | static void aesce_setkey_enc_192(unsigned char *rk, |
| 111 | const unsigned char *key) |
| 112 | { |
| 113 | uint32_t *rki; |
| 114 | uint32_t *rko; |
| 115 | uint32_t *rk_u32 = (uint32_t *) rk; |
| 116 | memcpy(rk, key, (192 / 8)); |
| 117 | |
| 118 | for (size_t i = 0; i < 8; i++) { |
| 119 | rki = rk_u32 + i * (192 / 32); |
| 120 | rko = rki + (192 / 32); |
| 121 | rko[0] = ror32_8(aes_sub(rki[(192 / 32) - 1])) ^ rcon[i] ^ rki[0]; |
| 122 | rko[1] = rko[0] ^ rki[1]; |
| 123 | rko[2] = rko[1] ^ rki[2]; |
| 124 | rko[3] = rko[2] ^ rki[3]; |
| 125 | if (i < 7) { |
| 126 | rko[4] = rko[3] ^ rki[4]; |
| 127 | rko[5] = rko[4] ^ rki[5]; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Key expansion, 256-bit case |
| 134 | */ |
| 135 | static void aesce_setkey_enc_256(unsigned char *rk, |
| 136 | const unsigned char *key) |
| 137 | { |
| 138 | uint32_t *rki; |
| 139 | uint32_t *rko; |
| 140 | uint32_t *rk_u32 = (uint32_t *) rk; |
| 141 | memcpy(rk, key, (256 / 8)); |
| 142 | |
| 143 | for (size_t i = 0; i < 7; i++) { |
| 144 | rki = rk_u32 + i * (256 / 32); |
| 145 | rko = rki + (256 / 32); |
| 146 | rko[0] = ror32_8(aes_sub(rki[(256 / 32) - 1])) ^ rcon[i] ^ rki[0]; |
| 147 | rko[1] = rko[0] ^ rki[1]; |
| 148 | rko[2] = rko[1] ^ rki[2]; |
| 149 | rko[3] = rko[2] ^ rki[3]; |
| 150 | if (i < 6) { |
| 151 | rko[4] = aes_sub(rko[3]) ^ rki[4]; |
| 152 | rko[5] = rko[4] ^ rki[5]; |
| 153 | rko[6] = rko[5] ^ rki[6]; |
| 154 | rko[7] = rko[6] ^ rki[7]; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Key expansion, wrapper |
| 161 | */ |
| 162 | int mbedtls_aesce_setkey_enc(unsigned char *rk, |
| 163 | const unsigned char *key, |
| 164 | size_t bits) |
| 165 | { |
| 166 | switch (bits) { |
| 167 | case 128: aesce_setkey_enc_128(rk, key); break; |
| 168 | case 192: aesce_setkey_enc_192(rk, key); break; |
| 169 | case 256: aesce_setkey_enc_256(rk, key); break; |
| 170 | default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 176 | #endif /* MBEDTLS_HAVE_ARM64 */ |
| 177 | |
| 178 | #endif /* MBEDTLS_AESCE_C */ |