Exportable version of WizziLab's modem driver.

Dependents:   modem_ref_helper

include/kal_crypto.h

Committer:
Jeej
Date:
2021-02-19
Revision:
60:08efaaca0e83
Parent:
56:67e3d9608403

File content as of revision 60:08efaaca0e83:

/// @copyright
/// ========================================================================={{{
/// Copyright (c) 20XX                                                         /
/// All rights reserved                                                        /
///                                                                            /
/// IMPORTANT: This Software may not be modified, copied or distributed unless /
/// embedded on a WizziLab product. Other than for the foregoing purpose, this /
/// Software and/or its documentation may not be used, reproduced, copied,     /
/// prepared derivative works of, modified, performed, distributed, displayed  /
/// or sold for any purpose. For the sole purpose of embedding this Software   /
/// on a WizziLab product, copy, modification and distribution of this         /
/// Software is granted provided that the following conditions are respected:  /
///                                                                            /
/// *  Redistributions of source code must retain the above copyright notice,  /
///    this list of conditions and the following disclaimer                    /
///                                                                            /
/// *  Redistributions in binary form must reproduce the above copyright       /
///    notice, this list of conditions and the following disclaimer in the     /
///    documentation and/or other materials provided with the distribution.    /
///                                                                            /
/// *  The name of WizziLab can not be used to endorse or promote products     /
///    derived from this software without specific prior written permission.   /
///                                                                            /
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS        /
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  /
/// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR /
/// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR          /
/// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      /
/// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        /
/// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,            /
/// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     /
/// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING    /
/// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS         /
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.               /
/// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,       /
/// ENHANCEMENTS OR MODIFICATIONS.                                             /
///                                                                            /
/// Should you have any questions regarding your right to use this Software,   /
/// contact WizziLab at www.wizzilab.com.                                      /
///                                                                            /
/// =========================================================================}}}
/// @endcopyright

//  =======================================================================
/// @file           kal_crypto.h
/// @brief          KAL Crypto utilities
//  =======================================================================

#ifndef _KAL_CRYPTO_H_
#define _KAL_CRYPTO_H_

#include "hal_types.h"

// ======================================================================
//
//
//                        SHA-2 256 Tool-suite. 
//          (From Brad Conte's Licence-free implementation)
//
//
// ======================================================================

/// SHA256 digest output size in bytes
#define SHA256_BLOCK_SIZE 32 

//======================================================================
// kal_sha256_init
//----------------------------------------------------------------------
/// @brief Initialize an SHA Hash generation. To be called before any
///        other kal_sha256_update/final functions.
//======================================================================
_public void kal_sha256_init(void);

//======================================================================
// kal_sha256_update
//----------------------------------------------------------------------
/// @brief Used to 'push' new data into the hash calculation.
/// @param data pointer to the (char) data stream to hash.
/// @param len data stream length in bytes
//======================================================================
_public void kal_sha256_update(u8 data[], uint len);

//======================================================================
// kal_sha256_final
//----------------------------------------------------------------------
/// @brief To be called when all data has been pushed into the hash
///        generator. Finalize and outputs resulting SHA hash.
/// @param hash Pointer to the Output (char) buffer. Fills 32-bytes.
//======================================================================
_public void kal_sha256_final(u8 hash[]);

// ======================================================================
//
//
//                        CBC-MAC / CTR / CCM Tool-suite.
//
//
// ======================================================================

//======================================================================
// kal_aes128_init
//----------------------------------------------------------------------
/// @brief  Perform Key expansion
/// @param  key         const u8*               encryption key
/// @retval             void
//======================================================================
_public void kal_aes128_init(u8* key);

//======================================================================
// kal_aes128_ecb
//----------------------------------------------------------------------
/// @brief  Encrypt / decrypt in ECB mode (non chaining).
/// @param  p           u8*                     payload inout 
///                                             p(i) = cipher(p(i), key)
/// @param  len         u16                     payload length
/// @retval             void
//======================================================================
_public void kal_aes128_ecb(u8* p, u16 len);

//======================================================================
// kal_aes128_ctr
//----------------------------------------------------------------------
/// @brief  Encrypt / decrypt in CTR mode (non chaining).
///         Note : only 1 byte counter is supported.
/// @param  p           u8*                     payload inout 
///                                             p(i) = cipher(iv+i, key) ^ p(i)
/// @param  len         u16                     payload length
/// @param  iv          u8*                     counter IV (returns next value) 
/// @param  be          u8                      counter endianness (byte15 is lsb when TRUE, otherwise byte0)
/// @retval             void
//======================================================================
_public void kal_aes128_ctr(u8* p, u16 len, u8* iv, u8 be);

//======================================================================
// kal_aes128_cbc_mac
//----------------------------------------------------------------------
/// @brief  Generate MAC with block-chaining
/// @param  mac         u8*                     MAC 16-bytes inout
/// @param  p           u8*                     payload
/// @param  len         u16                      payload length
/// @retval             void
//======================================================================
_public void kal_aes128_cbc_mac(u8* mac, u8* p, u16 len);

//======================================================================
// kal_aes128_cmac_next
//----------------------------------------------------------------------
/// @brief  Execute CMAC algorithm on a entire number of 16-byte blocks, 
///         not including the last block.
/// @param  mac         u8*                     MAC 16-bytes inout
/// @param  p           u8*                     payload
/// @param  len         u16                     payload length
/// @retval             void
//======================================================================
_public void kal_aes128_cmac_next(u8* mac, u8* p, u16 len);

//======================================================================
// kal_aes128_cmac_last
//----------------------------------------------------------------------
/// @brief  Execute CMAC algorithm on any size and finalize.
/// @param  mac         u8*                     MAC 16-bytes inout
/// @param  p           u8*                     payload
/// @param  len         u16                     payload length
/// @retval             void
//======================================================================
_public void kal_aes128_cmac_last(u8* mac, u8* p, u16 len);

// ======================================================================
//
//
//                        SIPHASH-2-4
//
//
// ======================================================================

//======================================================================
// kal_siphash24
//----------------------------------------------------------------------
/// @brief  Generate MAC of an input vector using SIPHASH-2-4
/// @param  out         u8*                     64-bit result
/// @param  in          u8*                     input buffer
/// @param  len         u8                      input buffer length in bytes
/// @param  key         const u8*               cipher key
/// @retval             void
//======================================================================
_public void kal_siphash24(u8 *out, u8* v, u8 len, const u8* key);

#endif // _KAL_CRYPTO_H_