1 /**
2 * Symmetric Key Length Specification
3 * 
4 * Copyright:
5 * (C) 2010 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.algo_base.key_spec;
12 
13 import botan.utils.types;
14 /**
15 * Represents the length requirements on an algorithm key
16 */
17 struct KeyLengthSpecification
18 {
19 public:
20     /**
21     * Constructor for fixed length keys
22     * 
23     * Params:
24     *  keylen = the supported key length
25     */
26     this(size_t keylen)
27     {
28         m_min_keylen = keylen;
29         m_max_keylen = keylen;
30         m_keylen_mod = 1;
31     }
32 
33     /**
34     * Constructor for variable length keys
35     * 
36     * Params:
37     *  min_k = the smallest supported key length
38     *  max_k = the largest supported key length
39     *  k_mod = the number of bytes the key must be a multiple of
40     */
41     this(size_t min_k,
42          size_t max_k,
43          size_t k_mod = 1)
44     {
45         m_min_keylen = min_k;
46         m_max_keylen = max_k ? max_k : min_k;
47         m_keylen_mod = k_mod;
48     }
49 
50     /**
51     * Params:
52     *  length = is a key length in bytes
53     * 
54     * Returns: true iff this length is a valid length for this algo
55     */
56     bool validKeylength(size_t length) const
57     {
58         return ((length >= m_min_keylen) &&
59                 (length <= m_max_keylen) &&
60                 (length % m_keylen_mod == 0));
61     }
62 
63     /**
64     * Returns: minimum key length in bytes
65     */
66     size_t minimumKeylength() const
67     {
68         return m_min_keylen;
69     }
70 
71     /**
72     * Returns: maximum key length in bytes
73     */
74     size_t maximumKeylength() const
75     {
76         return m_max_keylen;
77     }
78 
79     /**
80     * Returns: key length multiple in bytes
81     */
82     size_t keylengthMultiple() const
83     {
84         return m_keylen_mod;
85     }
86 
87     KeyLengthSpecification multiple(size_t n) const
88     {
89         return KeyLengthSpecification(n * m_min_keylen,
90                                         n * m_max_keylen,
91                                         n * m_keylen_mod);
92     }
93 
94 private:
95 
96     size_t m_min_keylen, m_max_keylen, m_keylen_mod;
97 }