hkdf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 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. #include <stdio.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/kdf.h>
  13. #include <openssl/obj_mac.h>
  14. #include <openssl/params.h>
  15. /*
  16. * test vector from
  17. * https://datatracker.ietf.org/doc/html/rfc5869
  18. */
  19. static unsigned char hkdf_salt[] = {
  20. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  21. 0x0c
  22. };
  23. static unsigned char hkdf_ikm[] = {
  24. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  25. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
  26. };
  27. static unsigned char hkdf_info[] = {
  28. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9
  29. };
  30. /* Expected output keying material */
  31. static unsigned char hkdf_okm[] = {
  32. 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64,
  33. 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  34. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08,
  35. 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
  36. };
  37. int main(int argc, char **argv)
  38. {
  39. int rv = 1;
  40. EVP_KDF *kdf = NULL;
  41. EVP_KDF_CTX *kctx = NULL;
  42. unsigned char out[42];
  43. OSSL_PARAM params[5], *p = params;
  44. OSSL_LIB_CTX *library_context = NULL;
  45. library_context = OSSL_LIB_CTX_new();
  46. if (library_context == NULL) {
  47. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  48. goto end;
  49. }
  50. /* Fetch the key derivation function implementation */
  51. kdf = EVP_KDF_fetch(library_context, "HKDF", NULL);
  52. if (kdf == NULL) {
  53. fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
  54. goto end;
  55. }
  56. /* Create a context for the key derivation operation */
  57. kctx = EVP_KDF_CTX_new(kdf);
  58. if (kctx == NULL) {
  59. fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
  60. goto end;
  61. }
  62. /* Set the underlying hash function used to derive the key */
  63. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  64. "SHA256", 0);
  65. /* Set input keying material */
  66. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, hkdf_ikm,
  67. sizeof(hkdf_ikm));
  68. /* Set application specific information */
  69. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, hkdf_info,
  70. sizeof(hkdf_info));
  71. /* Set salt */
  72. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, hkdf_salt,
  73. sizeof(hkdf_salt));
  74. *p = OSSL_PARAM_construct_end();
  75. /* Derive the key */
  76. if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
  77. fprintf(stderr, "EVP_KDF_derive() failed\n");
  78. goto end;
  79. }
  80. if (CRYPTO_memcmp(hkdf_okm, out, sizeof(hkdf_okm)) != 0) {
  81. fprintf(stderr, "Generated key does not match expected value\n");
  82. goto end;
  83. }
  84. rv = 0;
  85. end:
  86. EVP_KDF_CTX_free(kctx);
  87. EVP_KDF_free(kdf);
  88. OSSL_LIB_CTX_free(library_context);
  89. return rv;
  90. }