Microchip® Advanced Software Framework

wilc/drv_hash/crypto.h File Reference

APIs and data types for cryptographic algorithms.

Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.

#include "driver/include/m2m_types.h"

Data Structures

struct  tstrBuffer
 
struct  tstrHashContext
 
struct  tstrRSAPublicKey
 RSA Public Key Definition. More...
 

Macros

#define AES_BLOCK_SIZE   (16)
 
#define BI_FAIL   (-1)
 
#define BI_SUCCESS   (0)
 
#define BITS_PER_DIGIT   ((uint32)32)
 
#define GETU16(BUF, OFFSET)   ((((uint16)((BUF)[OFFSET]) << 8)) |(((uint16)(BUF)[OFFSET + 1])))
 
#define GETU32(BUF, OFFSET)
 
#define M2M_MEMCPY   m2m_memcpy
 
#define M2M_MEMSET   m2m_memset
 
#define MD4_DIGEST_SIZE   (16)
 
#define MD5_DIGEST_SIZE   (16)
 
#define MD5_FINISH(ctxt, digest)   MD5_Hash((ctxt), SHA_FLAGS_FINISH, NULL, 0, digest)
 
#define MD5_INIT(ctxt)   MD5_Hash((ctxt), SHA_FLAGS_INIT, NULL, 0, NULL)
 
#define MD5_UPDATE(ctxt, data, dataLen)   MD5_Hash((ctxt), SHA_FLAGS_UPDATE, (data), (dataLen), NULL)
 
#define PUTU16(VAL16, BUF, OFFSET)
 
#define PUTU32(VAL32, BUF, OFFSET)
 
#define SHA1_DIGEST_SIZE   (20)
 
#define SHA1_FINISH(ctxt, digest)   SHA1_Hash((ctxt), SHA_FLAGS_FINISH, NULL, 0, digest)
 
#define SHA1_INIT(ctxt)   SHA1_Hash((ctxt), SHA_FLAGS_INIT, NULL, 0, NULL)
 
#define SHA1_UPDATE(ctxt, data, dataLen)   SHA1_Hash((ctxt), SHA_FLAGS_UPDATE, (data), (dataLen), NULL)
 
#define SHA256_DIGEST_SIZE   (32)
 
#define SHA_BLOCK_SIZE   (64)
 
#define SHA_FLAGS_FINISH   0x04
 
#define SHA_FLAGS_FULL_HASH   (SHA_FLAGS_INIT | SHA_FLAGS_UPDATE | SHA_FLAGS_FINISH)
 
#define SHA_FLAGS_INIT   0x01
 
#define SHA_FLAGS_UPDATE   0x02
 
#define SHA_MAX_DIGEST_SIZE   SHA256_DIGEST_SIZE
 
#define SHA_ROTL(x, n)   (SHA_SHL(x,n) | SHA_SHR(x , (32 - n)))
 
#define SHA_ROTR(x, n)   (SHA_SHR(x,n) | SHA_SHL(x , (32 - n)))
 
#define SHA_SHL(x, n)   ((x & 0xFFFFFFFF) << n)
 
#define SHA_SHR(x, n)   ((x & 0xFFFFFFFF) >> n)
 

Typedefs

typedef void(* tpfHash )(tstrHashContext *pstrHashContext, uint8 u8Flags, uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Digest)
 
typedef void(* tpfHashProcessBlock )(uint32 *pu32HashState, const uint8 *pu8MessageBlock)
 
typedef tstrHashContext tstrMd5Context
 
typedef tstrHashContext tstrSha1Context
 
typedef tstrHashContext tstrSha256Context
 

Functions

void AES_CbcDec (uint8 *pu8IV, uint8 *pu8Data, uint16 u16DataSize, uint8 *pu8Key, uint8 u8KeySize)
 Decrypts a stream of data (in place) using AES algorithm in the Cipher Block Chain (CBC) mode. More...
 
void AES_CbcEnc (uint8 *pu8IV, uint8 *pu8Data, uint16 u16DataSize, uint8 *pu8Key, uint8 u8KeySize)
 Encrypts a stream of data (in place) using AES algorithm in the Cipher Block Chain (CBC) mode. More...
 
