/src/botan/build/include/botan/key_spec.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Symmetric Key Length Specification |
3 | | * (C) 2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_KEY_LEN_SPECIFICATION_H_ |
9 | | #define BOTAN_KEY_LEN_SPECIFICATION_H_ |
10 | | |
11 | | #include <botan/types.h> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | /** |
16 | | * Represents the length requirements on an algorithm key |
17 | | */ |
18 | | class BOTAN_PUBLIC_API(2,0) Key_Length_Specification final |
19 | | { |
20 | | public: |
21 | | /** |
22 | | * Constructor for fixed length keys |
23 | | * @param keylen the supported key length |
24 | | */ |
25 | | explicit Key_Length_Specification(size_t keylen) : |
26 | | m_min_keylen(keylen), |
27 | | m_max_keylen(keylen), |
28 | | m_keylen_mod(1) |
29 | 2.93k | { |
30 | 2.93k | } |
31 | | |
32 | | /** |
33 | | * Constructor for variable length keys |
34 | | * @param min_k the smallest supported key length |
35 | | * @param max_k the largest supported key length |
36 | | * @param k_mod the number of bytes the key must be a multiple of |
37 | | */ |
38 | | Key_Length_Specification(size_t min_k, |
39 | | size_t max_k, |
40 | | size_t k_mod = 1) : |
41 | | m_min_keylen(min_k), |
42 | | m_max_keylen(max_k ? max_k : min_k), |
43 | | m_keylen_mod(k_mod) |
44 | 48.7k | { |
45 | 48.7k | } |
46 | | |
47 | | /** |
48 | | * @param length is a key length in bytes |
49 | | * @return true iff this length is a valid length for this algo |
50 | | */ |
51 | | bool valid_keylength(size_t length) const |
52 | 51.6k | { |
53 | 51.6k | return ((length >= m_min_keylen) && |
54 | 51.6k | (length <= m_max_keylen) && |
55 | 51.6k | (length % m_keylen_mod == 0)); |
56 | 51.6k | } |
57 | | |
58 | | /** |
59 | | * @return minimum key length in bytes |
60 | | */ |
61 | | size_t minimum_keylength() const |
62 | 0 | { |
63 | 0 | return m_min_keylen; |
64 | 0 | } |
65 | | |
66 | | /** |
67 | | * @return maximum key length in bytes |
68 | | */ |
69 | | size_t maximum_keylength() const |
70 | 0 | { |
71 | 0 | return m_max_keylen; |
72 | 0 | } |
73 | | |
74 | | /** |
75 | | * @return key length multiple in bytes |
76 | | */ |
77 | | size_t keylength_multiple() const |
78 | 0 | { |
79 | 0 | return m_keylen_mod; |
80 | 0 | } |
81 | | |
82 | | /* |
83 | | * Multiplies all length requirements with the given factor |
84 | | * @param n the multiplication factor |
85 | | * @return a key length specification multiplied by the factor |
86 | | */ |
87 | | Key_Length_Specification multiple(size_t n) const |
88 | 0 | { |
89 | 0 | return Key_Length_Specification(n * m_min_keylen, |
90 | 0 | n * m_max_keylen, |
91 | 0 | n * m_keylen_mod); |
92 | 0 | } |
93 | | |
94 | | private: |
95 | | size_t m_min_keylen, m_max_keylen, m_keylen_mod; |
96 | | }; |
97 | | |
98 | | } |
99 | | |
100 | | #endif |