rsa_helper.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * RSA key extract helper
  4. *
  5. * Copyright (c) 2015, Intel Corporation
  6. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/err.h>
  11. #include <linux/fips.h>
  12. #include <crypto/internal/rsa.h>
  13. #include "rsapubkey.asn1.h"
  14. #include "rsaprivkey.asn1.h"
  15. int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
  16. const void *value, size_t vlen)
  17. {
  18. struct rsa_key *key = context;
  19. const u8 *ptr = value;
  20. size_t n_sz = vlen;
  21. /* invalid key provided */
  22. if (!value || !vlen)
  23. return -EINVAL;
  24. if (fips_enabled) {
  25. while (n_sz && !*ptr) {
  26. ptr++;
  27. n_sz--;
  28. }
  29. /* In FIPS mode only allow key size 2K and higher */
  30. if (n_sz < 256) {
  31. pr_err("RSA: key size not allowed in FIPS mode\n");
  32. return -EINVAL;
  33. }
  34. }
  35. key->n = value;
  36. key->n_sz = vlen;
  37. return 0;
  38. }
  39. int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
  40. const void *value, size_t vlen)
  41. {
  42. struct rsa_key *key = context;
  43. /* invalid key provided */
  44. if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
  45. return -EINVAL;
  46. key->e = value;
  47. key->e_sz = vlen;
  48. return 0;
  49. }
  50. int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
  51. const void *value, size_t vlen)
  52. {
  53. struct rsa_key *key = context;
  54. /* invalid key provided */
  55. if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
  56. return -EINVAL;
  57. key->d = value;
  58. key->d_sz = vlen;
  59. return 0;
  60. }
  61. int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
  62. const void *value, size_t vlen)
  63. {
  64. struct rsa_key *key = context;
  65. /* invalid key provided */
  66. if (!value || !vlen || vlen > key->n_sz)
  67. return -EINVAL;
  68. key->p = value;
  69. key->p_sz = vlen;
  70. return 0;
  71. }
  72. int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
  73. const void *value, size_t vlen)
  74. {
  75. struct rsa_key *key = context;
  76. /* invalid key provided */
  77. if (!value || !vlen || vlen > key->n_sz)
  78. return -EINVAL;
  79. key->q = value;
  80. key->q_sz = vlen;
  81. return 0;
  82. }
  83. int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
  84. const void *value, size_t vlen)
  85. {
  86. struct rsa_key *key = context;
  87. /* invalid key provided */
  88. if (!value || !vlen || vlen > key->n_sz)
  89. return -EINVAL;
  90. key->dp = value;
  91. key->dp_sz = vlen;
  92. return 0;
  93. }
  94. int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
  95. const void *value, size_t vlen)
  96. {
  97. struct rsa_key *key = context;
  98. /* invalid key provided */
  99. if (!value || !vlen || vlen > key->n_sz)
  100. return -EINVAL;
  101. key->dq = value;
  102. key->dq_sz = vlen;
  103. return 0;
  104. }
  105. int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
  106. const void *value, size_t vlen)
  107. {
  108. struct rsa_key *key = context;
  109. /* invalid key provided */
  110. if (!value || !vlen || vlen > key->n_sz)
  111. return -EINVAL;
  112. key->qinv = value;
  113. key->qinv_sz = vlen;
  114. return 0;
  115. }
  116. /**
  117. * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
  118. * provided struct rsa_key, pointers to the raw key as is,
  119. * so that the caller can copy it or MPI parse it, etc.
  120. *
  121. * @rsa_key: struct rsa_key key representation
  122. * @key: key in BER format
  123. * @key_len: length of key
  124. *
  125. * Return: 0 on success or error code in case of error
  126. */
  127. int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
  128. unsigned int key_len)
  129. {
  130. return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
  131. }
  132. EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
  133. /**
  134. * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
  135. * provided struct rsa_key, pointers to the raw key
  136. * as is, so that the caller can copy it or MPI parse it,
  137. * etc.
  138. *
  139. * @rsa_key: struct rsa_key key representation
  140. * @key: key in BER format
  141. * @key_len: length of key
  142. *
  143. * Return: 0 on success or error code in case of error
  144. */
  145. int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
  146. unsigned int key_len)
  147. {
  148. return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
  149. }
  150. EXPORT_SYMBOL_GPL(rsa_parse_priv_key);