aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/encrypt_block_aes.cpp
blob: 9db6e329fa1d8e45f1d6f15f55102b6805301ede (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
/**
 * \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 <cstring>

#include <exception>
#include <iostream>

namespace
{
    void exit_with_usage()
    {
        std::cout << "Usage: encrypt_block_aes.exe KEY0 IV0 [PLAIN0...] [-- KEY1 IV1 [PLAIN1...]...]\n";
        std::exit(EXIT_FAILURE);
    }
}

int main(int argc, char** argv)
{
    try
    {
        for (--argc, ++argv; argc > -1; --argc, ++argv)
        {
            if (argc < 2)
                exit_with_usage();

            AesNI_AlgorithmParams algorithm_params;
            aesni::from_string(algorithm_params.aes128_key, argv[0]);

            AesNI_State iv;
            aesni::from_string(iv.aes_block, argv[1]);

            AesNI_Box box;
            aesni_box_init(
                &box,
                AESNI_AES128,
                &algorithm_params,
                AESNI_OFB,
                &iv,
                aesni::ErrorDetailsThrowsInDestructor());

            for (argc -= 2, argv += 2; argc > 0; --argc, ++argv)
            {
                if (std::strcmp("--", argv[0]) == 0)
                    break;

                AesNI_State plaintext;
                aesni::from_string(plaintext.aes_block, argv[0]);

                AesNI_State ciphertext;
                aesni_box_encrypt(
                    &box,
                    &plaintext,
                    &ciphertext,
                    aesni::ErrorDetailsThrowsInDestructor());

                std::cout << ciphertext.aes_block << "\n";
            }
        }

        return 0;
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << "\n";
        return 1;
    }
}