blob: 90be5bda3e981b543e82af9f8cbf5fc37af73354 [file] [log] [blame]
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -08001/*
2 * t_cose_util.h
3 *
4 * Copyright 2019, Laurence Lundblade
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
Tamas Ban32814992019-04-25 14:25:43 +01008 * See BSD-3-Clause license in README.md.
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -08009 */
10
11
12#ifndef __T_COSE_UTIL_H__
13#define __T_COSE_UTIL_H__
14
15#include <stdint.h>
Laurence Lundbladee1610ad2019-02-20 13:53:20 -080016#include "q_useful_buf.h"
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080017#include "t_cose_common.h"
18
Laurence Lundbladee1610ad2019-02-20 13:53:20 -080019#ifdef __cplusplus
20extern "C" {
21#endif
22
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080023/**
24 * \file t_cose_util.h
25 *
26 * \brief Utility functions used internally by the t_cose implementation.
27 *
28 */
29
30
31/**
Laurence Lundblade2ad4e4f2019-04-07 12:02:49 -070032 * 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 */
37enum 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 Lundbladeaffd65a2018-12-18 10:50:48 -080050 * \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 */
72int32_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 Lundblade2ad4e4f2019-04-07 12:02:49 -070085 * \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 Lundbladeaffd65a2018-12-18 10:50:48 -080089 *
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 */
106enum t_cose_err_t create_tbs_hash(int32_t cose_alg_id,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800107 struct q_useful_buf buffer_for_hash,
108 struct q_useful_buf_c *hash,
109 struct q_useful_buf_c protected_headers,
Laurence Lundblade2ad4e4f2019-04-07 12:02:49 -0700110 enum t_cose_tbs_hash_mode_t payload_mode,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800111 struct q_useful_buf_c payload);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800112
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 */
148enum t_cose_err_t
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800149get_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 Lundbladeaffd65a2018-12-18 10:50:48 -0800155
156#endif /* __T_COSE_UTIL_H__ */