-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
211 lines (189 loc) · 5.33 KB
/
main.cpp
File metadata and controls
211 lines (189 loc) · 5.33 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Lib Includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <sqlite3.h>
#include <pthread.h>
#include <sys/inotify.h>
#include <fcntl.h>
/* Header Files */
#include "include/OLED_CONTROLER.h"
#include "include/bio_reader.h"
#include "include/sodium.h"
//file Path definition
#define keyPath "keys/key"
#define gpioPath "/sys/class/gpio/gpio60/value"
typedef unsigned char BYTE;
typedef unsigned long DWORD;
void *threadCrypto(void *key);
int main()
{
bool matched;
DWORD *score;
BYTE *templateBuffer1;
templateBuffer1 = (BYTE *)malloc(800); // area of the reader x * y
BYTE *templateBuffer2;
templateBuffer2 = (BYTE *)malloc(800);
BYTE *imageBuffer;
imageBuffer = (BYTE *)malloc(400 * 800);
printf("\n-------------------------------------\n");
///////////////////////q
printf("\n-------------------------------------\n");
printf("TCC2 - Claudio Leon\n");
printf("-------------------------------------\n");
printf("Iniciando bloco criptografico...\n");
///// CRIPTO ////
if (sodium_init() == -1)
{
printf("\nFalha na inicialização da biblioteca de criptografia");
}
else
{
printf("\nBiblioteca de criptografia inicializada");
}
unsigned char key[crypto_secretbox_KEYBYTES];
unsigned char nonce[crypto_secretbox_NONCEBYTES];
FILE *fp = fopen(keyPath, "rb");
if (fp == NULL)
{
printf("\n--- VIOLATED READER - STOP ---\n");
write_oled("Device violated!!.",1,0,1);
//printf("\nChave não criada, criando nova chave criptografica...\n");
//crypto_secretbox_keygen(key); // cria nova chave
//fp = fopen("keys/key", "wb");
//fwrite(key, sizeof key[0], crypto_secretbox_KEYBYTES, fp);
//fclose(fp);
exit(1);
}
else
{
printf("\nChave criptografica encontrada.\n");
fread(key, sizeof key[0], crypto_secretbox_KEYBYTES, fp);
fclose(fp);
}
fp = fopen("keys/nonce", "rb");
if (fp == NULL)
{
printf("\nNonce não criada, criando novo valor...");
randombytes_buf(nonce, sizeof nonce); // cria nonce
fp = fopen("keys/nonce", "wb");
fwrite(nonce, sizeof nonce[0], crypto_secretbox_NONCEBYTES, fp);
fclose(fp);
}
else
{
printf("\nNonce criptografica encontrada.");
fgets((char *)nonce, sizeof(nonce), fp);
fread(nonce, sizeof nonce[0], crypto_secretbox_NONCEBYTES, fp);
fclose(fp);
}
init_oled();
write_oled("TCC2 - Claudio Leon",1,0,1);//(x, y, tamanho fonte)
printf("\n-------------------------------------\n");
printf("Iniciando leito biometrico...\n");
if (errno = open_reader()) // inicia o leitor
{
printf("Failed to open biometric reader: %s.\n", strerror(errno));
exit(1);
}
else
{
printf("\nSucesso em abrir o leitor\n");
printf("\n-------------------------------------\n");
write_oled("Leitor iniciado",1,0,1);
}
// THREAD
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, threadCrypto, key);
// menu implementation
int command;
write_oled("Selecione sua opcao",1,0,1);
printf("\n\n\n-- MENU --\n");
printf("r - Register new fingerprint\n");
printf("m - Match a fingerprint\n");
printf("d - Delete a fingerprint\n");
printf("c - Close program\n");
while ((command = getchar()) != EOF)
{
printf("\n\n\n-- MENU --\n");
printf("r - Register new fingerprint\n");
printf("m - Match a fingerprint\n");
printf("d - Delete a fingerprint\n");
printf("c - Close program\n");
switch (command)
{
case 'r':
write_oled("Registro - Selecionado",1,0,1);
errno = read_finger(imageBuffer);
if (errno != 0)
{
printf("Error reading fingerprint: %s.\n", strerror(errno));
return (-1);
}
errno =
create_template(imageBuffer, templateBuffer1, key, nonce, true);
if (errno != 0)
{
printf("Error creating template error: %s.\n",
strerror(errno));
return (-1);
}
write_oled("Register Sucess",1,0,1);
break;
case 'm':
write_oled("Match - Selecionado",1,0,1);
read_finger(imageBuffer);
create_template(imageBuffer, templateBuffer1, key, nonce, false); // so desejo o buffer para comparar
matched = match_finger(templateBuffer1, score, key, nonce);
if (matched == true)
{
write_oled("Match found.",1,0,1);
}
else
{
write_oled("Match NOT found.",1,0,1);
}
break;
case 'c':
write_oled("Obrigado",1,0,1);
close_reader();
free(imageBuffer);
free(templateBuffer1);
free(templateBuffer2);
pthread_exit(NULL);
printf("\nThanks!!");
return 0;
}
}
return 0;
}
void *threadCrypto(void *key)
{
char GPIOStatus[10];
printf("Watching : %s\n",gpioPath);
while(1)
{
FILE *fp = fopen("/sys/class/gpio/gpio60/value", "rb");
if (fp == NULL)
{
printf("\nFalha na leitura do GPIO...\n");
}
fread(GPIOStatus, sizeof(int), 1, fp);
char *buf;
long status = strtol(GPIOStatus,&buf, 10);
if (status == 1)
{
printf("--- TAMPER-PROOF DETECTED TERMINATING CRYPTOGRAPH KEY AND CLOSING PROGRAM ---\n");
system("rm -f /home/debian/Biometric_crypto/keys/key");
write_oled("Device violated!!.",1,0,1);
exit(1);
}
fclose(fp);
sleep(0.1);
}
}