rsa-pkcs1pad.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 < 0)
  106. return err;
  107. if (err > PAGE_SIZE)
  108. return -ENOTSUPP;
  109. ctx->key_size = err;
  110. return 0;
  111. }
  112. static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
  113. unsigned int keylen)
  114. {
  115. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  116. int err;
  117. ctx->key_size = 0;
  118. err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
  119. if (err)
  120. return err;
  121. /* Find out new modulus size from rsa implementation */
  122. err = crypto_akcipher_maxsize(ctx->child);
  123. if (err < 0)
  124. return err;
  125. if (err > PAGE_SIZE)
  126. return -ENOTSUPP;
  127. ctx->key_size = err;
  128. return 0;
  129. }
  130. static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
  131. {
  132. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  133. /*
  134. * The maximum destination buffer size for the encrypt/sign operations
  135. * will be the same as for RSA, even though it's smaller for
  136. * decrypt/verify.
  137. */
  138. return ctx->key_size ?: -EINVAL;
  139. }
  140. static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
  141. struct scatterlist *next)
  142. {
  143. int nsegs = next ? 2 : 1;
  144. sg_init_table(sg, nsegs);
  145. sg_set_buf(sg, buf, len);
  146. if (next)
  147. sg_chain(sg, nsegs, next);
  148. }
  149. static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
  150. {
  151. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  152. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  153. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  154. unsigned int pad_len;
  155. unsigned int len;
  156. u8 *out_buf;
  157. if (err)
  158. goto out;
  159. len = req_ctx->child_req.dst_len;
  160. pad_len = ctx->key_size - len;
  161. /* Four billion to one */
  162. if (likely(!pad_len))
  163. goto out;
  164. out_buf = kzalloc(ctx->key_size, GFP_ATOMIC);
  165. err = -ENOMEM;
  166. if (!out_buf)
  167. goto out;
  168. sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
  169. out_buf + pad_len, len);
  170. sg_copy_from_buffer(req->dst,
  171. sg_nents_for_len(req->dst, ctx->key_size),
  172. out_buf, ctx->key_size);
  173. kzfree(out_buf);
  174. out:
  175. req->dst_len = ctx->key_size;
  176. kfree(req_ctx->in_buf);
  177. return err;
  178. }
  179. static void pkcs1pad_encrypt_sign_complete_cb(
  180. struct crypto_async_request *child_async_req, int err)
  181. {
  182. struct akcipher_request *req = child_async_req->data;
  183. struct crypto_async_request async_req;
  184. if (err == -EINPROGRESS)
  185. return;
  186. async_req.data = req->base.data;
  187. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  188. async_req.flags = child_async_req->flags;
  189. req->base.complete(&async_req,
  190. pkcs1pad_encrypt_sign_complete(req, err));
  191. }
  192. static int pkcs1pad_encrypt(struct akcipher_request *req)
  193. {
  194. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  195. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  196. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  197. int err;
  198. unsigned int i, ps_end;
  199. if (!ctx->key_size)
  200. return -EINVAL;
  201. if (req->src_len > ctx->key_size - 11)
  202. return -EOVERFLOW;
  203. if (req->dst_len < ctx->key_size) {
  204. req->dst_len = ctx->key_size;
  205. return -EOVERFLOW;
  206. }
  207. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  208. GFP_KERNEL);
  209. if (!req_ctx->in_buf)
  210. return -ENOMEM;
  211. ps_end = ctx->key_size - req->src_len - 2;
  212. req_ctx->in_buf[0] = 0x02;
  213. for (i = 1; i < ps_end; i++)
  214. req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
  215. req_ctx->in_buf[ps_end] = 0x00;
  216. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  217. ctx->key_size - 1 - req->src_len, req->src);
  218. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  219. if (!req_ctx->out_buf) {
  220. kfree(req_ctx->in_buf);
  221. return -ENOMEM;
  222. }
  223. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  224. ctx->key_size, NULL);
  225. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  226. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  227. pkcs1pad_encrypt_sign_complete_cb, req);
  228. /* Reuse output buffer */
  229. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  230. req->dst, ctx->key_size - 1, req->dst_len);
  231. err = crypto_akcipher_encrypt(&req_ctx->child_req);
  232. if (err != -EINPROGRESS &&
  233. (err != -EBUSY ||
  234. !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
  235. return pkcs1pad_encrypt_sign_complete(req, err);
  236. return err;
  237. }
  238. static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
  239. {
  240. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  241. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  242. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  243. unsigned int dst_len;
  244. unsigned int pos;
  245. u8 *out_buf;
  246. if (err)
  247. goto done;
  248. err = -EINVAL;
  249. dst_len = req_ctx->child_req.dst_len;
  250. if (dst_len < ctx->key_size - 1)
  251. goto done;
  252. out_buf = req_ctx->out_buf;
  253. if (dst_len == ctx->key_size) {
  254. if (out_buf[0] != 0x00)
  255. /* Decrypted value had no leading 0 byte */
  256. goto done;
  257. dst_len--;
  258. out_buf++;
  259. }
  260. if (out_buf[0] != 0x02)
  261. goto done;
  262. for (pos = 1; pos < dst_len; pos++)
  263. if (out_buf[pos] == 0x00)
  264. break;
  265. if (pos < 9 || pos == dst_len)
  266. goto done;
  267. pos++;
  268. err = 0;
  269. if (req->dst_len < dst_len - pos)
  270. err = -EOVERFLOW;
  271. req->dst_len = dst_len - pos;
  272. if (!err)
  273. sg_copy_from_buffer(req->dst,
  274. sg_nents_for_len(req->dst, req->dst_len),
  275. out_buf + pos, req->dst_len);
  276. done:
  277. kzfree(req_ctx->out_buf);
  278. return err;
  279. }
  280. static void pkcs1pad_decrypt_complete_cb(
  281. struct crypto_async_request *child_async_req, int err)
  282. {
  283. struct akcipher_request *req = child_async_req->data;
  284. struct crypto_async_request async_req;
  285. if (err == -EINPROGRESS)
  286. return;
  287. async_req.data = req->base.data;
  288. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  289. async_req.flags = child_async_req->flags;
  290. req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
  291. }
  292. static int pkcs1pad_decrypt(struct akcipher_request *req)
  293. {
  294. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  295. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  296. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  297. int err;
  298. if (!ctx->key_size || req->src_len != ctx->key_size)
  299. return -EINVAL;
  300. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  301. if (!req_ctx->out_buf)
  302. return -ENOMEM;
  303. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  304. ctx->key_size, NULL);
  305. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  306. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  307. pkcs1pad_decrypt_complete_cb, req);
  308. /* Reuse input buffer, output to a new buffer */
  309. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  310. req_ctx->out_sg, req->src_len,
  311. ctx->key_size);
  312. err = crypto_akcipher_decrypt(&req_ctx->child_req);
  313. if (err != -EINPROGRESS &&
  314. (err != -EBUSY ||
  315. !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
  316. return pkcs1pad_decrypt_complete(req, err);
  317. return err;
  318. }
  319. static int pkcs1pad_sign(struct akcipher_request *req)
  320. {
  321. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  322. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  323. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  324. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  325. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  326. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  327. int err;
  328. unsigned int ps_end, digest_size = 0;
  329. if (!ctx->key_size)
  330. return -EINVAL;
  331. digest_size = digest_info->size;
  332. if (req->src_len + digest_size > ctx->key_size - 11)
  333. return -EOVERFLOW;
  334. if (req->dst_len < ctx->key_size) {
  335. req->dst_len = ctx->key_size;
  336. return -EOVERFLOW;
  337. }
  338. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  339. GFP_KERNEL);
  340. if (!req_ctx->in_buf)
  341. return -ENOMEM;
  342. ps_end = ctx->key_size - digest_size - req->src_len - 2;
  343. req_ctx->in_buf[0] = 0x01;
  344. memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
  345. req_ctx->in_buf[ps_end] = 0x00;
  346. memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
  347. digest_info->size);
  348. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  349. ctx->key_size - 1 - req->src_len, req->src);
  350. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  351. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  352. pkcs1pad_encrypt_sign_complete_cb, req);
  353. /* Reuse output buffer */
  354. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  355. req->dst, ctx->key_size - 1, req->dst_len);
  356. err = crypto_akcipher_sign(&req_ctx->child_req);
  357. if (err != -EINPROGRESS &&
  358. (err != -EBUSY ||
  359. !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
  360. return pkcs1pad_encrypt_sign_complete(req, err);
  361. return err;
  362. }
  363. static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
  364. {
  365. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  366. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  367. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  368. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  369. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  370. const struct rsa_asn1_template *digest_info = ictx->digest_info;
  371. unsigned int dst_len;
  372. unsigned int pos;
  373. u8 *out_buf;
  374. if (err)
  375. goto done;
  376. err = -EINVAL;
  377. dst_len = req_ctx->child_req.dst_len;
  378. if (dst_len < ctx->key_size - 1)
  379. goto done;
  380. out_buf = req_ctx->out_buf;
  381. if (dst_len == ctx->key_size) {
  382. if (out_buf[0] != 0x00)
  383. /* Decrypted value had no leading 0 byte */
  384. goto done;
  385. dst_len--;
  386. out_buf++;
  387. }
  388. err = -EBADMSG;
  389. if (out_buf[0] != 0x01)
  390. goto done;
  391. for (pos = 1; pos < dst_len; pos++)
  392. if (out_buf[pos] != 0xff)
  393. break;
  394. if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
  395. goto done;
  396. pos++;
  397. if (crypto_memneq(out_buf + pos, digest_info->data, digest_info->size))
  398. goto done;
  399. pos += digest_info->size;
  400. err = 0;
  401. if (req->dst_len < dst_len - pos)
  402. err = -EOVERFLOW;
  403. req->dst_len = dst_len - pos;
  404. if (!err)
  405. sg_copy_from_buffer(req->dst,
  406. sg_nents_for_len(req->dst, req->dst_len),
  407. out_buf + pos, req->dst_len);
  408. done:
  409. kzfree(req_ctx->out_buf);
  410. return err;
  411. }
  412. static void pkcs1pad_verify_complete_cb(
  413. struct crypto_async_request *child_async_req, int err)
  414. {
  415. struct akcipher_request *req = child_async_req->data;
  416. struct crypto_async_request async_req;
  417. if (err == -EINPROGRESS)
  418. return;
  419. async_req.data = req->base.data;
  420. async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
  421. async_req.flags = child_async_req->flags;
  422. req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
  423. }
  424. /*
  425. * The verify operation is here for completeness similar to the verification
  426. * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
  427. * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
  428. * retrieve the DigestInfo from a signature, instead the user is expected
  429. * to call the sign operation to generate the expected signature and compare
  430. * signatures instead of the message-digests.
  431. */
  432. static int pkcs1pad_verify(struct akcipher_request *req)
  433. {
  434. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  435. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  436. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  437. int err;
  438. if (!ctx->key_size || req->src_len < ctx->key_size)
  439. return -EINVAL;
  440. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  441. if (!req_ctx->out_buf)
  442. return -ENOMEM;
  443. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  444. ctx->key_size, NULL);
  445. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  446. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  447. pkcs1pad_verify_complete_cb, req);
  448. /* Reuse input buffer, output to a new buffer */
  449. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  450. req_ctx->out_sg, req->src_len,
  451. ctx->key_size);
  452. err = crypto_akcipher_verify(&req_ctx->child_req);
  453. if (err != -EINPROGRESS &&
  454. (err != -EBUSY ||
  455. !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
  456. return pkcs1pad_verify_complete(req, err);
  457. return err;
  458. }
  459. static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
  460. {
  461. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  462. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  463. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  464. struct crypto_akcipher *child_tfm;
  465. child_tfm = crypto_spawn_akcipher(&ictx->spawn);
  466. if (IS_ERR(child_tfm))
  467. return PTR_ERR(child_tfm);
  468. ctx->child = child_tfm;
  469. return 0;
  470. }
  471. static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
  472. {
  473. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  474. crypto_free_akcipher(ctx->child);
  475. }
  476. static void pkcs1pad_free(struct akcipher_instance *inst)
  477. {
  478. struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
  479. struct crypto_akcipher_spawn *spawn = &ctx->spawn;
  480. crypto_drop_akcipher(spawn);
  481. kfree(inst);
  482. }
  483. static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
  484. {
  485. const struct rsa_asn1_template *digest_info;
  486. struct crypto_attr_type *algt;
  487. struct akcipher_instance *inst;
  488. struct pkcs1pad_inst_ctx *ctx;
  489. struct crypto_akcipher_spawn *spawn;
  490. struct akcipher_alg *rsa_alg;
  491. const char *rsa_alg_name;
  492. const char *hash_name;
  493. int err;
  494. algt = crypto_get_attr_type(tb);
  495. if (IS_ERR(algt))
  496. return PTR_ERR(algt);
  497. if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
  498. return -EINVAL;
  499. rsa_alg_name = crypto_attr_alg_name(tb[1]);
  500. if (IS_ERR(rsa_alg_name))
  501. return PTR_ERR(rsa_alg_name);
  502. hash_name = crypto_attr_alg_name(tb[2]);
  503. if (IS_ERR(hash_name))
  504. return PTR_ERR(hash_name);
  505. digest_info = rsa_lookup_asn1(hash_name);
  506. if (!digest_info)
  507. return -EINVAL;
  508. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  509. if (!inst)
  510. return -ENOMEM;
  511. ctx = akcipher_instance_ctx(inst);
  512. spawn = &ctx->spawn;
  513. ctx->digest_info = digest_info;
  514. crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
  515. err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
  516. crypto_requires_sync(algt->type, algt->mask));
  517. if (err)
  518. goto out_free_inst;
  519. rsa_alg = crypto_spawn_akcipher_alg(spawn);
  520. err = -ENAMETOOLONG;
  521. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  522. "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >=
  523. CRYPTO_MAX_ALG_NAME ||
  524. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  525. "pkcs1pad(%s,%s)",
  526. rsa_alg->base.cra_driver_name, hash_name) >=
  527. CRYPTO_MAX_ALG_NAME)
  528. goto out_drop_alg;
  529. inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  530. inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
  531. inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
  532. inst->alg.init = pkcs1pad_init_tfm;
  533. inst->alg.exit = pkcs1pad_exit_tfm;
  534. inst->alg.encrypt = pkcs1pad_encrypt;
  535. inst->alg.decrypt = pkcs1pad_decrypt;
  536. inst->alg.sign = pkcs1pad_sign;
  537. inst->alg.verify = pkcs1pad_verify;
  538. inst->alg.set_pub_key = pkcs1pad_set_pub_key;
  539. inst->alg.set_priv_key = pkcs1pad_set_priv_key;
  540. inst->alg.max_size = pkcs1pad_get_max_size;
  541. inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
  542. inst->free = pkcs1pad_free;
  543. err = akcipher_register_instance(tmpl, inst);
  544. if (err)
  545. goto out_drop_alg;
  546. return 0;
  547. out_drop_alg:
  548. crypto_drop_akcipher(spawn);
  549. out_free_inst:
  550. kfree(inst);
  551. return err;
  552. }
  553. struct crypto_template rsa_pkcs1pad_tmpl = {
  554. .name = "pkcs1pad",
  555. .create = pkcs1pad_create,
  556. .module = THIS_MODULE,
  557. };