sint32 BI_ModExp (uint8 *pu8X, uint32 u32XSize, uint8 *pu8E, uint32 u32ESize, uint8 *pu8N, uint32 u32NSize, uint8 *pu8Y, uint32 u32YSize)
 Computes the modular exponentiation of big integers for the expression: Result = (Base ^ Power) % Mod. More...
 
void DES_EncryptBlock (uint8 *pu8PlainTxt, uint8 *pu8CipherKey, uint8 *pu8CipherTxt)
 
void DRV_hmac_sha1 (uint8 *pu8Key, uint32 u32KeyLength, uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Digest)
 
void DRV_HMAC_Vector (tpfHash fpHash, uint8 *pu8Key, uint32 u32KeyLength, tstrBuffer *pstrInData, uint8 u8NumInputs, uint8 *pu8Out)
 Perform the HMAC operation on a given vector of data streams using the hash function passed to the DRV_HMAC_Vector. More...
 
void DRV_SHA1_Hash (tstrHashContext *pstrSha1Context, uint8 u8Flags, uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Digest)
 
void hmac_md5 (uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Key, uint32 u32KeyLength, uint8 *pu8Digest)
 
void HMAC_SHA256 (uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Key, uint32 u32KeyLength, uint8 *pu8Out)
 
void MD4_Hash (uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Digest)
 
void MD5_Hash (tstrHashContext *pstrMD5Context, uint8 u8Flags, uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Digest)
 
uint8 pbkdf2_sha1 (uint8 *key, uint8 key_len, uint8 *data, uint8 data_len, uint8 *digest)
 This function runs the PBKDF2 on the data with a given key. More...
 
uint16 RSA_Encrypt (tstrRSAPublicKey *pstrKey, uint8 *pu8Plain, uint16 u16PlainSize, uint8 *pu8Cipher)
 
sint8 RSA_SignVerify (tstrRSAPublicKey *pstrKey, uint8 *pu8Signature, uint16 u16SignLen, uint8 *pu8Hash, uint16 u16HashLength)
 
void SHA256_Hash (tstrHashContext *pstrSha256Context, uint8 u8Flags, uint8 *pu8Data, uint32 u32DataLength, uint8 *pu8Digest)
 

#define AES_BLOCK_SIZE   (16)
#define BI_FAIL   (-1)
#define BI_SUCCESS   (0)
#define BITS_PER_DIGIT   ((uint32)32)
#define GETU16 (   BUF,
  OFFSET 
)    ((((uint16)((BUF)[OFFSET]) << 8)) |(((uint16)(BUF)[OFFSET + 1])))

Retrieve 2 bytes from the given buffer at the given offset as 16-bit unsigned integer in the Network byte order.

#define GETU32 (   BUF,
  OFFSET 
)
Value:
((((uint32)((BUF)[OFFSET]) << 24)) | (((uint32)((BUF)[OFFSET + 1]) << 16)) | \
(((uint32)((BUF)[OFFSET + 2]) << 8)) | ((uint32)((BUF)[OFFSET + 3])))
unsigned long uint32
Range of values between 0 to 4294967295.
Definition: wilc/bsp/include/nm_bsp.h:116

Retrieve 4 bytes from the given buffer at the given offset as 32-bit unsigned integer in the Network byte order.

Referenced by SHA1ProcessBlock().

#define MD4_DIGEST_SIZE   (16)

Referenced by MD4_Hash().

#define MD5_DIGEST_SIZE   (16)

Referenced by MD5_HashSW().

