~kai-mast/ynetwork/trunk

« back to all changes in this revision

Viewing changes to CEncrypterAES.cpp

  • Committer: kai.mast at freakybytes
  • Date: 2011-03-29 22:36:44 UTC
  • Revision ID: kai.mast@freakybytes.org-20110329223644-2kue9pajtw3d1cvr
Pushed local changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "CEncrypterAES.h"
2
2
 
3
 
//! Code take from "Hoozi Resources"
4
 
//! Encrypt: http://www.hoozi.com/post/829n1/advanced-encryption-standard-aes-implementation-in-c-c-with-comments-part-1-encryption
5
 
//! Decrypt: http://www.hoozi.com/post/neqd5/advanced-encryption-standard-aes-implementation-in-c-c-with-comments-part-2-decryption
6
 
 
7
3
namespace yogi
8
4
{
9
 
    //! xtime is a macro that finds the product of {02} and the argument to xtime modulo {1b}
10
 
    inline s32 xtime(s32 x)
11
 
    {
12
 
        return (x<<1) ^ (((x>>7) & 1) * 0x1b);
13
 
    }
14
 
 
15
 
    //! Multiplty is a macro used to multiply numbers in the field GF(2^8)
16
 
    inline s32 multiply(s32 x, s32 y)
17
 
    {
18
 
        return ((y & 1) * x) ^ ((y>>1 & 1) * xtime(x)) ^ ((y>>2 & 1) * xtime(xtime(x))) ^ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))));
19
 
    }
20
 
 
21
 
    //! The number of columns comprising a state in AES. This is a constant in AES. Value=4
22
 
    static u32 NumberColumns = 4;
23
 
 
24
 
    //! The round constant word array, Rcon[i], contains the values given by
25
 
    //! x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(28)
26
 
    //! Note that i starts at 1, not 0).
27
 
    static int Rcon[255] = {
28
 
        0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
29
 
        0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
30
 
        0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
31
 
        0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
32
 
        0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
33
 
        0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
34
 
        0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
35
 
        0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
36
 
        0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
37
 
        0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
38
 
        0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
39
 
        0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
40
 
        0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
41
 
        0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
42
 
        0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
43
 
        0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb
44
 
    };
45
 
 
46
 
    s32 CEncrypterAES::getSBoxValue(u32 num)
47
 
    {
48
 
        s32 sbox[256] =   {
49
 
            //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F
50
 
            0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0
51
 
            0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1
52
 
            0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2
53
 
            0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3
54
 
            0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4
55
 
            0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5
56
 
            0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6
57
 
            0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7
58
 
            0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8
59
 
            0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9
60
 
            0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A
61
 
            0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B
62
 
            0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C
63
 
            0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D
64
 
            0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E
65
 
            0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
66
 
        }; //F
67
 
 
68
 
        return sbox[num];
69
 
    }
70
 
 
71
 
    s32 CEncrypterAES::getSBoxValueInvert(s32 num)
72
 
    {
73
 
        s32 rsbox[256] = {
74
 
            0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
75
 
            0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
76
 
            0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
77
 
            0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
78
 
            0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
79
 
            0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
80
 
            0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
81
 
            0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
82
 
            0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
83
 
            0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
84
 
            0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
85
 
            0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
86
 
            0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
87
 
            0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
88
 
            0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
89
 
            0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
90
 
        };
91
 
        return rsbox[num];
92
 
    }
93
 
 
94
 
    CEncrypterAES::CEncrypterAES() : RoundKey(0), Key(0), KeyLength(0), NumberRounds(0)
 
5
    static u32 AES_BLOCK_SIZE = 16;
 
6
 
 
7
    CEncrypterAES::CEncrypterAES() : valid(false)
95
8
    {
96
9
    }
97
10
 
98
11
    CEncrypterAES::~CEncrypterAES()
99
12
    {
100
 
        if(RoundKey)
101
 
            delete RoundKey;
102
 
    }
103
 
 
104
 
    //!  This function produces numberStates*(numberRounds+1) round keys. The round keys are used in each round to encrypt the states.
105
 
    void CEncrypterAES::keyExpansion()
