2 * Copyright (C) 2013 Gregor Pintar <grpintar@gmail.com>
4 * Permission is granted to deal in this work without any restriction,
5 * including unlimited rights to use, publicly perform, publish,
6 * reproduce, relicence, modify, merge, and/or distribute in any form,
7 * for any purpose, with or without fee, and by any means.
9 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind,
10 * to the utmost extent permitted by applicable law. In no event
11 * shall a licensor, author or contributor be held liable for any
12 * issues arising in any way out of dealing in the work.
20 #include <kripto/memwipe.h>
21 #include <kripto/cast.h>
22 #include <kripto/loadstore.h>
23 #include <kripto/rotate.h>
24 #include <kripto/mac.h>
25 #include <kripto/pbkdf2.h>
27 #include <kripto/scrypt.h>
29 #define QR(A, B, C, D) \
31 B ^= ROL32(A + D, 7); \
32 C ^= ROL32(B + A, 9); \
33 D ^= ROL32(C + B, 13); \
34 A ^= ROL32(D + C, 18); \
37 static void salsa20_core(uint32_t *x
)
66 QR(x15
, x12
, x13
, x14
);
78 QR(x15
, x12
, x13
, x14
);
90 QR(x15
, x12
, x13
, x14
);
101 QR(x10
, x11
, x8
, x9
);
102 QR(x15
, x12
, x13
, x14
);
122 static void blockmix(uint32_t *b
, uint32_t *t
, const size_t r
)
127 memcpy(x
, b
+ (r
<< 5) - 16, 64);
129 for(i
= 0; i
< (r
<< 5);)
149 memcpy(t
+ i
- 16, x
, 64);
152 for(i
= 0; i
< r
; i
++)
154 memcpy(b
+ (i
<< 4), t
+ (i
<< 5), 64);
155 memcpy(b
+ ((i
+ r
) << 4), t
+ (i
<< 5) + 16, 64);
173 for(i
= 0; i
< (r
<< 5); i
++)
174 t1
[i
] = LOAD32L(b
+ (i
<< 2));
176 for(i
= 0; i
< n
; i
++)
178 memcpy(t0
+ (r
<< 5) * i
, t1
, r
<< 7);
182 for(i
= 0; i
< n
; i
++)
185 tn
= (((uint64_t)t1
[(r
<< 5) - 15] << 32)
189 for(j
= 0; j
< (r
<< 5); j
++)
190 t1
[j
] ^= t0
[(r
<< 5) * tn
+ j
];
195 for(i
= 0; i
< (r
<< 5); i
++)
196 STORE32L(t1
[i
], b
+ (i
<< 2));
201 const kripto_mac_desc
*mac
,
202 unsigned int mac_rounds
,
207 unsigned int pass_len
,
209 unsigned int salt_len
,
220 b
= malloc((r
<< 7) * p
+ (r
<< 7) * n
+ (r
<< 8));
223 t0
= (uint32_t *)(b
+ (r
<< 7) * p
);
240 for(i
= 0; i
< p
; i
++)
241 smix(b
+ (r
<< 7) * i
, r
, n
, t0
, t1
, t2
);
256 kripto_memwipe(b
, (r
<< 7) * p
+ (r
<< 7) * n
+ (r
<< 8));
262 kripto_memwipe(b
, (r
<< 7) * p
+ (r
<< 7) * n
+ (r
<< 8));