chacha_enc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Adapted from the public domain code by D. Bernstein from SUPERCOP. */
  10. #include <string.h>
  11. #include "internal/endian.h"
  12. #include "crypto/chacha.h"
  13. #include "crypto/ctype.h"
  14. typedef unsigned int u32;
  15. typedef unsigned char u8;
  16. typedef union {
  17. u32 u[16];
  18. u8 c[64];
  19. } chacha_buf;
  20. # define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
  21. # ifndef PEDANTIC
  22. # if defined(__GNUC__) && __GNUC__>=2 && \
  23. !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  24. # if defined(__riscv_zbb) || defined(__riscv_zbkb)
  25. # if __riscv_xlen == 64
  26. # undef ROTATE
  27. # define ROTATE(x, n) ({ u32 ret; \
  28. asm ("roriw %0, %1, %2" \
  29. : "=r"(ret) \
  30. : "r"(x), "i"(32 - (n))); ret;})
  31. # endif
  32. # if __riscv_xlen == 32
  33. # undef ROTATE
  34. # define ROTATE(x, n) ({ u32 ret; \
  35. asm ("rori %0, %1, %2" \
  36. : "=r"(ret) \
  37. : "r"(x), "i"(32 - (n))); ret;})
  38. # endif
  39. # endif
  40. # endif
  41. # endif
  42. # define U32TO8_LITTLE(p, v) do { \
  43. (p)[0] = (u8)(v >> 0); \
  44. (p)[1] = (u8)(v >> 8); \
  45. (p)[2] = (u8)(v >> 16); \
  46. (p)[3] = (u8)(v >> 24); \
  47. } while(0)
  48. /* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
  49. # define QUARTERROUND(a,b,c,d) ( \
  50. x[a] += x[b], x[d] = ROTATE((x[d] ^ x[a]),16), \
  51. x[c] += x[d], x[b] = ROTATE((x[b] ^ x[c]),12), \
  52. x[a] += x[b], x[d] = ROTATE((x[d] ^ x[a]), 8), \
  53. x[c] += x[d], x[b] = ROTATE((x[b] ^ x[c]), 7) )
  54. /* chacha_core performs 20 rounds of ChaCha on the input words in
  55. * |input| and writes the 64 output bytes to |output|. */
  56. static void chacha20_core(chacha_buf *output, const u32 input[16])
  57. {
  58. u32 x[16];
  59. int i;
  60. DECLARE_IS_ENDIAN;
  61. memcpy(x, input, sizeof(x));
  62. for (i = 20; i > 0; i -= 2) {
  63. QUARTERROUND(0, 4, 8, 12);
  64. QUARTERROUND(1, 5, 9, 13);
  65. QUARTERROUND(2, 6, 10, 14);
  66. QUARTERROUND(3, 7, 11, 15);
  67. QUARTERROUND(0, 5, 10, 15);
  68. QUARTERROUND(1, 6, 11, 12);
  69. QUARTERROUND(2, 7, 8, 13);
  70. QUARTERROUND(3, 4, 9, 14);
  71. }
  72. if (IS_LITTLE_ENDIAN) {
  73. for (i = 0; i < 16; ++i)
  74. output->u[i] = x[i] + input[i];
  75. } else {
  76. for (i = 0; i < 16; ++i)
  77. U32TO8_LITTLE(output->c + 4 * i, (x[i] + input[i]));
  78. }
  79. }
  80. #ifdef INCLUDE_C_CHACHA20
  81. void ChaCha20_ctr32_c(unsigned char *out, const unsigned char *inp, size_t len,
  82. const unsigned int key[8], const unsigned int counter[4])
  83. #else
  84. void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp, size_t len,
  85. const unsigned int key[8], const unsigned int counter[4])
  86. #endif
  87. {
  88. u32 input[16];
  89. chacha_buf buf;
  90. size_t todo, i;
  91. /* sigma constant "expand 32-byte k" in little-endian encoding */
  92. input[0] = ((u32)ossl_toascii('e')) | ((u32)ossl_toascii('x') << 8)
  93. | ((u32)ossl_toascii('p') << 16)
  94. | ((u32)ossl_toascii('a') << 24);
  95. input[1] = ((u32)ossl_toascii('n')) | ((u32)ossl_toascii('d') << 8)
  96. | ((u32)ossl_toascii(' ') << 16)
  97. | ((u32)ossl_toascii('3') << 24);
  98. input[2] = ((u32)ossl_toascii('2')) | ((u32)ossl_toascii('-') << 8)
  99. | ((u32)ossl_toascii('b') << 16)
  100. | ((u32)ossl_toascii('y') << 24);
  101. input[3] = ((u32)ossl_toascii('t')) | ((u32)ossl_toascii('e') << 8)
  102. | ((u32)ossl_toascii(' ') << 16)
  103. | ((u32)ossl_toascii('k') << 24);
  104. input[4] = key[0];
  105. input[5] = key[1];
  106. input[6] = key[2];
  107. input[7] = key[3];
  108. input[8] = key[4];
  109. input[9] = key[5];
  110. input[10] = key[6];
  111. input[11] = key[7];
  112. input[12] = counter[0];
  113. input[13] = counter[1];
  114. input[14] = counter[2];
  115. input[15] = counter[3];
  116. while (len > 0) {
  117. todo = sizeof(buf);
  118. if (len < todo)
  119. todo = len;
  120. chacha20_core(&buf, input);
  121. for (i = 0; i < todo; i++)
  122. out[i] = inp[i] ^ buf.c[i];
  123. out += todo;
  124. inp += todo;
  125. len -= todo;
  126. /*
  127. * Advance 32-bit counter. Note that as subroutine is so to
  128. * say nonce-agnostic, this limited counter width doesn't
  129. * prevent caller from implementing wider counter. It would
  130. * simply take two calls split on counter overflow...
  131. */
  132. input[12]++;
  133. }
  134. }