crypto_scrypt-common.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*-
  2. * Copyright 2013 Alexander Peslyak
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted.
  7. *
  8. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  9. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  10. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  11. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  12. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  13. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  14. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  15. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18. * SUCH DAMAGE.
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include "crypto_pwhash_scryptsalsa208sha256.h"
  23. #include "crypto_scrypt.h"
  24. #include "private/common.h"
  25. #include "runtime.h"
  26. #include "utils.h"
  27. static const char *const itoa64 =
  28. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  29. static uint8_t *
  30. encode64_uint32(uint8_t *dst, size_t dstlen, uint32_t src, uint32_t srcbits)
  31. {
  32. uint32_t bit;
  33. for (bit = 0; bit < srcbits; bit += 6) {
  34. if (dstlen < 1) {
  35. return NULL; /* LCOV_EXCL_LINE */
  36. }
  37. *dst++ = itoa64[src & 0x3f];
  38. dstlen--;
  39. src >>= 6;
  40. }
  41. return dst;
  42. }
  43. static uint8_t *
  44. encode64(uint8_t *dst, size_t dstlen, const uint8_t *src, size_t srclen)
  45. {
  46. size_t i;
  47. for (i = 0; i < srclen;) {
  48. uint8_t *dnext;
  49. uint32_t value = 0, bits = 0;
  50. do {
  51. value |= (uint32_t) src[i++] << bits;
  52. bits += 8;
  53. } while (bits < 24 && i < srclen);
  54. dnext = encode64_uint32(dst, dstlen, value, bits);
  55. if (!dnext) {
  56. return NULL; /* LCOV_EXCL_LINE */
  57. }
  58. dstlen -= dnext - dst;
  59. dst = dnext;
  60. }
  61. return dst;
  62. }
  63. static int
  64. decode64_one(uint32_t *dst, uint8_t src)
  65. {
  66. const char *ptr = strchr(itoa64, src);
  67. if (ptr) {
  68. *dst = (uint32_t)(ptr - itoa64);
  69. return 0;
  70. }
  71. *dst = 0;
  72. return -1;
  73. }
  74. static const uint8_t *
  75. decode64_uint32(uint32_t *dst, uint32_t dstbits, const uint8_t *src)
  76. {
  77. uint32_t bit;
  78. uint32_t value;
  79. value = 0;
  80. for (bit = 0; bit < dstbits; bit += 6) {
  81. uint32_t one;
  82. if (decode64_one(&one, *src)) {
  83. *dst = 0;
  84. return NULL;
  85. }
  86. src++;
  87. value |= one << bit;
  88. }
  89. *dst = value;
  90. return src;
  91. }
  92. const uint8_t *
  93. escrypt_parse_setting(const uint8_t *setting,
  94. uint32_t *N_log2_p, uint32_t *r_p, uint32_t *p_p)
  95. {
  96. const uint8_t *src;
  97. if (setting[0] != '$' || setting[1] != '7' || setting[2] != '$') {
  98. return NULL;
  99. }
  100. src = setting + 3;
  101. if (decode64_one(N_log2_p, *src)) {
  102. return NULL;
  103. }
  104. src++;
  105. src = decode64_uint32(r_p, 30, src);
  106. if (!src) {
  107. return NULL;
  108. }
  109. src = decode64_uint32(p_p, 30, src);
  110. if (!src) {
  111. return NULL;
  112. }
  113. return src;
  114. }
  115. uint8_t *
  116. escrypt_r(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen,
  117. const uint8_t *setting, uint8_t *buf, size_t buflen)
  118. {
  119. uint8_t hash[crypto_pwhash_scryptsalsa208sha256_STRHASHBYTES];
  120. escrypt_kdf_t escrypt_kdf;
  121. const uint8_t *src;
  122. const uint8_t *salt;
  123. uint8_t *dst;
  124. size_t prefixlen;
  125. size_t saltlen;
  126. size_t need;
  127. uint64_t N;
  128. uint32_t N_log2;
  129. uint32_t r;
  130. uint32_t p;
  131. src = escrypt_parse_setting(setting, &N_log2, &r, &p);
  132. if (!src) {
  133. return NULL;
  134. }
  135. N = (uint64_t) 1 << N_log2;
  136. prefixlen = src - setting;
  137. salt = src;
  138. src = (const uint8_t *) strrchr((const char *) salt, '$');
  139. if (src) {
  140. saltlen = src - salt;
  141. } else {
  142. saltlen = strlen((const char *) salt);
  143. }
  144. need = prefixlen + saltlen + 1 +
  145. crypto_pwhash_scryptsalsa208sha256_STRHASHBYTES_ENCODED + 1;
  146. if (need > buflen || need < saltlen) {
  147. return NULL;
  148. }
  149. #ifdef HAVE_EMMINTRIN_H
  150. escrypt_kdf =
  151. sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse;
  152. #else
  153. escrypt_kdf = escrypt_kdf_nosse;
  154. #endif
  155. if (escrypt_kdf(local, passwd, passwdlen, salt, saltlen, N, r, p, hash,
  156. sizeof(hash))) {
  157. return NULL;
  158. }
  159. dst = buf;
  160. memcpy(dst, setting, prefixlen + saltlen);
  161. dst += prefixlen + saltlen;
  162. *dst++ = '$';
  163. dst = encode64(dst, buflen - (dst - buf), hash, sizeof(hash));
  164. sodium_memzero(hash, sizeof hash);
  165. if (!dst || dst >= buf + buflen) {
  166. return NULL; /* Can't happen LCOV_EXCL_LINE */
  167. }
  168. *dst = 0; /* NUL termination */
  169. return buf;
  170. }
  171. uint8_t *
  172. escrypt_gensalt_r(uint32_t N_log2, uint32_t r, uint32_t p, const uint8_t *src,
  173. size_t srclen, uint8_t *buf, size_t buflen)
  174. {
  175. uint8_t *dst;
  176. size_t prefixlen =
  177. (sizeof "$7$" - 1U) + (1U /* N_log2 */) + (5U /* r */) + (5U /* p */);
  178. size_t saltlen = BYTES2CHARS(srclen);
  179. size_t need;
  180. need = prefixlen + saltlen + 1;
  181. if (need > buflen || need < saltlen || saltlen < srclen) {
  182. return NULL; /* LCOV_EXCL_LINE */
  183. }
  184. if (N_log2 > 63 || ((uint64_t) r * (uint64_t) p >= (1U << 30))) {
  185. return NULL; /* LCOV_EXCL_LINE */
  186. }
  187. dst = buf;
  188. *dst++ = '$';
  189. *dst++ = '7';
  190. *dst++ = '$';
  191. *dst++ = itoa64[N_log2];
  192. dst = encode64_uint32(dst, buflen - (dst - buf), r, 30);
  193. if (!dst) {
  194. return NULL; /* Can't happen LCOV_EXCL_LINE */
  195. }
  196. dst = encode64_uint32(dst, buflen - (dst - buf), p, 30);
  197. if (!dst) {
  198. return NULL; /* Can't happen LCOV_EXCL_LINE */
  199. }
  200. dst = encode64(dst, buflen - (dst - buf), src, srclen);
  201. if (!dst || dst >= buf + buflen) {
  202. return NULL; /* Can't happen LCOV_EXCL_LINE */
  203. }
  204. *dst = 0; /* NUL termination */
  205. return buf;
  206. }
  207. int
  208. crypto_pwhash_scryptsalsa208sha256_ll(const uint8_t *passwd, size_t passwdlen,
  209. const uint8_t *salt, size_t saltlen,
  210. uint64_t N, uint32_t r, uint32_t p,
  211. uint8_t *buf, size_t buflen)
  212. {
  213. escrypt_kdf_t escrypt_kdf;
  214. escrypt_local_t local;
  215. int retval;
  216. if (escrypt_init_local(&local)) {
  217. return -1; /* LCOV_EXCL_LINE */
  218. }
  219. #if defined(HAVE_EMMINTRIN_H)
  220. escrypt_kdf =
  221. sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse;
  222. #else
  223. escrypt_kdf = escrypt_kdf_nosse;
  224. #endif
  225. retval = escrypt_kdf(&local, passwd, passwdlen, salt, saltlen, N, r, p, buf,
  226. buflen);
  227. if (escrypt_free_local(&local)) {
  228. return -1; /* LCOV_EXCL_LINE */
  229. }
  230. return retval;
  231. }