aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/aes128ecb_encrypt_file.cpp
blob: 694d8fe1a5bde2e5683e6f57833f4cf6d457507a (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
/**
 * \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.
 */

#include <aesni/all.h>
#include <aesnixx/all.hpp>

#include <cstdlib>

#include <exception>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

namespace
{
    void exit_with_usage()
    {
        std::cout << "Usage: aes128ecb_encrypt_file.exe KEY SRC DEST\n";
        std::exit(EXIT_FAILURE);
    }

    std::ifstream::pos_type get_file_size(const std::string& path)
    {
        std::ifstream ifs;
        ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
        ifs.open(path, std::ifstream::binary | std::ifstream::ate);
        return ifs.tellg();
    }
}

int main(int argc, char** argv)
{
    AesNI_Block128 key;
    AesNI_KeySchedule128 key_schedule;

    if (argc != 4)
        exit_with_usage();

    if (aesni_parse_block128(&key, argv[1]) != 0)
    {
        std::cerr << "Invalid 128-bit AES block '" << argv[1] << "'\n";
        exit_with_usage();
    }

    try
    {
        const std::string src_path(argv[2]);
        const std::string dest_path(argv[3]);

        const auto src_size = static_cast<std::size_t>(get_file_size(src_path));

        std::ifstream src_ifs;
        src_ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
        src_ifs.open(src_path, std::ifstream::binary);

        std::vector<char> src_buf;
        src_buf.reserve(src_size);
        src_buf.assign(std::istreambuf_iterator<char>(src_ifs),
                       std::istreambuf_iterator<char>());

        aesni_expand_key_schedule128(key, &key_schedule);

        std::size_t dest_size;

        aesni_encrypt_buffer_ecb128(
            src_buf.data(),
            src_size,
            NULL,
            &dest_size,
            &key_schedule,
            aesni::ErrorDetailsThrowsInDestructor());

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

        aesni_encrypt_buffer_ecb128(
            src_buf.data(),
            src_size,
            dest_buf.data(),
            &dest_size,
            &key_schedule,
            aesni::ErrorDetailsThrowsInDestructor());

        std::ofstream dest_ofs;
        dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
        dest_ofs.open(dest_path, std::ofstream::binary);
        dest_ofs.write(dest_buf.data(), dest_size);
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << "\n";
        return 1;
    }

    return 0;
}