Embedded SDK
Embedded SDK
lcx_cipher.h
Go to the documentation of this file.
1 /* @BANNER@ */
2 
11 #pragma once
12 #include "cx_errors.h"
13 #include "lcx_wrappers.h"
14 #include "lcx_common.h"
15 #include "lcx_aes.h"
16 #include <stddef.h>
17 
19 #define MAX_IV_LENGTH 16
21 #define MAX_BLOCK_LENGTH 16
22 
23 #ifdef HAVE_CMAC
25 #define CMAC_MAX_BLOCK_LENGTH (16)
26 
28 typedef struct {
29  uint8_t state[CMAC_MAX_BLOCK_LENGTH];
30  uint8_t unprocessed_block[CMAC_MAX_BLOCK_LENGTH];
31  size_t unprocessed_len;
32 } cx_cmac_context_t;
33 #endif // HAVE_CMAC
34 
36 typedef enum {
42 
44 typedef struct {
45  uint32_t size;
46  uint8_t keys[32];
47 } cipher_key_t;
48 
50 typedef struct {
51  cx_err_t (*enc_func)(const uint8_t *in_block, uint8_t *out_block);
52  cx_err_t (*dec_func)(const uint8_t *in_block, uint8_t *out_block);
53  cx_err_t (*ctr_func)(const cipher_key_t *ctx_key,
54  size_t len,
55  size_t *nc_off,
56  uint8_t *nonce_counter,
57  uint8_t *stream_block,
58  const uint8_t *input,
59  uint8_t *output);
60  cx_err_t (*setkey_func)(const cipher_key_t *ctx_key,
61  uint32_t operation,
62  const uint8_t *key,
63  uint32_t key_bitlen);
64  cx_err_t (*ctx_reset)(void);
66 
68 typedef struct {
69  uint32_t key_bitlen;
70  uint32_t iv_size;
71  uint32_t block_size;
72 #if !defined(BOLOS_OS_UPGRADER_APP)
74 #else
75  cx_cipher_base_t *base;
76 #endif
77 
79 
81 typedef struct {
83  uint32_t key_bitlen;
84  uint32_t operation;
85  void (*add_padding)(uint8_t *output, size_t out_len, size_t data_len);
86  cx_err_t (*get_padding)(uint8_t *input,
87  size_t in_len,
88  size_t *data_len);
89  uint8_t unprocessed_data[MAX_BLOCK_LENGTH];
90  size_t unprocessed_len;
92  size_t iv_size;
93  uint32_t mode;
96 #ifdef HAVE_CMAC
97  cx_cmac_context_t *cmac_ctx;
98 #endif // HAVE_CMAC
100 
112 WARN_UNUSED_RESULT cx_err_t cx_cipher_init(cx_cipher_context_t *ctx);
113 
134 WARN_UNUSED_RESULT cx_err_t cx_cipher_setup(cx_cipher_context_t *ctx,
135  const cx_cipher_id_t type,
136  uint32_t mode);
137 
158 WARN_UNUSED_RESULT cx_err_t cx_cipher_setkey(cx_cipher_context_t *ctx,
159  const uint8_t *key,
160  uint32_t key_bitlen,
161  uint32_t operation);
162 
180 WARN_UNUSED_RESULT cx_err_t cx_cipher_setiv(cx_cipher_context_t *ctx,
181  const uint8_t *iv,
182  size_t iv_len);
183 
202 WARN_UNUSED_RESULT cx_err_t cx_cipher_set_padding(cx_cipher_context_t *ctx, uint32_t padding);
203 
231 WARN_UNUSED_RESULT cx_err_t cx_cipher_update(cx_cipher_context_t *ctx,
232  const uint8_t *input,
233  size_t in_len,
234  uint8_t *output,
235  size_t *out_len);
236 
259 WARN_UNUSED_RESULT cx_err_t cx_cipher_finish(cx_cipher_context_t *ctx,
260  uint8_t *output,
261  size_t *out_len);
262 
291 WARN_UNUSED_RESULT cx_err_t cx_cipher_enc_dec(cx_cipher_context_t *ctx,
292  const uint8_t *iv,
293  size_t iv_len,
294  const uint8_t *input,
295  size_t in_len,
296  uint8_t *output,
297  size_t *out_len);
298 
300 
301 void add_one_and_zeros_padding(uint8_t *output, size_t out_len, size_t data_len);
AES (Advanced Encryption Standard).
void cx_cipher_reset(cx_cipher_context_t *ctx)
Definition: cx_cipher.c:516
void add_one_and_zeros_padding(uint8_t *output, size_t out_len, size_t data_len)
Definition: cx_cipher.c:79
WARN_UNUSED_RESULT cx_err_t cx_cipher_update(cx_cipher_context_t *ctx, const uint8_t *input, size_t in_len, uint8_t *output, size_t *out_len)
Encrypt or decrypt with the given context.
Definition: cx_cipher.c:324
cx_cipher_id_t
Definition: lcx_cipher.h:36
@ CX_CIPHER_AES_192
AES with a 192-bit key.
Definition: lcx_cipher.h:39
@ CX_CIPHER_NONE
No cipher.
Definition: lcx_cipher.h:37
@ CX_CIPHER_AES_256
AES with a 256-bit key.
Definition: lcx_cipher.h:40
@ CX_CIPHER_AES_128
AES with a 128-bit key.
Definition: lcx_cipher.h:38
WARN_UNUSED_RESULT cx_err_t cx_cipher_setkey(cx_cipher_context_t *ctx, const uint8_t *key, uint32_t key_bitlen, uint32_t operation)
Set the key to use.
Definition: cx_cipher.c:253
#define MAX_IV_LENGTH
Definition: lcx_cipher.h:19
WARN_UNUSED_RESULT cx_err_t cx_cipher_setiv(cx_cipher_context_t *ctx, const uint8_t *iv, size_t iv_len)
Set the initialization vector.
Definition: cx_cipher.c:276
WARN_UNUSED_RESULT cx_err_t cx_cipher_init(cx_cipher_context_t *ctx)
Initialize a cipher context as NONE.
Definition: cx_cipher.c:209
WARN_UNUSED_RESULT cx_err_t cx_cipher_finish(cx_cipher_context_t *ctx, uint8_t *output, size_t *out_len)
Finalize the operation.
Definition: cx_cipher.c:411
WARN_UNUSED_RESULT cx_err_t cx_cipher_setup(cx_cipher_context_t *ctx, const cx_cipher_id_t type, uint32_t mode)
Initialize and fill the context structure given the cipher info.
Definition: cx_cipher.c:218
WARN_UNUSED_RESULT cx_err_t cx_cipher_enc_dec(cx_cipher_context_t *ctx, const uint8_t *iv, size_t iv_len, const uint8_t *input, size_t in_len, uint8_t *output, size_t *out_len)
All-in-one encryption or decryption.
Definition: cx_cipher.c:492
WARN_UNUSED_RESULT cx_err_t cx_cipher_set_padding(cx_cipher_context_t *ctx, uint32_t padding)
Set the padding type.
Definition: cx_cipher.c:301
#define MAX_BLOCK_LENGTH
Definition: lcx_cipher.h:21
Cryptography flags.
uint32_t size
Definition: lcx_cipher.h:45
uint32_t mode
Mode of operation: ECB, CBC, CTR.
Definition: lcx_cipher.h:93
size_t iv_size
Length of the initialization vector.
Definition: lcx_cipher.h:92
uint32_t operation
Operation: encryption or decryption.
Definition: lcx_cipher.h:84
uint32_t key_bitlen
Key size in bits.
Definition: lcx_cipher.h:83
const cipher_key_t * cipher_key
Cipher-specific context.
Definition: lcx_cipher.h:95
const cx_cipher_info_t * cipher_info
Cipher information.
Definition: lcx_cipher.h:82
size_t unprocessed_len
Length of data to process.
Definition: lcx_cipher.h:90
const cx_cipher_base_t * base
Definition: lcx_cipher.h:73
uint32_t iv_size
Initialization vector size.
Definition: lcx_cipher.h:70
uint32_t key_bitlen
Key size.
Definition: lcx_cipher.h:69
uint32_t block_size
Block size.
Definition: lcx_cipher.h:71
unsigned char uint8_t
Definition: usbd_conf.h:53