mozglue/android/pbkdf2_sha256.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mozglue/android/pbkdf2_sha256.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,432 @@
     1.4 +/*-
     1.5 + * Copyright 2005,2007,2009 Colin Percival
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + *
    1.17 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
    1.18 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.20 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.21 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.22 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.23 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.24 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.25 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.26 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.27 + * SUCH DAMAGE.
    1.28 + */
    1.29 +#include <sys/types.h>
    1.30 +
    1.31 +#include <stdint.h>
    1.32 +#include <string.h>
    1.33 +
    1.34 +#include <sys/endian.h>
    1.35 +
    1.36 +#include "pbkdf2_sha256.h"
    1.37 +
    1.38 +static inline uint32_t
    1.39 +be32dec(const void *pp)
    1.40 +{
    1.41 +	const uint8_t *p = (uint8_t const *)pp;
    1.42 +
    1.43 +	return ((uint32_t)(p[3]) +
    1.44 +		((uint32_t)(p[2]) << 8) +
    1.45 +		((uint32_t)(p[1]) << 16) +
    1.46 +		((uint32_t)(p[0]) << 24));
    1.47 +}
    1.48 +
    1.49 +static inline void
    1.50 +be32enc(void *pp, uint32_t x)
    1.51 +{
    1.52 +	uint8_t * p = (uint8_t *)pp;
    1.53 +
    1.54 +	p[3] = x & 0xff;
    1.55 +	p[2] = (x >> 8) & 0xff;
    1.56 +	p[1] = (x >> 16) & 0xff;
    1.57 +	p[0] = (x >> 24) & 0xff;
    1.58 +}
    1.59 +
    1.60 +/*
    1.61 + * Encode a length len/4 vector of (uint32_t) into a length len vector of
    1.62 + * (unsigned char) in big-endian form.  Assumes len is a multiple of 4.
    1.63 + */
    1.64 +static void
    1.65 +be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
    1.66 +{
    1.67 +	size_t i;
    1.68 +
    1.69 +	for (i = 0; i < len / 4; i++)
    1.70 +		be32enc(dst + i * 4, src[i]);
    1.71 +}
    1.72 +
    1.73 +/*
    1.74 + * Decode a big-endian length len vector of (unsigned char) into a length
    1.75 + * len/4 vector of (uint32_t).  Assumes len is a multiple of 4.
    1.76 + */
    1.77 +static void
    1.78 +be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
    1.79 +{
    1.80 +	size_t i;
    1.81 +
    1.82 +	for (i = 0; i < len / 4; i++)
    1.83 +		dst[i] = be32dec(src + i * 4);
    1.84 +}
    1.85 +
    1.86 +/* Elementary functions used by SHA256 */
    1.87 +#define Ch(x, y, z)	((x & (y ^ z)) ^ z)
    1.88 +#define Maj(x, y, z)	((x & (y | z)) | (y & z))
    1.89 +#define SHR(x, n)	(x >> n)
    1.90 +#define ROTR(x, n)	((x >> n) | (x << (32 - n)))
    1.91 +#define S0(x)		(ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
    1.92 +#define S1(x)		(ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
    1.93 +#define s0(x)		(ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
    1.94 +#define s1(x)		(ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
    1.95 +
    1.96 +/* SHA256 round function */
    1.97 +#define RND(a, b, c, d, e, f, g, h, k)			\
    1.98 +	t0 = h + S1(e) + Ch(e, f, g) + k;		\
    1.99 +	t1 = S0(a) + Maj(a, b, c);			\
   1.100 +	d += t0;					\
   1.101 +	h  = t0 + t1;
   1.102 +
   1.103 +/* Adjusted round function for rotating state */
   1.104 +#define RNDr(S, W, i, k)			\
   1.105 +	RND(S[(64 - i) % 8], S[(65 - i) % 8],	\
   1.106 +	    S[(66 - i) % 8], S[(67 - i) % 8],	\
   1.107 +	    S[(68 - i) % 8], S[(69 - i) % 8],	\
   1.108 +	    S[(70 - i) % 8], S[(71 - i) % 8],	\
   1.109 +	    W[i] + k)
   1.110 +
   1.111 +/*
   1.112 + * SHA256 block compression function.  The 256-bit state is transformed via
   1.113 + * the 512-bit input block to produce a new state.
   1.114 + */
   1.115 +static void
   1.116 +SHA256_Transform(uint32_t * state, const unsigned char block[64])
   1.117 +{
   1.118 +	uint32_t W[64];
   1.119 +	uint32_t S[8];
   1.120 +	uint32_t t0, t1;
   1.121 +	int i;
   1.122 +
   1.123 +	/* 1. Prepare message schedule W. */
   1.124 +	be32dec_vect(W, block, 64);
   1.125 +	for (i = 16; i < 64; i++)
   1.126 +		W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
   1.127 +
   1.128 +	/* 2. Initialize working variables. */
   1.129 +	memcpy(S, state, 32);
   1.130 +
   1.131 +	/* 3. Mix. */
   1.132 +	RNDr(S, W, 0, 0x428a2f98);
   1.133 +	RNDr(S, W, 1, 0x71374491);
   1.134 +	RNDr(S, W, 2, 0xb5c0fbcf);
   1.135 +	RNDr(S, W, 3, 0xe9b5dba5);
   1.136 +	RNDr(S, W, 4, 0x3956c25b);
   1.137 +	RNDr(S, W, 5, 0x59f111f1);
   1.138 +	RNDr(S, W, 6, 0x923f82a4);
   1.139 +	RNDr(S, W, 7, 0xab1c5ed5);
   1.140 +	RNDr(S, W, 8, 0xd807aa98);
   1.141 +	RNDr(S, W, 9, 0x12835b01);
   1.142 +	RNDr(S, W, 10, 0x243185be);
   1.143 +	RNDr(S, W, 11, 0x550c7dc3);
   1.144 +	RNDr(S, W, 12, 0x72be5d74);
   1.145 +	RNDr(S, W, 13, 0x80deb1fe);
   1.146 +	RNDr(S, W, 14, 0x9bdc06a7);
   1.147 +	RNDr(S, W, 15, 0xc19bf174);
   1.148 +	RNDr(S, W, 16, 0xe49b69c1);
   1.149 +	RNDr(S, W, 17, 0xefbe4786);
   1.150 +	RNDr(S, W, 18, 0x0fc19dc6);
   1.151 +	RNDr(S, W, 19, 0x240ca1cc);
   1.152 +	RNDr(S, W, 20, 0x2de92c6f);
   1.153 +	RNDr(S, W, 21, 0x4a7484aa);
   1.154 +	RNDr(S, W, 22, 0x5cb0a9dc);
   1.155 +	RNDr(S, W, 23, 0x76f988da);
   1.156 +	RNDr(S, W, 24, 0x983e5152);
   1.157 +	RNDr(S, W, 25, 0xa831c66d);
   1.158 +	RNDr(S, W, 26, 0xb00327c8);
   1.159 +	RNDr(S, W, 27, 0xbf597fc7);
   1.160 +	RNDr(S, W, 28, 0xc6e00bf3);
   1.161 +	RNDr(S, W, 29, 0xd5a79147);
   1.162 +	RNDr(S, W, 30, 0x06ca6351);
   1.163 +	RNDr(S, W, 31, 0x14292967);
   1.164 +	RNDr(S, W, 32, 0x27b70a85);
   1.165 +	RNDr(S, W, 33, 0x2e1b2138);
   1.166 +	RNDr(S, W, 34, 0x4d2c6dfc);
   1.167 +	RNDr(S, W, 35, 0x53380d13);
   1.168 +	RNDr(S, W, 36, 0x650a7354);
   1.169 +	RNDr(S, W, 37, 0x766a0abb);
   1.170 +	RNDr(S, W, 38, 0x81c2c92e);
   1.171 +	RNDr(S, W, 39, 0x92722c85);
   1.172 +	RNDr(S, W, 40, 0xa2bfe8a1);
   1.173 +	RNDr(S, W, 41, 0xa81a664b);
   1.174 +	RNDr(S, W, 42, 0xc24b8b70);
   1.175 +	RNDr(S, W, 43, 0xc76c51a3);
   1.176 +	RNDr(S, W, 44, 0xd192e819);
   1.177 +	RNDr(S, W, 45, 0xd6990624);
   1.178 +	RNDr(S, W, 46, 0xf40e3585);
   1.179 +	RNDr(S, W, 47, 0x106aa070);
   1.180 +	RNDr(S, W, 48, 0x19a4c116);
   1.181 +	RNDr(S, W, 49, 0x1e376c08);
   1.182 +	RNDr(S, W, 50, 0x2748774c);
   1.183 +	RNDr(S, W, 51, 0x34b0bcb5);
   1.184 +	RNDr(S, W, 52, 0x391c0cb3);
   1.185 +	RNDr(S, W, 53, 0x4ed8aa4a);
   1.186 +	RNDr(S, W, 54, 0x5b9cca4f);
   1.187 +	RNDr(S, W, 55, 0x682e6ff3);
   1.188 +	RNDr(S, W, 56, 0x748f82ee);
   1.189 +	RNDr(S, W, 57, 0x78a5636f);
   1.190 +	RNDr(S, W, 58, 0x84c87814);
   1.191 +	RNDr(S, W, 59, 0x8cc70208);
   1.192 +	RNDr(S, W, 60, 0x90befffa);
   1.193 +	RNDr(S, W, 61, 0xa4506ceb);
   1.194 +	RNDr(S, W, 62, 0xbef9a3f7);
   1.195 +	RNDr(S, W, 63, 0xc67178f2);
   1.196 +
   1.197 +	/* 4. Mix local working variables into global state. */
   1.198 +	for (i = 0; i < 8; i++)
   1.199 +		state[i] += S[i];
   1.200 +
   1.201 +	/* Clean the stack. */
   1.202 +	memset(W, 0, 256);
   1.203 +	memset(S, 0, 32);
   1.204 +	t0 = t1 = 0;
   1.205 +}
   1.206 +
   1.207 +static unsigned char PAD[64] = {
   1.208 +	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.209 +	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.210 +	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.211 +	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   1.212 +};
   1.213 +
   1.214 +/* Add padding and terminating bit-count. */
   1.215 +static void
   1.216 +SHA256_Pad(SHA256_CTX * ctx)
   1.217 +{
   1.218 +	unsigned char len[8];
   1.219 +	uint32_t r, plen;
   1.220 +
   1.221 +	/*
   1.222 +	 * Convert length to a vector of bytes -- we do this now rather
   1.223 +	 * than later because the length will change after we pad.
   1.224 +	 */
   1.225 +	be32enc_vect(len, ctx->count, 8);
   1.226 +
   1.227 +	/* Add 1--64 bytes so that the resulting length is 56 mod 64. */
   1.228 +	r = (ctx->count[1] >> 3) & 0x3f;
   1.229 +	plen = (r < 56) ? (56 - r) : (120 - r);
   1.230 +	SHA256_Update(ctx, PAD, (size_t)plen);
   1.231 +
   1.232 +	/* Add the terminating bit-count. */
   1.233 +	SHA256_Update(ctx, len, 8);
   1.234 +}
   1.235 +
   1.236 +/* SHA-256 initialization.  Begins a SHA-256 operation. */
   1.237 +void
   1.238 +SHA256_Init(SHA256_CTX * ctx)
   1.239 +{
   1.240 +
   1.241 +	/* Zero bits processed so far. */
   1.242 +	ctx->count[0] = ctx->count[1] = 0;
   1.243 +
   1.244 +	/* Magic initialization constants. */
   1.245 +	ctx->state[0] = 0x6A09E667;
   1.246 +	ctx->state[1] = 0xBB67AE85;
   1.247 +	ctx->state[2] = 0x3C6EF372;
   1.248 +	ctx->state[3] = 0xA54FF53A;
   1.249 +	ctx->state[4] = 0x510E527F;
   1.250 +	ctx->state[5] = 0x9B05688C;
   1.251 +	ctx->state[6] = 0x1F83D9AB;
   1.252 +	ctx->state[7] = 0x5BE0CD19;
   1.253 +}
   1.254 +
   1.255 +/* Add bytes into the hash. */
   1.256 +void
   1.257 +SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
   1.258 +{
   1.259 +	uint32_t bitlen[2];
   1.260 +	uint32_t r;
   1.261 +	const unsigned char *src = in;
   1.262 +
   1.263 +	/* Number of bytes left in the buffer from previous updates. */
   1.264 +	r = (ctx->count[1] >> 3) & 0x3f;
   1.265 +
   1.266 +	/* Convert the length into a number of bits. */
   1.267 +	bitlen[1] = ((uint32_t)len) << 3;
   1.268 +	bitlen[0] = (uint32_t)(len >> 29);
   1.269 +
   1.270 +	/* Update number of bits. */
   1.271 +	if ((ctx->count[1] += bitlen[1]) < bitlen[1])
   1.272 +		ctx->count[0]++;
   1.273 +	ctx->count[0] += bitlen[0];
   1.274 +
   1.275 +	/* Handle the case where we don't need to perform any transforms. */
   1.276 +	if (len < 64 - r) {
   1.277 +		memcpy(&ctx->buf[r], src, len);
   1.278 +		return;
   1.279 +	}
   1.280 +
   1.281 +	/* Finish the current block. */
   1.282 +	memcpy(&ctx->buf[r], src, 64 - r);
   1.283 +	SHA256_Transform(ctx->state, ctx->buf);
   1.284 +	src += 64 - r;
   1.285 +	len -= 64 - r;
   1.286 +
   1.287 +	/* Perform complete blocks. */
   1.288 +	while (len >= 64) {
   1.289 +		SHA256_Transform(ctx->state, src);
   1.290 +		src += 64;
   1.291 +		len -= 64;
   1.292 +	}
   1.293 +
   1.294 +	/* Copy left over data into buffer. */
   1.295 +	memcpy(ctx->buf, src, len);
   1.296 +}
   1.297 +
   1.298 +/*
   1.299 + * SHA-256 finalization.  Pads the input data, exports the hash value,
   1.300 + * and clears the context state.
   1.301 + */
   1.302 +void
   1.303 +SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
   1.304 +{
   1.305 +
   1.306 +	/* Add padding. */
   1.307 +	SHA256_Pad(ctx);
   1.308 +
   1.309 +	/* Write the hash. */
   1.310 +	be32enc_vect(digest, ctx->state, 32);
   1.311 +
   1.312 +	/* Clear the context state. */
   1.313 +	memset((void *)ctx, 0, sizeof(*ctx));
   1.314 +}
   1.315 +
   1.316 +/* Initialize an HMAC-SHA256 operation with the given key. */
   1.317 +void
   1.318 +HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
   1.319 +{
   1.320 +	unsigned char pad[64];
   1.321 +	unsigned char khash[32];
   1.322 +	const unsigned char * K = _K;
   1.323 +	size_t i;
   1.324 +
   1.325 +	/* If Klen > 64, the key is really SHA256(K). */
   1.326 +	if (Klen > 64) {
   1.327 +		SHA256_Init(&ctx->ictx);
   1.328 +		SHA256_Update(&ctx->ictx, K, Klen);
   1.329 +		SHA256_Final(khash, &ctx->ictx);
   1.330 +		K = khash;
   1.331 +		Klen = 32;
   1.332 +	}
   1.333 +
   1.334 +	/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
   1.335 +	SHA256_Init(&ctx->ictx);
   1.336 +	memset(pad, 0x36, 64);
   1.337 +	for (i = 0; i < Klen; i++)
   1.338 +		pad[i] ^= K[i];
   1.339 +	SHA256_Update(&ctx->ictx, pad, 64);
   1.340 +
   1.341 +	/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
   1.342 +	SHA256_Init(&ctx->octx);
   1.343 +	memset(pad, 0x5c, 64);
   1.344 +	for (i = 0; i < Klen; i++)
   1.345 +		pad[i] ^= K[i];
   1.346 +	SHA256_Update(&ctx->octx, pad, 64);
   1.347 +
   1.348 +	/* Clean the stack. */
   1.349 +	memset(khash, 0, 32);
   1.350 +}
   1.351 +
   1.352 +/* Add bytes to the HMAC-SHA256 operation. */
   1.353 +void
   1.354 +HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void *in, size_t len)
   1.355 +{
   1.356 +
   1.357 +	/* Feed data to the inner SHA256 operation. */
   1.358 +	SHA256_Update(&ctx->ictx, in, len);
   1.359 +}
   1.360 +
   1.361 +/* Finish an HMAC-SHA256 operation. */
   1.362 +void
   1.363 +HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx)
   1.364 +{
   1.365 +	unsigned char ihash[32];
   1.366 +
   1.367 +	/* Finish the inner SHA256 operation. */
   1.368 +	SHA256_Final(ihash, &ctx->ictx);
   1.369 +
   1.370 +	/* Feed the inner hash to the outer SHA256 operation. */
   1.371 +	SHA256_Update(&ctx->octx, ihash, 32);
   1.372 +
   1.373 +	/* Finish the outer SHA256 operation. */
   1.374 +	SHA256_Final(digest, &ctx->octx);
   1.375 +
   1.376 +	/* Clean the stack. */
   1.377 +	memset(ihash, 0, 32);
   1.378 +}
   1.379 +
   1.380 +/**
   1.381 + * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
   1.382 + * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
   1.383 + * write the output to buf.  The value dkLen must be at most 32 * (2^32 - 1).
   1.384 + */
   1.385 +void
   1.386 +PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
   1.387 +    size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
   1.388 +{
   1.389 +	HMAC_SHA256_CTX PShctx, hctx;
   1.390 +	size_t i;
   1.391 +	uint8_t ivec[4];
   1.392 +	uint8_t U[32];
   1.393 +	uint8_t T[32];
   1.394 +	uint64_t j;
   1.395 +	int k;
   1.396 +	size_t clen;
   1.397 +
   1.398 +	/* Compute HMAC state after processing P and S. */
   1.399 +	HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
   1.400 +	HMAC_SHA256_Update(&PShctx, salt, saltlen);
   1.401 +
   1.402 +	/* Iterate through the blocks. */
   1.403 +	for (i = 0; i * 32 < dkLen; i++) {
   1.404 +		/* Generate INT(i + 1). */
   1.405 +		be32enc(ivec, (uint32_t)(i + 1));
   1.406 +
   1.407 +		/* Compute U_1 = PRF(P, S || INT(i)). */
   1.408 +		memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
   1.409 +		HMAC_SHA256_Update(&hctx, ivec, 4);
   1.410 +		HMAC_SHA256_Final(U, &hctx);
   1.411 +
   1.412 +		/* T_i = U_1 ... */
   1.413 +		memcpy(T, U, 32);
   1.414 +
   1.415 +		for (j = 2; j <= c; j++) {
   1.416 +			/* Compute U_j. */
   1.417 +			HMAC_SHA256_Init(&hctx, passwd, passwdlen);
   1.418 +			HMAC_SHA256_Update(&hctx, U, 32);
   1.419 +			HMAC_SHA256_Final(U, &hctx);
   1.420 +
   1.421 +			/* ... xor U_j ... */
   1.422 +			for (k = 0; k < 32; k++)
   1.423 +				T[k] ^= U[k];
   1.424 +		}
   1.425 +
   1.426 +		/* Copy as many bytes as necessary into buf. */
   1.427 +		clen = dkLen - i * 32;
   1.428 +		if (clen > 32)
   1.429 +			clen = 32;
   1.430 +		memcpy(&buf[i * 32], T, clen);
   1.431 +	}
   1.432 +
   1.433 +	/* Clean PShctx, since we never called _Final on it. */
   1.434 +	memset(&PShctx, 0, sizeof(HMAC_SHA256_CTX));
   1.435 +}

mercurial