1 //user simon_64_32 12 -e key tests tests.cip
4 // Created by Calvin McCoy on 9/2/17.
12 #include "cipher_constants.h"
18 $ ./user simon_256_128 ECB -e keyfile input output
27 #define VERSION "custom_build"
35 {"simon_64_32", 0, SIMON},
36 {"simon_72_48", 1, SIMON},
37 {"simon_96_48", 2, SIMON},
38 {"simon_96_64", 3, SIMON},
39 {"simon_128_64", 4, SIMON},
40 {"simon_96_96", 5, SIMON},
41 {"simon_144_96", 6, SIMON},
42 {"simon_128_128", 7, SIMON},
43 {"simon_192_128", 8, SIMON},
44 {"simon_256_128", 9, SIMON},
45 {"speck_64_32", 0, SPECK},
46 {"speck_72_48", 1, SPECK},
47 {"speck_96_48", 2, SPECK},
48 {"speck_96_64", 3, SPECK},
49 {"speck_128_64", 4, SPECK},
50 {"speck_96_96", 5, SPECK},
51 {"speck_144_96", 6, SPECK},
52 {"speck_128_128", 7, SPECK},
53 {"speck_192_128", 8, SPECK},
54 {"speck_256_128", 9, SPECK}
60 struct timeval tstart;
61 gettimeofday(&tstart,0);
62 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
65 double TimeStop(double t)
69 gettimeofday(&tend,0);
70 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
77 void print_usage(void);
80 int main(int argc, char *argv[]) {
82 /* Test That The Minumum Number of Arguments are Provided */
84 fprintf(stderr, "Missing All Required Inputs!!\n");
89 uint8_t cfg_index = 255;
90 uint8_t cfg_cipher = SIMON;
93 for (int j = 0; j < sizeof (valid_cfgs) / sizeof (valid_cfgs[0]); ++j) {
94 if (!strcmp(argv[1], valid_cfgs[j].str)) {
95 cfg_index = valid_cfgs[j].index;
96 cfg_cipher = valid_cfgs[j].cipher;
100 if (255 == cfg_index) {
101 fprintf(stderr, "Not a Valid Simon/Speck Cipher Configuration");
105 const uint16_t key_size_bits = key_sizes[cfg_index];
106 const uint8_t block_size_bits = block_sizes[cfg_index];
107 const uint8_t block_size = block_size_bits >> 3;
110 // Set Cipher Direction: -e -> Encrypt, -d -> Decrypt
111 if (!strncmp(argv[3], "-e", 2)) cfg_dir = ENCRYPT;
112 else if (!strncmp(argv[3], "-d", 2)) cfg_dir = DECRYPT;
114 fprintf(stderr, "Can Only Encrypt or Decrypt");
118 uint8_t (*initPtr)(SimSpk_Cipher *, enum cipher_config_t, enum mode_t, void *, uint8_t *, uint8_t *);
119 uint8_t (*operationPtr)(SimSpk_Cipher, const void *, void *);
121 if (SIMON == cfg_cipher) {
122 initPtr = &Simon_Init;
123 if (ENCRYPT == cfg_dir) operationPtr = &Simon_Encrypt;
124 else operationPtr = &Simon_Decrypt;
127 initPtr = &Speck_Init;
128 if (ENCRYPT == cfg_dir) operationPtr = &Speck_Encrypt;
129 else operationPtr = &Speck_Decrypt;
135 key_fd = fopen(argv[4],"rb");
136 fseek(key_fd, 0L, SEEK_END);
137 size_t key_file_size = (size_t)ftell(key_fd);
138 size_t key_size = key_size_bits >> 3;
139 printf("Key File Size (Bits): %zu\n", key_file_size*8);
140 if (key_size > key_file_size) {
141 fprintf(stderr, "Insufficient Key Bytes! Found:%zu Need %zu\n", key_file_size, key_size);
145 uint8_t *key_buffer = malloc((size_t)key_size);
146 fread(key_buffer, 1, key_size, key_fd);
150 // Read in Input Data to Allocated Array
152 in_fd = fopen(argv[5],"rb");
153 fseek(in_fd, 0L, SEEK_END);
154 size_t input_size = (size_t)ftell(in_fd);
155 printf("File Size: %zX\n", input_size);
157 uint8_t *working_buffer = malloc((size_t)input_size);
158 fread(working_buffer, 1, input_size, in_fd);
161 SimSpk_Cipher my_cipher;
162 int result = (*initPtr)(&my_cipher, (enum cipher_config_t)cfg_index, ECB, key_buffer, NULL, NULL);
164 fprintf(stderr, "Failed to Init Cipher\n");
167 printf("Key Size: %d\n", my_cipher.key_size);
168 printf("Block Size: %d\n", my_cipher.block_size);
171 uint8_t *backup_buffer;
172 double t=TimeStart();
175 for(int i=0;i<100;i++) {
177 *backup_buffer = working_buffer;
178 for(int block=0; block < input_size / block_size; block++){
179 (*operationPtr)(my_cipher, working_buffer, working_buffer);
180 working_buffer += block_size;
184 double time=TimeStop(t);
185 printf("ratio %e\n",(double)input_size*100/time);
188 out_fd = fopen(argv[6],"wb");
189 fwrite(backup_buffer, 1, input_size, out_fd);
192 /* Free ALl Buffers */
195 printf("All done!\n");
200 void print_usage(void){
201 printf("Simon/Speck User Tool\n");
202 printf("Version: %s\n", VERSION);
203 printf("usage: user <CIPHER_CONFIGURATION> <BLOCK_MODE> <CIPHER_DIRECTION> <KEYFILE> <INPUT_FILE> <OUTPUT>\n");