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 #include "modules.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /* Shared header file for all cipher algorithms */
33 
40 #if IS_USED(MODULE_CRYPTO_AES_256)
41  #define CIPHERS_MAX_KEY_SIZE 32
42 #elif IS_USED(MODULE_CRYPTO_AES_192)
43  #define CIPHERS_MAX_KEY_SIZE 24
44 #else
45  #define CIPHERS_MAX_KEY_SIZE 16
46 #endif
47 #define CIPHER_MAX_BLOCK_SIZE 16
48 
55 #if IS_USED(MODULE_CRYPTO_AES_256) || IS_USED(MODULE_CRYPTO_AES_192) || \
56  IS_USED(MODULE_CRYPTO_AES_128)
57  #define CIPHER_MAX_CONTEXT_SIZE CIPHERS_MAX_KEY_SIZE
58 #else
59 /* 0 is not a possibility because 0-sized arrays are not allowed in ISO C */
60  #define CIPHER_MAX_CONTEXT_SIZE 1
61 #endif
62 
63 /* return codes */
64 
65 #define CIPHER_ERR_INVALID_KEY_SIZE -3
66 #define CIPHER_ERR_INVALID_LENGTH -4
67 #define CIPHER_ERR_ENC_FAILED -5
68 #define CIPHER_ERR_DEC_FAILED -6
71 #define CIPHER_ERR_BAD_CONTEXT_SIZE 0
73 #define CIPHER_INIT_SUCCESS 1
74 
78 typedef struct {
79  uint8_t key_size;
80  uint8_t context[CIPHER_MAX_CONTEXT_SIZE];
82 
86 typedef struct cipher_interface_st {
88  uint8_t block_size;
89 
96  int (*init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size);
97 
99  int (*encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block,
100  uint8_t *cipher_block);
101 
103  int (*decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block,
104  uint8_t *plain_block);
106 
109 
113 extern const cipher_id_t CIPHER_AES;
114 
119 typedef struct {
124 } cipher_t;
125 
141 int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key,
142  uint8_t key_size);
143 
157 int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
158  uint8_t *output);
159 
173 int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
174  uint8_t *output);
175 
184 int cipher_get_block_size(const cipher_t *cipher);
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
191 #endif /* CRYPTO_CIPHERS_H */
struct cipher_interface_st cipher_interface_t
BlockCipher-Interface for the Cipher-Algorithms.
int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key, uint8_t key_size)
Initialize new cipher state.
#define CIPHER_MAX_CONTEXT_SIZE
Context sizes needed for the different ciphers.
Definition: ciphers.h:60
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output)
Decrypt data of BLOCK_SIZE length *.
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output)
Encrypt data of BLOCK_SIZE length *.
int cipher_get_block_size(const cipher_t *cipher)
Get block size of cipher *.
const cipher_interface_t * cipher_id_t
Pointer type to BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:108
const cipher_id_t CIPHER_AES
AES cipher id.
Common macros and compiler attributes/pragmas configuration.
the context for cipher-operations
Definition: ciphers.h:78
uint8_t key_size
key size used
Definition: ciphers.h:79
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:86
int(* decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block, uint8_t *plain_block)
the decrypt function
Definition: ciphers.h:103
uint8_t block_size
Blocksize of this cipher.
Definition: ciphers.h:88
int(* init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size)
the init function.
Definition: ciphers.h:96
int(* encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block, uint8_t *cipher_block)
the encrypt function
Definition: ciphers.h:99
basic struct for using block ciphers contains the cipher interface and the context
Definition: ciphers.h:119
cipher_context_t context
The encryption context (buffer) for the algorithm.
Definition: ciphers.h:122
const cipher_interface_t * interface
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:120