Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/crypto/oaes_lib.h
1201 views
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
36
#ifdef __cplusplus
37
extern "C" {
38
#endif
39
40
#ifdef _WIN32
41
# ifdef OAES_SHARED
42
# ifdef oaes_lib_EXPORTS
43
# define OAES_API __declspec(dllexport)
44
# else
45
# define OAES_API __declspec(dllimport)
46
# endif
47
# else
48
# define OAES_API
49
# endif
50
#else
51
# define OAES_API
52
#endif // WIN32
53
54
#define OAES_VERSION "0.8.1"
55
#define OAES_BLOCK_SIZE 16
56
57
typedef void OAES_CTX;
58
59
typedef enum
60
{
61
OAES_RET_FIRST = 0,
62
OAES_RET_SUCCESS = 0,
63
OAES_RET_UNKNOWN,
64
OAES_RET_ARG1,
65
OAES_RET_ARG2,
66
OAES_RET_ARG3,
67
OAES_RET_ARG4,
68
OAES_RET_ARG5,
69
OAES_RET_NOKEY,
70
OAES_RET_MEM,
71
OAES_RET_BUF,
72
OAES_RET_HEADER,
73
OAES_RET_COUNT
74
} OAES_RET;
75
76
/*
77
* oaes_set_option() takes one of these values for its [option] parameter
78
* some options accept either an optional or a required [value] parameter
79
*/
80
// no option
81
#define OAES_OPTION_NONE 0
82
// enable ECB mode, disable CBC mode
83
#define OAES_OPTION_ECB 1
84
// enable CBC mode, disable ECB mode
85
// value is optional, may pass uint8_t iv[OAES_BLOCK_SIZE] to specify
86
// the value of the initialization vector, iv
87
#define OAES_OPTION_CBC 2
88
89
#ifdef OAES_DEBUG
90
typedef int ( * oaes_step_cb ) (
91
const uint8_t state[OAES_BLOCK_SIZE],
92
const char * step_name,
93
int step_count,
94
void * user_data );
95
// enable state stepping mode
96
// value is required, must pass oaes_step_cb to receive the state at each step
97
#define OAES_OPTION_STEP_ON 4
98
// disable state stepping mode
99
#define OAES_OPTION_STEP_OFF 8
100
#endif // OAES_DEBUG
101
102
typedef uint16_t OAES_OPTION;
103
104
typedef struct _oaes_key
105
{
106
size_t data_len;
107
uint8_t *data;
108
size_t exp_data_len;
109
uint8_t *exp_data;
110
size_t num_keys;
111
size_t key_base;
112
} oaes_key;
113
114
typedef struct _oaes_ctx
115
{
116
#ifdef OAES_HAVE_ISAAC
117
randctx * rctx;
118
#endif // OAES_HAVE_ISAAC
119
120
#ifdef OAES_DEBUG
121
oaes_step_cb step_cb;
122
#endif // OAES_DEBUG
123
124
oaes_key * key;
125
OAES_OPTION options;
126
uint8_t iv[OAES_BLOCK_SIZE];
127
} oaes_ctx;
128
/*
129
* // usage:
130
*
131
* OAES_CTX * ctx = oaes_alloc();
132
* .
133
* .
134
* .
135
* {
136
* oaes_gen_key_xxx( ctx );
137
* {
138
* oaes_key_export( ctx, _buf, &_buf_len );
139
* // or
140
* oaes_key_export_data( ctx, _buf, &_buf_len );\
141
* }
142
* }
143
* // or
144
* {
145
* oaes_key_import( ctx, _buf, _buf_len );
146
* // or
147
* oaes_key_import_data( ctx, _buf, _buf_len );
148
* }
149
* .
150
* .
151
* .
152
* oaes_encrypt( ctx, m, m_len, c, &c_len );
153
* .
154
* .
155
* .
156
* oaes_decrypt( ctx, c, c_len, m, &m_len );
157
* .
158
* .
159
* .
160
* oaes_free( &ctx );
161
*/
162
163
OAES_API OAES_CTX * oaes_alloc(void);
164
165
OAES_API OAES_RET oaes_free( OAES_CTX ** ctx );
166
167
OAES_API OAES_RET oaes_set_option( OAES_CTX * ctx,
168
OAES_OPTION option, const void * value );
169
170
OAES_API OAES_RET oaes_key_gen_128( OAES_CTX * ctx );
171
172
OAES_API OAES_RET oaes_key_gen_192( OAES_CTX * ctx );
173
174
OAES_API OAES_RET oaes_key_gen_256( OAES_CTX * ctx );
175
176
// export key with header information
177
// set data == NULL to get the required data_len
178
OAES_API OAES_RET oaes_key_export( OAES_CTX * ctx,
179
uint8_t * data, size_t * data_len );
180
181
// directly export the data from key
182
// set data == NULL to get the required data_len
183
OAES_API OAES_RET oaes_key_export_data( OAES_CTX * ctx,
184
uint8_t * data, size_t * data_len );
185
186
// import key with header information
187
OAES_API OAES_RET oaes_key_import( OAES_CTX * ctx,
188
const uint8_t * data, size_t data_len );
189
190
// directly import data into key
191
OAES_API OAES_RET oaes_key_import_data( OAES_CTX * ctx,
192
const uint8_t * data, size_t data_len );
193
194
// set c == NULL to get the required c_len
195
OAES_API OAES_RET oaes_encrypt( OAES_CTX * ctx,
196
const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len );
197
198
// set m == NULL to get the required m_len
199
OAES_API OAES_RET oaes_decrypt( OAES_CTX * ctx,
200
const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len );
201
202
// set buf == NULL to get the required buf_len
203
OAES_API OAES_RET oaes_sprintf(
204
char * buf, size_t * buf_len, const uint8_t * data, size_t data_len );
205
206
OAES_API OAES_RET oaes_encryption_round( const uint8_t * key, uint8_t * c );
207
208
OAES_API OAES_RET oaes_pseudo_encrypt_ecb( OAES_CTX * ctx, uint8_t * c );
209
210
#ifdef __cplusplus
211
}
212
#endif
213
214
#endif // _OAES_LIB_H
215
216