-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfecrypt.c
123 lines (108 loc) · 2.31 KB
/
tfecrypt.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "tfdef.h"
#include "skein.h"
#include "tfe.h"
#include "tfsupport.h"
static char key[TF_KEY_SIZE];
static char srcblk[DATASIZE], dstblk[DATASIZE];
static struct skein sk;
static struct tfe_stream tfe;
static int will_exit;
static void usage(void)
{
printf("usage: tfecrypt key srcfile dstfile.\n");
printf("This program is part of Threefish stream cipher reference.\n");
exit(1);
}
static void xerror(const char *s)
{
perror(s);
exit(2);
}
int main(int argc, char **argv)
{
int ifd, ofd;
char *kfname, *infname, *onfname;
size_t lio, lrem, ldone, lblock;
char *pblk;
if (argc < 4) usage();
kfname = argv[1];
infname = argv[2];
onfname = argv[3];
if (!kfname || !infname || !onfname) usage();
if (!strcmp(kfname, "-")) ifd = 0;
else {
ifd = open(kfname, O_RDONLY);
if (ifd == -1) xerror(kfname);
}
skein_init(&sk, TF_NR_KEY_BITS);
will_exit = 0;
while (1) {
if (will_exit) break;
pblk = srcblk;
ldone = 0;
lrem = lblock = sizeof(srcblk);
_rkagain: lio = read(ifd, pblk, lrem);
if (lio == 0) will_exit = 1;
if (lio != NOSIZE) ldone += lio;
else xerror(kfname);
if (lio && lio < lrem) {
pblk += lio;
lrem -= lio;
goto _rkagain;
}
skein_update(&sk, srcblk, ldone);
}
skein_final(key, &sk);
if (ifd != 0) close(ifd);
if (!strcmp(infname, "-")) ifd = 0;
else {
ifd = open(infname, O_RDONLY);
if (ifd == -1) xerror(infname);
}
if (!strcmp(onfname, "-")) ofd = 1;
else {
ofd = creat(onfname, 0666);
if (ofd == -1) xerror(onfname);
}
tfe_init(&tfe, key);
will_exit = 0;
while (1) {
if (will_exit) break;
pblk = srcblk;
ldone = 0;
lrem = lblock = sizeof(srcblk);
_ragain: lio = read(ifd, pblk, lrem);
if (lio == 0) will_exit = 1;
if (lio != NOSIZE) ldone += lio;
else xerror(infname);
if (lio && lio < lrem) {
pblk += lio;
lrem -= lio;
goto _ragain;
}
tf_stream_crypt(&tfe, dstblk, srcblk, ldone);
pblk = dstblk;
lrem = ldone;
ldone = 0;
_wagain: lio = write(ofd, pblk, lrem);
if (lio != NOSIZE) ldone += lio;
else xerror(onfname);
if (lio < lrem) {
pblk += lio;
lrem -= lio;
goto _wagain;
}
}
tfe_emit(NULL, 0, &tfe);
close(ifd);
close(ofd);
return 0;
}