1 /**
2 * Symmetric Algorithm Base Class
3 *
4 * Copyright:
5 * (C) 1999-2007 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.sym_algo;
12 
13 import botan.utils.exceptn;
14 public import botan.algo_base.key_spec;
15 public import botan.algo_base.symkey;
16 public import botan.utils.types;
17 
18 /**
19 * This class represents a symmetric algorithm object.
20 */
21 interface SymmetricAlgorithm
22 {
23 public:
24     
25     /**
26     * Returns: minimum allowed key length
27     */
28     final size_t maximumKeylength() const
29     {
30         return keySpec().maximumKeylength();
31     }
32     
33     /**
34     * Returns: maxmium allowed key length
35     */
36     final size_t minimumKeylength() const
37     {
38         return keySpec().minimumKeylength();
39     }
40     
41     /**
42     * Check whether a given key length is valid for this algorithm.
43     * 
44     * Params:
45     *  length = the key length to be checked.
46     * 
47     * Returns: true if the key length is valid.
48     */
49     final bool validKeylength(size_t length) const
50     {
51         return keySpec().validKeylength(length);
52     }
53     
54     /**
55     * Set the symmetric key of this object.
56     * 
57     * Params:
58     *  key = the $(D SymmetricKey) to be set.
59     */
60     final void setKey(in SymmetricKey key)
61     {
62         setKey(key.ptr, key.length);
63     }
64     
65     /// ditto
66     final void setKey(Alloc)(auto const ref RefCounted!(Vector!( ubyte, Alloc ), Alloc) key)
67     {
68         setKey(key.ptr, key.length);
69     }
70 
71     /// ditto
72     final void setKey(Alloc)(auto const ref Vector!( ubyte, Alloc ) key)
73     {
74         setKey(key.ptr, key.length);
75     }
76     
77     /**
78     * Set the symmetric key of this object.
79     * 
80     * Params:
81     *  key = the to be set as a ubyte array.
82     *  length = in bytes of key param
83     */
84     final void setKey(const(ubyte)* key, size_t length)
85     {
86         if (!validKeylength(length))
87             throw new InvalidKeyLength(name, length);
88         keySchedule(key, length);
89     }
90 
91     /// Clear underlying buffers
92     abstract void clear();
93     
94     /**
95     * Returns: object describing limits on key size
96     */
97     abstract KeyLengthSpecification keySpec() const;
98 
99     abstract @property string name() const;
100     
101 protected:
102     /**
103     * Run the key schedule
104     * 
105     * Params:
106     *  key = the key
107     *  length = of key
108     */
109     abstract void keySchedule(const(ubyte)* key, size_t length);
110 }
111