-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoeod.c
187 lines (169 loc) · 5 KB
/
oeod.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
/*
* Copyright 2021 Stephen Farrell. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/**
* @file
*
* A round-trip test using my new EVP mode for the sender ephemeral to encrypt and my code to decrypt.
*
*/
/*
* Openssl includes
*/
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>
#include <openssl/evp.h>
#include <openssl/ssl.h>
/*
* Happykey include
*/
#include "hpke.h"
#define MEMCHAR 0xfa
#ifndef OSSL_HPKE_MAXSIZE
#define OSSL_HPKE_MAXSIZE 1024
#endif
#ifndef OSSL_HPKE_DEFSIZE
#define OSSL_HPKE_DEFSIZE (4 * 1024)
#endif
/*!
* @brief for odd/occasional debugging
*
* @param fout is a FILE * to use
* @param msg is prepended to print
* @param buf is the buffer to print
* @param blen is the length of the buffer
* @return 1 for success
*/
static int neod_pbuf(char *msg,unsigned char *buf,size_t blen)
{
if (!msg) {
printf("NULL msg:");
} else {
printf("%s (%lu): ",msg,blen);
}
if (!buf) {
printf("buf is NULL, so probably something wrong\n");
return 1;
}
if (blen==OSSL_HPKE_MAXSIZE) {
printf("length is OSSL_HPKE_MAXSIZE, so probably unused\n");
return 1;
}
if (blen==0) {
printf("length is 0, so probably something wrong\n");
return 1;
}
size_t i=0;
for (i=0;i<blen;i++) {
printf("%02x",buf[i]);
}
printf("\n");
return 1;
}
int main(int argc, char **argv)
{
/*
* Generate a key pair
*/
int hpke_mode=OSSL_HPKE_MODE_BASE;
OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
hpke_suite.kem_id=OSSL_HPKE_KEM_ID_P384;
size_t publen=OSSL_HPKE_MAXSIZE; unsigned char pub[OSSL_HPKE_MAXSIZE];
memset(pub,MEMCHAR,publen);
size_t privlen=OSSL_HPKE_MAXSIZE; unsigned char priv[OSSL_HPKE_MAXSIZE];
memset(priv,MEMCHAR,privlen);
EVP_PKEY *privevp=NULL;
int rv=OSSL_HPKE_keygen(NULL, NULL, hpke_mode, hpke_suite,
NULL, 0, pub, &publen, &privevp);
if (rv!=1) {
fprintf(stderr,"Error (%d) from OSSL_HPKE_keygen (receiver)\n",rv);
exit(1);
}
neod_pbuf("receiver pub",pub,publen);
EVP_PKEY *senderpriv=NULL;
size_t senderpublen=OSSL_HPKE_MAXSIZE; unsigned char senderpub[OSSL_HPKE_MAXSIZE];
rv=OSSL_HPKE_keygen(NULL, NULL, hpke_mode, hpke_suite,
NULL, 0, senderpub, &senderpublen, &senderpriv);
if (rv!=1) {
fprintf(stderr,"Error (%d) from OSSL_HPKE_keygen (sender)\n",rv);
exit(1);
}
neod_pbuf("sender pub",senderpub,senderpublen);
/*
* Setup AAD/Info buffers etc.
*/
size_t aadlen=OSSL_HPKE_MAXSIZE; unsigned char aad[OSSL_HPKE_MAXSIZE];
size_t infolen=OSSL_HPKE_MAXSIZE; unsigned char info[OSSL_HPKE_MAXSIZE];
size_t cipherlen=OSSL_HPKE_MAXSIZE; unsigned char cipher[OSSL_HPKE_MAXSIZE];
size_t psklen=0; unsigned char *psk=NULL; char *pskid=NULL;
size_t clearlen=OSSL_HPKE_MAXSIZE; unsigned char clear[OSSL_HPKE_MAXSIZE];
/*
* Initial values
*/
#define INFO (char*) "The Info"
memset(info,MEMCHAR,infolen);
infolen=strlen(INFO); memcpy(info,INFO,strlen(INFO)); neod_pbuf("info",info,infolen);
#define AAD (char*) "aad aad aad lots and lots of aad"
memset(aad,MEMCHAR,aadlen);
aadlen=strlen(AAD); memcpy(aad,AAD,strlen(AAD)); neod_pbuf("aad",aad,aadlen);
#define MESSAGE (char*)"we need another trip to the bottle bank"
memset(clear,MEMCHAR,clearlen);
clearlen=strlen(MESSAGE); memcpy(clear,MESSAGE,strlen(MESSAGE)); neod_pbuf("clear",clear,clearlen);
memset(cipher,MEMCHAR,cipherlen);
/*
* Call EVP mode encrypt
*/
rv=OSSL_HPKE_enc(
NULL, NULL, hpke_mode, hpke_suite,
pskid, psk, psklen,
pub, publen,
NULL, 0, NULL,
clear, clearlen,
aad, aadlen,
info, infolen,
NULL, 0, /* seq */
senderpub, &senderpublen, senderpriv,
cipher, &cipherlen
#ifdef TESTVECTORS
, NULL
#endif
);
if (rv!=1) {
printf("Error Encrypting (%d) - exiting\n",rv);
exit(rv);
}
neod_pbuf("ciphertext",cipher,cipherlen);
neod_pbuf("psk",psk,psklen);
printf("pskid: %s\n",(pskid==NULL?"NULL":pskid));
/*
* Call happykey decrypt
*/
rv=OSSL_HPKE_dec(
NULL, NULL, hpke_mode, hpke_suite,
pskid, psk, psklen,
NULL, 0, // publen, pub,
NULL, 0, privevp,
senderpub, senderpublen,
cipher, cipherlen,
aad, aadlen,
info, infolen,
NULL, 0, /* seq */
clear, &clearlen
);
if (rv!=1) {
printf("Error decrypting (%d) - exiting\n",rv);
exit(rv);
}
neod_pbuf("recovered clear",clear,clearlen);
return 1;
}