aesce.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Armv8-A Cryptographic Extension support functions for Aarch64
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #if defined(__clang__) && (__clang_major__ >= 4)
  8. /* Ideally, we would simply use MBEDTLS_ARCH_IS_ARMV8_A in the following #if,
  9. * but that is defined by build_info.h, and we need this block to happen first. */
  10. #if defined(__ARM_ARCH)
  11. #if __ARM_ARCH >= 8
  12. #define MBEDTLS_AESCE_ARCH_IS_ARMV8_A
  13. #endif
  14. #endif
  15. #if defined(MBEDTLS_AESCE_ARCH_IS_ARMV8_A) && !defined(__ARM_FEATURE_CRYPTO)
  16. /* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
  17. *
  18. * The intrinsic declaration are guarded by predefined ACLE macros in clang:
  19. * these are normally only enabled by the -march option on the command line.
  20. * By defining the macros ourselves we gain access to those declarations without
  21. * requiring -march on the command line.
  22. *
  23. * `arm_neon.h` is included by common.h, so we put these defines
  24. * at the top of this file, before any includes.
  25. */
  26. #define __ARM_FEATURE_CRYPTO 1
  27. /* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions
  28. *
  29. * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it
  30. * for older compilers.
  31. */
  32. #define __ARM_FEATURE_AES 1
  33. #define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG
  34. #endif
  35. #endif /* defined(__clang__) && (__clang_major__ >= 4) */
  36. #include <string.h>
  37. #include "common.h"
  38. #if defined(MBEDTLS_AESCE_C)
  39. #include "aesce.h"
  40. #if defined(MBEDTLS_AESCE_HAVE_CODE)
  41. /* Compiler version checks. */
  42. #if defined(__clang__)
  43. # if defined(MBEDTLS_ARCH_IS_ARM32) && (__clang_major__ < 11)
  44. # error "Minimum version of Clang for MBEDTLS_AESCE_C on 32-bit Arm or Thumb is 11.0."
  45. # elif defined(MBEDTLS_ARCH_IS_ARM64) && (__clang_major__ < 4)
  46. # error "Minimum version of Clang for MBEDTLS_AESCE_C on aarch64 is 4.0."
  47. # endif
  48. #elif defined(__GNUC__)
  49. # if __GNUC__ < 6
  50. # error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0."
  51. # endif
  52. #elif defined(_MSC_VER)
  53. /* TODO: We haven't verified MSVC from 1920 to 1928. If someone verified that,
  54. * please update this and document of `MBEDTLS_AESCE_C` in
  55. * `mbedtls_config.h`. */
  56. # if _MSC_VER < 1929
  57. # error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2."
  58. # endif
  59. #elif defined(__ARMCC_VERSION)
  60. # if defined(MBEDTLS_ARCH_IS_ARM32) && (__ARMCC_VERSION < 6200002)
  61. /* TODO: We haven't verified armclang for 32-bit Arm/Thumb prior to 6.20.
  62. * If someone verified that, please update this and document of
  63. * `MBEDTLS_AESCE_C` in `mbedtls_config.h`. */
  64. # error "Minimum version of armclang for MBEDTLS_AESCE_C on 32-bit Arm is 6.20."
  65. # elif defined(MBEDTLS_ARCH_IS_ARM64) && (__ARMCC_VERSION < 6060000)
  66. # error "Minimum version of armclang for MBEDTLS_AESCE_C on aarch64 is 6.6."
  67. # endif
  68. #endif
  69. #if !(defined(__ARM_FEATURE_CRYPTO) || defined(__ARM_FEATURE_AES)) || \
  70. defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
  71. # if defined(__ARMCOMPILER_VERSION)
  72. # if __ARMCOMPILER_VERSION <= 6090000
  73. # error "Must use minimum -march=armv8-a+crypto for MBEDTLS_AESCE_C"
  74. # else
  75. # pragma clang attribute push (__attribute__((target("aes"))), apply_to=function)
  76. # define MBEDTLS_POP_TARGET_PRAGMA
  77. # endif
  78. # elif defined(__clang__)
  79. # pragma clang attribute push (__attribute__((target("aes"))), apply_to=function)
  80. # define MBEDTLS_POP_TARGET_PRAGMA
  81. # elif defined(__GNUC__)
  82. # pragma GCC push_options
  83. # pragma GCC target ("+crypto")
  84. # define MBEDTLS_POP_TARGET_PRAGMA
  85. # elif defined(_MSC_VER)
  86. # error "Required feature(__ARM_FEATURE_AES) is not enabled."
  87. # endif
  88. #endif /* !(__ARM_FEATURE_CRYPTO || __ARM_FEATURE_AES) ||
  89. MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */
  90. #if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
  91. #include <sys/auxv.h>
  92. #if !defined(HWCAP_NEON)
  93. #define HWCAP_NEON (1 << 12)
  94. #endif
  95. #if !defined(HWCAP2_AES)
  96. #define HWCAP2_AES (1 << 0)
  97. #endif
  98. #if !defined(HWCAP_AES)
  99. #define HWCAP_AES (1 << 3)
  100. #endif
  101. #if !defined(HWCAP_ASIMD)
  102. #define HWCAP_ASIMD (1 << 1)
  103. #endif
  104. signed char mbedtls_aesce_has_support_result = -1;
  105. #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
  106. /*
  107. * AES instruction support detection routine
  108. */
  109. int mbedtls_aesce_has_support_impl(void)
  110. {
  111. /* To avoid many calls to getauxval, cache the result. This is
  112. * thread-safe, because we store the result in a char so cannot
  113. * be vulnerable to non-atomic updates.
  114. * It is possible that we could end up setting result more than
  115. * once, but that is harmless.
  116. */
  117. if (mbedtls_aesce_has_support_result == -1) {
  118. #if defined(MBEDTLS_ARCH_IS_ARM32)
  119. unsigned long auxval = getauxval(AT_HWCAP);
  120. unsigned long auxval2 = getauxval(AT_HWCAP2);
  121. if (((auxval & HWCAP_NEON) == HWCAP_NEON) &&
  122. ((auxval2 & HWCAP2_AES) == HWCAP2_AES)) {
  123. mbedtls_aesce_has_support_result = 1;
  124. } else {
  125. mbedtls_aesce_has_support_result = 0;
  126. }
  127. #else
  128. unsigned long auxval = getauxval(AT_HWCAP);
  129. if ((auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
  130. (HWCAP_ASIMD | HWCAP_AES)) {
  131. mbedtls_aesce_has_support_result = 1;
  132. } else {
  133. mbedtls_aesce_has_support_result = 0;
  134. }
  135. #endif
  136. }
  137. return mbedtls_aesce_has_support_result;
  138. }
  139. #endif
  140. #endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
  141. /* Single round of AESCE encryption */
  142. #define AESCE_ENCRYPT_ROUND \
  143. block = vaeseq_u8(block, vld1q_u8(keys)); \
  144. block = vaesmcq_u8(block); \
  145. keys += 16
  146. /* Two rounds of AESCE encryption */
  147. #define AESCE_ENCRYPT_ROUND_X2 AESCE_ENCRYPT_ROUND; AESCE_ENCRYPT_ROUND
  148. MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
  149. static uint8x16_t aesce_encrypt_block(uint8x16_t block,
  150. unsigned char *keys,
  151. int rounds)
  152. {
  153. /* 10, 12 or 14 rounds. Unroll loop. */
  154. if (rounds == 10) {
  155. goto rounds_10;
  156. }
  157. if (rounds == 12) {
  158. goto rounds_12;
  159. }
  160. AESCE_ENCRYPT_ROUND_X2;
  161. rounds_12:
  162. AESCE_ENCRYPT_ROUND_X2;
  163. rounds_10:
  164. AESCE_ENCRYPT_ROUND_X2;
  165. AESCE_ENCRYPT_ROUND_X2;
  166. AESCE_ENCRYPT_ROUND_X2;
  167. AESCE_ENCRYPT_ROUND_X2;
  168. AESCE_ENCRYPT_ROUND;
  169. /* AES AddRoundKey for the previous round.
  170. * SubBytes, ShiftRows for the final round. */
  171. block = vaeseq_u8(block, vld1q_u8(keys));
  172. keys += 16;
  173. /* Final round: no MixColumns */
  174. /* Final AddRoundKey */
  175. block = veorq_u8(block, vld1q_u8(keys));
  176. return block;
  177. }
  178. /* Single round of AESCE decryption
  179. *
  180. * AES AddRoundKey, SubBytes, ShiftRows
  181. *
  182. * block = vaesdq_u8(block, vld1q_u8(keys));
  183. *
  184. * AES inverse MixColumns for the next round.
  185. *
  186. * This means that we switch the order of the inverse AddRoundKey and
  187. * inverse MixColumns operations. We have to do this as AddRoundKey is
  188. * done in an atomic instruction together with the inverses of SubBytes
  189. * and ShiftRows.
  190. *
  191. * It works because MixColumns is a linear operation over GF(2^8) and
  192. * AddRoundKey is an exclusive or, which is equivalent to addition over
  193. * GF(2^8). (The inverse of MixColumns needs to be applied to the
  194. * affected round keys separately which has been done when the
  195. * decryption round keys were calculated.)
  196. *
  197. * block = vaesimcq_u8(block);
  198. */
  199. #define AESCE_DECRYPT_ROUND \
  200. block = vaesdq_u8(block, vld1q_u8(keys)); \
  201. block = vaesimcq_u8(block); \
  202. keys += 16
  203. /* Two rounds of AESCE decryption */
  204. #define AESCE_DECRYPT_ROUND_X2 AESCE_DECRYPT_ROUND; AESCE_DECRYPT_ROUND
  205. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  206. static uint8x16_t aesce_decrypt_block(uint8x16_t block,
  207. unsigned char *keys,
  208. int rounds)
  209. {
  210. /* 10, 12 or 14 rounds. Unroll loop. */
  211. if (rounds == 10) {
  212. goto rounds_10;
  213. }
  214. if (rounds == 12) {
  215. goto rounds_12;
  216. }
  217. AESCE_DECRYPT_ROUND_X2;
  218. rounds_12:
  219. AESCE_DECRYPT_ROUND_X2;
  220. rounds_10:
  221. AESCE_DECRYPT_ROUND_X2;
  222. AESCE_DECRYPT_ROUND_X2;
  223. AESCE_DECRYPT_ROUND_X2;
  224. AESCE_DECRYPT_ROUND_X2;
  225. AESCE_DECRYPT_ROUND;
  226. /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
  227. * last full round. */
  228. block = vaesdq_u8(block, vld1q_u8(keys));
  229. keys += 16;
  230. /* Inverse AddRoundKey for inverting the initial round key addition. */
  231. block = veorq_u8(block, vld1q_u8(keys));
  232. return block;
  233. }
  234. #endif
  235. /*
  236. * AES-ECB block en(de)cryption
  237. */
  238. int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
  239. int mode,
  240. const unsigned char input[16],
  241. unsigned char output[16])
  242. {
  243. uint8x16_t block = vld1q_u8(&input[0]);
  244. unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
  245. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  246. if (mode == MBEDTLS_AES_DECRYPT) {
  247. block = aesce_decrypt_block(block, keys, ctx->nr);
  248. } else
  249. #else
  250. (void) mode;
  251. #endif
  252. {
  253. block = aesce_encrypt_block(block, keys, ctx->nr);
  254. }
  255. vst1q_u8(&output[0], block);
  256. return 0;
  257. }
  258. /*
  259. * Compute decryption round keys from encryption round keys
  260. */
  261. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  262. void mbedtls_aesce_inverse_key(unsigned char *invkey,
  263. const unsigned char *fwdkey,
  264. int nr)
  265. {
  266. int i, j;
  267. j = nr;
  268. vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
  269. for (i = 1, j--; j > 0; i++, j--) {
  270. vst1q_u8(invkey + i * 16,
  271. vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
  272. }
  273. vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
  274. }
  275. #endif
  276. static inline uint32_t aes_rot_word(uint32_t word)
  277. {
  278. return (word << (32 - 8)) | (word >> 8);
  279. }
  280. static inline uint32_t aes_sub_word(uint32_t in)
  281. {
  282. uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
  283. uint8x16_t zero = vdupq_n_u8(0);
  284. /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
  285. * the correct result as ShiftRows doesn't change the first row. */
  286. v = vaeseq_u8(zero, v);
  287. return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
  288. }
  289. /*
  290. * Key expansion function
  291. */
  292. static void aesce_setkey_enc(unsigned char *rk,
  293. const unsigned char *key,
  294. const size_t key_bit_length)
  295. {
  296. static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
  297. 0x20, 0x40, 0x80, 0x1b, 0x36 };
  298. /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
  299. * - Section 5, Nr = Nk + 6
  300. * - Section 5.2, the length of round keys is Nb*(Nr+1)
  301. */
  302. const size_t key_len_in_words = key_bit_length / 32; /* Nk */
  303. const size_t round_key_len_in_words = 4; /* Nb */
  304. const size_t rounds_needed = key_len_in_words + 6; /* Nr */
  305. const size_t round_keys_len_in_words =
  306. round_key_len_in_words * (rounds_needed + 1); /* Nb*(Nr+1) */
  307. const uint32_t *rko_end = (uint32_t *) rk + round_keys_len_in_words;
  308. memcpy(rk, key, key_len_in_words * 4);
  309. for (uint32_t *rki = (uint32_t *) rk;
  310. rki + key_len_in_words < rko_end;
  311. rki += key_len_in_words) {
  312. size_t iteration = (size_t) (rki - (uint32_t *) rk) / key_len_in_words;
  313. uint32_t *rko;
  314. rko = rki + key_len_in_words;
  315. rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
  316. rko[0] ^= rcon[iteration] ^ rki[0];
  317. rko[1] = rko[0] ^ rki[1];
  318. rko[2] = rko[1] ^ rki[2];
  319. rko[3] = rko[2] ^ rki[3];
  320. if (rko + key_len_in_words > rko_end) {
  321. /* Do not write overflow words.*/
  322. continue;
  323. }
  324. #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
  325. switch (key_bit_length) {
  326. case 128:
  327. break;
  328. case 192:
  329. rko[4] = rko[3] ^ rki[4];
  330. rko[5] = rko[4] ^ rki[5];
  331. break;
  332. case 256:
  333. rko[4] = aes_sub_word(rko[3]) ^ rki[4];
  334. rko[5] = rko[4] ^ rki[5];
  335. rko[6] = rko[5] ^ rki[6];
  336. rko[7] = rko[6] ^ rki[7];
  337. break;
  338. }
  339. #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
  340. }
  341. }
  342. /*
  343. * Key expansion, wrapper
  344. */
  345. int mbedtls_aesce_setkey_enc(unsigned char *rk,
  346. const unsigned char *key,
  347. size_t bits)
  348. {
  349. switch (bits) {
  350. case 128:
  351. case 192:
  352. case 256:
  353. aesce_setkey_enc(rk, key, bits);
  354. break;
  355. default:
  356. return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
  357. }
  358. return 0;
  359. }
  360. #if defined(MBEDTLS_GCM_C)
  361. #if defined(MBEDTLS_ARCH_IS_ARM32)
  362. #if defined(__clang__)
  363. /* On clang for A32/T32, work around some missing intrinsics and types which are listed in
  364. * [ACLE](https://arm-software.github.io/acle/neon_intrinsics/advsimd.html#polynomial-1)
  365. * These are only required for GCM.
  366. */
  367. #define vreinterpretq_u64_p64(a) ((uint64x2_t) a)
  368. typedef uint8x16_t poly128_t;
  369. static inline poly128_t vmull_p64(poly64_t a, poly64_t b)
  370. {
  371. poly128_t r;
  372. asm ("vmull.p64 %[r], %[a], %[b]" : [r] "=w" (r) : [a] "w" (a), [b] "w" (b) :);
  373. return r;
  374. }
  375. /* This is set to cause some more missing intrinsics to be defined below */
  376. #define COMMON_MISSING_INTRINSICS
  377. static inline poly128_t vmull_high_p64(poly64x2_t a, poly64x2_t b)
  378. {
  379. return vmull_p64((poly64_t) (vget_high_u64((uint64x2_t) a)),
  380. (poly64_t) (vget_high_u64((uint64x2_t) b)));
  381. }
  382. #endif /* defined(__clang__) */
  383. static inline uint8x16_t vrbitq_u8(uint8x16_t x)
  384. {
  385. /* There is no vrbitq_u8 instruction in A32/T32, so provide
  386. * an equivalent non-Neon implementation. Reverse bit order in each
  387. * byte with 4x rbit, rev. */
  388. asm ("ldm %[p], { r2-r5 } \n\t"
  389. "rbit r2, r2 \n\t"
  390. "rev r2, r2 \n\t"
  391. "rbit r3, r3 \n\t"
  392. "rev r3, r3 \n\t"
  393. "rbit r4, r4 \n\t"
  394. "rev r4, r4 \n\t"
  395. "rbit r5, r5 \n\t"
  396. "rev r5, r5 \n\t"
  397. "stm %[p], { r2-r5 } \n\t"
  398. :
  399. /* Output: 16 bytes of memory pointed to by &x */
  400. "+m" (*(uint8_t(*)[16]) &x)
  401. :
  402. [p] "r" (&x)
  403. :
  404. "r2", "r3", "r4", "r5"
  405. );
  406. return x;
  407. }
  408. #endif /* defined(MBEDTLS_ARCH_IS_ARM32) */
  409. #if defined(MBEDTLS_COMPILER_IS_GCC) && __GNUC__ == 5
  410. /* Some intrinsics are not available for GCC 5.X. */
  411. #define COMMON_MISSING_INTRINSICS
  412. #endif /* MBEDTLS_COMPILER_IS_GCC && __GNUC__ == 5 */
  413. #if defined(COMMON_MISSING_INTRINSICS)
  414. /* Missing intrinsics common to both GCC 5, and Clang on 32-bit */
  415. #define vreinterpretq_p64_u8(a) ((poly64x2_t) a)
  416. #define vreinterpretq_u8_p128(a) ((uint8x16_t) a)
  417. static inline poly64x1_t vget_low_p64(poly64x2_t a)
  418. {
  419. uint64x1_t r = vget_low_u64(vreinterpretq_u64_p64(a));
  420. return (poly64x1_t) r;
  421. }
  422. #endif /* COMMON_MISSING_INTRINSICS */
  423. /* vmull_p64/vmull_high_p64 wrappers.
  424. *
  425. * Older compilers miss some intrinsic functions for `poly*_t`. We use
  426. * uint8x16_t and uint8x16x3_t as input/output parameters.
  427. */
  428. #if defined(MBEDTLS_COMPILER_IS_GCC)
  429. /* GCC reports incompatible type error without cast. GCC think poly64_t and
  430. * poly64x1_t are different, that is different with MSVC and Clang. */
  431. #define MBEDTLS_VMULL_P64(a, b) vmull_p64((poly64_t) a, (poly64_t) b)
  432. #else
  433. /* MSVC reports `error C2440: 'type cast'` with cast. Clang does not report
  434. * error with/without cast. And I think poly64_t and poly64x1_t are same, no
  435. * cast for clang also. */
  436. #define MBEDTLS_VMULL_P64(a, b) vmull_p64(a, b)
  437. #endif /* MBEDTLS_COMPILER_IS_GCC */
  438. static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b)
  439. {
  440. return vreinterpretq_u8_p128(
  441. MBEDTLS_VMULL_P64(
  442. (poly64_t) vget_low_p64(vreinterpretq_p64_u8(a)),
  443. (poly64_t) vget_low_p64(vreinterpretq_p64_u8(b))
  444. ));
  445. }
  446. static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b)
  447. {
  448. return vreinterpretq_u8_p128(
  449. vmull_high_p64(vreinterpretq_p64_u8(a),
  450. vreinterpretq_p64_u8(b)));
  451. }
  452. /* GHASH does 128b polynomial multiplication on block in GF(2^128) defined by
  453. * `x^128 + x^7 + x^2 + x + 1`.
  454. *
  455. * Arm64 only has 64b->128b polynomial multipliers, we need to do 4 64b
  456. * multiplies to generate a 128b.
  457. *
  458. * `poly_mult_128` executes polynomial multiplication and outputs 256b that
  459. * represented by 3 128b due to code size optimization.
  460. *
  461. * Output layout:
  462. * | | | |
  463. * |------------|-------------|-------------|
  464. * | ret.val[0] | h3:h2:00:00 | high 128b |
  465. * | ret.val[1] | :m2:m1:00 | middle 128b |
  466. * | ret.val[2] | : :l1:l0 | low 128b |
  467. */
  468. static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b)
  469. {
  470. uint8x16x3_t ret;
  471. uint8x16_t h, m, l; /* retval high/middle/low */
  472. uint8x16_t c, d, e;
  473. h = pmull_high(a, b); /* h3:h2:00:00 = a1*b1 */
  474. l = pmull_low(a, b); /* : :l1:l0 = a0*b0 */
  475. c = vextq_u8(b, b, 8); /* :c1:c0 = b0:b1 */
  476. d = pmull_high(a, c); /* :d2:d1:00 = a1*b0 */
  477. e = pmull_low(a, c); /* :e2:e1:00 = a0*b1 */
  478. m = veorq_u8(d, e); /* :m2:m1:00 = d + e */
  479. ret.val[0] = h;
  480. ret.val[1] = m;
  481. ret.val[2] = l;
  482. return ret;
  483. }
  484. /*
  485. * Modulo reduction.
  486. *
  487. * See: https://www.researchgate.net/publication/285612706_Implementing_GCM_on_ARMv8
  488. *
  489. * Section 4.3
  490. *
  491. * Modular reduction is slightly more complex. Write the GCM modulus as f(z) =
  492. * z^128 +r(z), where r(z) = z^7+z^2+z+ 1. The well known approach is to
  493. * consider that z^128 ≡r(z) (mod z^128 +r(z)), allowing us to write the 256-bit
  494. * operand to be reduced as a(z) = h(z)z^128 +l(z)≡h(z)r(z) + l(z). That is, we
  495. * simply multiply the higher part of the operand by r(z) and add it to l(z). If
  496. * the result is still larger than 128 bits, we reduce again.
  497. */
  498. static inline uint8x16_t poly_mult_reduce(uint8x16x3_t input)
  499. {
  500. uint8x16_t const ZERO = vdupq_n_u8(0);
  501. uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87));
  502. #if defined(__GNUC__)
  503. /* use 'asm' as an optimisation barrier to prevent loading MODULO from
  504. * memory. It is for GNUC compatible compilers.
  505. */
  506. asm volatile ("" : "+w" (r));
  507. #endif
  508. uint8x16_t const MODULO = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8));
  509. uint8x16_t h, m, l; /* input high/middle/low 128b */
  510. uint8x16_t c, d, e, f, g, n, o;
  511. h = input.val[0]; /* h3:h2:00:00 */
  512. m = input.val[1]; /* :m2:m1:00 */
  513. l = input.val[2]; /* : :l1:l0 */
  514. c = pmull_high(h, MODULO); /* :c2:c1:00 = reduction of h3 */
  515. d = pmull_low(h, MODULO); /* : :d1:d0 = reduction of h2 */
  516. e = veorq_u8(c, m); /* :e2:e1:00 = m2:m1:00 + c2:c1:00 */
  517. f = pmull_high(e, MODULO); /* : :f1:f0 = reduction of e2 */
  518. g = vextq_u8(ZERO, e, 8); /* : :g1:00 = e1:00 */
  519. n = veorq_u8(d, l); /* : :n1:n0 = d1:d0 + l1:l0 */
  520. o = veorq_u8(n, f); /* o1:o0 = f1:f0 + n1:n0 */
  521. return veorq_u8(o, g); /* = o1:o0 + g1:00 */
  522. }
  523. /*
  524. * GCM multiplication: c = a times b in GF(2^128)
  525. */
  526. void mbedtls_aesce_gcm_mult(unsigned char c[16],
  527. const unsigned char a[16],
  528. const unsigned char b[16])
  529. {
  530. uint8x16_t va, vb, vc;
  531. va = vrbitq_u8(vld1q_u8(&a[0]));
  532. vb = vrbitq_u8(vld1q_u8(&b[0]));
  533. vc = vrbitq_u8(poly_mult_reduce(poly_mult_128(va, vb)));
  534. vst1q_u8(&c[0], vc);
  535. }
  536. #endif /* MBEDTLS_GCM_C */
  537. #if defined(MBEDTLS_POP_TARGET_PRAGMA)
  538. #if defined(__clang__)
  539. #pragma clang attribute pop
  540. #elif defined(__GNUC__)
  541. #pragma GCC pop_options
  542. #endif
  543. #undef MBEDTLS_POP_TARGET_PRAGMA
  544. #endif
  545. #endif /* MBEDTLS_AESCE_HAVE_CODE */
  546. #endif /* MBEDTLS_AESCE_C */