Electroneum
oaes_lib.h
Go to the documentation of this file.
1 /*
2  * ---------------------------------------------------------------------------
3  * OpenAES License
4  * ---------------------------------------------------------------------------
5  * Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * - Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  * ---------------------------------------------------------------------------
29  */
30 
31 #ifndef _OAES_LIB_H
32 #define _OAES_LIB_H
33 
34 #include <stdint.h>
35 #include <stdlib.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #ifdef _WIN32
42 # ifdef OAES_SHARED
43 # ifdef oaes_lib_EXPORTS
44 # define OAES_API __declspec(dllexport)
45 # else
46 # define OAES_API __declspec(dllimport)
47 # endif
48 # else
49 # define OAES_API
50 # endif
51 #else
52 # define OAES_API
53 #endif // WIN32
54 
55 #define OAES_VERSION "0.8.1"
56 #define OAES_BLOCK_SIZE 16
57 
58 typedef void OAES_CTX;
59 
60 typedef enum
61 {
75 } OAES_RET;
76 
77 /*
78  * oaes_set_option() takes one of these values for its [option] parameter
79  * some options accept either an optional or a required [value] parameter
80  */
81 // no option
82 #define OAES_OPTION_NONE 0
83 // enable ECB mode, disable CBC mode
84 #define OAES_OPTION_ECB 1
85 // enable CBC mode, disable ECB mode
86 // value is optional, may pass uint8_t iv[OAES_BLOCK_SIZE] to specify
87 // the value of the initialization vector, iv
88 #define OAES_OPTION_CBC 2
89 
90 #ifdef OAES_DEBUG
91 typedef int ( * oaes_step_cb ) (
93  const char * step_name,
94  int step_count,
95  void * user_data );
96 // enable state stepping mode
97 // value is required, must pass oaes_step_cb to receive the state at each step
98 #define OAES_OPTION_STEP_ON 4
99 // disable state stepping mode
100 #define OAES_OPTION_STEP_OFF 8
101 #endif // OAES_DEBUG
102 
104 
105 typedef struct _oaes_key
106 {
107  size_t data_len;
109  size_t exp_data_len;
111  size_t num_keys;
112  size_t key_base;
113 } oaes_key;
114 
115 typedef struct _oaes_ctx
116 {
117 #ifdef OAES_HAVE_ISAAC
118  randctx * rctx;
119 #endif // OAES_HAVE_ISAAC
120 
121 #ifdef OAES_DEBUG
122  oaes_step_cb step_cb;
123 #endif // OAES_DEBUG
124 
128 } oaes_ctx;
129 /*
130  * // usage:
131  *
132  * OAES_CTX * ctx = oaes_alloc();
133  * .
134  * .
135  * .
136  * {
137  * oaes_gen_key_xxx( ctx );
138  * {
139  * oaes_key_export( ctx, _buf, &_buf_len );
140  * // or
141  * oaes_key_export_data( ctx, _buf, &_buf_len );\
142  * }
143  * }
144  * // or
145  * {
146  * oaes_key_import( ctx, _buf, _buf_len );
147  * // or
148  * oaes_key_import_data( ctx, _buf, _buf_len );
149  * }
150  * .
151  * .
152  * .
153  * oaes_encrypt( ctx, m, m_len, c, &c_len );
154  * .
155  * .
156  * .
157  * oaes_decrypt( ctx, c, c_len, m, &m_len );
158  * .
159  * .
160  * .
161  * oaes_free( &ctx );
162  */
163 
165 
167 
169  OAES_OPTION option, const void * value );
170 
172 
174 
176 
177 // export key with header information
178 // set data == NULL to get the required data_len
180  uint8_t * data, size_t * data_len );
181 
182 // directly export the data from key
183 // set data == NULL to get the required data_len
185  uint8_t * data, size_t * data_len );
186 
187 // import key with header information
189  const uint8_t * data, size_t data_len );
190 
191 // directly import data into key
193  const uint8_t * data, size_t data_len );
194 
195 // set c == NULL to get the required c_len
197  const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len );
198 
199 // set m == NULL to get the required m_len
201  const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len );
202 
203 // set buf == NULL to get the required buf_len
205  char * buf, size_t * buf_len, const uint8_t * data, size_t data_len );
206 
208 
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif // _OAES_LIB_H
OAES_API OAES_RET oaes_encrypt(OAES_CTX *ctx, const uint8_t *m, size_t m_len, uint8_t *c, size_t *c_len)
size_t num_keys
Definition: oaes_lib.h:111
OAES_API OAES_RET oaes_set_option(OAES_CTX *ctx, OAES_OPTION option, const void *value)
size_t key_base
Definition: oaes_lib.h:112
OAES_API OAES_RET oaes_key_gen_192(OAES_CTX *ctx)
oaes_key * key
Definition: oaes_lib.h:125
OAES_API OAES_RET oaes_decrypt(OAES_CTX *ctx, const uint8_t *c, size_t c_len, uint8_t *m, size_t *m_len)
uint8_t * data
Definition: oaes_lib.h:108
const char * key
Definition: hmac_keccak.cpp:39
unsigned short uint16_t
Definition: stdint.h:125
size_t exp_data_len
Definition: oaes_lib.h:109
OAES_API OAES_RET oaes_key_import(OAES_CTX *ctx, const uint8_t *data, size_t data_len)
unsigned char uint8_t
Definition: stdint.h:124
size_t data_len
Definition: oaes_lib.h:107
OAES_OPTION options
Definition: oaes_lib.h:126
OAES_API OAES_RET oaes_key_import_data(OAES_CTX *ctx, const uint8_t *data, size_t data_len)
OAES_API OAES_RET oaes_pseudo_encrypt_ecb(OAES_CTX *ctx, uint8_t *c)
uint16_t OAES_OPTION
Definition: oaes_lib.h:103
void OAES_CTX
Definition: oaes_lib.h:58
Definition: options.h:86
OAES_API OAES_RET oaes_key_export_data(OAES_CTX *ctx, uint8_t *data, size_t *data_len)
OAES_API OAES_RET oaes_key_gen_128(OAES_CTX *ctx)
struct _oaes_ctx oaes_ctx
OAES_API OAES_RET oaes_key_export(OAES_CTX *ctx, uint8_t *data, size_t *data_len)
uint8_t * exp_data
Definition: oaes_lib.h:110
OAES_API OAES_RET oaes_key_gen_256(OAES_CTX *ctx)
const char * buf
Definition: slow_memmem.cpp:74
#define OAES_API
Definition: oaes_lib.h:52
uint8_t iv[OAES_BLOCK_SIZE]
Definition: oaes_lib.h:127
OAES_API OAES_RET oaes_sprintf(char *buf, size_t *buf_len, const uint8_t *data, size_t data_len)
OAES_API OAES_RET oaes_encryption_round(const uint8_t *key, uint8_t *c)
OAES_API OAES_CTX * oaes_alloc(void)
Definition: blake256.h:37
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
OAES_API OAES_RET oaes_free(OAES_CTX **ctx)
#define OAES_BLOCK_SIZE
Definition: oaes_lib.h:56
OAES_RET
Definition: oaes_lib.h:60
struct _oaes_key oaes_key