143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
# hkdf.py
|
|
#
|
|
# Copyright (C) 2006-2026 wolfSSL Inc.
|
|
#
|
|
# This file is part of wolfSSL.
|
|
#
|
|
# wolfSSL is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# wolfSSL is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
|
|
|
# pylint: disable=no-member,no-name-in-module
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from wolfcrypt._ffi import ffi as _ffi
|
|
from wolfcrypt._ffi import lib as _lib
|
|
|
|
from wolfcrypt.exceptions import WolfCryptApiError
|
|
from wolfcrypt.utils import t2b
|
|
|
|
if TYPE_CHECKING:
|
|
if _lib.HMAC_ENABLED:
|
|
from wolfcrypt.hashes import _Hmac
|
|
|
|
|
|
if _lib.HKDF_ENABLED:
|
|
|
|
def HKDF(hash_cls: type[_Hmac], in_key: bytes | str, salt: bytes | str | None = None, info: bytes | str | None = None, out_len: int | None = None) -> bytes:
|
|
"""
|
|
Perform HKDF Extract-and-Expand in one call (wraps wc_HKDF).
|
|
|
|
Parameters:
|
|
- hash_cls: HMAC class, e.g. HmacSha256, see `wolfcrypt.hashes`.
|
|
- in_key: input key material (IKM) as bytes or str.
|
|
- salt: optional salt value (bytes or str). If None, treated as empty.
|
|
- info: optional context/application info (bytes or str). If None,
|
|
treated as empty.
|
|
- out_len: desired length of output keying material (bytes). If None,
|
|
defaults to the digest size of the hash.
|
|
|
|
Returns:
|
|
- bytes object containing the derived key of length `out_len`.
|
|
|
|
Raises:
|
|
- WolfCryptApiError on failure.
|
|
- ValueError for invalid arguments.
|
|
"""
|
|
in_key = t2b(in_key)
|
|
salt = b"" if salt is None else t2b(salt)
|
|
info = b"" if info is None else t2b(info)
|
|
|
|
if out_len is None:
|
|
out_len = hash_cls.digest_size
|
|
assert out_len is not None
|
|
|
|
out = _ffi.new(f"byte[{out_len}]")
|
|
ret = _lib.wc_HKDF(
|
|
hash_cls._type,
|
|
in_key,
|
|
len(in_key),
|
|
salt,
|
|
len(salt),
|
|
info,
|
|
len(info),
|
|
out,
|
|
out_len,
|
|
)
|
|
if ret != 0:
|
|
raise WolfCryptApiError("HKDF error", ret)
|
|
|
|
return _ffi.buffer(out, out_len)[:]
|
|
|
|
def HKDF_Extract(hash_cls: type[_Hmac], salt: bytes | str | None, in_key: bytes | str) -> bytes:
|
|
"""
|
|
HKDF-Extract: PRK = HMAC-Hash(salt, IKM)
|
|
Wraps wc_HKDF_Extract.
|
|
|
|
Parameters:
|
|
- hash_cls: HMAC class, e.g. HmacSha256, see `wolfcrypt.hashes`.
|
|
- salt: bytes/str (can be None -> treated as empty).
|
|
- in_key: input key material (IKM) as bytes/str.
|
|
|
|
Returns:
|
|
- PRK as bytes (length == hash digest size).
|
|
|
|
Raises WolfCryptApiError on failure.
|
|
"""
|
|
salt = b"" if salt is None else t2b(salt)
|
|
in_key = t2b(in_key)
|
|
|
|
out_len = hash_cls.digest_size
|
|
out = _ffi.new(f"byte[{out_len}]")
|
|
|
|
ret = _lib.wc_HKDF_Extract(hash_cls._type, salt, len(salt), in_key, len(in_key), out)
|
|
if ret != 0:
|
|
raise WolfCryptApiError("HKDF_Extract error", ret)
|
|
|
|
return _ffi.buffer(out, out_len)[:]
|
|
|
|
def HKDF_Expand(hash_cls: type[_Hmac], prk: bytes | str, info: bytes | str | None, out_len: int) -> bytes:
|
|
"""
|
|
HKDF-Expand: OKM = HKDF-Expand(PRK, info, L)
|
|
Wraps wc_HKDF_Expand.
|
|
|
|
Parameters:
|
|
- hash_cls: HMAC class, e.g. HmacSha256, see `wolfcrypt.hashes`.
|
|
- prk: pseudorandom key (output from HKDF-Extract) as bytes/str.
|
|
- info: optional context/application info (bytes/str). If None, treated as empty.
|
|
- out_len: length of output keying material in bytes.
|
|
|
|
Returns:
|
|
- OKM as bytes of length `out_len`.
|
|
|
|
Raises WolfCryptApiError on failure.
|
|
"""
|
|
prk = t2b(prk)
|
|
info = b"" if info is None else t2b(info)
|
|
|
|
if out_len is None or out_len <= 0:
|
|
raise ValueError("out_len must be a positive integer")
|
|
|
|
out = _ffi.new(f"byte[{out_len}]")
|
|
|
|
ret = _lib.wc_HKDF_Expand(
|
|
hash_cls._type, prk, len(prk), info, len(info), out, out_len
|
|
)
|
|
if ret != 0:
|
|
raise WolfCryptApiError("HKDF_Expand error", ret)
|
|
|
|
return _ffi.buffer(out, out_len)[:]
|