blob: 6e28ea5be9f1264d685be72e00472ef6506a65c8 [file] [log] [blame]
Neil Horman17f0f4a2008-08-14 22:15:52 +10001/*
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
18extern struct crypto_rng *crypto_default_rng;
19
20int crypto_get_default_rng(void);
21void crypto_put_default_rng(void);
22
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010023/**
24 * DOC: Random number generator API
25 *
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)
28 */
29
Neil Horman17f0f4a2008-08-14 22:15:52 +100030static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm)
31{
32 return (struct crypto_rng *)tfm;
33}
34
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010035/**
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
41 *
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.
45 *
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
50 * /dev/random driver.
51 *
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.
54 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100055static inline struct crypto_rng *crypto_alloc_rng(const char *alg_name,
56 u32 type, u32 mask)
57{
58 type &= ~CRYPTO_ALG_TYPE_MASK;
59 type |= CRYPTO_ALG_TYPE_RNG;
60 mask |= CRYPTO_ALG_TYPE_MASK;
61
62 return __crypto_rng_cast(crypto_alloc_base(alg_name, type, mask));
63}
64
65static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
66{
67 return &tfm->base;
68}
69
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010070/**
71 * crypto_rng_alg - obtain name of RNG
72 * @tfm: cipher handle
73 *
74 * Return the generic name (cra_name) of the initialized random number generator
75 *
76 * Return: generic name string
77 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100078static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
79{
80 return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
81}
82
83static inline struct rng_tfm *crypto_rng_crt(struct crypto_rng *tfm)
84{
85 return &crypto_rng_tfm(tfm)->crt_rng;
86}
87
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010088/**
89 * crypto_free_rng() - zeroize and free RNG handle
90 * @tfm: cipher handle to be freed
91 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100092static inline void crypto_free_rng(struct crypto_rng *tfm)
93{
94 crypto_free_tfm(crypto_rng_tfm(tfm));
95}
96
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010097/**
98 * crypto_rng_get_bytes() - get random number
99 * @tfm: cipher handle
100 * @rdata: output buffer holding the random numbers
101 * @dlen: length of the output buffer
102 *
103 * This function fills the caller-allocated buffer with random numbers using the
104 * random number generator referenced by the cipher handle.
105 *
Stephan Muellercde001e2015-03-06 08:26:31 +0100106 * Return: 0 function was successful; < 0 if an error occurred
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100107 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000108static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
109 u8 *rdata, unsigned int dlen)
110{
111 return crypto_rng_crt(tfm)->rng_gen_random(tfm, rdata, dlen);
112}
113
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100114/**
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
119 *
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.
128 *
129 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
130 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000131static inline int crypto_rng_reset(struct crypto_rng *tfm,
132 u8 *seed, unsigned int slen)
133{
134 return crypto_rng_crt(tfm)->rng_reset(tfm, seed, slen);
135}
136
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100137/**
138 * crypto_rng_seedsize() - obtain seed size of RNG
139 * @tfm: cipher handle
140 *
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.
146 *
147 * Return: seed size for the random number generator
148 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000149static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
150{
151 return crypto_rng_alg(tfm)->seedsize;
152}
153
154#endif