1 /* $NetBSD: nist_hash_drbg.h,v 1.1 2019/09/02 20:09:29 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef NIST_HASH_DRBG_H 33 #define NIST_HASH_DRBG_H 34 35 #include <sys/types.h> 36 37 /* Instantiation: SHA-256 */ 38 39 /* 10.1 DRBG Mechanisms Based on Hash Functions, Table 2, SHA-256 column */ 40 #define NIST_SHA256_HASH_DRBG_SEEDLEN 440u 41 42 #define NIST_HASH_DRBG_SEEDLEN NIST_SHA256_HASH_DRBG_SEEDLEN 43 #define nist_hash_drbg nist_sha256_hash_drbg 44 #define nist_hash_drbg_destroy nist_sha256_hash_drbg_destroy 45 #define nist_hash_drbg_generate nist_sha256_hash_drbg_generate 46 #define nist_hash_drbg_initialize nist_sha256_hash_drbg_initialize 47 #define nist_hash_drbg_instantiate nist_sha256_hash_drbg_instantiate 48 #define nist_hash_drbg_reseed nist_sha256_hash_drbg_reseed 49 50 /* 51 * By 10.1 DRBG Mechanisms Based on Hash Functions, Table 2, the limit 52 * is <2^48 requests between reseeds. We truncate this to fit in 53 * 32-bit signed integer instead for hysterical raisins. 54 */ 55 #define NIST_HASH_DRBG_RESEED_INTERVAL 0x7fffffff 56 57 /* 10.1 DRBG Mechanisms Based on Hash Functions, Table 2 */ 58 #define NIST_HASH_DRBG_MAX_REQUEST 0x80000 59 #define NIST_HASH_DRBG_MAX_REQUEST_BYTES (NIST_HASH_DRBG_MAX_REQUEST/8) 60 61 #define NIST_HASH_DRBG_SEEDLEN_BYTES (NIST_HASH_DRBG_SEEDLEN/8) 62 63 #define NIST_HASH_DRBG_MIN_SEEDLEN_BYTES \ 64 MIN(32, NIST_HASH_DRBG_SEEDLEN_BYTES) 65 66 /* 10.1.1.1 Hash_DRBG Internal State */ 67 68 struct nist_hash_drbg { 69 uint8_t V[NIST_HASH_DRBG_SEEDLEN_BYTES]; 70 uint8_t C[NIST_HASH_DRBG_SEEDLEN_BYTES]; 71 unsigned reseed_counter; 72 }; 73 74 typedef struct nist_hash_drbg NIST_HASH_DRBG; 75 76 int nist_hash_drbg_initialize(void); /* self-test */ 77 int nist_hash_drbg_instantiate(struct nist_hash_drbg *, 78 const void *, size_t, const void *, size_t, const void *, size_t); 79 int nist_hash_drbg_reseed(struct nist_hash_drbg *, 80 const void *, size_t, const void *, size_t); 81 int nist_hash_drbg_generate(struct nist_hash_drbg *, void *, size_t, 82 const void *, size_t); 83 int nist_hash_drbg_destroy(struct nist_hash_drbg *); 84 85 #endif /* NIST_HASH_DRBG_H */ 86