Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * RNG operations. |
| 5 | * |
| 6 | * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 15 | #include <linux/atomic.h> |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 16 | #include <crypto/internal/rng.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/random.h> |
| 21 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 23 | #include <linux/string.h> |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 24 | #include <linux/cryptouser.h> |
| 25 | #include <net/netlink.h> |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 26 | |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 27 | #include "internal.h" |
| 28 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 29 | static DEFINE_MUTEX(crypto_default_rng_lock); |
| 30 | struct crypto_rng *crypto_default_rng; |
| 31 | EXPORT_SYMBOL_GPL(crypto_default_rng); |
| 32 | static int crypto_default_rng_refcnt; |
| 33 | |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 34 | static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm) |
| 35 | { |
| 36 | return container_of(tfm, struct crypto_rng, base); |
| 37 | } |
| 38 | |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 39 | static inline struct old_rng_alg *crypto_old_rng_alg(struct crypto_rng *tfm) |
| 40 | { |
| 41 | return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng; |
| 42 | } |
| 43 | |
Herbert Xu | ff030b0 | 2015-04-20 13:39:04 +0800 | [diff] [blame] | 44 | static int generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen, |
| 45 | u8 *dst, unsigned int dlen) |
| 46 | { |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 47 | return crypto_old_rng_alg(tfm)->rng_make_random(tfm, dst, dlen); |
Herbert Xu | ff030b0 | 2015-04-20 13:39:04 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Herbert Xu | 3c5d8fa | 2015-04-21 10:46:37 +0800 | [diff] [blame] | 50 | static int rngapi_reset(struct crypto_rng *tfm, const u8 *seed, |
| 51 | unsigned int slen) |
| 52 | { |
| 53 | u8 *buf = NULL; |
| 54 | u8 *src = (u8 *)seed; |
| 55 | int err; |
| 56 | |
| 57 | if (slen) { |
| 58 | buf = kmalloc(slen, GFP_KERNEL); |
| 59 | if (!buf) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | memcpy(buf, seed, slen); |
| 63 | src = buf; |
| 64 | } |
| 65 | |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 66 | err = crypto_old_rng_alg(tfm)->rng_reset(tfm, src, slen); |
Herbert Xu | 3c5d8fa | 2015-04-21 10:46:37 +0800 | [diff] [blame] | 67 | |
| 68 | kzfree(buf); |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen) |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 73 | { |
| 74 | u8 *buf = NULL; |
| 75 | int err; |
| 76 | |
| 77 | if (!seed && slen) { |
| 78 | buf = kmalloc(slen, GFP_KERNEL); |
| 79 | if (!buf) |
| 80 | return -ENOMEM; |
| 81 | |
| 82 | get_random_bytes(buf, slen); |
| 83 | seed = buf; |
| 84 | } |
| 85 | |
Herbert Xu | 3c5d8fa | 2015-04-21 10:46:37 +0800 | [diff] [blame] | 86 | err = tfm->seed(tfm, seed, slen); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 87 | |
| 88 | kfree(buf); |
| 89 | return err; |
| 90 | } |
Herbert Xu | 3c5d8fa | 2015-04-21 10:46:37 +0800 | [diff] [blame] | 91 | EXPORT_SYMBOL_GPL(crypto_rng_reset); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 92 | |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 93 | static int crypto_rng_init_tfm(struct crypto_tfm *tfm) |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 94 | { |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 95 | struct crypto_rng *rng = __crypto_rng_cast(tfm); |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 96 | struct rng_alg *alg = crypto_rng_alg(rng); |
| 97 | struct old_rng_alg *oalg = crypto_old_rng_alg(rng); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 98 | |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 99 | if (oalg->rng_make_random) { |
| 100 | rng->generate = generate; |
| 101 | rng->seed = rngapi_reset; |
| 102 | rng->seedsize = oalg->seedsize; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | rng->generate = alg->generate; |
| 107 | rng->seed = alg->seed; |
| 108 | rng->seedsize = alg->seedsize; |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 113 | static unsigned int seedsize(struct crypto_alg *alg) |
| 114 | { |
| 115 | struct rng_alg *ralg = container_of(alg, struct rng_alg, base); |
| 116 | |
| 117 | return alg->cra_rng.rng_make_random ? |
| 118 | alg->cra_rng.seedsize : ralg->seedsize; |
| 119 | } |
| 120 | |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 121 | #ifdef CONFIG_NET |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 122 | static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 123 | { |
| 124 | struct crypto_report_rng rrng; |
| 125 | |
Mathias Krause | 9a5467bf | 2013-02-05 18:19:13 +0100 | [diff] [blame] | 126 | strncpy(rrng.type, "rng", sizeof(rrng.type)); |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 127 | |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 128 | rrng.seedsize = seedsize(alg); |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 129 | |
David S. Miller | 6662df3 | 2012-04-01 20:19:05 -0400 | [diff] [blame] | 130 | if (nla_put(skb, CRYPTOCFGA_REPORT_RNG, |
| 131 | sizeof(struct crypto_report_rng), &rrng)) |
| 132 | goto nla_put_failure; |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | |
| 135 | nla_put_failure: |
| 136 | return -EMSGSIZE; |
| 137 | } |
Herbert Xu | 3acc847 | 2011-11-03 23:46:07 +1100 | [diff] [blame] | 138 | #else |
| 139 | static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 140 | { |
| 141 | return -ENOSYS; |
| 142 | } |
| 143 | #endif |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 144 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 145 | static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg) |
| 146 | __attribute__ ((unused)); |
| 147 | static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg) |
| 148 | { |
| 149 | seq_printf(m, "type : rng\n"); |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 150 | seq_printf(m, "seedsize : %u\n", seedsize(alg)); |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 151 | } |
| 152 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 153 | const struct crypto_type crypto_rng_type = { |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 154 | .extsize = crypto_alg_extsize, |
| 155 | .init_tfm = crypto_rng_init_tfm, |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 156 | #ifdef CONFIG_PROC_FS |
| 157 | .show = crypto_rng_show, |
| 158 | #endif |
Steffen Klassert | 792608e | 2011-09-27 07:47:11 +0200 | [diff] [blame] | 159 | .report = crypto_rng_report, |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 160 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 161 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 162 | .type = CRYPTO_ALG_TYPE_RNG, |
| 163 | .tfmsize = offsetof(struct crypto_rng, base), |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 164 | }; |
| 165 | EXPORT_SYMBOL_GPL(crypto_rng_type); |
| 166 | |
Herbert Xu | d0e8305 | 2015-04-20 13:39:03 +0800 | [diff] [blame] | 167 | struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask) |
| 168 | { |
| 169 | return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask); |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(crypto_alloc_rng); |
| 172 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 173 | int crypto_get_default_rng(void) |
| 174 | { |
| 175 | struct crypto_rng *rng; |
| 176 | int err; |
| 177 | |
| 178 | mutex_lock(&crypto_default_rng_lock); |
| 179 | if (!crypto_default_rng) { |
| 180 | rng = crypto_alloc_rng("stdrng", 0, 0); |
| 181 | err = PTR_ERR(rng); |
| 182 | if (IS_ERR(rng)) |
| 183 | goto unlock; |
| 184 | |
| 185 | err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng)); |
| 186 | if (err) { |
| 187 | crypto_free_rng(rng); |
| 188 | goto unlock; |
| 189 | } |
| 190 | |
| 191 | crypto_default_rng = rng; |
| 192 | } |
| 193 | |
| 194 | crypto_default_rng_refcnt++; |
| 195 | err = 0; |
| 196 | |
| 197 | unlock: |
| 198 | mutex_unlock(&crypto_default_rng_lock); |
| 199 | |
| 200 | return err; |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(crypto_get_default_rng); |
| 203 | |
| 204 | void crypto_put_default_rng(void) |
| 205 | { |
| 206 | mutex_lock(&crypto_default_rng_lock); |
| 207 | if (!--crypto_default_rng_refcnt) { |
| 208 | crypto_free_rng(crypto_default_rng); |
| 209 | crypto_default_rng = NULL; |
| 210 | } |
| 211 | mutex_unlock(&crypto_default_rng_lock); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(crypto_put_default_rng); |
| 214 | |
Herbert Xu | acec27f | 2015-04-21 10:46:38 +0800 | [diff] [blame] | 215 | int crypto_register_rng(struct rng_alg *alg) |
| 216 | { |
| 217 | struct crypto_alg *base = &alg->base; |
| 218 | |
| 219 | if (alg->seedsize > PAGE_SIZE / 8) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | base->cra_type = &crypto_rng_type; |
| 223 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 224 | base->cra_flags |= CRYPTO_ALG_TYPE_RNG; |
| 225 | |
| 226 | return crypto_register_alg(base); |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(crypto_register_rng); |
| 229 | |
| 230 | void crypto_unregister_rng(struct rng_alg *alg) |
| 231 | { |
| 232 | crypto_unregister_alg(&alg->base); |
| 233 | } |
| 234 | EXPORT_SYMBOL_GPL(crypto_unregister_rng); |
| 235 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 236 | MODULE_LICENSE("GPL"); |
Christian Kujau | a8ccc39 | 2009-08-13 11:53:56 +1000 | [diff] [blame] | 237 | MODULE_DESCRIPTION("Random Number Generator"); |