1 ////USAGE : ./user speck_256_128 ECB -e key lena.ppm tests.cip
4 //user simon_64_32 12 -e key tests tests.cip
7 // Created by Calvin McCoy on 9/2/17.
15 #include "cipher_constants.h"
21 $ ./user simon_256_128 ECB -e keyfile input output
30 #define VERSION "custom_build"
38 {"simon_64_32", 0, SIMON},
39 {"simon_72_48", 1, SIMON},
40 {"simon_96_48", 2, SIMON},
41 {"simon_96_64", 3, SIMON},
42 {"simon_128_64", 4, SIMON},
43 {"simon_96_96", 5, SIMON},
44 {"simon_144_96", 6, SIMON},
45 {"simon_128_128", 7, SIMON},
46 {"simon_192_128", 8, SIMON},
47 {"simon_256_128", 9, SIMON},
48 {"speck_64_32", 0, SPECK},
49 {"speck_72_48", 1, SPECK},
50 {"speck_96_48", 2, SPECK},
51 {"speck_96_64", 3, SPECK},
52 {"speck_128_64", 4, SPECK},
53 {"speck_96_96", 5, SPECK},
54 {"speck_144_96", 6, SPECK},
55 {"speck_128_128", 7, SPECK},
56 {"speck_192_128", 8, SPECK},
57 {"speck_256_128", 9, SPECK}
63 struct timeval tstart;
64 gettimeofday(&tstart,0);
65 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
68 double TimeStop(double t)
72 gettimeofday(&tend,0);
73 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
80 void print_usage(void);
83 int main(int argc, char *argv[]) {
85 /* Test That The Minumum Number of Arguments are Provided */
87 fprintf(stderr, "Missing All Required Inputs!!\n");
92 uint8_t cfg_index = 255;
93 uint8_t cfg_cipher = SIMON;
96 for (int j = 0; j < sizeof (valid_cfgs) / sizeof (valid_cfgs[0]); ++j) {
97 if (!strcmp(argv[1], valid_cfgs[j].str)) {
98 cfg_index = valid_cfgs[j].index;
99 cfg_cipher = valid_cfgs[j].cipher;
103 if (255 == cfg_index) {
104 fprintf(stderr, "Not a Valid Simon/Speck Cipher Configuration");
108 const uint16_t key_size_bits = key_sizes[cfg_index];
109 const uint8_t block_size_bits = block_sizes[cfg_index];
110 const uint8_t block_size = block_size_bits >> 3;
113 // Set Cipher Direction: -e -> Encrypt, -d -> Decrypt
114 if (!strncmp(argv[3], "-e", 2)) cfg_dir = ENCRYPT;
115 else if (!strncmp(argv[3], "-d", 2)) cfg_dir = DECRYPT;
117 fprintf(stderr, "Can Only Encrypt or Decrypt");
121 uint8_t (*initPtr)(SimSpk_Cipher *, enum cipher_config_t, enum mode_t, void *, uint8_t *, uint8_t *);
122 uint8_t (*operationPtr)(SimSpk_Cipher, const void *, void *);
124 if (SIMON == cfg_cipher) {
125 initPtr = &Simon_Init;
126 if (ENCRYPT == cfg_dir) operationPtr = &Simon_Encrypt;
127 else operationPtr = &Simon_Decrypt;
130 initPtr = &Speck_Init;
131 if (ENCRYPT == cfg_dir) operationPtr = &Speck_Encrypt;
132 else operationPtr = &Speck_Decrypt;
138 key_fd = fopen(argv[4],"rb");
139 fseek(key_fd, 0L, SEEK_END);
140 size_t key_file_size = (size_t)ftell(key_fd);
141 size_t key_size = key_size_bits >> 3;
142 printf("Key File Size (Bits): %zu\n", key_file_size*8);
143 if (key_size > key_file_size) {
144 fprintf(stderr, "Insufficient Key Bytes! Found:%zu Need %zu\n", key_file_size, key_size);
148 uint8_t *key_buffer = malloc((size_t)key_size);
149 fread(key_buffer, 1, key_size, key_fd);
153 // Read in Input Data to Allocated Array
155 in_fd = fopen(argv[5],"rb");
156 fseek(in_fd, 0L, SEEK_END);
157 size_t input_size = (size_t)ftell(in_fd);
158 printf("File Size: %zX\n", input_size);
160 uint8_t *working_buffer = malloc((size_t)input_size);
161 fread(working_buffer, 1, input_size, in_fd);
164 SimSpk_Cipher my_cipher;
165 int result = (*initPtr)(&my_cipher, (enum cipher_config_t)cfg_index, ECB, key_buffer, NULL, NULL);
167 fprintf(stderr, "Failed to Init Cipher\n");
170 printf("Key Size: %d\n", my_cipher.key_size);
171 printf("Block Size: %d\n", my_cipher.block_size);
174 uint8_t *backup_buffer;
175 uint8_t *start=working_buffer;
176 double t=TimeStart();
179 for(int i=0;i<100;i++) {
180 working_buffer=start;
181 backup_buffer = working_buffer;
182 for(int block=0; block < input_size / block_size; block++){
183 (*operationPtr)(my_cipher, working_buffer, working_buffer);
184 working_buffer += block_size;
188 double time=TimeStop(t);
189 printf("ratio %e\n",(double)input_size*100/time);
192 out_fd = fopen(argv[6],"wb");
193 fwrite(backup_buffer, 1, input_size, out_fd);
196 /* Free ALl Buffers */
199 printf("All done!\n");
204 void print_usage(void){
205 printf("Simon/Speck User Tool\n");
206 printf("Version: %s\n", VERSION);
207 printf("usage: user <CIPHER_CONFIGURATION> <BLOCK_MODE> <CIPHER_DIRECTION> <KEYFILE> <INPUT_FILE> <OUTPUT>\n");