blob: 5539862ea2fc4e42a82a568d2d0fc4fc44d0e871 [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 *
8 * See BSD-3-Clause license in README.mdE.
9 */
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/**
32 * \brief Return hash algorithm ID from a signature algorithm ID
33 *
34 * \param[in] cose_sig_alg_id A COSE signature algorithm identifier.
35 *
36 * \return \c INT32_MAX when the signature algorithm ID is not known.
37 *
38 * This works off of algorithm identifiers defined in the [IANA COSE
39 * Registry] (https://www.iana.org/assignments/cose/cose.xhtml).
40 * Corresponding local integer constants are defined in
41 * t_cose_defines.h.
42 *
43 * COSE signing algorithms are the combination of public key
44 * algorithm, curve, key size, hash algorithm and hash size. They are
45 * simple integers making them convenient for direct use in code.
46 *
47 * This function returns an identifier for only the hash algorithm
48 * from the combined identifier.
49 *
50 * If the needed algorithm identifiers are not in the IANA registry,
51 * they can be added to it. This will take some time and work. It is
52 * also fine to use algorithms in the proprietary space.
53 */
54int32_t hash_alg_id_from_sig_alg_id(int32_t cose_sig_alg_id);
55
56
57/**
58 * \brief Create the hash of the to-be-signed (TBS) bytes for COSE.
59 *
60 * \param[in] cose_alg_id The COSE signing algorithm ID. Used to
61 * determine which hash function to use.
62 * \param[in] buffer_for_hash Pointer and length of buffer into which
63 * the resulting hash is put.
64 * \param[out] hash Pointer and length of the
65 * resulting hash.
66 * \param[in] protected_headers The CBOR encoded protected headers.
67 * \param[in] payload The CBOR encoded payload
68 *
69 * \return This returns one of the error codes defined by \ref t_cose_err_t.
70 *
71 * \retval T_COSE_ERR_SIG_STRUCT
72 * Most likely this is because the protected_headers passed in
73 * is larger than \ref T_COSE_SIGN1_MAX_PROT_HEADER.
74 * \retval T_COSE_ERR_UNSUPPORTED_HASH
75 * If the hash algorithm is not known.
76 * \retval T_COSE_ERR_HASH_GENERAL_FAIL
77 * In case of some general hash failure.
78 *
79 * The input to the public key signature algorithm in COSE is the hash
80 * of a CBOR encoded structure containing the protected headers
81 * algorithm ID and a few other things. This formats that structure
82 * and computes the hash of it. These are known as the to-be-signed or
83 * "TBS" bytes.
84 */
85enum t_cose_err_t create_tbs_hash(int32_t cose_alg_id,
Laurence Lundbladee1610ad2019-02-20 13:53:20 -080086 struct q_useful_buf buffer_for_hash,
87 struct q_useful_buf_c *hash,
88 struct q_useful_buf_c protected_headers,
89 struct q_useful_buf_c payload);
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -080090
91
92/**
93 * Size of the key returned by get_short_circuit_kid(). It is always
94 * this size.
95 */
96#define T_COSE_SHORT_CIRCUIT_KID_SIZE 32
97
98
99/**
100 * \brief Get the special kid for short-circuit signing.
101 *
102 * \param[in] buffer_for_kid Pointer and length of buffer into which
103 * the resulting hash is put. It should
104 * always be at least \ref
105 * T_COSE_SHORT_CIRCUIT_KID_SIZE.
106 * \param[out] kid Pointer and length of the returned kid.
107 *
108 * \retval T_COSE_SUCCESS
109 * The kid was returned.
110 * \retval T_COSE_ERR_KEY_BUFFER_SIZE
111 * \c buffer_for_kid is too small
112 *
113 * This always returns the same key ID. It always indicates
114 * short-circuit signing. It is OK to hard code this as the
115 * probability of collision with this ID is extremely low and the same
116 * as for collision between any two key IDs (kids) of any sort.
117 *
118 * This is the value of the kid.
119 *
120 * 0xef, 0x95, 0x4b, 0x4b, 0xd9, 0xbd, 0xf6, 0x70,
121 * 0xd0, 0x33, 0x60, 0x82, 0xf5, 0xef, 0x15, 0x2a,
122 * 0xf8, 0xf3, 0x5b, 0x6a, 0x6c, 0x00, 0xef, 0xa6,
123 * 0xa9, 0xa7, 0x1f, 0x49, 0x51, 0x7e, 0x18, 0xc6
124 *
125 */
126enum t_cose_err_t
Laurence Lundbladee1610ad2019-02-20 13:53:20 -0800127get_short_circuit_kid(struct q_useful_buf buffer_for_kid,
128 struct q_useful_buf_c *kid);
129
130#ifdef __cplusplus
131}
132#endif
Laurence Lundbladeaffd65a2018-12-18 10:50:48 -0800133
134#endif /* __T_COSE_UTIL_H__ */