#define MD5_FINISH (   ctxt,
  digest 
)    MD5_Hash((ctxt), SHA_FLAGS_FINISH, NULL, 0, digest)
#define MD5_INIT (   ctxt)    MD5_Hash((ctxt), SHA_FLAGS_INIT, NULL, 0, NULL)
#define MD5_UPDATE (   ctxt,
  data,
  dataLen 
)    MD5_Hash((ctxt), SHA_FLAGS_UPDATE, (data), (dataLen), NULL)
#define PUTU16 (   VAL16,
  BUF,
  OFFSET 
)
Value:
do \
{ \
(BUF)[OFFSET ] = BYTE_1((VAL16)); \
(BUF)[OFFSET +1 ] = BYTE_0((VAL16)); \
}while(0)
#define BYTE_0(word)
Definition: wilc/common/include/nm_common.h:204
#define BYTE_1(word)
Definition: wilc/common/include/nm_common.h:205
#define PUTU32 (   VAL32,
  BUF,
  OFFSET 
)
Value:
do \
{ \
(BUF)[OFFSET ] = BYTE_3((VAL32)); \
(BUF)[OFFSET +1 ] = BYTE_2((VAL32)); \
(BUF)[OFFSET +2 ] = BYTE_1((VAL32)); \
(BUF)[OFFSET +3 ] = BYTE_0((VAL32)); \
}while(0)
#define BYTE_0(word)
Definition: wilc/common/include/nm_common.h:204
#define BYTE_2(word)
Definition: wilc/common/include/nm_common.h:206
#define BYTE_1(word)
Definition: wilc/common/include/nm_common.h:205
#define BYTE_3(word)
Definition: wilc/common/include/nm_common.h:207

Referenced by DRV_SHA1_Hash(), SHA1_HashSW(), SHA224_HashSW(), SHA256_HashSW(), and SHA512_HashSW().

#define SHA1_DIGEST_SIZE   (20)

Referenced by DRV_SHA1_Hash(), and SHA1_HashSW().

#define SHA1_FINISH (   ctxt,
  digest 
)    SHA1_Hash((ctxt), SHA_FLAGS_FINISH, NULL, 0, digest)

Referenced by Cert_DecodeX520Name().

#define SHA1_INIT (   ctxt)    SHA1_Hash((ctxt), SHA_FLAGS_INIT, NULL, 0, NULL)

Referenced by Cert_DecodeX520Name().

#define SHA1_UPDATE (   ctxt,
  data,
  dataLen 
)    SHA1_Hash((ctxt), SHA_FLAGS_UPDATE, (data), (dataLen), NULL)

Referenced by Cert_DecodeX520Name().

#define SHA256_DIGEST_SIZE   (32)

Referenced by SHA256_HashSW().

#define SHA_BLOCK_SIZE   (64)
#define SHA_FLAGS_FINISH   0x04
#define SHA_FLAGS_FULL_HASH   (SHA_FLAGS_INIT | SHA_FLAGS_UPDATE | SHA_FLAGS_FINISH)

Perform Full hash operation with the DIGEST returned to the caller.

Referenced by DRV_HMAC_Vector(), and HMAC_Vector().

#define SHA_FLAGS_INIT   0x01

A flag to tell the hash function to start hash from the initial state.

Referenced by Cert_ComputeTBSCertHash(), DRV_HMAC_Vector(), DRV_SHA1_Hash(), HMAC_Vector(), MD5_HashSW(), SHA1_HashSW(), SHA224_HashSW(), SHA256_HashSW(), and SHA512_HashSW().

#define SHA_FLAGS_UPDATE   0x02
#define SHA_MAX_DIGEST_SIZE   SHA256_DIGEST_SIZE

Referenced by DRV_HMAC_Vector(), and HMAC_Vector().

#define SHA_ROTL (   x,
 
)    (SHA_SHL(x,n) | SHA_SHR(x , (32 - n)))

Referenced by SHA1ProcessBlock().

#define SHA_ROTR (   x,
 
)    (SHA_SHR(x,n) | SHA_SHL(x , (32 - n)))
#define SHA_SHL (   x,
 
)    ((x & 0xFFFFFFFF) << n)
#define SHA_SHR (   x,
 
)    ((x & 0xFFFFFFFF) >> n)

tpfHash
tpfHashProcessBlock

void AES_CbcDec ( uint8 pu8IV,
uint8 pu8Data,
uint16  u16DataSize,
uint8 pu8Key,
uint8  u8KeySize 
)

Decrypts a stream of data (in place) using AES algorithm in the Cipher Block Chain (CBC) mode.

Parameters
[in]pu8IVInitialization vector.
[in/out]pu8Data Data to be decrypted.
[in]u16DataSizeSize of data in bytes.
[in]pu8KeyDecryption Key.
[in]u8KeySizeSize of the input key in bytes.
Author
Ahmed Ezzat
Date
11 Nov 2012
Version
1.0
void AES_CbcEnc ( uint8 pu8IV,
uint8 pu8Data,
uint16  u16DataSize,
uint8 pu8Key,
uint8  u8KeySize 
)

