blob: 7fca37144b5905c777c9623f542370f8b6755478 [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
Herbert Xud0e83052015-04-20 13:39:03 +080018struct crypto_rng {
Herbert Xuff030b02015-04-20 13:39:04 +080019 int (*generate)(struct crypto_rng *tfm,
20 const u8 *src, unsigned int slen,
21 u8 *dst, unsigned int dlen);
Herbert Xu3c5d8fa2015-04-21 10:46:37 +080022 int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
Herbert Xud0e83052015-04-20 13:39:03 +080023 struct crypto_tfm base;
24};
25
Neil Horman17f0f4a2008-08-14 22:15:52 +100026extern struct crypto_rng *crypto_default_rng;
27
28int crypto_get_default_rng(void);
29void crypto_put_default_rng(void);
30
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010031/**
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
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010038/**
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 */
Herbert Xud0e83052015-04-20 13:39:03 +080058struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
Neil Horman17f0f4a2008-08-14 22:15:52 +100059
60static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
61{
62 return &tfm->base;
63}
64
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010065/**
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 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100073static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
74{
75 return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
76}
77
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010078/**
79 * crypto_free_rng() - zeroize and free RNG handle
80 * @tfm: cipher handle to be freed
81 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100082static inline void crypto_free_rng(struct crypto_rng *tfm)
83{
Herbert Xud0e83052015-04-20 13:39:03 +080084 crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
Neil Horman17f0f4a2008-08-14 22:15:52 +100085}
86
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010087/**
Herbert Xuff030b02015-04-20 13:39:04 +080088 * 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 */
101static 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/**
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100109 * 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 *
Stephan Muellercde001e2015-03-06 08:26:31 +0100117 * Return: 0 function was successful; < 0 if an error occurred
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100118 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000119static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
120 u8 *rdata, unsigned int dlen)
121{
Herbert Xuff030b02015-04-20 13:39:04 +0800122 return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000123}
124
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100125/**
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 */
Herbert Xu3c5d8fa2015-04-21 10:46:37 +0800142int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
143 unsigned int slen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000144
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100145/**
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 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000157static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
158{
159 return crypto_rng_alg(tfm)->seedsize;
160}
161
162#endif