rsa_helper.c 4.2 KB

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