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.
 
 
 
 
 
 

171 lines
5.3 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. /* Adapted from the public domain, estream code by D. Bernstein. */
  15. #include <openssl/chacha.h>
  16. #include <assert.h>
  17. #include <string.h>
  18. #include <openssl/cpu.h>
  19. #include "../internal.h"
  20. #define U8TO32_LITTLE(p) \
  21. (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
  22. ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
  23. #if !defined(OPENSSL_NO_ASM) && \
  24. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
  25. defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  26. /* ChaCha20_ctr32 is defined in asm/chacha-*.pl. */
  27. void ChaCha20_ctr32(uint8_t *out, const uint8_t *in, size_t in_len,
  28. const uint32_t key[8], const uint32_t counter[4]);
  29. void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
  30. const uint8_t key[32], const uint8_t nonce[12],
  31. uint32_t counter) {
  32. assert(!buffers_alias(out, in_len, in, in_len) || in == out);
  33. uint32_t counter_nonce[4]; counter_nonce[0] = counter;
  34. counter_nonce[1] = U8TO32_LITTLE(nonce + 0);
  35. counter_nonce[2] = U8TO32_LITTLE(nonce + 4);
  36. counter_nonce[3] = U8TO32_LITTLE(nonce + 8);
  37. const uint32_t *key_ptr = (const uint32_t *)key;
  38. #if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64)
  39. /* The assembly expects the key to be four-byte aligned. */
  40. uint32_t key_u32[8];
  41. if ((((uintptr_t)key) & 3) != 0) {
  42. key_u32[0] = U8TO32_LITTLE(key + 0);
  43. key_u32[1] = U8TO32_LITTLE(key + 4);
  44. key_u32[2] = U8TO32_LITTLE(key + 8);
  45. key_u32[3] = U8TO32_LITTLE(key + 12);
  46. key_u32[4] = U8TO32_LITTLE(key + 16);
  47. key_u32[5] = U8TO32_LITTLE(key + 20);
  48. key_u32[6] = U8TO32_LITTLE(key + 24);
  49. key_u32[7] = U8TO32_LITTLE(key + 28);
  50. key_ptr = key_u32;
  51. }
  52. #endif
  53. ChaCha20_ctr32(out, in, in_len, key_ptr, counter_nonce);
  54. }
  55. #else
  56. /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
  57. static const uint8_t sigma[16] = { 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3',
  58. '2', '-', 'b', 'y', 't', 'e', ' ', 'k' };
  59. #define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
  60. #define XOR(v, w) ((v) ^ (w))
  61. #define PLUS(x, y) ((x) + (y))
  62. #define PLUSONE(v) (PLUS((v), 1))
  63. #define U32TO8_LITTLE(p, v) \
  64. { \
  65. (p)[0] = (v >> 0) & 0xff; \
  66. (p)[1] = (v >> 8) & 0xff; \
  67. (p)[2] = (v >> 16) & 0xff; \
  68. (p)[3] = (v >> 24) & 0xff; \
  69. }
  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 20 rounds of ChaCha on the input words in
  77. * |input| and writes the 64 output bytes to |output|. */
  78. static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
  79. uint32_t x[16];
  80. int i;
  81. memcpy(x, input, sizeof(uint32_t) * 16);
  82. for (i = 20; i > 0; i -= 2) {
  83. QUARTERROUND(0, 4, 8, 12)
  84. QUARTERROUND(1, 5, 9, 13)
  85. QUARTERROUND(2, 6, 10, 14)
  86. QUARTERROUND(3, 7, 11, 15)
  87. QUARTERROUND(0, 5, 10, 15)
  88. QUARTERROUND(1, 6, 11, 12)
  89. QUARTERROUND(2, 7, 8, 13)
  90. QUARTERROUND(3, 4, 9, 14)
  91. }
  92. for (i = 0; i < 16; ++i) {
  93. x[i] = PLUS(x[i], input[i]);
  94. }
  95. for (i = 0; i < 16; ++i) {
  96. U32TO8_LITTLE(output + 4 * i, x[i]);
  97. }
  98. }
  99. void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
  100. const uint8_t key[32], const uint8_t nonce[12],
  101. uint32_t counter) {
  102. assert(!buffers_alias(out, in_len, in, in_len) || in == out);
  103. uint32_t input[16];
  104. uint8_t buf[64];
  105. size_t todo, i;
  106. input[0] = U8TO32_LITTLE(sigma + 0);
  107. input[1] = U8TO32_LITTLE(sigma + 4);
  108. input[2] = U8TO32_LITTLE(sigma + 8);
  109. input[3] = U8TO32_LITTLE(sigma + 12);
  110. input[4] = U8TO32_LITTLE(key + 0);
  111. input[5] = U8TO32_LITTLE(key + 4);
  112. input[6] = U8TO32_LITTLE(key + 8);
  113. input[7] = U8TO32_LITTLE(key + 12);
  114. input[8] = U8TO32_LITTLE(key + 16);
  115. input[9] = U8TO32_LITTLE(key + 20);
  116. input[10] = U8TO32_LITTLE(key + 24);
  117. input[11] = U8TO32_LITTLE(key + 28);
  118. input[12] = counter;
  119. input[13] = U8TO32_LITTLE(nonce + 0);
  120. input[14] = U8TO32_LITTLE(nonce + 4);
  121. input[15] = U8TO32_LITTLE(nonce + 8);
  122. while (in_len > 0) {
  123. todo = sizeof(buf);
  124. if (in_len < todo) {
  125. todo = in_len;
  126. }
  127. chacha_core(buf, input);
  128. for (i = 0; i < todo; i++) {
  129. out[i] = in[i] ^ buf[i];
  130. }
  131. out += todo;
  132. in += todo;
  133. in_len -= todo;
  134. input[12]++;
  135. }
  136. }
  137. #endif