monotone

monotone Mtn Source Tree

Root/botan/cbc_mac.cpp

1/*************************************************
2* CBC-MAC Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/cbc_mac.h>
7#include <botan/lookup.h>
8#include <botan/bit_ops.h>
9#include <algorithm>
10
11namespace Botan {
12
13/*************************************************
14* Update an CBC-MAC Calculation *
15*************************************************/
16void CBC_MAC::add_data(const byte input[], u32bit length)
17 {
18 u32bit xored = std::min(OUTPUT_LENGTH - position, length);
19 xor_buf(state + position, input, xored);
20 position += xored;
21
22 if(position < OUTPUT_LENGTH)
23 return;
24
25 e->encrypt(state);
26 input += xored;
27 length -= xored;
28 while(length >= OUTPUT_LENGTH)
29 {
30 xor_buf(state, input, OUTPUT_LENGTH);
31 e->encrypt(state);
32 input += OUTPUT_LENGTH;
33 length -= OUTPUT_LENGTH;
34 }
35
36 xor_buf(state, input, length);
37 position = length;
38 }
39
40/*************************************************
41* Finalize an CBC-MAC Calculation *
42*************************************************/
43void CBC_MAC::final_result(byte mac[])
44 {
45 if(position)
46 e->encrypt(state);
47
48 copy_mem(mac, state.begin(), state.size());
49 state.clear();
50 position = 0;
51 }
52
53/*************************************************
54* CBC-MAC Key Schedule *
55*************************************************/
56void CBC_MAC::key(const byte key[], u32bit length)
57 {
58 e->set_key(key, length);
59 }
60
61/*************************************************
62* Clear memory of sensitive data *
63*************************************************/
64void CBC_MAC::clear() throw()
65 {
66 e->clear();
67 state.clear();
68 position = 0;
69 }
70
71/*************************************************
72* Return the name of this type *
73*************************************************/
74std::string CBC_MAC::name() const
75 {
76 return "CBC-MAC(" + e->name() + ")";
77 }
78
79/*************************************************
80* Return a clone of this object *
81*************************************************/
82MessageAuthenticationCode* CBC_MAC::clone() const
83 {
84 return new CBC_MAC(e->name());
85 }
86
87/*************************************************
88* CBC-MAC Constructor *
89*************************************************/
90CBC_MAC::CBC_MAC(const std::string& cipher) :
91 MessageAuthenticationCode(block_size_of(cipher),
92 min_keylength_of(cipher),
93 max_keylength_of(cipher),
94 keylength_multiple_of(cipher)),
95 state(block_size_of(cipher))
96 {
97 e = get_block_cipher(cipher);
98 position = 0;
99 }
100
101/*************************************************
102* CBC-MAC Destructor *
103*************************************************/
104CBC_MAC::~CBC_MAC()
105 {
106 delete e;
107 }
108
109}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status