2
0

dh_kdf.c 2.8 KB

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