You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

catena.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. * Copyright (c) 2014 cforler
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "config.h"
  23. #include "catena.h"
  24. #include <sodium.h>
  25. #if __BYTE_ORDER == __LITTLE_ENDIAN
  26. #define TO_LITTLE_ENDIAN_64(n) (n)
  27. #define TO_LITTLE_ENDIAN_32(n) (n)
  28. #else
  29. #define TO_LITTLE_ENDIAN_64 GUINT64_SWAP_LE_BE
  30. #define TO_LITTLE_ENDIAN_32 GUINT32_SWAP_LE_BE
  31. #endif
  32. /* Recommended default values */
  33. #define H_LEN CATENA_HLEN
  34. #define KEY_LEN 16
  35. const uint8_t VERSION_ID[] = "Butterfly-Full";
  36. const uint8_t LAMBDA = 4;
  37. const uint8_t GARLIC = 16;
  38. const uint8_t MIN_GARLIC = 16;
  39. /*
  40. * Hash part
  41. */
  42. static inline void
  43. __Hash1(const uint8_t *input, const uint32_t inputlen,
  44. uint8_t hash[H_LEN])
  45. {
  46. crypto_generichash_blake2b_state ctx;
  47. crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN);
  48. crypto_generichash_blake2b_update (&ctx, input, inputlen);
  49. crypto_generichash_blake2b_final (&ctx, hash, H_LEN);
  50. }
  51. /***************************************************/
  52. static inline
  53. void __Hash2(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2,
  54. const uint8_t i2len, uint8_t hash[H_LEN])
  55. {
  56. crypto_generichash_blake2b_state ctx;
  57. crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN);
  58. crypto_generichash_blake2b_update (&ctx, i1, i1len);
  59. crypto_generichash_blake2b_update (&ctx, i2, i2len);
  60. crypto_generichash_blake2b_final (&ctx, hash, H_LEN);
  61. }
  62. /***************************************************/
  63. static inline
  64. void __Hash3(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2,
  65. const uint8_t i2len, const uint8_t *i3, const uint8_t i3len,
  66. uint8_t hash[H_LEN])
  67. {
  68. crypto_generichash_blake2b_state ctx;
  69. crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN);
  70. crypto_generichash_blake2b_update (&ctx, i1, i1len);
  71. crypto_generichash_blake2b_update (&ctx, i2, i2len);
  72. crypto_generichash_blake2b_update (&ctx, i3, i3len);
  73. crypto_generichash_blake2b_final (&ctx, hash, H_LEN);
  74. }
  75. /***************************************************/
  76. static inline
  77. void __Hash4(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2,
  78. const uint8_t i2len, const uint8_t *i3, const uint8_t i3len,
  79. const uint8_t *i4, const uint8_t i4len, uint8_t hash[H_LEN])
  80. {
  81. crypto_generichash_blake2b_state ctx;
  82. crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN);
  83. crypto_generichash_blake2b_update (&ctx, i1, i1len);
  84. crypto_generichash_blake2b_update (&ctx, i2, i2len);
  85. crypto_generichash_blake2b_update (&ctx, i3, i3len);
  86. crypto_generichash_blake2b_update (&ctx, i4, i4len);
  87. crypto_generichash_blake2b_final (&ctx, hash, H_LEN);
  88. }
  89. /***************************************************/
  90. static inline
  91. void __Hash5(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2,
  92. const uint8_t i2len, const uint8_t *i3, const uint8_t i3len,
  93. const uint8_t *i4, const uint8_t i4len, const uint8_t *i5,
  94. const uint8_t i5len, uint8_t hash[H_LEN])
  95. {
  96. crypto_generichash_blake2b_state ctx;
  97. crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN);
  98. crypto_generichash_blake2b_update (&ctx, i1, i1len);
  99. crypto_generichash_blake2b_update (&ctx, i2, i2len);
  100. crypto_generichash_blake2b_update (&ctx, i3, i3len);
  101. crypto_generichash_blake2b_update (&ctx, i4, i4len);
  102. crypto_generichash_blake2b_update (&ctx, i5, i5len);
  103. crypto_generichash_blake2b_final (&ctx, hash, H_LEN);
  104. }
  105. static inline void
  106. __HashFast(int vindex, const uint8_t* i1, const uint8_t* i2,
  107. uint8_t hash[H_LEN])
  108. {
  109. __Hash2 (i1, H_LEN, i2, H_LEN, hash);
  110. }
  111. static void __ResetState(void)
  112. {
  113. }
  114. /*
  115. * Misc utils
  116. */
  117. const uint8_t ZERO8[H_LEN] = {0};
  118. /* see: http://en.wikipedia.org/wiki/Xorshift#Variations */
  119. static int p;
  120. static uint64_t s[16];
  121. static void
  122. initXSState (const uint8_t* a, const uint8_t* b)
  123. {
  124. p = 0;
  125. for (int i = 0; i < 8; i++) {
  126. s[i] = UINT64_C(0);
  127. s[i + 8] = UINT64_C(0);
  128. for (int j = 0; j < 8; j++) {
  129. s[i] |= ((uint64_t) a[i * 8 + j]) << j * 8;
  130. s[i + 8] |= ((uint64_t) b[i * 8 + j]) << j * 8;
  131. }
  132. }
  133. }
  134. static uint64_t
  135. xorshift1024star (void)
  136. {
  137. uint64_t s0 = s[p];
  138. uint64_t s1 = s[p = (p + 1) & 15];
  139. s1 ^= s1 << 31;
  140. s1 ^= s1 >> 11;
  141. s0 ^= s0 >> 30;
  142. return (s[p] = s0 ^ s1) * UINT64_C(1181783497276652981);
  143. }
  144. static void
  145. H_INIT (const uint8_t* x, const uint16_t xlen, uint8_t *vm1, uint8_t *vm2)
  146. {
  147. const uint8_t l = 2;
  148. uint8_t *tmp = (uint8_t*) g_malloc (l * H_LEN);
  149. for (uint8_t i = 0; i != l; ++i) {
  150. __Hash2 (&i, 1, x, xlen, tmp + i * H_LEN);
  151. }
  152. memcpy (vm1, tmp, H_LEN);
  153. memcpy (vm2, tmp+(l/2*H_LEN), H_LEN);
  154. g_free (tmp);
  155. }
  156. static void
  157. H_First (const uint8_t* i1, const uint8_t* i2, uint8_t* hash)
  158. {
  159. uint8_t i = 0;
  160. uint8_t *x = (uint8_t*) g_malloc (H_LEN);
  161. __ResetState ();
  162. __Hash2 (i1, H_LEN, i2, H_LEN, x);
  163. __Hash2 (&i, 1, x, H_LEN, hash);
  164. g_free (x);
  165. }
  166. static inline void
  167. initmem (const uint8_t x[H_LEN], const uint64_t c, uint8_t *r)
  168. {
  169. uint8_t *vm2 = (uint8_t*) g_malloc (H_LEN);
  170. uint8_t *vm1 = (uint8_t*) g_malloc (H_LEN);
  171. H_INIT (x, H_LEN, vm1, vm2);
  172. __ResetState ();
  173. __HashFast (0, vm1, vm2, r);
  174. __HashFast (1, r, vm1, r + H_LEN);
  175. /* Top row */
  176. for (uint64_t i = 2; i < c; i++) {
  177. __HashFast (i, r + (i - 1) * H_LEN, r + (i - 2) * H_LEN, r + i * H_LEN);
  178. }
  179. g_free (vm2);
  180. g_free (vm1);
  181. }
  182. static inline void
  183. catena_gamma (const uint8_t garlic, const uint8_t *salt,
  184. const uint8_t saltlen, uint8_t *r)
  185. {
  186. const uint64_t q = UINT64_C(1) << ((3 * garlic + 3) / 4);
  187. uint64_t i, j, j2;
  188. uint8_t *tmp = g_malloc (H_LEN);
  189. uint8_t *tmp2 = g_malloc (H_LEN);
  190. __Hash1 (salt, saltlen, tmp);
  191. __Hash1 (tmp, H_LEN, tmp2);
  192. initXSState (tmp, tmp2);
  193. __ResetState ();
  194. for (i = 0; i < q; i++) {
  195. j = xorshift1024star () >> (64 - garlic);
  196. j2 = xorshift1024star () >> (64 - garlic);
  197. __HashFast (i, r + j * H_LEN, r + j2 * H_LEN, r + j * H_LEN);
  198. }
  199. g_free (tmp);
  200. g_free (tmp2);
  201. }
  202. static void
  203. XOR (const uint8_t *input1, const uint8_t *input2, uint8_t *output)
  204. {
  205. uint32_t i;
  206. for(i = 0; i < H_LEN; i++) {
  207. output[i] = input1[i] ^ input2[i];
  208. }
  209. }
  210. /*
  211. * Butterfly part
  212. */
  213. /*
  214. * Sigma function that defines the diagonal connections of a DBG
  215. * diagonal front: flip the (g-i)th bit (Inverse Butterfly Graph)
  216. * diagonal back: flip the i-(g-1)th bit (Regular Butterfly Graph)
  217. */
  218. static uint64_t
  219. sigma(const uint8_t g, const uint64_t i, const uint64_t j)
  220. {
  221. if (i < g) {
  222. return (j ^ (UINT64_C(1) << (g - 1 - i))); /* diagonal front */
  223. }
  224. else {
  225. return (j ^ (UINT64_C(1) << (i - (g - 1)))); /* diagonal back */
  226. }
  227. }
  228. /*calculate actual index from level and element index*/
  229. static uint64_t
  230. idx(uint64_t i, uint64_t j, uint8_t co, uint64_t c, uint64_t m)
  231. {
  232. i += co;
  233. if (i % 3 == 0) {
  234. return j;
  235. }
  236. else if (i % 3 == 1) {
  237. if (j < m) {
  238. /* still fits in the array */
  239. return j + c;
  240. }
  241. else {
  242. /* start overwriting elements at the beginning */
  243. return j - m;
  244. }
  245. }
  246. /* i % 3 == 2 */
  247. return j + m;
  248. }
  249. /*
  250. * Computes the hash of x using a Double Butterfly Graph,
  251. * that forms as (2^g,\lamba)-Superconcentrator
  252. */
  253. static void
  254. Flap (const uint8_t x[H_LEN], const uint8_t lambda, const uint8_t garlic,
  255. const uint8_t *salt, const uint8_t saltlen, uint8_t h[H_LEN])
  256. {
  257. const uint64_t c = UINT64_C(1) << garlic;
  258. const uint64_t m = UINT64_C(1) << (garlic - 1); /* 0.5 * 2^g */
  259. const uint32_t l = 2 * garlic;
  260. uint8_t *r = g_malloc ((c + m) * H_LEN);
  261. uint8_t *tmp = g_malloc (H_LEN);
  262. uint64_t i, j;
  263. uint8_t k;
  264. uint8_t co = 0; /* carry over from last iteration */
  265. /* Top row */
  266. initmem (x, c, r);
  267. /*Gamma Function*/
  268. catena_gamma (garlic, salt, saltlen, r);
  269. /* DBH */
  270. for (k = 0; k < lambda; k++) {
  271. for (i = 1; i < l; i++) {
  272. XOR (r + idx (i - 1, c - 1, co, c, m) * H_LEN,
  273. r + idx (i - 1, 0, co, c, m) * H_LEN, tmp);
  274. /*
  275. * r0 := H(tmp || vsigma(g,i-1,0) )
  276. * __Hash2(tmp, H_LEN, r+idx(i-1,sigma(garlic,i-1,0),co,c,m) * H_LEN, H_LEN,
  277. * r+idx(i,0,co,c,m) *H_LEN);
  278. */
  279. H_First (tmp,
  280. r + idx (i - 1, sigma (garlic, i - 1, 0), co, c, m) * H_LEN,
  281. r + idx (i, 0, co, c, m) * H_LEN);
  282. __ResetState ();
  283. /* vertices */
  284. for (j = 1; j < c; j++) {
  285. /* tmp:= rj-1 XOR vj */
  286. XOR (r + idx (i, j - 1, co, c, m) * H_LEN,
  287. r + idx (i - 1, j, co, c, m) * H_LEN, tmp);
  288. /* rj := H(tmp || vsigma(g,i-1,j)) */
  289. __HashFast (j, tmp,
  290. r + idx (i - 1, sigma (garlic, i - 1, j), co, c, m) * H_LEN,
  291. r + idx (i, j, co, c, m) * H_LEN);
  292. }
  293. }
  294. co = (co + (i - 1)) % 3;
  295. }
  296. memcpy(h, r + idx(0,c-1,co,c,m) * H_LEN, H_LEN);
  297. g_free (r);
  298. g_free (tmp);
  299. }
  300. static int
  301. __Catena (const uint8_t *pwd, const uint32_t pwdlen,
  302. const uint8_t *salt, const uint8_t saltlen, const uint8_t *data,
  303. const uint32_t datalen, const uint8_t lambda, const uint8_t min_garlic,
  304. const uint8_t garlic, const uint8_t hashlen, const uint8_t client,
  305. const uint8_t tweak_id, uint8_t *hash)
  306. {
  307. uint8_t x[H_LEN];
  308. uint8_t hv[H_LEN];
  309. uint8_t t[4];
  310. uint8_t c;
  311. if ((hashlen > H_LEN) || (garlic > 63) || (min_garlic > garlic)
  312. || (lambda == 0) || (min_garlic == 0)) {
  313. return -1;
  314. }
  315. /*Compute H(V)*/
  316. __Hash1 (VERSION_ID, strlen ((char*) VERSION_ID), hv);
  317. /* Compute Tweak */
  318. t[0] = tweak_id;
  319. t[1] = lambda;
  320. t[2] = hashlen;
  321. t[3] = saltlen;
  322. /* Compute H(AD) */
  323. __Hash1 ((uint8_t *) data, datalen, x);
  324. /* Compute the initial value to hash */
  325. __Hash5 (hv, H_LEN, t, 4, x, H_LEN, pwd, pwdlen, salt, saltlen, x);
  326. /*Overwrite Password if enabled*/
  327. #ifdef OVERWRITE
  328. erasepwd(pwd,pwdlen);
  329. #endif
  330. Flap (x, lambda, (min_garlic + 1) / 2, salt, saltlen, x);
  331. for (c = min_garlic; c <= garlic; c++) {
  332. Flap (x, lambda, c, salt, saltlen, x);
  333. if ((c == garlic) && (client == CLIENT)) {
  334. memcpy (hash, x, H_LEN);
  335. return 0;
  336. }
  337. __Hash2 (&c, 1, x, H_LEN, x);
  338. memset (x + hashlen, 0, H_LEN - hashlen);
  339. }
  340. memcpy (hash, x, hashlen);
  341. return 0;
  342. }
  343. /***************************************************/
  344. int
  345. catena (const uint8_t *pwd, const uint32_t pwdlen, const uint8_t *salt,
  346. const uint8_t saltlen, const uint8_t *data, const uint32_t datalen,
  347. const uint8_t lambda, const uint8_t min_garlic, const uint8_t garlic,
  348. const uint8_t hashlen, uint8_t *hash)
  349. {
  350. return __Catena (pwd, pwdlen, salt, saltlen, data, datalen, lambda,
  351. min_garlic, garlic, hashlen, REGULAR, PASSWORD_HASHING_MODE, hash);
  352. }
  353. int
  354. simple_catena (const uint8_t *pwd, const uint32_t pwdlen,
  355. const uint8_t *salt, const uint8_t saltlen,
  356. const uint8_t *data, const uint32_t datalen,
  357. uint8_t hash[H_LEN])
  358. {
  359. return __Catena (pwd, pwdlen, salt, saltlen, data, datalen,
  360. LAMBDA, MIN_GARLIC, GARLIC, H_LEN,
  361. REGULAR, PASSWORD_HASHING_MODE, hash);
  362. }
  363. int
  364. catena_test (void)
  365. {
  366. /* From catena-v3.1 spec */
  367. guint8 pw[] = {0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64};
  368. guint8 salt[] = {0x73, 0x61, 0x6c, 0x74};
  369. guint8 ad[] = {0x64, 0x61,0x74, 0x61};
  370. guint8 expected[] = {
  371. 0x20, 0xc5, 0x91, 0x93, 0x8f, 0xc3, 0xaf, 0xcc, 0x3b, 0xba, 0x91, 0xd2, 0xfb,
  372. 0x84, 0xbf, 0x7b, 0x44, 0x04, 0xf9, 0x4c, 0x45, 0xed, 0x4d, 0x11, 0xa7, 0xe2,
  373. 0xb4, 0x12, 0x3e, 0xab, 0x0b, 0x77, 0x4a, 0x12, 0xb4, 0x22, 0xd0, 0xda, 0xb5,
  374. 0x25, 0x29, 0x02, 0xfc, 0x54, 0x47, 0xea, 0x82, 0x63, 0x8c, 0x1a, 0xfb, 0xa7,
  375. 0xa9, 0x94, 0x24, 0x13, 0x0e, 0x44, 0x36, 0x3b, 0x9d, 0x9f, 0xc9, 0x60
  376. };
  377. guint8 real[H_LEN];
  378. if (catena (pw, sizeof (pw), salt, sizeof (salt), ad, sizeof (ad),
  379. 4, 10, 10, H_LEN, real) != 0) {
  380. return -1;
  381. }
  382. return memcmp (real, expected, H_LEN);
  383. }