Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 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 | |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame^] | 18 | struct 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 Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 24 | extern struct crypto_rng *crypto_default_rng; |
| 25 | |
| 26 | int crypto_get_default_rng(void); |
| 27 | void crypto_put_default_rng(void); |
| 28 | |
Stephan Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 29 | /** |
| 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 Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 36 | /** |
| 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 Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame^] | 56 | struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 57 | |
| 58 | static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm) |
| 59 | { |
| 60 | return &tfm->base; |
| 61 | } |
| 62 | |
Stephan Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 63 | /** |
| 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 Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 71 | static 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 Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 76 | /** |
| 77 | * crypto_free_rng() - zeroize and free RNG handle |
| 78 | * @tfm: cipher handle to be freed |
| 79 | */ |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 80 | static inline void crypto_free_rng(struct crypto_rng *tfm) |
| 81 | { |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame^] | 82 | crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm)); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 83 | } |
| 84 | |
Stephan Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 85 | /** |
| 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 Mueller | cde001e | 2015-03-06 08:26:31 +0100 | [diff] [blame] | 94 | * Return: 0 function was successful; < 0 if an error occurred |
Stephan Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 95 | */ |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 96 | static inline int crypto_rng_get_bytes(struct crypto_rng *tfm, |
| 97 | u8 *rdata, unsigned int dlen) |
| 98 | { |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame^] | 99 | return tfm->generate(tfm, rdata, dlen); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 100 | } |
| 101 | |
Stephan Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 102 | /** |
| 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 Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 119 | static inline int crypto_rng_reset(struct crypto_rng *tfm, |
| 120 | u8 *seed, unsigned int slen) |
| 121 | { |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame^] | 122 | return tfm->seed(tfm, seed, slen); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 123 | } |
| 124 | |
Stephan Mueller | aa1b6fb | 2014-11-12 05:25:31 +0100 | [diff] [blame] | 125 | /** |
| 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 Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 137 | static inline int crypto_rng_seedsize(struct crypto_rng *tfm) |
| 138 | { |
| 139 | return crypto_rng_alg(tfm)->seedsize; |
| 140 | } |
| 141 | |
| 142 | #endif |