forked from zhenlin36/scatter_gather_aes_cuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockCipher.h
109 lines (91 loc) · 2.9 KB
/
BlockCipher.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
/**
* BlockCipher.h
*
* A simple abstraction for the basic functionality of a block cipher engine.
*
* @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 __BLOCKCIPHER_H
#define __BLOCKCIPHER_H
#ifndef USUAL_TYPES
#define USUAL_TYPES
typedef unsigned char uchar;
typedef unsigned int uint; /* assuming sizeof(uint) == 4 */
#endif /* USUAL_TYPES */
#define DIR_NONE 0
/**
* Flag to setup the encryption key schedule.
*/
#define DIR_ENCRYPT 1
/**
* Flag to setup the decryption key schedule.
*/
#define DIR_DECRYPT 2
/**
* Flag to setup both key schedules (encryption/decryption).
*/
#define DIR_BOTH (DIR_ENCRYPT | DIR_DECRYPT) /* both directions */
class BlockCipher {
public:
/**
* Block size in bits.
*/
virtual uint blockBits() const = 0;
/**
* Block size in uchars.
*/
virtual uint blockSize() const = 0;
/**
* Key size in bits.
*/
virtual uint keyBits() const = 0;
/**
* Key size in uchars.
*/
virtual uint keySize() const = 0;
/**
* Convert one data block from uchar[] to uint[] representation.
*/
virtual void uchar2int(const uchar *b, uint *i) = 0;
/**
* Convert one data block from int[] to uchar[] representation.
*/
virtual void int2uchar(const uint *i, uchar *b) = 0;
/**
* Setup the key schedule for encryption, decryption, or both.
*
* @param cipherKey the cipher key.
* @param keyBits size of the cipher key in bits.
* @param direction cipher direction (DIR_ENCRYPT, DIR_DECRYPT, or DIR_BOTH).
*/
virtual void makeKey(const uchar *cipherKey, uint keyBits, uint dir) = 0;
/**
* Encrypt exactly one block of plaintext.
*
* @param pt plaintext block.
* @param ct ciphertext block.
*/
virtual void encrypt(const uint *pt, uint *ct) = 0;
/**
* Decrypt exactly one block of ciphertext.
*
* @param ct ciphertext block.
* @param pt plaintext block.
*/
virtual void decrypt(const uint *ct, uint *pt) = 0;
};
#endif /* __BLOCKCIPHER_H */