/*1* ---------------------------------------------------------------------------2* OpenAES License3* ---------------------------------------------------------------------------4* Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are met:9*10* - Redistributions of source code must retain the above copyright notice,11* this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"17* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE20* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN24* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE26* POSSIBILITY OF SUCH DAMAGE.27* ---------------------------------------------------------------------------28*/2930#ifndef _OAES_LIB_H31#define _OAES_LIB_H3233#include <stdint.h>3435#ifdef __cplusplus36extern "C" {37#endif3839#ifdef _WIN3240# ifdef OAES_SHARED41# ifdef oaes_lib_EXPORTS42# define OAES_API __declspec(dllexport)43# else44# define OAES_API __declspec(dllimport)45# endif46# else47# define OAES_API48# endif49#else50# define OAES_API51#endif // WIN325253#define OAES_VERSION "0.8.1"54#define OAES_BLOCK_SIZE 165556typedef void OAES_CTX;5758typedef enum59{60OAES_RET_FIRST = 0,61OAES_RET_SUCCESS = 0,62OAES_RET_UNKNOWN,63OAES_RET_ARG1,64OAES_RET_ARG2,65OAES_RET_ARG3,66OAES_RET_ARG4,67OAES_RET_ARG5,68OAES_RET_NOKEY,69OAES_RET_MEM,70OAES_RET_BUF,71OAES_RET_HEADER,72OAES_RET_COUNT73} OAES_RET;7475/*76* oaes_set_option() takes one of these values for its [option] parameter77* some options accept either an optional or a required [value] parameter78*/79// no option80#define OAES_OPTION_NONE 081// enable ECB mode, disable CBC mode82#define OAES_OPTION_ECB 183// enable CBC mode, disable ECB mode84// value is optional, may pass uint8_t iv[OAES_BLOCK_SIZE] to specify85// the value of the initialization vector, iv86#define OAES_OPTION_CBC 28788#ifdef OAES_DEBUG89typedef int ( * oaes_step_cb ) (90const uint8_t state[OAES_BLOCK_SIZE],91const char * step_name,92int step_count,93void * user_data );94// enable state stepping mode95// value is required, must pass oaes_step_cb to receive the state at each step96#define OAES_OPTION_STEP_ON 497// disable state stepping mode98#define OAES_OPTION_STEP_OFF 899#endif // OAES_DEBUG100101typedef uint16_t OAES_OPTION;102103typedef struct _oaes_key104{105size_t data_len;106uint8_t *data;107size_t exp_data_len;108uint8_t *exp_data;109size_t num_keys;110size_t key_base;111} oaes_key;112113typedef struct _oaes_ctx114{115#ifdef OAES_HAVE_ISAAC116randctx * rctx;117#endif // OAES_HAVE_ISAAC118119#ifdef OAES_DEBUG120oaes_step_cb step_cb;121#endif // OAES_DEBUG122123oaes_key * key;124OAES_OPTION options;125uint8_t iv[OAES_BLOCK_SIZE];126} oaes_ctx;127/*128* // usage:129*130* OAES_CTX * ctx = oaes_alloc();131* .132* .133* .134* {135* oaes_gen_key_xxx( ctx );136* {137* oaes_key_export( ctx, _buf, &_buf_len );138* // or139* oaes_key_export_data( ctx, _buf, &_buf_len );\140* }141* }142* // or143* {144* oaes_key_import( ctx, _buf, _buf_len );145* // or146* oaes_key_import_data( ctx, _buf, _buf_len );147* }148* .149* .150* .151* oaes_encrypt( ctx, m, m_len, c, &c_len );152* .153* .154* .155* oaes_decrypt( ctx, c, c_len, m, &m_len );156* .157* .158* .159* oaes_free( &ctx );160*/161162OAES_API OAES_CTX * oaes_alloc(void);163164OAES_API OAES_RET oaes_free( OAES_CTX ** ctx );165166OAES_API OAES_RET oaes_set_option( OAES_CTX * ctx,167OAES_OPTION option, const void * value );168169OAES_API OAES_RET oaes_key_gen_128( OAES_CTX * ctx );170171OAES_API OAES_RET oaes_key_gen_192( OAES_CTX * ctx );172173OAES_API OAES_RET oaes_key_gen_256( OAES_CTX * ctx );174175// export key with header information176// set data == NULL to get the required data_len177OAES_API OAES_RET oaes_key_export( OAES_CTX * ctx,178uint8_t * data, size_t * data_len );179180// directly export the data from key181// set data == NULL to get the required data_len182OAES_API OAES_RET oaes_key_export_data( OAES_CTX * ctx,183uint8_t * data, size_t * data_len );184185// import key with header information186OAES_API OAES_RET oaes_key_import( OAES_CTX * ctx,187const uint8_t * data, size_t data_len );188189// directly import data into key190OAES_API OAES_RET oaes_key_import_data( OAES_CTX * ctx,191const uint8_t * data, size_t data_len );192193// set c == NULL to get the required c_len194OAES_API OAES_RET oaes_encrypt( OAES_CTX * ctx,195const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len );196197// set m == NULL to get the required m_len198OAES_API OAES_RET oaes_decrypt( OAES_CTX * ctx,199const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len );200201// set buf == NULL to get the required buf_len202OAES_API OAES_RET oaes_sprintf(203char * buf, size_t * buf_len, const uint8_t * data, size_t data_len );204205OAES_API OAES_RET oaes_encryption_round( const uint8_t * key, uint8_t * c );206207OAES_API OAES_RET oaes_pseudo_encrypt_ecb( OAES_CTX * ctx, uint8_t * c );208209#ifdef __cplusplus210}211#endif212213#endif // _OAES_LIB_H214215216