Botan 3.3.0
Crypto and TLS for C&
x919_mac.cpp
Go to the documentation of this file.
1/*
2* ANSI X9.19 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/internal/x919_mac.h>
9
10#include <botan/internal/stl_util.h>
11
12namespace Botan {
13
14/*
15* Update an ANSI X9.19 MAC Calculation
16*/
17void ANSI_X919_MAC::add_data(std::span<const uint8_t> input) {
19
20 BufferSlicer in(input);
21
22 const auto to_be_xored = in.take(std::min(8 - m_position, in.remaining()));
23 xor_buf(&m_state[m_position], to_be_xored.data(), to_be_xored.size());
24 m_position += to_be_xored.size();
25
26 if(m_position < 8) {
27 return;
28 }
29
30 m_des1->encrypt(m_state);
31 while(in.remaining() >= 8) {
32 xor_buf(m_state, in.take(8).data(), 8);
33 m_des1->encrypt(m_state);
34 }
35
36 const auto remaining = in.take(in.remaining());
37 xor_buf(m_state, remaining.data(), remaining.size());
38 m_position = remaining.size();
39}
40
41/*
42* Finalize an ANSI X9.19 MAC Calculation
43*/
44void ANSI_X919_MAC::final_result(std::span<uint8_t> mac) {
45 if(m_position) {
46 m_des1->encrypt(m_state);
47 }
48 m_des2->decrypt(m_state.data(), mac.data());
49 m_des1->encrypt(mac.data());
50 zeroise(m_state);
51 m_position = 0;
52}
53
55 return m_des1->has_keying_material() && m_des2->has_keying_material();
56}
57
58/*
59* ANSI X9.19 MAC Key Schedule
60*/
61void ANSI_X919_MAC::key_schedule(std::span<const uint8_t> key) {
62 m_state.resize(8);
63
64 m_des1->set_key(key.first(8));
65
66 if(key.size() == 16) {
67 key = key.last(8);
68 }
69
70 m_des2->set_key(key.first(8));
71}
72
73/*
74* Clear memory of sensitive data
75*/
77 m_des1->clear();
78 m_des2->clear();
79 zap(m_state);
80 m_position = 0;
81}
82
83std::string ANSI_X919_MAC::name() const {
84 return "X9.19-MAC";
85}
86
87std::unique_ptr<MessageAuthenticationCode> ANSI_X919_MAC::new_object() const {
88 return std::make_unique<ANSI_X919_MAC>();
89}
90
91/*
92* ANSI X9.19 MAC Constructor
93*/
94ANSI_X919_MAC::ANSI_X919_MAC() : m_des1(BlockCipher::create("DES")), m_des2(m_des1->new_object()), m_position(0) {}
95
96} // namespace Botan
std::unique_ptr< MessageAuthenticationCode > new_object() const override
Definition x919_mac.cpp:87
std::string name() const override
Definition x919_mac.cpp:83
bool has_keying_material() const override
Definition x919_mac.cpp:54
void clear() override
Definition x919_mac.cpp:76
void assert_key_material_set() const
Definition sym_algo.h:139
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:108
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:117
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:340