Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

205 řádky
6.4 KiB

  1. /*
  2. * This code is based on an OpenSSL implementation of chacha20.
  3. * Hence, the copyright below applies.
  4. *
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. /* Adapted from the public domain code by D. Bernstein from SUPERCOP. */
  55. #include <stdint.h>
  56. #include <string.h>
  57. #include "chacha.h"
  58. /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
  59. static const char sigma[16] = "expand 32-byte k";
  60. #define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
  61. #define XOR(v, w) ((v) ^ (w))
  62. #define PLUS(x, y) ((x) + (y))
  63. #define PLUSONE(v) (PLUS((v), 1))
  64. #define U32TO8_LITTLE(p, v) \
  65. { (p)[0] = (v >> 0) & 0xff; (p)[1] = (v >> 8) & 0xff; \
  66. (p)[2] = (v >> 16) & 0xff; (p)[3] = (v >> 24) & 0xff; }
  67. #define U8TO32_LITTLE(p) \
  68. (((uint32_t)((p)[0]) ) | ((uint32_t)((p)[1]) << 8) | \
  69. ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24) )
  70. /* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
  71. #define QUARTERROUND(a,b,c,d) \
  72. x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \
  73. x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \
  74. x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \
  75. x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7);
  76. /* chacha_core performs |num_rounds| rounds of ChaCha20 on the input words in
  77. * |input| and writes the 64 output bytes to |output|. */
  78. static void chacha_core(unsigned char output[64], const uint32_t input[16],
  79. int num_rounds)
  80. {
  81. uint32_t x[16];
  82. int i;
  83. memcpy(x, input, sizeof(uint32_t) * 16);
  84. for (i = num_rounds; i > 0; i -= 2) {
  85. QUARTERROUND( 0, 4, 8,12)
  86. QUARTERROUND( 1, 5, 9,13)
  87. QUARTERROUND( 2, 6,10,14)
  88. QUARTERROUND( 3, 7,11,15)
  89. QUARTERROUND( 0, 5,10,15)
  90. QUARTERROUND( 1, 6,11,12)
  91. QUARTERROUND( 2, 7, 8,13)
  92. QUARTERROUND( 3, 4, 9,14)
  93. }
  94. for (i = 0; i < 16; ++i)
  95. x[i] = PLUS(x[i], input[i]);
  96. for (i = 0; i < 16; ++i)
  97. U32TO8_LITTLE(output + 4 * i, x[i]);
  98. }
  99. void CRYPTO_chacha_20(unsigned char *out,
  100. const unsigned char *in, size_t in_len,
  101. const unsigned char key[32],
  102. const unsigned char nonce[12],
  103. uint32_t counter)
  104. {
  105. uint32_t input[16];
  106. unsigned char buf[64];
  107. size_t todo, i;
  108. input[0] = U8TO32_LITTLE(sigma + 0);
  109. input[1] = U8TO32_LITTLE(sigma + 4);
  110. input[2] = U8TO32_LITTLE(sigma + 8);
  111. input[3] = U8TO32_LITTLE(sigma + 12);
  112. input[4] = U8TO32_LITTLE(key + 0);
  113. input[5] = U8TO32_LITTLE(key + 4);
  114. input[6] = U8TO32_LITTLE(key + 8);
  115. input[7] = U8TO32_LITTLE(key + 12);
  116. input[8] = U8TO32_LITTLE(key + 16);
  117. input[9] = U8TO32_LITTLE(key + 20);
  118. input[10] = U8TO32_LITTLE(key + 24);
  119. input[11] = U8TO32_LITTLE(key + 28);
  120. input[12] = counter;
  121. input[13] = U8TO32_LITTLE(nonce + 0);
  122. input[14] = U8TO32_LITTLE(nonce + 4);
  123. input[15] = U8TO32_LITTLE(nonce + 8);
  124. while (in_len > 0) {
  125. todo = sizeof(buf);
  126. if (in_len < todo)
  127. todo = in_len;
  128. chacha_core(buf, input, 20);
  129. for (i = 0; i < todo; i++)
  130. out[i] = in[i] ^ buf[i];
  131. out += todo;
  132. in += todo;
  133. in_len -= todo;
  134. input[12]++;
  135. if (input[12] == 0)
  136. input[13]++;
  137. }
  138. }
  139. void CRYPTO_chacha_20_keystream(unsigned char *out,
  140. size_t out_len,
  141. const unsigned char key[32],
  142. const unsigned char nonce[12],
  143. uint32_t counter)
  144. {
  145. uint32_t input[16];
  146. unsigned char buf[64];
  147. size_t todo, i;
  148. input[0] = U8TO32_LITTLE(sigma + 0);
  149. input[1] = U8TO32_LITTLE(sigma + 4);
  150. input[2] = U8TO32_LITTLE(sigma + 8);
  151. input[3] = U8TO32_LITTLE(sigma + 12);
  152. input[4] = U8TO32_LITTLE(key + 0);
  153. input[5] = U8TO32_LITTLE(key + 4);
  154. input[6] = U8TO32_LITTLE(key + 8);
  155. input[7] = U8TO32_LITTLE(key + 12);
  156. input[8] = U8TO32_LITTLE(key + 16);
  157. input[9] = U8TO32_LITTLE(key + 20);
  158. input[10] = U8TO32_LITTLE(key + 24);
  159. input[11] = U8TO32_LITTLE(key + 28);
  160. input[12] = counter;
  161. input[13] = U8TO32_LITTLE(nonce + 0);
  162. input[14] = U8TO32_LITTLE(nonce + 4);
  163. input[15] = U8TO32_LITTLE(nonce + 8);
  164. while (out_len > 0) {
  165. todo = sizeof(buf);
  166. if (out_len < todo)
  167. todo = out_len;
  168. chacha_core(buf, input, 20);
  169. for (i = 0; i < todo; i++)
  170. out[i] = buf[i];
  171. out += todo;
  172. out_len -= todo;
  173. input[12]++;
  174. if (input[12] == 0)
  175. input[13]++;
  176. }
  177. }