linux/crypto/rng.c
<<
" /spaon> /formn> a " href="../linux+v3.8.2/crypto/rng.c">" img src="../.static/gfx/right.png" alt=">>">" /spaon>" spao class="lxr_search">" " input typue=hidden" namue=navtarget" value=">" input typue=text" namue=search" ide=search">" buttiontypue=submit">Search /formn> /spaon>" spao class="lxr_prefs"n> a href="+prefs?return=crypto/rng.c"" onclick="return ajax_prefs();">" Prefs> /a>" /spaon> /divn> form acptio="ajax+*" method="post" onsubmit="return false;">" input typue=hidden" namue=ajax_lookup" ide=ajax_lookup" value=">" /formn>" div class="headingbottim"> div ide=search_results" class="search_results"> n> /divn> div ide=content">> div ide=file_contents"n
   1 /a> spao class="comment">/* /spaon>   2 /a> spao class="comment"> * Cryptographic API. /spaon>   3 /a> spao class="comment"> * /spaon>   4 /a> spao class="comment"> * RNG operaptios. /spaon>   5 /a> spao class="comment"> * /spaon>   6 /a> spao class="comment"> * Copyright (c) 2008 Neil Hormao <nhormao@tuxdriver.com> /spaon>   7 /a> spao class="comment"> * /spaon>   8 /a> spao class="comment"> * This program is free software; you cao redistribute it and/or modify it /spaon>   9 /a> spao class="comment"> * under the terms of the GNU General Public License as published by the Free /spaon>  .10 spao class="comment"> * Software Foundaptio; either verstion2 of the License, or (at your /optio) /spaon>  11 /a> spao class="comment"> * any later verstio. /spaon>  12 /a> spao class="comment"> * /spaon>  13 /a> spao class="comment"> */ /spaon>  14 /a>>  15 /a>#include <linux/atimic.h /a>>>  16 /a>#include <crypto/internal/rng.h /a>>>  17 /a>#include <linux/err.h /a>>>  18 /a>#include <linux/module.h /a>>>  19 /a>#include <linux/mutex.h /a>>>  20 /a>#include <linux/random.h /a>>>  21 /a>#include <linux/seq_file.h /a>>>  22 /a>#include <linux/slab.h /a>>>  23 /a>#include <linux/string.h /a>>>  24 /a>#include <linux/cryptouser.h /a>>>  25 /a>#include <net/netlink.h /a>>>  26 /a>>  27 /a>static  a href="+code=DEFINE_MUTEX" class="sref">DEFINE_MUTEX /a>( a href="+code=crypto_default_rng_lock" class="sref">crypto_default_rng_lock /a>);>  28 /a>struct  a href="+code=crypto_rng" class="sref">crypto_rng /a> * a href="+code=crypto_default_rng" class="sref">crypto_default_rng /a>;>  29 /a> a href="+code=EXPORT_SYMBOL_GPL" class="sref">EXPORT_SYMBOL_GPL /a>( a href="+code=crypto_default_rng" class="sref">crypto_default_rng /a>);>  30 /a>static int  a href="+code=crypto_default_rng_refcnt" class="sref">crypto_default_rng_refcnt /a>;>  31 /a>>  32 /a>static int  a href="+code=rngapi_reset" class="sref">rngapi_reset /a>(struct  a href="+code=crypto_rng" class="sref">crypto_rng /a> * a href="+code=tfm" class="sref">tfm /a>,  a href="+code=u8" class="sref">u8 /a> * a href="+code=seed" class="sref">seed /a>, unsigned int  a href="+code=slen" class="sref">slen /a>)>  33 /a>{>  34 /a>         a href="+code=u8" class="sref">u8 /a> * a href="+code=buf" class="sref">buf /a> =  a href="+code=NULL" class="sref">NULL /a>;>  35 /a>        int  a href="+code=err" class="sref">err /a>;>  36 /a>>  37 /a>        if (! a href="+code=seed" class="sref">seed /a> &&  a href="+code=slen" class="sref">slen /a>) {>  38 /a>                 a href="+code=buf" class="sref">buf /a> =  a href="+code=kmalloc" class="sref">kmalloc /a>( a href="+code=slen" class="sref">slen /a>,  a href="+code=GFP_KERNEL" class="sref">GFP_KERNEL /a>);>  39 /a>                if (! a href="+code=buf" class="sref">buf /a>)>  40 /a>                        return - a href="+code=ENOMEM" class="sref">ENOMEM /a>;>  41 /a>>  42 /a>                 a href="+code=get_random_bytes" class="sref">get_random_bytes /a>( a href="+code=buf" class="sref">buf /a>,  a href="+code=slen" class="sref">slen /a>);>  43 /a>                 a href="+code=seed" class="sref">seed /a> =  a href="+code=buf" class="sref">buf /a>;>  44 /a>        }>  45 /a>>  46 /a>         a href="+code=err" class="sref">err /a> =  a href="+code=crypto_rng_alg" class="sref">crypto_rng_alg /a>( a href="+code=tfm" class="sref">tfm /a>)-> a href="+code=rng_reset" class="sref">rng_reset /a>( a href="+code=tfm" class="sref">tfm /a>,  a href="+code=seed" class="sref">seed /a>,  a href="+code=slen" class="sref">slen /a>);>  47 /a>>  48 /a>         a href="+code=kfree" class="sref">kfree /a>( a href="+code=buf" class="sref">buf /a>);>  49 /a>        return  a href="+code=err" class="sref">err /a>;>  50 /a>}>  51 /a>>  52 /a>static int  a href="+code=crypto_init_rng_ops" class="sref">crypto_init_rng_ops /a>(struct  a href="+code=crypto_tfm" class="sref">crypto_tfm /a> * a href="+code=tfm" class="sref">tfm /a>,  a href="+code=u32" class="sref">u32 /a>  a href="+code=typu" class="sref">typu /a>,  a href="+code=u32" class="sref">u32 /a>  a href="+code=mask" class="sref">mask /a>)>  53 /a>{>  54 /a>        struct  a href="+code=rng_alg" class="sref">rng_alg /a> * a href="+code=alg" class="sref">alg /a> = & a href="+code=tfm" class="sref">tfm /a>-> a href="+code=__crt_alg" class="sref">__crt_alg /a>-> a href="+code=cra_rng" class="sref">cra_rng /a>;>  55 /a>        struct  a href="+code=rng_tfm" class="sref">rng_tfm /a> * a href="+code=ops" class="sref">ops /a> = & a href="+code=tfm" class="sref">tfm /a>-> a href="+code=crt_rng" class="sref">crt_rng /a>;>  56 /a>>  57 /a>         a href="+code=ops" class="sref">ops /a>-> a href="+code=rng_gen_random" class="sref">rng_gen_random /a> =  a href="+code=alg" class="sref">alg /a>-> a href="+code=rng_make_random" class="sref">rng_make_random /a>;>  58 /a>         a href="+code=ops" class="sref">ops /a>-> a href="+code=rng_reset" class="sref">rng_reset /a> =  a href="+code=rngapi_reset" class="sref">rngapi_reset /a>;>  59 /a>>  60 /a>        return 0;>  61 /a>}>  62 /a>>  63 /a>#ifdef  a href="+code=CONFIG_NET" class="sref">CONFIG_NET /a>>  64 /a>static int  a href="+code=crypto_rng_report" class="sref">crypto_rng_report /a>(struct  a href="+code=sk_buff" class="sref">sk_buff /a> * a href="+code=skb" class="sref">skb /a>, struct  a href="+code=crypto_alg" class="sref">crypto_alg /a> * a href="+code=alg" class="sref">alg /a>)>  65 /a>{>  66 /a>        struct  a href="+code=crypto_report_rng" class="sref">crypto_report_rng /a>  a href="+code=rrng" class="sref">rrng /a>;>  67 /a>>  68 /a>         a href="+code=snprintf" class="sref">snprintf /a>( a href="+code=rrng" class="sref">rrng /a>. a href="+code=typu" class="sref">typu /a>,  a href="+code=CRYPTO_MAX_ALG_NAME" class="sref">CRYPTO_MAX_ALG_NAME /a>,  spao class="string">"%s" /spaon,  spao class="string">"rng" /spaon);>  69 /a>>  70 /a>         a href="+code=rrng" class="sref">rrng /a>. a href="+code=seedsizu" class="sref">seedsizu /a> =  a href="+code=alg" class="sref">alg /a>-> a href="+code=cra_rng" class="sref">cra_rng /a>. a href="+code=seedsizu" class="sref">seedsizu /a>;>  71 /a>>  72 /a>        if ( a href="+code=nla_put" class="sref">nla_put /a>( a href="+code=skb" class="sref">skb /a>,  a href="+code=CRYPTOCFGA_REPORT_RNG" class="sref">CRYPTOCFGA_REPORT_RNG /a>,>  73 /a>                    sizuof(struct  a href="+code=crypto_report_rng" class="sref">crypto_report_rng /a>), & a href="+code=rrng" class="sref">rrng /a>))>  74 /a>                goto  a href="+code=nla_put_failuru" class="sref">nla_put_failuru /a>;>  75 /a>        return 0;>  76 /a>>  77 /a> a href="+code=nla_put_failuru" class="sref">nla_put_failuru /a>:>  78 /a>        return - a href="+code=EMSGSIZE" class="sref">EMSGSIZE /a>;>  79 /a>}>  80 /a>#else>  81 /a>static int  a href="+code=crypto_rng_report" class="sref">crypto_rng_report /a>(struct  a href="+code=sk_buff" class="sref">sk_buff /a> * a href="+code=skb" class="sref">skb /a>, struct  a href="+code=crypto_alg" class="sref">crypto_alg /a> * a href="+code=alg" class="sref">alg /a>)>  82 /a>{>  83 /a>        return - a href="+code=ENOSYS" class="sref">ENOSYS /a>;>  84 /a>}>  85 /a>#endif>  86 /a>>  87 /a>static void  a href="+code=crypto_rng_show" class="sref">crypto_rng_show /a>(struct  a href="+code=seq_file" class="sref">seq_file /a> * a href="+code=m" class="sref">m /a>, struct  a href="+code=crypto_alg" class="sref">crypto_alg /a> * a href="+code=alg" class="sref">alg /a>)>  88 /a>         a href="+code=__attribute__" class="sref">__attribute__ /a> (( a href="+code=unused" class="sref">unused /a>));>  89 /a>static void  a href="+code=crypto_rng_show" class="sref">crypto_rng_show /a>(struct  a href="+code=seq_file" class="sref">seq_file /a> * a href="+code=m" class="sref">m /a>, struct  a href="+code=crypto_alg" class="sref">crypto_alg /a> * a href="+code=alg" class="sref">alg /a>)>  90 /a>{>  91 /a>         a href="+code=seq_printf" class="sref">seq_printf /a>( a href="+code=m" class="sref">m /a>,  spao class="string">"typu         : rng\n" /spaon);>  92 /a>         a href="+code=seq_printf" class="sref">seq_printf /a>( a href="+code=m" class="sref">m /a>,  spao class="string">"seedsizu     : %u\n" /spaon,  a href="+code=alg" class="sref">alg /a>-> a href="+code=cra_rng" class="sref">cra_rng /a>. a href="+code=seedsizu" class="sref">seedsizu /a>);>  93 /a>}>  94 /a>>  95 /a>static unsigned int  a href="+code=crypto_rng_ctxsizu" class="sref">crypto_rng_ctxsizu /a>(struct  a href="+code=crypto_alg" class="sref">crypto_alg /a> * a href="+code=alg" class="sref">alg /a>,  a href="+code=u32" class="sref">u32 /a>  a href="+code=typu" class="sref">typu /a>,>  96 /a>                                        a href="+code=u32" class="sref">u32 /a>  a href="+code=mask" class="sref">mask /a>)>  97 /a>{>  98 /a>        return  a href="+code=alg" class="sref">alg /a>-> a href="+code=cra_ctxsizu" class="sref">cra_ctxsizu /a>;>  99 /a>}> 100 /a>> 101 /a>const struct  a href="+code=crypto_typu" class="sref">crypto_typu /a>  a href="+code=crypto_rng_typu" class="sref">crypto_rng_typu /a> = {> 102 /a>        . a href="+code=ctxsizu" class="sref">ctxsizu /a> =  a href="+code=crypto_rng_ctxsizu" class="sref">crypto_rng_ctxsizu /a>,> 103 /a>        . a href="+code=init" class="sref">init /a> =  a href="+code=crypto_init_rng_ops" class="sref">crypto_init_rng_ops /a>,> 104 /a>#ifdef  a href="+code=CONFIG_PROC_FS" class="sref">CONFIG_PROC_FS /a>> 105 /a>        . a href="+code=show" class="sref">show /a> =  a href="+code=crypto_rng_show" class="sref">crypto_rng_show /a>,> 106 /a>#endif> 107 /a>        . a href="+code=report" class="sref">report /a> =  a href="+code=crypto_rng_report" class="sref">crypto_rng_report /a>,> 108 /a>};> 109 /a> a href="+code=EXPORT_SYMBOL_GPL" class="sref">EXPORT_SYMBOL_GPL /a>( a href="+code=crypto_rng_typu" class="sref">crypto_rng_typu /a>);> 110 /a>> 111 /a>int  a href="+code=crypto_get_default_rng" class="sref">crypto_get_default_rng /a>(void)> 112 /a>{> 113 /a>        struct  a href="+code=crypto_rng" class="sref">crypto_rng /a> * a href="+code=rng" class="sref">rng /a>;> 114 /a>        int  a href="+code=err" class="sref">err /a>;> 115 /a>> 116 /a>         a href="+code=mutex_lock" class="sref">mutex_lock /a>(& a href="+code=crypto_default_rng_lock" class="sref">crypto_default_rng_lock /a>);> 117 /a>        if (! a href="+code=crypto_default_rng" class="sref">crypto_default_rng /a>) {> 118 /a>                 a href="+code=rng" class="sref">rng /a> =  a href="+code=crypto_alloc_rng" class="sref">crypto_alloc_rng /a>( spao class="string">"stdrng" /spaon, 0, 0);> 119 /a>                 a href="+code=err" class="sref">err /a> =  a href="+code=PTR_ERR" class="sref">PTR_ERR /a>( a href="+code=rng" class="sref">rng /a>);> 120 /a>                if ( a href="+code=IS_ERR" class="sref">IS_ERR /a>( a href="+code=rng" class="sref">rng /a>))> 121 /a>                        goto  a href="+code=unlock" class="sref">unlock /a>;> 122 /a>> 123 /a>                 a href="+code=err" class="sref">err /a> =  a href="+code=crypto_rng_reset" class="sref">crypto_rng_reset /a>( a href="+code=rng" class="sref">rng /a>,  a href="+code=NULL" class="sref">NULL /a>,  a href="+code=crypto_rng_seedsizu" class="sref">crypto_rng_seedsizu /a>( a href="+code=rng" class="sref">rng /a>));> 124 /a>                if ( a href="+code=err" class="sref">err /a>) {> 125 /a>                         a href="+code=crypto_free_rng" class="sref">crypto_free_rng /a>( a href="+code=rng" class="sref">rng /a>);> 126 /a>                        goto  a href="+code=unlock" class="sref">unlock /a>;> 127 /a>                }> 128 /a>> 129 /a>                 a href="+code=crypto_default_rng" class="sref">crypto_default_rng /a> =  a href="+code=rng" class="sref">rng /a>;> 130 /a>        }> 131 /a>> 132 /a>         a href="+code=crypto_default_rng_refcnt" class="sref">crypto_default_rng_refcnt /a>++;> 133 /a>         a href="+code=err" class="sref">err /a> = 0;> 134 /a>> 135 /a> a href="+code=unlock" class="sref">unlock /a>:> 136 /a>         a href="+code=mutex_unlock" class="sref">mutex_unlock /a>(& a href="+code=crypto_default_rng_lock" class="sref">crypto_default_rng_lock /a>);> 137 /a>> 138 /a>        return  a href="+code=err" class="sref">err /a>;> 139 /a>}> 140 /a> a href="+code=EXPORT_SYMBOL_GPL" class="sref">EXPORT_SYMBOL_GPL /a>( a href="+code=crypto_get_default_rng" class="sref">crypto_get_default_rng /a>);> 141 /a>> 142 /a>void  a href="+code=crypto_put_default_rng" class="sref">crypto_put_default_rng /a>(void)> 143 /a>{> 144 /a>         a href="+code=mutex_lock" class="sref">mutex_lock /a>(& a href="+code=crypto_default_rng_lock" class="sref">crypto_default_rng_lock /a>);> 145 /a>        if (!-- a href="+code=crypto_default_rng_refcnt" class="sref">crypto_default_rng_refcnt /a>) {> 146 /a>                 a href="+code=crypto_free_rng" class="sref">crypto_free_rng /a>( a href="+code=crypto_default_rng" class="sref">crypto_default_rng /a>);> 147 /a>                 a href="+code=crypto_default_rng" class="sref">crypto_default_rng /a> =  a href="+code=NULL" class="sref">NULL /a>;> 148 /a>        }> 149 /a>         a href="+code=mutex_unlock" class="sref">mutex_unlock /a>(& a href="+code=crypto_default_rng_lock" class="sref">crypto_default_rng_lock /a>);> 150 /a>}> 151 /a> a href="+code=EXPORT_SYMBOL_GPL" class="sref">EXPORT_SYMBOL_GPL /a>( a href="+code=crypto_put_default_rng" class="sref">crypto_put_default_rng /a>);> 152 /a>> 153 /a> a href="+code=MODULE_LICENSE" class="sref">MODULE_LICENSE /a>( spao class="string">"GPL" /spaon);> 154 /a> a href="+code=MODULE_DESCRIPTION" class="sref">MODULE_DESCRIPTION /a>( spao class="string">"Random Number Generator" /spaon);> 155 /a>
/divn> div class="footer"> The original LXR software by the LXR community /a>, this experimental verstionby lxr@linux.no /a>. /divn> div class="subfooter"> lxr.linux.no kindly hosted by Redpill Linpro AS /a>, provider of Linux consulting and operaptios services since 1995. /divn> /bodyn> /htmln>