Botan 2.19.5 Crypto and TLS for C&

cbc_mac.cpp
Go to the documentation of this file.
1/*
2* CBC-MAC
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/cbc_mac.h>
9
10 namespace Botan {
11
12/*
13* Update an CBC-MAC Calculation
14*/
15 void CBC_MAC::add_data(const uint8_t input[], size_t length)
16 {
17 verify_key_set(m_state.empty() == false);
18
19 size_t xored = std::min(output_length() - m_position, length);
20 xor_buf(&m_state[m_position], input, xored);
21 m_position += xored;
22
23 if(m_position < output_length())
24 return;
25
26 m_cipher->encrypt(m_state);
27 input += xored;
28 length -= xored;
29 while(length >= output_length())
30 {
31 xor_buf(m_state, input, output_length());
32 m_cipher->encrypt(m_state);
33 input += output_length();
34 length -= output_length();
35 }
36
37 xor_buf(m_state, input, length);
38 m_position = length;
39 }
40
41/*
42* Finalize an CBC-MAC Calculation
43*/
44 void CBC_MAC::final_result(uint8_t mac[])
45 {
46 verify_key_set(m_state.empty() == false);
47
48 if(m_position)
49 m_cipher->encrypt(m_state);
50
51 copy_mem(mac, m_state.data(), m_state.size());
52 zeroise(m_state);
53 m_position = 0;
54 }
55
56/*
57* CBC-MAC Key Schedule
58*/
59 void CBC_MAC::key_schedule(const uint8_t key[], size_t length)
60 {
61 m_state.resize(m_cipher->block_size());
62 m_cipher->set_key(key, length);
63 }
64
65/*
66* Clear memory of sensitive data
67*/
68 void CBC_MAC::clear()
69 {
70 m_cipher->clear();
71 zap(m_state);
72 m_position = 0;
73 }
74
75/*
76* Return the name of this type
77*/
78std::string CBC_MAC::name() const
79 {
80 return "CBC-MAC(" + m_cipher->name() + ")";
81 }
82
83/*
84* Return a clone of this object
85*/
86 MessageAuthenticationCode* CBC_MAC::clone() const
87 {
88 return new CBC_MAC(m_cipher->clone());
89 }
90
91/*
92* CBC-MAC Constructor
93*/
94 CBC_MAC::CBC_MAC(BlockCipher* cipher) :
95 m_cipher(cipher)
96 {
97 }
98
99}
Botan::BlockCipher
Definition: block_cipher.h:22
Botan::CBC_MAC::output_length
size_t output_length() const override
Definition: cbc_mac.h:26
Botan::CBC_MAC::CBC_MAC
CBC_MAC(BlockCipher *cipher)
Definition: cbc_mac.cpp:94
Botan::CBC_MAC::clear
void clear() override
Definition: cbc_mac.cpp:68
Botan::CBC_MAC::clone
MessageAuthenticationCode * clone() const override
Definition: cbc_mac.cpp:86
Botan::CBC_MAC::name
std::string name() const override
Definition: cbc_mac.cpp:78
Botan::MessageAuthenticationCode
Definition: mac.h:23
Botan::SymmetricAlgorithm::verify_key_set
void verify_key_set(bool cond) const
Definition: sym_algo.h:171
Botan
Definition: alg_id.cpp:13
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:114
Botan::zap
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:124
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:133
Botan::xor_buf
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:262