M5Utility 0.0.9 git rev:d29fae5
Loading...
Searching...
No Matches
des.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_DES_HPP
11#define M5_UTILITY_DES_HPP
12#include <cstdint>
13#include <array>
14
15namespace m5 {
16namespace utility {
17namespace crypto {
18
23class TripleDES {
24public:
25 using Key = std::array<uint8_t, 8>;
26 using Key8 = Key;
27 using Key16 = std::array<uint8_t, 16>;
28 using Key24 = std::array<uint8_t, 24>;
29
34 enum class Mode : uint8_t {
35 ECB,
36 CBC,
37 };
42 enum class Padding : uint8_t {
43 None,
44 Zero,
45 PKCS7,
46 };
47
55 TripleDES(const Mode mode, const Padding pad, const uint8_t iv[8] = nullptr);
56
59
70 inline uint32_t encrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key& key)
71 {
72 return encrypt_3key(out, in, in_len, key, key, key);
73 }
85 inline uint32_t encrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key& key1, const Key& key2)
86 {
87 return encrypt_3key(out, in, in_len, key1, key2, key1);
88 }
99 uint32_t encrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key16& key);
112 inline uint32_t encrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key& key1, const Key& key2,
113 const Key& key3)
114 {
115 return encrypt_3key(out, in, in_len, key1, key2, key3);
116 }
127 uint32_t encrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key24& key);
129
132 inline uint32_t decrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key& key)
133 {
134 return decrypt_3key(out, in, in_len, key, key, key);
135 }
136
137 inline uint32_t decrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key& key1, const Key& key2)
138 {
139 return decrypt_3key(out, in, in_len, key1, key2, key1);
140 }
141
142 inline uint32_t decrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key& key1, const Key& key2,
143 const Key& key3)
144 {
145 return decrypt_3key(out, in, in_len, key1, key2, key3);
146 }
147
148 uint32_t decrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key16& key);
149 uint32_t decrypt(uint8_t* out, const uint8_t* in, const uint32_t in_len, const Key24& key);
151
152 static void encrypt_block(uint8_t out[8], const uint8_t in[8], const Key& key);
153 static void decrypt_block(uint8_t out[8], const uint8_t in[8], const Key& key);
154
155protected:
156 static void key_schedule(uint64_t subkeys[16], const Key& key);
157 static uint32_t feistel(const uint32_t r, const uint64_t subkey);
158 static uint64_t permute_bits(const uint64_t in, const uint8_t* table, const uint32_t table_len,
159 const uint32_t in_bits);
160 static void process_block(uint8_t out[8], const uint8_t in[8], const Key& key, bool encrypt);
161
162 static void encrypt_block_3key(uint8_t out[8], const uint8_t in[8], const Key8& k1, const Key8& k2, const Key8& k3);
163 uint32_t encrypt_3key(uint8_t* out, const uint8_t* in, uint32_t in_len, const Key& k1, const Key& k2,
164 const Key& k3);
165 static void decrypt_block_3key(uint8_t out[8], const uint8_t in[8], const Key8& k1, const Key8& k2, const Key8& k3);
166 uint32_t decrypt_3key(uint8_t* out, const uint8_t* in, uint32_t in_len, const Key& k1, const Key& k2,
167 const Key& k3);
168
169private:
170 Mode _mode{};
171 Padding _pad{};
172 uint8_t _iv[8]{};
173};
174
175} // namespace crypto
176} // namespace utility
177} // namespace m5
178
179#endif
Definition des.hpp:23
uint32_t encrypt(uint8_t *out, const uint8_t *in, const uint32_t in_len, const Key &key)
Encrypto 1-key 3DES.
Definition des.hpp:70
Mode
Operation mode.
Definition des.hpp:34
Padding
Padding mode.
Definition des.hpp:42
uint32_t encrypt(uint8_t *out, const uint8_t *in, const uint32_t in_len, const Key &key1, const Key &key2)
Encrypto 2-key 3DES.
Definition des.hpp:85
TripleDES(const Mode mode, const Padding pad, const uint8_t iv[8]=nullptr)
Constructor.
Definition des.cpp:135
uint32_t encrypt(uint8_t *out, const uint8_t *in, const uint32_t in_len, const Key &key1, const Key &key2, const Key &key3)
Encrypto 3-key 3DES.
Definition des.hpp:112
Top level namespace of M5.
Definition base64.cpp:18
For utilities.