106
 
    {
107
 
        u32 i, j;
108
 
        u8 temp[4], k;
109
 
 
110
 
        if(RoundKey)
111
 
            delete[] RoundKey;
112
 
 
113
 
        RoundKey = new u8[4* NumberColumns * (NumberRounds+1)];
114
 
 
115
 
        // The first round key is the key itself.
116
 
        for(i=0; i < KeyLength/4; i++)
117
 
        {
118
 
            RoundKey[i*4] = Key[i*4];
119
 
            RoundKey[i*4+1] = Key[i*4+1];
120
 
            RoundKey[i*4+2] = Key[i*4+2];
121
 
            RoundKey[i*4+3]= Key[i*4+3];
122
 
        }
123
 
 
124
 
        // All other round keys are found from the previous round keys.
125
 
        while (i < (NumberColumns * (NumberRounds+1)))
126
 
        {
127
 
            for(j=0; j<4; j++)
128
 
            {
129
 
                temp[j]=RoundKey[(i-1) * 4 + j];
130
 
            }
131
 
 
132
 
            if (!(i % (KeyLength/4)))
133
 
            {
134
 
                // This function rotates the 4 bytes in a word to the left once.
135
 
                // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
136
 
 
137
 
                // Function RotWord()
138
 
                {
139
 
                    k = temp[0];
140
 
                    temp[0] = temp[1];
141
 
                    temp[1] = temp[2];
142
 
                    temp[2] = temp[3];
143
 
                    temp[3] = k;
144
 
                }
145
 
 
146
 
                // SubWord() is a function that takes a four-byte input word and
147
 
                // applies the S-box to each of the four bytes to produce an output word.
148
 
 
149
 
                // Function Subword()
150
 
                {
151
 
                    temp[0]=getSBoxValue(temp[0]);
152
 
                    temp[1]=getSBoxValue(temp[1]);
153
 
                    temp[2]=getSBoxValue(temp[2]);
154
 
                    temp[3]=getSBoxValue(temp[3]);
155
 
                }
156
 
 
157
 
                temp[0] =  temp[0] ^ Rcon[i/(KeyLength/4)];
158
 
            }
159
 
            else if ((KeyLength/4) > 6 && i % (KeyLength/4) == 4)
160
 
            {
161
 
                // Function Subword()
162
 
                {
163
 
                    temp[0]=getSBoxValue(temp[0]);
164
 
                    temp[1]=getSBoxValue(temp[1]);
165
 
                    temp[2]=getSBoxValue(temp[2]);
166
 
                    temp[3]=getSBoxValue(temp[3]);
167
 
                }
168
 
            }
169
 
            RoundKey[i*4+0] = RoundKey[(i-(KeyLength/4))*4+0] ^ temp[0];
170
 
            RoundKey[i*4+1] = RoundKey[(i-(KeyLength/4))*4+1] ^ temp[1];
171
 
            RoundKey[i*4+2] = RoundKey[(i-(KeyLength/4))*4+2] ^ temp[2];
172
 
            RoundKey[i*4+3] = RoundKey[(i-(KeyLength/4))*4+3] ^ temp[3];
173
 
 
174
 
            ++i;
175
 
        }
176
 
    }
177
 
 
178
 
    //! This function adds the round key to state.
179
 
    //! The round key is added to the state by an XOR function.
180
 
    void CEncrypterAES::addRoundKey(u32 round)
181
 
    {
182
 
        for(u32 i=0; i<4; i++)
183
 
        {
184
 
            for(u32 j=0; j<4; j++)
185
 
            {
186
 
                std::cout << RoundKey[round * NumberColumns * 4 + i * NumberColumns + j] << std::endl;
187
 
 
188
 
                state[j][i] ^= RoundKey[round * NumberColumns * 4 + i * NumberColumns + j];
189
 
            }
190
 
        }
191
 
    }
192
 
 
193
 
    //! The SubBytes Function Substitutes the values in the
194
 
    //! state matrix with values in an S-box.
195
 
    void CEncrypterAES::subBytes()
196
 
    {
197
 
        s32 i,j;
198
 
        for(i=0; i<4; i++)
199
 
        {
200
 
            for(j=0; j<4; j++)
201
 
            {
202
 
                state[i][j] = getSBoxValue(state[i][j]);
203
 
            }
204
 
        }
205
 
    }
206
 
 
207
 
    //! The SubBytes Function Substitutes the values in the
208
 
    //! state matrix with values in an S-box.
209
 
    void CEncrypterAES::invSubBytes()
