Embedded SDK
Embedded SDK
lcx_chacha_poly.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 
29 #ifndef LCX_CHACHA_POLY_H
30 #define LCX_CHACHA_POLY_H
31 
32 #if defined(HAVE_CHACHA_POLY)
33 #if defined(HAVE_POLY1305) && defined(HAVE_CHACHA)
34 
35 #include "lcx_chacha.h"
36 #include "lcx_poly1305.h"
37 #include "ox.h"
38 #include <stddef.h>
39 
40 typedef struct {
41  cx_chacha_context_t chacha20_ctx;
42  cx_poly1305_context_t poly1305_ctx;
43  size_t aad_len;
44  size_t ciphertext_len;
45  uint32_t state;
46  uint32_t mode;
47 } cx_chachapoly_context_t;
48 
49 void cx_chachapoly_init(cx_chachapoly_context_t *ctx);
50 
51 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_set_key(cx_chachapoly_context_t *ctx,
52  const uint8_t *key,
53  size_t key_len);
54 
55 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_start(cx_chachapoly_context_t *ctx,
56  uint32_t mode,
57  const uint8_t *iv,
58  size_t iv_len);
59 
60 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_update_aad(cx_chachapoly_context_t *ctx,
61  const uint8_t *aad,
62  size_t aad_len);
63 
64 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_update(cx_chachapoly_context_t *ctx,
65  const uint8_t *input,
66  uint8_t *output,
67  size_t len);
68 
69 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_finish(cx_chachapoly_context_t *ctx,
70  uint8_t *tag,
71  size_t tag_len);
72 
73 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_encrypt_and_tag(cx_chachapoly_context_t *ctx,
74  const uint8_t *input,
75  size_t len,
76  const uint8_t *iv,
77  size_t iv_len,
78  const uint8_t *aad,
79  size_t aad_len,
80  uint8_t *output,
81  uint8_t *tag,
82  size_t tag_len);
83 
84 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_decrypt_and_auth(cx_chachapoly_context_t *ctx,
85  const uint8_t *input,
86  size_t len,
87  const uint8_t *iv,
88  size_t iv_len,
89  const uint8_t *aad,
90  size_t aad_len,
91  uint8_t *output,
92  const uint8_t *tag,
93  size_t tag_len);
94 
95 WARN_UNUSED_RESULT cx_err_t cx_chachapoly_check_tag(cx_chachapoly_context_t *ctx,
96  const uint8_t *tag,
97  size_t tag_len);
98 
99 #endif // HAVE_POLY1305 && HAVE_CHACHA
100 #endif // HAVE_CHACHA_POLY
101 #endif /* LCX_CHACHA_POLY_H */
Chacha cipher.
POLY1305 Message Authentication Code.
unsigned char uint8_t
Definition: usbd_conf.h:53