Embedded SDK
Embedded SDK
lcx_aead.h
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * Ledger Nano S - Secure firmware
4  * (c) 2022 Ledger
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  ********************************************************************************/
18 
34 #ifndef LCX_AEAD_H
35 #define LCX_AEAD_H
36 
37 #if defined(HAVE_AEAD)
38 
39 #include "cx_errors.h"
40 #include <stddef.h>
41 #if defined(HAVE_AES_GCM)
42 #include "lcx_aes_gcm.h"
43 #endif
44 #if defined(HAVE_CHACHA_POLY) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
45 #include "lcx_chacha_poly.h"
46 #endif
47 
48 #define GCM_MAX_IV_LENGTH 64
49 #define MAX_TAG_LENGTH 16
50 
54 typedef enum {
55  CX_AEAD_AES128_GCM,
56  CX_AEAD_AES192_GCM,
57  CX_AEAD_AES256_GCM,
58  CX_AEAD_CHACHA20_POLY1305
59 } cx_aead_type_t;
60 
65 typedef struct {
66  void (*init)(void *ctx);
67  cx_err_t (*set_key)(void *ctx, const uint8_t *key, size_t key_len);
68  cx_err_t (*start)(void *ctx,
69  uint32_t mode,
70  const uint8_t *iv,
71  size_t iv_len);
72  cx_err_t (*update_aad)(void *ctx,
73  const uint8_t *aad,
74  size_t aad_len);
75  cx_err_t (*update)(void *ctx,
76  const uint8_t *input,
77  uint8_t *output,
78  size_t len);
79  cx_err_t (*finish)(void *ctx,
80  uint8_t *tag,
81  size_t tag_len);
82  cx_err_t (*encrypt_and_tag)(void *ctx,
83  const uint8_t *input,
84  size_t len,
85  const uint8_t *iv,
86  size_t iv_len,
87  const uint8_t *aad,
88  size_t aad_len,
89  uint8_t *output,
90  uint8_t *tag,
91  size_t tag_len);
92 
93  cx_err_t (*auth_decrypt)(void *ctx,
94  const uint8_t *input,
95  size_t len,
96  const uint8_t *iv,
97  size_t iv_len,
98  const uint8_t *aad,
99  size_t aad_len,
100  uint8_t *output,
101  const uint8_t *tag,
102  size_t tag_len);
103  cx_err_t (*check_tag)(void *ctx, const uint8_t *tag, size_t tag_len);
104 } cx_aead_base_t;
105 
109 typedef struct {
110  cx_aead_type_t type;
111  size_t key_bitlen;
112  size_t block_size;
113  const cx_aead_base_t *func;
114 } cx_aead_info_t;
115 
120 typedef struct {
121  const cx_aead_info_t *info;
122  uint32_t mode;
123  void *base_ctx;
125 } cx_aead_context_t;
126 
144 WARN_UNUSED_RESULT cx_err_t cx_aead_init(cx_aead_context_t *ctx);
145 
161 WARN_UNUSED_RESULT cx_err_t cx_aead_setup(cx_aead_context_t *ctx, cx_aead_type_t type);
162 
179 WARN_UNUSED_RESULT cx_err_t cx_aead_set_key(cx_aead_context_t *ctx,
180  const uint8_t *key,
181  size_t key_len,
182  uint32_t mode);
183 
197 WARN_UNUSED_RESULT cx_err_t cx_aead_set_iv(cx_aead_context_t *ctx,
198  const uint8_t *iv,
199  size_t iv_len);
200 
215 WARN_UNUSED_RESULT cx_err_t cx_aead_update_ad(cx_aead_context_t *ctx,
216  const uint8_t *ad,
217  size_t ad_len);
218 
239 WARN_UNUSED_RESULT cx_err_t
240 cx_aead_update(cx_aead_context_t *ctx, uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len);
241 
255 WARN_UNUSED_RESULT cx_err_t cx_aead_write_tag(cx_aead_context_t *ctx, uint8_t *tag, size_t tag_len);
256 
270 WARN_UNUSED_RESULT cx_err_t cx_aead_check_tag(cx_aead_context_t *ctx,
271  const uint8_t *tag,
272  size_t tag_len);
273 
304 WARN_UNUSED_RESULT cx_err_t cx_aead_encrypt(cx_aead_context_t *ctx,
305  const uint8_t *iv,
306  size_t iv_len,
307  const uint8_t *ad,
308  size_t ad_len,
309  uint8_t *in,
310  size_t in_len,
311  uint8_t *out,
312  size_t *out_len,
313  uint8_t *tag,
314  size_t tag_len);
315 
347 WARN_UNUSED_RESULT cx_err_t cx_aead_decrypt(cx_aead_context_t *ctx,
348  const uint8_t *iv,
349  size_t iv_len,
350  const uint8_t *ad,
351  size_t ad_len,
352  uint8_t *in,
353  size_t in_len,
354  uint8_t *out,
355  size_t *out_len,
356  const uint8_t *tag,
357  size_t tag_len);
358 
359 #endif // HAVE_AEAD
360 
361 #endif // LCX_AED_H
AES in Galois/Counter Mode (AES-GCM)
CHACHA20_POLY1305 Authenticated Encryption with Additional Data (AEAD)
unsigned char uint8_t
Definition: usbd_conf.h:53