Loading...
Searching...
No Matches
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
9#pragma once
10
23
24#include <stdint.h>
25#include "modules.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/* Shared header file for all cipher algorithms */
32
39#if IS_USED(MODULE_CRYPTO_AES_256)
40 #define CIPHERS_MAX_KEY_SIZE 32
41#elif IS_USED(MODULE_CRYPTO_AES_192)
42 #define CIPHERS_MAX_KEY_SIZE 24
43#else
44 #define CIPHERS_MAX_KEY_SIZE 16
45#endif
46#define CIPHER_MAX_BLOCK_SIZE 16
47
54#if IS_USED(MODULE_CRYPTO_AES_256) || IS_USED(MODULE_CRYPTO_AES_192) || \
55 IS_USED(MODULE_CRYPTO_AES_128)
56 #define CIPHER_MAX_CONTEXT_SIZE CIPHERS_MAX_KEY_SIZE
57#else
58/* 0 is not a possibility because 0-sized arrays are not allowed in ISO C */
59 #define CIPHER_MAX_CONTEXT_SIZE 1
60#endif
61
62/* return codes */
63
64#define CIPHER_ERR_INVALID_KEY_SIZE -3
65#define CIPHER_ERR_INVALID_LENGTH -4
66#define CIPHER_ERR_ENC_FAILED -5
67#define CIPHER_ERR_DEC_FAILED -6
70#define CIPHER_ERR_BAD_CONTEXT_SIZE 0
72#define CIPHER_INIT_SUCCESS 1
73
77typedef struct {
78 uint8_t key_size;
81
85typedef struct cipher_interface_st {
87 uint8_t block_size;
88
95 int (*init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size);
96
98 int (*encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block,
99 uint8_t *cipher_block);
100
102 int (*decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block,
103 uint8_t *plain_block);
105
108
112extern const cipher_id_t CIPHER_AES;
113
124
140int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key,
141 uint8_t key_size);
142
156int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
157 uint8_t *output);
158
172int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
173 uint8_t *output);
174
184
185#ifdef __cplusplus
186}
187#endif
188
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:59
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:107
const cipher_id_t CIPHER_AES
AES cipher id.
Common macros and compiler attributes/pragmas configuration.
the context for cipher-operations
Definition ciphers.h:77
uint8_t context[CIPHER_MAX_CONTEXT_SIZE]
buffer for cipher operations
Definition ciphers.h:79
uint8_t key_size
key size used
Definition ciphers.h:78
BlockCipher-Interface for the Cipher-Algorithms.
Definition ciphers.h:85
int(* decrypt)(const cipher_context_t *ctx, const uint8_t *cipher_block, uint8_t *plain_block)
the decrypt function
Definition ciphers.h:102
uint8_t block_size
Blocksize of this cipher.
Definition ciphers.h:87
int(* init)(cipher_context_t *ctx, const uint8_t *key, uint8_t key_size)
the init function.
Definition ciphers.h:95
int(* encrypt)(const cipher_context_t *ctx, const uint8_t *plain_block, uint8_t *cipher_block)
the encrypt function
Definition ciphers.h:98
basic struct for using block ciphers contains the cipher interface and the context
Definition ciphers.h:118
cipher_context_t context
The encryption context (buffer) for the algorithm.
Definition ciphers.h:121
const cipher_interface_t * interface
BlockCipher-Interface for the Cipher-Algorithms.
Definition ciphers.h:119