-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsbk-read.c
145 lines (120 loc) · 3.12 KB
/
sbk-read.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
/*
* Copyright (c) 2018 Tim van der Molen <tim@kariliq.nl>
*
* 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"
int
sbk_grow_buffers(struct sbk_ctx *ctx, size_t size)
{
unsigned char *buf;
if (size <= ctx->ibufsize)
return 0;
if (ctx->ibufsize <= (SIZE_MAX - EVP_MAX_BLOCK_LENGTH) / 2 &&
size <= ctx->ibufsize * 2)
size = ctx->ibufsize * 2;
else if (size > SIZE_MAX - EVP_MAX_BLOCK_LENGTH) {
warnx("Buffer size too large");
return -1;
}
if ((buf = realloc(ctx->ibuf, size)) == NULL) {
warn(NULL);
return -1;
}
ctx->ibuf = buf;
ctx->ibufsize = size;
size += EVP_MAX_BLOCK_LENGTH;
if ((buf = realloc(ctx->obuf, size)) == NULL) {
warn(NULL);
return -1;
}
ctx->obuf = buf;
ctx->obufsize = size;
return 0;
}
int
sbk_decrypt_init(struct sbk_ctx *ctx, uint32_t counter)
{
if (!HMAC_Init_ex(ctx->hmac_ctx, NULL, 0, NULL, NULL)) {
warnx("Cannot initialise MAC context");
return -1;
}
ctx->iv[0] = counter >> 24;
ctx->iv[1] = counter >> 16;
ctx->iv[2] = counter >> 8;
ctx->iv[3] = counter;
if (!EVP_DecryptInit_ex(ctx->cipher_ctx, NULL, NULL, ctx->cipher_key,
ctx->iv)) {
warnx("Cannot initialise cipher context");
return -1;
}
return 0;
}
int
sbk_decrypt_update(struct sbk_ctx *ctx, size_t ibuflen, size_t *obuflen)
{
int len;
if (!HMAC_Update(ctx->hmac_ctx, ctx->ibuf, ibuflen)) {
warnx("Cannot compute MAC");
return -1;
}
if (ibuflen > (unsigned int)(INT_MAX - EVP_MAX_BLOCK_LENGTH)) {
warnx("Ciphertext buffer too large");
return -1;
}
if (!EVP_DecryptUpdate(ctx->cipher_ctx, ctx->obuf, &len, ctx->ibuf,
ibuflen)) {
warnx("Cannot decrypt data");
return -1;
}
*obuflen = len;
return 0;
}
int
sbk_decrypt_final(struct sbk_ctx *ctx, size_t *obuflen,
const unsigned char *theirmac)
{
unsigned char ourmac[EVP_MAX_MD_SIZE];
unsigned int ourmaclen;
int len;
if (!HMAC_Final(ctx->hmac_ctx, ourmac, &ourmaclen)) {
warnx("Cannot compute MAC");
return -1;
}
if (memcmp(ourmac, theirmac, SBK_MAC_LEN) != 0) {
warnx("MAC mismatch");
return -1;
}
if (!EVP_DecryptFinal_ex(ctx->cipher_ctx, ctx->obuf + *obuflen,
&len)) {
warnx("Cannot decrypt data");
return -1;
}
*obuflen += len;
return 0;
}
int
sbk_read(struct sbk_ctx *ctx, void *ptr, size_t size)
{
if (fread(ptr, size, 1, ctx->fp) != 1) {
if (ferror(ctx->fp))
warn(NULL);
else
warnx("Unexpected end of file");
return -1;
}
return 0;
}