1 /*
2  * Copyright (c) 2023, The TrustedFirmware-M Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef CC3XX_POLY1305_H
9 #define CC3XX_POLY1305_H
10 
11 #include "cc3xx_error.h"
12 #include "cc3xx_pka.h"
13 
14 #include <stdint.h>
15 #include <stddef.h>
16 
17 #define POLY1305_TAG_LEN 16
18 #define POLY1305_BLOCK_SIZE 16
19 #define POLY1305_KEY_SIZE 16
20 #define POLY1305_PKA_REG_SIZE 20
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 struct cc3xx_poly1305_state_t {
27     struct cc3xx_pka_state_t pka_state;
28 
29     cc3xx_pka_reg_id_t modulus_reg;
30     cc3xx_pka_reg_id_t barrett_tag_reg;
31     cc3xx_pka_reg_id_t key_r_reg;
32     cc3xx_pka_reg_id_t key_s_reg;
33     cc3xx_pka_reg_id_t accumulator_reg;
34     cc3xx_pka_reg_id_t data_input_reg;
35     cc3xx_pka_reg_id_t mask_reg;
36 
37     uint32_t key_r[POLY1305_PKA_REG_SIZE / sizeof(uint32_t)];
38     uint32_t key_s[POLY1305_PKA_REG_SIZE / sizeof(uint32_t)];
39     uint32_t accumulator[POLY1305_PKA_REG_SIZE / sizeof(uint32_t)];
40 
41     uint32_t block_buf[POLY1305_BLOCK_SIZE / sizeof(uint32_t)];
42     size_t block_buf_size_in_use;
43 };
44 
45 /**
46  * @brief                       Initialize the poly1305 operation.
47  *
48  * @param[in]  poly_key_r       This is the "r" portion of the poly1035 key (the
49  *                              first half of the zero block). Must be
50  *                              ``POLY1305_KEY_SIZE`` bytes in length.
51  *
52  * @param[in]  poly_key_s       This is the "s" portion of the poly1035 key (the
53  *                              second half of the zero block). Must be
54  *                              ``POLY1305_KEY_SIZE`` bytes in length.
55  *
56  */
57 void cc3xx_lowlevel_poly1305_init(uint32_t *poly_key_r, uint32_t *poly_key_s);
58 
59 /**
60  * @brief                        Input data into the poly1305 operation.
61 
62  * @param[in]  buf               A pointer to the data to be input.
63  * @param[in]  length            The size of the data to be input.
64  */
65 void cc3xx_lowlevel_poly1305_update(const uint8_t *buf, size_t length);
66 
67 /**
68  * @brief                        Get the current state of the poly1305 operation.
69  *                               Allows for restartable poly1305 operations.
70 
71  * @param[out] state            The cc3xx_pooly1035_state_t to write the state
72  *                              into.
73  */
74 void cc3xx_lowlevel_poly1305_get_state(struct cc3xx_poly1305_state_t *state);
75 
76 /**
77  * @brief                        Set the current state of the poly1305 operation.
78  *                               Allows for restartable poly1305 operations.
79  *
80  * @note                         This funtion initializes the operation, there
81  *                               is no need to seperately call
82  *                               cc3xx_poly1305_init.
83 
84  * @param[in]  state            The cc3xx_poly1305_state_t to read the state
85  *                              from.
86  */
87 void cc3xx_lowlevel_poly1305_set_state(const struct cc3xx_poly1305_state_t *state);
88 
89 /**
90  * @brief                        Finish a poly1305 operation, and write the tag
91  *                               into the buffer.
92  *
93  * @param[out]  tag              The buffer to write the tag into.
94  */
95 void cc3xx_lowlevel_poly1305_finish(uint32_t *tag);
96 
97 /**
98  * @brief                       Uninitialize the poly1305 operation.
99  */
100 void cc3xx_lowlevel_poly1305_uninit(void);
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif /* CC3XX_POLY1305_H */
107