Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * RNG operations.
0005  *
0006  * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
0007  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
0008  *
0009  * This program is free software; you can redistribute it and/or modify it
0010  * under the terms of the GNU General Public License as published by the Free
0011  * Software Foundation; either version 2 of the License, or (at your option)
0012  * any later version.
0013  *
0014  */
0015 
0016 #include <linux/atomic.h>
0017 #include <crypto/internal/rng.h>
0018 #include <linux/err.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/random.h>
0022 #include <linux/seq_file.h>
0023 #include <linux/slab.h>
0024 #include <linux/string.h>
0025 #include <linux/cryptouser.h>
0026 #include <net/netlink.h>
0027 
0028 #include "internal.h"
0029 
0030 static DEFINE_MUTEX(crypto_default_rng_lock);
0031 struct crypto_rng *crypto_default_rng;
0032 EXPORT_SYMBOL_GPL(crypto_default_rng);
0033 static int crypto_default_rng_refcnt;
0034 
0035 static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm)
0036 {
0037     return container_of(tfm, struct crypto_rng, base);
0038 }
0039 
0040 int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
0041 {
0042     u8 *buf = NULL;
0043     int err;
0044 
0045     if (!seed && slen) {
0046         buf = kmalloc(slen, GFP_KERNEL);
0047         if (!buf)
0048             return -ENOMEM;
0049 
0050         get_random_bytes(buf, slen);
0051         seed = buf;
0052     }
0053 
0054     err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
0055 
0056     kzfree(buf);
0057     return err;
0058 }
0059 EXPORT_SYMBOL_GPL(crypto_rng_reset);
0060 
0061 static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
0062 {
0063     return 0;
0064 }
0065 
0066 static unsigned int seedsize(struct crypto_alg *alg)
0067 {
0068     struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
0069 
0070     return ralg->seedsize;
0071 }
0072 
0073 #ifdef CONFIG_NET
0074 static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
0075 {
0076     struct crypto_report_rng rrng;
0077 
0078     strncpy(rrng.type, "rng", sizeof(rrng.type));
0079 
0080     rrng.seedsize = seedsize(alg);
0081 
0082     if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
0083             sizeof(struct crypto_report_rng), &rrng))
0084         goto nla_put_failure;
0085     return 0;
0086 
0087 nla_put_failure:
0088     return -EMSGSIZE;
0089 }
0090 #else
0091 static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
0092 {
0093     return -ENOSYS;
0094 }
0095 #endif
0096 
0097 static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
0098     __attribute__ ((unused));
0099 static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
0100 {
0101     seq_printf(m, "type         : rng\n");
0102     seq_printf(m, "seedsize     : %u\n", seedsize(alg));
0103 }
0104 
0105 static const struct crypto_type crypto_rng_type = {
0106     .extsize = crypto_alg_extsize,
0107     .init_tfm = crypto_rng_init_tfm,
0108 #ifdef CONFIG_PROC_FS
0109     .show = crypto_rng_show,
0110 #endif
0111     .report = crypto_rng_report,
0112     .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0113     .maskset = CRYPTO_ALG_TYPE_MASK,
0114     .type = CRYPTO_ALG_TYPE_RNG,
0115     .tfmsize = offsetof(struct crypto_rng, base),
0116 };
0117 
0118 struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
0119 {
0120     return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
0121 }
0122 EXPORT_SYMBOL_GPL(crypto_alloc_rng);
0123 
0124 int crypto_get_default_rng(void)
0125 {
0126     struct crypto_rng *rng;
0127     int err;
0128 
0129     mutex_lock(&crypto_default_rng_lock);
0130     if (!crypto_default_rng) {
0131         rng = crypto_alloc_rng("stdrng", 0, 0);
0132         err = PTR_ERR(rng);
0133         if (IS_ERR(rng))
0134             goto unlock;
0135 
0136         err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
0137         if (err) {
0138             crypto_free_rng(rng);
0139             goto unlock;
0140         }
0141 
0142         crypto_default_rng = rng;
0143     }
0144 
0145     crypto_default_rng_refcnt++;
0146     err = 0;
0147 
0148 unlock:
0149     mutex_unlock(&crypto_default_rng_lock);
0150 
0151     return err;
0152 }
0153 EXPORT_SYMBOL_GPL(crypto_get_default_rng);
0154 
0155 void crypto_put_default_rng(void)
0156 {
0157     mutex_lock(&crypto_default_rng_lock);
0158     crypto_default_rng_refcnt--;
0159     mutex_unlock(&crypto_default_rng_lock);
0160 }
0161 EXPORT_SYMBOL_GPL(crypto_put_default_rng);
0162 
0163 #if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
0164 int crypto_del_default_rng(void)
0165 {
0166     int err = -EBUSY;
0167 
0168     mutex_lock(&crypto_default_rng_lock);
0169     if (crypto_default_rng_refcnt)
0170         goto out;
0171 
0172     crypto_free_rng(crypto_default_rng);
0173     crypto_default_rng = NULL;
0174 
0175     err = 0;
0176 
0177 out:
0178     mutex_unlock(&crypto_default_rng_lock);
0179 
0180     return err;
0181 }
0182 EXPORT_SYMBOL_GPL(crypto_del_default_rng);
0183 #endif
0184 
0185 int crypto_register_rng(struct rng_alg *alg)
0186 {
0187     struct crypto_alg *base = &alg->base;
0188 
0189     if (alg->seedsize > PAGE_SIZE / 8)
0190         return -EINVAL;
0191 
0192     base->cra_type = &crypto_rng_type;
0193     base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0194     base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
0195 
0196     return crypto_register_alg(base);
0197 }
0198 EXPORT_SYMBOL_GPL(crypto_register_rng);
0199 
0200 void crypto_unregister_rng(struct rng_alg *alg)
0201 {
0202     crypto_unregister_alg(&alg->base);
0203 }
0204 EXPORT_SYMBOL_GPL(crypto_unregister_rng);
0205 
0206 int crypto_register_rngs(struct rng_alg *algs, int count)
0207 {
0208     int i, ret;
0209 
0210     for (i = 0; i < count; i++) {
0211         ret = crypto_register_rng(algs + i);
0212         if (ret)
0213             goto err;
0214     }
0215 
0216     return 0;
0217 
0218 err:
0219     for (--i; i >= 0; --i)
0220         crypto_unregister_rng(algs + i);
0221 
0222     return ret;
0223 }
0224 EXPORT_SYMBOL_GPL(crypto_register_rngs);
0225 
0226 void crypto_unregister_rngs(struct rng_alg *algs, int count)
0227 {
0228     int i;
0229 
0230     for (i = count - 1; i >= 0; --i)
0231         crypto_unregister_rng(algs + i);
0232 }
0233 EXPORT_SYMBOL_GPL(crypto_unregister_rngs);
0234 
0235 MODULE_LICENSE("GPL");
0236 MODULE_DESCRIPTION("Random Number Generator");