rsa.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* RSA asymmetric public-key algorithm [RFC3447]
  2. *
  3. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "RSA: "fmt
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include "public_key.h"
  16. MODULE_LICENSE("GPL");
  17. MODULE_DESCRIPTION("RSA Public Key Algorithm");
  18. #define kenter(FMT, ...) \
  19. pr_devel("==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  20. #define kleave(FMT, ...) \
  21. pr_devel("<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  22. /*
  23. * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
  24. */
  25. static const u8 RSA_digest_info_MD5[] = {
  26. 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08,
  27. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* OID */
  28. 0x05, 0x00, 0x04, 0x10
  29. };
  30. static const u8 RSA_digest_info_SHA1[] = {
  31. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  32. 0x2B, 0x0E, 0x03, 0x02, 0x1A,
  33. 0x05, 0x00, 0x04, 0x14
  34. };
  35. static const u8 RSA_digest_info_RIPE_MD_160[] = {
  36. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  37. 0x2B, 0x24, 0x03, 0x02, 0x01,
  38. 0x05, 0x00, 0x04, 0x14
  39. };
  40. static const u8 RSA_digest_info_SHA224[] = {
  41. 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
  42. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
  43. 0x05, 0x00, 0x04, 0x1C
  44. };
  45. static const u8 RSA_digest_info_SHA256[] = {
  46. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
  47. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  48. 0x05, 0x00, 0x04, 0x20
  49. };
  50. static const u8 RSA_digest_info_SHA384[] = {
  51. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
  52. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
  53. 0x05, 0x00, 0x04, 0x30
  54. };
  55. static const u8 RSA_digest_info_SHA512[] = {
  56. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
  57. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
  58. 0x05, 0x00, 0x04, 0x40
  59. };
  60. static const struct {
  61. const u8 *data;
  62. size_t size;
  63. } RSA_ASN1_templates[PKEY_HASH__LAST] = {
  64. #define _(X) { RSA_digest_info_##X, sizeof(RSA_digest_info_##X) }
  65. [PKEY_HASH_MD5] = _(MD5),
  66. [PKEY_HASH_SHA1] = _(SHA1),
  67. [PKEY_HASH_RIPE_MD_160] = _(RIPE_MD_160),
  68. [PKEY_HASH_SHA256] = _(SHA256),
  69. [PKEY_HASH_SHA384] = _(SHA384),
  70. [PKEY_HASH_SHA512] = _(SHA512),
  71. [PKEY_HASH_SHA224] = _(SHA224),
  72. #undef _
  73. };
  74. /*
  75. * RSAVP1() function [RFC3447 sec 5.2.2]
  76. */
  77. static int RSAVP1(const struct public_key *key, MPI s, MPI *_m)
  78. {
  79. MPI m;
  80. int ret;
  81. /* (1) Validate 0 <= s < n */
  82. if (mpi_cmp_ui(s, 0) < 0) {
  83. kleave(" = -EBADMSG [s < 0]");
  84. return -EBADMSG;
  85. }
  86. if (mpi_cmp(s, key->rsa.n) >= 0) {
  87. kleave(" = -EBADMSG [s >= n]");
  88. return -EBADMSG;
  89. }
  90. m = mpi_alloc(0);
  91. if (!m)
  92. return -ENOMEM;
  93. /* (2) m = s^e mod n */
  94. ret = mpi_powm(m, s, key->rsa.e, key->rsa.n);
  95. if (ret < 0) {
  96. mpi_free(m);
  97. return ret;
  98. }
  99. *_m = m;
  100. return 0;
  101. }
  102. /*
  103. * Integer to Octet String conversion [RFC3447 sec 4.1]
  104. */
  105. static int RSA_I2OSP(MPI x, size_t xLen, u8 **_X)
  106. {
  107. unsigned X_size, x_size;
  108. int X_sign;
  109. u8 *X;
  110. /* Make sure the string is the right length. The number should begin
  111. * with { 0x00, 0x01, ... } so we have to account for 15 leading zero
  112. * bits not being reported by MPI.
  113. */
  114. x_size = mpi_get_nbits(x);
  115. pr_devel("size(x)=%u xLen*8=%zu\n", x_size, xLen * 8);
  116. if (x_size != xLen * 8 - 15)
  117. return -ERANGE;
  118. X = mpi_get_buffer(x, &X_size, &X_sign);
  119. if (!X)
  120. return -ENOMEM;
  121. if (X_sign < 0) {
  122. kfree(X);
  123. return -EBADMSG;
  124. }
  125. if (X_size != xLen - 1) {
  126. kfree(X);
  127. return -EBADMSG;
  128. }
  129. *_X = X;
  130. return 0;
  131. }
  132. /*
  133. * Perform the RSA signature verification.
  134. * @H: Value of hash of data and metadata
  135. * @EM: The computed signature value
  136. * @k: The size of EM (EM[0] is an invalid location but should hold 0x00)
  137. * @hash_size: The size of H
  138. * @asn1_template: The DigestInfo ASN.1 template
  139. * @asn1_size: Size of asm1_template[]
  140. */
  141. static int RSA_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size,
  142. const u8 *asn1_template, size_t asn1_size)
  143. {
  144. unsigned PS_end, T_offset, i;
  145. kenter(",,%zu,%zu,%zu", k, hash_size, asn1_size);
  146. if (k < 2 + 1 + asn1_size + hash_size)
  147. return -EBADMSG;
  148. /* Decode the EMSA-PKCS1-v1_5 */
  149. if (EM[1] != 0x01) {
  150. kleave(" = -EBADMSG [EM[1] == %02u]", EM[1]);
  151. return -EBADMSG;
  152. }
  153. T_offset = k - (asn1_size + hash_size);
  154. PS_end = T_offset - 1;
  155. if (EM[PS_end] != 0x00) {
  156. kleave(" = -EBADMSG [EM[T-1] == %02u]", EM[PS_end]);
  157. return -EBADMSG;
  158. }
  159. for (i = 2; i < PS_end; i++) {
  160. if (EM[i] != 0xff) {
  161. kleave(" = -EBADMSG [EM[PS%x] == %02u]", i - 2, EM[i]);
  162. return -EBADMSG;
  163. }
  164. }
  165. if (memcmp(asn1_template, EM + T_offset, asn1_size) != 0) {
  166. kleave(" = -EBADMSG [EM[T] ASN.1 mismatch]");
  167. return -EBADMSG;
  168. }
  169. if (memcmp(H, EM + T_offset + asn1_size, hash_size) != 0) {
  170. kleave(" = -EKEYREJECTED [EM[T] hash mismatch]");
  171. return -EKEYREJECTED;
  172. }
  173. kleave(" = 0");
  174. return 0;
  175. }
  176. /*
  177. * Perform the verification step [RFC3447 sec 8.2.2].
  178. */
  179. static int RSA_verify_signature(const struct public_key *key,
  180. const struct public_key_signature *sig)
  181. {
  182. size_t tsize;
  183. int ret;
  184. /* Variables as per RFC3447 sec 8.2.2 */
  185. const u8 *H = sig->digest;
  186. u8 *EM = NULL;
  187. MPI m = NULL;
  188. size_t k;
  189. kenter("");
  190. if (!RSA_ASN1_templates[sig->pkey_hash_algo].data)
  191. return -ENOTSUPP;
  192. /* (1) Check the signature size against the public key modulus size */
  193. k = mpi_get_nbits(key->rsa.n);
  194. tsize = mpi_get_nbits(sig->rsa.s);
  195. /* According to RFC 4880 sec 3.2, length of MPI is computed starting
  196. * from most significant bit. So the RFC 3447 sec 8.2.2 size check
  197. * must be relaxed to conform with shorter signatures - so we fail here
  198. * only if signature length is longer than modulus size.
  199. */
  200. pr_devel("step 1: k=%zu size(S)=%zu\n", k, tsize);
  201. if (k < tsize) {
  202. ret = -EBADMSG;
  203. goto error;
  204. }
  205. /* Round up and convert to octets */
  206. k = (k + 7) / 8;
  207. /* (2b) Apply the RSAVP1 verification primitive to the public key */
  208. ret = RSAVP1(key, sig->rsa.s, &m);
  209. if (ret < 0)
  210. goto error;
  211. /* (2c) Convert the message representative (m) to an encoded message
  212. * (EM) of length k octets.
  213. *
  214. * NOTE! The leading zero byte is suppressed by MPI, so we pass a
  215. * pointer to the _preceding_ byte to RSA_verify()!
  216. */
  217. ret = RSA_I2OSP(m, k, &EM);
  218. if (ret < 0)
  219. goto error;
  220. ret = RSA_verify(H, EM - 1, k, sig->digest_size,
  221. RSA_ASN1_templates[sig->pkey_hash_algo].data,
  222. RSA_ASN1_templates[sig->pkey_hash_algo].size);
  223. error:
  224. kfree(EM);
  225. mpi_free(m);
  226. kleave(" = %d", ret);
  227. return ret;
  228. }
  229. const struct public_key_algorithm RSA_public_key_algorithm = {
  230. .name = "RSA",
  231. .n_pub_mpi = 2,
  232. .n_sec_mpi = 3,
  233. .n_sig_mpi = 1,
  234. .verify_signature = RSA_verify_signature,
  235. };
  236. EXPORT_SYMBOL_GPL(RSA_public_key_algorithm);