OAES_RET oaes_key_import_data( OAES_CTX * ctx, const uint8_t * data, size_t data_len ) { oaes_ctx * _ctx = (oaes_ctx *) ctx; OAES_RET _rc = OAES_RET_SUCCESS; if( NULL == _ctx ) return OAES_RET_ARG1; if( NULL == data ) return OAES_RET_ARG2; switch( data_len ) { case 16: case 24: case 32: break; default: return OAES_RET_ARG3; } if( _ctx->key ) oaes_key_destroy( &(_ctx->key) ); _ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 ); if( NULL == _ctx->key ) return OAES_RET_MEM; _ctx->key->data_len = data_len; _ctx->key->data = (uint8_t *) calloc( data_len, sizeof( uint8_t )); if( NULL == _ctx->key->data ) { oaes_key_destroy( &(_ctx->key) ); return OAES_RET_MEM; } memcpy( _ctx->key->data, data, data_len ); _rc = _rc || oaes_key_expand( ctx ); if( _rc != OAES_RET_SUCCESS ) { oaes_key_destroy( &(_ctx->key) ); return _rc; } return OAES_RET_SUCCESS; }
OAES_RET oaes_free( OAES_CTX ** ctx ) { oaes_ctx ** _ctx = (oaes_ctx **) ctx; if( NULL == _ctx ) return OAES_RET_ARG1; if( NULL == *_ctx ) return OAES_RET_SUCCESS; if( (*_ctx)->key ) oaes_key_destroy( &((*_ctx)->key) ); #ifdef OAES_HAVE_ISAAC if( (*_ctx)->rctx ) { free( (*_ctx)->rctx ); (*_ctx)->rctx = NULL; } #endif // OAES_HAVE_ISAAC free( *_ctx ); *_ctx = NULL; return OAES_RET_SUCCESS; }
static OAES_RET oaes_key_gen( OAES_CTX * ctx, size_t key_size ) { size_t _i; oaes_key * _key = NULL; oaes_ctx * _ctx = (oaes_ctx *) ctx; OAES_RET _rc = OAES_RET_SUCCESS; if( NULL == _ctx ) return OAES_RET_ARG1; _key = (oaes_key *) calloc( sizeof( oaes_key ), 1 ); if( NULL == _key ) return OAES_RET_MEM; if( _ctx->key ) oaes_key_destroy( &(_ctx->key) ); _key->data_len = key_size; _key->data = (uint8_t *) calloc( key_size, sizeof( uint8_t )); if( NULL == _key->data ) { free( _key ); return OAES_RET_MEM; } for( _i = 0; _i < key_size; _i++ ) #ifdef OAES_HAVE_ISAAC _key->data[_i] = (uint8_t) rand( _ctx->rctx ); #else _key->data[_i] = (uint8_t) rand(); #endif // OAES_HAVE_ISAAC _ctx->key = _key; _rc = _rc || oaes_key_expand( ctx ); if( _rc != OAES_RET_SUCCESS ) { oaes_key_destroy( &(_ctx->key) ); return _rc; } return OAES_RET_SUCCESS; }
OAES_RET oaes_key_import( OAES_CTX * ctx, const uint8_t * data, size_t data_len ) { oaes_ctx * _ctx = (oaes_ctx *) ctx; OAES_RET _rc = OAES_RET_SUCCESS; int _key_length; if( NULL == _ctx ) return OAES_RET_ARG1; if( NULL == data ) return OAES_RET_ARG2; switch( data_len ) { case 16 + OAES_BLOCK_SIZE: case 24 + OAES_BLOCK_SIZE: case 32 + OAES_BLOCK_SIZE: break; default: return OAES_RET_ARG3; } // header if( 0 != memcmp( data, oaes_header, 4 ) ) return OAES_RET_HEADER; // header version switch( data[4] ) { case 0x01: break; default: return OAES_RET_HEADER; } // header type switch( data[5] ) { case 0x01: break; default: return OAES_RET_HEADER; } // options _key_length = data[7]; switch( _key_length ) { case 16: case 24: case 32: break; default: return OAES_RET_HEADER; } if( (int)data_len != _key_length + OAES_BLOCK_SIZE ) return OAES_RET_ARG3; if( _ctx->key ) oaes_key_destroy( &(_ctx->key) ); _ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 ); if( NULL == _ctx->key ) return OAES_RET_MEM; _ctx->key->data_len = _key_length; _ctx->key->data = (uint8_t *) calloc( _key_length, sizeof( uint8_t )); if( NULL == _ctx->key->data ) { oaes_key_destroy( &(_ctx->key) ); return OAES_RET_MEM; } memcpy( _ctx->key->data, data + OAES_BLOCK_SIZE, _key_length ); _rc = _rc || oaes_key_expand( ctx ); if( _rc != OAES_RET_SUCCESS ) { oaes_key_destroy( &(_ctx->key) ); return _rc; } return OAES_RET_SUCCESS; }