0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #ifndef _DRBG_H
0040 #define _DRBG_H
0041
0042
0043 #include <linux/random.h>
0044 #include <linux/scatterlist.h>
0045 #include <crypto/hash.h>
0046 #include <crypto/skcipher.h>
0047 #include <linux/module.h>
0048 #include <linux/crypto.h>
0049 #include <linux/slab.h>
0050 #include <crypto/internal/rng.h>
0051 #include <crypto/rng.h>
0052 #include <linux/fips.h>
0053 #include <linux/mutex.h>
0054 #include <linux/list.h>
0055 #include <linux/workqueue.h>
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 struct drbg_string {
0068 const unsigned char *buf;
0069 size_t len;
0070 struct list_head list;
0071 };
0072
0073 static inline void drbg_string_fill(struct drbg_string *string,
0074 const unsigned char *buf, size_t len)
0075 {
0076 string->buf = buf;
0077 string->len = len;
0078 INIT_LIST_HEAD(&string->list);
0079 }
0080
0081 struct drbg_state;
0082 typedef uint32_t drbg_flag_t;
0083
0084 struct drbg_core {
0085 drbg_flag_t flags;
0086 __u8 statelen;
0087 __u8 blocklen_bytes;
0088 char cra_name[CRYPTO_MAX_ALG_NAME];
0089
0090 char backend_cra_name[CRYPTO_MAX_ALG_NAME];
0091 };
0092
0093 struct drbg_state_ops {
0094 int (*update)(struct drbg_state *drbg, struct list_head *seed,
0095 int reseed);
0096 int (*generate)(struct drbg_state *drbg,
0097 unsigned char *buf, unsigned int buflen,
0098 struct list_head *addtl);
0099 int (*crypto_init)(struct drbg_state *drbg);
0100 int (*crypto_fini)(struct drbg_state *drbg);
0101
0102 };
0103
0104 struct drbg_test_data {
0105 struct drbg_string *testentropy;
0106 };
0107
0108 enum drbg_seed_state {
0109 DRBG_SEED_STATE_UNSEEDED,
0110 DRBG_SEED_STATE_PARTIAL,
0111 DRBG_SEED_STATE_FULL,
0112 };
0113
0114 struct drbg_state {
0115 struct mutex drbg_mutex;
0116 unsigned char *V;
0117 unsigned char *Vbuf;
0118
0119 unsigned char *C;
0120 unsigned char *Cbuf;
0121
0122 size_t reseed_ctr;
0123 size_t reseed_threshold;
0124
0125 unsigned char *scratchpad;
0126 unsigned char *scratchpadbuf;
0127 void *priv_data;
0128
0129 struct crypto_skcipher *ctr_handle;
0130 struct skcipher_request *ctr_req;
0131 __u8 *outscratchpadbuf;
0132 __u8 *outscratchpad;
0133 struct crypto_wait ctr_wait;
0134 struct scatterlist sg_in, sg_out;
0135
0136 enum drbg_seed_state seeded;
0137 unsigned long last_seed_time;
0138 bool pr;
0139 bool fips_primed;
0140 unsigned char *prev;
0141 struct crypto_rng *jent;
0142 const struct drbg_state_ops *d_ops;
0143 const struct drbg_core *core;
0144 struct drbg_string test_data;
0145 };
0146
0147 static inline __u8 drbg_statelen(struct drbg_state *drbg)
0148 {
0149 if (drbg && drbg->core)
0150 return drbg->core->statelen;
0151 return 0;
0152 }
0153
0154 static inline __u8 drbg_blocklen(struct drbg_state *drbg)
0155 {
0156 if (drbg && drbg->core)
0157 return drbg->core->blocklen_bytes;
0158 return 0;
0159 }
0160
0161 static inline __u8 drbg_keylen(struct drbg_state *drbg)
0162 {
0163 if (drbg && drbg->core)
0164 return (drbg->core->statelen - drbg->core->blocklen_bytes);
0165 return 0;
0166 }
0167
0168 static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
0169 {
0170
0171 return (1 << 16);
0172 }
0173
0174 static inline size_t drbg_max_addtl(struct drbg_state *drbg)
0175 {
0176
0177 #if (__BITS_PER_LONG == 32)
0178
0179
0180
0181
0182
0183 return (SIZE_MAX - 1);
0184 #else
0185 return (1UL<<35);
0186 #endif
0187 }
0188
0189 static inline size_t drbg_max_requests(struct drbg_state *drbg)
0190 {
0191
0192 return (1<<20);
0193 }
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208 static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
0209 unsigned char *outbuf, unsigned int outlen,
0210 struct drbg_string *addtl)
0211 {
0212 return crypto_rng_generate(drng, addtl->buf, addtl->len,
0213 outbuf, outlen);
0214 }
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
0234 unsigned char *outbuf, unsigned int outlen,
0235 struct drbg_string *addtl,
0236 struct drbg_test_data *test_data)
0237 {
0238 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
0239 test_data->testentropy->len);
0240 return crypto_rng_generate(drng, addtl->buf, addtl->len,
0241 outbuf, outlen);
0242 }
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258 static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
0259 struct drbg_string *pers,
0260 struct drbg_test_data *test_data)
0261 {
0262 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
0263 test_data->testentropy->len);
0264 return crypto_rng_reset(drng, pers->buf, pers->len);
0265 }
0266
0267
0268 #define DRBG_CTR ((drbg_flag_t)1<<0)
0269 #define DRBG_HMAC ((drbg_flag_t)1<<1)
0270 #define DRBG_HASH ((drbg_flag_t)1<<2)
0271 #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
0272
0273 #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
0274 #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
0275 #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
0276 #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
0277 DRBG_STRENGTH256)
0278
0279 enum drbg_prefixes {
0280 DRBG_PREFIX0 = 0x00,
0281 DRBG_PREFIX1,
0282 DRBG_PREFIX2,
0283 DRBG_PREFIX3
0284 };
0285
0286 #endif