aegis128-neon-inner.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019 Linaro, Ltd. <[email protected]>
  4. */
  5. #ifdef CONFIG_ARM64
  6. #include <asm/neon-intrinsics.h>
  7. #define AES_ROUND "aese %0.16b, %1.16b \n\t aesmc %0.16b, %0.16b"
  8. #else
  9. #include <arm_neon.h>
  10. #define AES_ROUND "aese.8 %q0, %q1 \n\t aesmc.8 %q0, %q0"
  11. #endif
  12. #define AEGIS_BLOCK_SIZE 16
  13. #include <stddef.h>
  14. extern int aegis128_have_aes_insn;
  15. void *memcpy(void *dest, const void *src, size_t n);
  16. struct aegis128_state {
  17. uint8x16_t v[5];
  18. };
  19. extern const uint8_t crypto_aes_sbox[];
  20. static struct aegis128_state aegis128_load_state_neon(const void *state)
  21. {
  22. return (struct aegis128_state){ {
  23. vld1q_u8(state),
  24. vld1q_u8(state + 16),
  25. vld1q_u8(state + 32),
  26. vld1q_u8(state + 48),
  27. vld1q_u8(state + 64)
  28. } };
  29. }
  30. static void aegis128_save_state_neon(struct aegis128_state st, void *state)
  31. {
  32. vst1q_u8(state, st.v[0]);
  33. vst1q_u8(state + 16, st.v[1]);
  34. vst1q_u8(state + 32, st.v[2]);
  35. vst1q_u8(state + 48, st.v[3]);
  36. vst1q_u8(state + 64, st.v[4]);
  37. }
  38. static inline __attribute__((always_inline))
  39. uint8x16_t aegis_aes_round(uint8x16_t w)
  40. {
  41. uint8x16_t z = {};
  42. #ifdef CONFIG_ARM64
  43. if (!__builtin_expect(aegis128_have_aes_insn, 1)) {
  44. static const uint8_t shift_rows[] = {
  45. 0x0, 0x5, 0xa, 0xf, 0x4, 0x9, 0xe, 0x3,
  46. 0x8, 0xd, 0x2, 0x7, 0xc, 0x1, 0x6, 0xb,
  47. };
  48. static const uint8_t ror32by8[] = {
  49. 0x1, 0x2, 0x3, 0x0, 0x5, 0x6, 0x7, 0x4,
  50. 0x9, 0xa, 0xb, 0x8, 0xd, 0xe, 0xf, 0xc,
  51. };
  52. uint8x16_t v;
  53. // shift rows
  54. w = vqtbl1q_u8(w, vld1q_u8(shift_rows));
  55. // sub bytes
  56. #ifndef CONFIG_CC_IS_GCC
  57. v = vqtbl4q_u8(vld1q_u8_x4(crypto_aes_sbox), w);
  58. v = vqtbx4q_u8(v, vld1q_u8_x4(crypto_aes_sbox + 0x40), w - 0x40);
  59. v = vqtbx4q_u8(v, vld1q_u8_x4(crypto_aes_sbox + 0x80), w - 0x80);
  60. v = vqtbx4q_u8(v, vld1q_u8_x4(crypto_aes_sbox + 0xc0), w - 0xc0);
  61. #else
  62. asm("tbl %0.16b, {v16.16b-v19.16b}, %1.16b" : "=w"(v) : "w"(w));
  63. w -= 0x40;
  64. asm("tbx %0.16b, {v20.16b-v23.16b}, %1.16b" : "+w"(v) : "w"(w));
  65. w -= 0x40;
  66. asm("tbx %0.16b, {v24.16b-v27.16b}, %1.16b" : "+w"(v) : "w"(w));
  67. w -= 0x40;
  68. asm("tbx %0.16b, {v28.16b-v31.16b}, %1.16b" : "+w"(v) : "w"(w));
  69. #endif
  70. // mix columns
  71. w = (v << 1) ^ (uint8x16_t)(((int8x16_t)v >> 7) & 0x1b);
  72. w ^= (uint8x16_t)vrev32q_u16((uint16x8_t)v);
  73. w ^= vqtbl1q_u8(v ^ w, vld1q_u8(ror32by8));
  74. return w;
  75. }
  76. #endif
  77. /*
  78. * We use inline asm here instead of the vaeseq_u8/vaesmcq_u8 intrinsics
  79. * to force the compiler to issue the aese/aesmc instructions in pairs.
  80. * This is much faster on many cores, where the instruction pair can
  81. * execute in a single cycle.
  82. */
  83. asm(AES_ROUND : "+w"(w) : "w"(z));
  84. return w;
  85. }
  86. static inline __attribute__((always_inline))
  87. struct aegis128_state aegis128_update_neon(struct aegis128_state st,
  88. uint8x16_t m)
  89. {
  90. m ^= aegis_aes_round(st.v[4]);
  91. st.v[4] ^= aegis_aes_round(st.v[3]);
  92. st.v[3] ^= aegis_aes_round(st.v[2]);
  93. st.v[2] ^= aegis_aes_round(st.v[1]);
  94. st.v[1] ^= aegis_aes_round(st.v[0]);
  95. st.v[0] ^= m;
  96. return st;
  97. }
  98. static inline __attribute__((always_inline))
  99. void preload_sbox(void)
  100. {
  101. if (!IS_ENABLED(CONFIG_ARM64) ||
  102. !IS_ENABLED(CONFIG_CC_IS_GCC) ||
  103. __builtin_expect(aegis128_have_aes_insn, 1))
  104. return;
  105. asm("ld1 {v16.16b-v19.16b}, [%0], #64 \n\t"
  106. "ld1 {v20.16b-v23.16b}, [%0], #64 \n\t"
  107. "ld1 {v24.16b-v27.16b}, [%0], #64 \n\t"
  108. "ld1 {v28.16b-v31.16b}, [%0] \n\t"
  109. :: "r"(crypto_aes_sbox));
  110. }
  111. void crypto_aegis128_init_neon(void *state, const void *key, const void *iv)
  112. {
  113. static const uint8_t const0[] = {
  114. 0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d,
  115. 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62,
  116. };
  117. static const uint8_t const1[] = {
  118. 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1,
  119. 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd,
  120. };
  121. uint8x16_t k = vld1q_u8(key);
  122. uint8x16_t kiv = k ^ vld1q_u8(iv);
  123. struct aegis128_state st = {{
  124. kiv,
  125. vld1q_u8(const1),
  126. vld1q_u8(const0),
  127. k ^ vld1q_u8(const0),
  128. k ^ vld1q_u8(const1),
  129. }};
  130. int i;
  131. preload_sbox();
  132. for (i = 0; i < 5; i++) {
  133. st = aegis128_update_neon(st, k);
  134. st = aegis128_update_neon(st, kiv);
  135. }
  136. aegis128_save_state_neon(st, state);
  137. }
  138. void crypto_aegis128_update_neon(void *state, const void *msg)
  139. {
  140. struct aegis128_state st = aegis128_load_state_neon(state);
  141. preload_sbox();
  142. st = aegis128_update_neon(st, vld1q_u8(msg));
  143. aegis128_save_state_neon(st, state);
  144. }
  145. #ifdef CONFIG_ARM
  146. /*
  147. * AArch32 does not provide these intrinsics natively because it does not
  148. * implement the underlying instructions. AArch32 only provides 64-bit
  149. * wide vtbl.8/vtbx.8 instruction, so use those instead.
  150. */
  151. static uint8x16_t vqtbl1q_u8(uint8x16_t a, uint8x16_t b)
  152. {
  153. union {
  154. uint8x16_t val;
  155. uint8x8x2_t pair;
  156. } __a = { a };
  157. return vcombine_u8(vtbl2_u8(__a.pair, vget_low_u8(b)),
  158. vtbl2_u8(__a.pair, vget_high_u8(b)));
  159. }
  160. static uint8x16_t vqtbx1q_u8(uint8x16_t v, uint8x16_t a, uint8x16_t b)
  161. {
  162. union {
  163. uint8x16_t val;
  164. uint8x8x2_t pair;
  165. } __a = { a };
  166. return vcombine_u8(vtbx2_u8(vget_low_u8(v), __a.pair, vget_low_u8(b)),
  167. vtbx2_u8(vget_high_u8(v), __a.pair, vget_high_u8(b)));
  168. }
  169. static int8_t vminvq_s8(int8x16_t v)
  170. {
  171. int8x8_t s = vpmin_s8(vget_low_s8(v), vget_high_s8(v));
  172. s = vpmin_s8(s, s);
  173. s = vpmin_s8(s, s);
  174. s = vpmin_s8(s, s);
  175. return vget_lane_s8(s, 0);
  176. }
  177. #endif
  178. static const uint8_t permute[] __aligned(64) = {
  179. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  180. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  181. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  182. };
  183. void crypto_aegis128_encrypt_chunk_neon(void *state, void *dst, const void *src,
  184. unsigned int size)
  185. {
  186. struct aegis128_state st = aegis128_load_state_neon(state);
  187. const int short_input = size < AEGIS_BLOCK_SIZE;
  188. uint8x16_t msg;
  189. preload_sbox();
  190. while (size >= AEGIS_BLOCK_SIZE) {
  191. uint8x16_t s = st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
  192. msg = vld1q_u8(src);
  193. st = aegis128_update_neon(st, msg);
  194. msg ^= s;
  195. vst1q_u8(dst, msg);
  196. size -= AEGIS_BLOCK_SIZE;
  197. src += AEGIS_BLOCK_SIZE;
  198. dst += AEGIS_BLOCK_SIZE;
  199. }
  200. if (size > 0) {
  201. uint8x16_t s = st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
  202. uint8_t buf[AEGIS_BLOCK_SIZE];
  203. const void *in = src;
  204. void *out = dst;
  205. uint8x16_t m;
  206. if (__builtin_expect(short_input, 0))
  207. in = out = memcpy(buf + AEGIS_BLOCK_SIZE - size, src, size);
  208. m = vqtbl1q_u8(vld1q_u8(in + size - AEGIS_BLOCK_SIZE),
  209. vld1q_u8(permute + 32 - size));
  210. st = aegis128_update_neon(st, m);
  211. vst1q_u8(out + size - AEGIS_BLOCK_SIZE,
  212. vqtbl1q_u8(m ^ s, vld1q_u8(permute + size)));
  213. if (__builtin_expect(short_input, 0))
  214. memcpy(dst, out, size);
  215. else
  216. vst1q_u8(out - AEGIS_BLOCK_SIZE, msg);
  217. }
  218. aegis128_save_state_neon(st, state);
  219. }
  220. void crypto_aegis128_decrypt_chunk_neon(void *state, void *dst, const void *src,
  221. unsigned int size)
  222. {
  223. struct aegis128_state st = aegis128_load_state_neon(state);
  224. const int short_input = size < AEGIS_BLOCK_SIZE;
  225. uint8x16_t msg;
  226. preload_sbox();
  227. while (size >= AEGIS_BLOCK_SIZE) {
  228. msg = vld1q_u8(src) ^ st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
  229. st = aegis128_update_neon(st, msg);
  230. vst1q_u8(dst, msg);
  231. size -= AEGIS_BLOCK_SIZE;
  232. src += AEGIS_BLOCK_SIZE;
  233. dst += AEGIS_BLOCK_SIZE;
  234. }
  235. if (size > 0) {
  236. uint8x16_t s = st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
  237. uint8_t buf[AEGIS_BLOCK_SIZE];
  238. const void *in = src;
  239. void *out = dst;
  240. uint8x16_t m;
  241. if (__builtin_expect(short_input, 0))
  242. in = out = memcpy(buf + AEGIS_BLOCK_SIZE - size, src, size);
  243. m = s ^ vqtbx1q_u8(s, vld1q_u8(in + size - AEGIS_BLOCK_SIZE),
  244. vld1q_u8(permute + 32 - size));
  245. st = aegis128_update_neon(st, m);
  246. vst1q_u8(out + size - AEGIS_BLOCK_SIZE,
  247. vqtbl1q_u8(m, vld1q_u8(permute + size)));
  248. if (__builtin_expect(short_input, 0))
  249. memcpy(dst, out, size);
  250. else
  251. vst1q_u8(out - AEGIS_BLOCK_SIZE, msg);
  252. }
  253. aegis128_save_state_neon(st, state);
  254. }
  255. int crypto_aegis128_final_neon(void *state, void *tag_xor,
  256. unsigned int assoclen,
  257. unsigned int cryptlen,
  258. unsigned int authsize)
  259. {
  260. struct aegis128_state st = aegis128_load_state_neon(state);
  261. uint8x16_t v;
  262. int i;
  263. preload_sbox();
  264. v = st.v[3] ^ (uint8x16_t)vcombine_u64(vmov_n_u64(8ULL * assoclen),
  265. vmov_n_u64(8ULL * cryptlen));
  266. for (i = 0; i < 7; i++)
  267. st = aegis128_update_neon(st, v);
  268. v = st.v[0] ^ st.v[1] ^ st.v[2] ^ st.v[3] ^ st.v[4];
  269. if (authsize > 0) {
  270. v = vqtbl1q_u8(~vceqq_u8(v, vld1q_u8(tag_xor)),
  271. vld1q_u8(permute + authsize));
  272. return vminvq_s8((int8x16_t)v);
  273. }
  274. vst1q_u8(tag_xor, v);
  275. return 0;
  276. }