Botan  1.10.9
ssl3_mac.cpp
Go to the documentation of this file.
1 /*
2 * SSL3-MAC
3 * (C) 1999-2004 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/ssl3_mac.h>
9 
10 namespace Botan {
11 
12 /*
13 * Update a SSL3-MAC Calculation
14 */
15 void SSL3_MAC::add_data(const byte input[], size_t length)
16  {
17  hash->update(input, length);
18  }
19 
20 /*
21 * Finalize a SSL3-MAC Calculation
22 */
23 void SSL3_MAC::final_result(byte mac[])
24  {
25  hash->final(mac);
26  hash->update(o_key);
27  hash->update(mac, output_length());
28  hash->final(mac);
29  hash->update(i_key);
30  }
31 
32 /*
33 * SSL3-MAC Key Schedule
34 */
35 void SSL3_MAC::key_schedule(const byte key[], size_t length)
36  {
37  hash->clear();
38  std::fill(i_key.begin(), i_key.end(), 0x36);
39  std::fill(o_key.begin(), o_key.end(), 0x5C);
40 
41  i_key.copy(key, length);
42  o_key.copy(key, length);
43  hash->update(i_key);
44  }
45 
46 /*
47 * Clear memory of sensitive data
48 */
50  {
51  hash->clear();
52  zeroise(i_key);
53  zeroise(o_key);
54  }
55 
56 /*
57 * Return the name of this type
58 */
59 std::string SSL3_MAC::name() const
60  {
61  return "SSL3-MAC(" + hash->name() + ")";
62  }
63 
64 /*
65 * Return a clone of this object
66 */
68  {
69  return new SSL3_MAC(hash->clone());
70  }
71 
72 /*
73 * SSL3-MAC Constructor
74 */
75 SSL3_MAC::SSL3_MAC(HashFunction* hash_in) : hash(hash_in)
76  {
77  if(hash->hash_block_size() == 0)
78  throw Invalid_Argument("SSL3-MAC cannot be used with " + hash->name());
79 
80  // Quirk to deal with specification bug
81  const size_t INNER_HASH_LENGTH =
82  (hash->name() == "SHA-160") ? 60 : hash->hash_block_size();
83 
84  i_key.resize(INNER_HASH_LENGTH);
85  o_key.resize(INNER_HASH_LENGTH);
86  }
87 
88 }
void resize(size_t n)
Definition: secmem.h:211
virtual void clear()=0
size_t output_length() const
Definition: ssl3_mac.h:23
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
MessageAuthenticationCode * clone() const
Definition: ssl3_mac.cpp:67
virtual HashFunction * clone() const =0
void copy(const T in[], size_t n)
Definition: secmem.h:120
unsigned char byte
Definition: types.h:22
MessageAuthenticationCode * mac
Definition: fpe_fe1.cpp:94
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
std::string name() const
Definition: ssl3_mac.cpp:59
virtual std::string name() const =0
void final(byte out[])
Definition: buf_comp.h:80
virtual size_t hash_block_size() const
Definition: hash.h:32
SSL3_MAC(HashFunction *hash)
Definition: ssl3_mac.cpp:75
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415