dh_kdf.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2013-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. /*
  10. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "e_os.h"
  15. #include "e_os.h"
  16. #include <string.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/asn1.h>
  21. #include <openssl/kdf.h>
  22. #include "internal/provider.h"
  23. #include "crypto/dh.h"
  24. /* Key derivation function from X9.63/SECG */
  25. int ossl_dh_kdf_X9_42_asn1(unsigned char *out, size_t outlen,
  26. const unsigned char *Z, size_t Zlen,
  27. const char *cek_alg,
  28. const unsigned char *ukm, size_t ukmlen,
  29. const EVP_MD *md,
  30. OSSL_LIB_CTX *libctx, const char *propq)
  31. {
  32. int ret = 0;
  33. EVP_KDF_CTX *kctx = NULL;
  34. EVP_KDF *kdf = NULL;
  35. OSSL_PARAM params[5], *p = params;
  36. const char *mdname = EVP_MD_get0_name(md);
  37. kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_X942KDF_ASN1, propq);
  38. kctx = EVP_KDF_CTX_new(kdf);
  39. if (kctx == NULL)
  40. goto err;
  41. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  42. (char *)mdname, 0);
  43. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  44. (unsigned char *)Z, Zlen);
  45. if (ukm != NULL)
  46. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_UKM,
  47. (unsigned char *)ukm, ukmlen);
  48. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
  49. (char *)cek_alg, 0);
  50. *p = OSSL_PARAM_construct_end();
  51. ret = EVP_KDF_derive(kctx, out, outlen, params) > 0;
  52. err:
  53. EVP_KDF_CTX_free(kctx);
  54. EVP_KDF_free(kdf);
  55. return ret;
  56. }
  57. #if !defined(FIPS_MODULE)
  58. int DH_KDF_X9_42(unsigned char *out, size_t outlen,
  59. const unsigned char *Z, size_t Zlen,
  60. ASN1_OBJECT *key_oid,
  61. const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
  62. {
  63. char key_alg[OSSL_MAX_NAME_SIZE];
  64. const OSSL_PROVIDER *prov = EVP_MD_get0_provider(md);
  65. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  66. if (!OBJ_obj2txt(key_alg, sizeof(key_alg), key_oid, 0))
  67. return 0;
  68. return ossl_dh_kdf_X9_42_asn1(out, outlen, Z, Zlen, key_alg,
  69. ukm, ukmlen, md, libctx, NULL);
  70. }
  71. #endif /* !defined(FIPS_MODULE) */