]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/crypto/rng.h
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>
19 int (*generate
)(struct crypto_rng
*tfm
,
20 const u8
*src
, unsigned int slen
,
21 u8
*dst
, unsigned int dlen
);
22 int (*seed
)(struct crypto_rng
*tfm
, const u8
*seed
, unsigned int slen
);
23 struct crypto_tfm base
;
26 extern struct crypto_rng
*crypto_default_rng
;
28 int crypto_get_default_rng(void);
29 void crypto_put_default_rng(void);
32 * DOC: Random number generator API
34 * The random number generator API is used with the ciphers of type
35 * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
39 * crypto_alloc_rng() -- allocate RNG handle
40 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
41 * message digest cipher
42 * @type: specifies the type of the cipher
43 * @mask: specifies the mask for the cipher
45 * Allocate a cipher handle for a random number generator. The returned struct
46 * crypto_rng is the cipher handle that is required for any subsequent
47 * API invocation for that random number generator.
49 * For all random number generators, this call creates a new private copy of
50 * the random number generator that does not share a state with other
51 * instances. The only exception is the "krng" random number generator which
52 * is a kernel crypto API use case for the get_random_bytes() function of the
55 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
56 * of an error, PTR_ERR() returns the error code.
58 struct crypto_rng
*crypto_alloc_rng(const char *alg_name
, u32 type
, u32 mask
);
60 static inline struct crypto_tfm
*crypto_rng_tfm(struct crypto_rng
*tfm
)
66 * crypto_rng_alg - obtain name of RNG
69 * Return the generic name (cra_name) of the initialized random number generator
71 * Return: generic name string
73 static inline struct rng_alg
*crypto_rng_alg(struct crypto_rng
*tfm
)
75 return &crypto_rng_tfm(tfm
)->__crt_alg
->cra_rng
;
79 * crypto_free_rng() - zeroize and free RNG handle
80 * @tfm: cipher handle to be freed
82 static inline void crypto_free_rng(struct crypto_rng
*tfm
)
84 crypto_destroy_tfm(tfm
, crypto_rng_tfm(tfm
));
88 * crypto_rng_generate() - get random number
90 * @src: Input buffer holding additional data, may be NULL
91 * @slen: Length of additional data
92 * @dst: output buffer holding the random numbers
93 * @dlen: length of the output buffer
95 * This function fills the caller-allocated buffer with random
96 * numbers using the random number generator referenced by the
99 * Return: 0 function was successful; < 0 if an error occurred
101 static inline int crypto_rng_generate(struct crypto_rng
*tfm
,
102 const u8
*src
, unsigned int slen
,
103 u8
*dst
, unsigned int dlen
)
105 return tfm
->generate(tfm
, src
, slen
, dst
, dlen
);
109 * crypto_rng_get_bytes() - get random number
110 * @tfm: cipher handle
111 * @rdata: output buffer holding the random numbers
112 * @dlen: length of the output buffer
114 * This function fills the caller-allocated buffer with random numbers using the
115 * random number generator referenced by the cipher handle.
117 * Return: 0 function was successful; < 0 if an error occurred
119 static inline int crypto_rng_get_bytes(struct crypto_rng
*tfm
,
120 u8
*rdata
, unsigned int dlen
)
122 return crypto_rng_generate(tfm
, NULL
, 0, rdata
, dlen
);
126 * crypto_rng_reset() - re-initialize the RNG
127 * @tfm: cipher handle
128 * @seed: seed input data
129 * @slen: length of the seed input data
131 * The reset function completely re-initializes the random number generator
132 * referenced by the cipher handle by clearing the current state. The new state
133 * is initialized with the caller provided seed or automatically, depending
134 * on the random number generator type (the ANSI X9.31 RNG requires
135 * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
136 * The seed is provided as a parameter to this function call. The provided seed
137 * should have the length of the seed size defined for the random number
138 * generator as defined by crypto_rng_seedsize.
140 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
142 int crypto_rng_reset(struct crypto_rng
*tfm
, const u8
*seed
,
146 * crypto_rng_seedsize() - obtain seed size of RNG
147 * @tfm: cipher handle
149 * The function returns the seed size for the random number generator
150 * referenced by the cipher handle. This value may be zero if the random
151 * number generator does not implement or require a reseeding. For example,
152 * the SP800-90A DRBGs implement an automated reseeding after reaching a
153 * pre-defined threshold.
155 * Return: seed size for the random number generator
157 static inline int crypto_rng_seedsize(struct crypto_rng
*tfm
)
159 return crypto_rng_alg(tfm
)->seedsize
;