ciphers.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Freie Universit├Ąt Berlin, Computer Systems & Telematics
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
22 #ifndef CRYPTO_CIPHERS_H
23 #define CRYPTO_CIPHERS_H
24 
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* Shared header file for all cipher algorithms */
32 
33 /* Set the algorithms that should be compiled in here. When these defines
34  * are set, then packets will be compiled 5 times.
35  */
36 // #define CRYPTO_THREEDES
37 // #define CRYPTO_AES
38 
40 #define CIPHERS_MAX_KEY_SIZE 20
41 #define CIPHER_MAX_BLOCK_SIZE 16
42 
43 
51 #if defined(CRYPTO_THREEDES)
52  #define CIPHER_MAX_CONTEXT_SIZE 24
53 #elif defined(CRYPTO_AES)
54  #define CIPHER_MAX_CONTEXT_SIZE CIPHERS_MAX_KEY_SIZE
55 #else
56  // 0 is not a possibility because 0-sized arrays are not allowed in ISO C
57  #define CIPHER_MAX_CONTEXT_SIZE 1
58 #endif
59 
60 /* return codes */
61 
62 #define CIPHER_ERR_INVALID_KEY_SIZE -3
63 #define CIPHER_ERR_INVALID_LENGTH -4
64 #define CIPHER_ERR_ENC_FAILED -5
65 #define CIPHER_ERR_DEC_FAILED -6
66 
67 #define CIPHER_ERR_BAD_CONTEXT_SIZE 0
68 
69 #define CIPHER_INIT_SUCCESS 1
70 
74 typedef struct {
75  uint8_t context[CIPHER_MAX_CONTEXT_SIZE];
77 
78 
82 typedef struct cipher_interface_st {
84  uint8_t block_size;
85 
87  uint8_t max_key_size;
88 
90  int (*init)(cipher_context_t* ctx, const uint8_t* key, uint8_t key_size);
91 
93  int (*encrypt)(const cipher_context_t* ctx, const uint8_t* plain_block,
94  uint8_t* cipher_block);
95 
97  int (*decrypt)(const cipher_context_t* ctx, const uint8_t* cipher_block,
98  uint8_t* plain_block);
100 
101 
102 typedef const cipher_interface_t *cipher_id_t;
103 
104 extern const cipher_id_t CIPHER_AES_128;
105 
106 
111 typedef struct {
116 } cipher_t;
117 
118 
131 int cipher_init(cipher_t* cipher, cipher_id_t cipher_id, const uint8_t* key,
132  uint8_t key_size);
133 
134 
144 int cipher_encrypt(const cipher_t* cipher, const uint8_t* input, uint8_t* output);
145 
146 
156 int cipher_decrypt(const cipher_t* cipher, const uint8_t* input, uint8_t* output);
157 
158 
165 int cipher_get_block_size(const cipher_t* cipher);
166 
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
173 #endif /* CRYPTO_CIPHERS_H */
int(* init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size)
the init function
Definition: ciphers.h:90
the context for cipher-operations
Definition: ciphers.h:74
uint8_t block_size
Blocksize of this cipher.
Definition: ciphers.h:84
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:82
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output)
Encrypt data of BLOCK_SIZE length *.
int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key, uint8_t key_size)
Initialize new cipher state.
basic struct for using block ciphers contains the cipher interface and the context ...
Definition: ciphers.h:111
#define CIPHER_MAX_CONTEXT_SIZE
Context sizes needed for the different ciphers.
Definition: ciphers.h:57
const cipher_interface_t * interface
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:112
cipher_context_t context
The encryption context (buffer) for the algorithm.
Definition: ciphers.h:114
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output)
Decrypt data of BLOCK_SIZE length *.
int cipher_get_block_size(const cipher_t *cipher)
Get block size of cipher *.
int(* decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block, uint8_t *plain_block)
the decrypt function
Definition: ciphers.h:97
struct cipher_interface_st cipher_interface_t
BlockCipher-Interface for the Cipher-Algorithms.
int(* encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block, uint8_t *cipher_block)
the encrypt function
Definition: ciphers.h:93
uint8_t max_key_size
Maximum key size for this cipher.
Definition: ciphers.h:87