123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082 |
- #include <crypto/drbg.h>
- #include <linux/kernel.h>
- static const struct drbg_core drbg_cores[] = {
- #ifdef CONFIG_CRYPTO_DRBG_CTR
- {
- .flags = DRBG_CTR | DRBG_STRENGTH128,
- .statelen = 32,
- .blocklen_bytes = 16,
- .cra_name = "ctr_aes128",
- .backend_cra_name = "aes",
- }, {
- .flags = DRBG_CTR | DRBG_STRENGTH192,
- .statelen = 40,
- .blocklen_bytes = 16,
- .cra_name = "ctr_aes192",
- .backend_cra_name = "aes",
- }, {
- .flags = DRBG_CTR | DRBG_STRENGTH256,
- .statelen = 48,
- .blocklen_bytes = 16,
- .cra_name = "ctr_aes256",
- .backend_cra_name = "aes",
- },
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_HASH
- {
- .flags = DRBG_HASH | DRBG_STRENGTH128,
- .statelen = 55,
- .blocklen_bytes = 20,
- .cra_name = "sha1",
- .backend_cra_name = "sha1",
- }, {
- .flags = DRBG_HASH | DRBG_STRENGTH256,
- .statelen = 111,
- .blocklen_bytes = 48,
- .cra_name = "sha384",
- .backend_cra_name = "sha384",
- }, {
- .flags = DRBG_HASH | DRBG_STRENGTH256,
- .statelen = 111,
- .blocklen_bytes = 64,
- .cra_name = "sha512",
- .backend_cra_name = "sha512",
- }, {
- .flags = DRBG_HASH | DRBG_STRENGTH256,
- .statelen = 55,
- .blocklen_bytes = 32,
- .cra_name = "sha256",
- .backend_cra_name = "sha256",
- },
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_HMAC
- {
- .flags = DRBG_HMAC | DRBG_STRENGTH128,
- .statelen = 20,
- .blocklen_bytes = 20,
- .cra_name = "hmac_sha1",
- .backend_cra_name = "hmac(sha1)",
- }, {
- .flags = DRBG_HMAC | DRBG_STRENGTH256,
- .statelen = 48,
- .blocklen_bytes = 48,
- .cra_name = "hmac_sha384",
- .backend_cra_name = "hmac(sha384)",
- }, {
- .flags = DRBG_HMAC | DRBG_STRENGTH256,
- .statelen = 64,
- .blocklen_bytes = 64,
- .cra_name = "hmac_sha512",
- .backend_cra_name = "hmac(sha512)",
- }, {
- .flags = DRBG_HMAC | DRBG_STRENGTH256,
- .statelen = 32,
- .blocklen_bytes = 32,
- .cra_name = "hmac_sha256",
- .backend_cra_name = "hmac(sha256)",
- },
- #endif
- };
- static int drbg_uninstantiate(struct drbg_state *drbg);
- static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
- {
- switch (flags & DRBG_STRENGTH_MASK) {
- case DRBG_STRENGTH128:
- return 16;
- case DRBG_STRENGTH192:
- return 24;
- case DRBG_STRENGTH256:
- return 32;
- default:
- return 32;
- }
- }
- #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
- static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
- {
- struct s {
- __be32 conv;
- };
- struct s *conversion = (struct s *) buf;
- conversion->conv = cpu_to_be32(val);
- }
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_CTR
- #define CRYPTO_DRBG_CTR_STRING "CTR "
- MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
- MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
- MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
- MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
- MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
- MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
- static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
- const unsigned char *key);
- static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
- const struct drbg_string *in);
- static int drbg_init_sym_kernel(struct drbg_state *drbg);
- static int drbg_fini_sym_kernel(struct drbg_state *drbg);
- static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
- u8 *inbuf, u32 inbuflen,
- u8 *outbuf, u32 outlen);
- #define DRBG_CTR_NULL_LEN 128
- #define DRBG_OUTSCRATCHLEN DRBG_CTR_NULL_LEN
- static int drbg_ctr_bcc(struct drbg_state *drbg,
- unsigned char *out, const unsigned char *key,
- struct list_head *in)
- {
- int ret = 0;
- struct drbg_string *curr = NULL;
- struct drbg_string data;
- short cnt = 0;
- drbg_string_fill(&data, out, drbg_blocklen(drbg));
-
- drbg_kcapi_symsetkey(drbg, key);
- list_for_each_entry(curr, in, list) {
- const unsigned char *pos = curr->buf;
- size_t len = curr->len;
-
- while (len) {
-
- if (drbg_blocklen(drbg) == cnt) {
- cnt = 0;
- ret = drbg_kcapi_sym(drbg, out, &data);
- if (ret)
- return ret;
- }
- out[cnt] ^= *pos;
- pos++;
- cnt++;
- len--;
- }
- }
-
- if (cnt)
- ret = drbg_kcapi_sym(drbg, out, &data);
- return ret;
- }
- static int drbg_ctr_df(struct drbg_state *drbg,
- unsigned char *df_data, size_t bytes_to_return,
- struct list_head *seedlist)
- {
- int ret = -EFAULT;
- unsigned char L_N[8];
-
- struct drbg_string S1, S2, S4, cipherin;
- LIST_HEAD(bcc_list);
- unsigned char *pad = df_data + drbg_statelen(drbg);
- unsigned char *iv = pad + drbg_blocklen(drbg);
- unsigned char *temp = iv + drbg_blocklen(drbg);
- size_t padlen = 0;
- unsigned int templen = 0;
-
- unsigned int i = 0;
-
- const unsigned char *K = (unsigned char *)
- "\x00\x01\x02\x03\x04\x05\x06\x07"
- "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
- "\x10\x11\x12\x13\x14\x15\x16\x17"
- "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
- unsigned char *X;
- size_t generated_len = 0;
- size_t inputlen = 0;
- struct drbg_string *seed = NULL;
- memset(pad, 0, drbg_blocklen(drbg));
- memset(iv, 0, drbg_blocklen(drbg));
-
-
- if ((512/8) < bytes_to_return)
- return -EINVAL;
-
- list_for_each_entry(seed, seedlist, list)
- inputlen += seed->len;
- drbg_cpu_to_be32(inputlen, &L_N[0]);
-
- drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
-
- padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
-
- if (padlen)
- padlen = drbg_blocklen(drbg) - padlen;
-
- padlen++;
- pad[0] = 0x80;
-
- drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
- list_add_tail(&S1.list, &bcc_list);
- drbg_string_fill(&S2, L_N, sizeof(L_N));
- list_add_tail(&S2.list, &bcc_list);
- list_splice_tail(seedlist, &bcc_list);
- drbg_string_fill(&S4, pad, padlen);
- list_add_tail(&S4.list, &bcc_list);
-
- while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
-
- drbg_cpu_to_be32(i, iv);
-
- ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
- if (ret)
- goto out;
-
- i++;
- templen += drbg_blocklen(drbg);
- }
-
- X = temp + (drbg_keylen(drbg));
- drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
-
-
- drbg_kcapi_symsetkey(drbg, temp);
- while (generated_len < bytes_to_return) {
- short blocklen = 0;
-
- ret = drbg_kcapi_sym(drbg, X, &cipherin);
- if (ret)
- goto out;
- blocklen = (drbg_blocklen(drbg) <
- (bytes_to_return - generated_len)) ?
- drbg_blocklen(drbg) :
- (bytes_to_return - generated_len);
-
- memcpy(df_data + generated_len, X, blocklen);
- generated_len += blocklen;
- }
- ret = 0;
- out:
- memset(iv, 0, drbg_blocklen(drbg));
- memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
- memset(pad, 0, drbg_blocklen(drbg));
- return ret;
- }
- static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
- int reseed)
- {
- int ret = -EFAULT;
-
- unsigned char *temp = drbg->scratchpad;
- unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
- drbg_blocklen(drbg);
- if (3 > reseed)
- memset(df_data, 0, drbg_statelen(drbg));
- if (!reseed) {
-
- crypto_inc(drbg->V, drbg_blocklen(drbg));
- ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
- drbg_keylen(drbg));
- if (ret)
- goto out;
- }
-
- if (seed) {
- ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
- if (ret)
- goto out;
- }
- ret = drbg_kcapi_sym_ctr(drbg, df_data, drbg_statelen(drbg),
- temp, drbg_statelen(drbg));
- if (ret)
- return ret;
-
- ret = crypto_skcipher_setkey(drbg->ctr_handle, temp,
- drbg_keylen(drbg));
- if (ret)
- goto out;
-
- memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
-
- crypto_inc(drbg->V, drbg_blocklen(drbg));
- ret = 0;
- out:
- memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
- if (2 != reseed)
- memset(df_data, 0, drbg_statelen(drbg));
- return ret;
- }
- static int drbg_ctr_generate(struct drbg_state *drbg,
- unsigned char *buf, unsigned int buflen,
- struct list_head *addtl)
- {
- int ret;
- int len = min_t(int, buflen, INT_MAX);
-
- if (addtl && !list_empty(addtl)) {
- ret = drbg_ctr_update(drbg, addtl, 2);
- if (ret)
- return 0;
- }
-
- ret = drbg_kcapi_sym_ctr(drbg, drbg->ctr_null_value, DRBG_CTR_NULL_LEN,
- buf, len);
- if (ret)
- return ret;
-
- ret = drbg_ctr_update(drbg, NULL, 3);
- if (ret)
- len = ret;
- return len;
- }
- static const struct drbg_state_ops drbg_ctr_ops = {
- .update = drbg_ctr_update,
- .generate = drbg_ctr_generate,
- .crypto_init = drbg_init_sym_kernel,
- .crypto_fini = drbg_fini_sym_kernel,
- };
- #endif
- #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
- static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
- const struct list_head *in);
- static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
- const unsigned char *key);
- static int drbg_init_hash_kernel(struct drbg_state *drbg);
- static int drbg_fini_hash_kernel(struct drbg_state *drbg);
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_HMAC
- #define CRYPTO_DRBG_HMAC_STRING "HMAC "
- MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
- MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
- MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
- MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
- MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
- MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
- MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
- MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
- static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
- int reseed)
- {
- int ret = -EFAULT;
- int i = 0;
- struct drbg_string seed1, seed2, vdata;
- LIST_HEAD(seedlist);
- LIST_HEAD(vdatalist);
- if (!reseed) {
-
- memset(drbg->V, 1, drbg_statelen(drbg));
- drbg_kcapi_hmacsetkey(drbg, drbg->C);
- }
- drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
- list_add_tail(&seed1.list, &seedlist);
-
- drbg_string_fill(&seed2, NULL, 1);
- list_add_tail(&seed2.list, &seedlist);
-
- if (seed)
- list_splice_tail(seed, &seedlist);
- drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
- list_add_tail(&vdata.list, &vdatalist);
- for (i = 2; 0 < i; i--) {
-
- unsigned char prefix = DRBG_PREFIX0;
- if (1 == i)
- prefix = DRBG_PREFIX1;
-
- seed2.buf = &prefix;
- ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist);
- if (ret)
- return ret;
- drbg_kcapi_hmacsetkey(drbg, drbg->C);
-
- ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist);
- if (ret)
- return ret;
-
- if (!seed)
- return ret;
- }
- return 0;
- }
- static int drbg_hmac_generate(struct drbg_state *drbg,
- unsigned char *buf,
- unsigned int buflen,
- struct list_head *addtl)
- {
- int len = 0;
- int ret = 0;
- struct drbg_string data;
- LIST_HEAD(datalist);
-
- if (addtl && !list_empty(addtl)) {
- ret = drbg_hmac_update(drbg, addtl, 1);
- if (ret)
- return ret;
- }
- drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
- list_add_tail(&data.list, &datalist);
- while (len < buflen) {
- unsigned int outlen = 0;
-
- ret = drbg_kcapi_hash(drbg, drbg->V, &datalist);
- if (ret)
- return ret;
- outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
- drbg_blocklen(drbg) : (buflen - len);
-
- memcpy(buf + len, drbg->V, outlen);
- len += outlen;
- }
-
- if (addtl && !list_empty(addtl))
- ret = drbg_hmac_update(drbg, addtl, 1);
- else
- ret = drbg_hmac_update(drbg, NULL, 1);
- if (ret)
- return ret;
- return len;
- }
- static const struct drbg_state_ops drbg_hmac_ops = {
- .update = drbg_hmac_update,
- .generate = drbg_hmac_generate,
- .crypto_init = drbg_init_hash_kernel,
- .crypto_fini = drbg_fini_hash_kernel,
- };
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_HASH
- #define CRYPTO_DRBG_HASH_STRING "HASH "
- MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
- MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
- MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
- MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
- MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
- MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
- MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
- MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
- static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
- const unsigned char *add, size_t addlen)
- {
-
- unsigned char *dstptr;
- const unsigned char *addptr;
- unsigned int remainder = 0;
- size_t len = addlen;
- dstptr = dst + (dstlen-1);
- addptr = add + (addlen-1);
- while (len) {
- remainder += *dstptr + *addptr;
- *dstptr = remainder & 0xff;
- remainder >>= 8;
- len--; dstptr--; addptr--;
- }
- len = dstlen - addlen;
- while (len && remainder > 0) {
- remainder = *dstptr + 1;
- *dstptr = remainder & 0xff;
- remainder >>= 8;
- len--; dstptr--;
- }
- }
- static int drbg_hash_df(struct drbg_state *drbg,
- unsigned char *outval, size_t outlen,
- struct list_head *entropylist)
- {
- int ret = 0;
- size_t len = 0;
- unsigned char input[5];
- unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
- struct drbg_string data;
-
- input[0] = 1;
- drbg_cpu_to_be32((outlen * 8), &input[1]);
-
- drbg_string_fill(&data, input, 5);
- list_add(&data.list, entropylist);
-
- while (len < outlen) {
- short blocklen = 0;
-
- ret = drbg_kcapi_hash(drbg, tmp, entropylist);
- if (ret)
- goto out;
-
- input[0]++;
- blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
- drbg_blocklen(drbg) : (outlen - len);
- memcpy(outval + len, tmp, blocklen);
- len += blocklen;
- }
- out:
- memset(tmp, 0, drbg_blocklen(drbg));
- return ret;
- }
- static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
- int reseed)
- {
- int ret = 0;
- struct drbg_string data1, data2;
- LIST_HEAD(datalist);
- LIST_HEAD(datalist2);
- unsigned char *V = drbg->scratchpad;
- unsigned char prefix = DRBG_PREFIX1;
- if (!seed)
- return -EINVAL;
- if (reseed) {
-
- memcpy(V, drbg->V, drbg_statelen(drbg));
- drbg_string_fill(&data1, &prefix, 1);
- list_add_tail(&data1.list, &datalist);
- drbg_string_fill(&data2, V, drbg_statelen(drbg));
- list_add_tail(&data2.list, &datalist);
- }
- list_splice_tail(seed, &datalist);
-
- ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
- if (ret)
- goto out;
-
- prefix = DRBG_PREFIX0;
- drbg_string_fill(&data1, &prefix, 1);
- list_add_tail(&data1.list, &datalist2);
- drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
- list_add_tail(&data2.list, &datalist2);
-
- ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
- out:
- memset(drbg->scratchpad, 0, drbg_statelen(drbg));
- return ret;
- }
- static int drbg_hash_process_addtl(struct drbg_state *drbg,
- struct list_head *addtl)
- {
- int ret = 0;
- struct drbg_string data1, data2;
- LIST_HEAD(datalist);
- unsigned char prefix = DRBG_PREFIX2;
-
- if (!addtl || list_empty(addtl))
- return 0;
-
- drbg_string_fill(&data1, &prefix, 1);
- drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
- list_add_tail(&data1.list, &datalist);
- list_add_tail(&data2.list, &datalist);
- list_splice_tail(addtl, &datalist);
- ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
- if (ret)
- goto out;
-
- drbg_add_buf(drbg->V, drbg_statelen(drbg),
- drbg->scratchpad, drbg_blocklen(drbg));
- out:
- memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
- return ret;
- }
- static int drbg_hash_hashgen(struct drbg_state *drbg,
- unsigned char *buf,
- unsigned int buflen)
- {
- int len = 0;
- int ret = 0;
- unsigned char *src = drbg->scratchpad;
- unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
- struct drbg_string data;
- LIST_HEAD(datalist);
-
- memcpy(src, drbg->V, drbg_statelen(drbg));
- drbg_string_fill(&data, src, drbg_statelen(drbg));
- list_add_tail(&data.list, &datalist);
- while (len < buflen) {
- unsigned int outlen = 0;
-
- ret = drbg_kcapi_hash(drbg, dst, &datalist);
- if (ret) {
- len = ret;
- goto out;
- }
- outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
- drbg_blocklen(drbg) : (buflen - len);
-
- memcpy(buf + len, dst, outlen);
- len += outlen;
-
- if (len < buflen)
- crypto_inc(src, drbg_statelen(drbg));
- }
- out:
- memset(drbg->scratchpad, 0,
- (drbg_statelen(drbg) + drbg_blocklen(drbg)));
- return len;
- }
- static int drbg_hash_generate(struct drbg_state *drbg,
- unsigned char *buf, unsigned int buflen,
- struct list_head *addtl)
- {
- int len = 0;
- int ret = 0;
- union {
- unsigned char req[8];
- __be64 req_int;
- } u;
- unsigned char prefix = DRBG_PREFIX3;
- struct drbg_string data1, data2;
- LIST_HEAD(datalist);
-
- ret = drbg_hash_process_addtl(drbg, addtl);
- if (ret)
- return ret;
-
- len = drbg_hash_hashgen(drbg, buf, buflen);
-
-
- drbg_string_fill(&data1, &prefix, 1);
- list_add_tail(&data1.list, &datalist);
- drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
- list_add_tail(&data2.list, &datalist);
- ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
- if (ret) {
- len = ret;
- goto out;
- }
-
- drbg_add_buf(drbg->V, drbg_statelen(drbg),
- drbg->scratchpad, drbg_blocklen(drbg));
- drbg_add_buf(drbg->V, drbg_statelen(drbg),
- drbg->C, drbg_statelen(drbg));
- u.req_int = cpu_to_be64(drbg->reseed_ctr);
- drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
- out:
- memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
- return len;
- }
- static const struct drbg_state_ops drbg_hash_ops = {
- .update = drbg_hash_update,
- .generate = drbg_hash_generate,
- .crypto_init = drbg_init_hash_kernel,
- .crypto_fini = drbg_fini_hash_kernel,
- };
- #endif
- static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,
- int reseed)
- {
- int ret = drbg->d_ops->update(drbg, seed, reseed);
- if (ret)
- return ret;
- drbg->seeded = true;
-
- drbg->reseed_ctr = 1;
- return ret;
- }
- static void drbg_async_seed(struct work_struct *work)
- {
- struct drbg_string data;
- LIST_HEAD(seedlist);
- struct drbg_state *drbg = container_of(work, struct drbg_state,
- seed_work);
- unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
- unsigned char entropy[32];
- BUG_ON(!entropylen);
- BUG_ON(entropylen > sizeof(entropy));
- get_random_bytes(entropy, entropylen);
- drbg_string_fill(&data, entropy, entropylen);
- list_add_tail(&data.list, &seedlist);
- mutex_lock(&drbg->drbg_mutex);
-
- crypto_free_rng(drbg->jent);
- drbg->jent = NULL;
-
- drbg->seeded = false;
- __drbg_seed(drbg, &seedlist, true);
- if (drbg->seeded)
- drbg->reseed_threshold = drbg_max_requests(drbg);
- mutex_unlock(&drbg->drbg_mutex);
- memzero_explicit(entropy, entropylen);
- }
- static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
- bool reseed)
- {
- int ret;
- unsigned char entropy[((32 + 16) * 2)];
- unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
- struct drbg_string data1;
- LIST_HEAD(seedlist);
-
- if (pers && pers->len > (drbg_max_addtl(drbg))) {
- pr_devel("DRBG: personalization string too long %zu\n",
- pers->len);
- return -EINVAL;
- }
- if (list_empty(&drbg->test_data.list)) {
- drbg_string_fill(&data1, drbg->test_data.buf,
- drbg->test_data.len);
- pr_devel("DRBG: using test entropy\n");
- } else {
-
- BUG_ON(!entropylen);
- if (!reseed)
- entropylen = ((entropylen + 1) / 2) * 3;
- BUG_ON((entropylen * 2) > sizeof(entropy));
-
- get_random_bytes(entropy, entropylen);
- if (!drbg->jent) {
- drbg_string_fill(&data1, entropy, entropylen);
- pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
- entropylen);
- } else {
-
- ret = crypto_rng_get_bytes(drbg->jent,
- entropy + entropylen,
- entropylen);
- if (ret) {
- pr_devel("DRBG: jent failed with %d\n", ret);
- return ret;
- }
- drbg_string_fill(&data1, entropy, entropylen * 2);
- pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
- entropylen * 2);
- }
- }
- list_add_tail(&data1.list, &seedlist);
-
- if (pers && pers->buf && 0 < pers->len) {
- list_add_tail(&pers->list, &seedlist);
- pr_devel("DRBG: using personalization string\n");
- }
- if (!reseed) {
- memset(drbg->V, 0, drbg_statelen(drbg));
- memset(drbg->C, 0, drbg_statelen(drbg));
- }
- ret = __drbg_seed(drbg, &seedlist, reseed);
- memzero_explicit(entropy, entropylen * 2);
- return ret;
- }
- static inline void drbg_dealloc_state(struct drbg_state *drbg)
- {
- if (!drbg)
- return;
- kzfree(drbg->Vbuf);
- drbg->Vbuf = NULL;
- drbg->V = NULL;
- kzfree(drbg->Cbuf);
- drbg->Cbuf = NULL;
- drbg->C = NULL;
- kzfree(drbg->scratchpadbuf);
- drbg->scratchpadbuf = NULL;
- drbg->reseed_ctr = 0;
- drbg->d_ops = NULL;
- drbg->core = NULL;
- }
- static inline int drbg_alloc_state(struct drbg_state *drbg)
- {
- int ret = -ENOMEM;
- unsigned int sb_size = 0;
- switch (drbg->core->flags & DRBG_TYPE_MASK) {
- #ifdef CONFIG_CRYPTO_DRBG_HMAC
- case DRBG_HMAC:
- drbg->d_ops = &drbg_hmac_ops;
- break;
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_HASH
- case DRBG_HASH:
- drbg->d_ops = &drbg_hash_ops;
- break;
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_CTR
- case DRBG_CTR:
- drbg->d_ops = &drbg_ctr_ops;
- break;
- #endif
- default:
- ret = -EOPNOTSUPP;
- goto err;
- }
- ret = drbg->d_ops->crypto_init(drbg);
- if (ret < 0)
- goto err;
- drbg->Vbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
- if (!drbg->Vbuf) {
- ret = -ENOMEM;
- goto fini;
- }
- drbg->V = PTR_ALIGN(drbg->Vbuf, ret + 1);
- drbg->Cbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
- if (!drbg->Cbuf) {
- ret = -ENOMEM;
- goto fini;
- }
- drbg->C = PTR_ALIGN(drbg->Cbuf, ret + 1);
-
- if (drbg->core->flags & DRBG_HMAC)
- sb_size = 0;
- else if (drbg->core->flags & DRBG_CTR)
- sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) +
- drbg_statelen(drbg) +
- drbg_blocklen(drbg) +
- drbg_blocklen(drbg) +
- drbg_statelen(drbg) + drbg_blocklen(drbg);
- else
- sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
- if (0 < sb_size) {
- drbg->scratchpadbuf = kzalloc(sb_size + ret, GFP_KERNEL);
- if (!drbg->scratchpadbuf) {
- ret = -ENOMEM;
- goto fini;
- }
- drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1);
- }
- return 0;
- fini:
- drbg->d_ops->crypto_fini(drbg);
- err:
- drbg_dealloc_state(drbg);
- return ret;
- }
- static int drbg_generate(struct drbg_state *drbg,
- unsigned char *buf, unsigned int buflen,
- struct drbg_string *addtl)
- {
- int len = 0;
- LIST_HEAD(addtllist);
- if (!drbg->core) {
- pr_devel("DRBG: not yet seeded\n");
- return -EINVAL;
- }
- if (0 == buflen || !buf) {
- pr_devel("DRBG: no output buffer provided\n");
- return -EINVAL;
- }
- if (addtl && NULL == addtl->buf && 0 < addtl->len) {
- pr_devel("DRBG: wrong format of additional information\n");
- return -EINVAL;
- }
-
- len = -EINVAL;
- if (buflen > (drbg_max_request_bytes(drbg))) {
- pr_devel("DRBG: requested random numbers too large %u\n",
- buflen);
- goto err;
- }
-
-
- if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
- pr_devel("DRBG: additional information string too long %zu\n",
- addtl->len);
- goto err;
- }
-
-
- if (drbg->reseed_threshold < drbg->reseed_ctr)
- drbg->seeded = false;
- if (drbg->pr || !drbg->seeded) {
- pr_devel("DRBG: reseeding before generation (prediction "
- "resistance: %s, state %s)\n",
- drbg->pr ? "true" : "false",
- drbg->seeded ? "seeded" : "unseeded");
-
- len = drbg_seed(drbg, addtl, true);
- if (len)
- goto err;
-
- addtl = NULL;
- }
- if (addtl && 0 < addtl->len)
- list_add_tail(&addtl->list, &addtllist);
-
- len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
-
- drbg->reseed_ctr++;
- if (0 >= len)
- goto err;
-
- #if 0
- if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) {
- int err = 0;
- pr_devel("DRBG: start to perform self test\n");
- if (drbg->core->flags & DRBG_HMAC)
- err = alg_test("drbg_pr_hmac_sha256",
- "drbg_pr_hmac_sha256", 0, 0);
- else if (drbg->core->flags & DRBG_CTR)
- err = alg_test("drbg_pr_ctr_aes128",
- "drbg_pr_ctr_aes128", 0, 0);
- else
- err = alg_test("drbg_pr_sha256",
- "drbg_pr_sha256", 0, 0);
- if (err) {
- pr_err("DRBG: periodical self test failed\n");
-
- drbg_uninstantiate(drbg);
- return 0;
- } else {
- pr_devel("DRBG: self test successful\n");
- }
- }
- #endif
-
- len = 0;
- err:
- return len;
- }
- static int drbg_generate_long(struct drbg_state *drbg,
- unsigned char *buf, unsigned int buflen,
- struct drbg_string *addtl)
- {
- unsigned int len = 0;
- unsigned int slice = 0;
- do {
- int err = 0;
- unsigned int chunk = 0;
- slice = ((buflen - len) / drbg_max_request_bytes(drbg));
- chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
- mutex_lock(&drbg->drbg_mutex);
- err = drbg_generate(drbg, buf + len, chunk, addtl);
- mutex_unlock(&drbg->drbg_mutex);
- if (0 > err)
- return err;
- len += chunk;
- } while (slice > 0 && (len < buflen));
- return 0;
- }
- static void drbg_schedule_async_seed(struct random_ready_callback *rdy)
- {
- struct drbg_state *drbg = container_of(rdy, struct drbg_state,
- random_ready);
- schedule_work(&drbg->seed_work);
- }
- static int drbg_prepare_hrng(struct drbg_state *drbg)
- {
- int err;
-
- if (list_empty(&drbg->test_data.list))
- return 0;
- INIT_WORK(&drbg->seed_work, drbg_async_seed);
- drbg->random_ready.owner = THIS_MODULE;
- drbg->random_ready.func = drbg_schedule_async_seed;
- err = add_random_ready_callback(&drbg->random_ready);
- switch (err) {
- case 0:
- break;
- case -EALREADY:
- err = 0;
-
- default:
- drbg->random_ready.func = NULL;
- return err;
- }
- drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0);
-
- drbg->reseed_threshold = 50;
- return err;
- }
- static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
- int coreref, bool pr)
- {
- int ret;
- bool reseed = true;
- pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
- "%s\n", coreref, pr ? "enabled" : "disabled");
- mutex_lock(&drbg->drbg_mutex);
-
-
-
- if (!drbg->core) {
- drbg->core = &drbg_cores[coreref];
- drbg->pr = pr;
- drbg->seeded = false;
- drbg->reseed_threshold = drbg_max_requests(drbg);
- ret = drbg_alloc_state(drbg);
- if (ret)
- goto unlock;
- ret = drbg_prepare_hrng(drbg);
- if (ret)
- goto free_everything;
- if (IS_ERR(drbg->jent)) {
- ret = PTR_ERR(drbg->jent);
- drbg->jent = NULL;
- if (fips_enabled || ret != -ENOENT)
- goto free_everything;
- pr_info("DRBG: Continuing without Jitter RNG\n");
- }
- reseed = false;
- }
- ret = drbg_seed(drbg, pers, reseed);
- if (ret && !reseed)
- goto free_everything;
- mutex_unlock(&drbg->drbg_mutex);
- return ret;
- unlock:
- mutex_unlock(&drbg->drbg_mutex);
- return ret;
- free_everything:
- mutex_unlock(&drbg->drbg_mutex);
- drbg_uninstantiate(drbg);
- return ret;
- }
- static int drbg_uninstantiate(struct drbg_state *drbg)
- {
- if (drbg->random_ready.func) {
- del_random_ready_callback(&drbg->random_ready);
- cancel_work_sync(&drbg->seed_work);
- crypto_free_rng(drbg->jent);
- drbg->jent = NULL;
- }
- if (drbg->d_ops)
- drbg->d_ops->crypto_fini(drbg);
- drbg_dealloc_state(drbg);
-
- return 0;
- }
- static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
- const u8 *data, unsigned int len)
- {
- struct drbg_state *drbg = crypto_rng_ctx(tfm);
- mutex_lock(&drbg->drbg_mutex);
- drbg_string_fill(&drbg->test_data, data, len);
- mutex_unlock(&drbg->drbg_mutex);
- }
- #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
- struct sdesc {
- struct shash_desc shash;
- char ctx[];
- };
- static int drbg_init_hash_kernel(struct drbg_state *drbg)
- {
- struct sdesc *sdesc;
- struct crypto_shash *tfm;
- tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
- if (IS_ERR(tfm)) {
- pr_info("DRBG: could not allocate digest TFM handle: %s\n",
- drbg->core->backend_cra_name);
- return PTR_ERR(tfm);
- }
- BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
- sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
- GFP_KERNEL);
- if (!sdesc) {
- crypto_free_shash(tfm);
- return -ENOMEM;
- }
- sdesc->shash.tfm = tfm;
- sdesc->shash.flags = 0;
- drbg->priv_data = sdesc;
- return crypto_shash_alignmask(tfm);
- }
- static int drbg_fini_hash_kernel(struct drbg_state *drbg)
- {
- struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
- if (sdesc) {
- crypto_free_shash(sdesc->shash.tfm);
- kzfree(sdesc);
- }
- drbg->priv_data = NULL;
- return 0;
- }
- static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
- const unsigned char *key)
- {
- struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
- crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
- }
- static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
- const struct list_head *in)
- {
- struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
- struct drbg_string *input = NULL;
- crypto_shash_init(&sdesc->shash);
- list_for_each_entry(input, in, list)
- crypto_shash_update(&sdesc->shash, input->buf, input->len);
- return crypto_shash_final(&sdesc->shash, outval);
- }
- #endif
- #ifdef CONFIG_CRYPTO_DRBG_CTR
- static int drbg_fini_sym_kernel(struct drbg_state *drbg)
- {
- struct crypto_cipher *tfm =
- (struct crypto_cipher *)drbg->priv_data;
- if (tfm)
- crypto_free_cipher(tfm);
- drbg->priv_data = NULL;
- if (drbg->ctr_handle)
- crypto_free_skcipher(drbg->ctr_handle);
- drbg->ctr_handle = NULL;
- if (drbg->ctr_req)
- skcipher_request_free(drbg->ctr_req);
- drbg->ctr_req = NULL;
- kfree(drbg->ctr_null_value_buf);
- drbg->ctr_null_value = NULL;
- kfree(drbg->outscratchpadbuf);
- drbg->outscratchpadbuf = NULL;
- return 0;
- }
- static void drbg_skcipher_cb(struct crypto_async_request *req, int error)
- {
- struct drbg_state *drbg = req->data;
- if (error == -EINPROGRESS)
- return;
- drbg->ctr_async_err = error;
- complete(&drbg->ctr_completion);
- }
- static int drbg_init_sym_kernel(struct drbg_state *drbg)
- {
- struct crypto_cipher *tfm;
- struct crypto_skcipher *sk_tfm;
- struct skcipher_request *req;
- unsigned int alignmask;
- char ctr_name[CRYPTO_MAX_ALG_NAME];
- tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0);
- if (IS_ERR(tfm)) {
- pr_info("DRBG: could not allocate cipher TFM handle: %s\n",
- drbg->core->backend_cra_name);
- return PTR_ERR(tfm);
- }
- BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm));
- drbg->priv_data = tfm;
- if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
- drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) {
- drbg_fini_sym_kernel(drbg);
- return -EINVAL;
- }
- sk_tfm = crypto_alloc_skcipher(ctr_name, 0, 0);
- if (IS_ERR(sk_tfm)) {
- pr_info("DRBG: could not allocate CTR cipher TFM handle: %s\n",
- ctr_name);
- drbg_fini_sym_kernel(drbg);
- return PTR_ERR(sk_tfm);
- }
- drbg->ctr_handle = sk_tfm;
- init_completion(&drbg->ctr_completion);
- req = skcipher_request_alloc(sk_tfm, GFP_KERNEL);
- if (!req) {
- pr_info("DRBG: could not allocate request queue\n");
- drbg_fini_sym_kernel(drbg);
- return -ENOMEM;
- }
- drbg->ctr_req = req;
- skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
- drbg_skcipher_cb, drbg);
- alignmask = crypto_skcipher_alignmask(sk_tfm);
- drbg->ctr_null_value_buf = kzalloc(DRBG_CTR_NULL_LEN + alignmask,
- GFP_KERNEL);
- if (!drbg->ctr_null_value_buf) {
- drbg_fini_sym_kernel(drbg);
- return -ENOMEM;
- }
- drbg->ctr_null_value = (u8 *)PTR_ALIGN(drbg->ctr_null_value_buf,
- alignmask + 1);
- drbg->outscratchpadbuf = kmalloc(DRBG_OUTSCRATCHLEN + alignmask,
- GFP_KERNEL);
- if (!drbg->outscratchpadbuf) {
- drbg_fini_sym_kernel(drbg);
- return -ENOMEM;
- }
- drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf,
- alignmask + 1);
- return alignmask;
- }
- static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
- const unsigned char *key)
- {
- struct crypto_cipher *tfm =
- (struct crypto_cipher *)drbg->priv_data;
- crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
- }
- static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
- const struct drbg_string *in)
- {
- struct crypto_cipher *tfm =
- (struct crypto_cipher *)drbg->priv_data;
-
- BUG_ON(in->len < drbg_blocklen(drbg));
- crypto_cipher_encrypt_one(tfm, outval, in->buf);
- return 0;
- }
- static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
- u8 *inbuf, u32 inlen,
- u8 *outbuf, u32 outlen)
- {
- struct scatterlist sg_in;
- int ret;
- sg_init_one(&sg_in, inbuf, inlen);
- while (outlen) {
- u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
- struct scatterlist sg_out;
-
- sg_init_one(&sg_out, drbg->outscratchpad, cryptlen);
- skcipher_request_set_crypt(drbg->ctr_req, &sg_in, &sg_out,
- cryptlen, drbg->V);
- ret = crypto_skcipher_encrypt(drbg->ctr_req);
- switch (ret) {
- case 0:
- break;
- case -EINPROGRESS:
- case -EBUSY:
- wait_for_completion(&drbg->ctr_completion);
- if (!drbg->ctr_async_err) {
- reinit_completion(&drbg->ctr_completion);
- break;
- }
- default:
- goto out;
- }
- init_completion(&drbg->ctr_completion);
- memcpy(outbuf, drbg->outscratchpad, cryptlen);
- outlen -= cryptlen;
- }
- ret = 0;
- out:
- memzero_explicit(drbg->outscratchpad, DRBG_OUTSCRATCHLEN);
- return ret;
- }
- #endif
- static inline void drbg_convert_tfm_core(const char *cra_driver_name,
- int *coreref, bool *pr)
- {
- int i = 0;
- size_t start = 0;
- int len = 0;
- *pr = true;
-
- if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
- start = 10;
- *pr = false;
- } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
- start = 8;
- } else {
- return;
- }
-
- len = strlen(cra_driver_name) - start;
- for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
- if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
- len)) {
- *coreref = i;
- return;
- }
- }
- }
- static int drbg_kcapi_init(struct crypto_tfm *tfm)
- {
- struct drbg_state *drbg = crypto_tfm_ctx(tfm);
- mutex_init(&drbg->drbg_mutex);
- return 0;
- }
- static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
- {
- drbg_uninstantiate(crypto_tfm_ctx(tfm));
- }
- static int drbg_kcapi_random(struct crypto_rng *tfm,
- const u8 *src, unsigned int slen,
- u8 *dst, unsigned int dlen)
- {
- struct drbg_state *drbg = crypto_rng_ctx(tfm);
- struct drbg_string *addtl = NULL;
- struct drbg_string string;
- if (slen) {
-
- drbg_string_fill(&string, src, slen);
- addtl = &string;
- }
- return drbg_generate_long(drbg, dst, dlen, addtl);
- }
- static int drbg_kcapi_seed(struct crypto_rng *tfm,
- const u8 *seed, unsigned int slen)
- {
- struct drbg_state *drbg = crypto_rng_ctx(tfm);
- struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
- bool pr = false;
- struct drbg_string string;
- struct drbg_string *seed_string = NULL;
- int coreref = 0;
- drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
- &pr);
- if (0 < slen) {
- drbg_string_fill(&string, seed, slen);
- seed_string = &string;
- }
- return drbg_instantiate(drbg, seed_string, coreref, pr);
- }
- static inline int __init drbg_healthcheck_sanity(void)
- {
- int len = 0;
- #define OUTBUFLEN 16
- unsigned char buf[OUTBUFLEN];
- struct drbg_state *drbg = NULL;
- int ret = -EFAULT;
- int rc = -EFAULT;
- bool pr = false;
- int coreref = 0;
- struct drbg_string addtl;
- size_t max_addtllen, max_request_bytes;
-
- if (!fips_enabled)
- return 0;
- #ifdef CONFIG_CRYPTO_DRBG_CTR
- drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
- #elif defined CONFIG_CRYPTO_DRBG_HASH
- drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
- #else
- drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
- #endif
- drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
- if (!drbg)
- return -ENOMEM;
- mutex_init(&drbg->drbg_mutex);
- drbg->core = &drbg_cores[coreref];
- drbg->reseed_threshold = drbg_max_requests(drbg);
-
- max_addtllen = drbg_max_addtl(drbg);
- max_request_bytes = drbg_max_request_bytes(drbg);
- drbg_string_fill(&addtl, buf, max_addtllen + 1);
-
- len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
- BUG_ON(0 < len);
-
- len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
- BUG_ON(0 < len);
-
- ret = drbg_seed(drbg, &addtl, false);
- BUG_ON(0 == ret);
-
- rc = 0;
- pr_devel("DRBG: Sanity tests for failure code paths successfully "
- "completed\n");
- kfree(drbg);
- return rc;
- }
- static struct rng_alg drbg_algs[22];
- static inline void __init drbg_fill_array(struct rng_alg *alg,
- const struct drbg_core *core, int pr)
- {
- int pos = 0;
- static int priority = 200;
- memcpy(alg->base.cra_name, "stdrng", 6);
- if (pr) {
- memcpy(alg->base.cra_driver_name, "drbg_pr_", 8);
- pos = 8;
- } else {
- memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10);
- pos = 10;
- }
- memcpy(alg->base.cra_driver_name + pos, core->cra_name,
- strlen(core->cra_name));
- alg->base.cra_priority = priority;
- priority++;
-
- if (fips_enabled)
- alg->base.cra_priority += 200;
- alg->base.cra_ctxsize = sizeof(struct drbg_state);
- alg->base.cra_module = THIS_MODULE;
- alg->base.cra_init = drbg_kcapi_init;
- alg->base.cra_exit = drbg_kcapi_cleanup;
- alg->generate = drbg_kcapi_random;
- alg->seed = drbg_kcapi_seed;
- alg->set_ent = drbg_kcapi_set_entropy;
- alg->seedsize = 0;
- }
- static int __init drbg_init(void)
- {
- unsigned int i = 0;
- unsigned int j = 0;
- int ret;
- ret = drbg_healthcheck_sanity();
- if (ret)
- return ret;
- if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
- pr_info("DRBG: Cannot register all DRBG types"
- "(slots needed: %zu, slots available: %zu)\n",
- ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
- return -EFAULT;
- }
-
- for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
- drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
- for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
- drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
- return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
- }
- static void __exit drbg_exit(void)
- {
- crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
- }
- module_init(drbg_init);
- module_exit(drbg_exit);
- #ifndef CRYPTO_DRBG_HASH_STRING
- #define CRYPTO_DRBG_HASH_STRING ""
- #endif
- #ifndef CRYPTO_DRBG_HMAC_STRING
- #define CRYPTO_DRBG_HMAC_STRING ""
- #endif
- #ifndef CRYPTO_DRBG_CTR_STRING
- #define CRYPTO_DRBG_CTR_STRING ""
- #endif
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
- MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
- "using following cores: "
- CRYPTO_DRBG_HASH_STRING
- CRYPTO_DRBG_HMAC_STRING
- CRYPTO_DRBG_CTR_STRING);
- MODULE_ALIAS_CRYPTO("stdrng");
|