/*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/rng.h>50#include <crypto/rng.h>51#include <linux/fips.h>52#include <linux/mutex.h>53#include <linux/list.h>54#include <linux/workqueue.h>5556/*57* Concatenation Helper and string operation helper58*59* SP800-90A requires the concatenation of different data. To avoid copying60* buffers around or allocate additional memory, the following data structure61* is used to point to the original memory with its size. In addition, it62* is used to build a linked list. The linked list defines the concatenation63* of individual buffers. The order of memory block referenced in that64* linked list determines the order of concatenation.65*/66struct drbg_string {67const unsigned char *buf;68size_t len;69struct list_head list;70};7172static inline void drbg_string_fill(struct drbg_string *string,73const unsigned char *buf, size_t len)74{75string->buf = buf;76string->len = len;77INIT_LIST_HEAD(&string->list);78}7980struct drbg_state;81typedef uint32_t drbg_flag_t;8283struct drbg_core {84drbg_flag_t flags; /* flags for the cipher */85__u8 statelen; /* maximum state length */86__u8 blocklen_bytes; /* block size of output in bytes */87char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */88/* kernel crypto API backend cipher name */89char backend_cra_name[CRYPTO_MAX_ALG_NAME];90};9192struct drbg_state_ops {93int (*update)(struct drbg_state *drbg, struct list_head *seed,94int reseed);95int (*generate)(struct drbg_state *drbg,96unsigned char *buf, unsigned int buflen,97struct list_head *addtl);98int (*crypto_init)(struct drbg_state *drbg);99int (*crypto_fini)(struct drbg_state *drbg);100101};102103struct drbg_test_data {104struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */105};106107enum drbg_seed_state {108DRBG_SEED_STATE_UNSEEDED,109DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */110DRBG_SEED_STATE_FULL,111};112113struct drbg_state {114struct mutex drbg_mutex; /* lock around DRBG */115unsigned char *V; /* internal state 10.1.1.1 1a) */116unsigned char *Vbuf;117/* hash: static value 10.1.1.1 1b) hmac / ctr: key */118unsigned char *C;119unsigned char *Cbuf;120/* Number of RNG requests since last reseed -- 10.1.1.1 1c) */121size_t reseed_ctr;122size_t reseed_threshold;123/* some memory the DRBG can use for its operation */124unsigned char *scratchpad;125unsigned char *scratchpadbuf;126void *priv_data; /* Cipher handle */127128struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */129struct skcipher_request *ctr_req; /* CTR mode request handle */130__u8 *outscratchpadbuf; /* CTR mode output scratchpad */131__u8 *outscratchpad; /* CTR mode aligned outbuf */132struct crypto_wait ctr_wait; /* CTR mode async wait obj */133struct scatterlist sg_in, sg_out; /* CTR mode SGLs */134135enum drbg_seed_state seeded; /* DRBG fully seeded? */136unsigned long last_seed_time;137bool pr; /* Prediction resistance enabled? */138bool fips_primed; /* Continuous test primed? */139unsigned char *prev; /* FIPS 140-2 continuous test value */140struct crypto_rng *jent;141const struct drbg_state_ops *d_ops;142const struct drbg_core *core;143struct drbg_string test_data;144};145146static inline __u8 drbg_statelen(struct drbg_state *drbg)147{148if (drbg && drbg->core)149return drbg->core->statelen;150return 0;151}152153static inline __u8 drbg_blocklen(struct drbg_state *drbg)154{155if (drbg && drbg->core)156return drbg->core->blocklen_bytes;157return 0;158}159160static inline __u8 drbg_keylen(struct drbg_state *drbg)161{162if (drbg && drbg->core)163return (drbg->core->statelen - drbg->core->blocklen_bytes);164return 0;165}166167static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)168{169/* SP800-90A requires the limit 2**19 bits, but we return bytes */170return (1 << 16);171}172173static inline size_t drbg_max_addtl(struct drbg_state *drbg)174{175/* SP800-90A requires 2**35 bytes additional info str / pers str */176#if (__BITS_PER_LONG == 32)177/*178* SP800-90A allows smaller maximum numbers to be returned -- we179* return SIZE_MAX - 1 to allow the verification of the enforcement180* of this value in drbg_healthcheck_sanity.181*/182return (SIZE_MAX - 1);183#else184return (1UL<<35);185#endif186}187188static inline size_t drbg_max_requests(struct drbg_state *drbg)189{190/* SP800-90A requires 2**48 maximum requests before reseeding */191return (1<<20);192}193194/*195* This is a wrapper to the kernel crypto API function of196* crypto_rng_generate() to allow the caller to provide additional data.197*198* @drng DRBG handle -- see crypto_rng_get_bytes199* @outbuf output buffer -- see crypto_rng_get_bytes200* @outlen length of output buffer -- see crypto_rng_get_bytes201* @addtl_input additional information string input buffer202* @addtllen length of additional information string buffer203*204* return205* see crypto_rng_get_bytes206*/207static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,208unsigned char *outbuf, unsigned int outlen,209struct drbg_string *addtl)210{211return crypto_rng_generate(drng, addtl->buf, addtl->len,212outbuf, outlen);213}214215/*216* TEST code217*218* This is a wrapper to the kernel crypto API function of219* crypto_rng_generate() to allow the caller to provide additional data and220* allow furnishing of test_data221*222* @drng DRBG handle -- see crypto_rng_get_bytes223* @outbuf output buffer -- see crypto_rng_get_bytes224* @outlen length of output buffer -- see crypto_rng_get_bytes225* @addtl_input additional information string input buffer226* @addtllen length of additional information string buffer227* @test_data filled test data228*229* return230* see crypto_rng_get_bytes231*/232static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,233unsigned char *outbuf, unsigned int outlen,234struct drbg_string *addtl,235struct drbg_test_data *test_data)236{237crypto_rng_set_entropy(drng, test_data->testentropy->buf,238test_data->testentropy->len);239return crypto_rng_generate(drng, addtl->buf, addtl->len,240outbuf, outlen);241}242243/*244* TEST code245*246* This is a wrapper to the kernel crypto API function of247* crypto_rng_reset() to allow the caller to provide test_data248*249* @drng DRBG handle -- see crypto_rng_reset250* @pers personalization string input buffer251* @perslen length of additional information string buffer252* @test_data filled test data253*254* return255* see crypto_rng_reset256*/257static inline int crypto_drbg_reset_test(struct crypto_rng *drng,258struct drbg_string *pers,259struct drbg_test_data *test_data)260{261crypto_rng_set_entropy(drng, test_data->testentropy->buf,262test_data->testentropy->len);263return crypto_rng_reset(drng, pers->buf, pers->len);264}265266/* DRBG type flags */267#define DRBG_CTR ((drbg_flag_t)1<<0)268#define DRBG_HMAC ((drbg_flag_t)1<<1)269#define DRBG_HASH ((drbg_flag_t)1<<2)270#define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)271/* DRBG strength flags */272#define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)273#define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)274#define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)275#define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \276DRBG_STRENGTH256)277278enum drbg_prefixes {279DRBG_PREFIX0 = 0x00,280DRBG_PREFIX1,281DRBG_PREFIX2,282DRBG_PREFIX3283};284285#endif /* _DRBG_H */286287288