stream.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #define HYDRO_STREAM_CHACHA20_ROUNDS 12
  2. #define HYDRO_STREAM_CHACHA20_QUARTERROUND(a, b, c, d) \
  3. a += b; \
  4. d = ROTL32(d ^ a, 16); \
  5. c += d; \
  6. b = ROTL32(b ^ c, 12); \
  7. a += b; \
  8. d = ROTL32(d ^ a, 8); \
  9. c += d; \
  10. b = ROTL32(b ^ c, 7)
  11. static void hydro_stream_chacha20_rounds(uint32_t st[16])
  12. {
  13. int i;
  14. for (i = 0; i < HYDRO_STREAM_CHACHA20_ROUNDS; i += 2) {
  15. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[0], st[4], st[8], st[12]);
  16. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[1], st[5], st[9], st[13]);
  17. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[2], st[6], st[10], st[14]);
  18. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[3], st[7], st[11], st[15]);
  19. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[0], st[5], st[10], st[15]);
  20. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[1], st[6], st[11], st[12]);
  21. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[2], st[7], st[8], st[13]);
  22. HYDRO_STREAM_CHACHA20_QUARTERROUND(st[3], st[4], st[9], st[14]);
  23. }
  24. }
  25. static void hydro_stream_chacha20_update(uint32_t ks[16], uint32_t st[16])
  26. {
  27. int i;
  28. memcpy(ks, st, 4 * 16);
  29. hydro_stream_chacha20_rounds(st);
  30. for (i = 0; i < 16; i++) {
  31. ks[i] += st[i];
  32. }
  33. if (++st[12] == 0) {
  34. ++st[13];
  35. }
  36. }
  37. static void hydro_stream_chacha20_init(uint32_t st[16],
  38. const uint8_t nonce[hydro_stream_chacha20_NONCEBYTES],
  39. const uint8_t key[hydro_stream_chacha20_KEYBYTES])
  40. {
  41. int i;
  42. st[0] = 0x61707865UL;
  43. st[1] = 0x3120646eUL;
  44. st[2] = 0x79622d36UL;
  45. st[3] = 0x6b206574UL;
  46. for (i = 0; i < 8; i++) {
  47. st[4 + i] = LOAD32_LE(&key[4 * i]);
  48. }
  49. st[12] = 0;
  50. st[13] = LOAD32_LE(&nonce[4 * 0]);
  51. st[14] = LOAD32_LE(&nonce[4 * 1]);
  52. st[15] = LOAD32_LE(&nonce[4 * 2]);
  53. }
  54. static int hydro_stream_chacha20_xor(uint8_t *c, const uint8_t *m, size_t len,
  55. const uint8_t nonce[hydro_stream_chacha20_NONCEBYTES],
  56. const uint8_t key[hydro_stream_chacha20_KEYBYTES])
  57. {
  58. uint8_t tmp[64];
  59. uint32_t ks[16];
  60. uint32_t st[16];
  61. uint32_t x;
  62. int i;
  63. hydro_stream_chacha20_init(st, nonce, key);
  64. while (len >= 64) {
  65. hydro_stream_chacha20_update(ks, st);
  66. for (i = 0; i < 16; i++) {
  67. x = ks[i] ^ LOAD32_LE(m + 4 * i);
  68. STORE32_LE(c + 4 * i, x);
  69. }
  70. c += 64;
  71. m += 64;
  72. len -= 64;
  73. }
  74. if (len > 0) {
  75. hydro_stream_chacha20_update(ks, st);
  76. memset(tmp, 0, 64);
  77. for (i = 0; i < (int)len; i++) {
  78. tmp[i] = m[i];
  79. }
  80. for (i = 0; i < 16; i++) {
  81. x = ks[i] ^ LOAD32_LE(tmp + 4 * i);
  82. STORE32_LE(tmp + 4 * i, x);
  83. }
  84. for (i = 0; i < (int)len; i++) {
  85. c[i] = tmp[i];
  86. }
  87. }
  88. return 0;
  89. }
  90. static int hydro_stream_chacha20(uint8_t *c, size_t len,
  91. const uint8_t nonce[hydro_stream_chacha20_NONCEBYTES],
  92. const uint8_t key[hydro_stream_chacha20_KEYBYTES])
  93. {
  94. memset(c, 0, len);
  95. return hydro_stream_chacha20_xor(c, c, len, nonce, key);
  96. }
  97. static void hydro_stream_chacha20_block(
  98. uint8_t block[hydro_stream_chacha20_block_BYTES],
  99. const uint8_t nonce[hydro_stream_chacha20_block_NONCEBYTES],
  100. const uint8_t key[hydro_stream_chacha20_block_KEYBYTES])
  101. {
  102. uint32_t ks[16];
  103. uint32_t st[16];
  104. int i;
  105. hydro_stream_chacha20_init(st, &nonce[4], key);
  106. st[12] = LOAD32_LE(&nonce[0]);
  107. hydro_stream_chacha20_update(ks, st);
  108. for (i = 0; i < 16; i++) {
  109. STORE32_LE(block + 4 * i, ks[i]);
  110. }
  111. }
  112. static void hydro_stream_hchacha20(uint8_t subkey[hydro_stream_hchacha20_BYTES],
  113. const uint8_t nonce[hydro_stream_hchacha20_NONCEBYTES],
  114. const uint8_t key[hydro_stream_hchacha20_KEYBYTES])
  115. {
  116. uint32_t st[16];
  117. int i;
  118. hydro_stream_chacha20_init(st, &nonce[4], key);
  119. st[12] = LOAD32_LE(&nonce[0]);
  120. hydro_stream_chacha20_rounds(st);
  121. for (i = 0; i < 4; i++) {
  122. STORE32_LE(subkey + 4 * i, st[i]);
  123. }
  124. for (; i < 8; i++) {
  125. STORE32_LE(subkey + 4 * i, st[i + 12 - 4]);
  126. }
  127. }
  128. static int hydro_stream_xchacha20_xor(uint8_t *c, const uint8_t *m, size_t len,
  129. const uint8_t nonce[hydro_stream_xchacha20_NONCEBYTES],
  130. const uint8_t key[hydro_stream_xchacha20_KEYBYTES])
  131. {
  132. uint8_t subkey[hydro_stream_chacha20_KEYBYTES];
  133. uint8_t subnonce[hydro_stream_chacha20_NONCEBYTES];
  134. hydro_stream_hchacha20(subkey, nonce, key);
  135. COMPILER_ASSERT(
  136. hydro_stream_chacha20_KEYBYTES <= hydro_stream_hchacha20_BYTES);
  137. COMPILER_ASSERT(
  138. hydro_stream_xchacha20_NONCEBYTES - hydro_stream_hchacha20_NONCEBYTES ==
  139. 8);
  140. COMPILER_ASSERT(sizeof subnonce == 12);
  141. memset(subnonce, 0, 4);
  142. memcpy(subnonce + 4, nonce + hydro_stream_hchacha20_NONCEBYTES, 8);
  143. return hydro_stream_chacha20_xor(c, m, len, subnonce, subkey);
  144. }