blob: f13f3faca4d7779abd0d58d74b32e91d0968c461 [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 {
19 int (*generate)(struct crypto_rng *tfm, u8 *rdata, unsigned int dlen);
20 int (*seed)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
21 struct crypto_tfm base;
22};
23
Neil Horman17f0f4a2008-08-14 22:15:52 +100024extern struct crypto_rng *crypto_default_rng;
25
26int crypto_get_default_rng(void);
27void crypto_put_default_rng(void);
28
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010029/**
30 * DOC: Random number generator API
31 *
32 * The random number generator API is used with the ciphers of type
33 * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
34 */
35
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010036/**
37 * crypto_alloc_rng() -- allocate RNG handle
38 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
39 * message digest cipher
40 * @type: specifies the type of the cipher
41 * @mask: specifies the mask for the cipher
42 *
43 * Allocate a cipher handle for a random number generator. The returned struct
44 * crypto_rng is the cipher handle that is required for any subsequent
45 * API invocation for that random number generator.
46 *
47 * For all random number generators, this call creates a new private copy of
48 * the random number generator that does not share a state with other
49 * instances. The only exception is the "krng" random number generator which
50 * is a kernel crypto API use case for the get_random_bytes() function of the
51 * /dev/random driver.
52 *
53 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
54 * of an error, PTR_ERR() returns the error code.
55 */
Herbert Xud0e83052015-04-20 13:39:03 +080056struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
Neil Horman17f0f4a2008-08-14 22:15:52 +100057
58static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
59{
60 return &tfm->base;
61}
62
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010063/**
64 * crypto_rng_alg - obtain name of RNG
65 * @tfm: cipher handle
66 *
67 * Return the generic name (cra_name) of the initialized random number generator
68 *
69 * Return: generic name string
70 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100071static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
72{
73 return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
74}
75
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010076/**
77 * crypto_free_rng() - zeroize and free RNG handle
78 * @tfm: cipher handle to be freed
79 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100080static inline void crypto_free_rng(struct crypto_rng *tfm)
81{
Herbert Xud0e83052015-04-20 13:39:03 +080082 crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
Neil Horman17f0f4a2008-08-14 22:15:52 +100083}
84
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010085/**
86 * crypto_rng_get_bytes() - get random number
87 * @tfm: cipher handle
88 * @rdata: output buffer holding the random numbers
89 * @dlen: length of the output buffer
90 *
91 * This function fills the caller-allocated buffer with random numbers using the
92 * random number generator referenced by the cipher handle.
93 *
Stephan Muellercde001e2015-03-06 08:26:31 +010094 * Return: 0 function was successful; < 0 if an error occurred
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010095 */
Neil Horman17f0f4a2008-08-14 22:15:52 +100096static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
97 u8 *rdata, unsigned int dlen)
98{
Herbert Xud0e83052015-04-20 13:39:03 +080099 return tfm->generate(tfm, rdata, dlen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000100}
101
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100102/**
103 * crypto_rng_reset() - re-initialize the RNG
104 * @tfm: cipher handle
105 * @seed: seed input data
106 * @slen: length of the seed input data
107 *
108 * The reset function completely re-initializes the random number generator
109 * referenced by the cipher handle by clearing the current state. The new state
110 * is initialized with the caller provided seed or automatically, depending
111 * on the random number generator type (the ANSI X9.31 RNG requires
112 * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
113 * The seed is provided as a parameter to this function call. The provided seed
114 * should have the length of the seed size defined for the random number
115 * generator as defined by crypto_rng_seedsize.
116 *
117 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
118 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000119static inline int crypto_rng_reset(struct crypto_rng *tfm,
120 u8 *seed, unsigned int slen)
121{
Herbert Xud0e83052015-04-20 13:39:03 +0800122 return tfm->seed(tfm, seed, slen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000123}
124
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100125/**
126 * crypto_rng_seedsize() - obtain seed size of RNG
127 * @tfm: cipher handle
128 *
129 * The function returns the seed size for the random number generator
130 * referenced by the cipher handle. This value may be zero if the random
131 * number generator does not implement or require a reseeding. For example,
132 * the SP800-90A DRBGs implement an automated reseeding after reaching a
133 * pre-defined threshold.
134 *
135 * Return: seed size for the random number generator
136 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000137static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
138{
139 return crypto_rng_alg(tfm)->seedsize;
140}
141
142#endif