rsa-pkcs1pad.c 18 KB

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