You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chacha_krovetz.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * This code is based on Ted Krovetz's ChaCha implementation; details below.
  3. *
  4. * Note that I've ripped out all of the code that wasn't suitable for doing
  5. * block-oriented operation, all (residual) support for 128-bit ChaCha keys,
  6. * all (partial) support for counter values over 32 bits, the ability to xor
  7. * the stream with a plaintext, and so on.
  8. *
  9. * Future versions of this might remove bigendian conversions too. DO NOT use
  10. * this code for your stream cipher: go back to the original source. (I got
  11. * this copy from SUPERCOP).
  12. */
  13. /* Chacha implementation for 16-byte vectors by Ted Krovetz (ted@krovetz.net).
  14. * Assumes 32-bit int, 64-bit long long. Public domain. Modified: 2012.07.26.
  15. * Chacha is an improvement on the stream cipher Salsa, described at
  16. * http://cr.yp.to/papers.html#chacha
  17. */
  18. #include <string.h>
  19. #include <assert.h>
  20. #include "ottery-internal.h"
  21. /* Architecture-neutral way to specify 16-byte vector of ints */
  22. typedef unsigned vec __attribute__ ((vector_size (16)));
  23. /* This implementation is designed for Neon, SSE and AltiVec machines. The
  24. * following specify how to do certain vector operations efficiently on
  25. * each architecture, using intrinsics.
  26. * This implementation supports parallel processing of multiple blocks,
  27. * including potentially using general-purpose registers.
  28. */
  29. #if __ARM_NEON__
  30. #include <arm_neon.h>
  31. #define GPR_TOO 1
  32. #define VBPI 2
  33. #define ONE (vec)vsetq_lane_u32(1,vdupq_n_u32(0),0)
  34. #define NONCE(ctr,p) (vec)vcombine_u32(vcreate_u32(ctr),vcreate_u32(*(uint64_t *)p))
  35. #define ROTV1(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,1)
  36. #define ROTV2(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,2)
  37. #define ROTV3(x) (vec)vextq_u32((uint32x4_t)x,(uint32x4_t)x,3)
  38. #define ROTW16(x) (vec)vrev32q_u16((uint16x8_t)x)
  39. #if __clang__
  40. #define ROTW7(x) (x << ((vec){ 7, 7, 7, 7})) ^ (x >> ((vec){25,25,25,25}))
  41. #define ROTW8(x) (x << ((vec){ 8, 8, 8, 8})) ^ (x >> ((vec){24,24,24,24}))
  42. #define ROTW12(x) (x << ((vec){12,12,12,12})) ^ (x >> ((vec){20,20,20,20}))
  43. #else
  44. #define ROTW7(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,7),(uint32x4_t)x,25)
  45. #define ROTW8(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,8),(uint32x4_t)x,24)
  46. #define ROTW12(x) (vec)vsriq_n_u32(vshlq_n_u32((uint32x4_t)x,12),(uint32x4_t)x,20)
  47. #endif
  48. #elif __ALTIVEC__
  49. #include <altivec.h>
  50. #define GPR_TOO 1
  51. #define VBPI 3
  52. #define ONE ((vec){1,0,0,0})
  53. #define NONCE(ctr,p) vec_sro(*(vec *)p, (vector char)(vec){0,0,0,8*8})+((vec){((ctr)&0xffffffff), (ctr)>>32, 0, 0})
  54. #error "Don't use this code till it can be tested on altivec"
  55. #define REVW_BE(x) __builtin_bswap32(x)
  56. #define REVV_BE(x) vec_perm(x,x,(vector char){3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12})
  57. #define ROTV1(x) vec_perm(x,x,(vector char){4,5,6,7,8,9,10,11,12,13,14,15,0,1,2,3})
  58. #define ROTV2(x) vec_perm(x,x,(vector char){8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7})
  59. #define ROTV3(x) vec_perm(x,x,(vector char){12,13,14,15,0,1,2,3,4,5,6,7,8,9,10,11})
  60. #define ROTW7(x) vec_rl(x,vec_splat_u32(7))
  61. #define ROTW8(x) vec_rl(x,vec_splat_u32(8))
  62. #define ROTW12(x) vec_rl(x,vec_splat_u32(12))
  63. #define ROTW16(x) vec_rl(x,vec_splat_u32(-16)) /* trick to get 16 */
  64. #elif __SSE2__
  65. #include <emmintrin.h>
  66. #define GPR_TOO 0
  67. #if __clang__
  68. #define VBPI 4
  69. #else
  70. #define VBPI 3
  71. #endif
  72. #define ONE (vec)_mm_set_epi32(0,0,0,1)
  73. #define NONCE(ctr,p) (vec)(_mm_slli_si128(_mm_loadl_epi64((__m128i *)(p)),8)+_mm_set_epi64x(0,ctr))
  74. #define ROTV1(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(0,3,2,1))
  75. #define ROTV2(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(1,0,3,2))
  76. #define ROTV3(x) (vec)_mm_shuffle_epi32((__m128i)x,_MM_SHUFFLE(2,1,0,3))
  77. #define ROTW7(x) (vec)(_mm_slli_epi32((__m128i)x, 7) ^ _mm_srli_epi32((__m128i)x,25))
  78. #define ROTW12(x) (vec)(_mm_slli_epi32((__m128i)x,12) ^ _mm_srli_epi32((__m128i)x,20))
  79. #if __SSSE3__
  80. #include <tmmintrin.h>
  81. #define ROTW8(x) (vec)_mm_shuffle_epi8((__m128i)x,_mm_set_epi8(14,13,12,15,10,9,8,11,6,5,4,7,2,1,0,3))
  82. #define ROTW16(x) (vec)_mm_shuffle_epi8((__m128i)x,_mm_set_epi8(13,12,15,14,9,8,11,10,5,4,7,6,1,0,3,2))
  83. #else
  84. #define ROTW8(x) (vec)(_mm_slli_epi32((__m128i)x, 8) ^ _mm_srli_epi32((__m128i)x,24))
  85. #define ROTW16(x) (vec)(_mm_slli_epi32((__m128i)x,16) ^ _mm_srli_epi32((__m128i)x,16))
  86. #endif
  87. #else
  88. #error -- Implementation supports only machines with neon, altivec or SSE2
  89. #endif
  90. #ifndef REVV_BE
  91. #define REVV_BE(x) (x)
  92. #endif
  93. #ifndef REVW_BE
  94. #define REVW_BE(x) (x)
  95. #endif
  96. #define BPI (VBPI + GPR_TOO) /* Blocks computed per loop iteration */
  97. #define DQROUND_VECTORS(a,b,c,d) \
  98. a += b; d ^= a; d = ROTW16(d); \
  99. c += d; b ^= c; b = ROTW12(b); \
  100. a += b; d ^= a; d = ROTW8(d); \
  101. c += d; b ^= c; b = ROTW7(b); \
  102. b = ROTV1(b); c = ROTV2(c); d = ROTV3(d); \
  103. a += b; d ^= a; d = ROTW16(d); \
  104. c += d; b ^= c; b = ROTW12(b); \
  105. a += b; d ^= a; d = ROTW8(d); \
  106. c += d; b ^= c; b = ROTW7(b); \
  107. b = ROTV3(b); c = ROTV2(c); d = ROTV1(d);
  108. #define QROUND_WORDS(a,b,c,d) \
  109. a = a+b; d ^= a; d = d<<16 | d>>16; \
  110. c = c+d; b ^= c; b = b<<12 | b>>20; \
  111. a = a+b; d ^= a; d = d<< 8 | d>>24; \
  112. c = c+d; b ^= c; b = b<< 7 | b>>25;
  113. #define WRITE(op, d, v0, v1, v2, v3) \
  114. *(vec *)(op + d + 0) = REVV_BE(v0); \
  115. *(vec *)(op + d + 4) = REVV_BE(v1); \
  116. *(vec *)(op + d + 8) = REVV_BE(v2); \
  117. *(vec *)(op + d + 12) = REVV_BE(v3);
  118. struct chacha_state_krovetz {
  119. __attribute__ ((aligned (16))) uint8_t key[32];
  120. __attribute__ ((aligned (16))) uint8_t nonce[8];
  121. };
  122. #define LOOP_ITERATIONS 4
  123. static inline int
  124. ottery_blocks_chacha_krovetz(
  125. const int chacha_rounds,
  126. uint8_t *out,
  127. uint32_t block_idx,
  128. struct chacha_state_krovetz *st)
  129. __attribute__((always_inline));
  130. /** Generates 64 * BPI * LOOP_ITERATIONS bytes of output using the key and
  131. * nonce in st and the counter in block_idx, and store them in out.
  132. */
  133. static inline int
  134. ottery_blocks_chacha_krovetz(
  135. const int chacha_rounds,
  136. uint8_t *out,
  137. uint32_t block_idx,
  138. struct chacha_state_krovetz *st)
  139. /* Assumes all pointers are aligned properly for vector reads */
  140. {
  141. const unsigned char *k = st->key;
  142. const unsigned char *n = st->nonce;
  143. unsigned i, j, *op=(unsigned *)out, *kp, *np;
  144. __attribute__ ((aligned (16))) unsigned chacha_const[] =
  145. {0x61707865,0x3320646E,0x79622D32,0x6B206574};
  146. #if ( __ARM_NEON__ || __SSE2__)
  147. kp = (unsigned *)k;
  148. np = (unsigned *)n;
  149. #else
  150. __attribute__ ((aligned (16))) unsigned key[8], nonce[2];
  151. ((vec *)key)[0] = REVV_BE(((vec *)k)[0]);
  152. ((vec *)key)[1] = REVV_BE(((vec *)k)[1]);
  153. nonce[0] = REVW_BE(((unsigned *)n)[0]);
  154. nonce[1] = REVW_BE(((unsigned *)n)[1]);
  155. kp = (unsigned *)key;
  156. np = (unsigned *)nonce;
  157. #endif
  158. vec s0 = *(vec *)chacha_const;
  159. vec s1 = ((vec *)kp)[0];
  160. vec s2 = ((vec *)kp)[1];
  161. vec s3 = NONCE(block_idx, np);
  162. for (j = 0; j < LOOP_ITERATIONS; ++j) {
  163. vec v0,v1,v2,v3,v4,v5,v6,v7;
  164. v4 = v0 = s0; v5 = v1 = s1; v6 = v2 = s2; v3 = s3;
  165. v7 = v3 + ONE;
  166. #if VBPI > 2
  167. vec v8,v9,v10,v11;
  168. v8 = v4; v9 = v5; v10 = v6;
  169. v11 = v7 + ONE;
  170. #endif
  171. #if VBPI > 3
  172. vec v12,v13,v14,v15;
  173. v12 = v8; v13 = v9; v14 = v10;
  174. v15 = v11 + ONE;
  175. #endif
  176. #if GPR_TOO
  177. register unsigned x0, x1, x2, x3, x4, x5, x6, x7, x8,
  178. x9, x10, x11, x12, x13, x14, x15;
  179. x0 = chacha_const[0]; x1 = chacha_const[1];
  180. x2 = chacha_const[2]; x3 = chacha_const[3];
  181. x4 = kp[0]; x5 = kp[1]; x6 = kp[2]; x7 = kp[3];
  182. x8 = kp[4]; x9 = kp[5]; x10 = kp[6]; x11 = kp[7];
  183. const uint64_t x_ctr = block_idx + BPI*iters+(BPI-1);
  184. x12 = x_ctr & 0xffffffff; x13 = x_ctr>>32; x14 = np[0]; x15 = np[1];
  185. #endif
  186. for (i = chacha_rounds/2; i; i--) {
  187. DQROUND_VECTORS(v0,v1,v2,v3)
  188. DQROUND_VECTORS(v4,v5,v6,v7)
  189. #if VBPI > 2
  190. DQROUND_VECTORS(v8,v9,v10,v11)
  191. #endif
  192. #if VBPI > 3
  193. DQROUND_VECTORS(v12,v13,v14,v15)
  194. #endif
  195. #if GPR_TOO
  196. QROUND_WORDS( x0, x4, x8,x12)
  197. QROUND_WORDS( x1, x5, x9,x13)
  198. QROUND_WORDS( x2, x6,x10,x14)
  199. QROUND_WORDS( x3, x7,x11,x15)
  200. QROUND_WORDS( x0, x5,x10,x15)
  201. QROUND_WORDS( x1, x6,x11,x12)
  202. QROUND_WORDS( x2, x7, x8,x13)
  203. QROUND_WORDS( x3, x4, x9,x14)
  204. #endif
  205. }
  206. WRITE(op, 0, v0+s0, v1+s1, v2+s2, v3+s3)
  207. s3 += ONE;
  208. WRITE(op, 16, v4+s0, v5+s1, v6+s2, v7+s3)
  209. s3 += ONE;
  210. #if VBPI > 2
  211. WRITE(op, 32, v8+s0, v9+s1, v10+s2, v11+s3)
  212. s3 += ONE;
  213. #endif
  214. #if VBPI > 3
  215. WRITE(op, 48, v12+s0, v13+s1, v14+s2, v15+s3)
  216. s3 += ONE;
  217. #endif
  218. op += VBPI*16;
  219. #if GPR_TOO
  220. op[0] = REVW_BE((x0 + chacha_const[0]));
  221. op[1] = REVW_BE((x1 + chacha_const[1]));
  222. op[2] = REVW_BE((x2 + chacha_const[2]));
  223. op[3] = REVW_BE((x3 + chacha_const[3]));
  224. op[4] = REVW_BE((x4 + kp[0]));
  225. op[5] = REVW_BE((x5 + kp[1]));
  226. op[6] = REVW_BE((x6 + kp[2]));
  227. op[7] = REVW_BE((x7 + kp[3]));
  228. op[8] = REVW_BE((x8 + kp[4]));
  229. op[9] = REVW_BE((x9 + kp[5]));
  230. op[10] = REVW_BE((x10 + kp[6]));
  231. op[11] = REVW_BE((x11 + kp[7]));
  232. op[12] = REVW_BE((x12 + (x_ctr & 0xffffffff)));
  233. op[13] = REVW_BE((x13 + (x_ctr >> 32)));
  234. op[14] = REVW_BE((x14 + np[0]));
  235. op[15] = REVW_BE((x15 + np[1]));
  236. s3 += ONE;
  237. op += 16;
  238. #endif
  239. }
  240. return 0;
  241. }
  242. #define STATE_LEN (sizeof(struct chacha_state_krovetz))
  243. #define STATE_BYTES 40
  244. #define IDX_STEP (BPI * LOOP_ITERATIONS)
  245. #define OUTPUT_LEN (IDX_STEP * 64)
  246. static void
  247. chacha_krovetz_state_setup(void *state, const uint8_t *bytes)
  248. {
  249. struct chacha_state_krovetz *st = state;
  250. memcpy(st->key, bytes, 32);
  251. memcpy(st->nonce, bytes+32, 8);
  252. }
  253. static void
  254. chacha8_krovetz_generate(void *state, uint8_t *output, uint32_t idx)
  255. {
  256. struct chacha_state_krovetz *st = state;
  257. ottery_blocks_chacha_krovetz(8, output, idx * IDX_STEP, st);
  258. }
  259. static void
  260. chacha12_krovetz_generate(void *state, uint8_t *output, uint32_t idx)
  261. {
  262. struct chacha_state_krovetz *st = state;
  263. ottery_blocks_chacha_krovetz(12, output, idx * IDX_STEP, st);
  264. }
  265. static void
  266. chacha20_krovetz_generate(void *state, uint8_t *output, uint32_t idx)
  267. {
  268. struct chacha_state_krovetz *st = state;
  269. ottery_blocks_chacha_krovetz(20, output, idx * IDX_STEP, st);
  270. }
  271. #ifdef __SSSE3__
  272. #define NEED_CPUCAP OTTERY_CPUCAP_SSSE3|OTTERY_CPUCAP_SIMD
  273. #define FLAV "-SSSE3"
  274. #else
  275. #define NEED_CPUCAP OTTERY_CPUCAP_SIMD
  276. #define FLAV "-DEFAULT"
  277. #endif
  278. #define PRF_CHACHA(r) { \
  279. "CHACHA" #r, \
  280. "CHACHA" #r "-SIMD", \
  281. "CHACHA" #r "-SIMD" FLAV, \
  282. STATE_LEN, \
  283. STATE_BYTES, \
  284. OUTPUT_LEN, \
  285. NEED_CPUCAP, \
  286. chacha_krovetz_state_setup, \
  287. chacha ## r ## _krovetz_generate \
  288. }
  289. #if defined OTTERY_BUILDING_SIMD1
  290. const struct ottery_prf ottery_prf_chacha8_krovetz_1_ = PRF_CHACHA(8);
  291. const struct ottery_prf ottery_prf_chacha12_krovetz_1_ = PRF_CHACHA(12);
  292. const struct ottery_prf ottery_prf_chacha20_krovetz_1_ = PRF_CHACHA(20);
  293. #elif defined OTTERY_BUILDING_SIMD2
  294. const struct ottery_prf ottery_prf_chacha8_krovetz_2_ = PRF_CHACHA(8);
  295. const struct ottery_prf ottery_prf_chacha12_krovetz_2_ = PRF_CHACHA(12);
  296. const struct ottery_prf ottery_prf_chacha20_krovetz_2_ = PRF_CHACHA(20);
  297. #else
  298. #error "Which PRF symbols am I supposed to define?"
  299. #endif