2016-04-14 15:27:11 +00:00
|
|
|
#include <stdio.h>
|
2016-04-15 20:30:41 +00:00
|
|
|
#include <string.h>
|
2016-04-15 21:03:02 +00:00
|
|
|
#include <stdlib.h>
|
2016-04-14 15:27:11 +00:00
|
|
|
#include "constants.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
|
2016-04-15 21:44:44 +00:00
|
|
|
u8 * read_from_file(FILE * stream, long * read_o) {
|
2016-04-15 21:03:02 +00:00
|
|
|
long u8_size = sizeof(u8);
|
|
|
|
long size = u8_size * 1000;
|
|
|
|
long read = 0;
|
2016-04-15 21:44:44 +00:00
|
|
|
long left_over = 0;
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * buffer = malloc( size );
|
2016-04-15 21:44:44 +00:00
|
|
|
for (int i = 0; fread(buffer + i, u8_size, 1, stream); i++ ) {
|
2016-04-15 21:03:02 +00:00
|
|
|
read += u8_size;
|
2016-04-15 21:44:44 +00:00
|
|
|
if ( read > size - 100) {
|
|
|
|
size += 1000;
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * tmp = realloc(buffer, size);
|
|
|
|
if (!tmp) {
|
|
|
|
perror("Error, not enough memory!");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
buffer = tmp;
|
|
|
|
}
|
|
|
|
};
|
2016-04-15 21:44:44 +00:00
|
|
|
left_over = read % 8;
|
|
|
|
for (int j = 1; j <= left_over; j++) { // make 8 bytes
|
|
|
|
buffer[read + j] = (char) 32;
|
|
|
|
}
|
|
|
|
*read_o = read + left_over;
|
2016-04-15 21:03:02 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-04-15 20:30:41 +00:00
|
|
|
void encrypt_file(char * filename, u8 * password) {
|
|
|
|
FILE * in = fopen(filename, "rb");
|
|
|
|
FILE * out = fopen("encrypted.speck", "wb");
|
|
|
|
if ( !in ) {
|
|
|
|
perror("Error opening file.");
|
2016-04-15 21:03:02 +00:00
|
|
|
return;
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
long read = 0;
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * buffer = read_from_file(in, &read);
|
2016-04-15 21:56:38 +00:00
|
|
|
for (int i = 0; i < read/8; i++) {
|
2016-04-15 21:44:44 +00:00
|
|
|
encrypt(buffer+i*8, password);
|
|
|
|
}
|
|
|
|
|
2016-04-15 21:03:02 +00:00
|
|
|
fwrite(buffer, sizeof(u8), read, out);
|
2016-04-15 21:49:31 +00:00
|
|
|
printf("%s\n", buffer);
|
2016-04-15 21:44:44 +00:00
|
|
|
if ( fclose(in) || fclose(out) ) {
|
|
|
|
perror("Error closing files!");
|
|
|
|
}
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void decrypt_file(char * filename, u8 * password) {
|
|
|
|
FILE * in = fopen(filename, "rb");
|
|
|
|
FILE * out = fopen("decrypted.speck", "wb");
|
|
|
|
if ( !in ) {
|
|
|
|
perror("Error opening file.");
|
2016-04-15 21:03:02 +00:00
|
|
|
return;
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
long read = 0;
|
|
|
|
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * buffer = read_from_file(in, &read);
|
2016-04-15 21:56:38 +00:00
|
|
|
for (int i = 0; i < read/8; i++) {
|
2016-04-15 21:44:44 +00:00
|
|
|
decrypt(buffer+i*8, password);
|
|
|
|
}
|
2016-04-15 21:03:02 +00:00
|
|
|
|
2016-04-15 21:44:44 +00:00
|
|
|
fwrite(buffer, sizeof(u8), read, out);
|
2016-04-15 21:49:31 +00:00
|
|
|
printf("%s\n", buffer);
|
2016-04-15 21:44:44 +00:00
|
|
|
if ( fclose(in) || fclose(out) ) {
|
|
|
|
perror("Error closing files!");
|
|
|
|
}
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
|
|
|
|
if ( argc != 4) {
|
|
|
|
printf("Invalid usage! Correct usage %s filname password [1/0 which represent"\
|
|
|
|
"encrypt or decrypt]\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * password = argv[2];
|
2016-04-15 21:49:31 +00:00
|
|
|
if (strlen(password) < 12) {
|
|
|
|
perror("Password must be at least 12 characters long!");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-15 20:30:41 +00:00
|
|
|
u8 inputKey[strlen(password) - 1];
|
2016-04-15 21:56:38 +00:00
|
|
|
|
|
|
|
for (int i = 0; password[i]; i++) {
|
2016-04-15 20:30:41 +00:00
|
|
|
inputKey[i] = password[i];
|
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
|
2016-04-15 20:30:41 +00:00
|
|
|
u8 keys[SPECK_BLOCK_SIZE/16*SPECK_ROUNDS];
|
|
|
|
encryptKeySchedule(inputKey, keys);
|
|
|
|
|
2016-04-15 21:03:02 +00:00
|
|
|
if ( !strcmp(argv[3], "1") ) {
|
|
|
|
encrypt_file(argv[1], keys);
|
|
|
|
} else {
|
|
|
|
decrypt_file(argv[1], keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-04-14 14:47:11 +00:00
|
|
|
}
|