Encrypts a stream of data (in place) using AES algorithm in the Cipher Block Chain (CBC) mode.

Parameters
[in]pu8IVInitialization vector.
[in/out]pu8Data Data to be encrypted.
[in]u16DataSizeSize of data in bytes.
[in]pu8KeyEncryption Key.
[in]u8KeySizeSize of the input key in bytes.
Author
Ahmed Ezzat
Date
11 Nov 2012
Version
1.0
sint32 BI_ModExp ( uint8 pu8X,
uint32  u32XSize,
uint8 pu8E,
uint32  u32ESize,
uint8 pu8N,
uint32  u32NSize,
uint8 pu8Y,
uint32  u32YSize 
)

Computes the modular exponentiation of big integers for the expression: Result = (Base ^ Power) % Mod.

Parameters
[in]pu32BaseA big integer representing the base digit..
[in]u32BaseSizeSize of the base digit in 32-bit words.
[in]pu32PowerA big integer representing the exponent digit..
[in]u32PowerSizeSize of the exponent digit in 32-bit words.
[in]pu32ModA big integer representing the modulo digit..
[in]u32ModSizeSize of the modulo digit in 32-bit words.
[out]pu32ResultThe resultant big integer.
[in]u32ResultSizeSize of the result digit in 32-bit words.
Author
Ahmed Ezzat
Date
27 Nov 2012
void DES_EncryptBlock ( uint8 pu8PlainTxt,
uint8 pu8CipherKey,
uint8 pu8CipherTxt 
)
void DRV_hmac_sha1 ( uint8 pu8Key,
uint32  u32KeyLength,
uint8 pu8Data,
uint32  u32DataLength,
uint8 pu8Digest 
)
void DRV_HMAC_Vector ( tpfHash  fpHash,
uint8 pu8Key,
uint32  u32KeyLength,
tstrBuffer pstrInData,
uint8  u8NumInputs,
uint8 pu8Out 
)

Perform the HMAC operation on a given vector of data streams using the hash function passed to the DRV_HMAC_Vector.

References M2M_MEMSET, NULL, SHA_BLOCK_SIZE, SHA_FLAGS_FINISH, SHA_FLAGS_FULL_HASH, SHA_FLAGS_INIT, SHA_FLAGS_UPDATE, SHA_MAX_DIGEST_SIZE, and tstrHashContext::u8DigestSize.

Referenced by DRV_hmac_sha1().

void hmac_md5 ( uint8 pu8Data,
uint32  u32DataLength,
uint8 pu8Key,
uint32  u32KeyLength,
uint8 pu8Digest 
)
void HMAC_SHA256 ( uint8 pu8Data,
uint32  u32DataLength,
uint8 pu8Key,
uint32  u32KeyLength,
uint8 pu8Out 
)
void MD4_Hash ( uint8 pu8Data,
uint32  u32DataLength,
uint8 pu8Digest 
)
void MD5_Hash ( tstrHashContext pstrMD5Context,
uint8  u8Flags,
uint8 pu8Data,
uint32  u32DataLength,
uint8 pu8Digest 
)
uint8 pbkdf2_sha1 ( uint8 key,
uint8  key_len,
uint8 data,
uint8  data_len,
uint8 digest 
)

This function runs the PBKDF2 on the data with a given key.

To obtain the PSK from the password, PBKDF2 is run on SSID (data) with password (key).

References A_SHA_DIGEST_LEN, and pbkdf2_sha1_f().

Referenced by m2m_wifi_connect(), and m2m_wifi_enable_ap().

uint16 RSA_Encrypt ( tstrRSAPublicKey pstrKey,
uint8 pu8Plain,
uint16  u16PlainSize,
uint8 pu8Cipher 
)
sint8 RSA_SignVerify ( tstrRSAPublicKey pstrKey,
uint8 pu8Signature,
uint16  u16SignLen,
uint8 pu8Hash,
uint16  u16HashLength 
)
void SHA256_Hash ( tstrHashContext pstrSha256Context,
uint8  u8Flags,
uint8 pu8Data,
uint32  u32DataLength,
uint8 pu8Digest 
)