Cipher Fayadh
Block cipher Feistel 128-bit dengan fungsi putaran sigma-delta
Loading...
Searching...
No Matches
mac.hpp
Go to the documentation of this file.
1#ifndef MAC_HPP
2#define MAC_HPP
3
4#include "cipher.hpp"
5#include "util.hpp"
6#include <cstddef>
7#include <cstdint>
8
14
16constexpr size_t MAC_TAG_SIZE = 16;
17static_assert(BLOCK_SIZE == 16, "CMAC di sini mengasumsikan blok 128-bit (Rb = 0x87)");
18
19namespace cmac_detail {
30void dbl(const uint8_t in[16], uint8_t out[16]);
31} // namespace cmac_detail
32
36class Cmac {
37public:
43 explicit Cmac(const BlockCipher& cipher);
44 Cmac(const BlockCipher&&) = delete; // Mencegah dangling reference ke cipher sementara
45 ~Cmac();
46
47 Cmac(const Cmac&) = delete;
48 Cmac& operator=(const Cmac&) = delete;
49
56 void update(const uint8_t* data, size_t len);
57
64 void finalize(uint8_t tag[MAC_TAG_SIZE]);
65
74 static void compute(const BlockCipher& cipher, const uint8_t* data, size_t len, uint8_t tag[MAC_TAG_SIZE]);
75
85 static bool verify(const BlockCipher& cipher, const uint8_t* data, size_t len, const uint8_t tag[MAC_TAG_SIZE]);
86
87private:
88 const BlockCipher& cipher_;
89 uint8_t k1_[BLOCK_SIZE];
90 uint8_t k2_[BLOCK_SIZE];
91 uint8_t x_[BLOCK_SIZE];
92 uint8_t buf_[BLOCK_SIZE];
93 size_t buf_len_{0};
94
95 void process_buf();
96 void reset_state();
97};
98
104struct DerivedKeys {
105 uint8_t enc[KEY_SIZE]{0};
106 uint8_t mac[KEY_SIZE]{0};
107
108 DerivedKeys() = default;
109 ~DerivedKeys() {
110 util::secure_zero(enc, sizeof(enc));
111 util::secure_zero(mac, sizeof(mac));
112 }
114 DerivedKeys(const DerivedKeys&) = default;
116 DerivedKeys& operator=(const DerivedKeys&) = default;
118 DerivedKeys(DerivedKeys&&) noexcept = default;
120 DerivedKeys& operator=(DerivedKeys&&) noexcept = default;
121};
122
132DerivedKeys derive_keys(const BlockCipher& master_cipher);
133
134#endif // MAC_HPP
Interface block cipher 128-bit dan implementasinya.
constexpr size_t KEY_SIZE
Ukuran master key dalam byte (128 bit, 16x8 bit).
Definition cipher.hpp:15
constexpr size_t BLOCK_SIZE
Ukuran satu blok dalam byte (128 bit, 16x8 bit).
Definition cipher.hpp:13
Interface block cipher 128-bit.
Definition cipher.hpp:28
static void compute(const BlockCipher &cipher, const uint8_t *data, size_t len, uint8_t tag[MAC_TAG_SIZE])
Menghitung tag CMAC untuk seluruh data dalam satu pemanggilan (one-shot).
Definition mac.cpp:130
Cmac(const BlockCipher &cipher)
Membuat objek CMAC dan menurunkan subkey K1 dan K2 dari cipher.
Definition mac.cpp:28
void finalize(uint8_t tag[MAC_TAG_SIZE])
Menyelesaikan perhitungan tag CMAC, menuliskan tag ke buffer keluaran, dan mereset state internal aga...
Definition mac.cpp:99
static bool verify(const BlockCipher &cipher, const uint8_t *data, size_t len, const uint8_t tag[MAC_TAG_SIZE])
Memverifikasi keabsahan tag CMAC dalam waktu konstan (constant-time).
Definition mac.cpp:136
void update(const uint8_t *data, size_t len)
Menambahkan chunk data ke dalam perhitungan CMAC secara inkremental.
Definition mac.cpp:59
DerivedKeys derive_keys(const BlockCipher &master_cipher)
Menghasilkan kunci enkripsi dan kunci MAC terpisah dari master cipher (KDF berbasis PRF).
Definition mac.cpp:147
constexpr size_t MAC_TAG_SIZE
Ukuran tag MAC dalam byte (128 bit / 16 byte).
Definition mac.hpp:16
Struktur pasangan kunci hasil derivasi untuk enkripsi dan autentikasi.
Definition mac.hpp:104
DerivedKeys(const DerivedKeys &)=default
Copy constructor (default).
DerivedKeys & operator=(const DerivedKeys &)=default
Copy assignment (default).
uint8_t enc[KEY_SIZE]
Kunci untuk enkripsi data payload.
Definition mac.hpp:105
DerivedKeys(DerivedKeys &&) noexcept=default
Move constructor (default).
uint8_t mac[KEY_SIZE]
Kunci untuk tag integritas CMAC.
Definition mac.hpp:106
Fungsi utilitas pembantu untuk operasi byte, heksadesimal, I/O berkas, dan keacakan kriptografis.
void secure_zero(void *p, size_t len)
Membersihkan memori dengan mengisi byte nol (0) menggunakan volatile pointer.
Definition bytes.cpp:36