25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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