ecdh_kdf.c 2.1 KB

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