Debian Codegraph @

src/crypto/oaes_lib.h

raw symbols SCIP
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 {
62 OAES_RET_FIRST = 0,
63 OAES_RET_SUCCESS = 0,
64 OAES_RET_UNKNOWN,
65 OAES_RET_ARG1,
66 OAES_RET_ARG2,
67 OAES_RET_ARG3,
68 OAES_RET_ARG4,
69 OAES_RET_ARG5,
70 OAES_RET_NOKEY,
71 OAES_RET_MEM,
72 OAES_RET_BUF,
73 OAES_RET_HEADER,
74 OAES_RET_COUNT
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 ) (
92 const uint8_t state[OAES_BLOCK_SIZE],
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
103 typedef uint16_t OAES_OPTION;
104
105 typedef struct _oaes_key
106 {
107 size_t data_len;
108 uint8_t *data;
109 size_t exp_data_len;
110 uint8_t *exp_data;
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
125 oaes_key * key;
126 OAES_OPTION options;
127 uint8_t iv[OAES_BLOCK_SIZE];
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
164 OAES_API OAES_CTX * oaes_alloc(void);
165
166 OAES_API OAES_RET oaes_free( OAES_CTX ** ctx );
167
168 OAES_API OAES_RET oaes_set_option( OAES_CTX * ctx,
169 OAES_OPTION option, const void * value );
170
171 OAES_API OAES_RET oaes_key_gen_128( OAES_CTX * ctx );
172
173 OAES_API OAES_RET oaes_key_gen_192( OAES_CTX * ctx );
174
175 OAES_API OAES_RET oaes_key_gen_256( OAES_CTX * ctx );
176
177 // export key with header information
178 // set data == NULL to get the required data_len
179 OAES_API OAES_RET oaes_key_export( OAES_CTX * ctx,
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
184 OAES_API OAES_RET oaes_key_export_data( OAES_CTX * ctx,
185 uint8_t * data, size_t * data_len );
186
187 // import key with header information
188 OAES_API OAES_RET oaes_key_import( OAES_CTX * ctx,
189 const uint8_t * data, size_t data_len );
190
191 // directly import data into key
192 OAES_API OAES_RET oaes_key_import_data( OAES_CTX * ctx,
193 const uint8_t * data, size_t data_len );
194
195 // set c == NULL to get the required c_len
196 OAES_API OAES_RET oaes_encrypt( OAES_CTX * ctx,
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
200 OAES_API OAES_RET oaes_decrypt( OAES_CTX * ctx,
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
204 OAES_API OAES_RET oaes_sprintf(
205 char * buf, size_t * buf_len, const uint8_t * data, size_t data_len );
206
207 OAES_API OAES_RET oaes_encryption_round( const uint8_t * key, uint8_t * c );
208
209 OAES_API OAES_RET oaes_pseudo_encrypt_ecb( OAES_CTX * ctx, uint8_t * c );
210
211 #ifdef __cplusplus
212 }
213 #endif
214
215 #endif // _OAES_LIB_H