11cb0ef41Sopenharmony_ciSelecting algorithm implementations by properties 21cb0ef41Sopenharmony_ci================================================= 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciProperties are associated with algorithms and are used to select between 51cb0ef41Sopenharmony_cidifferent implementations dynamically. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciThis implementation is based on a number of assumptions: 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci* Property definition is uncommon. I.e. providers will be loaded and 101cb0ef41Sopenharmony_ci unloaded relatively infrequently, if at all. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci* The number of distinct property names will be small. 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci* Providers will often give the same implementation properties to most or 151cb0ef41Sopenharmony_ci all of their implemented algorithms. E.g. the FIPS property would be set 161cb0ef41Sopenharmony_ci across an entire provider. Likewise for, hardware, accelerated, software, 171cb0ef41Sopenharmony_ci HSM and, perhaps, constant_time. 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci* There are a lot of algorithm implementations, therefore property 201cb0ef41Sopenharmony_ci definitions should be space efficient. However... 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci* ... property queries are very common. These must be fast. 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci* Property queries come from a small set and are reused many times typically. 251cb0ef41Sopenharmony_ci I.e. an application tends to use the same set of queries over and over, 261cb0ef41Sopenharmony_ci rather than spanning a wide variety of queries. 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci* Property queries can never add new property definitions. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciSome consequences of these assumptions are: 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci* That definition is uncommon and queries are very common, we can treat 331cb0ef41Sopenharmony_ci the property definitions as almost immutable. Specifically, a query can 341cb0ef41Sopenharmony_ci never change the state of the definitions. 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci* That definition is uncommon and needs to be space efficient, it will 371cb0ef41Sopenharmony_ci be feasible to use a hash table to contain the names (and possibly also 381cb0ef41Sopenharmony_ci values) of all properties and to reference these instead of duplicating 391cb0ef41Sopenharmony_ci strings. Moreover, such a data structure need not be garbage collected. 401cb0ef41Sopenharmony_ci By converting strings to integers using a structure such as this, string 411cb0ef41Sopenharmony_ci comparison degenerates to integer comparison. Additionally, lists of 421cb0ef41Sopenharmony_ci properties can be sorted by the string index which makes comparisons linear 431cb0ef41Sopenharmony_ci time rather than quadratic time - the O(n log n) sort cost being amortised. 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci* A cache for property definitions is also viable, if only implementation 461cb0ef41Sopenharmony_ci properties are used and not algorithm properties, or at least these are 471cb0ef41Sopenharmony_ci maintained separately. This cache would be a hash table, indexed by 481cb0ef41Sopenharmony_ci the property definition string, and algorithms with the same properties 491cb0ef41Sopenharmony_ci would share their definition structure. Again, reducing space use. 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci* A query cache is desirable. This would be a hash table keyed by the 521cb0ef41Sopenharmony_ci algorithm identifier and the entire query string and it would map to 531cb0ef41Sopenharmony_ci the chosen algorithm. When a provider is loaded or unloaded, this cache 541cb0ef41Sopenharmony_ci must be invalidated. The cache will also be invalidated when the global 551cb0ef41Sopenharmony_ci properties are changed as doing so removes the need to index on both the 561cb0ef41Sopenharmony_ci global and requested property strings. 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciThe implementation: 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci* [property_lock.c](property_lock.c) 611cb0ef41Sopenharmony_ci contains some wrapper functions to handle the global 621cb0ef41Sopenharmony_ci lock more easily. The global lock is held for short periods of time with 631cb0ef41Sopenharmony_ci per algorithm locking being used for longer intervals. 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci* [property_string.c](property_string.c) 661cb0ef41Sopenharmony_ci contains the string cache which converts property 671cb0ef41Sopenharmony_ci names and values to small integer indices. Names and values are stored in 681cb0ef41Sopenharmony_ci separate hash tables. The two Boolean values, the strings "yes" and "no", 691cb0ef41Sopenharmony_ci are populated as the first two members of the value table. All property 701cb0ef41Sopenharmony_ci names reserved by OpenSSL are also populated here. No functions are 711cb0ef41Sopenharmony_ci provided to convert from an index back to the original string (this can be 721cb0ef41Sopenharmony_ci done by maintaining parallel stacks of strings if required). 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci* [property_parse.c](property_parse.c) 751cb0ef41Sopenharmony_ci contains the property definition and query parsers. 761cb0ef41Sopenharmony_ci These convert ASCII strings into lists of properties. The resulting 771cb0ef41Sopenharmony_ci lists are sorted by the name index. Some additional utility functions 781cb0ef41Sopenharmony_ci for dealing with property lists are also included: comparison of a query 791cb0ef41Sopenharmony_ci against a definition and merging two queries into a single larger query. 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci* [property.c](property.c) 821cb0ef41Sopenharmony_ci contains the main APIs for defining and using properties. 831cb0ef41Sopenharmony_ci Algorithms are discovered from their NID and a query string. 841cb0ef41Sopenharmony_ci The results are cached. 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci The caching of query results has to be efficient but it must also be robust 871cb0ef41Sopenharmony_ci against a denial of service attack. The cache cannot be permitted to grow 881cb0ef41Sopenharmony_ci without bounds and must garbage collect under-used entries. The garbage 891cb0ef41Sopenharmony_ci collection does not have to be exact. 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci* [defn_cache.c](defn_cache.c) 921cb0ef41Sopenharmony_ci contains a cache that maps property definition strings to 931cb0ef41Sopenharmony_ci parsed properties. It is used by property.c to improve performance when 941cb0ef41Sopenharmony_ci the same definition appears multiple times. 95