210
 
    {
211
 
        s32 i,j;
212
 
        for(i=0; i<4; i++)
213
 
        {
214
 
            for(j=0; j<4 ; j++)
215
 
            {
216
 
                state[i][j] = getSBoxValueInvert(state[i][j]);
217
 
            }
218
 
        }
219
 
    }
220
 
 
221
 
    //! The ShiftRows() function shifts the rows in the state to the left.
222
 
    //! Each row is shifted with different offset.
223
 
    //! Offset = Row number. So the first row is not shifted.
224
 
    void CEncrypterAES::shiftRows()
225
 
    {
226
 
        u8 temp;
227
 
 
228
 
        // Rotate first row 1 columns to left
229
 
        temp = state[1][0];
230
 
        state[1][0] = state[1][1];
231
 
        state[1][1] = state[1][2];
232
 
        state[1][2] = state[1][3];
233
 
        state[1][3] = temp;
234
 
 
235
 
        // Rotate second row 2 columns to left
236
 
        temp = state[2][0];
237
 
        state[2][0] = state[2][2];
238
 
        state[2][2] = temp;
239
 
 
240
 
        temp = state[2][1];
241
 
        state[2][1] = state[2][3];
242
 
        state[2][3] = temp;
243
 
 
244
 
        // Rotate third row 3 columns to left
245
 
        temp= state[3][0];
246
 
        state[3][0] = state[3][3];
247
 
        state[3][3] = state[3][2];
248
 
        state[3][2] = state[3][1];
249
 
        state[3][1] = temp;
250
 
    }
251
 
 
252
 
    void CEncrypterAES::invShiftRows()
253
 
    {
254
 
        u8 temp;
255
 
 
256
 
        // Rotate first row 1 columns to right
257
 
        temp=state[1][3];
258
 
        state[1][3]=state[1][2];
259
 
        state[1][2]=state[1][1];
260
 
        state[1][1]=state[1][0];
261
 
        state[1][0]=temp;
262
 
 
263
 
        // Rotate second row 2 columns to right
264
 
        temp=state[2][0];
265
 
        state[2][0]=state[2][2];
266
 
        state[2][2]=temp;
267
 
 
268
 
        temp=state[2][1];
269
 
        state[2][1]=state[2][3];
270
 
        state[2][3]=temp;
271
 
 
272
 
        // Rotate third row 3 columns to right
273
 
        temp=state[3][0];
274
 
        state[3][0]=state[3][1];
275
 
        state[3][1]=state[3][2];
276
 
        state[3][2]=state[3][3];
277
 
        state[3][3]=temp;
278
 
    }
279
 
 
280
 
    //! MixColumns function mixes the columns of the state matrix
281
 
    void CEncrypterAES::mixColumns()
282
 
    {
283
 
        s32 i;
284
 
        u8 Tmp,Tm,t;
285
 
 
286
 
        for(i=0; i<4; i++)
287
 
        {
288
 
            t=state[0][i];
289
 
            Tmp = state[0][i] ^ state[1][i] ^ state[2][i] ^ state[3][i] ;
290
 
            Tm = state[0][i] ^ state[1][i] ;
291
 
            Tm = xtime(Tm);
292
 
            state[0][i] ^= Tm ^ Tmp ;
293
 
            Tm = state[1][i] ^ state[2][i] ;
294
 
            Tm = xtime(Tm);
295
 
            state[1][i] ^= Tm ^ Tmp ;
296
 
            Tm = state[2][i] ^ state[3][i] ;
297
 
            Tm = xtime(Tm);
298
 
            state[2][i] ^= Tm ^ Tmp ;
299
 
            Tm = state[3][i] ^ t ;
300
 
            Tm = xtime(Tm);
301
 
            state[3][i] ^= Tm ^ Tmp ;
302
 
        }
303
 
    }
304
 
 
305
 
    //! MixColumns function mixes the columns of the state matrix.
306
 
    //! The method used to multiply may be difficult to understand for beginners.
307
 
    //! Please use the references to gain more information.
308
 
    void CEncrypterAES::invMixColumns()
