-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAES.h
159 lines (127 loc) · 3.58 KB
/
AES.h
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
/**
* AES.h
*
* The Advanced Encryption Standard (AES, aka AES) block cipher,
* designed by J. Daemen and V. Rijmen.
*
* @author Paulo S. L. M. Barreto
*
* This software is hereby placed in the public domain.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __AES_H
#define __AES_H
#include "BlockCipher.h"
#ifndef USUAL_TYPES
#define USUAL_TYPES
typedef unsigned char uchar;
typedef unsigned int uint; /* assuming sizeof(uint) == 4 */
#endif /* USUAL_TYPES */
#ifndef AES_BLOCKBITS
#define AES_BLOCKBITS 128
#endif
#if AES_BLOCKBITS != 128
#error "AES_BLOCKBITS must be 128"
#endif
#ifndef AES_BLOCKSIZE
#define AES_BLOCKSIZE 16 /* uchars */
#endif
#if AES_BLOCKSIZE != 16
#error "AES_BLOCKSIZE must be 16"
#endif
#ifndef AES_MINKEYBITS
#define AES_MINKEYBITS 128
#endif
#if AES_MINKEYBITS != 128
#error "AES_MINKEYBITS must be 128"
#endif
#ifndef AES_MINKEYSIZE
#define AES_MINKEYSIZE 16 /* uchars */
#endif
#if AES_MINKEYSIZE != 16
#error "AES_MINKEYSIZE must be 16"
#endif
#ifndef AES_MAXKEYBITS
#define AES_MAXKEYBITS 256
#endif
#if AES_MAXKEYBITS != 256
#error "AES_MAXKEYBITS must be 256"
#endif
#ifndef AES_MAXKEYSIZE
#define AES_MAXKEYSIZE 32 /* uchars */
#endif
#if AES_MAXKEYSIZE != 32
#error "AES_MAXKEYSIZE must be 32"
#endif
#define MAXKC (AES_MAXKEYBITS/32)
#define MAXKB (AES_MAXKEYBITS/8)
#define MAXNR 14
#ifndef TTABLE
#define TTABLE 1024
#endif
__global__ void AES_encrypt(const uint *pt, uint *ct, uint *rek, uint Nr, uint size);
__global__ void AES_decrypt(const uint *ct, uint *pt, uint *rdk, uint Nr);
class AES: public BlockCipher {
public:
AES();
virtual ~AES();
/**
* Block size in bits.
*/
inline uint blockBits() const {
return AES_BLOCKBITS;
}
/**
* Block size in uchars.
*/
inline uint blockSize() const {
return AES_BLOCKSIZE;
}
/**
* Key size in bits.
*/
inline uint keyBits() const {
return (Nr - 6) << 5;
}
/**
* Key size in uchars.
*/
inline uint keySize() const {
return (Nr - 6) << 2;
}
/**
* Convert one data block from uchar[] to uint[] representation.
*/
void uchar2int(const uchar *b, uint *i);
/**
* Convert one data block from int[] to uchar[] representation.
*/
void int2uchar(const uint *i, uchar *b);
void makeKey(const uchar *cipherKey, uint keyBits, uint dir);
void encrypt(const uint *pt, uint *ct);
void decrypt(const uint *ct, uint *pt);
void encrypt_ecb(const uint *pt, uint *ct, uint n);
void encrypt_ecb_async(const uint *pt, uint *ct, uint n);
private:
// static void Initialize();
void ExpandKey(const uchar *cipherKey, uint keyBits);
void InvertKey();
uint Nr;
uint e_sched[4*(MAXNR + 1)];
uint d_sched[4*(MAXNR + 1)];
// Pointers to GPU key schedules
uint *ce_sched;
uint *cd_sched;
};
#endif /* __AES_H */