aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/aes.cc
blob: 71348744d148ab3fb4f751bbf13b1482046a4276 (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
#include <qpdf/Pl_AES_PDF.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QIntC.hh>

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>

static void usage()
{
    std::cerr << "Usage: aes options hex-key infile outfile" << std::endl
              << "  -cbc         -- disable CBC mode" << std::endl
              << "  +cbc         -- enable CBC mode" << std::endl
              << "  -encrypt     -- encrypt" << std::endl
              << "  -decrypt     -- decrypt CBC mode" << std::endl
              << "  -zero-iv     -- use zero initialization vector" << std::endl
              << "  -static-iv   -- use static initialization vector" << std::endl
              << "  -no-padding  -- disable padding" << std::endl
              << "Options must precede key and file names." << std::endl;
    exit(2);
}

int main(int argc, char* argv[])
{
    bool encrypt = true;
    bool cbc_mode = true;
    char* hexkey = 0;
    char* infilename = 0;
    char* outfilename = 0;
    bool zero_iv = false;
    bool static_iv = false;
    bool disable_padding = false;

    for (int i = 1; i < argc; ++i)
    {
        char* arg = argv[i];
        if ((arg[0] == '-') || (arg[0] == '+'))
        {
            if (strcmp(arg, "-cbc") == 0)
            {
                cbc_mode = false;
            }
            else if (strcmp(arg, "+cbc") == 0)
            {
                cbc_mode = true;
            }
            else if (strcmp(arg, "-decrypt") == 0)
            {
                encrypt = false;
            }
            else if (strcmp(arg, "-encrypt") == 0)
            {
                encrypt = true;
            }
            else if (strcmp(arg, "-zero-iv") == 0)
            {
                zero_iv = true;
            }
            else if (strcmp(arg, "-static-iv") == 0)
            {
                static_iv = true;
            }
            else if (strcmp(arg, "-no-padding") == 0)
            {
                disable_padding = true;
            }
            else
            {
                usage();
            }
        }
        else if (argc == i + 3)
        {
            hexkey = argv[i];
            infilename = argv[i+1];
            outfilename = argv[i+2];
            break;
        }
        else
        {
            usage();
        }
    }
    if (outfilename == 0)
    {
        usage();
    }

    unsigned int hexkeylen = QIntC::to_uint(strlen(hexkey));
    unsigned int keylen = hexkeylen / 2;

    FILE* infile = QUtil::safe_fopen(infilename, "rb");
    FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
    unsigned char* key = new unsigned char[keylen];
    for (unsigned int i = 0; i < strlen(hexkey); i += 2)
    {
        char t[3];
        t[0] = hexkey[i];
        t[1] = hexkey[i + 1];
        t[2] = '\0';

        long val = strtol(t, 0, 16);
        key[i/2] = static_cast<unsigned char>(val);
    }

    Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
    Pl_AES_PDF* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key, keylen);
    delete [] key;
    key = 0;
    if (! cbc_mode)
    {
        aes->disableCBC();
    }
    if (zero_iv)
    {
        aes->useZeroIV();
    }
    else if (static_iv)
    {
        aes->useStaticIV();
    }
    if (disable_padding)
    {
        aes->disablePadding();
    }

    // 16 < buffer size, buffer_size is not a multiple of 8 for testing
    unsigned char buf[83];
    bool done = false;
    while (! done)
    {
        size_t len = fread(buf, 1, sizeof(buf), infile);
        if (len <= 0)
        {
            done = true;
        }
        else
        {
            aes->write(buf, len);
        }
    }
    aes->finish();
    delete aes;
    delete out;
    fclose(infile);
    fclose(outfile);
    return 0;
}