-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ciphers.h
183 lines (152 loc) · 5.49 KB
/
ciphers.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2013 Freie Universität Berlin, Computer Systems & Telematics
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_crypto
* @{
*
* @file
* @brief Headers for the packet encryption class. They are used to encrypt single packets.
*
* @author Freie Universitaet Berlin, Computer Systems & Telematics
* @author Nicolai Schmittberger <[email protected]>
* @author Zakaria Kasmi <[email protected]>
* @author Mark Essien <[email protected]>
*/
#ifndef CRYPTO_CIPHERS_H
#define CRYPTO_CIPHERS_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Shared header file for all cipher algorithms */
/** @brief the length of keys in bytes */
#define CIPHERS_MAX_KEY_SIZE 20
#define CIPHER_MAX_BLOCK_SIZE 16
/**
* Context sizes needed for the different ciphers.
* Always order by number of bytes descending!!! <br><br>
*
* threedes needs 24 bytes <br>
* aes needs CIPHERS_MAX_KEY_SIZE bytes <br>
*/
#if defined(MODULE_CRYPTO_3DES)
#define CIPHER_MAX_CONTEXT_SIZE 24
#elif defined(MODULE_CRYPTO_AES)
#define CIPHER_MAX_CONTEXT_SIZE CIPHERS_MAX_KEY_SIZE
#else
/* 0 is not a possibility because 0-sized arrays are not allowed in ISO C */
#define CIPHER_MAX_CONTEXT_SIZE 1
#endif
/* return codes */
#define CIPHER_ERR_INVALID_KEY_SIZE -3
#define CIPHER_ERR_INVALID_LENGTH -4
#define CIPHER_ERR_ENC_FAILED -5
#define CIPHER_ERR_DEC_FAILED -6
/** Is returned by the cipher_init functions, if the corresponding alogirithm has not been included in the build */
#define CIPHER_ERR_BAD_CONTEXT_SIZE 0
/** Returned by cipher_init upon successful initialization of a cipher. */
#define CIPHER_INIT_SUCCESS 1
/**
* @brief the context for cipher-operations
*/
typedef struct {
uint8_t context[CIPHER_MAX_CONTEXT_SIZE]; /**< buffer for cipher operations */
} cipher_context_t;
/**
* @brief BlockCipher-Interface for the Cipher-Algorithms
*/
typedef struct cipher_interface_st {
/** @brief Blocksize of this cipher */
uint8_t block_size;
/**
* @brief the init function.
*
* This function is responsible for checking that the given key_size is
* valid for the chosen cipher.
*/
int (*init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size);
/** @brief the encrypt function */
int (*encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block,
uint8_t *cipher_block);
/** @brief the decrypt function */
int (*decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block,
uint8_t *plain_block);
} cipher_interface_t;
typedef const cipher_interface_t *cipher_id_t;
extern const cipher_id_t CIPHER_AES_128;
/**
* @brief basic struct for using block ciphers
* contains the cipher interface and the context
*/
typedef struct {
const cipher_interface_t *interface; /**< BlockCipher-Interface for the
Cipher-Algorithms */
cipher_context_t context; /**< The encryption context (buffer)
for the algorithm */
} cipher_t;
/**
* @brief Initialize new cipher state
*
* @param cipher cipher struct to init (already allocated memory)
* @param cipher_id cipher algorithm id
* @param key encryption key to use
* @param key_size length of the encryption key
*
* @return CIPHER_INIT_SUCCESS if the initialization was successful.
* @return CIPHER_ERR_BAD_CONTEXT_SIZE if CIPHER_MAX_CONTEXT_SIZE has not
* been defined (which means that the cipher has not been included
* in the build)
* @return The command may return CIPHER_ERR_INVALID_KEY_SIZE if the
* key size is not valid.
*/
int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key,
uint8_t key_size);
/**
* @brief Encrypt data of BLOCK_SIZE length
* *
*
* @param cipher Already initialized cipher struct
* @param input pointer to input data to encrypt
* @param output pointer to allocated memory for encrypted data. It has to
* be of size BLOCK_SIZE
*
* @return The result of the encrypt operation of the underlying
* cipher, which is always 1 in case of success
* @return A negative value for an error
*/
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
uint8_t *output);
/**
* @brief Decrypt data of BLOCK_SIZE length
* *
*
* @param cipher Already initialized cipher struct
* @param input pointer to input data (of size BLOCKS_SIZE) to decrypt
* @param output pointer to allocated memory for decrypted data. It has to
* be of size BLOCK_SIZE
*
* @return The result of the decrypt operation of the underlying
* cipher, which is always 1 in case of success
* @return A negative value for an error
*/
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
uint8_t *output);
/**
* @brief Get block size of cipher
* *
*
* @param cipher Already initialized cipher struct
*
* @return The cipher's block size (in bytes)
*/
int cipher_get_block_size(const cipher_t *cipher);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* CRYPTO_CIPHERS_H */