Path: blob/main/contrib/bearssl/src/rand/hmac_drbg.c
39507 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/* see bearssl.h */27void28br_hmac_drbg_init(br_hmac_drbg_context *ctx,29const br_hash_class *digest_class, const void *seed, size_t len)30{31size_t hlen;3233ctx->vtable = &br_hmac_drbg_vtable;34hlen = br_digest_size(digest_class);35memset(ctx->K, 0x00, hlen);36memset(ctx->V, 0x01, hlen);37ctx->digest_class = digest_class;38br_hmac_drbg_update(ctx, seed, len);39}4041/* see bearssl.h */42void43br_hmac_drbg_generate(br_hmac_drbg_context *ctx, void *out, size_t len)44{45const br_hash_class *dig;46br_hmac_key_context kc;47br_hmac_context hc;48size_t hlen;49unsigned char *buf;50unsigned char x;5152dig = ctx->digest_class;53hlen = br_digest_size(dig);54br_hmac_key_init(&kc, dig, ctx->K, hlen);55buf = out;56while (len > 0) {57size_t clen;5859br_hmac_init(&hc, &kc, 0);60br_hmac_update(&hc, ctx->V, hlen);61br_hmac_out(&hc, ctx->V);62clen = hlen;63if (clen > len) {64clen = len;65}66memcpy(buf, ctx->V, clen);67buf += clen;68len -= clen;69}7071/*72* To prepare the state for the next request, we should call73* br_hmac_drbg_update() with an empty additional seed. However,74* we already have an initialized HMAC context with the right75* initial key, and we don't want to push another one on the76* stack, so we inline that update() call here.77*/78br_hmac_init(&hc, &kc, 0);79br_hmac_update(&hc, ctx->V, hlen);80x = 0x00;81br_hmac_update(&hc, &x, 1);82br_hmac_out(&hc, ctx->K);83br_hmac_key_init(&kc, dig, ctx->K, hlen);84br_hmac_init(&hc, &kc, 0);85br_hmac_update(&hc, ctx->V, hlen);86br_hmac_out(&hc, ctx->V);87}8889/* see bearssl.h */90void91br_hmac_drbg_update(br_hmac_drbg_context *ctx, const void *seed, size_t len)92{93const br_hash_class *dig;94br_hmac_key_context kc;95br_hmac_context hc;96size_t hlen;97unsigned char x;9899dig = ctx->digest_class;100hlen = br_digest_size(dig);101102/*103* 1. K = HMAC(K, V || 0x00 || seed)104*/105br_hmac_key_init(&kc, dig, ctx->K, hlen);106br_hmac_init(&hc, &kc, 0);107br_hmac_update(&hc, ctx->V, hlen);108x = 0x00;109br_hmac_update(&hc, &x, 1);110br_hmac_update(&hc, seed, len);111br_hmac_out(&hc, ctx->K);112br_hmac_key_init(&kc, dig, ctx->K, hlen);113114/*115* 2. V = HMAC(K, V)116*/117br_hmac_init(&hc, &kc, 0);118br_hmac_update(&hc, ctx->V, hlen);119br_hmac_out(&hc, ctx->V);120121/*122* 3. If the additional seed is empty, then stop here.123*/124if (len == 0) {125return;126}127128/*129* 4. K = HMAC(K, V || 0x01 || seed)130*/131br_hmac_init(&hc, &kc, 0);132br_hmac_update(&hc, ctx->V, hlen);133x = 0x01;134br_hmac_update(&hc, &x, 1);135br_hmac_update(&hc, seed, len);136br_hmac_out(&hc, ctx->K);137br_hmac_key_init(&kc, dig, ctx->K, hlen);138139/*140* 5. V = HMAC(K, V)141*/142br_hmac_init(&hc, &kc, 0);143br_hmac_update(&hc, ctx->V, hlen);144br_hmac_out(&hc, ctx->V);145}146147/* see bearssl.h */148const br_prng_class br_hmac_drbg_vtable = {149sizeof(br_hmac_drbg_context),150(void (*)(const br_prng_class **, const void *, const void *, size_t))151&br_hmac_drbg_init,152(void (*)(const br_prng_class **, void *, size_t))153&br_hmac_drbg_generate,154(void (*)(const br_prng_class **, const void *, size_t))155&br_hmac_drbg_update156};157158159