rsa-pkcs1pad.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  215. if (!req_ctx->out_buf) {
  216. kfree(req_ctx->in_buf);
  217. return -ENOMEM;
  218. }
  219. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  220. ctx->key_size, NULL);
  221. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  222. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  223. pkcs1pad_encrypt_sign_complete_cb, req);
  224. /* Reuse output buffer */
  225. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  226. req->dst, ctx->key_size - 1, req->dst_len);
  227. err = crypto_akcipher_encrypt(&req_ctx->child_req);
  228. if (err != -EINPROGRESS && err != -EBUSY)
  229. return pkcs1pad_encrypt_sign_complete(req, err);
  230. return err;
  231. }
  232. static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
  233. {
  234. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  235. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  236. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  237. unsigned int dst_len;
  238. unsigned int pos;
  239. u8 *out_buf;
  240. if (err)
  241. goto done;
  242. err = -EINVAL;
  243. dst_len = req_ctx->child_req.dst_len;
  244. if (dst_len < ctx->key_size - 1)
  245. goto done;
  246. out_buf = req_ctx->out_buf;
  247. if (dst_len == ctx->key_size) {
  248. if (out_buf[0] != 0x00)
  249. /* Decrypted value had no leading 0 byte */
  250. goto done;
  251. dst_len--;
  252. out_buf++;
  253. }
  254. if (out_buf[0] != 0x02)
  255. goto done;
  256. for (pos = 1; pos < dst_len; pos++)
  257. if (out_buf[pos] == 0x00)
  258. break;
  259. if (pos < 9 || pos == dst_len)
  260. goto done;
  261. pos++;
  262. err = 0;
  263. if (req->dst_len < dst_len - pos)
  264. err = -EOVERFLOW;
  265. req->dst_len = dst_len - pos;
  266. if (!err)
  267. sg_copy_from_buffer(req->dst,
  268. sg_nents_for_len(req->dst, req->dst_len),
  269. out_buf + pos, req->dst_len);
  270. done:
  271. kzfree(req_ctx->out_buf);
  272. return err;
  273. }
  274. static void pkcs1pad_decrypt_complete_cb(
  275. struct crypto_async_request *child_async_req, int err)
  276. {
  277. struct akcipher_request *req = child_async_req->data;
  278. struct crypto_async_request async_req;
  279. if (err == -EINPROGRESS)
  280. return;
  281. async_req.data = req->base.data;
  282. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  283. async_req.flags = child_async_req->flags;
  284. req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
  285. }
  286. static int pkcs1pad_decrypt(struct akcipher_request *req)
  287. {
  288. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  289. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  290. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  291. int err;
  292. if (!ctx->key_size || req->src_len != ctx->key_size)
  293. return -EINVAL;
  294. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  295. if (!req_ctx->out_buf)
  296. return -ENOMEM;
  297. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  298. ctx->key_size, NULL);
  299. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  300. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  301. pkcs1pad_decrypt_complete_cb, req);
  302. /* Reuse input buffer, output to a new buffer */
  303. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  304. req_ctx->out_sg, req->src_len,
  305. ctx->key_size);
  306. err = crypto_akcipher_decrypt(&req_ctx->child_req);
  307. if (err != -EINPROGRESS && err != -EBUSY)
  308. return pkcs1pad_decrypt_complete(req, err);
  309. return err;
  310. }
  311. static int pkcs1pad_sign(struct akcipher_request *req)
  312. {
  313. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  314. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  315. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  316. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  317. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  318. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  319. int err;
  320. unsigned int ps_end, digest_size = 0;
  321. if (!ctx->key_size)
  322. return -EINVAL;
  323. digest_size = digest_info->size;
  324. if (req->src_len + digest_size > ctx->key_size - 11)
  325. return -EOVERFLOW;
  326. if (req->dst_len < ctx->key_size) {
  327. req->dst_len = ctx->key_size;
  328. return -EOVERFLOW;
  329. }
  330. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  331. GFP_KERNEL);
  332. if (!req_ctx->in_buf)
  333. return -ENOMEM;
  334. ps_end = ctx->key_size - digest_size - req->src_len - 2;
  335. req_ctx->in_buf[0] = 0x01;
  336. memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
  337. req_ctx->in_buf[ps_end] = 0x00;
  338. memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
  339. digest_info->size);
  340. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  341. ctx->key_size - 1 - req->src_len, req->src);
  342. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  343. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  344. pkcs1pad_encrypt_sign_complete_cb, req);
  345. /* Reuse output buffer */
  346. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  347. req->dst, ctx->key_size - 1, req->dst_len);
  348. err = crypto_akcipher_sign(&req_ctx->child_req);
  349. if (err != -EINPROGRESS && err != -EBUSY)
  350. return pkcs1pad_encrypt_sign_complete(req, err);
  351. return err;
  352. }
  353. static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
  354. {
  355. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  356. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  357. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  358. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  359. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  360. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  361. unsigned int dst_len;
  362. unsigned int pos;
  363. u8 *out_buf;
  364. if (err)
  365. goto done;
  366. err = -EINVAL;
  367. dst_len = req_ctx->child_req.dst_len;
  368. if (dst_len < ctx->key_size - 1)
  369. goto done;
  370. out_buf = req_ctx->out_buf;
  371. if (dst_len == ctx->key_size) {
  372. if (out_buf[0] != 0x00)
  373. /* Decrypted value had no leading 0 byte */
  374. goto done;
  375. dst_len--;
  376. out_buf++;
  377. }
  378. err = -EBADMSG;
  379. if (out_buf[0] != 0x01)
  380. goto done;
  381. for (pos = 1; pos < dst_len; pos++)
  382. if (out_buf[pos] != 0xff)
  383. break;
  384. if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
  385. goto done;
  386. pos++;
  387. if (crypto_memneq(out_buf + pos, digest_info->data, digest_info->size))
  388. goto done;
  389. pos += digest_info->size;
  390. err = 0;
  391. if (req->dst_len < dst_len - pos)
  392. err = -EOVERFLOW;
  393. req->dst_len = dst_len - pos;
  394. if (!err)
  395. sg_copy_from_buffer(req->dst,
  396. sg_nents_for_len(req->dst, req->dst_len),
  397. out_buf + pos, req->dst_len);
  398. done:
  399. kzfree(req_ctx->out_buf);
  400. return err;
  401. }
  402. static void pkcs1pad_verify_complete_cb(
  403. struct crypto_async_request *child_async_req, int err)
  404. {
  405. struct akcipher_request *req = child_async_req->data;
  406. struct crypto_async_request async_req;
  407. if (err == -EINPROGRESS)
  408. return;
  409. async_req.data = req->base.data;
  410. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  411. async_req.flags = child_async_req->flags;
  412. req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
  413. }
  414. /*
  415. * The verify operation is here for completeness similar to the verification
  416. * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
  417. * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
  418. * retrieve the DigestInfo from a signature, instead the user is expected
  419. * to call the sign operation to generate the expected signature and compare
  420. * signatures instead of the message-digests.
  421. */
  422. static int pkcs1pad_verify(struct akcipher_request *req)
  423. {
  424. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  425. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  426. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  427. int err;
  428. if (!ctx->key_size || req->src_len < ctx->key_size)
  429. return -EINVAL;
  430. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  431. if (!req_ctx->out_buf)
  432. return -ENOMEM;
  433. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  434. ctx->key_size, NULL);
  435. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  436. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  437. pkcs1pad_verify_complete_cb, req);
  438. /* Reuse input buffer, output to a new buffer */
  439. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  440. req_ctx->out_sg, req->src_len,
  441. ctx->key_size);
  442. err = crypto_akcipher_verify(&req_ctx->child_req);
  443. if (err != -EINPROGRESS && err != -EBUSY)
  444. return pkcs1pad_verify_complete(req, err);
  445. return err;
  446. }
  447. static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
  448. {
  449. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  450. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  451. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  452. struct crypto_akcipher *child_tfm;
  453. child_tfm = crypto_spawn_akcipher(&ictx->spawn);
  454. if (IS_ERR(child_tfm))
  455. return PTR_ERR(child_tfm);
  456. ctx->child = child_tfm;
  457. return 0;
  458. }
  459. static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
  460. {
  461. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  462. crypto_free_akcipher(ctx->child);
  463. }
  464. static void pkcs1pad_free(struct akcipher_instance *inst)
  465. {
  466. struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
  467. struct crypto_akcipher_spawn *spawn = &ctx->spawn;
  468. crypto_drop_akcipher(spawn);
  469. kfree(inst);
  470. }
  471. static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
  472. {
  473. const struct rsa_asn1_template *digest_info;
  474. struct crypto_attr_type *algt;
  475. struct akcipher_instance *inst;
  476. struct pkcs1pad_inst_ctx *ctx;
  477. struct crypto_akcipher_spawn *spawn;
  478. struct akcipher_alg *rsa_alg;
  479. const char *rsa_alg_name;
  480. const char *hash_name;
  481. int err;
  482. algt = crypto_get_attr_type(tb);
  483. if (IS_ERR(algt))
  484. return PTR_ERR(algt);
  485. if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
  486. return -EINVAL;
  487. rsa_alg_name = crypto_attr_alg_name(tb[1]);
  488. if (IS_ERR(rsa_alg_name))
  489. return PTR_ERR(rsa_alg_name);
  490. hash_name = crypto_attr_alg_name(tb[2]);
  491. if (IS_ERR(hash_name))
  492. return PTR_ERR(hash_name);
  493. digest_info = rsa_lookup_asn1(hash_name);
  494. if (!digest_info)
  495. return -EINVAL;
  496. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  497. if (!inst)
  498. return -ENOMEM;
  499. ctx = akcipher_instance_ctx(inst);
  500. spawn = &ctx->spawn;
  501. ctx->digest_info = digest_info;
  502. crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
  503. err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
  504. crypto_requires_sync(algt->type, algt->mask));
  505. if (err)
  506. goto out_free_inst;
  507. rsa_alg = crypto_spawn_akcipher_alg(spawn);
  508. err = -ENAMETOOLONG;
  509. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  510. "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >=
  511. CRYPTO_MAX_ALG_NAME ||
  512. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  513. "pkcs1pad(%s,%s)",
  514. rsa_alg->base.cra_driver_name, hash_name) >=
  515. CRYPTO_MAX_ALG_NAME)
  516. goto out_drop_alg;
  517. inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  518. inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
  519. inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
  520. inst->alg.init = pkcs1pad_init_tfm;
  521. inst->alg.exit = pkcs1pad_exit_tfm;
  522. inst->alg.encrypt = pkcs1pad_encrypt;
  523. inst->alg.decrypt = pkcs1pad_decrypt;
  524. inst->alg.sign = pkcs1pad_sign;
  525. inst->alg.verify = pkcs1pad_verify;
  526. inst->alg.set_pub_key = pkcs1pad_set_pub_key;
  527. inst->alg.set_priv_key = pkcs1pad_set_priv_key;
  528. inst->alg.max_size = pkcs1pad_get_max_size;
  529. inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
  530. inst->free = pkcs1pad_free;
  531. err = akcipher_register_instance(tmpl, inst);
  532. if (err)
  533. goto out_drop_alg;
  534. return 0;
  535. out_drop_alg:
  536. crypto_drop_akcipher(spawn);
  537. out_free_inst:
  538. kfree(inst);
  539. return err;
  540. }
  541. struct crypto_template rsa_pkcs1pad_tmpl = {
  542. .name = "pkcs1pad",
  543. .create = pkcs1pad_create,
  544. .module = THIS_MODULE,
  545. };