Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * t_cose_crypto.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_CRYPTO_H__ |
| 13 | #define __T_COSE_CRYPTO_H__ |
| 14 | |
| 15 | #include "t_cose_common.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 <stdint.h> |
| 18 | #include "t_cose_defines.h" |
| 19 | |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * \file t_cose_crypto.h |
| 26 | * |
| 27 | * \brief This is the adaptation layer for cryptographic functions used by |
| 28 | * t_cose. |
| 29 | * |
| 30 | * This is small wrapper around the cryptographic functions to: |
| 31 | * - Map COSE algorithm IDs to TF-M algorithm IDs |
| 32 | * - Map crypto errors to \ref t_cose_err_t errors |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 33 | * - Have inputs and outputs be \c struct \c q_useful_buf_c and |
| 34 | * \c struct \c q_useful_buf |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 35 | * - Handle key selection |
| 36 | * |
| 37 | * The idea is that implementations can be made of these functions |
| 38 | * that adapt to various cryptographic libraries that are used on |
| 39 | * various platforms and OSs. |
| 40 | * |
| 41 | * This runs entirely off of COSE-style algorithm identifiers. They |
| 42 | * are simple integers and thus work nice as function parameters. An |
| 43 | * initial set is defined by [COSE (RFC 8152)] |
| 44 | * (https://tools.ietf.org/html/rfc8152). New ones can be registered |
| 45 | * in the [IANA COSE Registry] |
| 46 | * (https://www.iana.org/assignments/cose/cose.xhtml). Local use new |
| 47 | * ones can also be defined (\c \#define) if what is needed is not in |
| 48 | * the IANA registry. |
| 49 | * |
| 50 | * Binary data is returned to the caller using a \c struct \c |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 51 | * q_useful_buf to pass the buffer to receive the data and its length in |
| 52 | * and a \c q_useful_buf_c to return the pointer and length of the |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 53 | * returned data. The point of this is coding hygiene. The buffer |
| 54 | * passed in is not const as it is to be modified. The \c |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 55 | * q_useful_buf_c returned is const. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 56 | * |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 57 | * The pointer in the \c q_useful_buf_c will always point to the buffer |
| 58 | * passed in via the \c q_useful_buf so the lifetime of the data is |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 59 | * under control of the caller. |
| 60 | * |
| 61 | * This is not intended as any sort of general cryptographic API. It |
| 62 | * is just the functions needed by t_cose in the form that is most |
| 63 | * useful for t_cose. |
| 64 | */ |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Size of the signature output for the NIST P-256 Curve. |
| 69 | */ |
| 70 | #define T_COSE_EC_P256_SIG_SIZE 64 |
| 71 | |
| 72 | /** |
| 73 | * Size of the largest signature of any of the algorithm types |
| 74 | * supported. |
| 75 | * |
| 76 | * This will have to be adjusted if support for other algorithms |
| 77 | * larger is added. |
| 78 | * |
| 79 | * This is a compile time constant so it can be used to define stack |
| 80 | * variable sizes. |
| 81 | */ |
| 82 | #define T_COSE_MAX_EC_SIG_SIZE T_COSE_EC_P256_SIG_SIZE |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * \brief Get the size in bytes of a particular signature type. |
| 87 | * |
| 88 | * \param[in] cose_sig_alg_id The COSE algorithm ID. |
| 89 | * |
| 90 | * \return The size in bytes of the signature for a public-key signing |
| 91 | * algorithm. |
| 92 | */ |
| 93 | static inline size_t t_cose_signature_size(int32_t cose_sig_alg_id); |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * \brief Perform public key signing. Part of the t_cose crypto |
| 98 | * adaptation layer. |
| 99 | * |
| 100 | * \param[in] cose_alg_id The algorithm to sign with. The IDs are |
| 101 | * defined in [COSE (RFC 8152)] |
| 102 | * (https://tools.ietf.org/html/rfc8152) or |
| 103 | * in the [IANA COSE Registry] |
| 104 | * (https://www.iana.org/assignments/cose/cose.xhtml). |
| 105 | * A proprietary ID can also be defined |
| 106 | * locally (\c \#define) if the needed |
| 107 | * one hasn't been registered. |
| 108 | * \param[in] key_select Indicates which key to use to sign. |
| 109 | * \param[in] hash_to_sign The bytes to sign. Typically, a hash of |
| 110 | * a payload. |
| 111 | * \param[in] signature_buffer Pointer and length of buffer into which |
| 112 | * the resulting signature is put. |
| 113 | * \param[in] signature Pointer and length of the signature |
| 114 | * returned. |
| 115 | * |
| 116 | * \retval T_COSE_SUCCESS |
| 117 | * Successfully created the signature. |
| 118 | * \retval T_COSE_ERR_SIG_BUFFER_SIZE |
| 119 | * The \c signature_buffer too small. |
| 120 | * \retval T_COSE_ERR_UNSUPPORTED_SIGNING_ALG |
| 121 | * The requested signing algorithm, \c cose_alg_id, is not |
| 122 | * supported. |
| 123 | * \retval T_COSE_ERR_UNKNOWN_KEY |
| 124 | * The key identified by \c key_select was not found. |
| 125 | * \retval T_COSE_ERR_WRONG_TYPE_OF_KEY |
| 126 | * The key was found, but it was the wrong type. |
| 127 | * \retval T_COSE_ERR_INVALID_ARGUMENT |
| 128 | * Some (unspecified) argument was not valid. |
| 129 | * \retval T_COSE_ERR_INSUFFICIENT_MEMORY |
| 130 | * Insufficient heap memory. |
| 131 | * \retval T_COSE_ERR_FAIL |
| 132 | * General unspecific failure. |
| 133 | * \retval T_COSE_ERR_TAMPERING_DETECTED |
| 134 | * Equivalent to \c PSA_ERROR_TAMPERING_DETECTED. |
| 135 | * |
| 136 | * This is called to do public key signing. The implementation will |
| 137 | * vary from one platform / OS to another but should conform to the |
| 138 | * description here. |
| 139 | * |
| 140 | * The key selection depends on the platform / OS. |
| 141 | * |
| 142 | * See the note in the Detailed Description (the \\file comment block) |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 143 | * for details on how \c q_useful_buf and \c q_useful_buf_c are used to |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 144 | * return the signature. |
| 145 | * |
| 146 | * To find out the size of the signature buffer needed, call this with |
| 147 | * \c signature_buffer->ptr \c NULL and \c signature_buffer->len a |
| 148 | * very large number like \c UINT32_MAX. The size will be returned in |
| 149 | * \c signature->len. |
| 150 | */ |
| 151 | enum t_cose_err_t |
| 152 | t_cose_crypto_pub_key_sign(int32_t cose_alg_id, |
| 153 | int32_t key_select, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 154 | struct q_useful_buf_c hash_to_sign, |
| 155 | struct q_useful_buf signature_buffer, |
| 156 | struct q_useful_buf_c *signature); |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 157 | |
| 158 | |
| 159 | /** |
| 160 | * \brief perform public key signature verification. Part of the |
| 161 | * t_cose crypto adaptation layer. |
| 162 | * |
| 163 | * \param[in] cose_alg_id The algorithm to use for verification. |
| 164 | * The IDs are defined in [COSE (RFC 8152)] |
| 165 | * (https://tools.ietf.org/html/rfc8152) |
| 166 | * or in the [IANA COSE Registry] |
| 167 | * (https://www.iana.org/assignments/cose/cose.xhtml). |
| 168 | * A proprietary ID can also be defined |
| 169 | * locally (\c \#define) if the needed one |
| 170 | * hasn't been registered. |
| 171 | * \param[in] key_select Verification key selection. |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 172 | * \param[in] key_id A key id or \c NULL_Q_USEFUL_BUF_C. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 173 | * \param[in] hash_to_verify The data or hash that is to be verified. |
| 174 | * \param[in] signature The signature. |
| 175 | * |
| 176 | * This verifies that the \c signature passed in was over the \c |
| 177 | * hash_to_verify passed in. |
| 178 | * |
| 179 | * The public key used to verify the signature is selected by the \c |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 180 | * key_id if it is not \c NULL_Q_USEFUL_BUF_C or the \c key_select if it |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 181 | * is. |
| 182 | * |
| 183 | * The key selected must be, or include, a public key of the correct |
| 184 | * type for \c cose_alg_id. |
| 185 | * |
| 186 | * \retval T_COSE_SUCCESS |
| 187 | * The signature is valid |
| 188 | * \retval T_COSE_ERR_SIG_VERIFY |
| 189 | * Signature verification failed. For example, the |
| 190 | * cryptographic operations completed successfully but hash |
| 191 | * wasn't as expected. |
| 192 | * \retval T_COSE_ERR_UNKNOWN_KEY |
| 193 | * The key identified by \c key_select or a \c kid was |
| 194 | * not found. |
| 195 | * \retval T_COSE_ERR_WRONG_TYPE_OF_KEY |
| 196 | * The key was found, but it was the wrong type |
| 197 | * for the operation. |
| 198 | * \retval T_COSE_ERR_UNSUPPORTED_SIGNING_ALG |
| 199 | * The requested signing algorithm is not supported. |
| 200 | * \retval T_COSE_ERR_INVALID_ARGUMENT |
| 201 | * Some (unspecified) argument was not valid. |
| 202 | * \retval T_COSE_ERR_INSUFFICIENT_MEMORY |
| 203 | * Out of heap memory. |
| 204 | * \retval T_COSE_ERR_FAIL |
| 205 | * General unspecific failure. |
| 206 | * \retval T_COSE_ERR_TAMPERING_DETECTED |
| 207 | * Equivalent to \c PSA_ERROR_TAMPERING_DETECTED. |
| 208 | */ |
| 209 | enum t_cose_err_t |
| 210 | t_cose_crypto_pub_key_verify(int32_t cose_alg_id, |
| 211 | int32_t key_select, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 212 | struct q_useful_buf_c key_id, |
| 213 | struct q_useful_buf_c hash_to_verify, |
| 214 | struct q_useful_buf_c signature); |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 215 | |
| 216 | |
| 217 | /** |
| 218 | * The size of X and Y coordinate in 2 parameter style EC public |
| 219 | * key. Format is as defined in [COSE (RFC 8152)] |
| 220 | * (https://tools.ietf.org/html/rfc8152) and [SEC 1: Elliptic Curve |
| 221 | * Cryptography](http://www.secg.org/sec1-v2.pdf). |
| 222 | * |
| 223 | * This size is well-known and documented in public standards. |
| 224 | */ |
| 225 | #define T_COSE_CRYPTO_EC_P256_COORD_SIZE 32 |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * \brief Get an elliptic curve public key. Part of the t_cose crypto |
| 230 | * adaptation layer. |
| 231 | * |
| 232 | * \param[in] key_select Used to look up the public |
| 233 | * key to return when \c kid is |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 234 | * \c NULL_Q_USEFUL_BUF_C. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 235 | * \param[in] kid A key ID to look up against. May be |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 236 | * \c NULL_Q_USEFUL_BUF_C. This is typically |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 237 | * the kid from the COSE unprotected header. |
| 238 | * \param[out] cose_curve_id The curve ID of the key returned as |
| 239 | * defined by [COSE (RFC 8152)] |
| 240 | * (https://tools.ietf.org/html/rfc8152) |
| 241 | * or the IANA COSE registry. |
| 242 | * \param[in] buf_to_hold_x_coord Pointer and length into which the |
| 243 | * X coordinate is put. |
| 244 | * \param[in] buf_to_hold_y_coord Pointer and length into which the |
| 245 | * Y coordinate is put. |
| 246 | * \param[out] x_coord Pointer and length of the returned X |
| 247 | * coordinate. |
| 248 | * \param[out] y_coord Pointer and length of the returned Y |
| 249 | * coordinate. |
| 250 | * |
| 251 | * \retval T_COSE_SUCCESS |
| 252 | * The key was found and is returned. |
| 253 | * \retval T_COSE_ERR_UNKNOWN_KEY |
| 254 | * The key identified by \c key_select or a \c kid was not |
| 255 | * found. |
| 256 | * \retval T_COSE_ERR_WRONG_TYPE_OF_KEY |
| 257 | * The key was found, but it was the wrong type for the |
| 258 | * operation. |
| 259 | * \retval T_COSE_ERR_FAIL |
| 260 | * General unspecific failure. |
| 261 | * \retval T_COSE_ERR_KEY_BUFFER_SIZE |
| 262 | * Buffer to hold the output was too small. |
| 263 | * |
| 264 | * This finds and returns a public key. Where it looks for the key is |
| 265 | * dependent on the OS / platform. |
| 266 | * |
| 267 | * \ref T_COSE_CRYPTO_EC_P256_COORD_SIZE is the size of the X or Y |
| 268 | * coordinate for the NIST P-256 curve. |
| 269 | * |
| 270 | * See the note in the Detailed Description (the \\file comment block) |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 271 | * for details on how \c q_useful_buf and \c q_useful_buf_c are used to |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 272 | * return the X and Y coordinates. |
| 273 | */ |
| 274 | enum t_cose_err_t |
| 275 | t_cose_crypto_get_ec_pub_key(int32_t key_select, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 276 | struct q_useful_buf_c kid, |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 277 | int32_t *cose_curve_id, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 278 | struct q_useful_buf buf_to_hold_x_coord, |
| 279 | struct q_useful_buf buf_to_hold_y_coord, |
| 280 | struct q_useful_buf_c *x_coord, |
| 281 | struct q_useful_buf_c *y_coord); |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 282 | |
| 283 | |
| 284 | /* |
| 285 | * No function to get private key because there is no need for it. |
| 286 | * The private signing key only needs to exist behind |
| 287 | * t_cose_crypto_pub_key_sign(). |
| 288 | */ |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
Laurence Lundblade | e8d4e6d | 2019-03-18 17:11:11 -0700 | [diff] [blame] | 293 | #ifdef T_COSE_USE_B_CON_SHA256 |
| 294 | /* This is code for use with Brad Conte's crypto. See |
| 295 | * https://github.com/B-Con/crypto-algorithms and see the description |
| 296 | * of t_cose_crypto_hash |
| 297 | */ |
| 298 | #include "sha256.h" |
| 299 | #endif |
| 300 | |
| 301 | |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 302 | /** |
| 303 | * The context for use with the hash adaptation layer here. |
Laurence Lundblade | e8d4e6d | 2019-03-18 17:11:11 -0700 | [diff] [blame] | 304 | * |
| 305 | * Hash implementations for this porting layer are put into two |
| 306 | * different categories. |
| 307 | * |
| 308 | * The first can be supported generically without any dependency on |
| 309 | * the actual hash implementation in this header. These only need a |
| 310 | * pointer or handle for the hash context. Usually these are |
| 311 | * implemented by a service, system API or crypto HW that runs in a |
| 312 | * separate context or process. They probably allocate memory |
| 313 | * internally. These can use context.ptr or context.handle to hold the |
| 314 | * pointer or handle to the hash context. |
| 315 | * |
| 316 | * The second sort of hash implementations need more than just a |
| 317 | * pointer or handle. Typically these are libraries that are linked |
| 318 | * with this code and run in the same process / context / thread as |
| 319 | * this code. These can be efficient requiring no context switches or |
| 320 | * memory allocations. These type require this header be modified for |
| 321 | * the #include which defines the hash context and so this struct |
| 322 | * includes that context as a member. This context is allocated on the |
| 323 | * stack, so any members added here should be small enough to go on |
| 324 | * the stack. USE_B_CON_SHA256 is an example of this type. |
| 325 | * |
| 326 | * The actual implementation of the hash is in a separate .c file |
| 327 | * that will be specific to the particular platform, library, |
| 328 | * service or such used. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 329 | */ |
| 330 | struct t_cose_crypto_hash { |
Laurence Lundblade | e8d4e6d | 2019-03-18 17:11:11 -0700 | [diff] [blame] | 331 | |
| 332 | #ifdef T_COSE_USE_B_CON_SHA256 |
| 333 | /* Specific context for Brad Conte's sha256.c */ |
| 334 | SHA256_CTX context; |
| 335 | #else |
| 336 | /* |
| 337 | * Generic pointer / handle that can work for many |
| 338 | * hash implementations. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 339 | */ |
Laurence Lundblade | e8d4e6d | 2019-03-18 17:11:11 -0700 | [diff] [blame] | 340 | union { |
| 341 | void *ptr; |
| 342 | uint64_t handle; |
| 343 | } context; |
| 344 | int64_t status; |
| 345 | #endif |
| 346 | |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | |
| 350 | /** |
| 351 | * The size of the output of SHA-256 in bytes. |
| 352 | * |
| 353 | * (It is safe to define this independently here as its size is |
| 354 | * well-known and fixed. There is no need to reference |
| 355 | * platform-specific headers and incur messy dependence.) |
| 356 | */ |
| 357 | #define T_COSE_CRYPTO_SHA256_SIZE 32 |
| 358 | |
| 359 | |
| 360 | /** |
| 361 | * \brief Start cryptographic hash. Part of the t_cose crypto |
| 362 | * adaptation layer. |
| 363 | * |
| 364 | * \param[in,out] hash_ctx Pointer to the hash context that |
| 365 | * will be initialized. |
| 366 | * \param[in] cose_hash_alg_id Algorithm ID that identifies the |
| 367 | * hash to use. This is from the |
| 368 | * [IANA COSE Registry] |
| 369 | * (https://www.iana.org/assignments/cose/cose.xhtml). |
| 370 | * As of the creation of this interface |
| 371 | * no identifiers of only a hash |
| 372 | * functions have been registered. |
| 373 | * Signature algorithms that include |
| 374 | * specification of the hash have been |
| 375 | * registered, but they are not to be |
| 376 | * used here. Until hash functions only |
| 377 | * have been officially registered, some |
| 378 | * IDs are defined in the proprietary |
| 379 | * space in t_cose_common.h. |
| 380 | * |
| 381 | * \retval T_COSE_ERR_UNSUPPORTED_HASH |
| 382 | * The requested algorithm is unknown or unsupported. |
| 383 | * |
| 384 | * \retval T_COSE_ERR_HASH_GENERAL_FAIL |
| 385 | * Some general failure of the hash function |
| 386 | * |
| 387 | * This initializes the hash context for the particular algorithm. It |
| 388 | * must be called first. A \c hash_ctx can be reused if it is |
| 389 | * reinitialized. |
| 390 | */ |
| 391 | enum t_cose_err_t |
| 392 | t_cose_crypto_hash_start(struct t_cose_crypto_hash *hash_ctx, |
| 393 | int32_t cose_hash_alg_id); |
| 394 | |
| 395 | |
| 396 | /** |
| 397 | * \brief Feed data into a cryptographic hash. Part of the t_cose |
| 398 | * crypto adaptation layer. |
| 399 | * |
| 400 | * \param[in,out] hash_ctx Pointer to the hash context in which |
| 401 | * accumulate the hash. |
| 402 | * \param[in] data_to_hash Pointer and length of data to feed into |
| 403 | * hash. The pointer may by \c NULL in which |
| 404 | * case no hashing is performed. |
| 405 | * |
| 406 | * There is no return value. If an error occurs it is remembered in \c |
| 407 | * hash_ctx and returned when t_cose_crypto_hash_finish() is called. |
| 408 | * Once in the error state, this function may be called, but it will |
| 409 | * not do anything. |
Laurence Lundblade | e8d4e6d | 2019-03-18 17:11:11 -0700 | [diff] [blame] | 410 | * |
| 411 | * This function can be called with \c data_to_hash.ptr NULL and it |
| 412 | * will pretend to hash. This allows the same code that is used to |
| 413 | * produce the real hash to be used to return a length of the would be |
| 414 | * hash for encoded data structure size calculations. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 415 | */ |
| 416 | void t_cose_crypto_hash_update(struct t_cose_crypto_hash *hash_ctx, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 417 | struct q_useful_buf_c data_to_hash); |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 418 | |
| 419 | |
| 420 | /** |
| 421 | * \brief Finish a cryptographic hash. Part of the t_cose crypto |
| 422 | * adaptation layer. |
| 423 | * |
| 424 | * \param[in,out] hash_ctx Pointer to the hash context. |
| 425 | * \param[in] buffer_to_hold_result Pointer and length into which |
| 426 | * the resulting hash is put. |
| 427 | * \param[out] hash_result Pointer and length of the |
| 428 | * resulting hash. |
| 429 | * |
| 430 | * \retval T_COSE_ERR_HASH_GENERAL_FAIL |
| 431 | * Some general failure of the hash function. |
| 432 | * \retval T_COSE_ERR_HASH_BUFFER_SIZE |
| 433 | * The size of the buffer to hold the hash result was |
| 434 | * too small. |
| 435 | * |
| 436 | * Call this to complete the hashing operation. If the everything |
| 437 | * completed correctly, the resulting hash is returned. Note that any |
| 438 | * errors that occurred during t_cose_crypto_hash_update() are |
| 439 | * returned here. |
| 440 | * |
| 441 | * See the note in the Detailed Description (the \\file comment block) |
Laurence Lundblade | e8d4e6d | 2019-03-18 17:11:11 -0700 | [diff] [blame] | 442 | * for details on how \c q_useful_buf and \c q_useful_buf_c are used |
| 443 | * to return the hash. |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 444 | */ |
| 445 | enum t_cose_err_t |
| 446 | t_cose_crypto_hash_finish(struct t_cose_crypto_hash *hash_ctx, |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 447 | struct q_useful_buf buffer_to_hold_result, |
| 448 | struct q_useful_buf_c *hash_result); |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 449 | |
| 450 | |
| 451 | |
| 452 | /* |
| 453 | * Public inline function. See documentation above. |
| 454 | */ |
| 455 | static inline size_t t_cose_signature_size(int32_t cose_sig_alg_id) |
| 456 | { |
| 457 | switch(cose_sig_alg_id) { |
| 458 | case COSE_ALGORITHM_ES256: |
| 459 | return T_COSE_EC_P256_SIG_SIZE; |
| 460 | default: |
| 461 | return T_COSE_EC_P256_SIG_SIZE; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | |
Laurence Lundblade | e1610ad | 2019-02-20 13:53:20 -0800 | [diff] [blame] | 466 | #ifdef __cplusplus |
| 467 | } |
| 468 | #endif |
| 469 | |
Laurence Lundblade | affd65a | 2018-12-18 10:50:48 -0800 | [diff] [blame] | 470 | #endif /* __T_COSE_CRYPTO_H__ */ |