packages feed

crypton-2.3.1 / cbits / crypton_rfc6979.c

/* * The HMAC_DRBG of RFC 6979 section 3.2 for SHA-224, SHA-256, SHA-384 and * SHA-512, kept in C so that a nonce costs its compressions and not a * crossing into C for each of them. */#include <stdint.h>#include <string.h>#include "crypton_bzero.h"#include "crypton_rfc6979.h"#include "crypton_sha256.h"#include "crypton_sha512.h"static void h_init(const struct crypton_rfc6979 *st, union crypton_rfc6979_ctx *c){	switch (st->alg) {	case CRYPTON_RFC6979_SHA224: crypton_sha224_init(&c->s256); break;	case CRYPTON_RFC6979_SHA256: crypton_sha256_init(&c->s256); break;	case CRYPTON_RFC6979_SHA384: crypton_sha384_init(&c->s512); break;	default: crypton_sha512_init(&c->s512); break;	}}static void h_update(const struct crypton_rfc6979 *st, union crypton_rfc6979_ctx *c,                     const uint8_t *p, uint32_t n){	switch (st->alg) {	case CRYPTON_RFC6979_SHA224: crypton_sha224_update(&c->s256, p, n); break;	case CRYPTON_RFC6979_SHA256: crypton_sha256_update(&c->s256, p, n); break;	case CRYPTON_RFC6979_SHA384: crypton_sha384_update(&c->s512, p, n); break;	default: crypton_sha512_update(&c->s512, p, n); break;	}}static void h_final(const struct crypton_rfc6979 *st, union crypton_rfc6979_ctx *c,                    uint8_t *out){	switch (st->alg) {	case CRYPTON_RFC6979_SHA224: crypton_sha224_finalize(&c->s256, out); break;	case CRYPTON_RFC6979_SHA256: crypton_sha256_finalize(&c->s256, out); break;	case CRYPTON_RFC6979_SHA384: crypton_sha384_finalize(&c->s512, out); break;	default: crypton_sha512_finalize(&c->s512, out); break;	}}/* The ipad and opad contexts of the current K. */static void rekey(struct crypton_rfc6979 *st){	uint8_t pad[128];	uint32_t i;	memset(pad, 0, st->bsize);	memcpy(pad, st->k, st->dsize);	for (i = 0; i < st->bsize; i++)		pad[i] ^= 0x36;	h_init(st, &st->ictx);	h_update(st, &st->ictx, pad, st->bsize);	for (i = 0; i < st->bsize; i++)		pad[i] ^= 0x36 ^ 0x5c;	h_init(st, &st->octx);	h_update(st, &st->octx, pad, st->bsize);	crypton_bzero(pad, sizeof(pad));}/* out = HMAC_K(a || b || c); out may be K or V. */static void mac(struct crypton_rfc6979 *st,                const uint8_t *a, uint32_t alen,                const uint8_t *b, uint32_t blen,                const uint8_t *c, uint32_t clen,                uint8_t *out){	union crypton_rfc6979_ctx w;	uint8_t inner[64];	w = st->ictx;	h_update(st, &w, a, alen);	if (blen)		h_update(st, &w, b, blen);	if (clen)		h_update(st, &w, c, clen);	h_final(st, &w, inner);	w = st->octx;	h_update(st, &w, inner, st->dsize);	h_final(st, &w, out);	crypton_bzero(&w, sizeof(w));	crypton_bzero(inner, sizeof(inner));}int crypton_rfc6979_init(struct crypton_rfc6979 *st, int alg,                         const uint8_t *seed, uint32_t seedlen){	static const uint8_t zero = 0x00, one = 0x01;	switch (alg) {	case CRYPTON_RFC6979_SHA224: st->dsize = 28; st->bsize = 64; break;	case CRYPTON_RFC6979_SHA256: st->dsize = 32; st->bsize = 64; break;	case CRYPTON_RFC6979_SHA384: st->dsize = 48; st->bsize = 128; break;	case CRYPTON_RFC6979_SHA512: st->dsize = 64; st->bsize = 128; break;	default: return -1;	}	st->alg = alg;	st->started = 0;	/* steps b to g */	memset(st->v, 0x01, st->dsize);	memset(st->k, 0x00, st->dsize);	rekey(st);	mac(st, st->v, st->dsize, &zero, 1, seed, seedlen, st->k);	rekey(st);	mac(st, st->v, st->dsize, NULL, 0, NULL, 0, st->v);	mac(st, st->v, st->dsize, &one, 1, seed, seedlen, st->k);	rekey(st);	mac(st, st->v, st->dsize, NULL, 0, NULL, 0, st->v);	return 0;}/* * The next candidate of step h: bits2int of qlen bits, big-endian in * (qlen + 7) / 8 bytes.  Every call after the first first moves past the * previous candidate, as step h.3 says. */void crypton_rfc6979_next(struct crypton_rfc6979 *st, uint8_t *out, uint32_t qlen){	static const uint8_t zero = 0x00;	uint32_t tlen = (qlen + 7) / 8, off, n, shift, i;	if (st->started) {		mac(st, st->v, st->dsize, &zero, 1, NULL, 0, st->k);		rekey(st);		mac(st, st->v, st->dsize, NULL, 0, NULL, 0, st->v);	}	st->started = 1;	for (off = 0; off < tlen; off += n) {		mac(st, st->v, st->dsize, NULL, 0, NULL, 0, st->v);		n = tlen - off < st->dsize ? tlen - off : st->dsize;		memcpy(out + off, st->v, n);	}	shift = 8 * tlen - qlen;	if (shift) {		for (i = tlen; i-- > 1; )			out[i] = (uint8_t)((out[i] >> shift) | (out[i - 1] << (8 - shift)));		out[0] >>= shift;	}}void crypton_rfc6979_wipe(struct crypton_rfc6979 *st){	crypton_bzero(st, sizeof(*st));}uint32_t crypton_rfc6979_size(void){	return sizeof(struct crypton_rfc6979);}