-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfcrypt.c
148 lines (131 loc) · 3.26 KB
/
tfcrypt.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
#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 "tfsupport.h"
#define SECTSIZE 512
static char key[TF_KEY_SIZE], xtskey[TF_KEY_SIZE], ctr[TF_BLOCK_SIZE];
static char srcblk[DATASIZE], dstblk[DATASIZE];
static struct skein sk;
static int will_exit;
static void usage(void)
{
printf("usage: tfcrypt <-e/-d/-we/-wd> key srcfile dstfile.\n");
printf("-e: encrypt, -d: decrypt.\n");
printf("-we: encrypt with skein, -wd: decrypt with skein.\n");
printf("Encrypts srcfile into dstfile with key using XTS mode.");
printf("The length of rawkey for -e/-d must be twice the length of cipher key.");
printf("This program is part of Threefish cipher reference.\n");
exit(1);
}
static void xerror(const char *s)
{
perror(s);
exit(2);
}
int main(int argc, char **argv)
{
int ifd, ofd;
int encryptf;
char *kfname, *infname, *onfname;
size_t lio, lrem, ldone, lblock;
char *pblk;
if (argc < 5) usage();
if (!strcmp(argv[1], "-e")) encryptf = 1;
else if (!strcmp(argv[1], "-d")) encryptf = 2;
else if (!strcmp(argv[1], "-we")) encryptf = 3;
else if (!strcmp(argv[1], "-wd")) encryptf = 4;
else usage();
kfname = argv[2];
infname = argv[3];
onfname = argv[4];
if (!kfname || !infname || !onfname) usage();
if (!strcmp(kfname, "-")) ifd = 0;
else {
ifd = open(kfname, O_RDONLY);
if (ifd == -1) xerror(kfname);
}
if (encryptf < 3) goto _rawkey;
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);
skein_init(&sk, TF_MAX_BITS);
skein_update(&sk, key, TF_KEY_SIZE);
skein_final(xtskey, &sk);
tf_convkey(key);
tf_convkey(xtskey);
goto _keydone;
_rawkey:
read(ifd, key, sizeof(key));
tf_convkey(key);
read(ifd, xtskey, sizeof(xtskey));
tf_convkey(xtskey);
_keydone:
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);
}
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;
}
if (encryptf == 1 || encryptf == 3)
tf_xts_encrypt(key, xtskey, ctr, dstblk, srcblk, ldone, TF_BLOCKS_FROM_BYTES(SECTSIZE));
else if (encryptf == 2 || encryptf == 4)
tf_xts_decrypt(key, xtskey, ctr, dstblk, srcblk, ldone, TF_BLOCKS_FROM_BYTES(SECTSIZE));
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;
}
}
close(ifd);
close(ofd);
return 0;
}