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.
 
 
 
 
 
 

168 lines
5.2 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 U32TO8_LITTLE(p, v) \
  61. { \
  62. (p)[0] = (v >> 0) & 0xff; \
  63. (p)[1] = (v >> 8) & 0xff; \
  64. (p)[2] = (v >> 16) & 0xff; \
  65. (p)[3] = (v >> 24) & 0xff; \
  66. }
  67. // QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round.
  68. #define QUARTERROUND(a, b, c, d) \
  69. x[a] += x[b]; x[d] = ROTATE(x[d] ^ x[a], 16); \
  70. x[c] += x[d]; x[b] = ROTATE(x[b] ^ x[c], 12); \
  71. x[a] += x[b]; x[d] = ROTATE(x[d] ^ x[a], 8); \
  72. x[c] += x[d]; x[b] = ROTATE(x[b] ^ x[c], 7);
  73. // chacha_core performs 20 rounds of ChaCha on the input words in
  74. // |input| and writes the 64 output bytes to |output|.
  75. static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
  76. uint32_t x[16];
  77. int i;
  78. OPENSSL_memcpy(x, input, sizeof(uint32_t) * 16);
  79. for (i = 20; i > 0; i -= 2) {
  80. QUARTERROUND(0, 4, 8, 12)
  81. QUARTERROUND(1, 5, 9, 13)
  82. QUARTERROUND(2, 6, 10, 14)
  83. QUARTERROUND(3, 7, 11, 15)
  84. QUARTERROUND(0, 5, 10, 15)
  85. QUARTERROUND(1, 6, 11, 12)
  86. QUARTERROUND(2, 7, 8, 13)
  87. QUARTERROUND(3, 4, 9, 14)
  88. }
  89. for (i = 0; i < 16; ++i) {
  90. x[i] += input[i];
  91. }
  92. for (i = 0; i < 16; ++i) {
  93. U32TO8_LITTLE(output + 4 * i, x[i]);
  94. }
  95. }
  96. void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
  97. const uint8_t key[32], const uint8_t nonce[12],
  98. uint32_t counter) {
  99. assert(!buffers_alias(out, in_len, in, in_len) || in == out);
  100. uint32_t input[16];
  101. uint8_t buf[64];
  102. size_t todo, i;
  103. input[0] = U8TO32_LITTLE(sigma + 0);
  104. input[1] = U8TO32_LITTLE(sigma + 4);
  105. input[2] = U8TO32_LITTLE(sigma + 8);
  106. input[3] = U8TO32_LITTLE(sigma + 12);
  107. input[4] = U8TO32_LITTLE(key + 0);
  108. input[5] = U8TO32_LITTLE(key + 4);
  109. input[6] = U8TO32_LITTLE(key + 8);
  110. input[7] = U8TO32_LITTLE(key + 12);
  111. input[8] = U8TO32_LITTLE(key + 16);
  112. input[9] = U8TO32_LITTLE(key + 20);
  113. input[10] = U8TO32_LITTLE(key + 24);
  114. input[11] = U8TO32_LITTLE(key + 28);
  115. input[12] = counter;
  116. input[13] = U8TO32_LITTLE(nonce + 0);
  117. input[14] = U8TO32_LITTLE(nonce + 4);
  118. input[15] = U8TO32_LITTLE(nonce + 8);
  119. while (in_len > 0) {
  120. todo = sizeof(buf);
  121. if (in_len < todo) {
  122. todo = in_len;
  123. }
  124. chacha_core(buf, input);
  125. for (i = 0; i < todo; i++) {
  126. out[i] = in[i] ^ buf[i];
  127. }
  128. out += todo;
  129. in += todo;
  130. in_len -= todo;
  131. input[12]++;
  132. }
  133. }
  134. #endif