Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 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 | #define OAES_VERSION "0.7.0" |
| 41 | #define OAES_BLOCK_SIZE 16 |
| 42 | |
| 43 | typedef void OAES_CTX; |
| 44 | |
| 45 | typedef enum |
| 46 | { |
| 47 | OAES_RET_FIRST = 0, |
| 48 | OAES_RET_SUCCESS = 0, |
| 49 | OAES_RET_UNKNOWN, |
| 50 | OAES_RET_ARG1, |
| 51 | OAES_RET_ARG2, |
| 52 | OAES_RET_ARG3, |
| 53 | OAES_RET_ARG4, |
| 54 | OAES_RET_ARG5, |
| 55 | OAES_RET_NOKEY, |
| 56 | OAES_RET_MEM, |
| 57 | OAES_RET_BUF, |
| 58 | OAES_RET_HEADER, |
| 59 | OAES_RET_COUNT |
| 60 | } OAES_RET; |
| 61 | |
| 62 | /* |
| 63 | * oaes_set_option() takes one of these values for its [option] parameter |
| 64 | * some options accept either an optional or a required [value] parameter |
| 65 | */ |
| 66 | // no option |
| 67 | #define OAES_OPTION_NONE 0 |
| 68 | // enable ECB mode, disable CBC mode |
| 69 | #define OAES_OPTION_ECB 1 |
| 70 | // enable CBC mode, disable ECB mode |
| 71 | // value is optional, may pass uint8_t iv[OAES_BLOCK_SIZE] to specify |
| 72 | // the value of the initialization vector, iv |
| 73 | #define OAES_OPTION_CBC 2 |
| 74 | |
| 75 | #ifdef OAES_DEBUG |
| 76 | typedef int ( * oaes_step_cb ) ( |
| 77 | const uint8_t state[OAES_BLOCK_SIZE], |
| 78 | const char * step_name, |
| 79 | int step_count, |
| 80 | void * user_data ); |
| 81 | // enable state stepping mode |
| 82 | // value is required, must pass oaes_step_cb to receive the state at each step |
| 83 | #define OAES_OPTION_STEP_ON 4 |
| 84 | // disable state stepping mode |
| 85 | #define OAES_OPTION_STEP_OFF 8 |
| 86 | #endif // OAES_DEBUG |
| 87 | |
| 88 | typedef uint16_t OAES_OPTION; |
| 89 | |
| 90 | /* |
| 91 | * // usage: |
| 92 | * |
| 93 | * OAES_CTX * ctx = oaes_alloc(); |
| 94 | * . |
| 95 | * . |
| 96 | * . |
| 97 | * { |
| 98 | * oaes_gen_key_xxx( ctx ); |
| 99 | * { |
| 100 | * oaes_key_export( ctx, _buf, &_buf_len ); |
| 101 | * // or |
| 102 | * oaes_key_export_data( ctx, _buf, &_buf_len );\ |
| 103 | * } |
| 104 | * } |
| 105 | * // or |
| 106 | * { |
| 107 | * oaes_key_import( ctx, _buf, _buf_len ); |
| 108 | * // or |
| 109 | * oaes_key_import_data( ctx, _buf, _buf_len ); |
| 110 | * } |
| 111 | * . |
| 112 | * . |
| 113 | * . |
| 114 | * oaes_encrypt( ctx, m, m_len, c, &c_len ); |
| 115 | * . |
| 116 | * . |
| 117 | * . |
| 118 | * oaes_decrypt( ctx, c, c_len, m, &m_len ); |
| 119 | * . |
| 120 | * . |
| 121 | * . |
| 122 | * oaes_free( &ctx ); |
| 123 | */ |
| 124 | |
| 125 | OAES_CTX * oaes_alloc(); |
| 126 | |
| 127 | OAES_RET oaes_free( OAES_CTX ** ctx ); |
| 128 | |
| 129 | OAES_RET oaes_set_option( OAES_CTX * ctx, |
| 130 | OAES_OPTION option, const void * value ); |
| 131 | |
| 132 | OAES_RET oaes_key_gen_128( OAES_CTX * ctx ); |
| 133 | |
| 134 | OAES_RET oaes_key_gen_192( OAES_CTX * ctx ); |
| 135 | |
| 136 | OAES_RET oaes_key_gen_256( OAES_CTX * ctx ); |
| 137 | |
| 138 | // export key with header information |
| 139 | // set data == NULL to get the required data_len |
| 140 | OAES_RET oaes_key_export( OAES_CTX * ctx, |
| 141 | uint8_t * data, size_t * data_len ); |
| 142 | |
| 143 | // directly export the data from key |
| 144 | // set data == NULL to get the required data_len |
| 145 | OAES_RET oaes_key_export_data( OAES_CTX * ctx, |
| 146 | uint8_t * data, size_t * data_len ); |
| 147 | |
| 148 | // import key with header information |
| 149 | OAES_RET oaes_key_import( OAES_CTX * ctx, |
| 150 | const uint8_t * data, size_t data_len ); |
| 151 | |
| 152 | // directly import data into key |
| 153 | OAES_RET oaes_key_import_data( OAES_CTX * ctx, |
| 154 | const uint8_t * data, size_t data_len ); |
| 155 | |
| 156 | // set c == NULL to get the required c_len |
| 157 | OAES_RET oaes_encrypt( OAES_CTX * ctx, |
| 158 | const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len ); |
| 159 | |
| 160 | // set m == NULL to get the required m_len |
| 161 | OAES_RET oaes_decrypt( OAES_CTX * ctx, |
| 162 | const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len ); |
| 163 | |
| 164 | // set buf == NULL to get the required buf_len |
| 165 | OAES_RET oaes_sprintf( |
| 166 | char * buf, size_t * buf_len, const uint8_t * data, size_t data_len ); |
| 167 | |
| 168 | #ifdef __cplusplus |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #endif // _OAES_LIB_H |