]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/kem/kem_util.c
Add HPKE DHKEM provider support for EC, X25519 and X448.
[thirdparty/openssl.git] / providers / implementations / kem / kem_util.c
1 /*
2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h> /* for memcpy() */
11 #include <openssl/core_names.h>
12 #include <openssl/crypto.h>
13 #include "eckem.h"
14
15 typedef struct {
16 unsigned int id;
17 const char *mode;
18 } KEM_MODE;
19
20 static const KEM_MODE eckem_modename_id_map[] = {
21 { KEM_MODE_DHKEM, OSSL_KEM_PARAM_OPERATION_DHKEM },
22 { 0, NULL }
23 };
24
25 int ossl_eckem_modename2id(const char *name)
26 {
27 size_t i;
28
29 if (name == NULL)
30 return KEM_MODE_UNDEFINED;
31
32 for (i = 0; eckem_modename_id_map[i].mode != NULL; ++i) {
33 if (OPENSSL_strcasecmp(name, eckem_modename_id_map[i].mode) == 0)
34 return eckem_modename_id_map[i].id;
35 }
36 return KEM_MODE_UNDEFINED;
37 }
38
39 /* suiteid = concat("KEM", I2OSP(kem_id, 2)) */
40 void ossl_dhkem_getsuiteid(unsigned char suiteid[5], uint16_t kemid)
41 {
42 memcpy(suiteid, "KEM", 3);
43 suiteid[3] = kemid >> 8;
44 suiteid[4] = kemid & 0xFF;
45 }