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 
34 #define CIPHERS_MAX_KEY_SIZE 20
35 #define CIPHER_MAX_BLOCK_SIZE 16
36 
44 #if defined(MODULE_CRYPTO_3DES)
45  #define CIPHER_MAX_CONTEXT_SIZE 24
46 #elif defined(MODULE_CRYPTO_AES)
47  #define CIPHER_MAX_CONTEXT_SIZE CIPHERS_MAX_KEY_SIZE
48 #else
49 /* 0 is not a possibility because 0-sized arrays are not allowed in ISO C */
50  #define CIPHER_MAX_CONTEXT_SIZE 1
51 #endif
52 
53 /* return codes */
54 
55 #define CIPHER_ERR_INVALID_KEY_SIZE -3
56 #define CIPHER_ERR_INVALID_LENGTH -4
57 #define CIPHER_ERR_ENC_FAILED -5
58 #define CIPHER_ERR_DEC_FAILED -6
59 
60 #define CIPHER_ERR_BAD_CONTEXT_SIZE 0
61 
62 #define CIPHER_INIT_SUCCESS 1
63 
67 typedef struct {
68  uint8_t context[CIPHER_MAX_CONTEXT_SIZE];
70 
71 
75 typedef struct cipher_interface_st {
77  uint8_t block_size;
78 
80  uint8_t max_key_size;
81 
83  int (*init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size);
84 
86  int (*encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block,
87  uint8_t *cipher_block);
88 
90  int (*decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block,
91  uint8_t *plain_block);
93 
94 
95 typedef const cipher_interface_t *cipher_id_t;
96 
97 extern const cipher_id_t CIPHER_AES_128;
98 
99 
104 typedef struct {
109 } cipher_t;
110 
111 
127 int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key,
128  uint8_t key_size);
129 
130 
144 int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
145  uint8_t *output);
146 
147 
161 int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
162  uint8_t *output);
163 
164 
173 int cipher_get_block_size(const cipher_t *cipher);
174 
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
181 #endif /* CRYPTO_CIPHERS_H */
cipher_t
basic struct for using block ciphers contains the cipher interface and the context
Definition: ciphers.h:104
cipher_interface_st::block_size
uint8_t block_size
Blocksize of this cipher.
Definition: ciphers.h:77
cipher_encrypt
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output)
Encrypt data of BLOCK_SIZE length *.
CIPHER_MAX_CONTEXT_SIZE
#define CIPHER_MAX_CONTEXT_SIZE
Context sizes needed for the different ciphers.
Definition: ciphers.h:50
cipher_t::context
cipher_context_t context
The encryption context (buffer) for the algorithm.
Definition: ciphers.h:107
cipher_interface_st
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:75
cipher_init
int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key, uint8_t key_size)
Initialize new cipher state.
cipher_t::interface
const cipher_interface_t * interface
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:105
cipher_interface_st::decrypt
int(* decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block, uint8_t *plain_block)
the decrypt function
Definition: ciphers.h:90
cipher_context_t
the context for cipher-operations
Definition: ciphers.h:67
cipher_decrypt
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output)
Decrypt data of BLOCK_SIZE length *.
cipher_interface_st::max_key_size
uint8_t max_key_size
Maximum key size for this cipher.
Definition: ciphers.h:80
cipher_interface_st::encrypt
int(* encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block, uint8_t *cipher_block)
the encrypt function
Definition: ciphers.h:86
cipher_get_block_size
int cipher_get_block_size(const cipher_t *cipher)
Get block size of cipher *.
cipher_interface_t
struct cipher_interface_st cipher_interface_t
BlockCipher-Interface for the Cipher-Algorithms.
cipher_interface_st::init
int(* init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size)
the init function
Definition: ciphers.h:83