Botan 1.10.17

x919_mac.cpp
Go to the documentation of this file.
1 /*
2 * ANSI X9.19 MAC
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/x919_mac.h>
9 #include <botan/internal/xor_buf.h>
10 #include <algorithm>
11
12 namespace Botan {
13
14 /*
15 * Update an ANSI X9.19 MAC Calculation
16 */
17 void ANSI_X919_MAC::add_data(const byte input[], size_t length)
18 {
19 size_t xored = std::min(8 - position, length);
20 xor_buf(&state[position], input, xored);
21 position += xored;
22
23 if(position < 8) return;
24
25 e->encrypt(state);
26 input += xored;
27 length -= xored;
28 while(length >= 8)
29 {
30 xor_buf(state, input, 8);
31 e->encrypt(state);
32 input += 8;
33 length -= 8;
34 }
35
36 xor_buf(state, input, length);
37 position = length;
38 }
39
40 /*
41 * Finalize an ANSI X9.19 MAC Calculation
42 */
43 void ANSI_X919_MAC::final_result(byte mac[])
44 {
45 if(position)
46 e->encrypt(state);
47 d->decrypt(state, mac);
48 e->encrypt(mac);
49 zeroise(state);
50 position = 0;
51 }
52
53 /*
54 * ANSI X9.19 MAC Key Schedule
55 */
56 void ANSI_X919_MAC::key_schedule(const byte key[], size_t length)
57 {
58 e->set_key(key, 8);
59 if(length == 8) d->set_key(key, 8);
60 else d->set_key(key + 8, 8);
61 }
62
63 /*
64 * Clear memory of sensitive data
65 */
66 void ANSI_X919_MAC::clear()
67 {
68 e->clear();
69 d->clear();
70 zeroise(state);
71 position = 0;
72 }
73
74 std::string ANSI_X919_MAC::name() const
75 {
76 return "X9.19-MAC";
77 }
78
79 MessageAuthenticationCode* ANSI_X919_MAC::clone() const
80 {
81 return new ANSI_X919_MAC(e->clone());
82 }
83
84 /*
85 * ANSI X9.19 MAC Constructor
86 */
87 ANSI_X919_MAC::ANSI_X919_MAC(BlockCipher* e_in) :
88 e(e_in), d(e->clone()), state(e->block_size()), position(0)
89 {
90 if(e->name() != "DES")
91 throw Invalid_Argument("ANSI X9.19 MAC only supports DES");
92 }
93
94 /*
95 * ANSI X9.19 MAC Destructor
96 le*/
97 ANSI_X919_MAC::~ANSI_X919_MAC()
98 {
99 delete e;
100 delete d;
101 }
102
103 }
Botan::Algorithm::clear
virtual void clear()=0
Botan::ANSI_X919_MAC::clear
void clear()
Definition: x919_mac.cpp:66
Botan::BlockCipher::clone
virtual BlockCipher * clone() const =0
Botan::ANSI_X919_MAC::~ANSI_X919_MAC
~ANSI_X919_MAC()
Definition: x919_mac.cpp:97
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::MessageAuthenticationCode
Definition: mac.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::BlockCipher::decrypt
void decrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:57
Botan::BlockCipher::encrypt
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::ANSI_X919_MAC::name
std::string name() const
Definition: x919_mac.cpp:74
Botan
Definition: algo_base.h:14
Botan::ANSI_X919_MAC::ANSI_X919_MAC
ANSI_X919_MAC(BlockCipher *cipher)
Definition: x919_mac.cpp:87
Botan::Algorithm::name
virtual std::string name() const =0
Botan::ANSI_X919_MAC::clone
MessageAuthenticationCode * clone() const
Definition: x919_mac.cpp:79
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::BlockCipher
Definition: block_cipher.h:18