309
 
    {
310
 
        s32 i;
311
 
        u8 a,b,c,d;
312
 
 
313
 
        for(i=0; i<4; i++)
314
 
        {
315
 
            a = state[0][i];
316
 
            b = state[1][i];
317
 
            c = state[2][i];
318
 
            d = state[3][i];
319
 
 
320
 
            state[0][i] = multiply(a, 0x0e) ^ multiply(b, 0x0b) ^ multiply(c, 0x0d) ^ multiply(d, 0x09);
321
 
            state[1][i] = multiply(a, 0x09) ^ multiply(b, 0x0e) ^ multiply(c, 0x0b) ^ multiply(d, 0x0d);
322
 
            state[2][i] = multiply(a, 0x0d) ^ multiply(b, 0x09) ^ multiply(c, 0x0e) ^ multiply(d, 0x0b);
323
 
            state[3][i] = multiply(a, 0x0b) ^ multiply(b, 0x0d) ^ multiply(c, 0x09) ^ multiply(d, 0x0e);
324
 
        }
 
13
        this->reset();
325
14
    }
326
15
 
327
16
    EEncryptionType CEncrypterAES::getType()
328
17
    {
329
 
        return EET_AES;
330
 
    }
331
 
 
332
 
    bool CEncrypterAES::setKey(std::string newKey)
333
 
    {
334
 
        if((newKey.size()*8 == 128 ) || (newKey.size()*8 == 192) || (newKey.size()*8 == 256))
 
18
        return Encryption_AES;
 
19
    }
 
20
 
 
21
    void CEncrypterAES::reset()
 
22
    {
 
23
        if(!valid)
 
24
            return;
 
25
 
 
26
        EVP_CIPHER_CTX_cleanup(&e_ctx);
 
27
        EVP_CIPHER_CTX_cleanup(&d_ctx);
 
28
 
 
29
        valid = false;
 
30
    }
 
31
 
 
32
    bool CEncrypterAES::setKey(std::string key)
 
33
    {
 
34
        this->reset();
 
35
 
 
36
        u32 keySize = key.size()*8;
 
37
 
 
38
        u8* newkey = new u8[key.size()];
 
39
        u8* iv = new u8[key.size()];
 
40
 
 
41
        if((keySize != 128 ) && (keySize != 192) && (keySize != 256))
 
42
            return false;
 
43
 
 
44
        u32 i = 0;
 
45
        s32 nrounds = 5;
 
46
        std::string salt = "yoginetworksalt";
 
47
 
 
48
        if(keySize == 256)
 
49
            i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), (u8*)salt.data(), (u8*)key.data(), key.size(), nrounds, newkey, iv);
 
50
        else if(keySize == 192)
 
51
            i = EVP_BytesToKey(EVP_aes_192_cbc(), EVP_sha1(), (u8*)salt.data(), (u8*)key.data(), key.size(), nrounds, newkey, iv);
 
52
        else if(keySize == 128)
 
53
            i = EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), (u8*)salt.data(), (u8*)key.data(), key.size(), nrounds, newkey, iv);
 
54
 
 
55
        if(i != key.size())
335
56
        {
336
 
            if(Key)
337
 
                delete[] Key;
338
 
 
339
 
            Key = new u8[newKey.size()];
340
 
            KeyLength = newKey.size();
341
 
 
342
 
            NumberRounds = ( KeyLength/4 ) + 6;
343
 
 
344
 
            for(u32 i = 0; i < KeyLength; i++)
345
 
                Key[i] = newKey[i];
346
 
 
347
 
            this->keyExpansion();
348
 
 
349
 
            return true;
 
57
            delete []newkey;
 
58
            delete []iv;
 
59
 
 
60
            return false;
350
61
        }
351
62
 
352
 
        return false;
 
63
        EVP_CIPHER_CTX_init(&e_ctx);
 
64
 
 
65
        if(keySize == 256)
 
66
            EVP_EncryptInit_ex(&e_ctx, EVP_aes_256_cbc(), 0, newkey, iv);
 
67
        else if(keySize == 192)
 
68
            EVP_EncryptInit_ex(&e_ctx, EVP_aes_192_cbc(), 0, newkey, iv);
 
69
        else if(keySize == 128)
 
70
            EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), 0, newkey, iv);
 
71
 
 
72
        EVP_CIPHER_CTX_init(&d_ctx);
 
73
 
 
74
        if(keySize == 256)
 
75
            EVP_DecryptInit_ex(&d_ctx, EVP_aes_256_cbc(), 0, newkey, iv);
 
76
        else if(keySize == 192)
 
