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 | #include <time.h> |
| 35 | |
| 36 | #include "oaes_lib.h" |
| 37 | |
| 38 | void usage(const char * exe_name) |
| 39 | { |
| 40 | if( NULL == exe_name ) |
| 41 | return; |
| 42 | |
| 43 | printf( |
| 44 | "Usage:\n" |
| 45 | "\t%s [-ecb] [-key < 128 | 192 | 256 >] [-data <data_len>]\n", |
| 46 | exe_name |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * |
| 52 | */ |
| 53 | int main(int argc, char** argv) { |
| 54 | |
| 55 | size_t _i, _j; |
| 56 | time_t _time_start, _time_end; |
| 57 | OAES_CTX * ctx = NULL; |
| 58 | uint8_t *_encbuf, *_decbuf; |
| 59 | size_t _encbuf_len, _decbuf_len; |
| 60 | uint8_t _buf[1024 * 1024]; |
| 61 | short _is_ecb = 0; |
| 62 | int _key_len = 128; |
| 63 | size_t _data_len = 64; |
| 64 | |
| 65 | for( _i = 1; _i < argc; _i++ ) |
| 66 | { |
| 67 | int _found = 0; |
| 68 | |
| 69 | if( 0 == strcmp( argv[_i], "-ecb" ) ) |
| 70 | { |
| 71 | _found = 1; |
| 72 | _is_ecb = 1; |
| 73 | } |
| 74 | |
| 75 | if( 0 == strcmp( argv[_i], "-key" ) ) |
| 76 | { |
| 77 | _found = 1; |
| 78 | _i++; // key_len |
| 79 | if( _i >= argc ) |
| 80 | { |
| 81 | printf("Error: No value specified for '-%s'.\n", |
| 82 | "key"); |
| 83 | usage( argv[0] ); |
| 84 | return 1; |
| 85 | } |
| 86 | _key_len = atoi( argv[_i] ); |
| 87 | switch( _key_len ) |
| 88 | { |
| 89 | case 128: |
| 90 | case 192: |
| 91 | case 256: |
| 92 | break; |
| 93 | default: |
| 94 | printf("Error: Invalid value [%d] specified for '-%s'.\n", |
| 95 | _key_len, "key"); |
| 96 | usage( argv[0] ); |
| 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if( 0 == strcmp( argv[_i], "-data" ) ) |
| 102 | { |
| 103 | _found = 1; |
| 104 | _i++; // data_len |
| 105 | if( _i >= argc ) |
| 106 | { |
| 107 | printf("Error: No value specified for '-%s'.\n", |
| 108 | "data"); |
| 109 | usage( argv[0] ); |
| 110 | return 1; |
| 111 | } |
| 112 | _data_len = atoi( argv[_i] ); |
| 113 | } |
| 114 | |
| 115 | if( 0 == _found ) |
| 116 | { |
| 117 | printf("Error: Invalid option '%s'.\n", argv[_i]); |
| 118 | usage( argv[0] ); |
| 119 | return 1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // generate random test data |
| 124 | time( &_time_start ); |
| 125 | srand( _time_start ); |
| 126 | for( _i = 0; _i < 1024 * 1024; _i++ ) |
| 127 | _buf[_i] = rand(); |
| 128 | |
| 129 | ctx = oaes_alloc(); |
| 130 | if( NULL == ctx ) |
| 131 | { |
| 132 | printf("Error: Failed to initialize OAES.\n"); |
| 133 | return EXIT_FAILURE; |
| 134 | } |
| 135 | if( _is_ecb ) |
| 136 | if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) ) |
| 137 | printf("Error: Failed to set OAES options.\n"); |
| 138 | switch( _key_len ) |
| 139 | { |
| 140 | case 128: |
| 141 | if( OAES_RET_SUCCESS != oaes_key_gen_128(ctx) ) |
| 142 | printf("Error: Failed to generate OAES %d bit key.\n", _key_len); |
| 143 | break; |
| 144 | case 192: |
| 145 | if( OAES_RET_SUCCESS != oaes_key_gen_192(ctx) ) |
| 146 | printf("Error: Failed to generate OAES %d bit key.\n", _key_len); |
| 147 | break; |
| 148 | case 256: |
| 149 | if( OAES_RET_SUCCESS != oaes_key_gen_256(ctx) ) |
| 150 | printf("Error: Failed to generate OAES %d bit key.\n", _key_len); |
| 151 | break; |
| 152 | default: |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | if( OAES_RET_SUCCESS != oaes_encrypt( ctx, |
| 157 | (const uint8_t *)_buf, 1024 * 1024, NULL, &_encbuf_len ) ) |
| 158 | printf("Error: Failed to retrieve required buffer size for encryption.\n"); |
| 159 | _encbuf = (uint8_t *) calloc( _encbuf_len, sizeof( char ) ); |
| 160 | if( NULL == _encbuf ) |
| 161 | { |
| 162 | printf( "Error: Failed to allocate memory.\n" ); |
| 163 | return EXIT_FAILURE; |
| 164 | } |
| 165 | |
| 166 | if( OAES_RET_SUCCESS != oaes_decrypt( ctx, |
| 167 | _encbuf, _encbuf_len, NULL, &_decbuf_len ) ) |
| 168 | printf("Error: Failed to retrieve required buffer size for encryption.\n"); |
| 169 | _decbuf = (uint8_t *) calloc( _decbuf_len, sizeof( char ) ); |
| 170 | if( NULL == _decbuf ) |
| 171 | { |
| 172 | free( _encbuf ); |
| 173 | printf( "Error: Failed to allocate memory.\n" ); |
| 174 | return EXIT_FAILURE; |
| 175 | } |
| 176 | |
| 177 | time( &_time_start ); |
| 178 | |
| 179 | for( _i = 0; _i < _data_len; _i++ ) |
| 180 | { |
| 181 | if( OAES_RET_SUCCESS != oaes_encrypt( ctx, |
| 182 | (const uint8_t *)_buf, 1024 * 1024, _encbuf, &_encbuf_len ) ) |
| 183 | printf("Error: Encryption failed.\n"); |
| 184 | if( OAES_RET_SUCCESS != oaes_decrypt( ctx, |
| 185 | _encbuf, _encbuf_len, _decbuf, &_decbuf_len ) ) |
| 186 | printf("Error: Decryption failed.\n"); |
| 187 | } |
| 188 | |
| 189 | time( &_time_end ); |
| 190 | printf( "Test encrypt and decrypt:\n\ttime: %lld seconds\n\tdata: %ld MB" |
| 191 | "\n\tkey: %d bits\n\tmode: %s\n", |
| 192 | _time_end - _time_start, _data_len, |
| 193 | _key_len, _is_ecb? "EBC" : "CBC" ); |
| 194 | free( _encbuf ); |
| 195 | free( _decbuf ); |
| 196 | if( OAES_RET_SUCCESS != oaes_free( &ctx ) ) |
| 197 | printf("Error: Failed to uninitialize OAES.\n"); |
| 198 | |
| 199 | return (EXIT_SUCCESS); |
| 200 | } |