aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cxx/include/aesxx/aes.hpp
blob: efac790aa01638b645e39fff309038ec81d66d51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "AES tools" project.
// For details, see https://github.com/egor-tensin/aes-tools.
// Distributed under the MIT License.

#pragma once

#include "algorithm.hpp"
#include "api.hpp"
#include "error.hpp"
#include "mode.hpp"

#include <aes/all.h>

#include <cstddef>

#include <string>

namespace aes
{
    namespace aes128
    {
        typedef AES_AES128_Block Block;
        typedef AES_AES128_RoundKeys RoundKeys;
        typedef AES_AES128_Key Key;
    }

    template <>
    struct Types<AES_AES128>
    {
        typedef aes128::Block Block;
        typedef aes128::RoundKeys RoundKeys;
        typedef aes128::Key Key;
    };

    template <>
    inline std::size_t get_number_of_rounds<AES_AES128>()
    {
        return 11;
    }

    template <>
    inline void from_string<AES_AES128>(aes128::Block& dest, const char* src)
    {
        aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{});
    }

    template <>
    inline std::string to_string<AES_AES128>(const aes128::Block& src)
    {
        AES_AES128_BlockString str;
        aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline std::string to_matrix_string<AES_AES128>(const aes128::Block& src)
    {
        AES_AES128_BlockMatrixString str;
        aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline void from_string<AES_AES128>(aes128::Key& dest, const char* src)
    {
        aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{});
    }

    template <>
    inline std::string to_string<AES_AES128>(const aes128::Key& src)
    {
        AES_AES128_KeyString str;
        aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline void expand_key<AES_AES128>(
        const aes128::Key& key,
        aes128::RoundKeys& encryption_keys)
    {
        aes_AES128_expand_key(&key, &encryption_keys);
    }

    template <>
    inline void derive_decryption_keys<AES_AES128>(
        const aes128::RoundKeys& encryption_keys,
        aes128::RoundKeys& decryption_keys)
    {
        aes_AES128_derive_decryption_keys(
            &encryption_keys, &decryption_keys);
    }

    AESXX_ENCRYPT_BLOCK_ECB(AES128);
    AESXX_DECRYPT_BLOCK_ECB(AES128);
    AESXX_ENCRYPT_BLOCK_CBC(AES128);
    AESXX_DECRYPT_BLOCK_CBC(AES128);
    AESXX_ENCRYPT_BLOCK_CFB(AES128);
    AESXX_DECRYPT_BLOCK_CFB(AES128);
    AESXX_ENCRYPT_BLOCK_OFB(AES128);
    AESXX_DECRYPT_BLOCK_OFB(AES128);
    AESXX_ENCRYPT_BLOCK_CTR(AES128);
    AESXX_DECRYPT_BLOCK_CTR(AES128);

    namespace aes192
    {
        typedef AES_AES192_Block Block;
        typedef AES_AES192_RoundKeys RoundKeys;
        typedef AES_AES192_Key Key;
    }

    template <>
    struct Types<AES_AES192>
    {
        typedef aes192::Block Block;
        typedef aes192::RoundKeys RoundKeys;
        typedef aes192::Key Key;
    };

    template <>
    inline std::size_t get_number_of_rounds<AES_AES192>()
    {
        return 13;
    }

    template <>
    inline void from_string<AES_AES192>(aes192::Block& dest, const char* src)
    {
        aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{});
    }

    template <>
    inline std::string to_string<AES_AES192>(const aes192::Block& src)
    {
        AES_AES192_BlockString str;
        aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline std::string to_matrix_string<AES_AES192>(const aes192::Block& src)
    {
        AES_AES192_BlockMatrixString str;
        aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline void from_string<AES_AES192>(aes192::Key& dest, const char* src)
    {
        aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{});
    }

    template <>
    inline std::string to_string<AES_AES192>(const aes192::Key& src)
    {
        AES_AES192_KeyString str;
        aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline void expand_key<AES_AES192>(
        const aes192::Key& key,
        aes192::RoundKeys& encryption_keys)
    {
        aes_AES192_expand_key(&key, &encryption_keys);
    }

    template <>
    inline void derive_decryption_keys<AES_AES192>(
        const aes192::RoundKeys& encryption_keys,
        aes192::RoundKeys& decryption_keys)
    {
        aes_AES192_derive_decryption_keys(
            &encryption_keys, &decryption_keys);
    }

    AESXX_ENCRYPT_BLOCK_ECB(AES192);
    AESXX_DECRYPT_BLOCK_ECB(AES192);
    AESXX_ENCRYPT_BLOCK_CBC(AES192);
    AESXX_DECRYPT_BLOCK_CBC(AES192);
    AESXX_ENCRYPT_BLOCK_CFB(AES192);
    AESXX_DECRYPT_BLOCK_CFB(AES192);
    AESXX_ENCRYPT_BLOCK_OFB(AES192);
    AESXX_DECRYPT_BLOCK_OFB(AES192);
    AESXX_ENCRYPT_BLOCK_CTR(AES192);
    AESXX_DECRYPT_BLOCK_CTR(AES192);

    namespace aes256
    {
        typedef AES_AES256_Block Block;
        typedef AES_AES256_RoundKeys RoundKeys;
        typedef AES_AES256_Key Key;
    }

    template <>
    struct Types<AES_AES256>
    {
        typedef aes256::Block Block;
        typedef aes256::RoundKeys RoundKeys;
        typedef aes256::Key Key;
    };

    template <>
    inline std::size_t get_number_of_rounds<AES_AES256>()
    {
        return 15;
    }

    template <>
    inline void from_string<AES_AES256>(aes256::Block& dest, const char* src)
    {
        aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{});
    }

    template <>
    inline std::string to_string<AES_AES256>(const aes256::Block& src)
    {
        AES_AES256_BlockString str;
        aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline std::string to_matrix_string<AES_AES256>(const aes256::Block& src)
    {
        AES_AES256_BlockMatrixString str;
        aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline void from_string<AES_AES256>(aes256::Key& dest, const char* src)
    {
        aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{});
    }

    template <>
    inline std::string to_string<AES_AES256>(const aes256::Key& src)
    {
        AES_AES256_KeyString str;
        aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{});
        return {str.str};
    }

    template <>
    inline void expand_key<AES_AES256>(
        const aes256::Key& key,
        aes256::RoundKeys& encryption_keys)
    {
        aes_AES256_expand_key(&key, &encryption_keys);
    }

    template <>
    inline void derive_decryption_keys<AES_AES256>(
        const aes256::RoundKeys& encryption_keys,
        aes256::RoundKeys& decryption_keys)
    {
        aes_AES256_derive_decryption_keys(
            &encryption_keys, &decryption_keys);
    }

    AESXX_ENCRYPT_BLOCK_ECB(AES256);
    AESXX_DECRYPT_BLOCK_ECB(AES256);
    AESXX_ENCRYPT_BLOCK_CBC(AES256);
    AESXX_DECRYPT_BLOCK_CBC(AES256);
    AESXX_ENCRYPT_BLOCK_CFB(AES256);
    AESXX_DECRYPT_BLOCK_CFB(AES256);
    AESXX_ENCRYPT_BLOCK_OFB(AES256);
    AESXX_DECRYPT_BLOCK_OFB(AES256);
    AESXX_ENCRYPT_BLOCK_CTR(AES256);
    AESXX_DECRYPT_BLOCK_CTR(AES256);
}