-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpke_wrap.c
283 lines (267 loc) · 9.41 KB
/
hpke_wrap.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (c) Dan Harkins, 2020, 2021
*
* Copyright holder grants permission for redistribution and use in source
* and binary forms, with or without modification, provided that the
* following conditions are met:
* 1. Redistribution of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in all source files.
* 2. Redistribution in binary form must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* "DISCLAIMER OF LIABILITY
*
* THIS SOFTWARE IS PROVIDED BY DAN HARKINS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAN HARKINS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE."
*
* This license and distribution terms cannot be changed. In other words,
* this code cannot simply be copied and put under another distribution
* license (including the GNU public license).
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include "hpke.h"
static void
print_buffer (char *str, unsigned char *buf, int len)
{
int i;
printf("%s\n", str);
for (i = 0; i < len; i++) {
printf("%02x", buf[i]);
}
printf("\n");
}
/*
* convert a test vector character string into a usable octet string
*/
void
s2os (char *str, unsigned char **os, int *oslen)
{
int i, val;
char *ptr;
unsigned char *op;
*oslen = strlen(str)/2;
if ((*os = (unsigned char *)malloc(*oslen)) == NULL) {
fprintf(stderr, "unable to allocate os in s2os\n");
return;
}
memset(*os, 0, *oslen);
ptr = str;
op = *os;
for (i = 0; i < *oslen; i++) {
sscanf(ptr, "%02x", &val);
*op = val & 0xff;
op++;
ptr++; ptr++;
}
}
int
main (int argc, char **argv)
{
int c, b64 = 0, pad = 0;
hpke_ctx *ctx = NULL;
unsigned char *aad = NULL, *info = NULL, *pt = NULL, *ct = NULL, *pkR = NULL, *k = NULL, *enc;
int aad_len = 0, info_len = 0, pt_len = 0, ct_len = 0, pkR_len = 0, debug = 0, win = 0, enc_len;
unsigned char *b64enc, *b64ct;
int b64enc_len, b64ct_len;
for (;;) {
c = getopt(argc, argv, "a:i:p:c:k:d:bhw");
if (c < 0) {
break;
}
switch (c) {
case 'a':
s2os(optarg, &aad, &aad_len);
break;
case 'i':
s2os(optarg, &info, &info_len);
break;
case 'p':
pt = optarg;
pt_len = strlen(optarg);
ct_len += pt_len;
break;
case'k':
k = optarg;
break;
case 'd':
debug = atoi(optarg);
break;
case 'b':
b64 = 1;
break;
case 'w':
win = 1;
break;
case 'h':
default:
fprintf(stderr, "USAGE: %s [-aikspbh]\n"
"\t-a some AAD to include in the wrapping\n"
"\t-i some info to include in the wrapping\n"
"\t-k the recipient's public key\n"
"\t-p the plaintext to wrap\n"
"\t-b base64 encode the output (and base64 decode what's in -k)\n"
"\t-w include sequence number in ciphertext for receiver window\n"
"\t-h this help message\n",
argv[0]);
exit(1);
}
}
if ((pt == NULL) || (k == NULL)) {
fprintf(stderr, "%s: at a minimum you need to specify plaintext, and a recipient public key\n",
argv[0]);
exit(1);
}
if (b64) {
if ((pkR = (unsigned char *)malloc(strlen(k))) == NULL) {
fprintf(stderr, "%s: unable to allocate space for recipient's public key!\n", argv[0]);
exit(1);
}
memset(pkR, 0, strlen(k));
pkR_len = EVP_DecodeBlock(pkR, k, strlen(k));
pad = strlen(k);
while (k[pad - 1] == '=') {
pkR_len--;
pad--;
}
} else {
s2os(k, &pkR, &pkR_len);
}
/*
* the recipient public key dictates our ephemeral key and therefore the KEM.
* For simplicity, don't allow for a different KDF to be used, just use the
* hash algorithm from the KEM.
*/
switch (pkR_len) {
case 32:
/*
* compact p256
*/
if ((ctx = create_hpke_context(MODE_BASE, DHKEM_CP256,
HKDF_SHA_256, AES_256_SIV)) == NULL) {
fprintf(stderr, "%s: can't create HPKE context!\n", argv[0]);
exit(1);
}
win = 0; // unset if set, this is deterministic so no window needed
break;
case 48:
/*
* compact p384
*/
if ((ctx = create_hpke_context(MODE_BASE, DHKEM_CP384,
HKDF_SHA_384, AES_512_SIV)) == NULL) {
fprintf(stderr, "%s: can't create HPKE context!\n", argv[0]);
exit(1);
}
win = 0; // unset if set, this is deterministic so no window needed
break;
case 65:
/*
* uncompressed p256
*/
if ((ctx = create_hpke_context(MODE_BASE, DHKEM_P256,
HKDF_SHA_256, AES_128_GCM)) == NULL) {
fprintf(stderr, "%s: can't create HPKE context!\n", argv[0]);
exit(1);
}
break;
case 66:
/*
* compact p521
*
*/
if ((ctx = create_hpke_context(MODE_BASE, DHKEM_CP521,
HKDF_SHA_512, AES_512_SIV)) == NULL) {
fprintf(stderr, "%s: can't create HPKE context!\n", argv[0]);
exit(1);
}
win = 0; // unset if set, this is deterministic so no window needed
break;
case 97:
/*
* uncompressed p384
*/
if ((ctx = create_hpke_context(MODE_BASE, DHKEM_P384,
HKDF_SHA_384, AES_256_GCM)) == NULL) {
fprintf(stderr, "%s: can't create HPKE context!\n", argv[0]);
exit(1);
}
break;
case 133:
/*
* uncompressed p521
*/
if ((ctx = create_hpke_context(MODE_BASE, DHKEM_P521,
HKDF_SHA_512, AES_256_GCM)) == NULL) {
fprintf(stderr, "%s: can't create HPKE context!\n", argv[0]);
exit(1);
}
break;
default:
fprintf(stderr, "%s: unknown public key size, %d\n", argv[0], pkR_len);
exit(1);
}
set_hpke_debug(ctx, debug);
if (win) {
ct_len += 4;
set_hpke_recv_window(ctx, 1); /* prepend sequence number to cipher text */
}
if (sender(ctx, pkR, pkR_len, info, info_len, NULL, 0, NULL, 0, &enc, &enc_len) < 1) {
fprintf(stderr, "%s: can't do encap!\nTry again with -f maybe\n", argv[0]);
exit(1);
}
if ((ct = malloc(ct_len + 16)) == NULL) {
fprintf(stderr, "can't allocate space for ciphertext!\n");
exit(1);
}
if (wrap(ctx, aad, aad_len, pt, pt_len, ct+16, ct) != ct_len) {
fprintf(stderr, "expected to wrap %d....\n", ct_len);
// don't make this fatal, it won't unwrap if it's garbage
}
if (b64) {
if ((b64enc = (unsigned char *)malloc(enc_len*2)) == NULL) {
fprintf(stderr, "%s: unable to allocate room to encode ephemeral public key!\n", argv[0]);
exit(1);
}
memset(b64enc, 0, (enc_len*2));
b64enc_len = EVP_EncodeBlock(b64enc, enc, enc_len);
if ((b64ct = (unsigned char *)malloc((ct_len+16)*2)) == NULL) {
fprintf(stderr, "%s: unable to allocate room to encode ephemeral public key!\n", argv[0]);
exit(1);
}
memset(b64ct, 0, (ct_len+16)*2);
b64ct_len = EVP_EncodeBlock(b64ct, ct, ct_len + 16);
/*
* do some pseudo-PEM nonsense to pretty-print this goo
*/
printf("------ BEGIN ENC -------\n%s\n"
"-------- END ENC -------\n"
"------ BEGIN CT --------\n%s\n"
"-------- END CT --------\n", b64enc, b64ct);
free(b64enc);
free(b64ct);
} else {
print_buffer("enc:", enc, enc_len);
print_buffer("ct:", ct, ct_len + 16);
}
free_hpke_context(ctx);
free(ct);
free(enc);
free(pkR);
exit(0);
}