]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/crypto/rng.h
crypto: rng - Mark crypto_rng_reset seed as const
[mirror_ubuntu-bionic-kernel.git] / include / crypto / rng.h
1 /*
2 * RNG: Random Number Generator algorithms under the crypto API
3 *
4 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
5 *
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)
9 * any later version.
10 *
11 */
12
13 #ifndef _CRYPTO_RNG_H
14 #define _CRYPTO_RNG_H
15
16 #include <linux/crypto.h>
17
18 struct crypto_rng {
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;
24 };
25
26 extern struct crypto_rng *crypto_default_rng;
27
28 int crypto_get_default_rng(void);
29 void crypto_put_default_rng(void);
30
31 /**
32 * DOC: Random number generator API
33 *
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)
36 */
37
38 /**
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
44 *
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.
48 *
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
53 * /dev/random driver.
54 *
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.
57 */
58 struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
59
60 static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
61 {
62 return &tfm->base;
63 }
64
65 /**
66 * crypto_rng_alg - obtain name of RNG
67 * @tfm: cipher handle
68 *
69 * Return the generic name (cra_name) of the initialized random number generator
70 *
71 * Return: generic name string
72 */
73 static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
74 {
75 return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
76 }
77
78 /**
79 * crypto_free_rng() - zeroize and free RNG handle
80 * @tfm: cipher handle to be freed
81 */
82 static inline void crypto_free_rng(struct crypto_rng *tfm)
83 {
84 crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
85 }
86
87 /**
88 * crypto_rng_generate() - get random number
89 * @tfm: cipher handle
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
94 *
95 * This function fills the caller-allocated buffer with random
96 * numbers using the random number generator referenced by the
97 * cipher handle.
98 *
99 * Return: 0 function was successful; < 0 if an error occurred
100 */
101 static inline int crypto_rng_generate(struct crypto_rng *tfm,
102 const u8 *src, unsigned int slen,
103 u8 *dst, unsigned int dlen)
104 {
105 return tfm->generate(tfm, src, slen, dst, dlen);
106 }
107
108 /**
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
113 *
114 * This function fills the caller-allocated buffer with random numbers using the
115 * random number generator referenced by the cipher handle.
116 *
117 * Return: 0 function was successful; < 0 if an error occurred
118 */
119 static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
120 u8 *rdata, unsigned int dlen)
121 {
122 return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
123 }
124
125 /**
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
130 *
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.
139 *
140 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
141 */
142 int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
143 unsigned int slen);
144
145 /**
146 * crypto_rng_seedsize() - obtain seed size of RNG
147 * @tfm: cipher handle
148 *
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.
154 *
155 * Return: seed size for the random number generator
156 */
157 static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
158 {
159 return crypto_rng_alg(tfm)->seedsize;
160 }
161
162 #endif