blob: 0d3bf928f8de53628d1cf1311ca515e0f5f48747 [file] [log] [blame]
Nik Dewallyabac0e52024-08-02 13:42:27 +01001/* 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 */
17namespace crypto_model {
18
19// forward declarations
20class key_type;
21class algorithm;
22
23algorithm& get_algorithm(std::string);
24key_type& get_key_type(std::string);
25
26algorithm& get_random_hash_algorithm();
27
28algorithm& get_random_algorithm();
29
30key_type& get_random_key_type();
31
32uint get_random_key_size();
33
34//! Initialises the crypto model.
35void init_crypto_model();
36
37
38// Classes to hold data.
39class algorithm {
40 friend void crypto_model::init_crypto_model();
41 friend void crypto_model::internal::define_algorithm(std::string,bool,bool);
42
43public:
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
60private:
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
69class 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
73public:
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
86private:
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