Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

144 wiersze
4.4 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 <string.h>
  17. #include <openssl/cpu.h>
  18. #if defined(OPENSSL_WINDOWS) || (!defined(OPENSSL_X86_64) && !defined(OPENSSL_X86)) || !defined(__SSE2__)
  19. /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
  20. static const uint8_t sigma[16] = { 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3',
  21. '2', '-', 'b', 'y', 't', 'e', ' ', 'k' };
  22. #define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
  23. #define XOR(v, w) ((v) ^ (w))
  24. #define PLUS(x, y) ((x) + (y))
  25. #define PLUSONE(v) (PLUS((v), 1))
  26. #define U32TO8_LITTLE(p, v) \
  27. { \
  28. (p)[0] = (v >> 0) & 0xff; \
  29. (p)[1] = (v >> 8) & 0xff; \
  30. (p)[2] = (v >> 16) & 0xff; \
  31. (p)[3] = (v >> 24) & 0xff; \
  32. }
  33. #define U8TO32_LITTLE(p) \
  34. (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
  35. ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
  36. /* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
  37. #define QUARTERROUND(a,b,c,d) \
  38. x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \
  39. x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \
  40. x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \
  41. x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7);
  42. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  43. /* Defined in chacha_vec.c */
  44. void CRYPTO_chacha_20_neon(uint8_t *out, const uint8_t *in, size_t in_len,
  45. const uint8_t key[32], const uint8_t nonce[8],
  46. size_t counter);
  47. #endif
  48. /* chacha_core performs 20 rounds of ChaCha on the input words in
  49. * |input| and writes the 64 output bytes to |output|. */
  50. static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
  51. uint32_t x[16];
  52. int i;
  53. memcpy(x, input, sizeof(uint32_t) * 16);
  54. for (i = 20; i > 0; i -= 2) {
  55. QUARTERROUND(0, 4, 8, 12)
  56. QUARTERROUND(1, 5, 9, 13)
  57. QUARTERROUND(2, 6, 10, 14)
  58. QUARTERROUND(3, 7, 11, 15)
  59. QUARTERROUND(0, 5, 10, 15)
  60. QUARTERROUND(1, 6, 11, 12)
  61. QUARTERROUND(2, 7, 8, 13)
  62. QUARTERROUND(3, 4, 9, 14)
  63. }
  64. for (i = 0; i < 16; ++i) {
  65. x[i] = PLUS(x[i], input[i]);
  66. }
  67. for (i = 0; i < 16; ++i) {
  68. U32TO8_LITTLE(output + 4 * i, x[i]);
  69. }
  70. }
  71. void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
  72. const uint8_t key[32], const uint8_t nonce[8],
  73. size_t counter) {
  74. uint32_t input[16];
  75. uint8_t buf[64];
  76. size_t todo, i;
  77. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  78. if (CRYPTO_is_NEON_capable()) {
  79. CRYPTO_chacha_20_neon(out, in, in_len, key, nonce, counter);
  80. return;
  81. }
  82. #endif
  83. input[0] = U8TO32_LITTLE(sigma + 0);
  84. input[1] = U8TO32_LITTLE(sigma + 4);
  85. input[2] = U8TO32_LITTLE(sigma + 8);
  86. input[3] = U8TO32_LITTLE(sigma + 12);
  87. input[4] = U8TO32_LITTLE(key + 0);
  88. input[5] = U8TO32_LITTLE(key + 4);
  89. input[6] = U8TO32_LITTLE(key + 8);
  90. input[7] = U8TO32_LITTLE(key + 12);
  91. input[8] = U8TO32_LITTLE(key + 16);
  92. input[9] = U8TO32_LITTLE(key + 20);
  93. input[10] = U8TO32_LITTLE(key + 24);
  94. input[11] = U8TO32_LITTLE(key + 28);
  95. input[12] = counter;
  96. input[13] = ((uint64_t)counter) >> 32;
  97. input[14] = U8TO32_LITTLE(nonce + 0);
  98. input[15] = U8TO32_LITTLE(nonce + 4);
  99. while (in_len > 0) {
  100. todo = sizeof(buf);
  101. if (in_len < todo) {
  102. todo = in_len;
  103. }
  104. chacha_core(buf, input);
  105. for (i = 0; i < todo; i++) {
  106. out[i] = in[i] ^ buf[i];
  107. }
  108. out += todo;
  109. in += todo;
  110. in_len -= todo;
  111. input[12]++;
  112. if (input[12] == 0) {
  113. input[13]++;
  114. }
  115. }
  116. }
  117. #endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 && !OPENSSL_X86 || !__SSE2__ */