dh_support.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2020-2021 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 <string.h> /* strcmp */
  10. #include <openssl/dh.h>
  11. #include "internal/nelem.h"
  12. #include "crypto/dh.h"
  13. typedef struct dh_name2id_st{
  14. const char *name;
  15. int id;
  16. int type;
  17. } DH_GENTYPE_NAME2ID;
  18. /* Indicates that the paramgen_type can be used for either DH or DHX */
  19. #define TYPE_ANY -1
  20. #ifndef OPENSSL_NO_DH
  21. # define TYPE_DH DH_FLAG_TYPE_DH
  22. # define TYPE_DHX DH_FLAG_TYPE_DHX
  23. #else
  24. # define TYPE_DH 0
  25. # define TYPE_DHX 0
  26. #endif
  27. static const DH_GENTYPE_NAME2ID dhtype2id[] =
  28. {
  29. { "group", DH_PARAMGEN_TYPE_GROUP, TYPE_ANY },
  30. { "generator", DH_PARAMGEN_TYPE_GENERATOR, TYPE_DH },
  31. { "fips186_4", DH_PARAMGEN_TYPE_FIPS_186_4, TYPE_DHX },
  32. { "fips186_2", DH_PARAMGEN_TYPE_FIPS_186_2, TYPE_DHX },
  33. };
  34. const char *ossl_dh_gen_type_id2name(int id)
  35. {
  36. size_t i;
  37. for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) {
  38. if (dhtype2id[i].id == id)
  39. return dhtype2id[i].name;
  40. }
  41. return NULL;
  42. }
  43. #ifndef OPENSSL_NO_DH
  44. int ossl_dh_gen_type_name2id(const char *name, int type)
  45. {
  46. size_t i;
  47. for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) {
  48. if ((dhtype2id[i].type == TYPE_ANY
  49. || type == dhtype2id[i].type)
  50. && strcmp(dhtype2id[i].name, name) == 0)
  51. return dhtype2id[i].id;
  52. }
  53. return -1;
  54. }
  55. #endif