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 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "oaes_lib.h" |
| 36 | |
| 37 | void usage(const char * exe_name) |
| 38 | { |
| 39 | if( NULL == exe_name ) |
| 40 | return; |
| 41 | |
| 42 | printf( |
| 43 | "Usage:\n" |
| 44 | "\t%s [-ecb] [-key < 128 | 192 | 256 >] <text>\n", |
| 45 | exe_name |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | int main(int argc, char** argv) |
| 50 | { |
| 51 | size_t _i; |
| 52 | OAES_CTX * ctx = NULL; |
| 53 | uint8_t *_encbuf, *_decbuf; |
| 54 | size_t _encbuf_len, _decbuf_len, _buf_len; |
| 55 | char *_buf; |
| 56 | short _is_ecb = 0; |
| 57 | char * _text = NULL; |
| 58 | int _key_len = 128; |
| 59 | |
| 60 | if( argc < 2 ) |
| 61 | { |
| 62 | usage( argv[0] ); |
| 63 | return EXIT_FAILURE; |
| 64 | } |
| 65 | |
| 66 | for( _i = 1; _i < argc; _i++ ) |
| 67 | { |
| 68 | int _found = 0; |
| 69 | |
| 70 | if( 0 == strcmp( argv[_i], "-ecb" ) ) |
| 71 | { |
| 72 | _found = 1; |
| 73 | _is_ecb = 1; |
| 74 | } |
| 75 | |
| 76 | if( 0 == strcmp( argv[_i], "-key" ) ) |
| 77 | { |
| 78 | _found = 1; |
| 79 | _i++; // len |
| 80 | if( _i >= argc ) |
| 81 | { |
| 82 | printf("Error: No value specified for '-%s'.\n", |
| 83 | "key"); |
| 84 | usage( argv[0] ); |
| 85 | return EXIT_FAILURE; |
| 86 | } |
| 87 | _key_len = atoi( argv[_i] ); |
| 88 | switch( _key_len ) |
| 89 | { |
| 90 | case 128: |
| 91 | case 192: |
| 92 | case 256: |
| 93 | break; |
| 94 | default: |
| 95 | printf("Error: Invalid value [%d] specified for '-%s'.\n", |
| 96 | _key_len, "key"); |
| 97 | usage( argv[0] ); |
| 98 | return EXIT_FAILURE; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if( 0 == _found ) |
| 103 | { |
| 104 | if( _text ) |
| 105 | { |
| 106 | printf("Error: Invalid option '%s'.\n", argv[_i]); |
| 107 | usage( argv[0] ); |
| 108 | return EXIT_FAILURE; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | _text = (char *) calloc(strlen( argv[_i] ) + 1, sizeof(char)); |
| 113 | if( NULL == _text ) |
| 114 | { |
| 115 | printf("Error: Failed to allocate memory.\n", argv[_i]); |
| 116 | return EXIT_FAILURE; |
| 117 | } |
| 118 | strcpy( _text, argv[_i] ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if( NULL == _text ) |
| 124 | { |
| 125 | usage( argv[0] ); |
| 126 | return EXIT_FAILURE; |
| 127 | } |
| 128 | |
| 129 | oaes_sprintf( NULL, &_buf_len, |
| 130 | (const uint8_t *)_text, strlen( _text ) ); |
| 131 | _buf = (char *) calloc(_buf_len, sizeof(char)); |
| 132 | printf( "\n***** plaintext *****\n" ); |
| 133 | if( _buf ) |
| 134 | { |
| 135 | oaes_sprintf( _buf, &_buf_len, |
| 136 | (const uint8_t *)_text, strlen( _text ) ); |
| 137 | printf( "%s", _buf ); |
| 138 | } |
| 139 | printf( "\n**********************\n" ); |
| 140 | free( _buf ); |
| 141 | |
| 142 | ctx = oaes_alloc(); |
| 143 | if( NULL == ctx ) |
| 144 | { |
| 145 | printf("Error: Failed to initialize OAES.\n"); |
| 146 | free( _text ); |
| 147 | return EXIT_FAILURE; |
| 148 | } |
| 149 | if( _is_ecb ) |
| 150 | if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) ) |
| 151 | printf("Error: Failed to set OAES options.\n"); |
| 152 | switch( _key_len ) |
| 153 | { |
| 154 | case 128: |
| 155 | if( OAES_RET_SUCCESS != oaes_key_gen_128(ctx) ) |
| 156 | printf("Error: Failed to generate OAES %d bit key.\n", _key_len); |
| 157 | break; |
| 158 | case 192: |
| 159 | if( OAES_RET_SUCCESS != oaes_key_gen_192(ctx) ) |
| 160 | printf("Error: Failed to generate OAES %d bit key.\n", _key_len); |
| 161 | break; |
| 162 | case 256: |
| 163 | if( OAES_RET_SUCCESS != oaes_key_gen_256(ctx) ) |
| 164 | printf("Error: Failed to generate OAES %d bit key.\n", _key_len); |
| 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | if( OAES_RET_SUCCESS != oaes_encrypt( ctx, |
| 171 | (const uint8_t *)_text, strlen( _text ), NULL, &_encbuf_len ) ) |
| 172 | printf("Error: Failed to retrieve required buffer size for encryption.\n"); |
| 173 | _encbuf = (uint8_t *) calloc( _encbuf_len, sizeof(uint8_t) ); |
| 174 | if( NULL == _encbuf ) |
| 175 | { |
| 176 | printf( "Error: Failed to allocate memory.\n" ); |
| 177 | free( _text ); |
| 178 | return EXIT_FAILURE; |
| 179 | } |
| 180 | if( OAES_RET_SUCCESS != oaes_encrypt( ctx, |
| 181 | (const uint8_t *)_text, strlen( _text ), _encbuf, &_encbuf_len ) ) |
| 182 | printf("Error: Encryption failed.\n"); |
| 183 | |
| 184 | if( OAES_RET_SUCCESS != oaes_decrypt( ctx, |
| 185 | _encbuf, _encbuf_len, NULL, &_decbuf_len ) ) |
| 186 | printf("Error: Failed to retrieve required buffer size for encryption.\n"); |
| 187 | _decbuf = (uint8_t *) calloc( _decbuf_len, sizeof(uint8_t) ); |
| 188 | if( NULL == _decbuf ) |
| 189 | { |
| 190 | printf( "Error: Failed to allocate memory.\n" ); |
| 191 | free( _text ); |
| 192 | free( _encbuf ); |
| 193 | return EXIT_FAILURE; |
| 194 | } |
| 195 | if( OAES_RET_SUCCESS != oaes_decrypt( ctx, |
| 196 | _encbuf, _encbuf_len, _decbuf, &_decbuf_len ) ) |
| 197 | printf("Error: Decryption failed.\n"); |
| 198 | |
| 199 | if( OAES_RET_SUCCESS != oaes_free( &ctx ) ) |
| 200 | printf("Error: Failed to uninitialize OAES.\n"); |
| 201 | |
| 202 | oaes_sprintf( NULL, &_buf_len, _encbuf, _encbuf_len ); |
| 203 | _buf = (char *) calloc(_buf_len, sizeof(char)); |
| 204 | printf( "\n***** cyphertext *****\n" ); |
| 205 | if( _buf ) |
| 206 | { |
| 207 | oaes_sprintf( _buf, &_buf_len, _encbuf, _encbuf_len ); |
| 208 | printf( "%s", _buf ); |
| 209 | } |
| 210 | printf( "\n**********************\n" ); |
| 211 | free( _buf ); |
| 212 | |
| 213 | oaes_sprintf( NULL, &_buf_len, _decbuf, _decbuf_len ); |
| 214 | _buf = (char *) calloc(_buf_len, sizeof( char)); |
| 215 | printf( "\n***** plaintext *****\n" ); |
| 216 | if( _buf ) |
| 217 | { |
| 218 | oaes_sprintf( _buf, &_buf_len, _decbuf, _decbuf_len ); |
| 219 | printf( "%s", _buf ); |
| 220 | } |
| 221 | printf( "\n**********************\n\n" ); |
| 222 | free( _buf ); |
| 223 | |
| 224 | free( _encbuf ); |
| 225 | free( _decbuf ); |
| 226 | free( _text ); |
| 227 | |
| 228 | return (EXIT_SUCCESS); |
| 229 | } |