CryptoSys PKI Pro Manual

PRF_Bytes

Generate output bytes using a pseudorandom function (PRF).

VBA/VB6 Syntax

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

C/C++ Syntax

long __stdcall PRF_Bytes(unsigned char *lpOutput, long nOutBytes, const void *lpMessage, long nMsgLen, const void *lpKey, long nKeyLen, const char *szCustom, long nOptions);

Parameters

lpOutput
[out] byte buffer to receive the output.
nOutBytes
[in] size of output buffer in bytes.
lpMessage
[in] byte array containing the input data.
nMsgLen
[in] length of the input data in bytes.
lpKey
[in] byte array containing the key.
nKeyLen
[in] length of the key in bytes.
szCustom
[in] customization string (optional).
nOptions
[in] Option flags. Select one of:
PKI_KMAC_128 to use KMAC128 (KMAC with SHAKE128)
PKI_KMAC_256 to use KMAC256 (KMAC with SHAKE256).

Returns (VBA/C)

If successful, the return value is the number of bytes in the output; otherwise it returns a negative error code.

VBA Wrapper Syntax

Public Function prfBytes (nBytes As Long, lpMessage() As Byte, lpKey() As Byte, nOptions As Long, Optional szCustom As String = "") As Byte()

.NET Equivalent

Prf.Bytes Method

C++ (STL) Equivalent

static bvec_t dipki::Prf::Bytes (int numBytes, const bvec_t &message, const bvec_t &key, PrfAlg prfalg, const std::string &customStr="")

Python Equivalent

static Prf.bytes(numbytes, msg, key, prfalg, customstring="")

Remarks

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 "".

Example (VBA core function)

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

Example (VBA wrapper function)

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"

See Also

XOF_Bytes

[Contents] [Index]

[PREV: PKI_Version...]   [Contents]   [Index]   
   [NEXT: PWD_Prompt...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.