rsa_helper.c 4.2 KB

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