kdf_blake2b.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <errno.h>
  2. #include "crypto_kdf_blake2b.h"
  3. #include "crypto_generichash_blake2b.h"
  4. #include "private/common.h"
  5. size_t
  6. crypto_kdf_blake2b_bytes_min(void)
  7. {
  8. return crypto_kdf_blake2b_BYTES_MIN;
  9. }
  10. size_t
  11. crypto_kdf_blake2b_bytes_max(void)
  12. {
  13. return crypto_kdf_blake2b_BYTES_MAX;
  14. }
  15. size_t
  16. crypto_kdf_blake2b_contextbytes(void)
  17. {
  18. return crypto_kdf_blake2b_CONTEXTBYTES;
  19. }
  20. size_t
  21. crypto_kdf_blake2b_keybytes(void)
  22. {
  23. return crypto_kdf_blake2b_KEYBYTES;
  24. }
  25. int crypto_kdf_blake2b_derive_from_key(unsigned char *subkey, size_t subkey_len,
  26. uint64_t subkey_id,
  27. const char ctx[crypto_kdf_blake2b_CONTEXTBYTES],
  28. const unsigned char key[crypto_kdf_blake2b_KEYBYTES])
  29. {
  30. unsigned char ctx_padded[crypto_generichash_blake2b_PERSONALBYTES];
  31. unsigned char salt[crypto_generichash_blake2b_SALTBYTES];
  32. memcpy(ctx_padded, ctx, crypto_kdf_blake2b_CONTEXTBYTES);
  33. memset(ctx_padded + crypto_kdf_blake2b_CONTEXTBYTES, 0, sizeof ctx_padded - crypto_kdf_blake2b_CONTEXTBYTES);
  34. STORE64_LE(salt, subkey_id);
  35. memset(salt + 8, 0, (sizeof salt) - 8);
  36. if (subkey_len < crypto_kdf_blake2b_BYTES_MIN ||
  37. subkey_len > crypto_kdf_blake2b_BYTES_MAX) {
  38. errno = EINVAL;
  39. return -1;
  40. }
  41. return crypto_generichash_blake2b_salt_personal(subkey, subkey_len,
  42. NULL, 0,
  43. key, crypto_kdf_blake2b_KEYBYTES,
  44. salt, ctx_padded);
  45. }