-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsotpet_main.c
151 lines (132 loc) · 3.9 KB
/
sotpet_main.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
/* SOTPET - Simple One-Trick Pony Encryption Tool */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#if !BSD
#include "linuxfun.h"
#else
#include "bsdfun.h"
#endif
#include "sotpet.h"
#include "whirlpool.h"
#include "sotpet_trailer.h"
#include "sotpet_level2.hpp"
#include "buftools.h"
#define PASSBUF_LEN 512
const char * title = "Simple One-Trick-Pony Encryption Tool (sorbet) V%s\n";
const char * copylight = "(C)2023 Lusers' Malfunctioning Software Association aka. KJ Wolf\n";
const char * usage = "usage: %s -d|-e|-h {passwordfile} < {infile} > {outfile}\n"
" alt: %s -d|-e|-h {passwordfile} {infile} {outfile}\n"
" -d decryption\n"
" -e encryption\n"
/* " -p pw password\n"
" -P pf password file\n" */
" -h help\n"
"\n";
const char * note1 = "\t**ATTENTION**\n"
"Please be aware of that leaving your passwordfile undeleted / unerased / \n"
"unwiped on a usual persistent medium might get you into trouble.\n";
const char * help2 = "this tool accepts a pipe in and a pipe out\n";
const char * help4 = "Environment variables:\n\tSORBET_CPUS, SORBET_NUMBLOCKS [512], SORBET_BLOCKSIZE [1024],\n\tSORBET_USE_TRAILER [1]\n";
int main(int argc, char *argv[])
{
struct trailerset trailer;
void *sotpet;
bool encflg;
int i,r,res=0;
char passbuf[PASSBUF_LEN];
FILE *f;
char *p;
short cpus;
int numblocks = atoi(getenv_fb("SORBET_NUMBLOCKS", "512"));
int blocksize = atoi(getenv_fb("SORBET_BLOCKSIZE", "1024"));
bool use_trailer = atoi(getenv_fb("SORBET_USE_TRAILER", "1"));
int ifi = STDIN_FILENO;
int ofi = STDOUT_FILENO;
p = getenv("SORBET_CPUS");
cpus = p ? (atoi(p)) : getcpus();
fprintf(stderr, "CPUS=%hd\n", cpus);
fprintf(stderr, title, SOTPET_VERSION);
fputs(copylight, stderr);
if(argc>1 && !strcmp(argv[1],"-h"))
{
printf(usage, argv[0]);
puts(help2);
puts(help4);
fputs(note1, stdout);
return 1;
}
if(argc<3 && argc!=5)
{
fprintf(stderr, usage, argv[0]);
fputs(note1, stderr);
return 1;
}
encflg = !strcmp(argv[1],"-e");
if(!encflg && strcmp(argv[1],"-d"))
{
fprintf(stderr, "%s: %s: not recognized\n", argv[0], argv[1]);
return 9;
}
f=fopen(argv[2], "rt");
if(!f)
{
perror(argv[2]);
return 8;
}
if(!fgets(passbuf, PASSBUF_LEN, f))
{
perror(argv[2]);
return 7;
}
fclose(f);
if(argc>=5)
{
ifi = open(argv[3], O_RDONLY);
if(ifi<0)
{
perror(argv[3]);
return 11;
}
ofi = open(argv[4], O_WRONLY|O_EXCL|O_CREAT, 0600);
if(ofi<0)
{
perror(argv[4]);
return 10;
}
}
i=strlen(passbuf);
if(i>0 && passbuf[i-1]=='\n')
passbuf[--i]=0;
sotpet = sotpet_init(cpus, "test", passbuf, i, blocksize, 0, !encflg);
if(!sotpet)
{
fprintf(stderr, "sotpet_init() failed\n");
return 6;
}
r = sotpet_f2f_smart(encflg, ifi, ofi, cpus, numblocks, blocksize, use_trailer, &trailer, sotpet);
if(r)
{
fprintf(stderr, "sotpet_f2f_smart() failed (%d)\n", r);
return 5;
}
if(!memcmp(trailer.enc.magic, sotpet_magic_enc, MAGICSIZE) && !memcmp(trailer.enc.magic2, sotpet_magic2_enc, MAGICSIZE2))
{
fprintf(stderr, "trailer detected\n");
if(!memcmp(trailer.enc.hash, trailer.hash, HASHSIZE))
fprintf(stderr, "checksum okay\n");
else
{
fprintf(stderr, "checksum DIFFERS\n");
res=1;
}
}
else
res=encflg?0:2;
sotpet_exit(sotpet);
fprintf(stderr, "return value = %d\n", res);
return res;
}