ecdh_kdf.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2015-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. * ECDH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/ec.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/kdf.h>
  19. #include "ec_local.h"
  20. /* Key derivation function from X9.63/SECG */
  21. int ossl_ecdh_kdf_X9_63(unsigned char *out, size_t outlen,
  22. const unsigned char *Z, size_t Zlen,
  23. const unsigned char *sinfo, size_t sinfolen,
  24. const EVP_MD *md,
  25. OSSL_LIB_CTX *libctx, const char *propq)
  26. {
  27. int ret = 0;
  28. EVP_KDF_CTX *kctx = NULL;
  29. OSSL_PARAM params[4], *p = params;
  30. const char *mdname = EVP_MD_get0_name(md);
  31. EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_X963KDF, propq);
  32. if ((kctx = EVP_KDF_CTX_new(kdf)) != NULL) {
  33. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  34. (char *)mdname, 0);
  35. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  36. (void *)Z, Zlen);
  37. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
  38. (void *)sinfo, sinfolen);
  39. *p = OSSL_PARAM_construct_end();
  40. ret = EVP_KDF_derive(kctx, out, outlen, params) > 0;
  41. EVP_KDF_CTX_free(kctx);
  42. }
  43. EVP_KDF_free(kdf);
  44. return ret;
  45. }
  46. /*-
  47. * The old name for ecdh_KDF_X9_63
  48. * Retained for ABI compatibility
  49. */
  50. #ifndef OPENSSL_NO_DEPRECATED_3_0
  51. int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
  52. const unsigned char *Z, size_t Zlen,
  53. const unsigned char *sinfo, size_t sinfolen,
  54. const EVP_MD *md)
  55. {
  56. return ossl_ecdh_kdf_X9_63(out, outlen, Z, Zlen, sinfo, sinfolen, md, NULL,
  57. NULL);
  58. }
  59. #endif