rsa_helper.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef __UBOOT__
  9. #include <linux/compat.h>
  10. #include <linux/kernel.h>
  11. #include <linux/export.h>
  12. #endif
  13. #include <linux/err.h>
  14. #ifndef __UBOOT__
  15. #include <linux/fips.h>
  16. #endif
  17. #include <crypto/internal/rsa.h>
  18. #include "rsapubkey.asn1.h"
  19. #ifndef __UBOOT__
  20. #include "rsaprivkey.asn1.h"
  21. #endif
  22. int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
  23. const void *value, size_t vlen)
  24. {
  25. struct rsa_key *key = context;
  26. #ifndef __UBOOT__
  27. const u8 *ptr = value;
  28. size_t n_sz = vlen;
  29. #endif
  30. /* invalid key provided */
  31. if (!value || !vlen)
  32. return -EINVAL;
  33. #ifndef __UBOOT__
  34. if (fips_enabled) {
  35. while (n_sz && !*ptr) {
  36. ptr++;
  37. n_sz--;
  38. }
  39. /* In FIPS mode only allow key size 2K and higher */
  40. if (n_sz < 256) {
  41. pr_err("RSA: key size not allowed in FIPS mode\n");
  42. return -EINVAL;
  43. }
  44. }
  45. #endif
  46. key->n = value;
  47. key->n_sz = vlen;
  48. return 0;
  49. }
  50. int rsa_get_e(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->e = value;
  58. key->e_sz = vlen;
  59. return 0;
  60. }
  61. int rsa_get_d(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 || !key->n_sz || !vlen || vlen > key->n_sz)
  67. return -EINVAL;
  68. key->d = value;
  69. key->d_sz = vlen;
  70. return 0;
  71. }
  72. int rsa_get_p(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->p = value;
  80. key->p_sz = vlen;
  81. return 0;
  82. }
  83. int rsa_get_q(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->q = value;
  91. key->q_sz = vlen;
  92. return 0;
  93. }
  94. int rsa_get_dp(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->dp = value;
  102. key->dp_sz = vlen;
  103. return 0;
  104. }
  105. int rsa_get_dq(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->dq = value;
  113. key->dq_sz = vlen;
  114. return 0;
  115. }
  116. int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
  117. const void *value, size_t vlen)
  118. {
  119. struct rsa_key *key = context;
  120. /* invalid key provided */
  121. if (!value || !vlen || vlen > key->n_sz)
  122. return -EINVAL;
  123. key->qinv = value;
  124. key->qinv_sz = vlen;
  125. return 0;
  126. }
  127. /**
  128. * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
  129. * provided struct rsa_key, pointers to the raw key as is,
  130. * so that the caller can copy it or MPI parse it, etc.
  131. *
  132. * @rsa_key: struct rsa_key key representation
  133. * @key: key in BER format
  134. * @key_len: length of key
  135. *
  136. * Return: 0 on success or error code in case of error
  137. */
  138. int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
  139. unsigned int key_len)
  140. {
  141. return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
  142. }
  143. EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
  144. #ifndef __UBOOT__
  145. /**
  146. * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
  147. * provided struct rsa_key, pointers to the raw key
  148. * as is, so that the caller can copy it or MPI parse it,
  149. * etc.
  150. *
  151. * @rsa_key: struct rsa_key key representation
  152. * @key: key in BER format
  153. * @key_len: length of key
  154. *
  155. * Return: 0 on success or error code in case of error
  156. */
  157. int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
  158. unsigned int key_len)
  159. {
  160. return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
  161. }
  162. EXPORT_SYMBOL_GPL(rsa_parse_priv_key);
  163. #endif