-
Notifications
You must be signed in to change notification settings - Fork 9
/
sha256_app.c
181 lines (160 loc) · 4.38 KB
/
sha256_app.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#define BLOCK_BITS 512
#define BLOCK_BYTES 64
#define MAX_BITS 448
#define WORD_BITS 32
#define WORD_BYTES 4
#define WORDS_IN_BLOCK 16
#define LEN_BITS 64
#define DATA_IN_ADDR 0
#define STATUS_ADDR 4
#define DATA_OUT_ADDR 8
const char msg_last = 8;
const char hash_ack = 4;
const char rst_status = 0;
int main(){
unsigned char * sha_msg, * sha_msg_or;
unsigned int len_max = 64;
unsigned int cur_len;
unsigned int i, j;
uint32_t num_blocks;
unsigned char * blocks;
uint64_t num_bits;
uint32_t hash[8];
uint32_t status, data_in;
uint32_t word_to_send;
int c;
FILE * fp = fopen("header.txt", "r");
if(NULL == fp) {
perror("header file open\n");
exit(-1);
}
while ((c = getc(fp)) != EOF){
putchar(c);
}
fclose(fp);
int dev = open("/dev/sha256", O_RDWR);
if(dev < 0) {
perror("file open\n");
exit(-1);
}
puts(">> Author : Martina Fogliato\n>> Eurecom 2018\n>> Digital Systems");
while(1){
i = 0;
cur_len = len_max;
sha_msg = (char*)malloc(len_max*sizeof(char));
if(sha_msg == NULL) {
exit(-1);
}
sha_msg_or = sha_msg;
puts(">> Insert the text to be hashed, press enter to finish : \n");
while (( c = getchar() ) != '\n' && c != EOF){
sha_msg[i++]=(char)c;
if(i == cur_len){
cur_len += len_max;
sha_msg = realloc(sha_msg, cur_len);
if(sha_msg == NULL) {
perror("malloc");
exit(-1);
}
}
}
if(c == '\n' && i == 0) {
puts(">> Bye bye!");
free(sha_msg);
free(blocks);
close(dev);
return 0;
}
sha_msg[i] = '\0';
//padding
num_bits = i*8;
num_blocks = (num_bits + LEN_BITS + 1)/BLOCK_BITS + 1;
//printf("num_bits = %ld, num_blocks = %d\n", num_bits, num_blocks);
blocks = malloc(num_blocks * BLOCK_BYTES * sizeof(char));
if(blocks == NULL) {
perror("malloc");
exit(-1);
}
memset(blocks, 0, num_blocks * BLOCK_BYTES * sizeof(char));
memcpy(blocks, sha_msg_or, i);
blocks[i] = (char)0x80;
if((pwrite(dev, &rst_status, WORD_BYTES, STATUS_ADDR)) < 4){
puts("fail to reset status register");
}
for(i=0; i<(num_blocks*WORDS_IN_BLOCK-2); i++){
if((pread(dev, &status, WORD_BYTES, STATUS_ADDR)) < 4) {
puts("fail to read status register");
}
//check the device is ready and send one word
while(0x01 != (status & 0x00000001));
word_to_send = 0;
for(j=0; j<4; j++){
//printf("blocks[%d] = %02x\n", j+i*4, blocks[j+i*4]);
word_to_send |= ((uint32_t)blocks[j+i*4] << (32-8-j*8));
}
//printf("%x\n", word_to_send);
if((pwrite(dev, &word_to_send, WORD_BYTES, DATA_IN_ADDR))< 4){
puts("fail to write word");
}
}
word_to_send = 0;
word_to_send = ((uint32_t*)&num_bits)[1];
//printf("%x\n", word_to_send);
if((pread(dev, &status, WORD_BYTES, STATUS_ADDR)) < 4) {
puts("fail to read status register");
}
//check the device is ready and send one word
while(0x01 != (status & 0x00000001));
if((pwrite(dev, &word_to_send, WORD_BYTES, DATA_IN_ADDR)) < 4){
printf("fail to write block[%d]\n", i);
}
//signal the last word
if((pwrite(dev, &msg_last, WORD_BYTES, STATUS_ADDR)) < 4){
puts("fail to set bit 3 of status register");
}
//check the device is ready and send the last word
if((pread(dev, &status, WORD_BYTES, STATUS_ADDR)) < 4) {
puts("fail to read status register");
}
while(0x01 != (status & 0x00000001));
word_to_send = 0;
word_to_send = ((uint32_t*)&num_bits)[0];
//printf("%x\n", word_to_send);
if((pwrite(dev, &word_to_send, WORD_BYTES, DATA_IN_ADDR)) < 4){
puts("fail to write word.");
}
if((pread(dev, &status, WORD_BYTES, STATUS_ADDR)) < 4) {
puts("fail to read status register");
}
//wait that the hash is valid
while(0x3 != (status & 0x00000003));
//read the hash words
for(i=0; i<8; i++){
if((pread(dev, &hash[i], WORD_BYTES, DATA_OUT_ADDR+i*4)) < 4) {
puts("fail to read data out register");
}
}
//signal the hash has been read
if((pwrite(dev, &hash_ack, WORD_BYTES, STATUS_ADDR)) < 4) {
puts("fail to write bit 4 of status register");
}
printf(">> Hash : ");
for(i=0; i<8; i++){
printf("%x ", hash[i]);
}
puts("");
//go back to rst
if((pwrite(dev, &rst_status, WORD_BYTES, STATUS_ADDR)) < 4){
puts("fail to reset status register");
}
free(sha_msg);
}
}