27 #include <sys/types.h> 41 be32enc_vect(
unsigned char *dst,
const uint32_t *src,
size_t len)
45 for (i = 0; i < len / 4; i++)
46 be32enc(dst + i * 4, src[i]);
54 be32dec_vect(uint32_t *dst,
const unsigned char *src,
size_t len)
58 for (i = 0; i < len / 4; i++)
59 dst[i] = be32dec(src + i * 4);
63 #define Ch(x, y, z) ((x & (y ^ z)) ^ z) 64 #define Maj(x, y, z) ((x & (y | z)) | (y & z)) 65 #define SHR(x, n) (x >> n) 66 #define ROTR(x, n) ((x >> n) | (x << (32 - n))) 67 #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) 68 #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) 69 #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3)) 70 #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10)) 73 #define RND(a, b, c, d, e, f, g, h, k) \ 74 t0 = h + S1(e) + Ch(e, f, g) + k; \ 75 t1 = S0(a) + Maj(a, b, c); \ 80 #define RNDr(S, W, i, k) \ 81 RND(S[(64 - i) % 8], S[(65 - i) % 8], \ 82 S[(66 - i) % 8], S[(67 - i) % 8], \ 83 S[(68 - i) % 8], S[(69 - i) % 8], \ 84 S[(70 - i) % 8], S[(71 - i) % 8], \ 92 SHA256_Transform(uint32_t * state,
const unsigned char block[64])
100 be32dec_vect(W, block, 64);
101 for (i = 16; i < 64; i++)
102 W[i] =
s1(W[i - 2]) + W[i - 7] +
s0(W[i - 15]) + W[i - 16];
108 RNDr(S, W, 0, 0x428a2f98);
109 RNDr(S, W, 1, 0x71374491);
110 RNDr(S, W, 2, 0xb5c0fbcf);
111 RNDr(S, W, 3, 0xe9b5dba5);
112 RNDr(S, W, 4, 0x3956c25b);
113 RNDr(S, W, 5, 0x59f111f1);
114 RNDr(S, W, 6, 0x923f82a4);
115 RNDr(S, W, 7, 0xab1c5ed5);
116 RNDr(S, W, 8, 0xd807aa98);
117 RNDr(S, W, 9, 0x12835b01);
118 RNDr(S, W, 10, 0x243185be);
119 RNDr(S, W, 11, 0x550c7dc3);
120 RNDr(S, W, 12, 0x72be5d74);
121 RNDr(S, W, 13, 0x80deb1fe);
122 RNDr(S, W, 14, 0x9bdc06a7);
123 RNDr(S, W, 15, 0xc19bf174);
124 RNDr(S, W, 16, 0xe49b69c1);
125 RNDr(S, W, 17, 0xefbe4786);
126 RNDr(S, W, 18, 0x0fc19dc6);
127 RNDr(S, W, 19, 0x240ca1cc);
128 RNDr(S, W, 20, 0x2de92c6f);
129 RNDr(S, W, 21, 0x4a7484aa);
130 RNDr(S, W, 22, 0x5cb0a9dc);
131 RNDr(S, W, 23, 0x76f988da);
132 RNDr(S, W, 24, 0x983e5152);
133 RNDr(S, W, 25, 0xa831c66d);
134 RNDr(S, W, 26, 0xb00327c8);
135 RNDr(S, W, 27, 0xbf597fc7);
136 RNDr(S, W, 28, 0xc6e00bf3);
137 RNDr(S, W, 29, 0xd5a79147);
138 RNDr(S, W, 30, 0x06ca6351);
139 RNDr(S, W, 31, 0x14292967);
140 RNDr(S, W, 32, 0x27b70a85);
141 RNDr(S, W, 33, 0x2e1b2138);
142 RNDr(S, W, 34, 0x4d2c6dfc);
143 RNDr(S, W, 35, 0x53380d13);
144 RNDr(S, W, 36, 0x650a7354);
145 RNDr(S, W, 37, 0x766a0abb);
146 RNDr(S, W, 38, 0x81c2c92e);
147 RNDr(S, W, 39, 0x92722c85);
148 RNDr(S, W, 40, 0xa2bfe8a1);
149 RNDr(S, W, 41, 0xa81a664b);
150 RNDr(S, W, 42, 0xc24b8b70);
151 RNDr(S, W, 43, 0xc76c51a3);
152 RNDr(S, W, 44, 0xd192e819);
153 RNDr(S, W, 45, 0xd6990624);
154 RNDr(S, W, 46, 0xf40e3585);
155 RNDr(S, W, 47, 0x106aa070);
156 RNDr(S, W, 48, 0x19a4c116);
157 RNDr(S, W, 49, 0x1e376c08);
158 RNDr(S, W, 50, 0x2748774c);
159 RNDr(S, W, 51, 0x34b0bcb5);
160 RNDr(S, W, 52, 0x391c0cb3);
161 RNDr(S, W, 53, 0x4ed8aa4a);
162 RNDr(S, W, 54, 0x5b9cca4f);
163 RNDr(S, W, 55, 0x682e6ff3);
164 RNDr(S, W, 56, 0x748f82ee);
165 RNDr(S, W, 57, 0x78a5636f);
166 RNDr(S, W, 58, 0x84c87814);
167 RNDr(S, W, 59, 0x8cc70208);
168 RNDr(S, W, 60, 0x90befffa);
169 RNDr(S, W, 61, 0xa4506ceb);
170 RNDr(S, W, 62, 0xbef9a3f7);
171 RNDr(S, W, 63, 0xc67178f2);
174 for (i = 0; i < 8; i++)
183 static unsigned char PAD[64] = {
184 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
194 unsigned char len[8];
201 be32enc_vect(len, ctx->
count, 8);
204 r = (ctx->
count[1] >> 3) & 0x3f;
205 plen = (r < 56) ? (56 - r) : (120 - r);
221 ctx->
state[0] = 0x6A09E667;
222 ctx->
state[1] = 0xBB67AE85;
223 ctx->
state[2] = 0x3C6EF372;
224 ctx->
state[3] = 0xA54FF53A;
225 ctx->
state[4] = 0x510E527F;
226 ctx->
state[5] = 0x9B05688C;
227 ctx->
state[6] = 0x1F83D9AB;
228 ctx->
state[7] = 0x5BE0CD19;
237 const unsigned char *src = in;
240 r = (ctx->
count[1] >> 3) & 0x3f;
243 bitlen[1] = ((uint32_t)len) << 3;
244 bitlen[0] = (uint32_t)(len >> 29);
247 if ((ctx->
count[1] += bitlen[1]) < bitlen[1])
249 ctx->
count[0] += bitlen[0];
259 SHA256_Transform(ctx->
state, ctx->
buf);
265 SHA256_Transform(ctx->
state, src);
286 be32enc_vect(digest, ctx->
state, 32);
289 memset((
void *)ctx, 0,
sizeof(*ctx));
296 unsigned char pad[64];
297 unsigned char khash[32];
298 const unsigned char * K = _K;
312 memset(pad, 0x36, 64);
313 for (i = 0; i < Klen; i++)
319 memset(pad, 0x5c, 64);
320 for (i = 0; i < Klen; i++)
325 memset(khash, 0, 32);
341 unsigned char ihash[32];
353 memset(ihash, 0, 32);
363 size_t saltlen, uint64_t
c, uint8_t * buf,
size_t dkLen)
379 for (i = 0; i * 32 < dkLen; i++) {
381 be32enc(ivec, (uint32_t)(i + 1));
391 for (j = 2; j <=
c; j++) {
398 for (k = 0; k < 32; k++)
403 clen = dkLen - i * 32;
406 memcpy(&buf[i * 32], T, clen);
if(a.IndicesBefore(b, len, lenIndices))
void libscrypt_HMAC_SHA256_Update(HMAC_SHA256_CTX *ctx, const void *in, size_t len)
void libscrypt_HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX *ctx)
void libscrypt_SHA256_Update(SHA256_CTX *ctx, const void *in, size_t len)
void libscrypt_PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and write the output to buf.
void libscrypt_SHA256_Init(SHA256_CTX *ctx)
void * memcpy(void *a, const void *b, size_t c)
void libscrypt_SHA256_Final(unsigned char digest[32], SHA256_CTX *ctx)
void libscrypt_HMAC_SHA256_Init(HMAC_SHA256_CTX *ctx, const void *_K, size_t Klen)