-
Notifications
You must be signed in to change notification settings - Fork 10
/
sbk-file.c
176 lines (134 loc) · 3.7 KB
/
sbk-file.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
/*
* Copyright (c) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "sbk-internal.h"
void
sbk_free_file(struct sbk_file *file)
{
free(file);
}
int
sbk_write_file(struct sbk_ctx *ctx, struct sbk_file *file, FILE *fp)
{
size_t ibuflen, len, obuflen;
unsigned char mac[SBK_MAC_LEN];
if (sbk_grow_buffers(ctx, BUFSIZ) == -1)
return -1;
if (fseeko(ctx->fp, file->pos, SEEK_SET) == -1) {
warn("Cannot seek");
return -1;
}
if (sbk_decrypt_init(ctx, file->counter) == -1)
return -1;
if (!HMAC_Update(ctx->hmac_ctx, ctx->iv, SBK_IV_LEN)) {
warnx("Cannot compute MAC");
return -1;
}
for (len = file->len; len > 0; len -= ibuflen) {
ibuflen = (len < BUFSIZ) ? len : BUFSIZ;
if (sbk_read(ctx, ctx->ibuf, ibuflen) == -1)
return -1;
if (sbk_decrypt_update(ctx, ibuflen, &obuflen) == -1)
return -1;
if (fp != NULL && fwrite(ctx->obuf, obuflen, 1, fp) != 1) {
warn("Cannot write file");
return -1;
}
}
if (sbk_read(ctx, mac, sizeof mac) == -1)
return -1;
obuflen = 0;
if (sbk_decrypt_final(ctx, &obuflen, mac) == -1)
return -1;
if (obuflen > 0 && fp != NULL && fwrite(ctx->obuf, obuflen, 1, fp) !=
1) {
warn("Cannot write file");
return -1;
}
return 0;
}
static char *
sbk_decrypt_file_data(struct sbk_ctx *ctx, struct sbk_file *file,
size_t *buflen, int terminate)
{
size_t ibuflen, len, obuflen, obufsize;
unsigned char mac[SBK_MAC_LEN];
char *obuf, *ptr;
if (buflen != NULL)
*buflen = 0;
if (sbk_grow_buffers(ctx, BUFSIZ) == -1)
return NULL;
if (fseeko(ctx->fp, file->pos, SEEK_SET) == -1) {
warn("Cannot seek");
return NULL;
}
if (terminate)
terminate = 1;
if ((size_t)file->len > SIZE_MAX - EVP_MAX_BLOCK_LENGTH - terminate) {
warnx("File too large");
return NULL;
}
obufsize = file->len + EVP_MAX_BLOCK_LENGTH + terminate;
if ((obuf = malloc(obufsize)) == NULL) {
warn(NULL);
return NULL;
}
if (sbk_decrypt_init(ctx, file->counter) == -1)
goto error;
if (!HMAC_Update(ctx->hmac_ctx, ctx->iv, SBK_IV_LEN)) {
warnx("Cannot compute MAC");
goto error;
}
ptr = obuf;
for (len = file->len; len > 0; len -= ibuflen) {
ibuflen = (len < BUFSIZ) ? len : BUFSIZ;
if (sbk_read(ctx, ctx->ibuf, ibuflen) == -1)
goto error;
if (sbk_decrypt_update(ctx, ibuflen, &obuflen) == -1)
goto error;
memcpy(ptr, ctx->obuf, obuflen);
ptr += obuflen;
}
if (sbk_read(ctx, mac, sizeof mac) == -1)
goto error;
obuflen = 0;
if (sbk_decrypt_final(ctx, &obuflen, mac) == -1)
goto error;
if (obuflen > 0) {
memcpy(ptr, ctx->obuf, obuflen);
ptr += obuflen;
}
if (terminate)
*ptr = '\0';
if (buflen != NULL)
*buflen = ptr - obuf;
return obuf;
error:
free(obuf);
return NULL;
}
char *
sbk_get_file_data(struct sbk_ctx *ctx, struct sbk_file *file, size_t *len)
{
return sbk_decrypt_file_data(ctx, file, len, 0);
}
char *
sbk_get_file_data_as_string(struct sbk_ctx *ctx, struct sbk_file *file)
{
return sbk_decrypt_file_data(ctx, file, NULL, 1);
}