Contiki-NG
Loading...
Searching...
No Matches
cose-error.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/**
31 * \file
32 * COSE library error definitions
33 *
34 * This header defines error codes for COSE (CBOR Object Signing and
35 * Encryption) operations as specified in RFC 8152.
36 *
37 * \author
38 * Niclas Finne <niclas.finne@ri.se>, Nicolas Tsiftes <nicolas.tsiftes@ri.se>
39 */
40
41#ifndef COSE_ERROR_H_
42#define COSE_ERROR_H_
43
44#include <stdint.h>
45#include <stdbool.h>
46
47/**
48 * \brief COSE operation error codes
49 *
50 * Error codes for COSE operations use negative values to distinguish
51 * them from success (0) and positive return values (e.g., sizes).
52 */
53typedef enum {
54 /* Success */
55 COSE_SUCCESS = 0,
56
57 /* Parameter and input errors (-1 to -9) */
58 COSE_ERR_INVALID_PARAMETER = -1, /**< Invalid parameter provided */
59 COSE_ERR_NULL_POINTER = -2, /**< Null pointer provided */
60 COSE_ERR_BUFFER_TOO_SMALL = -3, /**< Buffer too small for operation */
61 COSE_ERR_INVALID_LENGTH = -4, /**< Invalid length parameter */
62 COSE_ERR_UNSUPPORTED_ALGORITHM = -5, /**< Algorithm not supported */
63
64 /* Memory and buffer errors (-10 to -19) */
65 COSE_ERR_MEMORY_ALLOCATION = -10, /**< Memory allocation failed */
66 COSE_ERR_BUFFER_OVERFLOW = -11, /**< Buffer overflow detected */
67
68 /* Cryptographic operation errors (-20 to -29) */
69 COSE_ERR_CRYPTO_KEYGEN = -20, /**< Key generation failed */
70 COSE_ERR_CRYPTO_SIGN = -21, /**< Digital signature failed */
71 COSE_ERR_CRYPTO_VERIFY = -22, /**< Signature verification failed */
72 COSE_ERR_CRYPTO_ENCRYPT = -23, /**< Encryption operation failed */
73 COSE_ERR_CRYPTO_DECRYPT = -24, /**< Decryption operation failed */
74 COSE_ERR_CRYPTO_AUTHENTICATION = -25, /**< Authentication tag verification failed */
75 COSE_ERR_CRYPTO_INVALID_KEY = -26, /**< Invalid cryptographic key */
76
77 /* CBOR structure errors (-30 to -39) */
78 COSE_ERR_CBOR_ENCODING = -30, /**< CBOR encoding failed */
79 COSE_ERR_CBOR_DECODING = -31, /**< CBOR decoding failed */
80 COSE_ERR_CBOR_INVALID_TYPE = -32, /**< Invalid CBOR data type */
81 COSE_ERR_CBOR_MALFORMED = -33, /**< Malformed CBOR data */
82
83 /* COSE structure errors (-40 to -49) */
84 COSE_ERR_INVALID_STRUCTURE = -40, /**< Invalid COSE structure */
85 COSE_ERR_MISSING_HEADER = -41, /**< Required header missing */
86 COSE_ERR_INVALID_HEADER = -42, /**< Invalid header content */
87 COSE_ERR_MISSING_PAYLOAD = -43, /**< Required payload missing */
88
89 /* General errors (-50 to -59) */
90 COSE_ERR_NOT_IMPLEMENTED = -50, /**< Feature not implemented */
91 COSE_ERR_INTERNAL_ERROR = -51, /**< Internal implementation error */
92 COSE_ERR_UNKNOWN = -52 /**< Unknown error condition */
94
95/**
96 * \brief Check if a COSE operation was successful
97 * \param result The result to check
98 * \return true if successful, false otherwise
99 */
100#define COSE_SUCCESS_CHECK(result) ((result) == COSE_SUCCESS)
101
102/**
103 * \brief Check if a COSE operation failed
104 * \param result The result to check
105 * \return true if failed, false otherwise
106 */
107#define COSE_FAILED(result) ((result) < COSE_SUCCESS)
108
109/**
110 * \brief Get a human-readable error message for a COSE error
111 * \param error The COSE error code
112 * \return String description of the error
113 */
114const char *cose_error_string(cose_error_t error);
115
116/**
117 * \brief Macro for checking and propagating COSE errors
118 *
119 * Usage: COSE_CHECK(some_cose_function(params));
120 */
121#define COSE_CHECK(call) do { \
122 cose_error_t _result = (call); \
123 if(COSE_FAILED(_result)) { \
124 return _result; \
125 } \
126} while(0)
127
128#endif /* COSE_ERROR_H_ */
cose_error_t
COSE operation error codes.
Definition cose-error.h:53
@ COSE_ERR_CRYPTO_ENCRYPT
Encryption operation failed.
Definition cose-error.h:72
@ COSE_ERR_BUFFER_TOO_SMALL
Buffer too small for operation.
Definition cose-error.h:60
@ COSE_ERR_CRYPTO_KEYGEN
Key generation failed.
Definition cose-error.h:69
@ COSE_ERR_INVALID_LENGTH
Invalid length parameter.
Definition cose-error.h:61
@ COSE_ERR_MISSING_HEADER
Required header missing.
Definition cose-error.h:85
@ COSE_ERR_UNSUPPORTED_ALGORITHM
Algorithm not supported.
Definition cose-error.h:62
@ COSE_ERR_CBOR_MALFORMED
Malformed CBOR data.
Definition cose-error.h:81
@ COSE_ERR_UNKNOWN
Unknown error condition.
Definition cose-error.h:92
@ COSE_ERR_CRYPTO_SIGN
Digital signature failed.
Definition cose-error.h:70
@ COSE_ERR_CRYPTO_AUTHENTICATION
Authentication tag verification failed.
Definition cose-error.h:74
@ COSE_ERR_CRYPTO_VERIFY
Signature verification failed.
Definition cose-error.h:71
@ COSE_ERR_CBOR_DECODING
CBOR decoding failed.
Definition cose-error.h:79
@ COSE_ERR_BUFFER_OVERFLOW
Buffer overflow detected.
Definition cose-error.h:66
@ COSE_ERR_MISSING_PAYLOAD
Required payload missing.
Definition cose-error.h:87
@ COSE_ERR_INVALID_PARAMETER
Invalid parameter provided.
Definition cose-error.h:58
@ COSE_ERR_CBOR_ENCODING
CBOR encoding failed.
Definition cose-error.h:78
@ COSE_ERR_NOT_IMPLEMENTED
Feature not implemented.
Definition cose-error.h:90
@ COSE_ERR_INVALID_STRUCTURE
Invalid COSE structure.
Definition cose-error.h:84
@ COSE_ERR_CRYPTO_DECRYPT
Decryption operation failed.
Definition cose-error.h:73
@ COSE_ERR_NULL_POINTER
Null pointer provided.
Definition cose-error.h:59
@ COSE_ERR_INVALID_HEADER
Invalid header content.
Definition cose-error.h:86
@ COSE_ERR_INTERNAL_ERROR
Internal implementation error.
Definition cose-error.h:91
@ COSE_ERR_MEMORY_ALLOCATION
Memory allocation failed.
Definition cose-error.h:65
@ COSE_ERR_CBOR_INVALID_TYPE
Invalid CBOR data type.
Definition cose-error.h:80
@ COSE_ERR_CRYPTO_INVALID_KEY
Invalid cryptographic key.
Definition cose-error.h:75
const char * cose_error_string(cose_error_t error)
Get a human-readable error message for a COSE error.
Definition cose-error.c:41