Generate output bytes using a pseudorandom function (PRF).
Public Declare Function PRF_Bytes Lib "diCrPKI.dll" (ByRef lpOutput As Byte, ByVal nOutBytes As Long, ByRef lpMessage As Byte, ByVal nMsgLen As Long, ByRef lpKey As Byte, ByVal nKeyLen As Long, ByVal strCustom As String, ByVal nOptions As Long) As Long
nRet = PRF_Bytes(abOutput(0), nOutBytes, abMessage(0), nMsgLen, abKey(0), nKeyLen, strCustom, nOptions)
' Note the "(0)" after the byte array parameters
long __stdcall PRF_Bytes(unsigned char *lpOutput, long nOutBytes, const void *lpMessage, long nMsgLen, const void *lpKey, long nKeyLen, const char *szCustom, long nOptions);
If successful, the return value is the number of bytes in the output; otherwise it returns a negative error code.
Public Function prfBytes
(nBytes As Long, lpMessage() As Byte, lpKey() As Byte, nOptions As Long, Optional szCustom As String = "") As Byte()
static bvec_t dipki::Prf::Bytes (int numBytes, const bvec_t &message, const bvec_t &key, PrfAlg prfalg, const std::string &customStr="")
static Prf.bytes(numbytes, msg, key, prfalg, customstring="")
The output buffer lpOutput must exist. It will be filled with exactly nOutBytes bytes.
Note there is no zero option for nOptions: a valid option flag must be specified.
For KMAC, the default customization string szCustom is the empty string ""
.
Dim strKeyHex As String Dim strMsgHex As String Dim nOutBits As Long Dim nOutBytes As Long Dim abMAC() As Byte Dim abMsg() As Byte Dim abKey() As Byte Dim nMsgLen As Long Dim nKeyLen As Long Dim strCustom As String Dim strOK As String Dim nRet As Long ' Ref: `KMAC_samples.pdf` "Secure Hashing - KMAC-Samples" 2017-02-27 ' <https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/KMAC_samples.pdf> ' Sample #2 ' Input in hex form strKeyHex = "404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F" strMsgHex = "00010203" nOutBits = 256 strCustom = "My Tagged Application" strOK = "3B1FBA963CD8B0B59E8C1A6D71888B7143651AF8BA0A7070C0979E2811324AA5" ' Convert to byte array form abKey = cnvBytesFromHexStr(strKeyHex) abMsg = cnvBytesFromHexStr(strMsgHex) nKeyLen = UBound(abKey) + 1 nMsgLen = UBound(abMsg) + 1 ' Required output size nOutBytes = (nOutBits / 8) ReDim abMAC(nOutBytes - 1) nRet = PRF_Bytes(abMAC(0), nOutBytes, abMsg(0), nMsgLen, abKey(0), nKeyLen, strCustom, PKI_KMAC_128) If nRet > 0 Then Debug.Print "KMAC=" & cnvHexStrFromBytes(abMAC) Debug.Print "OK =" & strOK Else Debug.Print "Error code " & nRet End If
This should result in output as follows:
KMAC=3B1FBA963CD8B0B59E8C1A6D71888B7143651AF8BA0A7070C0979E2811324AA5 OK =3B1FBA963CD8B0B59E8C1A6D71888B7143651AF8BA0A7070C0979E2811324AA5
Dim lpPrf() As Byte Dim lpMsg() As Byte Dim lpKey() As Byte lpKey = cnvFromHex("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F") lpMsg = cnvFromHex("00010203") ' NB order of parameters (szCustom <=> nOptions). lpPrf = prfBytes(256 \ 8, lpMsg, lpKey, PKI_KMAC_128, "My Tagged Application") Debug.Print "KMAC=" & cnvToHex(lpPrf) Debug.Print "OK =3B1FBA963CD8B0B59E8C1A6D71888B7143651AF8BA0A7070C0979E2811324AA5"