1 /**
2 * TLS Handshake Hash
3 * 
4 * Copyright:
5 * (C) 2004-2006,2011,2012 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.tls.handshake_hash;
12 
13 import botan.constants;
14 static if (BOTAN_HAS_TLS):
15 package:
16 
17 import memutils.vector;
18 import botan.tls.version_;
19 import botan.tls.magic;
20 import botan.tls.exceptn;
21 import botan.hash.hash;
22 import botan.libstate.libstate;
23 import botan.tls.exceptn;
24 import botan.libstate.libstate;
25 import botan.hash.hash;
26 import botan.utils.types;
27 
28 /**
29 * TLS Handshake Hash
30 */
31 struct HandshakeHash
32 {
33 public:
34     void update(const(ubyte)* input, size_t length)
35     { m_data ~= input[0 .. length]; }
36 
37     void update(ALLOC)(auto const ref Vector!(ubyte, ALLOC) input)
38     { m_data ~= input[]; }
39 
40     /**
41     * Return a TLS Handshake Hash
42     */
43     SecureVector!ubyte flushInto(TLSProtocolVersion _version, in string mac_algo) const
44     {
45         AlgorithmFactory af = globalState().algorithmFactory();
46         
47         Unique!HashFunction hash;
48         
49         if (_version.supportsCiphersuiteSpecificPrf())
50         {
51             if (mac_algo == "MD5" || mac_algo == "SHA-1")
52                 hash = af.makeHashFunction("SHA-256");
53             else
54                 hash = af.makeHashFunction(mac_algo);
55         }
56         else
57             hash = af.makeHashFunction("Parallel(MD5,SHA-160)");
58         
59         hash.update(m_data);
60         return hash.finished();
61     }
62 
63     ref const(Vector!ubyte) getContents() const return
64     { return m_data; }
65 
66 	void reset() { if (m_data.length == 0) m_data.reserve(2048); else m_data.clear(); }
67 
68     @property HandshakeHash clone() const 
69     { 
70         HandshakeHash ret;
71         ret.m_data = m_data.clone;
72         return ret;
73     }
74 private:
75     Vector!ubyte m_data;
76 }