summaryrefslogtreecommitdiffstats
path: root/providers/implementations/kem/kem_util.c
blob: 8ce2854ee4a0aeb5b24c98caee44d97141423f37 (plain)
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
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (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
 */

#include <string.h> /* for memcpy() */
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include "eckem.h"

typedef struct {
    unsigned int id;
    const char *mode;
} KEM_MODE;

static const KEM_MODE eckem_modename_id_map[] = {
    { KEM_MODE_DHKEM, OSSL_KEM_PARAM_OPERATION_DHKEM },
    { 0, NULL }
};

int ossl_eckem_modename2id(const char *name)
{
    size_t i;

    if (name == NULL)
        return KEM_MODE_UNDEFINED;

    for (i = 0; eckem_modename_id_map[i].mode != NULL; ++i) {
        if (OPENSSL_strcasecmp(name, eckem_modename_id_map[i].mode) == 0)
            return eckem_modename_id_map[i].id;
    }
    return KEM_MODE_UNDEFINED;
}

/* suiteid = concat("KEM", I2OSP(kem_id, 2)) */
void ossl_dhkem_getsuiteid(unsigned char suiteid[5], uint16_t kemid)
{
    memcpy(suiteid, "KEM", 3);
    suiteid[3] = kemid >> 8;
    suiteid[4] = kemid & 0xFF;
}