fipsprov.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2019 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>
  10. #include <stdio.h>
  11. #include <openssl/core.h>
  12. #include <openssl/core_numbers.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/params.h>
  15. /* Functions provided by the core */
  16. static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
  17. static OSSL_core_get_params_fn *c_get_params = NULL;
  18. /* Parameters we provide to the core */
  19. static const OSSL_ITEM fips_param_types[] = {
  20. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
  21. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
  22. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
  23. { 0, NULL }
  24. };
  25. static const OSSL_ITEM *fips_get_param_types(const OSSL_PROVIDER *prov)
  26. {
  27. return fips_param_types;
  28. }
  29. static int fips_get_params(const OSSL_PROVIDER *prov,
  30. const OSSL_PARAM params[])
  31. {
  32. const OSSL_PARAM *p;
  33. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  34. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
  35. return 0;
  36. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  37. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  38. return 0;
  39. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  40. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  41. return 0;
  42. return 1;
  43. }
  44. extern const OSSL_DISPATCH sha256_functions[];
  45. static const OSSL_ALGORITHM fips_digests[] = {
  46. { "SHA256", "fips=yes", sha256_functions },
  47. { NULL, NULL, NULL }
  48. };
  49. static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
  50. int operation_id,
  51. int *no_cache)
  52. {
  53. *no_cache = 0;
  54. switch (operation_id) {
  55. case OSSL_OP_DIGEST:
  56. return fips_digests;
  57. }
  58. return NULL;
  59. }
  60. /* Functions we provide to the core */
  61. static const OSSL_DISPATCH fips_dispatch_table[] = {
  62. { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))fips_get_param_types },
  63. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
  64. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
  65. { 0, NULL }
  66. };
  67. int OSSL_provider_init(const OSSL_PROVIDER *provider,
  68. const OSSL_DISPATCH *in,
  69. const OSSL_DISPATCH **out,
  70. void **provctx)
  71. {
  72. for (; in->function_id != 0; in++) {
  73. switch (in->function_id) {
  74. case OSSL_FUNC_CORE_GET_PARAM_TYPES:
  75. c_get_param_types = OSSL_get_core_get_param_types(in);
  76. break;
  77. case OSSL_FUNC_CORE_GET_PARAMS:
  78. c_get_params = OSSL_get_core_get_params(in);
  79. break;
  80. /* Just ignore anything we don't understand */
  81. default:
  82. break;
  83. }
  84. }
  85. *out = fips_dispatch_table;
  86. return 1;
  87. }