/*1* DRBG based on NIST SP800-90A2*3* Copyright Stephan Mueller <[email protected]>, 20144*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, and the entire permission notice in its entirety,10* including the disclaimer of warranties.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. The name of the author may not be used to endorse or promote15* products derived from this software without specific prior16* written permission.17*18* ALTERNATIVELY, this product may be distributed under the terms of19* the GNU General Public License, in which case the provisions of the GPL are20* required INSTEAD OF the above restrictions. (This clause is21* necessary due to a potential bad interaction between the GPL and22* the restrictions contained in a BSD-style copyright.)23*24* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED25* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES26* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF27* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE28* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT30* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR31* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF32* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT33* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE34* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH35* DAMAGE.36*/3738#ifndef _DRBG_H39#define _DRBG_H404142#include <linux/random.h>43#include <linux/scatterlist.h>44#include <crypto/hash.h>45#include <crypto/skcipher.h>46#include <linux/module.h>47#include <linux/crypto.h>48#include <linux/slab.h>49#include <crypto/internal/drbg.h>50#include <crypto/internal/rng.h>51#include <crypto/rng.h>52#include <linux/fips.h>53#include <linux/mutex.h>54#include <linux/list.h>55#include <linux/workqueue.h>5657struct drbg_state;58typedef uint32_t drbg_flag_t;5960struct drbg_core {61drbg_flag_t flags; /* flags for the cipher */62__u8 statelen; /* maximum state length */63__u8 blocklen_bytes; /* block size of output in bytes */64char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */65/* kernel crypto API backend cipher name */66char backend_cra_name[CRYPTO_MAX_ALG_NAME];67};6869struct drbg_state_ops {70int (*update)(struct drbg_state *drbg, struct list_head *seed,71int reseed);72int (*generate)(struct drbg_state *drbg,73unsigned char *buf, unsigned int buflen,74struct list_head *addtl);75int (*crypto_init)(struct drbg_state *drbg);76int (*crypto_fini)(struct drbg_state *drbg);7778};7980struct drbg_test_data {81struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */82};8384enum drbg_seed_state {85DRBG_SEED_STATE_UNSEEDED,86DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */87DRBG_SEED_STATE_FULL,88};8990struct drbg_state {91struct mutex drbg_mutex; /* lock around DRBG */92unsigned char *V; /* internal state 10.1.1.1 1a) */93unsigned char *Vbuf;94/* hash: static value 10.1.1.1 1b) hmac / ctr: key */95unsigned char *C;96unsigned char *Cbuf;97/* Number of RNG requests since last reseed -- 10.1.1.1 1c) */98size_t reseed_ctr;99size_t reseed_threshold;100/* some memory the DRBG can use for its operation */101unsigned char *scratchpad;102unsigned char *scratchpadbuf;103void *priv_data; /* Cipher handle */104105struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */106struct skcipher_request *ctr_req; /* CTR mode request handle */107__u8 *outscratchpadbuf; /* CTR mode output scratchpad */108__u8 *outscratchpad; /* CTR mode aligned outbuf */109struct crypto_wait ctr_wait; /* CTR mode async wait obj */110struct scatterlist sg_in, sg_out; /* CTR mode SGLs */111112enum drbg_seed_state seeded; /* DRBG fully seeded? */113unsigned long last_seed_time;114bool pr; /* Prediction resistance enabled? */115bool fips_primed; /* Continuous test primed? */116unsigned char *prev; /* FIPS 140-2 continuous test value */117struct crypto_rng *jent;118const struct drbg_state_ops *d_ops;119const struct drbg_core *core;120struct drbg_string test_data;121};122123static inline __u8 drbg_statelen(struct drbg_state *drbg)124{125if (drbg && drbg->core)126return drbg->core->statelen;127return 0;128}129130static inline __u8 drbg_blocklen(struct drbg_state *drbg)131{132if (drbg && drbg->core)133return drbg->core->blocklen_bytes;134return 0;135}136137static inline __u8 drbg_keylen(struct drbg_state *drbg)138{139if (drbg && drbg->core)140return (drbg->core->statelen - drbg->core->blocklen_bytes);141return 0;142}143144static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)145{146/* SP800-90A requires the limit 2**19 bits, but we return bytes */147return (1 << 16);148}149150static inline size_t drbg_max_addtl(struct drbg_state *drbg)151{152/* SP800-90A requires 2**35 bytes additional info str / pers str */153#if (__BITS_PER_LONG == 32)154/*155* SP800-90A allows smaller maximum numbers to be returned -- we156* return SIZE_MAX - 1 to allow the verification of the enforcement157* of this value in drbg_healthcheck_sanity.158*/159return (SIZE_MAX - 1);160#else161return (1UL<<35);162#endif163}164165static inline size_t drbg_max_requests(struct drbg_state *drbg)166{167/* SP800-90A requires 2**48 maximum requests before reseeding */168return (1<<20);169}170171/*172* This is a wrapper to the kernel crypto API function of173* crypto_rng_generate() to allow the caller to provide additional data.174*175* @drng DRBG handle -- see crypto_rng_get_bytes176* @outbuf output buffer -- see crypto_rng_get_bytes177* @outlen length of output buffer -- see crypto_rng_get_bytes178* @addtl_input additional information string input buffer179* @addtllen length of additional information string buffer180*181* return182* see crypto_rng_get_bytes183*/184static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,185unsigned char *outbuf, unsigned int outlen,186struct drbg_string *addtl)187{188return crypto_rng_generate(drng, addtl->buf, addtl->len,189outbuf, outlen);190}191192/*193* TEST code194*195* This is a wrapper to the kernel crypto API function of196* crypto_rng_generate() to allow the caller to provide additional data and197* allow furnishing of test_data198*199* @drng DRBG handle -- see crypto_rng_get_bytes200* @outbuf output buffer -- see crypto_rng_get_bytes201* @outlen length of output buffer -- see crypto_rng_get_bytes202* @addtl_input additional information string input buffer203* @addtllen length of additional information string buffer204* @test_data filled test data205*206* return207* see crypto_rng_get_bytes208*/209static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,210unsigned char *outbuf, unsigned int outlen,211struct drbg_string *addtl,212struct drbg_test_data *test_data)213{214crypto_rng_set_entropy(drng, test_data->testentropy->buf,215test_data->testentropy->len);216return crypto_rng_generate(drng, addtl->buf, addtl->len,217outbuf, outlen);218}219220/*221* TEST code222*223* This is a wrapper to the kernel crypto API function of224* crypto_rng_reset() to allow the caller to provide test_data225*226* @drng DRBG handle -- see crypto_rng_reset227* @pers personalization string input buffer228* @perslen length of additional information string buffer229* @test_data filled test data230*231* return232* see crypto_rng_reset233*/234static inline int crypto_drbg_reset_test(struct crypto_rng *drng,235struct drbg_string *pers,236struct drbg_test_data *test_data)237{238crypto_rng_set_entropy(drng, test_data->testentropy->buf,239test_data->testentropy->len);240return crypto_rng_reset(drng, pers->buf, pers->len);241}242243/* DRBG type flags */244#define DRBG_CTR ((drbg_flag_t)1<<0)245#define DRBG_HMAC ((drbg_flag_t)1<<1)246#define DRBG_HASH ((drbg_flag_t)1<<2)247#define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)248/* DRBG strength flags */249#define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)250#define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)251#define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)252#define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \253DRBG_STRENGTH256)254255enum drbg_prefixes {256DRBG_PREFIX0 = 0x00,257DRBG_PREFIX1,258DRBG_PREFIX2,259DRBG_PREFIX3260};261262#endif /* _DRBG_H */263264265