Skip to content

Latest commit

 

History

History
1275 lines (1119 loc) · 41.3 KB

pk11hpke.c

File metadata and controls

1275 lines (1119 loc) · 41.3 KB
 
Jan 25, 2021
Jan 25, 2021
2
* draft-irtf-cfrg-hpke-07
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "keyhi.h"
#include "pkcs11t.h"
#include "pk11func.h"
#include "pk11hpke.h"
#include "pk11pqg.h"
#include "secerr.h"
#include "secitem.h"
#include "secmod.h"
#include "secmodi.h"
#include "secmodti.h"
#include "secutil.h"
Mar 15, 2021
Mar 15, 2021
21
#define SERIALIZATION_VERSION 2
Jan 25, 2021
Jan 25, 2021
22
Mar 15, 2021
Mar 15, 2021
23
static const char *V1_LABEL = "HPKE-v1";
24
25
26
27
28
static const char *EXP_LABEL = "exp";
static const char *HPKE_LABEL = "HPKE";
static const char *INFO_LABEL = "info_hash";
static const char *KEM_LABEL = "KEM";
static const char *KEY_LABEL = "key";
Jan 25, 2021
Jan 25, 2021
29
static const char *NONCE_LABEL = "base_nonce";
30
31
32
33
34
35
36
37
38
39
40
41
static const char *PSK_ID_LABEL = "psk_id_hash";
static const char *SECRET_LABEL = "secret";
static const char *SEC_LABEL = "sec";
static const char *EAE_PRK_LABEL = "eae_prk";
static const char *SH_SEC_LABEL = "shared_secret";
struct HpkeContextStr {
const hpkeKemParams *kemParams;
const hpkeKdfParams *kdfParams;
const hpkeAeadParams *aeadParams;
PRUint8 mode; /* Base and PSK modes supported. */
SECItem *encapPubKey; /* Marshalled public key, sent to receiver. */
Jan 25, 2021
Jan 25, 2021
42
SECItem *baseNonce; /* Deterministic nonce for AEAD. */
43
44
45
46
47
48
49
50
51
52
53
54
55
56
SECItem *pskId; /* PSK identifier (non-secret). */
PK11Context *aeadContext; /* AEAD context used by Seal/Open. */
PRUint64 sequenceNumber; /* seqNo for decrypt IV construction. */
PK11SymKey *sharedSecret; /* ExtractAndExpand output key. */
PK11SymKey *key; /* Key used with the AEAD. */
PK11SymKey *exporterSecret; /* Derivation key for ExportSecret. */
PK11SymKey *psk; /* PSK imported by the application. */
};
static const hpkeKemParams kemParams[] = {
/* KEM, Nsk, Nsecret, Npk, oidTag, Hash mechanism */
{ HpkeDhKemX25519Sha256, 32, 32, 32, SEC_OID_CURVE25519, CKM_SHA256 },
};
Mar 15, 2021
Mar 15, 2021
57
#define MAX_WRAPPED_EXP_LEN 72 // Largest kdfParams->Nh + 8
58
59
60
static const hpkeKdfParams kdfParams[] = {
/* KDF, Nh, mechanism */
{ HpkeKdfHkdfSha256, SHA256_LENGTH, CKM_SHA256 },
Jan 25, 2021
Jan 25, 2021
61
62
{ HpkeKdfHkdfSha384, SHA384_LENGTH, CKM_SHA384 },
{ HpkeKdfHkdfSha512, SHA512_LENGTH, CKM_SHA512 },
Mar 15, 2021
Mar 15, 2021
64
#define MAX_WRAPPED_KEY_LEN 40 // Largest aeadParams->Nk + 8
65
66
67
static const hpkeAeadParams aeadParams[] = {
/* AEAD, Nk, Nn, tagLen, mechanism */
{ HpkeAeadAes128Gcm, 16, 12, 16, CKM_AES_GCM },
Mar 17, 2021
Mar 17, 2021
68
{ HpkeAeadAes256Gcm, 32, 12, 16, CKM_AES_GCM },
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{ HpkeAeadChaCha20Poly1305, 32, 12, 16, CKM_CHACHA20_POLY1305 },
};
static inline const hpkeKemParams *
kemId2Params(HpkeKemId kemId)
{
switch (kemId) {
case HpkeDhKemX25519Sha256:
return &kemParams[0];
default:
return NULL;
}
}
static inline const hpkeKdfParams *
kdfId2Params(HpkeKdfId kdfId)
{
switch (kdfId) {
case HpkeKdfHkdfSha256:
return &kdfParams[0];
Jan 25, 2021
Jan 25, 2021
89
90
91
92
case HpkeKdfHkdfSha384:
return &kdfParams[1];
case HpkeKdfHkdfSha512:
return &kdfParams[2];
93
94
95
96
97
98
99
100
101
102
103
default:
return NULL;
}
}
static const inline hpkeAeadParams *
aeadId2Params(HpkeAeadId aeadId)
{
switch (aeadId) {
case HpkeAeadAes128Gcm:
return &aeadParams[0];
Mar 17, 2021
Mar 17, 2021
104
case HpkeAeadAes256Gcm:
105
return &aeadParams[1];
Mar 17, 2021
Mar 17, 2021
106
107
case HpkeAeadChaCha20Poly1305:
return &aeadParams[2];
108
109
110
111
112
default:
return NULL;
}
}
Jan 25, 2021
Jan 25, 2021
113
114
static PRUint8 *
encodeNumber(PRUint64 value, PRUint8 *b, size_t count)
Jan 25, 2021
Jan 25, 2021
116
117
118
119
120
121
122
PRUint64 encoded;
PORT_Assert(b && count > 0 && count <= sizeof(encoded));
encoded = PR_htonll(value);
PORT_Memcpy(b, ((unsigned char *)(&encoded)) + (sizeof(encoded) - count),
count);
return b + count;
Jan 25, 2021
Jan 25, 2021
125
126
127
128
129
130
131
132
133
134
135
136
137
138
static PRUint8 *
decodeNumber(PRUint64 *value, PRUint8 *b, size_t count)
{
unsigned int i;
PRUint64 number = 0;
PORT_Assert(b && value && count <= sizeof(*value));
for (i = 0; i < count; i++) {
number = (number << 8) + b[i];
}
*value = number;
return b + count;
}
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
SECStatus
PK11_HPKE_ValidateParameters(HpkeKemId kemId, HpkeKdfId kdfId, HpkeAeadId aeadId)
{
/* If more variants are added, ensure the combination is also
* legal. For now it is, since only the AEAD may vary. */
const hpkeKemParams *kem = kemId2Params(kemId);
const hpkeKdfParams *kdf = kdfId2Params(kdfId);
const hpkeAeadParams *aead = aeadId2Params(aeadId);
if (!kem || !kdf || !aead) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
return SECSuccess;
}
HpkeContext *
PK11_HPKE_NewContext(HpkeKemId kemId, HpkeKdfId kdfId, HpkeAeadId aeadId,
PK11SymKey *psk, const SECItem *pskId)
{
SECStatus rv = SECSuccess;
PK11SlotInfo *slot = NULL;
HpkeContext *cx = NULL;
/* Both the PSK and the PSK ID default to empty. */
SECItem emptyItem = { siBuffer, NULL, 0 };
cx = PORT_ZNew(HpkeContext);
if (!cx) {
return NULL;
}
cx->mode = psk ? HpkeModePsk : HpkeModeBase;
cx->kemParams = kemId2Params(kemId);
cx->kdfParams = kdfId2Params(kdfId);
cx->aeadParams = aeadId2Params(aeadId);
CHECK_FAIL_ERR((!!psk != !!pskId), SEC_ERROR_INVALID_ARGS);
CHECK_FAIL_ERR(!cx->kemParams || !cx->kdfParams || !cx->aeadParams,
SEC_ERROR_INVALID_ARGS);
/* Import the provided PSK or the default. */
slot = PK11_GetBestSlot(CKM_EC_KEY_PAIR_GEN, NULL);
CHECK_FAIL(!slot);
if (psk) {
cx->psk = PK11_ReferenceSymKey(psk);
cx->pskId = SECITEM_DupItem(pskId);
} else {
cx->psk = PK11_ImportDataKey(slot, CKM_HKDF_DATA, PK11_OriginUnwrap,
CKA_DERIVE, &emptyItem, NULL);
cx->pskId = SECITEM_DupItem(&emptyItem);
}
CHECK_FAIL(!cx->psk);
CHECK_FAIL(!cx->pskId);
CLEANUP:
if (rv != SECSuccess) {
PK11_FreeSymKey(cx->psk);
SECITEM_FreeItem(cx->pskId, PR_TRUE);
cx->pskId = NULL;
cx->psk = NULL;
PORT_Free(cx);
cx = NULL;
}
if (slot) {
PK11_FreeSlot(slot);
}
return cx;
}
void
PK11_HPKE_DestroyContext(HpkeContext *cx, PRBool freeit)
{
if (!cx) {
return;
}
if (cx->aeadContext) {
PK11_DestroyContext((PK11Context *)cx->aeadContext, PR_TRUE);
cx->aeadContext = NULL;
}
PK11_FreeSymKey(cx->exporterSecret);
PK11_FreeSymKey(cx->sharedSecret);
PK11_FreeSymKey(cx->key);
PK11_FreeSymKey(cx->psk);
SECITEM_FreeItem(cx->pskId, PR_TRUE);
Jan 25, 2021
Jan 25, 2021
221
SECITEM_FreeItem(cx->baseNonce, PR_TRUE);
222
223
224
225
226
227
SECITEM_FreeItem(cx->encapPubKey, PR_TRUE);
cx->exporterSecret = NULL;
cx->sharedSecret = NULL;
cx->key = NULL;
cx->psk = NULL;
cx->pskId = NULL;
Jan 25, 2021
Jan 25, 2021
228
cx->baseNonce = NULL;
229
230
231
232
233
234
cx->encapPubKey = NULL;
if (freeit) {
PORT_ZFree(cx, sizeof(HpkeContext));
}
}
Jan 25, 2021
Jan 25, 2021
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* Export Format:
struct {
uint8 serilizationVersion;
uint16 kemId;
uint16 kdfId;
uint16 aeadId;
uint16 modeId;
uint64 sequenceNumber;
opaque senderPubKey<1..2^16-1>;
opaque baseNonce<1..2^16-1>;
opaque key<1..2^16-1>;
opaque exporterSecret<1..2^16-1>;
} HpkeSerializedContext
*/
Mar 15, 2021
Mar 15, 2021
249
#define EXPORTED_CTX_BASE_LEN 25 /* Fixed size plus 2B for each variable. */
Jan 25, 2021
Jan 25, 2021
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#define REMAINING_BYTES(walker, buf) \
buf->len - (walker - buf->data)
SECStatus
PK11_HPKE_ExportContext(const HpkeContext *cx, PK11SymKey *wrapKey, SECItem **serialized)
{
SECStatus rv;
size_t allocLen;
PRUint8 *walker;
SECItem *keyBytes = NULL; // Maybe wrapped
SECItem *exporterBytes = NULL; // Maybe wrapped
SECItem *serializedCx = NULL;
PRUint8 wrappedKeyBytes[MAX_WRAPPED_KEY_LEN] = { 0 };
PRUint8 wrappedExpBytes[MAX_WRAPPED_EXP_LEN] = { 0 };
SECItem wrappedKey = { siBuffer, wrappedKeyBytes, sizeof(wrappedKeyBytes) };
SECItem wrappedExp = { siBuffer, wrappedExpBytes, sizeof(wrappedExpBytes) };
CHECK_FAIL_ERR((!cx || !cx->aeadContext || !serialized), SEC_ERROR_INVALID_ARGS);
CHECK_FAIL_ERR((cx->aeadContext->operation != (CKA_NSS_MESSAGE | CKA_DECRYPT)),
SEC_ERROR_NOT_A_RECIPIENT);
/* If a wrapping key was provided, do the wrap first
* so that we know what size to allocate. */
if (wrapKey) {
rv = PK11_WrapSymKey(CKM_AES_KEY_WRAP_KWP, NULL, wrapKey,
cx->key, &wrappedKey);
CHECK_RV(rv);
rv = PK11_WrapSymKey(CKM_AES_KEY_WRAP_KWP, NULL, wrapKey,
cx->exporterSecret, &wrappedExp);
CHECK_RV(rv);
keyBytes = &wrappedKey;
exporterBytes = &wrappedExp;
} else {
rv = PK11_ExtractKeyValue(cx->key);
CHECK_RV(rv);
keyBytes = PK11_GetKeyData(cx->key);
CHECK_FAIL(!keyBytes);
PORT_Assert(keyBytes->len == cx->aeadParams->Nk);
rv = PK11_ExtractKeyValue(cx->exporterSecret);
CHECK_RV(rv);
exporterBytes = PK11_GetKeyData(cx->exporterSecret);
CHECK_FAIL(!exporterBytes);
PORT_Assert(exporterBytes->len == cx->kdfParams->Nh);
}
allocLen = EXPORTED_CTX_BASE_LEN + cx->baseNonce->len + cx->encapPubKey->len;
allocLen += wrapKey ? wrappedKey.len : cx->aeadParams->Nk;
allocLen += wrapKey ? wrappedExp.len : cx->kdfParams->Nh;
serializedCx = SECITEM_AllocItem(NULL, NULL, allocLen);
CHECK_FAIL(!serializedCx);
walker = &serializedCx->data[0];
*(walker)++ = (PRUint8)SERIALIZATION_VERSION;
walker = encodeNumber(cx->kemParams->id, walker, 2);
walker = encodeNumber(cx->kdfParams->id, walker, 2);
walker = encodeNumber(cx->aeadParams->id, walker, 2);
walker = encodeNumber(cx->mode, walker, 2);
walker = encodeNumber(cx->sequenceNumber, walker, 8);
/* sender public key, serialized. */
walker = encodeNumber(cx->encapPubKey->len, walker, 2);
PORT_Memcpy(walker, cx->encapPubKey->data, cx->encapPubKey->len);
walker += cx->encapPubKey->len;
/* base nonce */
walker = encodeNumber(cx->baseNonce->len, walker, 2);
PORT_Memcpy(walker, cx->baseNonce->data, cx->baseNonce->len);
walker += cx->baseNonce->len;
/* key. */
walker = encodeNumber(keyBytes->len, walker, 2);
PORT_Memcpy(walker, keyBytes->data, keyBytes->len);
walker += keyBytes->len;
/* exporter_secret. */
walker = encodeNumber(exporterBytes->len, walker, 2);
PORT_Memcpy(walker, exporterBytes->data, exporterBytes->len);
walker += exporterBytes->len;
CHECK_FAIL_ERR(REMAINING_BYTES(walker, serializedCx) != 0,
SEC_ERROR_LIBRARY_FAILURE);
*serialized = serializedCx;
CLEANUP:
if (rv != SECSuccess) {
SECITEM_ZfreeItem(serializedCx, PR_TRUE);
}
return rv;
}
HpkeContext *
PK11_HPKE_ImportContext(const SECItem *serialized, PK11SymKey *wrapKey)
{
SECStatus rv = SECSuccess;
HpkeContext *cx = NULL;
PRUint8 *walker;
PRUint64 tmpn;
PRUint8 tmp8;
HpkeKemId kem;
HpkeKdfId kdf;
HpkeAeadId aead;
PK11SlotInfo *slot = NULL;
PK11SymKey *tmpKey = NULL;
SECItem tmpItem = { siBuffer, NULL, 0 };
SECItem emptyItem = { siBuffer, NULL, 0 };
CHECK_FAIL_ERR((!serialized || !serialized->data || serialized->len == 0),
SEC_ERROR_INVALID_ARGS);
CHECK_FAIL_ERR((serialized->len < EXPORTED_CTX_BASE_LEN), SEC_ERROR_BAD_DATA);
walker = serialized->data;
tmp8 = *(walker++);
CHECK_FAIL_ERR((tmp8 != SERIALIZATION_VERSION), SEC_ERROR_BAD_DATA);
walker = decodeNumber(&tmpn, walker, 2);
kem = (HpkeKemId)tmpn;
walker = decodeNumber(&tmpn, walker, 2);
kdf = (HpkeKdfId)tmpn;
walker = decodeNumber(&tmpn, walker, 2);
aead = (HpkeAeadId)tmpn;
/* Create context. We'll manually set the mode, though we
* no longer have the PSK and have no need for it. */
cx = PK11_HPKE_NewContext(kem, kdf, aead, NULL, NULL);
CHECK_FAIL(!cx);
walker = decodeNumber(&tmpn, walker, 2);
CHECK_FAIL_ERR((tmpn != HpkeModeBase && tmpn != HpkeModePsk),
SEC_ERROR_BAD_DATA);
cx->mode = (HpkeModeId)tmpn;
walker = decodeNumber(&cx->sequenceNumber, walker, 8);
slot = PK11_GetBestSlot(CKM_HKDF_DERIVE, NULL);
CHECK_FAIL(!slot);
/* Import sender public key (serialized). */
walker = decodeNumber(&tmpn, walker, 2);
CHECK_FAIL_ERR(tmpn >= REMAINING_BYTES(walker, serialized),
SEC_ERROR_BAD_DATA);
tmpItem.data = walker;
tmpItem.len = tmpn;
cx->encapPubKey = SECITEM_DupItem(&tmpItem);
CHECK_FAIL(!cx->encapPubKey);
walker += tmpItem.len;
/* Import base_nonce. */
walker = decodeNumber(&tmpn, walker, 2);
CHECK_FAIL_ERR(tmpn != cx->aeadParams->Nn, SEC_ERROR_BAD_DATA);
CHECK_FAIL_ERR(tmpn >= REMAINING_BYTES(walker, serialized),
SEC_ERROR_BAD_DATA);
tmpItem.data = walker;
tmpItem.len = tmpn;
cx->baseNonce = SECITEM_DupItem(&tmpItem);
CHECK_FAIL(!cx->baseNonce);
walker += tmpItem.len;
/* Import key */
walker = decodeNumber(&tmpn, walker, 2);
CHECK_FAIL_ERR(tmpn >= REMAINING_BYTES(walker, serialized),
SEC_ERROR_BAD_DATA);
tmpItem.data = walker;
tmpItem.len = tmpn;
walker += tmpItem.len;
if (wrapKey) {
cx->key = PK11_UnwrapSymKey(wrapKey, CKM_AES_KEY_WRAP_KWP,
NULL, &tmpItem, cx->aeadParams->mech,
CKA_NSS_MESSAGE | CKA_DECRYPT, 0);
CHECK_FAIL(!cx->key);
} else {
CHECK_FAIL_ERR(tmpn != cx->aeadParams->Nk, SEC_ERROR_BAD_DATA);
tmpKey = PK11_ImportSymKey(slot, cx->aeadParams->mech,
PK11_OriginUnwrap, CKA_NSS_MESSAGE | CKA_DECRYPT,
&tmpItem, NULL);
CHECK_FAIL(!tmpKey);
cx->key = tmpKey;
}
/* Import exporter_secret. */
walker = decodeNumber(&tmpn, walker, 2);
CHECK_FAIL_ERR(tmpn != REMAINING_BYTES(walker, serialized),
SEC_ERROR_BAD_DATA);
tmpItem.data = walker;
tmpItem.len = tmpn;
walker += tmpItem.len;
if (wrapKey) {
cx->exporterSecret = PK11_UnwrapSymKey(wrapKey, CKM_AES_KEY_WRAP_KWP,
NULL, &tmpItem, cx->kdfParams->mech,
CKM_HKDF_DERIVE, 0);
CHECK_FAIL(!cx->exporterSecret);
} else {
CHECK_FAIL_ERR(tmpn != cx->kdfParams->Nh, SEC_ERROR_BAD_DATA);
tmpKey = PK11_ImportSymKey(slot, CKM_HKDF_DERIVE, PK11_OriginUnwrap,
CKA_DERIVE, &tmpItem, NULL);
CHECK_FAIL(!tmpKey);
cx->exporterSecret = tmpKey;
}
cx->aeadContext = PK11_CreateContextBySymKey(cx->aeadParams->mech,
CKA_NSS_MESSAGE | CKA_DECRYPT,
cx->key, &emptyItem);
CLEANUP:
if (rv != SECSuccess) {
PK11_FreeSymKey(tmpKey);
PK11_HPKE_DestroyContext(cx, PR_TRUE);
cx = NULL;
}
if (slot) {
PK11_FreeSlot(slot);
}
return cx;
}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
SECStatus
PK11_HPKE_Serialize(const SECKEYPublicKey *pk, PRUint8 *buf, unsigned int *len, unsigned int maxLen)
{
if (!pk || !len || pk->keyType != ecKey) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* If no buffer provided, return the length required for
* the serialized public key. */
if (!buf) {
*len = pk->u.ec.publicValue.len;
return SECSuccess;
}
if (maxLen < pk->u.ec.publicValue.len) {
PORT_SetError(SEC_ERROR_INPUT_LEN);
return SECFailure;
}
PORT_Memcpy(buf, pk->u.ec.publicValue.data, pk->u.ec.publicValue.len);
*len = pk->u.ec.publicValue.len;
return SECSuccess;
};
SECStatus
PK11_HPKE_Deserialize(const HpkeContext *cx, const PRUint8 *enc,
unsigned int encLen, SECKEYPublicKey **outPubKey)
{
SECStatus rv;
SECKEYPublicKey *pubKey = NULL;
SECOidData *oidData = NULL;
PLArenaPool *arena;
if (!cx || !enc || encLen == 0 || !outPubKey) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
CHECK_FAIL(!arena);
pubKey = PORT_ArenaZNew(arena, SECKEYPublicKey);
CHECK_FAIL(!pubKey);
pubKey->arena = arena;
pubKey->keyType = ecKey;
pubKey->pkcs11Slot = NULL;
pubKey->pkcs11ID = CK_INVALID_HANDLE;
rv = SECITEM_MakeItem(pubKey->arena, &pubKey->u.ec.publicValue,
enc, encLen);
CHECK_RV(rv);
pubKey->u.ec.encoding = ECPoint_Undefined;
pubKey->u.ec.size = 0;
oidData = SECOID_FindOIDByTag(cx->kemParams->oidTag);
CHECK_FAIL_ERR(!oidData, SEC_ERROR_INVALID_ALGORITHM);
// Create parameters.
CHECK_FAIL(!SECITEM_AllocItem(pubKey->arena, &pubKey->u.ec.DEREncodedParams,
2 + oidData->oid.len));
// Set parameters.
pubKey->u.ec.DEREncodedParams.data[0] = SEC_ASN1_OBJECT_ID;
pubKey->u.ec.DEREncodedParams.data[1] = oidData->oid.len;
Jan 25, 2021
Jan 25, 2021
536
PORT_Memcpy(pubKey->u.ec.DEREncodedParams.data + 2, oidData->oid.data, oidData->oid.len);
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
*outPubKey = pubKey;
CLEANUP:
if (rv != SECSuccess) {
SECKEY_DestroyPublicKey(pubKey);
}
return rv;
};
static SECStatus
pk11_hpke_CheckKeys(const HpkeContext *cx, const SECKEYPublicKey *pk,
const SECKEYPrivateKey *sk)
{
SECOidTag pkTag;
unsigned int i;
if (pk->keyType != ecKey || (sk && sk->keyType != ecKey)) {
PORT_SetError(SEC_ERROR_BAD_KEY);
return SECFailure;
}
pkTag = SECKEY_GetECCOid(&pk->u.ec.DEREncodedParams);
if (pkTag != cx->kemParams->oidTag) {
PORT_SetError(SEC_ERROR_BAD_KEY);
return SECFailure;
}
for (i = 0; i < PR_ARRAY_SIZE(kemParams); i++) {
if (cx->kemParams->oidTag == kemParams[i].oidTag) {
return SECSuccess;
}
}
return SECFailure;
}
static SECStatus
pk11_hpke_GenerateKeyPair(const HpkeContext *cx, SECKEYPublicKey **pkE,
SECKEYPrivateKey **skE)
{
SECStatus rv = SECSuccess;
SECKEYPrivateKey *privKey = NULL;
SECKEYPublicKey *pubKey = NULL;
SECOidData *oidData = NULL;
SECKEYECParams ecp;
PK11SlotInfo *slot = NULL;
ecp.data = NULL;
PORT_Assert(cx && skE && pkE);
oidData = SECOID_FindOIDByTag(cx->kemParams->oidTag);
CHECK_FAIL_ERR(!oidData, SEC_ERROR_INVALID_ALGORITHM);
ecp.data = PORT_Alloc(2 + oidData->oid.len);
CHECK_FAIL(!ecp.data);
ecp.len = 2 + oidData->oid.len;
ecp.type = siDEROID;
ecp.data[0] = SEC_ASN1_OBJECT_ID;
ecp.data[1] = oidData->oid.len;
Jan 25, 2021
Jan 25, 2021
592
PORT_Memcpy(&ecp.data[2], oidData->oid.data, oidData->oid.len);
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
slot = PK11_GetBestSlot(CKM_EC_KEY_PAIR_GEN, NULL);
CHECK_FAIL(!slot);
privKey = PK11_GenerateKeyPair(slot, CKM_EC_KEY_PAIR_GEN, &ecp, &pubKey,
PR_FALSE, PR_TRUE, NULL);
CHECK_FAIL_ERR((!privKey || !pubKey), SEC_ERROR_KEYGEN_FAIL);
PORT_Assert(rv == SECSuccess);
*skE = privKey;
*pkE = pubKey;
CLEANUP:
if (rv != SECSuccess) {
SECKEY_DestroyPrivateKey(privKey);
SECKEY_DestroyPublicKey(pubKey);
}
if (slot) {
PK11_FreeSlot(slot);
}
PORT_Free(ecp.data);
return rv;
}
static inline SECItem *
pk11_hpke_MakeExtractLabel(const char *prefix, unsigned int prefixLen,
const char *label, unsigned int labelLen,
const SECItem *suiteId, const SECItem *ikm)
{
SECItem *out = NULL;
Jan 25, 2021
Jan 25, 2021
622
PRUint8 *walker;
623
624
625
626
627
out = SECITEM_AllocItem(NULL, NULL, prefixLen + labelLen + suiteId->len + (ikm ? ikm->len : 0));
if (!out) {
return NULL;
}
Jan 25, 2021
Jan 25, 2021
628
629
630
631
632
633
634
walker = out->data;
PORT_Memcpy(walker, prefix, prefixLen);
walker += prefixLen;
PORT_Memcpy(walker, suiteId->data, suiteId->len);
walker += suiteId->len;
PORT_Memcpy(walker, label, labelLen);
walker += labelLen;
635
if (ikm && ikm->data) {
Jan 25, 2021
Jan 25, 2021
636
PORT_Memcpy(walker, ikm->data, ikm->len);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
}
return out;
}
static SECStatus
pk11_hpke_LabeledExtractData(const HpkeContext *cx, SECItem *salt,
const SECItem *suiteId, const char *label,
unsigned int labelLen, const SECItem *ikm, SECItem **out)
{
SECStatus rv;
CK_HKDF_PARAMS params = { 0 };
PK11SymKey *importedIkm = NULL;
PK11SymKey *prk = NULL;
PK11SlotInfo *slot = NULL;
SECItem *borrowed;
SECItem *outDerived = NULL;
SECItem *labeledIkm;
SECItem paramsItem = { siBuffer, (unsigned char *)&params,
sizeof(params) };
PORT_Assert(cx && ikm && label && labelLen && out && suiteId);
Mar 15, 2021
Mar 15, 2021
659
labeledIkm = pk11_hpke_MakeExtractLabel(V1_LABEL, strlen(V1_LABEL), label, labelLen, suiteId, ikm);
660
661
662
CHECK_FAIL(!labeledIkm);
params.bExtract = CK_TRUE;
params.bExpand = CK_FALSE;
Jan 25, 2021
Jan 25, 2021
663
params.prfHashMechanism = cx->kdfParams->mech;
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
params.ulSaltType = salt ? CKF_HKDF_SALT_DATA : CKF_HKDF_SALT_NULL;
params.pSalt = salt ? (CK_BYTE_PTR)salt->data : NULL;
params.ulSaltLen = salt ? salt->len : 0;
params.pInfo = labeledIkm->data;
params.ulInfoLen = labeledIkm->len;
slot = PK11_GetBestSlot(CKM_EC_KEY_PAIR_GEN, NULL);
CHECK_FAIL(!slot);
importedIkm = PK11_ImportDataKey(slot, CKM_HKDF_DATA, PK11_OriginUnwrap,
CKA_DERIVE, labeledIkm, NULL);
CHECK_FAIL(!importedIkm);
prk = PK11_Derive(importedIkm, CKM_HKDF_DATA, &paramsItem,
CKM_HKDF_DERIVE, CKA_DERIVE, 0);
CHECK_FAIL(!prk);
rv = PK11_ExtractKeyValue(prk);
CHECK_RV(rv);
borrowed = PK11_GetKeyData(prk);
CHECK_FAIL(!borrowed);
outDerived = SECITEM_DupItem(borrowed);
CHECK_FAIL(!outDerived);
*out = outDerived;
CLEANUP:
PK11_FreeSymKey(importedIkm);
PK11_FreeSymKey(prk);
SECITEM_FreeItem(labeledIkm, PR_TRUE);
if (slot) {
PK11_FreeSlot(slot);
}
return rv;
}
static SECStatus
pk11_hpke_LabeledExtract(const HpkeContext *cx, PK11SymKey *salt,
Jan 25, 2021
Jan 25, 2021
700
const SECItem *suiteId, const char *label, CK_MECHANISM_TYPE hashMech,
701
702
703
704
705
706
707
708
709
710
711
712
713
unsigned int labelLen, PK11SymKey *ikm, PK11SymKey **out)
{
SECStatus rv = SECSuccess;
SECItem *innerLabel = NULL;
PK11SymKey *labeledIkm = NULL;
PK11SymKey *prk = NULL;
CK_HKDF_PARAMS params = { 0 };
CK_KEY_DERIVATION_STRING_DATA labelData;
SECItem labelDataItem = { siBuffer, NULL, 0 };
SECItem paramsItem = { siBuffer, (unsigned char *)&params,
sizeof(params) };
PORT_Assert(cx && ikm && label && labelLen && out && suiteId);
Mar 15, 2021
Mar 15, 2021
714
innerLabel = pk11_hpke_MakeExtractLabel(V1_LABEL, strlen(V1_LABEL), label, labelLen, suiteId, NULL);
715
716
717
718
719
720
721
722
723
724
725
CHECK_FAIL(!innerLabel);
labelData.pData = innerLabel->data;
labelData.ulLen = innerLabel->len;
labelDataItem.data = (PRUint8 *)&labelData;
labelDataItem.len = sizeof(labelData);
labeledIkm = PK11_Derive(ikm, CKM_CONCATENATE_DATA_AND_BASE,
&labelDataItem, CKM_GENERIC_SECRET_KEY_GEN, CKA_DERIVE, 0);
CHECK_FAIL(!labeledIkm);
params.bExtract = CK_TRUE;
params.bExpand = CK_FALSE;
Jan 25, 2021
Jan 25, 2021
726
params.prfHashMechanism = hashMech;
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
params.ulSaltType = salt ? CKF_HKDF_SALT_KEY : CKF_HKDF_SALT_NULL;
params.hSaltKey = salt ? PK11_GetSymKeyHandle(salt) : CK_INVALID_HANDLE;
prk = PK11_Derive(labeledIkm, CKM_HKDF_DERIVE, &paramsItem,
CKM_HKDF_DERIVE, CKA_DERIVE, 0);
CHECK_FAIL(!prk);
*out = prk;
CLEANUP:
PK11_FreeSymKey(labeledIkm);
SECITEM_ZfreeItem(innerLabel, PR_TRUE);
return rv;
}
static SECStatus
pk11_hpke_LabeledExpand(const HpkeContext *cx, PK11SymKey *prk, const SECItem *suiteId,
const char *label, unsigned int labelLen, const SECItem *info,
Jan 25, 2021
Jan 25, 2021
744
745
unsigned int L, CK_MECHANISM_TYPE hashMech, PK11SymKey **outKey,
SECItem **outItem)
Jan 25, 2021
Jan 25, 2021
747
SECStatus rv = SECSuccess;
748
749
750
751
752
753
754
755
756
CK_MECHANISM_TYPE keyMech;
CK_MECHANISM_TYPE deriveMech;
CK_HKDF_PARAMS params = { 0 };
PK11SymKey *derivedKey = NULL;
SECItem *labeledInfoItem = NULL;
SECItem paramsItem = { siBuffer, (unsigned char *)&params,
sizeof(params) };
SECItem *derivedKeyData;
PRUint8 encodedL[2];
Jan 25, 2021
Jan 25, 2021
757
PRUint8 *walker = encodedL;
758
759
760
size_t len;
PORT_Assert(cx && prk && label && (!!outKey != !!outItem));
Jan 25, 2021
Jan 25, 2021
761
walker = encodeNumber(L, walker, 2);
762
len = info ? info->len : 0;
Mar 15, 2021
Mar 15, 2021
763
len += sizeof(encodedL) + strlen(V1_LABEL) + suiteId->len + labelLen;
764
765
766
labeledInfoItem = SECITEM_AllocItem(NULL, NULL, len);
CHECK_FAIL(!labeledInfoItem);
Jan 25, 2021
Jan 25, 2021
767
768
769
walker = labeledInfoItem->data;
PORT_Memcpy(walker, encodedL, sizeof(encodedL));
walker += sizeof(encodedL);
Mar 15, 2021
Mar 15, 2021
770
771
PORT_Memcpy(walker, V1_LABEL, strlen(V1_LABEL));
walker += strlen(V1_LABEL);
Jan 25, 2021
Jan 25, 2021
772
773
774
775
PORT_Memcpy(walker, suiteId->data, suiteId->len);
walker += suiteId->len;
PORT_Memcpy(walker, label, labelLen);
walker += labelLen;
Jan 25, 2021
Jan 25, 2021
777
PORT_Memcpy(walker, info->data, info->len);
778
779
780
781
}
params.bExtract = CK_FALSE;
params.bExpand = CK_TRUE;
Jan 25, 2021
Jan 25, 2021
782
params.prfHashMechanism = hashMech;
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
params.ulSaltType = CKF_HKDF_SALT_NULL;
params.pInfo = labeledInfoItem->data;
params.ulInfoLen = labeledInfoItem->len;
deriveMech = outItem ? CKM_HKDF_DATA : CKM_HKDF_DERIVE;
/* If we're expanding to the encryption key use the appropriate mechanism. */
keyMech = (label && !strcmp(KEY_LABEL, label)) ? cx->aeadParams->mech : CKM_HKDF_DERIVE;
derivedKey = PK11_Derive(prk, deriveMech, &paramsItem, keyMech, CKA_DERIVE, L);
CHECK_FAIL(!derivedKey);
if (outItem) {
/* Don't allow export of real keys. */
CHECK_FAIL_ERR(deriveMech != CKM_HKDF_DATA, SEC_ERROR_LIBRARY_FAILURE);
rv = PK11_ExtractKeyValue(derivedKey);
CHECK_RV(rv);
derivedKeyData = PK11_GetKeyData(derivedKey);
CHECK_FAIL_ERR((!derivedKeyData), SEC_ERROR_NO_KEY);
*outItem = SECITEM_DupItem(derivedKeyData);
CHECK_FAIL(!*outItem);
PK11_FreeSymKey(derivedKey);
} else {
*outKey = derivedKey;
}
CLEANUP:
if (rv != SECSuccess) {
PK11_FreeSymKey(derivedKey);
}
SECITEM_ZfreeItem(labeledInfoItem, PR_TRUE);
return rv;
}
static SECStatus
pk11_hpke_ExtractAndExpand(const HpkeContext *cx, PK11SymKey *ikm,
const SECItem *kemContext, PK11SymKey **out)
{
SECStatus rv;
PK11SymKey *eaePrk = NULL;
PK11SymKey *sharedSecret = NULL;
PRUint8 suiteIdBuf[5];
Jan 25, 2021
Jan 25, 2021
823
PRUint8 *walker;
824
825
826
827
PORT_Memcpy(suiteIdBuf, KEM_LABEL, strlen(KEM_LABEL));
SECItem suiteIdItem = { siBuffer, suiteIdBuf, sizeof(suiteIdBuf) };
PORT_Assert(cx && ikm && kemContext && out);
Jan 25, 2021
Jan 25, 2021
828
829
walker = &suiteIdBuf[3];
walker = encodeNumber(cx->kemParams->id, walker, 2);
830
831
rv = pk11_hpke_LabeledExtract(cx, NULL, &suiteIdItem, EAE_PRK_LABEL,
Jan 25, 2021
Jan 25, 2021
832
833
cx->kemParams->hashMech, strlen(EAE_PRK_LABEL),
ikm, &eaePrk);
834
835
836
CHECK_RV(rv);
rv = pk11_hpke_LabeledExpand(cx, eaePrk, &suiteIdItem, SH_SEC_LABEL, strlen(SH_SEC_LABEL),
Jan 25, 2021
Jan 25, 2021
837
838
kemContext, cx->kemParams->Nsecret, cx->kemParams->hashMech,
&sharedSecret, NULL);
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
CHECK_RV(rv);
*out = sharedSecret;
CLEANUP:
if (rv != SECSuccess) {
PK11_FreeSymKey(sharedSecret);
}
PK11_FreeSymKey(eaePrk);
return rv;
}
static SECStatus
pk11_hpke_Encap(HpkeContext *cx, const SECKEYPublicKey *pkE, SECKEYPrivateKey *skE,
SECKEYPublicKey *pkR)
{
SECStatus rv;
PK11SymKey *dh = NULL;
SECItem *kemContext = NULL;
SECItem *encPkR = NULL;
unsigned int tmpLen;
PORT_Assert(cx && skE && pkE && pkR);
rv = pk11_hpke_CheckKeys(cx, pkE, skE);
CHECK_RV(rv);
rv = pk11_hpke_CheckKeys(cx, pkR, NULL);
CHECK_RV(rv);
dh = PK11_PubDeriveWithKDF(skE, pkR, PR_FALSE, NULL, NULL, CKM_ECDH1_DERIVE,
CKM_SHA512_HMAC /* unused */, CKA_DERIVE, 0,
CKD_NULL, NULL, NULL);
CHECK_FAIL(!dh);
/* Encapsulate our sender public key. Many use cases
* (including ECH) require that the application fetch
* this value, so do it once and store into the cx. */
rv = PK11_HPKE_Serialize(pkE, NULL, &tmpLen, 0);
CHECK_RV(rv);
cx->encapPubKey = SECITEM_AllocItem(NULL, NULL, tmpLen);
CHECK_FAIL(!cx->encapPubKey);
rv = PK11_HPKE_Serialize(pkE, cx->encapPubKey->data,
&cx->encapPubKey->len, cx->encapPubKey->len);
CHECK_RV(rv);
rv = PK11_HPKE_Serialize(pkR, NULL, &tmpLen, 0);
CHECK_RV(rv);
kemContext = SECITEM_AllocItem(NULL, NULL, cx->encapPubKey->len + tmpLen);
CHECK_FAIL(!kemContext);
Jan 25, 2021
Jan 25, 2021
889
PORT_Memcpy(kemContext->data, cx->encapPubKey->data, cx->encapPubKey->len);
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
rv = PK11_HPKE_Serialize(pkR, &kemContext->data[cx->encapPubKey->len], &tmpLen, tmpLen);
CHECK_RV(rv);
rv = pk11_hpke_ExtractAndExpand(cx, dh, kemContext, &cx->sharedSecret);
CHECK_RV(rv);
CLEANUP:
if (rv != SECSuccess) {
PK11_FreeSymKey(cx->sharedSecret);
cx->sharedSecret = NULL;
}
SECITEM_FreeItem(encPkR, PR_TRUE);
SECITEM_FreeItem(kemContext, PR_TRUE);
PK11_FreeSymKey(dh);
return rv;
}
SECStatus
PK11_HPKE_ExportSecret(const HpkeContext *cx, const SECItem *info, unsigned int L,
PK11SymKey **out)
{
SECStatus rv;
PK11SymKey *exported;
PRUint8 suiteIdBuf[10];
Jan 25, 2021
Jan 25, 2021
914
PRUint8 *walker;
915
916
917
918
919
920
921
922
923
924
PORT_Memcpy(suiteIdBuf, HPKE_LABEL, strlen(HPKE_LABEL));
SECItem suiteIdItem = { siBuffer, suiteIdBuf, sizeof(suiteIdBuf) };
/* Arbitrary info length limit well under the specified max. */
if (!cx || !info || (!info->data && info->len) || info->len > 0xFFFF ||
!L || (L > 255 * cx->kdfParams->Nh)) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
Jan 25, 2021
Jan 25, 2021
925
926
927
928
walker = &suiteIdBuf[4];
walker = encodeNumber(cx->kemParams->id, walker, 2);
walker = encodeNumber(cx->kdfParams->id, walker, 2);
walker = encodeNumber(cx->aeadParams->id, walker, 2);
929
930
rv = pk11_hpke_LabeledExpand(cx, cx->exporterSecret, &suiteIdItem, SEC_LABEL,
Jan 25, 2021
Jan 25, 2021
931
932
strlen(SEC_LABEL), info, L, cx->kdfParams->mech,
&exported, NULL);
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
CHECK_RV(rv);
*out = exported;
CLEANUP:
return rv;
}
static SECStatus
pk11_hpke_Decap(HpkeContext *cx, const SECKEYPublicKey *pkR, SECKEYPrivateKey *skR,
const SECItem *encS)
{
SECStatus rv;
PK11SymKey *dh = NULL;
SECItem *encR = NULL;
SECItem *kemContext = NULL;
SECKEYPublicKey *pkS = NULL;
unsigned int tmpLen;
if (!cx || !skR || !pkR || !encS || !encS->data || !encS->len) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
rv = PK11_HPKE_Deserialize(cx, encS->data, encS->len, &pkS);
CHECK_RV(rv);
rv = pk11_hpke_CheckKeys(cx, pkR, skR);
CHECK_RV(rv);
rv = pk11_hpke_CheckKeys(cx, pkS, NULL);
CHECK_RV(rv);
dh = PK11_PubDeriveWithKDF(skR, pkS, PR_FALSE, NULL, NULL, CKM_ECDH1_DERIVE,
CKM_SHA512_HMAC /* unused */, CKA_DERIVE, 0,
CKD_NULL, NULL, NULL);
CHECK_FAIL(!dh);
/* kem_context = concat(enc, pkRm) */
rv = PK11_HPKE_Serialize(pkR, NULL, &tmpLen, 0);
CHECK_RV(rv);
kemContext = SECITEM_AllocItem(NULL, NULL, encS->len + tmpLen);
CHECK_FAIL(!kemContext);
Jan 25, 2021
Jan 25, 2021
976
PORT_Memcpy(kemContext->data, encS->data, encS->len);
977
978
979
980
981
rv = PK11_HPKE_Serialize(pkR, &kemContext->data[encS->len], &tmpLen,
kemContext->len - encS->len);
CHECK_RV(rv);
rv = pk11_hpke_ExtractAndExpand(cx, dh, kemContext, &cx->sharedSecret);
CHECK_RV(rv);
Jan 25, 2021
Jan 25, 2021
982
983
984
985
986
987
/* Store the sender serialized public key, which
* may be required by application use cases. */
cx->encapPubKey = SECITEM_DupItem(encS);
CHECK_FAIL(!cx->encapPubKey);
988
989
990
991
992
993
994
995
996
997
998
999
1000
CLEANUP:
if (rv != SECSuccess) {
PK11_FreeSymKey(cx->sharedSecret);
cx->sharedSecret = NULL;
}
PK11_FreeSymKey(dh);
SECKEY_DestroyPublicKey(pkS);
SECITEM_FreeItem(encR, PR_TRUE);
SECITEM_ZfreeItem(kemContext, PR_TRUE);
return rv;
}
const SECItem *