2 * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
19 #include <kripto/memwipe.h>
20 #include <kripto/cast.h>
21 #include <kripto/loadstore.h>
22 #include <kripto/rotate.h>
23 #include <kripto/mac.h>
24 #include <kripto/pbkdf2.h>
26 #include <kripto/scrypt.h>
28 #define QR(A, B, C, D) \
30 B ^= ROL32_07(A + D); \
31 C ^= ROL32_09(B + A); \
32 D ^= ROL32_13(C + B); \
33 A ^= ROL32_18(D + C); \
36 static void salsa20_core(uint32_t *x
)
65 QR(x15
, x12
, x13
, x14
);
77 QR(x15
, x12
, x13
, x14
);
89 QR(x15
, x12
, x13
, x14
);
100 QR(x10
, x11
, x8
, x9
);
101 QR(x15
, x12
, x13
, x14
);
121 static void blockmix(uint32_t *b
, uint32_t *t
, const size_t r
)
126 memcpy(x
, b
+ (r
<< 5) - 16, 64);
128 for(i
= 0; i
< (r
<< 5);)
148 memcpy(t
+ i
- 16, x
, 64);
151 for(i
= 0; i
< r
; i
++)
153 memcpy(b
+ (i
<< 4), t
+ (i
<< 5), 64);
154 memcpy(b
+ ((i
+ r
) << 4), t
+ (i
<< 5) + 16, 64);
172 for(i
= 0; i
< (r
<< 5); i
++)
173 t1
[i
] = LOAD32L(b
+ (i
<< 2));
175 for(i
= 0; i
< n
; i
++)
177 memcpy(t0
+ (r
<< 5) * i
, t1
, r
<< 7);
181 for(i
= 0; i
< n
; i
++)
184 tn
= (((uint64_t)t1
[(r
<< 5) - 15] << 32)
188 for(j
= 0; j
< (r
<< 5); j
++)
189 t1
[j
] ^= t0
[(r
<< 5) * tn
+ j
];
194 for(i
= 0; i
< (r
<< 5); i
++)
195 STORE32L(t1
[i
], b
+ (i
<< 2));
200 const kripto_mac_desc
*mac
,
201 unsigned int mac_rounds
,
206 unsigned int pass_len
,
208 unsigned int salt_len
,
219 b
= malloc((r
<< 7) * p
+ (r
<< 7) * n
+ (r
<< 8));
222 t0
= (uint32_t *)(b
+ (r
<< 7) * p
);
239 for(i
= 0; i
< p
; i
++)
240 smix(b
+ (r
<< 7) * i
, r
, n
, t0
, t1
, t2
);
255 kripto_memwipe(b
, (r
<< 7) * p
+ (r
<< 7) * n
+ (r
<< 8));
261 kripto_memwipe(b
, (r
<< 7) * p
+ (r
<< 7) * n
+ (r
<< 8));