Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * t_cose_util.h |
| 3 | * |
| 4 | * Copyright 2019, Laurence Lundblade |
| 5 | * |
| 6 | * SPDX-License-Identifier: BSD-3-Clause |
| 7 | * |
Tamas Ban | 3281499 | 2019-04-25 14:25:43 +0100 | [diff] [blame] | 8 | * See BSD-3-Clause license in README.md. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | #ifndef __T_COSE_UTIL_H__ |
| 13 | #define __T_COSE_UTIL_H__ |
| 14 | |
| 15 | #include <stdint.h> |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 16 | #include "q_useful_buf.h" |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 17 | #include "t_cose_common.h" |
| 18 | |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 23 | /** |
| 24 | * \file t_cose_util.h |
| 25 | * |
| 26 | * \brief Utility functions used internally by the t_cose implementation. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | /** |
Laurence Lundblade | 2ad4e4f | 2019-04-07 12:02:49 -0700 | [diff] [blame] | 32 | * The modes in which the payload is passed to create_tbs_hash(). This |
| 33 | * exists so the TBS bytes can be hashed in two separate chunks and |
| 34 | * avoids needing a second buffer the size of the payload in the |
| 35 | * t_cose implementation. |
| 36 | */ |
| 37 | enum t_cose_tbs_hash_mode_t { |
| 38 | /** The bytes passed for the payload include a wrapping bstr so |
| 39 | * one does not need to be added. |
| 40 | */ |
| 41 | T_COSE_TBS_PAYLOAD_IS_BSTR_WRAPPED, |
| 42 | /** The bytes passed for the payload do NOT have a wrapping bstr |
| 43 | * so one must be added. |
| 44 | */ |
| 45 | T_COSE_TBS_BARE_PAYLOAD |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | /** |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 50 | * \brief Return hash algorithm ID from a signature algorithm ID |
| 51 | * |
| 52 | * \param[in] cose_sig_alg_id A COSE signature algorithm identifier. |
| 53 | * |
| 54 | * \return \c INT32_MAX when the signature algorithm ID is not known. |
| 55 | * |
| 56 | * This works off of algorithm identifiers defined in the [IANA COSE |
| 57 | * Registry] (https://www.iana.org/assignments/cose/cose.xhtml). |
| 58 | * Corresponding local integer constants are defined in |
| 59 | * t_cose_defines.h. |
| 60 | * |
| 61 | * COSE signing algorithms are the combination of public key |
| 62 | * algorithm, curve, key size, hash algorithm and hash size. They are |
| 63 | * simple integers making them convenient for direct use in code. |
| 64 | * |
| 65 | * This function returns an identifier for only the hash algorithm |
| 66 | * from the combined identifier. |
| 67 | * |
| 68 | * If the needed algorithm identifiers are not in the IANA registry, |
| 69 | * they can be added to it. This will take some time and work. It is |
| 70 | * also fine to use algorithms in the proprietary space. |
| 71 | */ |
| 72 | int32_t hash_alg_id_from_sig_alg_id(int32_t cose_sig_alg_id); |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * \brief Create the hash of the to-be-signed (TBS) bytes for COSE. |
| 77 | * |
| 78 | * \param[in] cose_alg_id The COSE signing algorithm ID. Used to |
| 79 | * determine which hash function to use. |
| 80 | * \param[in] buffer_for_hash Pointer and length of buffer into which |
| 81 | * the resulting hash is put. |
| 82 | * \param[out] hash Pointer and length of the |
| 83 | * resulting hash. |
| 84 | * \param[in] protected_headers The CBOR encoded protected headers. |
Laurence Lundblade | 2ad4e4f | 2019-04-07 12:02:49 -0700 | [diff] [blame] | 85 | * \param[in] payload_mode See \ref t_cose_tbs_hash_mode_t. |
| 86 | * \param[in] payload The CBOR encoded payload. It may or may |
| 87 | * not have a wrapping bstr per |
| 88 | * \c payload_mode. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 89 | * |
| 90 | * \return This returns one of the error codes defined by \ref t_cose_err_t. |
| 91 | * |
| 92 | * \retval T_COSE_ERR_SIG_STRUCT |
| 93 | * Most likely this is because the protected_headers passed in |
| 94 | * is larger than \ref T_COSE_SIGN1_MAX_PROT_HEADER. |
| 95 | * \retval T_COSE_ERR_UNSUPPORTED_HASH |
| 96 | * If the hash algorithm is not known. |
| 97 | * \retval T_COSE_ERR_HASH_GENERAL_FAIL |
| 98 | * In case of some general hash failure. |
| 99 | * |
| 100 | * The input to the public key signature algorithm in COSE is the hash |
| 101 | * of a CBOR encoded structure containing the protected headers |
| 102 | * algorithm ID and a few other things. This formats that structure |
| 103 | * and computes the hash of it. These are known as the to-be-signed or |
| 104 | * "TBS" bytes. |
| 105 | */ |
| 106 | enum t_cose_err_t create_tbs_hash(int32_t cose_alg_id, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 107 | struct q_useful_buf buffer_for_hash, |
| 108 | struct q_useful_buf_c *hash, |
| 109 | struct q_useful_buf_c protected_headers, |
Laurence Lundblade | 2ad4e4f | 2019-04-07 12:02:49 -0700 | [diff] [blame] | 110 | enum t_cose_tbs_hash_mode_t payload_mode, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 111 | struct q_useful_buf_c payload); |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 112 | |
| 113 | |
| 114 | /** |
| 115 | * Size of the key returned by get_short_circuit_kid(). It is always |
| 116 | * this size. |
| 117 | */ |
| 118 | #define T_COSE_SHORT_CIRCUIT_KID_SIZE 32 |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * \brief Get the special kid for short-circuit signing. |
| 123 | * |
| 124 | * \param[in] buffer_for_kid Pointer and length of buffer into which |
| 125 | * the resulting hash is put. It should |
| 126 | * always be at least \ref |
| 127 | * T_COSE_SHORT_CIRCUIT_KID_SIZE. |
| 128 | * \param[out] kid Pointer and length of the returned kid. |
| 129 | * |
| 130 | * \retval T_COSE_SUCCESS |
| 131 | * The kid was returned. |
| 132 | * \retval T_COSE_ERR_KEY_BUFFER_SIZE |
| 133 | * \c buffer_for_kid is too small |
| 134 | * |
| 135 | * This always returns the same key ID. It always indicates |
| 136 | * short-circuit signing. It is OK to hard code this as the |
| 137 | * probability of collision with this ID is extremely low and the same |
| 138 | * as for collision between any two key IDs (kids) of any sort. |
| 139 | * |
| 140 | * This is the value of the kid. |
| 141 | * |
| 142 | * 0xef, 0x95, 0x4b, 0x4b, 0xd9, 0xbd, 0xf6, 0x70, |
| 143 | * 0xd0, 0x33, 0x60, 0x82, 0xf5, 0xef, 0x15, 0x2a, |
| 144 | * 0xf8, 0xf3, 0x5b, 0x6a, 0x6c, 0x00, 0xef, 0xa6, |
| 145 | * 0xa9, 0xa7, 0x1f, 0x49, 0x51, 0x7e, 0x18, 0xc6 |
| 146 | * |
| 147 | */ |
| 148 | enum t_cose_err_t |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 149 | get_short_circuit_kid(struct q_useful_buf buffer_for_kid, |
| 150 | struct q_useful_buf_c *kid); |
| 151 | |
| 152 | #ifdef __cplusplus |
| 153 | } |
| 154 | #endif |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 155 | |
| 156 | #endif /* __T_COSE_UTIL_H__ */ |