rsa-pkcs1pad.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * RSA padding templates.
  3. *
  4. * Copyright (c) 2015 Intel Corporation
  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. #include <crypto/algapi.h>
  12. #include <crypto/akcipher.h>
  13. #include <crypto/internal/akcipher.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/random.h>
  19. /*
  20. * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
  21. */
  22. static const u8 rsa_digest_info_md5[] = {
  23. 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
  24. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
  25. 0x05, 0x00, 0x04, 0x10
  26. };
  27. static const u8 rsa_digest_info_sha1[] = {
  28. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  29. 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  30. 0x05, 0x00, 0x04, 0x14
  31. };
  32. static const u8 rsa_digest_info_rmd160[] = {
  33. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  34. 0x2b, 0x24, 0x03, 0x02, 0x01,
  35. 0x05, 0x00, 0x04, 0x14
  36. };
  37. static const u8 rsa_digest_info_sha224[] = {
  38. 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
  39. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
  40. 0x05, 0x00, 0x04, 0x1c
  41. };
  42. static const u8 rsa_digest_info_sha256[] = {
  43. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
  44. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  45. 0x05, 0x00, 0x04, 0x20
  46. };
  47. static const u8 rsa_digest_info_sha384[] = {
  48. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
  49. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
  50. 0x05, 0x00, 0x04, 0x30
  51. };
  52. static const u8 rsa_digest_info_sha512[] = {
  53. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
  54. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
  55. 0x05, 0x00, 0x04, 0x40
  56. };
  57. static const struct rsa_asn1_template {
  58. const char *name;
  59. const u8 *data;
  60. size_t size;
  61. } rsa_asn1_templates[] = {
  62. #define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }
  63. _(md5),
  64. _(sha1),
  65. _(rmd160),
  66. _(sha256),
  67. _(sha384),
  68. _(sha512),
  69. _(sha224),
  70. { NULL }
  71. #undef _
  72. };
  73. static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
  74. {
  75. const struct rsa_asn1_template *p;
  76. for (p = rsa_asn1_templates; p->name; p++)
  77. if (strcmp(name, p->name) == 0)
  78. return p;
  79. return NULL;
  80. }
  81. struct pkcs1pad_ctx {
  82. struct crypto_akcipher *child;
  83. unsigned int key_size;
  84. };
  85. struct pkcs1pad_inst_ctx {
  86. struct crypto_akcipher_spawn spawn;
  87. const struct rsa_asn1_template *digest_info;
  88. };
  89. struct pkcs1pad_request {
  90. struct scatterlist in_sg[2], out_sg[1];
  91. uint8_t *in_buf, *out_buf;
  92. struct akcipher_request child_req;
  93. };
  94. static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
  95. unsigned int keylen)
  96. {
  97. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  98. int err;
  99. ctx->key_size = 0;
  100. err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
  101. if (err)
  102. return err;
  103. /* Find out new modulus size from rsa implementation */
  104. err = crypto_akcipher_maxsize(ctx->child);
  105. if (err > PAGE_SIZE)
  106. return -ENOTSUPP;
  107. ctx->key_size = err;
  108. return 0;
  109. }
  110. static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
  111. unsigned int keylen)
  112. {
  113. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  114. int err;
  115. ctx->key_size = 0;
  116. err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
  117. if (err)
  118. return err;
  119. /* Find out new modulus size from rsa implementation */
  120. err = crypto_akcipher_maxsize(ctx->child);
  121. if (err > PAGE_SIZE)
  122. return -ENOTSUPP;
  123. ctx->key_size = err;
  124. return 0;
  125. }
  126. static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
  127. {
  128. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  129. /*
  130. * The maximum destination buffer size for the encrypt/sign operations
  131. * will be the same as for RSA, even though it's smaller for
  132. * decrypt/verify.
  133. */
  134. return ctx->key_size;
  135. }
  136. static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
  137. struct scatterlist *next)
  138. {
  139. int nsegs = next ? 2 : 1;
  140. sg_init_table(sg, nsegs);
  141. sg_set_buf(sg, buf, len);
  142. if (next)
  143. sg_chain(sg, nsegs, next);
  144. }
  145. static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
  146. {
  147. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  148. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  149. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  150. unsigned int pad_len;
  151. unsigned int len;
  152. u8 *out_buf;
  153. if (err)
  154. goto out;
  155. len = req_ctx->child_req.dst_len;
  156. pad_len = ctx->key_size - len;
  157. /* Four billion to one */
  158. if (likely(!pad_len))
  159. goto out;
  160. out_buf = kzalloc(ctx->key_size, GFP_KERNEL);
  161. err = -ENOMEM;
  162. if (!out_buf)
  163. goto out;
  164. sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
  165. out_buf + pad_len, len);
  166. sg_copy_from_buffer(req->dst,
  167. sg_nents_for_len(req->dst, ctx->key_size),
  168. out_buf, ctx->key_size);
  169. kzfree(out_buf);
  170. out:
  171. req->dst_len = ctx->key_size;
  172. kfree(req_ctx->in_buf);
  173. return err;
  174. }
  175. static void pkcs1pad_encrypt_sign_complete_cb(
  176. struct crypto_async_request *child_async_req, int err)
  177. {
  178. struct akcipher_request *req = child_async_req->data;
  179. struct crypto_async_request async_req;
  180. if (err == -EINPROGRESS)
  181. return;
  182. async_req.data = req->base.data;
  183. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  184. async_req.flags = child_async_req->flags;
  185. req->base.complete(&async_req,
  186. pkcs1pad_encrypt_sign_complete(req, err));
  187. }
  188. static int pkcs1pad_encrypt(struct akcipher_request *req)
  189. {
  190. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  191. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  192. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  193. int err;
  194. unsigned int i, ps_end;
  195. if (!ctx->key_size)
  196. return -EINVAL;
  197. if (req->src_len > ctx->key_size - 11)
  198. return -EOVERFLOW;
  199. if (req->dst_len < ctx->key_size) {
  200. req->dst_len = ctx->key_size;
  201. return -EOVERFLOW;
  202. }
  203. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  204. GFP_KERNEL);
  205. if (!req_ctx->in_buf)
  206. return -ENOMEM;
  207. ps_end = ctx->key_size - req->src_len - 2;
  208. req_ctx->in_buf[0] = 0x02;
  209. for (i = 1; i < ps_end; i++)
  210. req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
  211. req_ctx->in_buf[ps_end] = 0x00;
  212. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  213. ctx->key_size - 1 - req->src_len, req->src);
  214. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  215. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  216. pkcs1pad_encrypt_sign_complete_cb, req);
  217. /* Reuse output buffer */
  218. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  219. req->dst, ctx->key_size - 1, req->dst_len);
  220. err = crypto_akcipher_encrypt(&req_ctx->child_req);
  221. if (err != -EINPROGRESS && err != -EBUSY)
  222. return pkcs1pad_encrypt_sign_complete(req, err);
  223. return err;
  224. }
  225. static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
  226. {
  227. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  228. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  229. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  230. unsigned int dst_len;
  231. unsigned int pos;
  232. u8 *out_buf;
  233. if (err)
  234. goto done;
  235. err = -EINVAL;
  236. dst_len = req_ctx->child_req.dst_len;
  237. if (dst_len < ctx->key_size - 1)
  238. goto done;
  239. out_buf = req_ctx->out_buf;
  240. if (dst_len == ctx->key_size) {
  241. if (out_buf[0] != 0x00)
  242. /* Decrypted value had no leading 0 byte */
  243. goto done;
  244. dst_len--;
  245. out_buf++;
  246. }
  247. if (out_buf[0] != 0x02)
  248. goto done;
  249. for (pos = 1; pos < dst_len; pos++)
  250. if (out_buf[pos] == 0x00)
  251. break;
  252. if (pos < 9 || pos == dst_len)
  253. goto done;
  254. pos++;
  255. err = 0;
  256. if (req->dst_len < dst_len - pos)
  257. err = -EOVERFLOW;
  258. req->dst_len = dst_len - pos;
  259. if (!err)
  260. sg_copy_from_buffer(req->dst,
  261. sg_nents_for_len(req->dst, req->dst_len),
  262. out_buf + pos, req->dst_len);
  263. done:
  264. kzfree(req_ctx->out_buf);
  265. return err;
  266. }
  267. static void pkcs1pad_decrypt_complete_cb(
  268. struct crypto_async_request *child_async_req, int err)
  269. {
  270. struct akcipher_request *req = child_async_req->data;
  271. struct crypto_async_request async_req;
  272. if (err == -EINPROGRESS)
  273. return;
  274. async_req.data = req->base.data;
  275. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  276. async_req.flags = child_async_req->flags;
  277. req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
  278. }
  279. static int pkcs1pad_decrypt(struct akcipher_request *req)
  280. {
  281. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  282. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  283. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  284. int err;
  285. if (!ctx->key_size || req->src_len != ctx->key_size)
  286. return -EINVAL;
  287. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  288. if (!req_ctx->out_buf)
  289. return -ENOMEM;
  290. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  291. ctx->key_size, NULL);
  292. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  293. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  294. pkcs1pad_decrypt_complete_cb, req);
  295. /* Reuse input buffer, output to a new buffer */
  296. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  297. req_ctx->out_sg, req->src_len,
  298. ctx->key_size);
  299. err = crypto_akcipher_decrypt(&req_ctx->child_req);
  300. if (err != -EINPROGRESS && err != -EBUSY)
  301. return pkcs1pad_decrypt_complete(req, err);
  302. return err;
  303. }
  304. static int pkcs1pad_sign(struct akcipher_request *req)
  305. {
  306. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  307. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  308. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  309. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  310. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  311. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  312. int err;
  313. unsigned int ps_end, digest_size = 0;
  314. if (!ctx->key_size)
  315. return -EINVAL;
  316. digest_size = digest_info->size;
  317. if (req->src_len + digest_size > ctx->key_size - 11)
  318. return -EOVERFLOW;
  319. if (req->dst_len < ctx->key_size) {
  320. req->dst_len = ctx->key_size;
  321. return -EOVERFLOW;
  322. }
  323. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  324. GFP_KERNEL);
  325. if (!req_ctx->in_buf)
  326. return -ENOMEM;
  327. ps_end = ctx->key_size - digest_size - req->src_len - 2;
  328. req_ctx->in_buf[0] = 0x01;
  329. memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
  330. req_ctx->in_buf[ps_end] = 0x00;
  331. memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
  332. digest_info->size);
  333. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  334. ctx->key_size - 1 - req->src_len, req->src);
  335. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  336. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  337. pkcs1pad_encrypt_sign_complete_cb, req);
  338. /* Reuse output buffer */
  339. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  340. req->dst, ctx->key_size - 1, req->dst_len);
  341. err = crypto_akcipher_sign(&req_ctx->child_req);
  342. if (err != -EINPROGRESS && err != -EBUSY)
  343. return pkcs1pad_encrypt_sign_complete(req, err);
  344. return err;
  345. }
  346. static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
  347. {
  348. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  349. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  350. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  351. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  352. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  353. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  354. unsigned int dst_len;
  355. unsigned int pos;
  356. u8 *out_buf;
  357. if (err)
  358. goto done;
  359. err = -EINVAL;
  360. dst_len = req_ctx->child_req.dst_len;
  361. if (dst_len < ctx->key_size - 1)
  362. goto done;
  363. out_buf = req_ctx->out_buf;
  364. if (dst_len == ctx->key_size) {
  365. if (out_buf[0] != 0x00)
  366. /* Decrypted value had no leading 0 byte */
  367. goto done;
  368. dst_len--;
  369. out_buf++;
  370. }
  371. err = -EBADMSG;
  372. if (out_buf[0] != 0x01)
  373. goto done;
  374. for (pos = 1; pos < dst_len; pos++)
  375. if (out_buf[pos] != 0xff)
  376. break;
  377. if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
  378. goto done;
  379. pos++;
  380. if (crypto_memneq(out_buf + pos, digest_info->data, digest_info->size))
  381. goto done;
  382. pos += digest_info->size;
  383. err = 0;
  384. if (req->dst_len < dst_len - pos)
  385. err = -EOVERFLOW;
  386. req->dst_len = dst_len - pos;
  387. if (!err)
  388. sg_copy_from_buffer(req->dst,
  389. sg_nents_for_len(req->dst, req->dst_len),
  390. out_buf + pos, req->dst_len);
  391. done:
  392. kzfree(req_ctx->out_buf);
  393. return err;
  394. }
  395. static void pkcs1pad_verify_complete_cb(
  396. struct crypto_async_request *child_async_req, int err)
  397. {
  398. struct akcipher_request *req = child_async_req->data;
  399. struct crypto_async_request async_req;
  400. if (err == -EINPROGRESS)
  401. return;
  402. async_req.data = req->base.data;
  403. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  404. async_req.flags = child_async_req->flags;
  405. req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
  406. }
  407. /*
  408. * The verify operation is here for completeness similar to the verification
  409. * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
  410. * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
  411. * retrieve the DigestInfo from a signature, instead the user is expected
  412. * to call the sign operation to generate the expected signature and compare
  413. * signatures instead of the message-digests.
  414. */
  415. static int pkcs1pad_verify(struct akcipher_request *req)
  416. {
  417. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  418. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  419. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  420. int err;
  421. if (!ctx->key_size || req->src_len < ctx->key_size)
  422. return -EINVAL;
  423. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  424. if (!req_ctx->out_buf)
  425. return -ENOMEM;
  426. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  427. ctx->key_size, NULL);
  428. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  429. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  430. pkcs1pad_verify_complete_cb, req);
  431. /* Reuse input buffer, output to a new buffer */
  432. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  433. req_ctx->out_sg, req->src_len,
  434. ctx->key_size);
  435. err = crypto_akcipher_verify(&req_ctx->child_req);
  436. if (err != -EINPROGRESS && err != -EBUSY)
  437. return pkcs1pad_verify_complete(req, err);
  438. return err;
  439. }
  440. static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
  441. {
  442. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  443. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  444. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  445. struct crypto_akcipher *child_tfm;
  446. child_tfm = crypto_spawn_akcipher(&ictx->spawn);
  447. if (IS_ERR(child_tfm))
  448. return PTR_ERR(child_tfm);
  449. ctx->child = child_tfm;
  450. return 0;
  451. }
  452. static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
  453. {
  454. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  455. crypto_free_akcipher(ctx->child);
  456. }
  457. static void pkcs1pad_free(struct akcipher_instance *inst)
  458. {
  459. struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
  460. struct crypto_akcipher_spawn *spawn = &ctx->spawn;
  461. crypto_drop_akcipher(spawn);
  462. kfree(inst);
  463. }
  464. static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
  465. {
  466. const struct rsa_asn1_template *digest_info;
  467. struct crypto_attr_type *algt;
  468. struct akcipher_instance *inst;
  469. struct pkcs1pad_inst_ctx *ctx;
  470. struct crypto_akcipher_spawn *spawn;
  471. struct akcipher_alg *rsa_alg;
  472. const char *rsa_alg_name;
  473. const char *hash_name;
  474. int err;
  475. algt = crypto_get_attr_type(tb);
  476. if (IS_ERR(algt))
  477. return PTR_ERR(algt);
  478. if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
  479. return -EINVAL;
  480. rsa_alg_name = crypto_attr_alg_name(tb[1]);
  481. if (IS_ERR(rsa_alg_name))
  482. return PTR_ERR(rsa_alg_name);
  483. hash_name = crypto_attr_alg_name(tb[2]);
  484. if (IS_ERR(hash_name))
  485. return PTR_ERR(hash_name);
  486. digest_info = rsa_lookup_asn1(hash_name);
  487. if (!digest_info)
  488. return -EINVAL;
  489. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  490. if (!inst)
  491. return -ENOMEM;
  492. ctx = akcipher_instance_ctx(inst);
  493. spawn = &ctx->spawn;
  494. ctx->digest_info = digest_info;
  495. crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
  496. err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
  497. crypto_requires_sync(algt->type, algt->mask));
  498. if (err)
  499. goto out_free_inst;
  500. rsa_alg = crypto_spawn_akcipher_alg(spawn);
  501. err = -ENAMETOOLONG;
  502. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  503. "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >=
  504. CRYPTO_MAX_ALG_NAME ||
  505. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  506. "pkcs1pad(%s,%s)",
  507. rsa_alg->base.cra_driver_name, hash_name) >=
  508. CRYPTO_MAX_ALG_NAME)
  509. goto out_drop_alg;
  510. inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  511. inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
  512. inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
  513. inst->alg.init = pkcs1pad_init_tfm;
  514. inst->alg.exit = pkcs1pad_exit_tfm;
  515. inst->alg.encrypt = pkcs1pad_encrypt;
  516. inst->alg.decrypt = pkcs1pad_decrypt;
  517. inst->alg.sign = pkcs1pad_sign;
  518. inst->alg.verify = pkcs1pad_verify;
  519. inst->alg.set_pub_key = pkcs1pad_set_pub_key;
  520. inst->alg.set_priv_key = pkcs1pad_set_priv_key;
  521. inst->alg.max_size = pkcs1pad_get_max_size;
  522. inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
  523. inst->free = pkcs1pad_free;
  524. err = akcipher_register_instance(tmpl, inst);
  525. if (err)
  526. goto out_drop_alg;
  527. return 0;
  528. out_drop_alg:
  529. crypto_drop_akcipher(spawn);
  530. out_free_inst:
  531. kfree(inst);
  532. return err;
  533. }
  534. struct crypto_template rsa_pkcs1pad_tmpl = {
  535. .name = "pkcs1pad",
  536. .create = pkcs1pad_create,
  537. .module = THIS_MODULE,
  538. };