2 * RNG: Random Number Generator algorithms under the crypto API
4 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <linux/crypto.h>
18 extern struct crypto_rng
*crypto_default_rng
;
20 int crypto_get_default_rng(void);
21 void crypto_put_default_rng(void);
24 * DOC: Random number generator API
26 * The random number generator API is used with the ciphers of type
27 * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
30 static inline struct crypto_rng
*__crypto_rng_cast(struct crypto_tfm
*tfm
)
32 return (struct crypto_rng
*)tfm
;
36 * crypto_alloc_rng() -- allocate RNG handle
37 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
38 * message digest cipher
39 * @type: specifies the type of the cipher
40 * @mask: specifies the mask for the cipher
42 * Allocate a cipher handle for a random number generator. The returned struct
43 * crypto_rng is the cipher handle that is required for any subsequent
44 * API invocation for that random number generator.
46 * For all random number generators, this call creates a new private copy of
47 * the random number generator that does not share a state with other
48 * instances. The only exception is the "krng" random number generator which
49 * is a kernel crypto API use case for the get_random_bytes() function of the
52 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
53 * of an error, PTR_ERR() returns the error code.
55 static inline struct crypto_rng
*crypto_alloc_rng(const char *alg_name
,
58 type
&= ~CRYPTO_ALG_TYPE_MASK
;
59 type
|= CRYPTO_ALG_TYPE_RNG
;
60 mask
|= CRYPTO_ALG_TYPE_MASK
;
62 return __crypto_rng_cast(crypto_alloc_base(alg_name
, type
, mask
));
65 static inline struct crypto_tfm
*crypto_rng_tfm(struct crypto_rng
*tfm
)
71 * crypto_rng_alg - obtain name of RNG
74 * Return the generic name (cra_name) of the initialized random number generator
76 * Return: generic name string
78 static inline struct rng_alg
*crypto_rng_alg(struct crypto_rng
*tfm
)
80 return &crypto_rng_tfm(tfm
)->__crt_alg
->cra_rng
;
83 static inline struct rng_tfm
*crypto_rng_crt(struct crypto_rng
*tfm
)
85 return &crypto_rng_tfm(tfm
)->crt_rng
;
89 * crypto_free_rng() - zeroize and free RNG handle
90 * @tfm: cipher handle to be freed
92 static inline void crypto_free_rng(struct crypto_rng
*tfm
)
94 crypto_free_tfm(crypto_rng_tfm(tfm
));
98 * crypto_rng_get_bytes() - get random number
100 * @rdata: output buffer holding the random numbers
101 * @dlen: length of the output buffer
103 * This function fills the caller-allocated buffer with random numbers using the
104 * random number generator referenced by the cipher handle.
106 * Return: 0 function was successful; < 0 if an error occurred
108 static inline int crypto_rng_get_bytes(struct crypto_rng
*tfm
,
109 u8
*rdata
, unsigned int dlen
)
111 return crypto_rng_crt(tfm
)->rng_gen_random(tfm
, rdata
, dlen
);
115 * crypto_rng_reset() - re-initialize the RNG
116 * @tfm: cipher handle
117 * @seed: seed input data
118 * @slen: length of the seed input data
120 * The reset function completely re-initializes the random number generator
121 * referenced by the cipher handle by clearing the current state. The new state
122 * is initialized with the caller provided seed or automatically, depending
123 * on the random number generator type (the ANSI X9.31 RNG requires
124 * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
125 * The seed is provided as a parameter to this function call. The provided seed
126 * should have the length of the seed size defined for the random number
127 * generator as defined by crypto_rng_seedsize.
129 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
131 static inline int crypto_rng_reset(struct crypto_rng
*tfm
,
132 u8
*seed
, unsigned int slen
)
134 return crypto_rng_crt(tfm
)->rng_reset(tfm
, seed
, slen
);
138 * crypto_rng_seedsize() - obtain seed size of RNG
139 * @tfm: cipher handle
141 * The function returns the seed size for the random number generator
142 * referenced by the cipher handle. This value may be zero if the random
143 * number generator does not implement or require a reseeding. For example,
144 * the SP800-90A DRBGs implement an automated reseeding after reaching a
145 * pre-defined threshold.
147 * Return: seed size for the random number generator
149 static inline int crypto_rng_seedsize(struct crypto_rng
*tfm
)
151 return crypto_rng_alg(tfm
)->seedsize
;