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