rsa-pkcs1pad.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. if (digest_info)
  317. digest_size = digest_info->size;
  318. if (req->src_len + digest_size > ctx->key_size - 11)
  319. return -EOVERFLOW;
  320. if (req->dst_len < ctx->key_size) {
  321. req->dst_len = ctx->key_size;
  322. return -EOVERFLOW;
  323. }
  324. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  325. GFP_KERNEL);
  326. if (!req_ctx->in_buf)
  327. return -ENOMEM;
  328. ps_end = ctx->key_size - digest_size - req->src_len - 2;
  329. req_ctx->in_buf[0] = 0x01;
  330. memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
  331. req_ctx->in_buf[ps_end] = 0x00;
  332. if (digest_info)
  333. memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
  334. digest_info->size);
  335. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  336. ctx->key_size - 1 - req->src_len, req->src);
  337. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  338. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  339. pkcs1pad_encrypt_sign_complete_cb, req);
  340. /* Reuse output buffer */
  341. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  342. req->dst, ctx->key_size - 1, req->dst_len);
  343. err = crypto_akcipher_sign(&req_ctx->child_req);
  344. if (err != -EINPROGRESS && err != -EBUSY)
  345. return pkcs1pad_encrypt_sign_complete(req, err);
  346. return err;
  347. }
  348. static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
  349. {
  350. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  351. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  352. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  353. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  354. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  355. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  356. unsigned int dst_len;
  357. unsigned int pos;
  358. u8 *out_buf;
  359. if (err)
  360. goto done;
  361. err = -EINVAL;
  362. dst_len = req_ctx->child_req.dst_len;
  363. if (dst_len < ctx->key_size - 1)
  364. goto done;
  365. out_buf = req_ctx->out_buf;
  366. if (dst_len == ctx->key_size) {
  367. if (out_buf[0] != 0x00)
  368. /* Decrypted value had no leading 0 byte */
  369. goto done;
  370. dst_len--;
  371. out_buf++;
  372. }
  373. err = -EBADMSG;
  374. if (out_buf[0] != 0x01)
  375. goto done;
  376. for (pos = 1; pos < dst_len; pos++)
  377. if (out_buf[pos] != 0xff)
  378. break;
  379. if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
  380. goto done;
  381. pos++;
  382. if (digest_info) {
  383. if (crypto_memneq(out_buf + pos, digest_info->data,
  384. digest_info->size))
  385. goto done;
  386. pos += digest_info->size;
  387. }
  388. err = 0;
  389. if (req->dst_len < dst_len - pos)
  390. err = -EOVERFLOW;
  391. req->dst_len = dst_len - pos;
  392. if (!err)
  393. sg_copy_from_buffer(req->dst,
  394. sg_nents_for_len(req->dst, req->dst_len),
  395. out_buf + pos, req->dst_len);
  396. done:
  397. kzfree(req_ctx->out_buf);
  398. return err;
  399. }
  400. static void pkcs1pad_verify_complete_cb(
  401. struct crypto_async_request *child_async_req, int err)
  402. {
  403. struct akcipher_request *req = child_async_req->data;
  404. struct crypto_async_request async_req;
  405. if (err == -EINPROGRESS)
  406. return;
  407. async_req.data = req->base.data;
  408. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  409. async_req.flags = child_async_req->flags;
  410. req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
  411. }
  412. /*
  413. * The verify operation is here for completeness similar to the verification
  414. * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
  415. * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
  416. * retrieve the DigestInfo from a signature, instead the user is expected
  417. * to call the sign operation to generate the expected signature and compare
  418. * signatures instead of the message-digests.
  419. */
  420. static int pkcs1pad_verify(struct akcipher_request *req)
  421. {
  422. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  423. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  424. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  425. int err;
  426. if (!ctx->key_size || req->src_len < ctx->key_size)
  427. return -EINVAL;
  428. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  429. if (!req_ctx->out_buf)
  430. return -ENOMEM;
  431. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  432. ctx->key_size, NULL);
  433. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  434. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  435. pkcs1pad_verify_complete_cb, req);
  436. /* Reuse input buffer, output to a new buffer */
  437. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  438. req_ctx->out_sg, req->src_len,
  439. ctx->key_size);
  440. err = crypto_akcipher_verify(&req_ctx->child_req);
  441. if (err != -EINPROGRESS && err != -EBUSY)
  442. return pkcs1pad_verify_complete(req, err);
  443. return err;
  444. }
  445. static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
  446. {
  447. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  448. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  449. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  450. struct crypto_akcipher *child_tfm;
  451. child_tfm = crypto_spawn_akcipher(&ictx->spawn);
  452. if (IS_ERR(child_tfm))
  453. return PTR_ERR(child_tfm);
  454. ctx->child = child_tfm;
  455. return 0;
  456. }
  457. static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
  458. {
  459. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  460. crypto_free_akcipher(ctx->child);
  461. }
  462. static void pkcs1pad_free(struct akcipher_instance *inst)
  463. {
  464. struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
  465. struct crypto_akcipher_spawn *spawn = &ctx->spawn;
  466. crypto_drop_akcipher(spawn);
  467. kfree(inst);
  468. }
  469. static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
  470. {
  471. const struct rsa_asn1_template *digest_info;
  472. struct crypto_attr_type *algt;
  473. struct akcipher_instance *inst;
  474. struct pkcs1pad_inst_ctx *ctx;
  475. struct crypto_akcipher_spawn *spawn;
  476. struct akcipher_alg *rsa_alg;
  477. const char *rsa_alg_name;
  478. const char *hash_name;
  479. int err;
  480. algt = crypto_get_attr_type(tb);
  481. if (IS_ERR(algt))
  482. return PTR_ERR(algt);
  483. if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
  484. return -EINVAL;
  485. rsa_alg_name = crypto_attr_alg_name(tb[1]);
  486. if (IS_ERR(rsa_alg_name))
  487. return PTR_ERR(rsa_alg_name);
  488. hash_name = crypto_attr_alg_name(tb[2]);
  489. if (IS_ERR(hash_name))
  490. hash_name = NULL;
  491. if (hash_name) {
  492. digest_info = rsa_lookup_asn1(hash_name);
  493. if (!digest_info)
  494. return -EINVAL;
  495. } else
  496. digest_info = NULL;
  497. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  498. if (!inst)
  499. return -ENOMEM;
  500. ctx = akcipher_instance_ctx(inst);
  501. spawn = &ctx->spawn;
  502. ctx->digest_info = digest_info;
  503. crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
  504. err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
  505. crypto_requires_sync(algt->type, algt->mask));
  506. if (err)
  507. goto out_free_inst;
  508. rsa_alg = crypto_spawn_akcipher_alg(spawn);
  509. err = -ENAMETOOLONG;
  510. if (!hash_name) {
  511. if (snprintf(inst->alg.base.cra_name,
  512. CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
  513. rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
  514. goto out_drop_alg;
  515. if (snprintf(inst->alg.base.cra_driver_name,
  516. CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
  517. rsa_alg->base.cra_driver_name) >=
  518. CRYPTO_MAX_ALG_NAME)
  519. goto out_drop_alg;
  520. } else {
  521. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  522. "pkcs1pad(%s,%s)", rsa_alg->base.cra_name,
  523. hash_name) >= CRYPTO_MAX_ALG_NAME)
  524. goto out_drop_alg;
  525. if (snprintf(inst->alg.base.cra_driver_name,
  526. CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s,%s)",
  527. rsa_alg->base.cra_driver_name,
  528. hash_name) >= CRYPTO_MAX_ALG_NAME)
  529. goto out_drop_alg;
  530. }
  531. inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  532. inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
  533. inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
  534. inst->alg.init = pkcs1pad_init_tfm;
  535. inst->alg.exit = pkcs1pad_exit_tfm;
  536. inst->alg.encrypt = pkcs1pad_encrypt;
  537. inst->alg.decrypt = pkcs1pad_decrypt;
  538. inst->alg.sign = pkcs1pad_sign;
  539. inst->alg.verify = pkcs1pad_verify;
  540. inst->alg.set_pub_key = pkcs1pad_set_pub_key;
  541. inst->alg.set_priv_key = pkcs1pad_set_priv_key;
  542. inst->alg.max_size = pkcs1pad_get_max_size;
  543. inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
  544. inst->free = pkcs1pad_free;
  545. err = akcipher_register_instance(tmpl, inst);
  546. if (err)
  547. goto out_drop_alg;
  548. return 0;
  549. out_drop_alg:
  550. crypto_drop_akcipher(spawn);
  551. out_free_inst:
  552. kfree(inst);
  553. return err;
  554. }
  555. struct crypto_template rsa_pkcs1pad_tmpl = {
  556. .name = "pkcs1pad",
  557. .create = pkcs1pad_create,
  558. .module = THIS_MODULE,
  559. };