xref: /openssl/util/perl/OpenSSL/paramnames.pm (revision ef7967d0)
1#! /usr/bin/env perl
2# Copyright 2023-2024 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
9package OpenSSL::paramnames;
10
11use strict;
12use warnings;
13
14require Exporter;
15our @ISA = qw(Exporter);
16our @EXPORT_OK = qw(generate_public_macros
17                    generate_internal_macros
18                    produce_decoder);
19
20my $case_sensitive = 1;
21
22my %params = (
23# Well known parameter names that core passes to providers
24    'PROV_PARAM_CORE_VERSION' =>         "openssl-version",# utf8_ptr
25    'PROV_PARAM_CORE_PROV_NAME' =>       "provider-name",  # utf8_ptr
26    'PROV_PARAM_CORE_MODULE_FILENAME' => "module-filename",# utf8_ptr
27
28# Well known parameter names that Providers can define
29    'PROV_PARAM_NAME' =>               "name",               # utf8_ptr
30    'PROV_PARAM_VERSION' =>            "version",            # utf8_ptr
31    'PROV_PARAM_BUILDINFO' =>          "buildinfo",          # utf8_ptr
32    'PROV_PARAM_STATUS' =>             "status",             # uint
33    'PROV_PARAM_SECURITY_CHECKS' =>    "security-checks",    # uint
34    'PROV_PARAM_HMAC_KEY_CHECK' =>     "hmac-key-check",     # uint
35    'PROV_PARAM_KMAC_KEY_CHECK' =>     "kmac-key-check",     # uint
36    'PROV_PARAM_TLS1_PRF_EMS_CHECK' => "tls1-prf-ems-check", # uint
37    'PROV_PARAM_NO_SHORT_MAC' =>       "no-short-mac",       # uint
38    'PROV_PARAM_DRBG_TRUNC_DIGEST' =>  "drbg-no-trunc-md",   # uint
39    'PROV_PARAM_HKDF_DIGEST_CHECK' =>      "hkdf-digest-check",      # uint
40    'PROV_PARAM_TLS13_KDF_DIGEST_CHECK' => "tls13-kdf-digest-check", # uint
41    'PROV_PARAM_TLS1_PRF_DIGEST_CHECK' =>  "tls1-prf-digest-check",  # uint
42    'PROV_PARAM_SSHKDF_DIGEST_CHECK' =>    "sshkdf-digest-check",    # uint
43    'PROV_PARAM_SSKDF_DIGEST_CHECK' =>     "sskdf-digest-check",     # uint
44    'PROV_PARAM_X963KDF_DIGEST_CHECK' =>   "x963kdf-digest-check",   # uint
45    'PROV_PARAM_DSA_SIGN_DISABLED' =>      "dsa-sign-disabled",      # uint
46    'PROV_PARAM_TDES_ENCRYPT_DISABLED' =>  "tdes-encrypt-disabled",  # uint
47    'PROV_PARAM_RSA_PSS_SALTLEN_CHECK' =>  "rsa-pss-saltlen-check",  # uint
48    'PROV_PARAM_RSA_SIGN_X931_PAD_DISABLED' =>  "rsa-sign-x931-pad-disabled",   # uint
49    'PROV_PARAM_RSA_PKCS15_PAD_DISABLED' => "rsa-pkcs15-pad-disabled", # uint
50    'PROV_PARAM_HKDF_KEY_CHECK' =>         "hkdf-key-check",         # uint
51    'PROV_PARAM_KBKDF_KEY_CHECK' =>        "kbkdf-key-check",        # uint
52    'PROV_PARAM_TLS13_KDF_KEY_CHECK' =>    "tls13-kdf-key-check",    # uint
53    'PROV_PARAM_TLS1_PRF_KEY_CHECK' =>     "tls1-prf-key-check",     # uint
54    'PROV_PARAM_SSHKDF_KEY_CHECK' =>       "sshkdf-key-check",       # uint
55    'PROV_PARAM_SSKDF_KEY_CHECK' =>        "sskdf-key-check",        # uint
56    'PROV_PARAM_X963KDF_KEY_CHECK' =>      "x963kdf-key-check",      # uint
57    'PROV_PARAM_X942KDF_KEY_CHECK' =>      "x942kdf-key-check",      # uint
58    'PROV_PARAM_PBKDF2_LOWER_BOUND_CHECK' => "pbkdf2-lower-bound-check", # uint
59    'PROV_PARAM_ECDH_COFACTOR_CHECK' =>    "ecdh-cofactor-check",    # uint
60    'PROV_PARAM_SIGNATURE_DIGEST_CHECK' => "signature-digest-check", # uint
61
62# Self test callback parameters
63    'PROV_PARAM_SELF_TEST_PHASE' =>  "st-phase",# utf8_string
64    'PROV_PARAM_SELF_TEST_TYPE' =>   "st-type", # utf8_string
65    'PROV_PARAM_SELF_TEST_DESC' =>   "st-desc", # utf8_string
66
67# Provider-native object abstractions
68#
69# These are used when a provider wants to pass object data or an object
70# reference back to libcrypto.  This is only useful for provider functions
71# that take a callback to which an PARAM array with these parameters
72# can be passed.
73#
74# This set of parameter names is explained in detail in provider-object(7)
75# (doc/man7/provider-object.pod)
76
77    'OBJECT_PARAM_TYPE' =>              "type",     # INTEGER
78    'OBJECT_PARAM_DATA_TYPE' =>         "data-type",# UTF8_STRING
79    'OBJECT_PARAM_DATA_STRUCTURE' =>    "data-structure",# UTF8_STRING
80    'OBJECT_PARAM_REFERENCE' =>         "reference",# OCTET_STRING
81    'OBJECT_PARAM_DATA' =>              "data",# OCTET_STRING or UTF8_STRING
82    'OBJECT_PARAM_DESC' =>              "desc",     # UTF8_STRING
83
84# Algorithm parameters
85# If "engine",or "properties",are specified, they should always be paired
86# with the algorithm type.
87# Note these are common names that are shared by many types (such as kdf, mac,
88# and pkey) e.g: see MAC_PARAM_DIGEST below.
89
90    'ALG_PARAM_DIGEST' =>       "digest",       # utf8_string
91    'ALG_PARAM_CIPHER' =>       "cipher",       # utf8_string
92    'ALG_PARAM_ENGINE' =>       "engine",       # utf8_string
93    'ALG_PARAM_MAC' =>          "mac",          # utf8_string
94    'ALG_PARAM_PROPERTIES' =>   "properties",   # utf8_string
95    'ALG_PARAM_FIPS_APPROVED_INDICATOR' => 'fips-indicator',   # int, -1, 0 or 1
96
97    # For any operation that deals with AlgorithmIdentifier, they should
98    # implement both of these.
99    # ALG_PARAM_ALGORITHM_ID is intended to be gettable, and is the
100    # implementation's idea of what its full AlgID should look like.
101    # ALG_PARAM_ALGORITHM_ID_PARAMS is intended to be both settable
102    # and gettable, to allow the calling application to pass or get
103    # AlgID parameters to and from the provided implementation.
104    'ALG_PARAM_ALGORITHM_ID' => "algorithm-id", # octet_string (DER)
105    'ALG_PARAM_ALGORITHM_ID_PARAMS' =>  "algorithm-id-params", # octet_string
106
107# cipher parameters
108    'CIPHER_PARAM_PADDING' =>              "padding",     # uint
109    'CIPHER_PARAM_USE_BITS' =>             "use-bits",    # uint
110    'CIPHER_PARAM_TLS_VERSION' =>          "tls-version", # uint
111    'CIPHER_PARAM_TLS_MAC' =>              "tls-mac",     # octet_ptr
112    'CIPHER_PARAM_TLS_MAC_SIZE' =>         "tls-mac-size",# size_t
113    'CIPHER_PARAM_MODE' =>                 "mode",        # uint
114    'CIPHER_PARAM_BLOCK_SIZE' =>           "blocksize",   # size_t
115    'CIPHER_PARAM_AEAD' =>                 "aead",        # int, 0 or 1
116    'CIPHER_PARAM_CUSTOM_IV' =>            "custom-iv",   # int, 0 or 1
117    'CIPHER_PARAM_CTS' =>                  "cts",         # int, 0 or 1
118    'CIPHER_PARAM_TLS1_MULTIBLOCK' =>      "tls-multi",   # int, 0 or 1
119    'CIPHER_PARAM_HAS_RAND_KEY' =>         "has-randkey", # int, 0 or 1
120    'CIPHER_PARAM_KEYLEN' =>               "keylen",      # size_t
121    'CIPHER_PARAM_IVLEN' =>                "ivlen",       # size_t
122    'CIPHER_PARAM_IV' =>                   "iv",          # octet_string OR octet_ptr
123    'CIPHER_PARAM_UPDATED_IV' =>           "updated-iv",  # octet_string OR octet_ptr
124    'CIPHER_PARAM_NUM' =>                  "num",         # uint
125    'CIPHER_PARAM_ROUNDS' =>               "rounds",      # uint
126    'CIPHER_PARAM_AEAD_TAG' =>             "tag",         # octet_string
127    'CIPHER_PARAM_PIPELINE_AEAD_TAG' =>    "pipeline-tag",# octet_ptr
128    'CIPHER_PARAM_AEAD_TLS1_AAD' =>        "tlsaad",      # octet_string
129    'CIPHER_PARAM_AEAD_TLS1_AAD_PAD' =>    "tlsaadpad",   # size_t
130    'CIPHER_PARAM_AEAD_TLS1_IV_FIXED' =>   "tlsivfixed",  # octet_string
131    'CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN' => "tlsivgen",    # octet_string
132    'CIPHER_PARAM_AEAD_TLS1_SET_IV_INV' => "tlsivinv",    # octet_string
133    'CIPHER_PARAM_AEAD_IVLEN' =>           '*CIPHER_PARAM_IVLEN',
134    'CIPHER_PARAM_AEAD_IV_GENERATED' => "iv-generated",   # uint
135    'CIPHER_PARAM_AEAD_TAGLEN' =>          "taglen",      # size_t
136    'CIPHER_PARAM_AEAD_MAC_KEY' =>         "mackey",      # octet_string
137    'CIPHER_PARAM_RANDOM_KEY' =>           "randkey",     # octet_string
138    'CIPHER_PARAM_RC2_KEYBITS' =>          "keybits",     # size_t
139    'CIPHER_PARAM_SPEED' =>                "speed",       # uint
140    'CIPHER_PARAM_CTS_MODE' =>             "cts_mode",    # utf8_string
141    'CIPHER_PARAM_DECRYPT_ONLY' =>         "decrypt-only",  # int, 0 or 1
142    'CIPHER_PARAM_FIPS_ENCRYPT_CHECK' =>   "encrypt-check", # int
143    'CIPHER_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
144    'CIPHER_PARAM_ALGORITHM_ID' =>         '*ALG_PARAM_ALGORITHM_ID',
145    # Historically, CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD was used.  For the
146    # time being, the old libcrypto functions will use both, so old providers
147    # continue to work.
148    # New providers are encouraged to use CIPHER_PARAM_ALGORITHM_ID_PARAMS.
149    'CIPHER_PARAM_ALGORITHM_ID_PARAMS' =>  '*ALG_PARAM_ALGORITHM_ID_PARAMS',
150    'CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD' => "alg_id_param", # octet_string
151    'CIPHER_PARAM_XTS_STANDARD' =>         "xts_standard",# utf8_string
152
153    'CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT' =>  "tls1multi_maxsndfrag",# uint
154    'CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE' =>        "tls1multi_maxbufsz",  # size_t
155    'CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE' =>         "tls1multi_interleave",# uint
156    'CIPHER_PARAM_TLS1_MULTIBLOCK_AAD' =>                "tls1multi_aad",       # octet_string
157    'CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN' =>        "tls1multi_aadpacklen",# uint
158    'CIPHER_PARAM_TLS1_MULTIBLOCK_ENC' =>                "tls1multi_enc",       # octet_string
159    'CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN' =>             "tls1multi_encin",     # octet_string
160    'CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN' =>            "tls1multi_enclen",    # size_t
161
162# digest parameters
163    'DIGEST_PARAM_XOFLEN' =>       "xoflen",       # size_t
164    'DIGEST_PARAM_SSL3_MS' =>      "ssl3-ms",      # octet string
165    'DIGEST_PARAM_PAD_TYPE' =>     "pad-type",     # uint
166    'DIGEST_PARAM_MICALG' =>       "micalg",       # utf8 string
167    'DIGEST_PARAM_BLOCK_SIZE' =>   "blocksize",    # size_t
168    'DIGEST_PARAM_SIZE' =>         "size",         # size_t
169    'DIGEST_PARAM_XOF' =>          "xof",          # int, 0 or 1
170    'DIGEST_PARAM_ALGID_ABSENT' => "algid-absent", # int, 0 or 1
171
172# MAC parameters
173    'MAC_PARAM_KEY' =>            "key",           # octet string
174    'MAC_PARAM_IV' =>             "iv",            # octet string
175    'MAC_PARAM_CUSTOM' =>         "custom",        # utf8 string
176    'MAC_PARAM_SALT' =>           "salt",          # octet string
177    'MAC_PARAM_XOF' =>            "xof",           # int, 0 or 1
178    'MAC_PARAM_DIGEST_NOINIT' =>  "digest-noinit", # int, 0 or 1
179    'MAC_PARAM_DIGEST_ONESHOT' => "digest-oneshot",# int, 0 or 1
180    'MAC_PARAM_C_ROUNDS' =>       "c-rounds",      # unsigned int
181    'MAC_PARAM_D_ROUNDS' =>       "d-rounds",      # unsigned int
182
183# If "engine",or "properties",are specified, they should always be paired
184# with "cipher",or "digest".
185
186    'MAC_PARAM_CIPHER' =>           '*ALG_PARAM_CIPHER',        # utf8 string
187    'MAC_PARAM_DIGEST' =>           '*ALG_PARAM_DIGEST',        # utf8 string
188    'MAC_PARAM_PROPERTIES' =>       '*ALG_PARAM_PROPERTIES',    # utf8 string
189    'MAC_PARAM_SIZE' =>             "size",                     # size_t
190    'MAC_PARAM_BLOCK_SIZE' =>       "block-size",               # size_t
191    'MAC_PARAM_TLS_DATA_SIZE' =>    "tls-data-size",            # size_t
192    'MAC_PARAM_FIPS_NO_SHORT_MAC' =>'*PROV_PARAM_NO_SHORT_MAC',
193    'MAC_PARAM_FIPS_KEY_CHECK' =>   '*PKEY_PARAM_FIPS_KEY_CHECK',
194    'MAC_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
195    'MAC_PARAM_FIPS_NO_SHORT_MAC' => '*PROV_PARAM_NO_SHORT_MAC',
196
197# KDF / PRF parameters
198    'KDF_PARAM_SECRET' =>       "secret",                   # octet string
199    'KDF_PARAM_KEY' =>          "key",                      # octet string
200    'KDF_PARAM_SALT' =>         "salt",                     # octet string
201    'KDF_PARAM_PASSWORD' =>     "pass",                     # octet string
202    'KDF_PARAM_PREFIX' =>       "prefix",                   # octet string
203    'KDF_PARAM_LABEL' =>        "label",                    # octet string
204    'KDF_PARAM_DATA' =>         "data",                     # octet string
205    'KDF_PARAM_DIGEST' =>       '*ALG_PARAM_DIGEST',        # utf8 string
206    'KDF_PARAM_CIPHER' =>       '*ALG_PARAM_CIPHER',        # utf8 string
207    'KDF_PARAM_MAC' =>          '*ALG_PARAM_MAC',           # utf8 string
208    'KDF_PARAM_MAC_SIZE' =>     "maclen",                   # size_t
209    'KDF_PARAM_PROPERTIES' =>   '*ALG_PARAM_PROPERTIES',    # utf8 string
210    'KDF_PARAM_ITER' =>         "iter",                     # unsigned int
211    'KDF_PARAM_MODE' =>         "mode",                     # utf8 string or int
212    'KDF_PARAM_PKCS5' =>        "pkcs5",                    # int
213    'KDF_PARAM_UKM' =>          "ukm",                      # octet string
214    'KDF_PARAM_CEK_ALG' =>      "cekalg",                   # utf8 string
215    'KDF_PARAM_SCRYPT_N' =>     "n",                        # uint64_t
216    'KDF_PARAM_SCRYPT_R' =>     "r",                        # uint32_t
217    'KDF_PARAM_SCRYPT_P' =>     "p",                        # uint32_t
218    'KDF_PARAM_SCRYPT_MAXMEM' => "maxmem_bytes",            # uint64_t
219    'KDF_PARAM_INFO' =>         "info",                     # octet string
220    'KDF_PARAM_SEED' =>         "seed",                     # octet string
221    'KDF_PARAM_SSHKDF_XCGHASH' => "xcghash",                # octet string
222    'KDF_PARAM_SSHKDF_SESSION_ID' => "session_id",          # octet string
223    'KDF_PARAM_SSHKDF_TYPE' =>  "type",                     # int
224    'KDF_PARAM_SIZE' =>         "size",                     # size_t
225    'KDF_PARAM_CONSTANT' =>     "constant",                 # octet string
226    'KDF_PARAM_PKCS12_ID' =>    "id",                       # int
227    'KDF_PARAM_KBKDF_USE_L' =>          "use-l",            # int
228    'KDF_PARAM_KBKDF_USE_SEPARATOR' =>  "use-separator",    # int
229    'KDF_PARAM_KBKDF_R' =>      "r",                        # int
230    'KDF_PARAM_X942_ACVPINFO' =>        "acvp-info",
231    'KDF_PARAM_X942_PARTYUINFO' =>      "partyu-info",
232    'KDF_PARAM_X942_PARTYVINFO' =>      "partyv-info",
233    'KDF_PARAM_X942_SUPP_PUBINFO' =>    "supp-pubinfo",
234    'KDF_PARAM_X942_SUPP_PRIVINFO' =>   "supp-privinfo",
235    'KDF_PARAM_X942_USE_KEYBITS' =>     "use-keybits",
236    'KDF_PARAM_HMACDRBG_ENTROPY' =>     "entropy",
237    'KDF_PARAM_HMACDRBG_NONCE' =>       "nonce",
238    'KDF_PARAM_THREADS' =>        "threads",                # uint32_t
239    'KDF_PARAM_EARLY_CLEAN' =>    "early_clean",            # uint32_t
240    'KDF_PARAM_ARGON2_AD' =>      "ad",                     # octet string
241    'KDF_PARAM_ARGON2_LANES' =>   "lanes",                  # uint32_t
242    'KDF_PARAM_ARGON2_MEMCOST' => "memcost",                # uint32_t
243    'KDF_PARAM_ARGON2_VERSION' => "version",                # uint32_t
244    'KDF_PARAM_FIPS_EMS_CHECK' => "ems_check",              # int
245    'KDF_PARAM_FIPS_DIGEST_CHECK' => '*PKEY_PARAM_FIPS_DIGEST_CHECK',
246    'KDF_PARAM_FIPS_KEY_CHECK' => '*PKEY_PARAM_FIPS_KEY_CHECK',
247    'KDF_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
248
249# Known RAND names
250    'RAND_PARAM_STATE' =>                   "state",
251    'RAND_PARAM_STRENGTH' =>                "strength",
252    'RAND_PARAM_MAX_REQUEST' =>             "max_request",
253    'RAND_PARAM_TEST_ENTROPY' =>            "test_entropy",
254    'RAND_PARAM_TEST_NONCE' =>              "test_nonce",
255    'RAND_PARAM_GENERATE' =>                "generate",
256    'RAND_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
257
258# RAND/DRBG names
259    'DRBG_PARAM_RESEED_REQUESTS' =>         "reseed_requests",
260    'DRBG_PARAM_RESEED_TIME_INTERVAL' =>    "reseed_time_interval",
261    'DRBG_PARAM_MIN_ENTROPYLEN' =>          "min_entropylen",
262    'DRBG_PARAM_MAX_ENTROPYLEN' =>          "max_entropylen",
263    'DRBG_PARAM_MIN_NONCELEN' =>            "min_noncelen",
264    'DRBG_PARAM_MAX_NONCELEN' =>            "max_noncelen",
265    'DRBG_PARAM_MAX_PERSLEN' =>             "max_perslen",
266    'DRBG_PARAM_MAX_ADINLEN' =>             "max_adinlen",
267    'DRBG_PARAM_RESEED_COUNTER' =>          "reseed_counter",
268    'DRBG_PARAM_RESEED_TIME' =>             "reseed_time",
269    'DRBG_PARAM_PROPERTIES' =>              '*ALG_PARAM_PROPERTIES',
270    'DRBG_PARAM_DIGEST' =>                  '*ALG_PARAM_DIGEST',
271    'DRBG_PARAM_CIPHER' =>                  '*ALG_PARAM_CIPHER',
272    'DRBG_PARAM_MAC' =>                     '*ALG_PARAM_MAC',
273    'DRBG_PARAM_USE_DF' =>                  "use_derivation_function",
274    'DRBG_PARAM_FIPS_DIGEST_CHECK' =>       '*PKEY_PARAM_FIPS_DIGEST_CHECK',
275    'DRBG_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
276
277# DRBG call back parameters
278    'DRBG_PARAM_ENTROPY_REQUIRED' =>        "entropy_required",
279    'DRBG_PARAM_PREDICTION_RESISTANCE' =>   "prediction_resistance",
280    'DRBG_PARAM_MIN_LENGTH' =>              "minium_length",
281    'DRBG_PARAM_MAX_LENGTH' =>              "maxium_length",
282    'DRBG_PARAM_RANDOM_DATA' =>             "random_data",
283    'DRBG_PARAM_SIZE' =>                    "size",
284
285# PKEY parameters
286# Common PKEY parameters
287    'PKEY_PARAM_BITS' =>                "bits",# integer
288    'PKEY_PARAM_MAX_SIZE' =>            "max-size",# integer
289    'PKEY_PARAM_SECURITY_BITS' =>       "security-bits",# integer
290    'PKEY_PARAM_DIGEST' =>              '*ALG_PARAM_DIGEST',
291    'PKEY_PARAM_CIPHER' =>              '*ALG_PARAM_CIPHER', # utf8 string
292    'PKEY_PARAM_ENGINE' =>              '*ALG_PARAM_ENGINE', # utf8 string
293    'PKEY_PARAM_PROPERTIES' =>          '*ALG_PARAM_PROPERTIES',
294    'PKEY_PARAM_DEFAULT_DIGEST' =>      "default-digest",# utf8 string
295    'PKEY_PARAM_MANDATORY_DIGEST' =>    "mandatory-digest",# utf8 string
296    'PKEY_PARAM_PAD_MODE' =>            "pad-mode",
297    'PKEY_PARAM_DIGEST_SIZE' =>         "digest-size",
298    'PKEY_PARAM_MASKGENFUNC' =>         "mgf",
299    'PKEY_PARAM_MGF1_DIGEST' =>         "mgf1-digest",
300    'PKEY_PARAM_MGF1_PROPERTIES' =>     "mgf1-properties",
301    'PKEY_PARAM_ENCODED_PUBLIC_KEY' =>  "encoded-pub-key",
302    'PKEY_PARAM_GROUP_NAME' =>          "group",
303    'PKEY_PARAM_DIST_ID' =>             "distid",
304    'PKEY_PARAM_PUB_KEY' =>             "pub",
305    'PKEY_PARAM_PRIV_KEY' =>            "priv",
306    'PKEY_PARAM_IMPLICIT_REJECTION' =>  "implicit-rejection",
307    'PKEY_PARAM_FIPS_DIGEST_CHECK' =>   "digest-check",
308    'PKEY_PARAM_FIPS_KEY_CHECK' =>      "key-check",
309    'PKEY_PARAM_ALGORITHM_ID' =>        '*ALG_PARAM_ALGORITHM_ID',
310    'PKEY_PARAM_ALGORITHM_ID_PARAMS' => '*ALG_PARAM_ALGORITHM_ID_PARAMS',
311
312# Diffie-Hellman/DSA Parameters
313    'PKEY_PARAM_FFC_P' =>               "p",
314    'PKEY_PARAM_FFC_G' =>               "g",
315    'PKEY_PARAM_FFC_Q' =>               "q",
316    'PKEY_PARAM_FFC_GINDEX' =>          "gindex",
317    'PKEY_PARAM_FFC_PCOUNTER' =>        "pcounter",
318    'PKEY_PARAM_FFC_SEED' =>            "seed",
319    'PKEY_PARAM_FFC_COFACTOR' =>        "j",
320    'PKEY_PARAM_FFC_H' =>               "hindex",
321    'PKEY_PARAM_FFC_VALIDATE_PQ' =>     "validate-pq",
322    'PKEY_PARAM_FFC_VALIDATE_G' =>      "validate-g",
323    'PKEY_PARAM_FFC_VALIDATE_LEGACY' => "validate-legacy",
324
325# Diffie-Hellman params
326    'PKEY_PARAM_DH_GENERATOR' =>        "safeprime-generator",
327    'PKEY_PARAM_DH_PRIV_LEN' =>         "priv_len",
328
329# Elliptic Curve Domain Parameters
330    'PKEY_PARAM_EC_PUB_X' =>     "qx",
331    'PKEY_PARAM_EC_PUB_Y' =>     "qy",
332
333# Elliptic Curve Explicit Domain Parameters
334    'PKEY_PARAM_EC_FIELD_TYPE' =>                   "field-type",
335    'PKEY_PARAM_EC_P' =>                            "p",
336    'PKEY_PARAM_EC_A' =>                            "a",
337    'PKEY_PARAM_EC_B' =>                            "b",
338    'PKEY_PARAM_EC_GENERATOR' =>                    "generator",
339    'PKEY_PARAM_EC_ORDER' =>                        "order",
340    'PKEY_PARAM_EC_COFACTOR' =>                     "cofactor",
341    'PKEY_PARAM_EC_SEED' =>                         "seed",
342    'PKEY_PARAM_EC_CHAR2_M' =>                      "m",
343    'PKEY_PARAM_EC_CHAR2_TYPE' =>                   "basis-type",
344    'PKEY_PARAM_EC_CHAR2_TP_BASIS' =>               "tp",
345    'PKEY_PARAM_EC_CHAR2_PP_K1' =>                  "k1",
346    'PKEY_PARAM_EC_CHAR2_PP_K2' =>                  "k2",
347    'PKEY_PARAM_EC_CHAR2_PP_K3' =>                  "k3",
348    'PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS' => "decoded-from-explicit",
349
350# Elliptic Curve Key Parameters
351    'PKEY_PARAM_USE_COFACTOR_FLAG' => "use-cofactor-flag",
352    'PKEY_PARAM_USE_COFACTOR_ECDH' => '*PKEY_PARAM_USE_COFACTOR_FLAG',
353
354# RSA Keys
355#
356# n, e, d are the usual public and private key components
357#
358# rsa-num is the number of factors, including p and q
359# rsa-factor is used for each factor: p, q, r_i (i = 3, ...)
360# rsa-exponent is used for each exponent: dP, dQ, d_i (i = 3, ...)
361# rsa-coefficient is used for each coefficient: qInv, t_i (i = 3, ...)
362#
363# The number of rsa-factor items must be equal to the number of rsa-exponent
364# items, and the number of rsa-coefficients must be one less.
365# (the base i for the coefficients is 2, not 1, at least as implied by
366# RFC 8017)
367
368    'PKEY_PARAM_RSA_N' =>           "n",
369    'PKEY_PARAM_RSA_E' =>           "e",
370    'PKEY_PARAM_RSA_D' =>           "d",
371    'PKEY_PARAM_RSA_FACTOR' =>      "rsa-factor",
372    'PKEY_PARAM_RSA_EXPONENT' =>    "rsa-exponent",
373    'PKEY_PARAM_RSA_COEFFICIENT' => "rsa-coefficient",
374    'PKEY_PARAM_RSA_FACTOR1' =>      "rsa-factor1",
375    'PKEY_PARAM_RSA_FACTOR2' =>      "rsa-factor2",
376    'PKEY_PARAM_RSA_FACTOR3' =>      "rsa-factor3",
377    'PKEY_PARAM_RSA_FACTOR4' =>      "rsa-factor4",
378    'PKEY_PARAM_RSA_FACTOR5' =>      "rsa-factor5",
379    'PKEY_PARAM_RSA_FACTOR6' =>      "rsa-factor6",
380    'PKEY_PARAM_RSA_FACTOR7' =>      "rsa-factor7",
381    'PKEY_PARAM_RSA_FACTOR8' =>      "rsa-factor8",
382    'PKEY_PARAM_RSA_FACTOR9' =>      "rsa-factor9",
383    'PKEY_PARAM_RSA_FACTOR10' =>     "rsa-factor10",
384    'PKEY_PARAM_RSA_EXPONENT1' =>    "rsa-exponent1",
385    'PKEY_PARAM_RSA_EXPONENT2' =>    "rsa-exponent2",
386    'PKEY_PARAM_RSA_EXPONENT3' =>    "rsa-exponent3",
387    'PKEY_PARAM_RSA_EXPONENT4' =>    "rsa-exponent4",
388    'PKEY_PARAM_RSA_EXPONENT5' =>    "rsa-exponent5",
389    'PKEY_PARAM_RSA_EXPONENT6' =>    "rsa-exponent6",
390    'PKEY_PARAM_RSA_EXPONENT7' =>    "rsa-exponent7",
391    'PKEY_PARAM_RSA_EXPONENT8' =>    "rsa-exponent8",
392    'PKEY_PARAM_RSA_EXPONENT9' =>    "rsa-exponent9",
393    'PKEY_PARAM_RSA_EXPONENT10' =>   "rsa-exponent10",
394    'PKEY_PARAM_RSA_COEFFICIENT1' => "rsa-coefficient1",
395    'PKEY_PARAM_RSA_COEFFICIENT2' => "rsa-coefficient2",
396    'PKEY_PARAM_RSA_COEFFICIENT3' => "rsa-coefficient3",
397    'PKEY_PARAM_RSA_COEFFICIENT4' => "rsa-coefficient4",
398    'PKEY_PARAM_RSA_COEFFICIENT5' => "rsa-coefficient5",
399    'PKEY_PARAM_RSA_COEFFICIENT6' => "rsa-coefficient6",
400    'PKEY_PARAM_RSA_COEFFICIENT7' => "rsa-coefficient7",
401    'PKEY_PARAM_RSA_COEFFICIENT8' => "rsa-coefficient8",
402    'PKEY_PARAM_RSA_COEFFICIENT9' => "rsa-coefficient9",
403
404# Key generation parameters
405    'PKEY_PARAM_RSA_BITS' =>             '*PKEY_PARAM_BITS',
406    'PKEY_PARAM_RSA_PRIMES' =>           "primes",
407    'PKEY_PARAM_RSA_DIGEST' =>           '*PKEY_PARAM_DIGEST',
408    'PKEY_PARAM_RSA_DIGEST_PROPS' =>     '*PKEY_PARAM_PROPERTIES',
409    'PKEY_PARAM_RSA_MASKGENFUNC' =>      '*PKEY_PARAM_MASKGENFUNC',
410    'PKEY_PARAM_RSA_MGF1_DIGEST' =>      '*PKEY_PARAM_MGF1_DIGEST',
411    'PKEY_PARAM_RSA_PSS_SALTLEN' =>      "saltlen",
412    'PKEY_PARAM_RSA_DERIVE_FROM_PQ'    =>     "rsa-derive-from-pq",
413
414# EC, X25519 and X448 Key generation parameters
415    'PKEY_PARAM_DHKEM_IKM' =>        "dhkem-ikm",
416
417# Key generation parameters
418    'PKEY_PARAM_FFC_TYPE' =>         "type",
419    'PKEY_PARAM_FFC_PBITS' =>        "pbits",
420    'PKEY_PARAM_FFC_QBITS' =>        "qbits",
421    'PKEY_PARAM_FFC_DIGEST' =>       '*PKEY_PARAM_DIGEST',
422    'PKEY_PARAM_FFC_DIGEST_PROPS' => '*PKEY_PARAM_PROPERTIES',
423
424    'PKEY_PARAM_EC_ENCODING' =>                "encoding",# utf8_string
425    'PKEY_PARAM_EC_POINT_CONVERSION_FORMAT' => "point-format",
426    'PKEY_PARAM_EC_GROUP_CHECK_TYPE' =>        "group-check",
427    'PKEY_PARAM_EC_INCLUDE_PUBLIC' =>          "include-public",
428    'PKEY_PARAM_FIPS_SIGN_CHECK' =>            "sign-check",
429    'PKEY_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
430
431# Key Exchange parameters
432    'EXCHANGE_PARAM_PAD' =>                   "pad",# uint
433    'EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE' => "ecdh-cofactor-mode",# int
434    'EXCHANGE_PARAM_KDF_TYPE' =>              "kdf-type",# utf8_string
435    'EXCHANGE_PARAM_KDF_DIGEST' =>            "kdf-digest",# utf8_string
436    'EXCHANGE_PARAM_KDF_DIGEST_PROPS' =>      "kdf-digest-props",# utf8_string
437    'EXCHANGE_PARAM_KDF_OUTLEN' =>            "kdf-outlen",# size_t
438# The following parameter is an octet_string on set and an octet_ptr on get
439    'EXCHANGE_PARAM_KDF_UKM' =>               "kdf-ukm",
440    'EXCHANGE_PARAM_FIPS_DIGEST_CHECK' =>     '*PKEY_PARAM_FIPS_DIGEST_CHECK',
441    'EXCHANGE_PARAM_FIPS_KEY_CHECK' =>        '*PKEY_PARAM_FIPS_KEY_CHECK',
442    'EXCHANGE_PARAM_FIPS_ECDH_COFACTOR_CHECK' => '*PROV_PARAM_ECDH_COFACTOR_CHECK',
443    'EXCHANGE_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
444
445# Signature parameters
446    'SIGNATURE_PARAM_ALGORITHM_ID' =>         '*PKEY_PARAM_ALGORITHM_ID',
447    'SIGNATURE_PARAM_ALGORITHM_ID_PARAMS' =>  '*PKEY_PARAM_ALGORITHM_ID_PARAMS',
448    'SIGNATURE_PARAM_PAD_MODE' =>             '*PKEY_PARAM_PAD_MODE',
449    'SIGNATURE_PARAM_DIGEST' =>               '*PKEY_PARAM_DIGEST',
450    'SIGNATURE_PARAM_PROPERTIES' =>           '*PKEY_PARAM_PROPERTIES',
451    'SIGNATURE_PARAM_PSS_SALTLEN' =>          "saltlen",
452    'SIGNATURE_PARAM_MGF1_DIGEST' =>          '*PKEY_PARAM_MGF1_DIGEST',
453    'SIGNATURE_PARAM_MGF1_PROPERTIES' =>      '*PKEY_PARAM_MGF1_PROPERTIES',
454    'SIGNATURE_PARAM_DIGEST_SIZE' =>          '*PKEY_PARAM_DIGEST_SIZE',
455    'SIGNATURE_PARAM_NONCE_TYPE' =>           "nonce-type",
456    'SIGNATURE_PARAM_INSTANCE' =>             "instance",
457    'SIGNATURE_PARAM_CONTEXT_STRING' =>       "context-string",
458    'SIGNATURE_PARAM_FIPS_DIGEST_CHECK' =>    '*PKEY_PARAM_FIPS_DIGEST_CHECK',
459    'SIGNATURE_PARAM_FIPS_VERIFY_MESSAGE' =>  'verify-message',
460    'SIGNATURE_PARAM_FIPS_KEY_CHECK' =>       '*PKEY_PARAM_FIPS_KEY_CHECK',
461    'SIGNATURE_PARAM_FIPS_SIGN_CHECK' =>      '*PKEY_PARAM_FIPS_SIGN_CHECK',
462    'SIGNATURE_PARAM_FIPS_RSA_PSS_SALTLEN_CHECK' => "rsa-pss-saltlen-check",
463    'SIGNATURE_PARAM_FIPS_SIGN_X931_PAD_CHECK' => "sign-x931-pad-check",
464    'SIGNATURE_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
465    'SIGNATURE_PARAM_SIGNATURE' =>          "signature",
466
467# Asym cipher parameters
468    'ASYM_CIPHER_PARAM_DIGEST' =>                   '*PKEY_PARAM_DIGEST',
469    'ASYM_CIPHER_PARAM_PROPERTIES' =>               '*PKEY_PARAM_PROPERTIES',
470    'ASYM_CIPHER_PARAM_ENGINE' =>                   '*PKEY_PARAM_ENGINE',
471    'ASYM_CIPHER_PARAM_PAD_MODE' =>                 '*PKEY_PARAM_PAD_MODE',
472    'ASYM_CIPHER_PARAM_MGF1_DIGEST' =>              '*PKEY_PARAM_MGF1_DIGEST',
473    'ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS' =>        '*PKEY_PARAM_MGF1_PROPERTIES',
474    'ASYM_CIPHER_PARAM_OAEP_DIGEST' =>              '*ALG_PARAM_DIGEST',
475    'ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS' =>        "digest-props",
476# The following parameter is an octet_string on set and an octet_ptr on get
477    'ASYM_CIPHER_PARAM_OAEP_LABEL' =>               "oaep-label",
478    'ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION' =>       "tls-client-version",
479    'ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION' =>   "tls-negotiated-version",
480    'ASYM_CIPHER_PARAM_IMPLICIT_REJECTION' =>       "implicit-rejection",
481    'ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED' => '*PROV_PARAM_RSA_PKCS15_PAD_DISABLED',
482    'ASYM_CIPHER_PARAM_FIPS_KEY_CHECK' =>           '*PKEY_PARAM_FIPS_KEY_CHECK',
483    'ASYM_CIPHER_PARAM_FIPS_APPROVED_INDICATOR' =>  '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
484
485# Encoder / decoder parameters
486
487    'ENCODER_PARAM_CIPHER' =>           '*ALG_PARAM_CIPHER',
488    'ENCODER_PARAM_PROPERTIES' =>       '*ALG_PARAM_PROPERTIES',
489# Currently PVK only, but reusable for others as needed
490    'ENCODER_PARAM_ENCRYPT_LEVEL' =>    "encrypt-level",
491    'ENCODER_PARAM_SAVE_PARAMETERS' =>  "save-parameters",# integer
492
493    'DECODER_PARAM_PROPERTIES' =>       '*ALG_PARAM_PROPERTIES',
494
495# Passphrase callback parameters
496    'PASSPHRASE_PARAM_INFO' =>          "info",
497
498# Keygen callback parameters, from provider to libcrypto
499    'GEN_PARAM_POTENTIAL' =>            "potential",# integer
500    'GEN_PARAM_ITERATION' =>            "iteration",# integer
501
502# ACVP Test parameters : These should not be used normally
503    'PKEY_PARAM_RSA_TEST_XP1' => "xp1",
504    'PKEY_PARAM_RSA_TEST_XP2' => "xp2",
505    'PKEY_PARAM_RSA_TEST_XP' =>  "xp",
506    'PKEY_PARAM_RSA_TEST_XQ1' => "xq1",
507    'PKEY_PARAM_RSA_TEST_XQ2' => "xq2",
508    'PKEY_PARAM_RSA_TEST_XQ' =>  "xq",
509    'PKEY_PARAM_RSA_TEST_P1' =>  "p1",
510    'PKEY_PARAM_RSA_TEST_P2' =>  "p2",
511    'PKEY_PARAM_RSA_TEST_Q1' =>  "q1",
512    'PKEY_PARAM_RSA_TEST_Q2' =>  "q2",
513    'SIGNATURE_PARAM_KAT' =>     "kat",
514
515# KEM parameters
516    'KEM_PARAM_OPERATION' =>            "operation",
517    'KEM_PARAM_IKME' =>                 "ikme",
518    'KEM_PARAM_FIPS_KEY_CHECK' =>       '*PKEY_PARAM_FIPS_KEY_CHECK',
519    'KEM_PARAM_FIPS_APPROVED_INDICATOR' => '*ALG_PARAM_FIPS_APPROVED_INDICATOR',
520
521# Capabilities
522
523# TLS-GROUP Capability
524    'CAPABILITY_TLS_GROUP_NAME' =>              "tls-group-name",
525    'CAPABILITY_TLS_GROUP_NAME_INTERNAL' =>     "tls-group-name-internal",
526    'CAPABILITY_TLS_GROUP_ID' =>                "tls-group-id",
527    'CAPABILITY_TLS_GROUP_ALG' =>               "tls-group-alg",
528    'CAPABILITY_TLS_GROUP_SECURITY_BITS' =>     "tls-group-sec-bits",
529    'CAPABILITY_TLS_GROUP_IS_KEM' =>            "tls-group-is-kem",
530    'CAPABILITY_TLS_GROUP_MIN_TLS' =>           "tls-min-tls",
531    'CAPABILITY_TLS_GROUP_MAX_TLS' =>           "tls-max-tls",
532    'CAPABILITY_TLS_GROUP_MIN_DTLS' =>          "tls-min-dtls",
533    'CAPABILITY_TLS_GROUP_MAX_DTLS' =>          "tls-max-dtls",
534
535# TLS-SIGALG Capability
536    'CAPABILITY_TLS_SIGALG_IANA_NAME' =>         "tls-sigalg-iana-name",
537    'CAPABILITY_TLS_SIGALG_CODE_POINT' =>        "tls-sigalg-code-point",
538    'CAPABILITY_TLS_SIGALG_NAME' =>              "tls-sigalg-name",
539    'CAPABILITY_TLS_SIGALG_OID' =>               "tls-sigalg-oid",
540    'CAPABILITY_TLS_SIGALG_SIG_NAME' =>          "tls-sigalg-sig-name",
541    'CAPABILITY_TLS_SIGALG_SIG_OID' =>           "tls-sigalg-sig-oid",
542    'CAPABILITY_TLS_SIGALG_HASH_NAME' =>         "tls-sigalg-hash-name",
543    'CAPABILITY_TLS_SIGALG_HASH_OID' =>          "tls-sigalg-hash-oid",
544    'CAPABILITY_TLS_SIGALG_KEYTYPE' =>           "tls-sigalg-keytype",
545    'CAPABILITY_TLS_SIGALG_KEYTYPE_OID' =>       "tls-sigalg-keytype-oid",
546    'CAPABILITY_TLS_SIGALG_SECURITY_BITS' =>     "tls-sigalg-sec-bits",
547    'CAPABILITY_TLS_SIGALG_MIN_TLS' =>           "tls-min-tls",
548    'CAPABILITY_TLS_SIGALG_MAX_TLS' =>           "tls-max-tls",
549
550# storemgmt parameters
551
552
553# Used by storemgmt_ctx_set_params():
554#
555# - STORE_PARAM_EXPECT is an INTEGER, and the value is any of the
556#   STORE_INFO numbers.  This is used to set the expected type of
557#   object loaded.
558#
559# - STORE_PARAM_SUBJECT, STORE_PARAM_ISSUER,
560#   STORE_PARAM_SERIAL, STORE_PARAM_FINGERPRINT,
561#   STORE_PARAM_DIGEST, STORE_PARAM_ALIAS
562#   are used as search criteria.
563#   (STORE_PARAM_DIGEST is used with STORE_PARAM_FINGERPRINT)
564
565    'STORE_PARAM_EXPECT' =>     "expect",       # INTEGER
566    'STORE_PARAM_SUBJECT' =>    "subject",      # DER blob => OCTET_STRING
567    'STORE_PARAM_ISSUER' =>     "name",         # DER blob => OCTET_STRING
568    'STORE_PARAM_SERIAL' =>     "serial",       # INTEGER
569    'STORE_PARAM_DIGEST' =>     "digest",       # UTF8_STRING
570    'STORE_PARAM_FINGERPRINT' => "fingerprint", # OCTET_STRING
571    'STORE_PARAM_ALIAS' =>      "alias",        # UTF8_STRING
572
573# You may want to pass properties for the provider implementation to use
574    'STORE_PARAM_PROPERTIES' => "properties",   # utf8_string
575# DECODER input type if a decoder is used by the store
576    'STORE_PARAM_INPUT_TYPE' => "input-type",   # UTF8_STRING
577
578
579# Libssl record layer
580    'LIBSSL_RECORD_LAYER_PARAM_OPTIONS' =>        "options",
581    'LIBSSL_RECORD_LAYER_PARAM_MODE' =>           "mode",
582    'LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD' =>     "read_ahead",
583    'LIBSSL_RECORD_LAYER_READ_BUFFER_LEN' =>      "read_buffer_len",
584    'LIBSSL_RECORD_LAYER_PARAM_USE_ETM' =>        "use_etm",
585    'LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC' =>     "stream_mac",
586    'LIBSSL_RECORD_LAYER_PARAM_TLSTREE' =>        "tlstree",
587    'LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN' =>   "max_frag_len",
588    'LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA' => "max_early_data",
589    'LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING' =>  "block_padding",
590    'LIBSSL_RECORD_LAYER_PARAM_HS_PADDING' =>     "hs_padding",
591);
592
593# Generate string based macros for public consumption
594sub generate_public_macros {
595    my @macros = ();
596
597    foreach my $name (keys %params) {
598        my $val = $params{$name};
599        my $def = '# define OSSL_' . $name . ' ';
600
601        if (substr($val, 0, 1) eq '*') {
602            $def .= 'OSSL_' . substr($val, 1);
603        } else {
604            $def .= '"' . $val . '"';
605        }
606        push(@macros, $def)
607    }
608    return join("\n", sort @macros);
609}
610
611# Generate number based macros for internal use
612# The numbers are unique per string
613sub generate_internal_macros {
614    my @macros = ();
615    my $count = 0;
616    my %reverse;
617
618    # Determine the number for each unique string
619    # Sort the names to improve the chance of cache coherency
620    foreach my $name (sort keys %params) {
621        my $val = $params{$name};
622
623        if (substr($val, 0, 1) ne '*' and not defined $reverse{$val}) {
624            $reverse{$val} = $count++;
625        }
626    }
627
628    # Output the defines
629    foreach my $name (keys %params) {
630        my $val = $params{$name};
631        my $def = '#define PIDX_' . $name . ' ';
632
633        if (substr($val, 0, 1) eq '*') {
634            $def .= 'PIDX_' . substr($val, 1);
635        } else {
636            $def .= $reverse{$val};
637        }
638        push(@macros, $def)
639    }
640    return "#define NUM_PIDX $count\n\n" . join("\n", sort @macros);
641}
642
643sub generate_trie {
644    my %trie;
645    my $nodes = 0;
646    my $chars = 0;
647
648    foreach my $name (sort keys %params) {
649        my $val = $params{$name};
650        if (substr($val, 0, 1) ne '*') {
651            my $cursor = \%trie;
652
653            $chars += length($val);
654            for my $i (0 .. length($val) - 1) {
655                my $c = substr($val, $i, 1);
656
657                if (not $case_sensitive) {
658                    $c = '_' if $c eq '-';
659                    $c = lc $c;
660                }
661
662                if (not defined $$cursor{$c}) {
663                    $cursor->{$c} = {};
664                    $nodes++;
665                }
666                $cursor = $cursor->{$c};
667            }
668            $cursor->{'val'} = $name;
669        }
670    }
671    #print "\n\n/* $nodes nodes for $chars letters*/\n\n";
672    return %trie;
673}
674
675sub generate_code_from_trie {
676    my $n = shift;
677    my $trieref = shift;
678    my $idt = "    ";
679    my $indent0 = $idt x ($n + 1);
680    my $indent1 = $indent0 . $idt;
681    my $strcmp = $case_sensitive ? 'strcmp' : 'strcasecmp';
682
683    print "int ossl_param_find_pidx(const char *s)\n{\n" if $n == 0;
684
685    if ($trieref->{'suffix'}) {
686        my $suf = $trieref->{'suffix'};
687
688        printf "%sif ($strcmp(\"$suf\", s + $n) == 0", $indent0;
689        if (not $case_sensitive) {
690            $suf =~ tr/_/-/;
691            print " || $strcmp(\"$suf\", s + $n) == 0"
692                if ($suf ne $trieref->{'suffix'});
693        }
694        printf ")\n%sreturn PIDX_%s;\n", $indent1, $trieref->{'name'};
695        #printf "%sbreak;\n", $indent0;
696        return;
697    }
698
699    printf "%sswitch(s\[%d\]) {\n", $indent0, $n;
700    printf "%sdefault:\n", $indent0;
701    for my $l (sort keys %$trieref) {
702        if ($l eq 'val') {
703            printf "%sbreak;\n", $indent1;
704            printf "%scase '\\0':\n", $indent0;
705            printf "%sreturn PIDX_%s;\n", $indent1, $trieref->{'val'};
706        } else {
707            printf "%sbreak;\n", $indent1;
708            printf "%scase '%s':", $indent0, $l;
709            if (not $case_sensitive) {
710                print "   case '-':" if ($l eq '_');
711                printf "   case '%s':", uc $l if ($l =~ /[a-z]/);
712            }
713            print "\n";
714            generate_code_from_trie($n + 1, $trieref->{$l});
715        }
716    }
717    printf "%s}\n", $indent0;
718    print "    return -1;\n}\n" if $n == 0;
719    return "";
720}
721
722# Find long endings and cache what they resolve to
723sub locate_long_endings {
724    my $trieref = shift;
725    my @names = keys %$trieref;
726    my $num = @names;
727
728    return (1, '', $trieref->{$names[0]}) if ($num == 1 and $names[0] eq 'val');
729
730    if ($num == 1) {
731        my ($res, $suffix, $name) = locate_long_endings($trieref->{$names[0]});
732        my $e = $names[0] . $suffix;
733        if ($res) {
734            $trieref->{'suffix'} = $e;
735            $trieref->{'name'} = $name;
736        }
737        return $res, $e, $name;
738    }
739
740    for my $l (@names) {
741        if ($l ne 'val') {
742            my ($res, $suffix, $name) = locate_long_endings($trieref->{$l});
743        }
744    }
745    return 0, '';
746}
747
748sub produce_decoder {
749    my %t = generate_trie();
750    my $s;
751
752    locate_long_endings(\%t);
753
754    open local *STDOUT, '>', \$s;
755    generate_code_from_trie(0, \%t);
756    return $s;
757}
758