Nik Dewally | abac0e5 | 2024-08-02 13:42:27 +0100 | [diff] [blame^] | 1 | /* Copyright (c) 2024 Arm Limited. All Rights Reserved. |
| 2 | * |
| 3 | * SPDX-License-Identifier: BSD-3-Clause |
| 4 | */ |
| 5 | |
| 6 | // crypto_model.hpp |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | #include "crypto_model_internal.hpp" |
| 12 | |
| 13 | /* |
| 14 | * `crypto_model` contains information about crypto key types, algorithms, and |
| 15 | * attributes, and their compatabilities with each-other. |
| 16 | */ |
| 17 | namespace crypto_model { |
| 18 | |
| 19 | // forward declarations |
| 20 | class key_type; |
| 21 | class algorithm; |
| 22 | |
| 23 | algorithm& get_algorithm(std::string); |
| 24 | key_type& get_key_type(std::string); |
| 25 | |
| 26 | algorithm& get_random_hash_algorithm(); |
| 27 | |
| 28 | algorithm& get_random_algorithm(); |
| 29 | |
| 30 | key_type& get_random_key_type(); |
| 31 | |
| 32 | uint get_random_key_size(); |
| 33 | |
| 34 | //! Initialises the crypto model. |
| 35 | void init_crypto_model(); |
| 36 | |
| 37 | |
| 38 | // Classes to hold data. |
| 39 | class algorithm { |
| 40 | friend void crypto_model::init_crypto_model(); |
| 41 | friend void crypto_model::internal::define_algorithm(std::string,bool,bool); |
| 42 | |
| 43 | public: |
| 44 | algorithm(); |
| 45 | ~algorithm(); |
| 46 | |
| 47 | std::string get_string(); |
| 48 | |
| 49 | // Gets the header value form of the algorithm, with a random hash function |
| 50 | // filled in if needed. |
| 51 | std::string get_string_with_hash(); |
| 52 | bool is_enabled(); |
| 53 | |
| 54 | bool is_hash_algorithm(); |
| 55 | bool requires_hash(); |
| 56 | |
| 57 | bool valid_for_key_type(key_type&); |
| 58 | key_type& random_valid_key_type(); |
| 59 | |
| 60 | private: |
| 61 | |
| 62 | std::string name; |
| 63 | std::vector<std::string> allowed_key_types; |
| 64 | bool requires_hash_flag; |
| 65 | bool is_hash_algorithm_flag; |
| 66 | bool enabled=true; |
| 67 | }; |
| 68 | |
| 69 | class key_type { |
| 70 | friend void crypto_model::init_crypto_model(); |
| 71 | friend void crypto_model::internal::define_key_type(std::string, std::vector<uint>, uint, uint); |
| 72 | |
| 73 | public: |
| 74 | key_type(); |
| 75 | ~key_type(); |
| 76 | |
| 77 | std::string get_string(); |
| 78 | bool is_enabled(); |
| 79 | |
| 80 | bool is_allowed_algorithm(algorithm&); |
| 81 | algorithm& random_allowed_algorithm(); |
| 82 | bool is_valid_key_size(uint size); |
| 83 | uint get_random_valid_key_size(); |
| 84 | |
| 85 | |
| 86 | private: |
| 87 | |
| 88 | std::string name; |
| 89 | |
| 90 | // If non empty, the key size must be one of the values in this vector. |
| 91 | std::vector<uint> allowed_key_sizes_bits; |
| 92 | uint max_key_size_bits; |
| 93 | uint min_key_size_bits; |
| 94 | |
| 95 | std::vector<std::string> allowed_algorithms; |
| 96 | |
| 97 | bool enabled=true; |
| 98 | }; |
| 99 | |
| 100 | } // namespace crypto_model |