1 /*
2  * Copyright (c) 2024, The TrustedFirmware-M Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __CC3XX_ECDSA_H__
9 #define __CC3XX_ECDSA_H__
10 
11 #include <stdint.h>
12 #include <stddef.h>
13 
14 #include "cc3xx_error.h"
15 #include "cc3xx_ec.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #define CC3XX_ECDSA_MAX_PRIVATE_KEY_SIZE      CC3XX_EC_MAX_POINT_SIZE
22 #define CC3XX_ECDSA_MAX_PUBLIC_COORD_KEY_SIZE CC3XX_EC_MAX_POINT_SIZE
23 
24 /**
25  * @brief                        Generate an ECDSA private key
26  *
27  * @param[in]  curve_id          The ID of the curve to use.
28  * @param[out] private_key       The buffer to write the private key into.
29  * @param[in]  private_key_len   The size of the private key buffer.
30  * @param[out] private_key_size  The size of the private key written into the
31  *                               buffer.
32  *
33  * @return                       CC3XX_ERR_SUCCESS on success, another
34  *                               cc3xx_err_t on error.
35  */
36 cc3xx_err_t cc3xx_lowlevel_ecdsa_genkey(cc3xx_ec_curve_id_t curve_id,
37                                         uint32_t *private_key,
38                                         size_t private_key_len,
39                                         size_t *private_key_size);
40 
41 /**
42  * @brief                        Calculate an ECDSA public key from a private key
43  *
44  * @param[in]  curve_id          The ID of the curve that the private key
45  *                               belongs to.
46  * @param[in]  private_key       The buffer to load the private key from.
47  * @param[in]  private_key_len   The size of the private key buffer.
48  * @param[out] public_key_x      The buffer to write the public key x coord into.
49  * @param[in]  public_key_x_len  The size of the public key x coord buffer.
50  * @param[out] public_key_x_size The size of the public key x coord written into
51  *                               the buffer.
52  * @param[out] public_key_y      The buffer to write the public key y coord into.
53  * @param[in]  public_key_y_len  The size of the public key y coord buffer.
54  * @param[out] public_key_y_size The size of the public key y coord written into
55  *                               the buffer.
56  *
57  * @return                       CC3XX_ERR_SUCCESS on success, another
58  *                               cc3xx_err_t on error.
59  */
60 cc3xx_err_t cc3xx_lowlevel_ecdsa_getpub(cc3xx_ec_curve_id_t curve_id,
61                                         const uint32_t *private_key, size_t private_key_len,
62                                         uint32_t *public_key_x, size_t public_key_x_len,
63                                         size_t *public_key_x_size,
64                                         uint32_t *public_key_y, size_t public_key_y_len,
65                                         size_t *public_key_y_size);
66 
67 /**
68  * @brief                        Generate an ECDSA signature
69  *
70  * @param[in]  curve_id          The ID of the curve that the private key
71  *                               belongs to.
72  * @param[in]  private_key       The buffer to load the private key from.
73  * @param[in]  private_key_len   The size of the private key buffer.
74  * @param[in]  hash              The buffer to read the hash from.
75  * @param[in]  hash_len          The size of the hash buffer.
76  * @param[out] sig_r             The buffer to read the signature r param from.
77  * @param[in]  sig_r_len         The size of the signature r param buffer.
78  * @param[out] sig_r_size        The size of the signature r param written into
79  *                               the buffer.
80  * @param[out] sig_s             The buffer to read the signature s param from.
81  * @param[in]  sig_s_len         The size of the signature s param buffer.
82  * @param[out] sig_s_size        The size of the signature s param written into
83  *                               the buffer.
84  *
85  * @return                       CC3XX_ERR_SUCCESS on success, another
86  *                               cc3xx_err_t on error.
87  */
88 cc3xx_err_t cc3xx_lowlevel_ecdsa_sign(cc3xx_ec_curve_id_t curve_id,
89                                       const uint32_t *private_key, size_t private_key_len,
90                                       const uint32_t *hash, size_t hash_len,
91                                       uint32_t *sig_r, size_t sig_r_len, size_t *sig_r_size,
92                                       uint32_t *sig_s, size_t sig_s_len, size_t *sig_s_size);
93 /**
94  * @brief                        Verify an ECDSA signature
95  *
96  * @param[in]  curve_id          The ID of the curve that the signature was
97  *                               generated using.
98  * @param[in]  public_key_x      The buffer to read the public key x coord from.
99  * @param[in]  public_key_x_len  The size of the public key x coord buffer.
100  * @param[in]  public_key_y      The buffer to read the public key y coord from.
101  * @param[in]  public_key_y_len  The size of the public key y coord buffer.
102  * @param[in]  hash              The buffer to read the hash from.
103  * @param[in]  hash_len          The size of the hash buffer.
104  * @param[in]  sig_r             The buffer to read the signature r param from.
105  * @param[in]  sig_r_len         The size of the signature r param buffer.
106  * @param[in]  sig_s             The buffer to read the signature s param from.
107  * @param[in]  sig_s_len         The size of the signature s param buffer.
108  *
109  * @return                       CC3XX_ERR_SUCCESS on signature validation
110  *                               success, another cc3xx_err_t on error.
111  */
112 cc3xx_err_t cc3xx_lowlevel_ecdsa_verify(cc3xx_ec_curve_id_t curve_id,
113                                         const uint32_t *public_key_x,
114                                         size_t public_key_x_len,
115                                         const uint32_t *public_key_y,
116                                         size_t public_key_y_len,
117                                         const uint32_t *hash, size_t hash_len,
118                                         const uint32_t *sig_r, size_t sig_r_len,
119                                         const uint32_t *sig_s, size_t sig_s_len);
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 #endif /* __CC3XX_ECDSA_H__ */
126