crypto_scrypt_smix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*-
  2. * Copyright 2009 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * This file was originally written by Colin Percival as part of the Tarsnap
  27. * online backup system.
  28. */
  29. #include <stdint.h>
  30. #include "sysendian.h"
  31. #include "crypto_scrypt_smix.h"
  32. static void blkcpy(void *, const void *, size_t);
  33. static void blkxor(void *, const void *, size_t);
  34. static void salsa20_8(uint32_t[16]);
  35. static void blockmix_salsa8(const uint32_t *, uint32_t *, uint32_t *, size_t);
  36. static uint64_t integerify(const void *, size_t);
  37. static void
  38. blkcpy(void * dest, const void * src, size_t len)
  39. {
  40. size_t * D = dest;
  41. const size_t * S = src;
  42. size_t L = len / sizeof(size_t);
  43. size_t i;
  44. for (i = 0; i < L; i++)
  45. D[i] = S[i];
  46. }
  47. static void
  48. blkxor(void * dest, const void * src, size_t len)
  49. {
  50. size_t * D = dest;
  51. const size_t * S = src;
  52. size_t L = len / sizeof(size_t);
  53. size_t i;
  54. for (i = 0; i < L; i++)
  55. D[i] ^= S[i];
  56. }
  57. /**
  58. * salsa20_8(B):
  59. * Apply the salsa20/8 core to the provided block.
  60. */
  61. static void
  62. salsa20_8(uint32_t B[16])
  63. {
  64. uint32_t x[16];
  65. size_t i;
  66. blkcpy(x, B, 64);
  67. for (i = 0; i < 8; i += 2) {
  68. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  69. /* Operate on columns. */
  70. x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
  71. x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
  72. x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
  73. x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
  74. x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
  75. x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
  76. x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
  77. x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
  78. /* Operate on rows. */
  79. x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
  80. x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
  81. x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
  82. x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
  83. x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
  84. x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
  85. x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
  86. x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
  87. #undef R
  88. }
  89. for (i = 0; i < 16; i++)
  90. B[i] += x[i];
  91. }
  92. /**
  93. * blockmix_salsa8(Bin, Bout, X, r):
  94. * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
  95. * bytes in length; the output Bout must also be the same size. The
  96. * temporary space X must be 64 bytes.
  97. */
  98. static void
  99. blockmix_salsa8(const uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
  100. {
  101. size_t i;
  102. /* 1: X <-- B_{2r - 1} */
  103. blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
  104. /* 2: for i = 0 to 2r - 1 do */
  105. for (i = 0; i < 2 * r; i += 2) {
  106. /* 3: X <-- H(X \xor B_i) */
  107. blkxor(X, &Bin[i * 16], 64);
  108. salsa20_8(X);
  109. /* 4: Y_i <-- X */
  110. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  111. blkcpy(&Bout[i * 8], X, 64);
  112. /* 3: X <-- H(X \xor B_i) */
  113. blkxor(X, &Bin[i * 16 + 16], 64);
  114. salsa20_8(X);
  115. /* 4: Y_i <-- X */
  116. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  117. blkcpy(&Bout[i * 8 + r * 16], X, 64);
  118. }
  119. }
  120. /**
  121. * integerify(B, r):
  122. * Return the result of parsing B_{2r-1} as a little-endian integer.
  123. */
  124. static uint64_t
  125. integerify(const void * B, size_t r)
  126. {
  127. const uint32_t * X = (const void *)((uintptr_t)(B) + (2 * r - 1) * 64);
  128. return (((uint64_t)(X[1]) << 32) + X[0]);
  129. }
  130. /**
  131. * crypto_scrypt_smix(B, r, N, V, XY):
  132. * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
  133. * the temporary storage V must be 128rN bytes in length; the temporary
  134. * storage XY must be 256r + 64 bytes in length. The value N must be a
  135. * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
  136. * multiple of 64 bytes.
  137. */
  138. void
  139. crypto_scrypt_smix(uint8_t * B, size_t r, uint64_t N, void * _V, void * XY)
  140. {
  141. uint32_t * X = XY;
  142. uint32_t * Y = (void *)((uint8_t *)(XY) + 128 * r);
  143. uint32_t * Z = (void *)((uint8_t *)(XY) + 256 * r);
  144. uint32_t * V = _V;
  145. uint64_t i;
  146. uint64_t j;
  147. size_t k;
  148. /* 1: X <-- B */
  149. for (k = 0; k < 32 * r; k++)
  150. X[k] = le32dec(&B[4 * k]);
  151. /* 2: for i = 0 to N - 1 do */
  152. for (i = 0; i < N; i += 2) {
  153. /* 3: V_i <-- X */
  154. blkcpy(&V[i * (32 * r)], X, 128 * r);
  155. /* 4: X <-- H(X) */
  156. blockmix_salsa8(X, Y, Z, r);
  157. /* 3: V_i <-- X */
  158. blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
  159. /* 4: X <-- H(X) */
  160. blockmix_salsa8(Y, X, Z, r);
  161. }
  162. /* 6: for i = 0 to N - 1 do */
  163. for (i = 0; i < N; i += 2) {
  164. /* 7: j <-- Integerify(X) mod N */
  165. j = integerify(X, r) & (N - 1);
  166. /* 8: X <-- H(X \xor V_j) */
  167. blkxor(X, &V[j * (32 * r)], 128 * r);
  168. blockmix_salsa8(X, Y, Z, r);
  169. /* 7: j <-- Integerify(X) mod N */
  170. j = integerify(Y, r) & (N - 1);
  171. /* 8: X <-- H(X \xor V_j) */
  172. blkxor(Y, &V[j * (32 * r)], 128 * r);
  173. blockmix_salsa8(Y, X, Z, r);
  174. }
  175. /* 10: B' <-- X */
  176. for (k = 0; k < 32 * r; k++)
  177. le32enc(&B[4 * k], X[k]);
  178. }