選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

208 行
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 = 20; i > 0; i -= 2)
  85. {
  86. QUARTERROUND( 0, 4, 8,12)
  87. QUARTERROUND( 1, 5, 9,13)
  88. QUARTERROUND( 2, 6,10,14)
  89. QUARTERROUND( 3, 7,11,15)
  90. QUARTERROUND( 0, 5,10,15)
  91. QUARTERROUND( 1, 6,11,12)
  92. QUARTERROUND( 2, 7, 8,13)
  93. QUARTERROUND( 3, 4, 9,14)
  94. }
  95. for (i = 0; i < 16; ++i)
  96. x[i] = PLUS(x[i], input[i]);
  97. for (i = 0; i < 16; ++i)
  98. U32TO8_LITTLE(output + 4 * i, x[i]);
  99. }
  100. void CRYPTO_chacha_20(unsigned char *out,
  101. const unsigned char *in, size_t in_len,
  102. const unsigned char key[32],
  103. const unsigned char nonce[12],
  104. uint32_t counter)
  105. {
  106. uint32_t input[16];
  107. unsigned char buf[64];
  108. size_t todo, i;
  109. input[0] = U8TO32_LITTLE(sigma + 0);
  110. input[1] = U8TO32_LITTLE(sigma + 4);
  111. input[2] = U8TO32_LITTLE(sigma + 8);
  112. input[3] = U8TO32_LITTLE(sigma + 12);
  113. input[4] = U8TO32_LITTLE(key + 0);
  114. input[5] = U8TO32_LITTLE(key + 4);
  115. input[6] = U8TO32_LITTLE(key + 8);
  116. input[7] = U8TO32_LITTLE(key + 12);
  117. input[8] = U8TO32_LITTLE(key + 16);
  118. input[9] = U8TO32_LITTLE(key + 20);
  119. input[10] = U8TO32_LITTLE(key + 24);
  120. input[11] = U8TO32_LITTLE(key + 28);
  121. input[12] = counter;
  122. input[13] = U8TO32_LITTLE(nonce + 0);
  123. input[14] = U8TO32_LITTLE(nonce + 4);
  124. input[15] = U8TO32_LITTLE(nonce + 8);
  125. while (in_len > 0)
  126. {
  127. todo = sizeof(buf);
  128. if (in_len < todo)
  129. todo = in_len;
  130. chacha_core(buf, input, 20);
  131. for (i = 0; i < todo; i++)
  132. out[i] = in[i] ^ buf[i];
  133. out += todo;
  134. in += todo;
  135. in_len -= todo;
  136. input[12]++;
  137. if (input[12] == 0)
  138. input[13]++;
  139. }
  140. }
  141. void CRYPTO_chacha_20_keystream(unsigned char *out,
  142. size_t out_len,
  143. const unsigned char key[32],
  144. const unsigned char nonce[12],
  145. uint32_t counter)
  146. {
  147. uint32_t input[16];
  148. unsigned char buf[64];
  149. size_t todo, i;
  150. input[0] = U8TO32_LITTLE(sigma + 0);
  151. input[1] = U8TO32_LITTLE(sigma + 4);
  152. input[2] = U8TO32_LITTLE(sigma + 8);
  153. input[3] = U8TO32_LITTLE(sigma + 12);
  154. input[4] = U8TO32_LITTLE(key + 0);
  155. input[5] = U8TO32_LITTLE(key + 4);
  156. input[6] = U8TO32_LITTLE(key + 8);
  157. input[7] = U8TO32_LITTLE(key + 12);
  158. input[8] = U8TO32_LITTLE(key + 16);
  159. input[9] = U8TO32_LITTLE(key + 20);
  160. input[10] = U8TO32_LITTLE(key + 24);
  161. input[11] = U8TO32_LITTLE(key + 28);
  162. input[12] = counter;
  163. input[13] = U8TO32_LITTLE(nonce + 0);
  164. input[14] = U8TO32_LITTLE(nonce + 4);
  165. input[15] = U8TO32_LITTLE(nonce + 8);
  166. while (out_len > 0)
  167. {
  168. todo = sizeof(buf);
  169. if (out_len < todo)
  170. todo = out_len;
  171. chacha_core(buf, input, 20);
  172. for (i = 0; i < todo; i++)
  173. out[i] = buf[i];
  174. out += todo;
  175. out_len -= todo;
  176. input[12]++;
  177. if (input[12] == 0)
  178. input[13]++;
  179. }
  180. }