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