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