77
            EVP_DecryptInit_ex(&d_ctx, EVP_aes_192_cbc(), 0, newkey, iv);
 
78
        else if(keySize == 128)
 
79
            EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), 0, newkey, iv);
 
80
 
 
81
        delete []newkey;
 
82
        delete []iv;
 
83
 
 
84
        valid = true;
 
85
 
 
86
        return true;
353
87
    }
354
88
 
355
89
    std::string CEncrypterAES::getKey()
356
90
    {
357
 
        if(!Key)
358
 
            return "";
359
 
 
360
 
        return std::string((const char*) Key);
 
91
        return "";
361
92
    }
362
93
 
363
94
    std::list<u32> CEncrypterAES::getPossibleKeyLengths()
371
102
        return keylist;
372
103
    }
373
104
 
374
 
    std::string CEncrypterAES::encrypt(std::string& input)
375
 
    {
376
 
        std::string output;
377
 
        output.resize(input.size());
378
 
 
379
 
        u32 amountBlocks = (input.size()+1)/16;
380
 
 
381
 
        if((input.size()+1) % 16)
382
 
            amountBlocks++;
383
 
 
384
 
        u8 block[16];
385
 
 
386
 
        for(u32 u = 0; u < amountBlocks; ++u)
387
 
        {
388
 
            for(u32 u2 = 0; u2 < 16; ++u2)
389
 
            {
390
 
                if(u*16 + u2 < input.size())
391
 
                    block[u2] = input[u*16 + u2];
392
 
                else
393
 
                    block[u2] = '\0';
394
 
            }
395
 
 
396
 
            if(!this->encryptBlock(block))
397
 
                return "";
398
 
 
399
 
            for(u32 u2 = 0; u2 < 16 && u*16 + u2 < output.size(); ++u2)
400
 
            {
401
 
                output[u*16 + u2] = block[u2];
402
 
            }
403
 
        }
404
 
 
405
 
        return output;
406
 
    }
407
 
 
408
 
    std::string CEncrypterAES::decrypt(std::string& input)
409
 
    {
410
 
        std::string output;
411
 
        output.resize(input.size());
412
 
 
413
 
        u32 amountBlocks = input.size()/16;
414
 
 
415
 
        if(input.size() % 16)
416
 
            amountBlocks++;
417
 
 
418
 
        u8 block[16];
419
 
 
420
 
        for(u32 u = 0; u < amountBlocks; ++u)
421
 
        {
422
 
            for(u32 u2 = 0; u2 < 4; ++u2)
423
 
            {
424
 
                if(u*16 + u2 < input.size())
425
 
                    block[u2] = input[u*16 + u2];
426
 
                else
427
 
                    block[u2] = '\0';
428
 
            }
429
 
 
430
 
            if(!this->decryptBlock(block))
431
 
                return "";
432
 
 
433
 
            for(u32 u2 = 0; u2 < 4 && u*16 + u2 < output.size(); ++u2)
434
 
            {
435
 
                output[u*16 + u2] = block[u2];
436
 
            }
437
 
        }
438
 
 
439
 
        return output;
440
 
    }
441
 
 
442
 
 
443
 
    bool CEncrypterAES::encryptBlock(u8* block)
444
 
    {
445
 
        if(!this->isValid())
446
 
            return false;
447
 
 
448
 
        u32 i,j,round = 0;
449
 
 
450
 
        //Copy the input PlainText to state array.
451
 
        for(i=0; i<4; i++)
452
 
        {
453
 
            for(j=0; j<4; j++)
454
 
            {
455
 
                state[j][i] = block[i*4 + j];
456
 
            }
457
 
        }
458
 
 
459
 
        // Add the First round key to the state before starting the rounds.
460
 
        this->addRoundKey(0);
461
 
 
462
 
 
463
 
        for(i=0; i<4; i++)
464
 
        {
465
 
            for(j=0; j<4; j++)
466
 
            {
467
 
                std::cout << state[j][i];
468
 
            }
469
 
        }
470
 
 
471
 
        std::cout << std::endl;
472
 
 
473
 
        // There will be Nr rounds.
474
 
        // The first Nr-1 rounds are identical.
475
 
        // These Nr-1 rounds are executed in the loop below.
476
 
        for(round = 1; round < KeyLength; round++)
477
 
        {
478
 
            this->subBytes();
479
 
            this->shiftRows();
480
 
            this->mixColumns();
481
 
            this->addRoundKey(round);
482
 
        }
483
 
 
484
 
        // The last round is given below.
485
 
        // The MixColumns function is not here in the last round.
486
 
        this->subBytes();
487
 
        this->shiftRows();
488
 
        this->addRoundKey(NumberRounds);
489
 
 
490
 
        // The encryption process is over.
491
 
        // Copy the state array to output array.
492
 
        for(i=0; i<4; i++)
493
 
        {
494
 
            for(j=0; j<4; j++)
495
 
            {
496
 
                block[i*4+j] = state[j][i];
497
 
            }
498
 
        }
499
 
 
500
 
        return true;
501
 
    }
