rsa_helper.c 2.6 KB

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