rsa_helper.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. key->n = mpi_read_raw_data(value, vlen);
  25. if (!key->n)
  26. return -ENOMEM;
  27. /* In FIPS mode only allow key size 2K & 3K */
  28. if (fips_enabled && (mpi_get_size(key->n) != 256 &&
  29. mpi_get_size(key->n) != 384)) {
  30. pr_err("RSA: key size not allowed in FIPS mode\n");
  31. mpi_free(key->n);
  32. key->n = NULL;
  33. return -EINVAL;
  34. }
  35. return 0;
  36. }
  37. int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
  38. const void *value, size_t vlen)
  39. {
  40. struct rsa_key *key = context;
  41. key->e = mpi_read_raw_data(value, vlen);
  42. if (!key->e)
  43. return -ENOMEM;
  44. return 0;
  45. }
  46. int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
  47. const void *value, size_t vlen)
  48. {
  49. struct rsa_key *key = context;
  50. key->d = mpi_read_raw_data(value, vlen);
  51. if (!key->d)
  52. return -ENOMEM;
  53. /* In FIPS mode only allow key size 2K & 3K */
  54. if (fips_enabled && (mpi_get_size(key->d) != 256 &&
  55. mpi_get_size(key->d) != 384)) {
  56. pr_err("RSA: key size not allowed in FIPS mode\n");
  57. mpi_free(key->d);
  58. key->d = NULL;
  59. return -EINVAL;
  60. }
  61. return 0;
  62. }
  63. static void free_mpis(struct rsa_key *key)
  64. {
  65. mpi_free(key->n);
  66. mpi_free(key->e);
  67. mpi_free(key->d);
  68. key->n = NULL;
  69. key->e = NULL;
  70. key->d = NULL;
  71. }
  72. /**
  73. * rsa_free_key() - frees rsa key allocated by rsa_parse_key()
  74. *
  75. * @rsa_key: struct rsa_key key representation
  76. */
  77. void rsa_free_key(struct rsa_key *key)
  78. {
  79. free_mpis(key);
  80. }
  81. EXPORT_SYMBOL_GPL(rsa_free_key);
  82. /**
  83. * rsa_parse_pub_key() - extracts an rsa public key from BER encoded buffer
  84. * and stores it in the provided struct rsa_key
  85. *
  86. * @rsa_key: struct rsa_key key representation
  87. * @key: key in BER format
  88. * @key_len: length of key
  89. *
  90. * Return: 0 on success or error code in case of error
  91. */
  92. int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
  93. unsigned int key_len)
  94. {
  95. int ret;
  96. free_mpis(rsa_key);
  97. ret = asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
  98. if (ret < 0)
  99. goto error;
  100. return 0;
  101. error:
  102. free_mpis(rsa_key);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
  106. /**
  107. * rsa_parse_pub_key() - extracts an rsa private key from BER encoded buffer
  108. * and stores it in the provided struct rsa_key
  109. *
  110. * @rsa_key: struct rsa_key key representation
  111. * @key: key in BER format
  112. * @key_len: length of key
  113. *
  114. * Return: 0 on success or error code in case of error
  115. */
  116. int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
  117. unsigned int key_len)
  118. {
  119. int ret;
  120. free_mpis(rsa_key);
  121. ret = asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
  122. if (ret < 0)
  123. goto error;
  124. return 0;
  125. error:
  126. free_mpis(rsa_key);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL_GPL(rsa_parse_priv_key);