-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-utility.c
60 lines (55 loc) · 1.75 KB
/
file-utility.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <sodium.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define OUTPUT_ERROR 1
#define INPUT_ERROR 2
#define FILE_CLOSE_ERROR 3
#define FILE_ACCESS_ERROR 4
#define FILENAME_CONSTRUCTION_ERROR 5
#define TERMINAL_CONTROL_ERROR 6
#define CRYPTO_INITIALIZATION_ERROR 7
#define KEY_GENERATION_ERROR 8
#define ENCRYPTION_ERROR 9
#define DECRYPTION_ERROR 10
#define KEY_SIZE crypto_secretbox_KEYBYTES
#define MAX_NAME_SIZE 20
#define TIME_SIZE 20
#define NONCE_SIZE crypto_secretbox_NONCEBYTES
#define REQUEST_SIZE MAX_NAME_SIZE*2 + 2
#define KEY_REQUEST_LEN REQUEST_SIZE + KEY_SIZE + TIME_SIZE
#define KEY_CIPHERTEXT_LEN crypto_secretbox_MACBYTES + KEY_REQUEST_LEN
#define MESSAGE_LEN 256
#define MESSAGE_CIPHERTEXT_LEN crypto_secretbox_MACBYTES + MESSAGE_LEN
//Reades a key from a file determined by the trusted and principal names: /trusted/principal.key
int read_key_from_file(unsigned char* folder_name, size_t folder_name_size, unsigned char* principal, unsigned char* key){
//constructing file path
char keypath[MAX_NAME_SIZE+5] = "";
strncat(keypath, (char*)folder_name, folder_name_size);
strcat(keypath, "/");
strcat(keypath, (char*)principal);
strcat(keypath, ".key");
//print path for debugging
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
//opening file
FILE * keyfile = fopen(keypath, "r");
if (keyfile == NULL) {
perror("File Open Failed: ");
return FILE_ACCESS_ERROR;
}
/* Seek to the beginning of the file */
fseek(keyfile, 0, SEEK_SET);
if (fread(key, 1, KEY_SIZE, keyfile) != KEY_SIZE){
if (feof(keyfile)){
printf("End of file was reached.\n");
}
if (ferror(keyfile)){
printf("An error occurred.\n");
}
fclose(keyfile);
return FILE_ACCESS_ERROR;
}
fclose(keyfile);
return 0;
}