502
 
 
503
 
    bool CEncrypterAES::decryptBlock(u8* block)
504
 
    {
505
 
        if(!this->isValid())
506
 
            return false;
507
 
 
508
 
        s32 i,j,round = 0;
509
 
 
510
 
        //Copy the input CipherText to state array.
511
 
        for(i=0; i<4; i++)
512
 
        {
513
 
            for(j=0; j<4; j++)
514
 
            {
515
 
                state[j][i] = block[i*4 + j];
516
 
            }
517
 
        }
518
 
 
519
 
        // Add the First round key to the state before starting the rounds.
520
 
        this->addRoundKey(NumberRounds);
521
 
 
522
 
        // There will be Nr rounds.
523
 
        // The first Nr-1 rounds are identical.
524
 
        // These Nr-1 rounds are executed in the loop below.
525
 
        for(round = NumberRounds-1; round>0; round--)
526
 
        {
527
 
            this->invShiftRows();
528
 
            this->invSubBytes();
529
 
            this->addRoundKey(round);
530
 
            this->invMixColumns();
531
 
        }
532
 
 
533
 
        // The last round is given below.
534
 
        // The MixColumns function is not here in the last round.
535
 
        this->invShiftRows();
536
 
        this->invSubBytes();
537
 
        this->addRoundKey(0);
538
 
 
539
 
        // The decryption process is over.
540
 
        // Copy the state array to output array.
541
 
        for(i=0; i<4; i++)
542
 
        {
543
 
            for(j=0; j<4; j++)
544
 
            {
545
 
                block[i*4+j] = state[j][i];
546
 
            }
547
 
        }
548
 
 
549
 
        return true;
 
105
    void CEncrypterAES::encrypt(std::string& input)
 
106
    {
 
107
        u32 len = input.size();
 
108
        s32 f_len = 0;
 
109
        s32 c_len = len + AES_BLOCK_SIZE;
 
110
 
 
111
        u8 *ciphertext = new u8[c_len];
 
112
 
 
113
        EVP_EncryptInit_ex(&e_ctx, 0, 0, 0,0);
 
114
        EVP_EncryptUpdate(&e_ctx, ciphertext, &c_len, (u8*)input.data(), len);
 
115
        EVP_EncryptFinal_ex(&e_ctx, ciphertext+c_len, &f_len);
 
116
 
 
117
        len = c_len + f_len;
 
118
 
 
119
        std::string output((c8*)ciphertext, len);
 
120
        delete []ciphertext;
 
121
 
 
122
        input = output;
 
123
    }
 
124
 
 
125
    void CEncrypterAES::decrypt(std::string& input)
 
126
    {
 
127
        /* plaintext will always be equal to or lesser than length of ciphertext*/
 
128
        s32 len = input.size();
 
129
        s32 p_len = input.size();
 
130
        s32 f_len = 0;
 
131
        u8 *plaintext = new u8[p_len];
 
132
 
 
133
        EVP_DecryptInit_ex(&d_ctx, 0,0,0,0);
 
134
        EVP_DecryptUpdate(&d_ctx, plaintext, &p_len, (u8*)input.data(), len);
 
135
        EVP_DecryptFinal_ex(&d_ctx, plaintext+p_len, &f_len);
 
136
 
 
137
        len =  p_len + f_len;
 
138
 
 
139
        std::string output((c8*)plaintext, len);
 
140
        delete []plaintext;
 
141
 
 
142
        input = output;
550
143
    }
551
144
 
552
145
    bool CEncrypterAES::isValid()
553
146
    {
554
 
        return (KeyLength != 0);
 
147
        return valid;
555
148
    }
556
149
}
 
150
 
 
151
 
 
152