aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cxx/include/aesxx/box.hpp
blob: ffca6ce3c960f2aae43784206a5b065adc80c56d (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
/**
 * \file
 * \author Egor Tensin <Egor.Tensin@gmail.com>
 * \date 2015
 * \copyright This file is licensed under the terms of the MIT License.
 *            See LICENSE.txt for details.
 */

#pragma once

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

#include <aes/all.h>

#include <cassert>
#include <cstddef>

#include <iostream>
#include <string>
#include <vector>

namespace aesni
{
    class Box
    {
    public:
        typedef AesNI_BoxBlock Block;
        typedef AesNI_BoxKey Key;

        static std::string format_key(const Key& src, Algorithm algorithm)
        {
            AesNI_BoxKeyString str;
            aesni_box_format_key(
                &str, algorithm, &src, ErrorDetailsThrowsInDestructor());
            return reinterpret_cast<const char*>(&str);
        }

        static std::string format_block(const Block& src, Algorithm algorithm)
        {
            AesNI_BoxBlockString str;
            aesni_box_format_block(
                &str, algorithm, &src, ErrorDetailsThrowsInDestructor());
            return reinterpret_cast<const char*>(&str);
        }

        static void parse_block(
            Block& dest,
            Algorithm algorithm,
            const char* src)
        {
            aesni_box_parse_block(&dest, algorithm, src,
                ErrorDetailsThrowsInDestructor());
        }

        static void parse_block(
            Block& dest,
            Algorithm algorithm,
            const std::string& src)
        {
            parse_block(dest, algorithm, src.c_str());
        }

        static void parse_key(
            Key& dest,
            Algorithm algorithm,
            const char* src)
        {
            aesni_box_parse_key(&dest, algorithm, src,
                ErrorDetailsThrowsInDestructor());
        }

        static void parse_key(
            Key& dest,
            Algorithm algorithm,
            const std::string& src)
        {
            parse_key(dest, algorithm, src.c_str());
        }

        Box(Algorithm algorithm, const Key& key)
            : algorithm(algorithm)
            , mode(AESNI_ECB)
        {
            aesni_box_init(&impl, algorithm, &key, mode, nullptr,
                ErrorDetailsThrowsInDestructor());
        }

        Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv)
            : algorithm(algorithm)
            , mode(mode)
        {
            aesni_box_init(&impl, algorithm, &key, mode, &iv,
                ErrorDetailsThrowsInDestructor());
        }

        void encrypt_block(const Block& plaintext, Block& ciphertext)
        {
            aesni_box_encrypt_block(
                &impl, &plaintext, &ciphertext,
                ErrorDetailsThrowsInDestructor());
        }

        void decrypt_block(const Block& ciphertext, Block& plaintext)
        {
            aesni_box_decrypt_block(
                &impl, &ciphertext, &plaintext,
                ErrorDetailsThrowsInDestructor());
        }

        std::vector<unsigned char> encrypt_buffer(
            const void* src_buf,
            std::size_t src_size)
        {
            std::size_t dest_size;

            aesni_box_encrypt_buffer(
                &impl,
                src_buf,
                src_size,
                nullptr,
                &dest_size,
                aesni::ErrorDetailsThrowsInDestructor());

            std::vector<unsigned char> dest_buf;
            dest_buf.resize(dest_size);

            aesni_box_encrypt_buffer(
                &impl,
                src_buf,
                src_size,
                dest_buf.data(),
                &dest_size,
                aesni::ErrorDetailsThrowsInDestructor());

            dest_buf.resize(dest_size);
            return dest_buf;
        }

        std::vector<unsigned char> decrypt_buffer(
            const void* src_buf,
            std::size_t src_size)
        {
            std::size_t dest_size;

            aesni_box_decrypt_buffer(
                &impl,
                src_buf,
                src_size,
                nullptr,
                &dest_size,
                aesni::ErrorDetailsThrowsInDestructor());

            std::vector<unsigned char> dest_buf;
            dest_buf.resize(dest_size);

            aesni_box_decrypt_buffer(
                &impl,
                src_buf,
                src_size,
                dest_buf.data(),
                &dest_size,
                aesni::ErrorDetailsThrowsInDestructor());

            dest_buf.resize(dest_size);
            return dest_buf;
        }

        std::string format_block(const Block& src)
        {
            return format_block(src, get_algorithm());
        }

        std::string format_key(const Key& src)
        {
            return format_key(src, get_algorithm());
        }

        void parse_block(Block& dest, const char* src)
        {
            parse_block(dest, get_algorithm(), src);
        }

        void parse_block(Block& dest, const std::string& src)
        {
            parse_block(dest, src.c_str());
        }

        void parse_key(Key& dest, const char* src)
        {
            parse_key(dest, get_algorithm(), src);
        }

        void parse_key(Key& dest, const std::string& src)
        {
            parse_key(dest, src.c_str());
        }

        Algorithm get_algorithm() const { return algorithm; }

        Mode get_mode() const { return mode; }

    private:
        Key key;

        Algorithm algorithm;
        Mode mode;

        AesNI_Box impl;
    };
}