^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * DRBG based on NIST SP800-90A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright Stephan Mueller <smueller@chronox.de>, 2014
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * notice, and the entire permission notice in its entirety,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * including the disclaimer of warranties.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 3. The name of the author may not be used to endorse or promote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * products derived from this software without specific prior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * ALTERNATIVELY, this product may be distributed under the terms of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * the GNU General Public License, in which case the provisions of the GPL are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * required INSTEAD OF the above restrictions. (This clause is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * necessary due to a potential bad interaction between the GPL and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * the restrictions contained in a BSD-style copyright.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #ifndef _DRBG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define _DRBG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <crypto/internal/rng.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <crypto/rng.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Concatenation Helper and string operation helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * SP800-90A requires the concatenation of different data. To avoid copying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * buffers around or allocate additional memory, the following data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * is used to point to the original memory with its size. In addition, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * is used to build a linked list. The linked list defines the concatenation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * of individual buffers. The order of memory block referenced in that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * linked list determines the order of concatenation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct drbg_string {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static inline void drbg_string_fill(struct drbg_string *string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const unsigned char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) string->buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) string->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) INIT_LIST_HEAD(&string->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct drbg_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) typedef uint32_t drbg_flag_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct drbg_core {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) drbg_flag_t flags; /* flags for the cipher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) __u8 statelen; /* maximum state length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) __u8 blocklen_bytes; /* block size of output in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* kernel crypto API backend cipher name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) char backend_cra_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct drbg_state_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int (*update)(struct drbg_state *drbg, struct list_head *seed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int reseed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int (*generate)(struct drbg_state *drbg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned char *buf, unsigned int buflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct list_head *addtl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int (*crypto_init)(struct drbg_state *drbg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int (*crypto_fini)(struct drbg_state *drbg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct drbg_test_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct drbg_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct mutex drbg_mutex; /* lock around DRBG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned char *V; /* internal state 10.1.1.1 1a) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned char *Vbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned char *C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned char *Cbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) size_t reseed_ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) size_t reseed_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* some memory the DRBG can use for its operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned char *scratchpad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unsigned char *scratchpadbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void *priv_data; /* Cipher handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct skcipher_request *ctr_req; /* CTR mode request handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) __u8 *outscratchpadbuf; /* CTR mode output scratchpad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) __u8 *outscratchpad; /* CTR mode aligned outbuf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct crypto_wait ctr_wait; /* CTR mode async wait obj */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) bool seeded; /* DRBG fully seeded? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) bool pr; /* Prediction resistance enabled? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) bool fips_primed; /* Continuous test primed? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) unsigned char *prev; /* FIPS 140-2 continuous test value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct work_struct seed_work; /* asynchronous seeding support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct crypto_rng *jent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) const struct drbg_state_ops *d_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) const struct drbg_core *core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct drbg_string test_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct random_ready_callback random_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static inline __u8 drbg_statelen(struct drbg_state *drbg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (drbg && drbg->core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return drbg->core->statelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static inline __u8 drbg_blocklen(struct drbg_state *drbg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (drbg && drbg->core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return drbg->core->blocklen_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static inline __u8 drbg_keylen(struct drbg_state *drbg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (drbg && drbg->core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return (drbg->core->statelen - drbg->core->blocklen_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* SP800-90A requires the limit 2**19 bits, but we return bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return (1 << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static inline size_t drbg_max_addtl(struct drbg_state *drbg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* SP800-90A requires 2**35 bytes additional info str / pers str */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #if (__BITS_PER_LONG == 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * SP800-90A allows smaller maximum numbers to be returned -- we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * return SIZE_MAX - 1 to allow the verification of the enforcement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * of this value in drbg_healthcheck_sanity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return (SIZE_MAX - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return (1UL<<35);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static inline size_t drbg_max_requests(struct drbg_state *drbg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* SP800-90A requires 2**48 maximum requests before reseeding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return (1<<20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * This is a wrapper to the kernel crypto API function of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * crypto_rng_generate() to allow the caller to provide additional data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * @drng DRBG handle -- see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * @outbuf output buffer -- see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * @outlen length of output buffer -- see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @addtl_input additional information string input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @addtllen length of additional information string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) unsigned char *outbuf, unsigned int outlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct drbg_string *addtl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return crypto_rng_generate(drng, addtl->buf, addtl->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) outbuf, outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * TEST code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * This is a wrapper to the kernel crypto API function of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * crypto_rng_generate() to allow the caller to provide additional data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * allow furnishing of test_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * @drng DRBG handle -- see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * @outbuf output buffer -- see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * @outlen length of output buffer -- see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * @addtl_input additional information string input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * @addtllen length of additional information string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * @test_data filled test data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * see crypto_rng_get_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unsigned char *outbuf, unsigned int outlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct drbg_string *addtl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct drbg_test_data *test_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) crypto_rng_set_entropy(drng, test_data->testentropy->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) test_data->testentropy->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return crypto_rng_generate(drng, addtl->buf, addtl->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) outbuf, outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * TEST code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * This is a wrapper to the kernel crypto API function of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * crypto_rng_reset() to allow the caller to provide test_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @drng DRBG handle -- see crypto_rng_reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @pers personalization string input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * @perslen length of additional information string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * @test_data filled test data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * see crypto_rng_reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct drbg_string *pers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct drbg_test_data *test_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) crypto_rng_set_entropy(drng, test_data->testentropy->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) test_data->testentropy->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return crypto_rng_reset(drng, pers->buf, pers->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* DRBG type flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) #define DRBG_CTR ((drbg_flag_t)1<<0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) #define DRBG_HMAC ((drbg_flag_t)1<<1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) #define DRBG_HASH ((drbg_flag_t)1<<2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* DRBG strength flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) DRBG_STRENGTH256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) enum drbg_prefixes {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) DRBG_PREFIX0 = 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) DRBG_PREFIX1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) DRBG_PREFIX2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) DRBG_PREFIX3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) #endif /* _DRBG_H */