blob: cf94c65816b4e2676f3ea6a6ad6d2d7f1afa632d [file] [log] [blame]
nagendra modadugu4fae5422016-05-10 16:11:54 -07001// Copyright 2016 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include <string.h>
15
16#include "cryptoc/p256_prng.h"
17#include "cryptoc/hmac.h"
18#include "cryptoc/sha256.h"
19
20static void uint64tobin(uint64_t v, uint8_t* t) {
21 t[0] = (uint8_t)(v >> 56);
22 t[1] = (uint8_t)(v >> 48);
23 t[2] = (uint8_t)(v >> 40);
24 t[3] = (uint8_t)(v >> 32);
25 t[4] = (uint8_t)(v >> 24);
26 t[5] = (uint8_t)(v >> 16);
27 t[6] = (uint8_t)(v >> 8);
28 t[7] = (uint8_t)(v);
29}
30
31// V = HMAC(K, V)
32static void update_V(P256_PRNG_CTX* ctx) {
33 LITE_HMAC_CTX hmac;
34 HMAC_SHA256_init(&hmac, ctx->Key, P256_PRNG_SIZE);
35 HMAC_update(&hmac, ctx->V, P256_PRNG_SIZE);
36 memcpy(ctx->V, HMAC_final(&hmac), P256_PRNG_SIZE);
37}
38
39// K = HMAC(K, V || [0,1] || count || seed)
40static void update_Key(P256_PRNG_CTX* ctx, int which,
41 const void* seed, size_t seed_size) {
42 LITE_HMAC_CTX hmac;
43 uint8_t tmp[16 + 1];
44
45 HMAC_SHA256_init(&hmac, ctx->Key, P256_PRNG_SIZE);
46 HMAC_update(&hmac, ctx->V, P256_PRNG_SIZE);
47 tmp[0] = which;
48 uint64tobin(ctx->instance_count, tmp + 1);
49 uint64tobin(ctx->call_count, tmp + 8 + 1);
50 HMAC_update(&hmac, tmp, sizeof(tmp));
51 HMAC_update(&hmac, seed, seed_size);
52 memcpy(ctx->Key, HMAC_final(&hmac), P256_PRNG_SIZE);
53}
54
55void p256_prng_add(P256_PRNG_CTX* ctx,
56 const void* seed, size_t seed_size) {
57 update_Key(ctx, 0, seed, seed_size);
58 update_V(ctx);
59 update_Key(ctx, 1, seed, seed_size);
60 update_V(ctx);
61}
62
63void p256_prng_init(P256_PRNG_CTX* ctx,
64 const void* seed, size_t seed_size,
65 uint64_t instance_count) {
66 // Key = { 0 }, V = { 1 }
67 memset(ctx->Key, 0, P256_PRNG_SIZE);
68 memset(ctx->V, 1, P256_PRNG_SIZE);
69
70 ctx->instance_count = instance_count;
71 ctx->call_count = 0;
72
73 p256_prng_add(ctx, seed, seed_size);
74}
75
76void p256_prng_draw(P256_PRNG_CTX* ctx, uint8_t dst[P256_PRNG_SIZE]) {
77 update_V(ctx);
78
79 // Output = V
80 memcpy(dst, ctx->V, P256_PRNG_SIZE);
81
82 ctx->call_count++;
83
84 update_Key(ctx, 0, NULL, 0);
85 update_V(ctx);
86}