|
| 1 | +/* |
| 2 | + * The MIT License (MIT) Copyright (c) 2015 Daniel Kubec <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"),to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#ifndef __CRYPTO_ECC_H__ |
| 24 | +#define __CRYPTO_ECC_H__ |
| 25 | + |
| 26 | +/* |
| 27 | + * TLS supported groups (named curves and PQ hybrids) as a pluggable |
| 28 | + * subsystem, mirroring the cipher/digest registries. A "group" is a |
| 29 | + * key-exchange group negotiated through the TLS "supported_groups" (RFC 8446 |
| 30 | + * / RFC 4492 "elliptic_curves") and "key_share" extensions. The registry |
| 31 | + * carries the metadata the passive dissector needs to identify a group and |
| 32 | + * size its key_share, plus optional keygen/derive hooks a backend may wire to |
| 33 | + * an accelerated (aws-lc) or portable implementation when a private key is |
| 34 | + * available. |
| 35 | + * |
| 36 | + * Two backends implement this interface, selected in Kconfig exactly like the |
| 37 | + * cipher backends: |
| 38 | + * modules/group/generic portable, identification-only (no keygen/derive) |
| 39 | + * modules/group/aws aws-lc accelerated keygen/derive for the classical |
| 40 | + * ECDHE groups (X25519, secp256r1/384r1/521r1) |
| 41 | + * |
| 42 | + * Group identifiers are the IANA "TLS Supported Groups" code points: |
| 43 | + * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml |
| 44 | + */ |
| 45 | +#include <hpc/compiler.h> |
| 46 | + |
| 47 | +/* Group family. */ |
| 48 | +enum group_category { |
| 49 | + GROUP_CAT_NONE = 0, |
| 50 | + GROUP_CAT_ECDHE, /* prime/binary/montgomery curve ECDHE */ |
| 51 | + GROUP_CAT_FFDHE, /* finite-field DHE (RFC 7919) */ |
| 52 | + GROUP_CAT_HYBRID, /* classical + post-quantum KEM hybrid */ |
| 53 | + GROUP_CAT_LAST |
| 54 | +}; |
| 55 | + |
| 56 | +/* |
| 57 | + * IANA TLS Supported Groups code points. The value is the on-the-wire u16 |
| 58 | + * carried in the supported_groups / key_share extensions. |
| 59 | + */ |
| 60 | +enum group_id { |
| 61 | + /* Elliptic-curve groups (RFC 8422 / RFC 8446) */ |
| 62 | + GROUP_SECP256R1 = 0x0017, |
| 63 | + GROUP_SECP384R1 = 0x0018, |
| 64 | + GROUP_SECP521R1 = 0x0019, |
| 65 | + GROUP_X25519 = 0x001d, |
| 66 | + GROUP_X448 = 0x001e, |
| 67 | + /* Finite-field DHE groups (RFC 7919) */ |
| 68 | + GROUP_FFDHE2048 = 0x0100, |
| 69 | + GROUP_FFDHE3072 = 0x0101, |
| 70 | + GROUP_FFDHE4096 = 0x0102, |
| 71 | + /* Post-quantum hybrids (RFC 9370 / drafts) */ |
| 72 | + GROUP_X25519MLKEM768 = 0x11ec, |
| 73 | + GROUP_SECP256R1MLKEM768 = 0x11eb, |
| 74 | + GROUP_SECP384R1MLKEM1024 = 0x11ed, |
| 75 | + GROUP_X25519KYBER768D00 = 0x6399, |
| 76 | + GROUP_SECP256R1KYBER768D00 = 0x639a, |
| 77 | +}; |
| 78 | + |
| 79 | +struct group_algorithm; |
| 80 | + |
| 81 | +/* |
| 82 | + * keygen: sample an ephemeral private key and write the matching public |
| 83 | + * key_share into @pub (public_key_size bytes). Returns 0 on success. |
| 84 | + * |
| 85 | + * derive: given our @priv (private_key_size bytes) and the peer's key_share |
| 86 | + * @peer (@peer_len bytes), write the shared secret into @ss |
| 87 | + * (shared_secret_size bytes). Returns 0 on success. |
| 88 | + * |
| 89 | + * A backend that only identifies groups (the generic fallback) leaves both |
| 90 | + * hooks NULL. |
| 91 | + */ |
| 92 | +typedef int (*fn_group_keygen)(const struct group_algorithm *g, |
| 93 | + u8 *priv, u8 *pub); |
| 94 | +typedef int (*fn_group_derive)(const struct group_algorithm *g, |
| 95 | + const u8 *priv, |
| 96 | + const u8 *peer, unsigned int peer_len, |
| 97 | + u8 *ss); |
| 98 | +typedef int (*fn_group_enum)(struct group_algorithm *); |
| 99 | + |
| 100 | +struct group_algorithm { |
| 101 | + unsigned int id; /* IANA code point (enum group_id) */ |
| 102 | + unsigned int category; /* enum group_category */ |
| 103 | + unsigned int private_key_size; /* ephemeral scalar length */ |
| 104 | + unsigned int public_key_size; /* our key_share length */ |
| 105 | + unsigned int shared_secret_size;/* derived secret length */ |
| 106 | + unsigned int tls12; /* usable in TLS 1.2 (RFC 4492/7919) */ |
| 107 | + unsigned int tls13; /* usable in TLS 1.3 (RFC 8446) */ |
| 108 | + const char *name; /* IANA name, e.g. "x25519" */ |
| 109 | + const char *desc; /* human-readable description */ |
| 110 | + fn_group_keygen keygen; |
| 111 | + fn_group_derive derive; |
| 112 | +}; |
| 113 | + |
| 114 | +void crypto_group_register(struct group_algorithm *alg); |
| 115 | +struct group_algorithm *crypto_group_by_id(unsigned int id); |
| 116 | +void crypto_group_enum(fn_group_enum fn); |
| 117 | + |
| 118 | +/* Convenience: IANA name for a code point, or NULL if unknown/unregistered. */ |
| 119 | +const char *crypto_group_name(unsigned int id); |
| 120 | + |
| 121 | +#if !defined(CONFIG_MODULES) && !defined(__CRYPTO_GROUP_MODULE__) |
| 122 | +#define __CRYPTO_GROUP_BUILT_IN_READY__ |
| 123 | +#ifdef CONFIG_CRYPTO_GROUP |
| 124 | +#include <modules/group/built-in.h> |
| 125 | +#endif |
| 126 | +#undef __CRYPTO_GROUP_BUILT_IN_READY__ |
| 127 | +#endif |
| 128 | + |
| 129 | +#endif |
0 commit comments