Skip to content

wolfCrypt Init and Cleanup

Functions

Name
int wc_HashGetOID(enum wc_HashType hash_type)
This function will return the OID for the wc_HashType provided.
int wc_HashGetDigestSize(enum wc_HashType hash_type)
This function returns the size of the digest (output) for a hash_type. The returns size is used to make sure the output buffer provided to wc_Hash is large enough.
int wc_Hash(enum wc_HashType hash_type, const byte * data, word32 data_len, byte * hash, word32 hash_len)
This function performs a hash on the provided data buffer and returns it in the hash buffer provided.
int wc_UnpackOctets(byte * out, word32 outSz, const byte * in, word32 inSz, word32 octetSz)
Expands a packed octet stream into the one_octet_per_byte_cell form every wolfCrypt byte* API expects. Declared only when WOLFSSL_WIDE_BYTE is set (CHAR_BIT != 8); elsewhere the two layouts coincide and no conversion is needed.
int wc_PackOctets(byte * out, word32 outSz, const byte * in, word32 inSz, word32 octetSz)
Packs a one_octet_per_byte_cell buffer into an octet stream, for storing to flash or handing to a byte_oriented peripheral. Inverse of wc_UnpackOctets(); requires WOLFSSL_WIDE_BYTE.
int wolfCrypt_Init(void )
Used to initialize resources used by wolfCrypt.
int wolfCrypt_Cleanup(void )
Used to clean up resources used by wolfCrypt.

Functions Documentation

function wc_HashGetOID

int wc_HashGetOID(
    enum wc_HashType hash_type
)

This function will return the OID for the wc_HashType provided.

Parameters:

  • hash_type A hash type from the “enum wc_HashType” such as “WC_HASH_TYPE_SHA256”.

See:

Return:

  • OID returns value greater than 0
  • HASH_TYPE_E hash type not supported.
  • BAD_FUNC_ARG one of the provided arguments is incorrect.

Example

enum wc_HashType hash_type = WC_HASH_TYPE_SHA256;
int oid = wc_HashGetOID(hash_type);
if (oid > 0) {
    // Success
}

function wc_HashGetDigestSize

int wc_HashGetDigestSize(
    enum wc_HashType hash_type
)

This function returns the size of the digest (output) for a hash_type. The returns size is used to make sure the output buffer provided to wc_Hash is large enough.

Parameters:

  • hash_type A hash type from the “enum wc_HashType” such as “WC_HASH_TYPE_SHA256”.

See: wc_Hash

Return:

  • Success A positive return value indicates the digest size for the hash.
  • Error Returns HASH_TYPE_E if hash_type is not supported.
  • Failure Returns BAD_FUNC_ARG if an invalid hash_type was used.

Example

int hash_len = wc_HashGetDigestSize(hash_type);
if (hash_len <= 0) {
WOLFSSL_MSG("Invalid hash type/len");
return BAD_FUNC_ARG;
}

function wc_Hash

int wc_Hash(
    enum wc_HashType hash_type,
    const byte * data,
    word32 data_len,
    byte * hash,
    word32 hash_len
)

This function performs a hash on the provided data buffer and returns it in the hash buffer provided.

Parameters:

  • hash_type A hash type from the “enum wc_HashType” such as “WC_HASH_TYPE_SHA256”.
  • data Pointer to buffer containing the data to hash.
  • data_len Length of the data buffer.
  • hash Pointer to buffer used to output the final hash to.
  • hash_len Length of the hash buffer.

See: wc_HashGetDigestSize

Return: 0 Success, else error (such as BAD_FUNC_ARG or BUFFER_E).

Example

enum wc_HashType hash_type = WC_HASH_TYPE_SHA256;
int hash_len = wc_HashGetDigestSize(hash_type);
if (hash_len > 0) {
    int ret = wc_Hash(hash_type, data, data_len, hash_data, hash_len);
    if(ret == 0) {
        // Success
    }
}

function wc_UnpackOctets

int wc_UnpackOctets(
    byte * out,
    word32 outSz,
    const byte * in,
    word32 inSz,
    word32 octetSz
)

Expands a packed octet stream into the one-octet-per-byte-cell form every wolfCrypt byte* API expects. Declared only when WOLFSSL_WIDE_BYTE is set (CHAR_BIT != 8); elsewhere the two layouts coincide and no conversion is needed.

Parameters:

  • out Destination, one octet per cell
  • outSz Size of out in byte cells
  • in Packed source, WC_OCTETS_PER_BYTE octets per cell, low octet first, and must not overlap out
  • inSz Size of in in byte cells; must be at least WC_PACKED_CELLS(octetSz)
  • octetSz Number of octets to expand

See: wc_PackOctets

Return:

  • 0 on success
  • BAD_FUNC_ARG when out or in is NULL
  • BUFFER_E when out is smaller than octetSz, or in smaller than WC_PACKED_CELLS(octetSz)

Example

byte sig[WC_MLDSA_65_SIG_SIZE];
int ret = wc_UnpackOctets(sig, (word32)sizeof(sig), sigPacked,
                          (word32)sizeof(sigPacked),
                          WC_MLDSA_65_SIG_SIZE);

function wc_PackOctets

int wc_PackOctets(
    byte * out,
    word32 outSz,
    const byte * in,
    word32 inSz,
    word32 octetSz
)

Packs a one-octet-per-byte-cell buffer into an octet stream, for storing to flash or handing to a byte-oriented peripheral. Inverse of wc_UnpackOctets(); requires WOLFSSL_WIDE_BYTE.

Parameters:

  • out Destination for the packed stream
  • outSz Size of out in byte cells
  • in Source, one octet per cell, and must not overlap out
  • inSz Size of in in byte cells; must be at least octetSz
  • octetSz Number of octets to pack

See: wc_UnpackOctets

Return:

  • 0 on success
  • BAD_FUNC_ARG when out or in is NULL
  • BUFFER_E when out is smaller than WC_PACKED_CELLS(octetSz), or in smaller than octetSz

Example

byte packed[WC_PACKED_CELLS(sizeof(sig))];
int ret = wc_PackOctets(packed, (word32)sizeof(packed), sig,
                        (word32)sizeof(sig), (word32)sizeof(sig));

function wolfCrypt_Init

int wolfCrypt_Init(
    void 
)

Used to initialize resources used by wolfCrypt.

Parameters:

  • none No parameters.

See: wolfCrypt_Cleanup

Return:

  • 0 upon success.
  • <0 upon failure of init resources.

Example

...
if (wolfCrypt_Init() != 0) {
    WOLFSSL_MSG("Error with wolfCrypt_Init call");
}

function wolfCrypt_Cleanup

int wolfCrypt_Cleanup(
    void 
)

Used to clean up resources used by wolfCrypt.

Parameters:

  • none No parameters.

See: wolfCrypt_Init

Return:

  • 0 upon success.
  • <0 upon failure of cleaning up resources.

Example

...
if (wolfCrypt_Cleanup() != 0) {
    WOLFSSL_MSG("Error with wolfCrypt_Cleanup call");
}

Updated on 2026-09-17 at 01:17:40 +0000