rsassa-pkcs1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * RSA Signature Scheme with Appendix - PKCS #1 v1.5 (RFC 8017 sec 8.2)
  4. *
  5. * https://www.rfc-editor.org/rfc/rfc8017#section-8.2
  6. *
  7. * Copyright (c) 2015 - 2024 Intel Corporation
  8. */
  9. #include <linux/module.h>
  10. #include <linux/scatterlist.h>
  11. #include <crypto/akcipher.h>
  12. #include <crypto/algapi.h>
  13. #include <crypto/hash.h>
  14. #include <crypto/sig.h>
  15. #include <crypto/internal/akcipher.h>
  16. #include <crypto/internal/rsa.h>
  17. #include <crypto/internal/sig.h>
  18. /*
  19. * Full Hash Prefix for EMSA-PKCS1-v1_5 encoding method (RFC 9580 table 24)
  20. *
  21. * RSA keys are usually much larger than the hash of the message to be signed.
  22. * The hash is therefore prepended by the Full Hash Prefix and a 0xff padding.
  23. * The Full Hash Prefix is an ASN.1 SEQUENCE containing the hash algorithm OID.
  24. *
  25. * https://www.rfc-editor.org/rfc/rfc9580#table-24
  26. */
  27. static const u8 hash_prefix_none[] = { };
  28. static const u8 hash_prefix_md5[] = {
  29. 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, /* SEQUENCE (SEQUENCE (OID */
  30. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* <algorithm>, */
  31. 0x05, 0x00, 0x04, 0x10 /* NULL), OCTET STRING <hash>) */
  32. };
  33. static const u8 hash_prefix_sha1[] = {
  34. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  35. 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  36. 0x05, 0x00, 0x04, 0x14
  37. };
  38. static const u8 hash_prefix_rmd160[] = {
  39. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  40. 0x2b, 0x24, 0x03, 0x02, 0x01,
  41. 0x05, 0x00, 0x04, 0x14
  42. };
  43. static const u8 hash_prefix_sha224[] = {
  44. 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
  45. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
  46. 0x05, 0x00, 0x04, 0x1c
  47. };
  48. static const u8 hash_prefix_sha256[] = {
  49. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
  50. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  51. 0x05, 0x00, 0x04, 0x20
  52. };
  53. static const u8 hash_prefix_sha384[] = {
  54. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
  55. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
  56. 0x05, 0x00, 0x04, 0x30
  57. };
  58. static const u8 hash_prefix_sha512[] = {
  59. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
  60. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
  61. 0x05, 0x00, 0x04, 0x40
  62. };
  63. static const u8 hash_prefix_sha3_256[] = {
  64. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
  65. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08,
  66. 0x05, 0x00, 0x04, 0x20
  67. };
  68. static const u8 hash_prefix_sha3_384[] = {
  69. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
  70. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09,
  71. 0x05, 0x00, 0x04, 0x30
  72. };
  73. static const u8 hash_prefix_sha3_512[] = {
  74. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
  75. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0a,
  76. 0x05, 0x00, 0x04, 0x40
  77. };
  78. static const struct hash_prefix {
  79. const char *name;
  80. const u8 *data;
  81. size_t size;
  82. } hash_prefixes[] = {
  83. #define _(X) { #X, hash_prefix_##X, sizeof(hash_prefix_##X) }
  84. _(none),
  85. _(md5),
  86. _(sha1),
  87. _(rmd160),
  88. _(sha256),
  89. _(sha384),
  90. _(sha512),
  91. _(sha224),
  92. #undef _
  93. #define _(X) { "sha3-" #X, hash_prefix_sha3_##X, sizeof(hash_prefix_sha3_##X) }
  94. _(256),
  95. _(384),
  96. _(512),
  97. #undef _
  98. { NULL }
  99. };
  100. static const struct hash_prefix *rsassa_pkcs1_find_hash_prefix(const char *name)
  101. {
  102. const struct hash_prefix *p;
  103. for (p = hash_prefixes; p->name; p++)
  104. if (strcmp(name, p->name) == 0)
  105. return p;
  106. return NULL;
  107. }
  108. static bool rsassa_pkcs1_invalid_hash_len(unsigned int len,
  109. const struct hash_prefix *p)
  110. {
  111. /*
  112. * Legacy protocols such as TLS 1.1 or earlier and IKE version 1
  113. * do not prepend a Full Hash Prefix to the hash. In that case,
  114. * the size of the Full Hash Prefix is zero.
  115. */
  116. if (p->data == hash_prefix_none)
  117. return false;
  118. /*
  119. * The final byte of the Full Hash Prefix encodes the hash length.
  120. *
  121. * This needs to be revisited should hash algorithms with more than
  122. * 1016 bits (127 bytes * 8) ever be added. The length would then
  123. * be encoded into more than one byte by ASN.1.
  124. */
  125. static_assert(HASH_MAX_DIGESTSIZE <= 127);
  126. return len != p->data[p->size - 1];
  127. }
  128. struct rsassa_pkcs1_ctx {
  129. struct crypto_akcipher *child;
  130. unsigned int key_size;
  131. };
  132. struct rsassa_pkcs1_inst_ctx {
  133. struct crypto_akcipher_spawn spawn;
  134. const struct hash_prefix *hash_prefix;
  135. };
  136. static int rsassa_pkcs1_sign(struct crypto_sig *tfm,
  137. const void *src, unsigned int slen,
  138. void *dst, unsigned int dlen)
  139. {
  140. struct sig_instance *inst = sig_alg_instance(tfm);
  141. struct rsassa_pkcs1_inst_ctx *ictx = sig_instance_ctx(inst);
  142. const struct hash_prefix *hash_prefix = ictx->hash_prefix;
  143. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  144. unsigned int pad_len;
  145. unsigned int ps_end;
  146. unsigned int len;
  147. u8 *in_buf;
  148. int err;
  149. if (!ctx->key_size)
  150. return -EINVAL;
  151. if (dlen < ctx->key_size)
  152. return -EOVERFLOW;
  153. if (rsassa_pkcs1_invalid_hash_len(slen, hash_prefix))
  154. return -EINVAL;
  155. if (slen + hash_prefix->size > ctx->key_size - 11)
  156. return -EOVERFLOW;
  157. pad_len = ctx->key_size - slen - hash_prefix->size - 1;
  158. /* RFC 8017 sec 8.2.1 step 1 - EMSA-PKCS1-v1_5 encoding generation */
  159. in_buf = dst;
  160. memmove(in_buf + pad_len + hash_prefix->size, src, slen);
  161. memcpy(in_buf + pad_len, hash_prefix->data, hash_prefix->size);
  162. ps_end = pad_len - 1;
  163. in_buf[0] = 0x01;
  164. memset(in_buf + 1, 0xff, ps_end - 1);
  165. in_buf[ps_end] = 0x00;
  166. /* RFC 8017 sec 8.2.1 step 2 - RSA signature */
  167. err = crypto_akcipher_sync_decrypt(ctx->child, in_buf,
  168. ctx->key_size - 1, in_buf,
  169. ctx->key_size);
  170. if (err < 0)
  171. return err;
  172. len = err;
  173. pad_len = ctx->key_size - len;
  174. /* Four billion to one */
  175. if (unlikely(pad_len)) {
  176. memmove(dst + pad_len, dst, len);
  177. memset(dst, 0, pad_len);
  178. }
  179. return ctx->key_size;
  180. }
  181. static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
  182. const void *src, unsigned int slen,
  183. const void *digest, unsigned int dlen)
  184. {
  185. struct sig_instance *inst = sig_alg_instance(tfm);
  186. struct rsassa_pkcs1_inst_ctx *ictx = sig_instance_ctx(inst);
  187. const struct hash_prefix *hash_prefix = ictx->hash_prefix;
  188. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  189. unsigned int child_reqsize = crypto_akcipher_reqsize(ctx->child);
  190. struct akcipher_request *child_req __free(kfree_sensitive) = NULL;
  191. struct crypto_wait cwait;
  192. struct scatterlist sg;
  193. unsigned int dst_len;
  194. unsigned int pos;
  195. u8 *out_buf;
  196. int err;
  197. /* RFC 8017 sec 8.2.2 step 1 - length checking */
  198. if (!ctx->key_size ||
  199. slen != ctx->key_size ||
  200. rsassa_pkcs1_invalid_hash_len(dlen, hash_prefix))
  201. return -EINVAL;
  202. /* RFC 8017 sec 8.2.2 step 2 - RSA verification */
  203. child_req = kmalloc(sizeof(*child_req) + child_reqsize + ctx->key_size,
  204. GFP_KERNEL);
  205. if (!child_req)
  206. return -ENOMEM;
  207. out_buf = (u8 *)(child_req + 1) + child_reqsize;
  208. memcpy(out_buf, src, slen);
  209. crypto_init_wait(&cwait);
  210. sg_init_one(&sg, out_buf, slen);
  211. akcipher_request_set_tfm(child_req, ctx->child);
  212. akcipher_request_set_crypt(child_req, &sg, &sg, slen, slen);
  213. akcipher_request_set_callback(child_req, CRYPTO_TFM_REQ_MAY_SLEEP,
  214. crypto_req_done, &cwait);
  215. err = crypto_akcipher_encrypt(child_req);
  216. err = crypto_wait_req(err, &cwait);
  217. if (err)
  218. return err;
  219. /* RFC 8017 sec 8.2.2 step 3 - EMSA-PKCS1-v1_5 encoding verification */
  220. dst_len = child_req->dst_len;
  221. if (dst_len < ctx->key_size - 1)
  222. return -EINVAL;
  223. if (dst_len == ctx->key_size) {
  224. if (out_buf[0] != 0x00)
  225. /* Encrypted value had no leading 0 byte */
  226. return -EINVAL;
  227. dst_len--;
  228. out_buf++;
  229. }
  230. if (out_buf[0] != 0x01)
  231. return -EBADMSG;
  232. for (pos = 1; pos < dst_len; pos++)
  233. if (out_buf[pos] != 0xff)
  234. break;
  235. if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
  236. return -EBADMSG;
  237. pos++;
  238. if (hash_prefix->size > dst_len - pos)
  239. return -EBADMSG;
  240. if (crypto_memneq(out_buf + pos, hash_prefix->data, hash_prefix->size))
  241. return -EBADMSG;
  242. pos += hash_prefix->size;
  243. /* RFC 8017 sec 8.2.2 step 4 - comparison of digest with out_buf */
  244. if (dlen != dst_len - pos)
  245. return -EKEYREJECTED;
  246. if (memcmp(digest, out_buf + pos, dlen) != 0)
  247. return -EKEYREJECTED;
  248. return 0;
  249. }
  250. static unsigned int rsassa_pkcs1_key_size(struct crypto_sig *tfm)
  251. {
  252. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  253. return ctx->key_size * BITS_PER_BYTE;
  254. }
  255. static int rsassa_pkcs1_set_pub_key(struct crypto_sig *tfm,
  256. const void *key, unsigned int keylen)
  257. {
  258. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  259. return rsa_set_key(ctx->child, &ctx->key_size, RSA_PUB, key, keylen);
  260. }
  261. static int rsassa_pkcs1_set_priv_key(struct crypto_sig *tfm,
  262. const void *key, unsigned int keylen)
  263. {
  264. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  265. return rsa_set_key(ctx->child, &ctx->key_size, RSA_PRIV, key, keylen);
  266. }
  267. static int rsassa_pkcs1_init_tfm(struct crypto_sig *tfm)
  268. {
  269. struct sig_instance *inst = sig_alg_instance(tfm);
  270. struct rsassa_pkcs1_inst_ctx *ictx = sig_instance_ctx(inst);
  271. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  272. struct crypto_akcipher *child_tfm;
  273. child_tfm = crypto_spawn_akcipher(&ictx->spawn);
  274. if (IS_ERR(child_tfm))
  275. return PTR_ERR(child_tfm);
  276. ctx->child = child_tfm;
  277. return 0;
  278. }
  279. static void rsassa_pkcs1_exit_tfm(struct crypto_sig *tfm)
  280. {
  281. struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
  282. crypto_free_akcipher(ctx->child);
  283. }
  284. static void rsassa_pkcs1_free(struct sig_instance *inst)
  285. {
  286. struct rsassa_pkcs1_inst_ctx *ctx = sig_instance_ctx(inst);
  287. struct crypto_akcipher_spawn *spawn = &ctx->spawn;
  288. crypto_drop_akcipher(spawn);
  289. kfree(inst);
  290. }
  291. static int rsassa_pkcs1_create(struct crypto_template *tmpl, struct rtattr **tb)
  292. {
  293. struct rsassa_pkcs1_inst_ctx *ctx;
  294. struct akcipher_alg *rsa_alg;
  295. struct sig_instance *inst;
  296. const char *hash_name;
  297. u32 mask;
  298. int err;
  299. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SIG, &mask);
  300. if (err)
  301. return err;
  302. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  303. if (!inst)
  304. return -ENOMEM;
  305. ctx = sig_instance_ctx(inst);
  306. err = crypto_grab_akcipher(&ctx->spawn, sig_crypto_instance(inst),
  307. crypto_attr_alg_name(tb[1]), 0, mask);
  308. if (err)
  309. goto err_free_inst;
  310. rsa_alg = crypto_spawn_akcipher_alg(&ctx->spawn);
  311. if (strcmp(rsa_alg->base.cra_name, "rsa") != 0) {
  312. err = -EINVAL;
  313. goto err_free_inst;
  314. }
  315. hash_name = crypto_attr_alg_name(tb[2]);
  316. if (IS_ERR(hash_name)) {
  317. err = PTR_ERR(hash_name);
  318. goto err_free_inst;
  319. }
  320. ctx->hash_prefix = rsassa_pkcs1_find_hash_prefix(hash_name);
  321. if (!ctx->hash_prefix) {
  322. err = -EINVAL;
  323. goto err_free_inst;
  324. }
  325. err = -ENAMETOOLONG;
  326. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  327. "pkcs1(%s,%s)", rsa_alg->base.cra_name,
  328. hash_name) >= CRYPTO_MAX_ALG_NAME)
  329. goto err_free_inst;
  330. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  331. "pkcs1(%s,%s)", rsa_alg->base.cra_driver_name,
  332. hash_name) >= CRYPTO_MAX_ALG_NAME)
  333. goto err_free_inst;
  334. inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
  335. inst->alg.base.cra_ctxsize = sizeof(struct rsassa_pkcs1_ctx);
  336. inst->alg.init = rsassa_pkcs1_init_tfm;
  337. inst->alg.exit = rsassa_pkcs1_exit_tfm;
  338. inst->alg.sign = rsassa_pkcs1_sign;
  339. inst->alg.verify = rsassa_pkcs1_verify;
  340. inst->alg.key_size = rsassa_pkcs1_key_size;
  341. inst->alg.set_pub_key = rsassa_pkcs1_set_pub_key;
  342. inst->alg.set_priv_key = rsassa_pkcs1_set_priv_key;
  343. inst->free = rsassa_pkcs1_free;
  344. err = sig_register_instance(tmpl, inst);
  345. if (err) {
  346. err_free_inst:
  347. rsassa_pkcs1_free(inst);
  348. }
  349. return err;
  350. }
  351. struct crypto_template rsassa_pkcs1_tmpl = {
  352. .name = "pkcs1",
  353. .create = rsassa_pkcs1_create,
  354. .module = THIS_MODULE,
  355. };
  356. MODULE_ALIAS_CRYPTO("pkcs1");