2
0

rsa_schemes.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/core.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/obj_mac.h>
  13. #include "internal/nelem.h"
  14. #include "crypto/rsa.h"
  15. static int meth2nid(const void *meth,
  16. int (*meth_is_a)(const void *meth, const char *name),
  17. const OSSL_ITEM *items, size_t items_n)
  18. {
  19. size_t i;
  20. if (meth != NULL)
  21. for (i = 0; i < items_n; i++)
  22. if (meth_is_a(meth, items[i].ptr))
  23. return (int)items[i].id;
  24. return NID_undef;
  25. }
  26. static const char *nid2name(int meth, const OSSL_ITEM *items, size_t items_n)
  27. {
  28. size_t i;
  29. for (i = 0; i < items_n; i++)
  30. if (meth == (int)items[i].id)
  31. return items[i].ptr;
  32. return NULL;
  33. }
  34. /*
  35. * The list of permitted hash functions are taken from
  36. * https://tools.ietf.org/html/rfc8017#appendix-A.2.1:
  37. *
  38. * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
  39. * { OID id-sha1 PARAMETERS NULL }|
  40. * { OID id-sha224 PARAMETERS NULL }|
  41. * { OID id-sha256 PARAMETERS NULL }|
  42. * { OID id-sha384 PARAMETERS NULL }|
  43. * { OID id-sha512 PARAMETERS NULL }|
  44. * { OID id-sha512-224 PARAMETERS NULL }|
  45. * { OID id-sha512-256 PARAMETERS NULL },
  46. * ... -- Allows for future expansion --
  47. * }
  48. */
  49. static const OSSL_ITEM oaeppss_name_nid_map[] = {
  50. { NID_sha1, OSSL_DIGEST_NAME_SHA1 },
  51. { NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
  52. { NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
  53. { NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
  54. { NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
  55. { NID_sha512_224, OSSL_DIGEST_NAME_SHA2_512_224 },
  56. { NID_sha512_256, OSSL_DIGEST_NAME_SHA2_512_256 },
  57. };
  58. static int md_is_a(const void *md, const char *name)
  59. {
  60. return EVP_MD_is_a(md, name);
  61. }
  62. int ossl_rsa_oaeppss_md2nid(const EVP_MD *md)
  63. {
  64. return meth2nid(md, md_is_a,
  65. oaeppss_name_nid_map, OSSL_NELEM(oaeppss_name_nid_map));
  66. }
  67. const char *ossl_rsa_oaeppss_nid2name(int md)
  68. {
  69. return nid2name(md, oaeppss_name_nid_map, OSSL_NELEM(oaeppss_name_nid_map));
  70. }
  71. const char *ossl_rsa_mgf_nid2name(int mgf)
  72. {
  73. if (mgf == NID_mgf1)
  74. return SN_mgf1;
  75. return NULL;
  76. }