ciphers.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2013 Freie Universität Berlin, Computer Systems & Telematics
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include <stdint.h>
22 #include "modules.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /* Shared header file for all cipher algorithms */
29 
36 #if IS_USED(MODULE_CRYPTO_AES_256)
37  #define CIPHERS_MAX_KEY_SIZE 32
38 #elif IS_USED(MODULE_CRYPTO_AES_192)
39  #define CIPHERS_MAX_KEY_SIZE 24
40 #else
41  #define CIPHERS_MAX_KEY_SIZE 16
42 #endif
43 #define CIPHER_MAX_BLOCK_SIZE 16
44 
51 #if IS_USED(MODULE_CRYPTO_AES_256) || IS_USED(MODULE_CRYPTO_AES_192) || \
52  IS_USED(MODULE_CRYPTO_AES_128)
53  #define CIPHER_MAX_CONTEXT_SIZE CIPHERS_MAX_KEY_SIZE
54 #else
55 /* 0 is not a possibility because 0-sized arrays are not allowed in ISO C */
56  #define CIPHER_MAX_CONTEXT_SIZE 1
57 #endif
58 
59 /* return codes */
60 
61 #define CIPHER_ERR_INVALID_KEY_SIZE -3
62 #define CIPHER_ERR_INVALID_LENGTH -4
63 #define CIPHER_ERR_ENC_FAILED -5
64 #define CIPHER_ERR_DEC_FAILED -6
67 #define CIPHER_ERR_BAD_CONTEXT_SIZE 0
69 #define CIPHER_INIT_SUCCESS 1
70 
74 typedef struct {
75  uint8_t key_size;
76  uint8_t context[CIPHER_MAX_CONTEXT_SIZE];
78 
82 typedef struct cipher_interface_st {
84  uint8_t block_size;
85 
92  int (*init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size);
93 
95  int (*encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block,
96  uint8_t *cipher_block);
97 
99  int (*decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block,
100  uint8_t *plain_block);
102 
105 
109 extern const cipher_id_t CIPHER_AES;
110 
115 typedef struct {
120 } cipher_t;
121 
137 int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key,
138  uint8_t key_size);
139 
153 int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
154  uint8_t *output);
155 
169 int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
170  uint8_t *output);
171 
180 int cipher_get_block_size(const cipher_t *cipher);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
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:56
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:104
const cipher_id_t CIPHER_AES
AES cipher id.
Common macros and compiler attributes/pragmas configuration.
the context for cipher-operations
Definition: ciphers.h:74
uint8_t key_size
key size used
Definition: ciphers.h:75
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:82
int(* decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block, uint8_t *plain_block)
the decrypt function
Definition: ciphers.h:99
uint8_t block_size
Blocksize of this cipher.
Definition: ciphers.h:84
int(* init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size)
the init function.
Definition: ciphers.h:92
int(* encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block, uint8_t *cipher_block)
the encrypt function
Definition: ciphers.h:95
basic struct for using block ciphers contains the cipher interface and the context
Definition: ciphers.h:115
cipher_context_t context
The encryption context (buffer) for the algorithm.
Definition: ciphers.h:118
const cipher_interface_t * interface
BlockCipher-Interface for the Cipher-Algorithms.
Definition: ciphers.h:116