crypto_scrypt_smix.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <string.h>
  31. #include "sysendian.h"
  32. #include "crypto_scrypt_smix.h"
  33. static void blkcpy(uint32_t *, const uint32_t *, size_t);
  34. static void blkxor(uint32_t *, const uint32_t *, size_t);
  35. static void salsa20_8(uint32_t[16]);
  36. static void blockmix_salsa8(const uint32_t *, uint32_t *, uint32_t *, size_t);
  37. static uint64_t integerify(const uint32_t *, size_t);
  38. static void
  39. blkcpy(uint32_t * dest, const uint32_t * src, size_t len)
  40. {
  41. memcpy(dest, src, len);
  42. }
  43. static void
  44. blkxor(uint32_t * dest, const uint32_t * src, size_t len)
  45. {
  46. size_t i;
  47. for (i = 0; i < len / 4; i++)
  48. dest[i] ^= src[i];
  49. }
  50. /**
  51. * salsa20_8(B):
  52. * Apply the salsa20/8 core to the provided block.
  53. */
  54. static void
  55. salsa20_8(uint32_t B[16])
  56. {
  57. uint32_t x[16];
  58. size_t i;
  59. blkcpy(x, B, 64);
  60. for (i = 0; i < 8; i += 2) {
  61. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  62. /* Operate on columns. */
  63. x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
  64. x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
  65. x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
  66. x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
  67. x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
  68. x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
  69. x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
  70. x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
  71. /* Operate on rows. */
  72. x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
  73. x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
  74. x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
  75. x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
  76. x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
  77. x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
  78. x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
  79. x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
  80. #undef R
  81. }
  82. for (i = 0; i < 16; i++)
  83. B[i] += x[i];
  84. }
  85. /**
  86. * blockmix_salsa8(Bin, Bout, X, r):
  87. * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
  88. * bytes in length; the output Bout must also be the same size. The
  89. * temporary space X must be 64 bytes.
  90. */
  91. static void
  92. blockmix_salsa8(const uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
  93. {
  94. size_t i;
  95. /* 1: X <-- B_{2r - 1} */
  96. blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
  97. /* 2: for i = 0 to 2r - 1 do */
  98. for (i = 0; i < 2 * r; i += 2) {
  99. /* 3: X <-- H(X \xor B_i) */
  100. blkxor(X, &Bin[i * 16], 64);
  101. salsa20_8(X);
  102. /* 4: Y_i <-- X */
  103. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  104. blkcpy(&Bout[i * 8], X, 64);
  105. /* 3: X <-- H(X \xor B_i) */
  106. blkxor(X, &Bin[i * 16 + 16], 64);
  107. salsa20_8(X);
  108. /* 4: Y_i <-- X */
  109. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  110. blkcpy(&Bout[i * 8 + r * 16], X, 64);
  111. }
  112. }
  113. /**
  114. * integerify(B, r):
  115. * Return the result of parsing B_{2r-1} as a little-endian integer.
  116. */
  117. static uint64_t
  118. integerify(const uint32_t * B, size_t r)
  119. {
  120. const uint32_t * X = B + (2 * r - 1) * 16;
  121. return (((uint64_t)(X[1]) << 32) + X[0]);
  122. }
  123. /**
  124. * crypto_scrypt_smix(B, r, N, V, XY):
  125. * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
  126. * the temporary storage V must be 128rN bytes in length; the temporary
  127. * storage XY must be 256r + 64 bytes in length. The value N must be a
  128. * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
  129. * multiple of 64 bytes.
  130. */
  131. void
  132. crypto_scrypt_smix(uint8_t * B, size_t r, uint64_t N, void * _v, void * XY)
  133. {
  134. uint32_t * X = XY;
  135. uint32_t * Y = (void *)((uint8_t *)(XY) + 128 * r);
  136. uint32_t * Z = (void *)((uint8_t *)(XY) + 256 * r);
  137. uint32_t * V = _v;
  138. uint64_t i;
  139. uint64_t j;
  140. size_t k;
  141. /* 1: X <-- B */
  142. for (k = 0; k < 32 * r; k++)
  143. X[k] = le32dec(&B[4 * k]);
  144. /* 2: for i = 0 to N - 1 do */
  145. for (i = 0; i < N; i += 2) {
  146. /* 3: V_i <-- X */
  147. blkcpy(&V[i * (32 * r)], X, 128 * r);
  148. /* 4: X <-- H(X) */
  149. blockmix_salsa8(X, Y, Z, r);
  150. /* 3: V_i <-- X */
  151. blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
  152. /* 4: X <-- H(X) */
  153. blockmix_salsa8(Y, X, Z, r);
  154. }
  155. /* 6: for i = 0 to N - 1 do */
  156. for (i = 0; i < N; i += 2) {
  157. /* 7: j <-- Integerify(X) mod N */
  158. j = integerify(X, r) & (N - 1);
  159. /* 8: X <-- H(X \xor V_j) */
  160. blkxor(X, &V[j * (32 * r)], 128 * r);
  161. blockmix_salsa8(X, Y, Z, r);
  162. /* 7: j <-- Integerify(X) mod N */
  163. j = integerify(Y, r) & (N - 1);
  164. /* 8: X <-- H(X \xor V_j) */
  165. blkxor(Y, &V[j * (32 * r)], 128 * r);
  166. blockmix_salsa8(Y, X, Z, r);
  167. }
  168. /* 10: B' <-- X */
  169. for (k = 0; k < 32 * r; k++)
  170. le32enc(&B[4 * k], X[k]);
  171. }