| #include <cn-cbor/cn-cbor.h> |
| #include "configure.h" |
| typedef unsigned char byte; |
| |
| typedef struct _cose * HCOSE; |
| typedef struct _cose_sign * HCOSE_SIGN; |
| typedef struct _cose_signer * HCOSE_SIGNER; |
| typedef struct _cose_encrypt * HCOSE_ENCRYPT; |
| typedef struct _cose_recipient * HCOSE_RECIPIENT; |
| typedef struct _cose_mac * HCOSE_MAC; |
| |
| /** |
| * All of the different kinds of errors |
| */ |
| typedef enum cose_error { |
| /** No error has occurred */ |
| COSE_ERR_NONE, |
| /** An invalid parameter was passed to a function */ |
| COSE_ERR_INVALID_PARAMETER, |
| /** Allocation failed */ |
| COSE_ERR_OUT_OF_MEMORY, |
| /** Error in processing CBOR */ |
| COSE_ERR_CBOR, |
| /** Unknown algorithm found */ |
| COSE_ERR_UNKNOWN_ALGORITHM, |
| /** No usable recipient found */ |
| COSE_ERR_NO_RECIPIENT_FOUND, |
| /** Decryption operation failed */ |
| COSE_ERR_DECRYPT_FAILED, |
| /** Cryptographic failure */ |
| COSE_ERR_CRYPTO_FAIL |
| } cose_error; |
| |
| /** |
| * Errors |
| */ |
| typedef struct _cose_errback { |
| /** The error, or CN_CBOR_NO_ERROR if none */ |
| cose_error err; |
| } cose_errback; |
| |
| typedef enum { |
| COSE_unknown_object = 0, |
| COSE_sign_object = 991, |
| COSE_sign1_object = 997, |
| COSE_enveloped_object = 992, |
| COSE_encrypted_object = 993, |
| COSE_mac_object = 994, |
| COSE_mac0_object = 996, |
| COSE_recipient_object = -1 |
| } COSE_object_type; |
| |
| // Generic functions for the COSE library |
| |
| HCOSE COSE_Decode(const byte * rgbData, size_t cbData, int * type, COSE_object_type struct_type, CBOR_CONTEXT_COMMA cose_errback * perr); // Decode the object |
| size_t COSE_Encode(HCOSE msg, byte * rgb, size_t ib, size_t cb); |
| |
| cn_cbor * COSE_get_cbor(HCOSE hmsg); |
| |
| // Functions for the signing object |
| |
| HCOSE_SIGN COSE_Sign_Init(CBOR_CONTEXT_COMMA cose_errback * perr); |
| bool COSE_Sign_Free(HCOSE_SIGN cose); |
| |
| HCOSE_ENCRYPT COSE_Encrypt_Init(CBOR_CONTEXT_COMMA cose_errback * perr); |
| bool COSE_Encrypt_Free(HCOSE_ENCRYPT cose); |
| |
| HCOSE_MAC COSE_Mac_Init(CBOR_CONTEXT_COMMA cose_errback * perr); |
| bool COSE_Mac_Free(HCOSE_MAC cose); |
| |
| typedef enum { |
| COSE_PROTECT_ONLY = 1, |
| COSE_UNPROTECT_ONLY = 2, |
| COSE_DONT_SEND = 4, |
| COSE_BOTH = 7 |
| } cose_protect_state; |
| |
| typedef enum { |
| COSE_Algorithm_AES_GCM_128 = 1, |
| COSE_Algorithm_AES_GCM_192 = 2, |
| COSE_Algorithm_AES_GCM_256 = 3, |
| |
| COSE_Algorithm_HMAC_256_64 = 4, |
| COSE_Algorithm_HMAC_256_256 = 5, |
| COSE_Algorithm_HMAC_384_384 = 6, |
| COSE_Algorithm_HMAC_512_512 = 7, |
| |
| COSE_Algorithm_CBC_MAC_128_64 = 14, |
| COSE_Algorithm_CBC_MAC_256_64 = 15, |
| COSE_Algorithm_CBC_MAC_128_128 = 25, |
| COSE_Algorithm_CBC_MAC_256_128 = 26, |
| |
| COSE_Algorithm_AES_CCM_16_64_128 = 10, |
| COSE_Algorithm_AES_CCM_16_64_256 = 11, |
| COSE_Algorithm_AES_CCM_64_64_128 = 30, |
| COSE_Algorithm_AES_CCM_64_64_256 = 31, |
| COSE_Algorithm_AES_CCM_16_128_128 = 12, |
| COSE_Algorithm_AES_CCM_16_128_256 = 13, |
| COSE_Algorithm_AES_CCM_64_128_128 = 32, |
| COSE_Algorithm_AES_CCM_64_128_256 = 33, |
| |
| COSE_Algorithm_ECDH_ES_HKDF_256 = -25, |
| COSE_Algorithm_ECDH_ES_HKDF_512 = -26, |
| COSE_Algorithm_ECDH_SS_HKDF_256 = -27, |
| COSE_Algorithm_ECDH_SS_HKDF_512 = -28, |
| |
| COSE_Algorithm_ECDH_ES_A128KW = -29, |
| COSE_Algorithm_ECDH_ES_A192KW = -30, |
| COSE_Algorithm_ECDH_ES_A256KW = -31, |
| COSE_Algorithm_ECDH_SS_A128KW = -32, |
| COSE_Algorithm_ECDH_SS_A192KW = -33, |
| COSE_Algorithm_ECDH_SS_A256KW = -34, |
| |
| COSE_Algorithm_AES_KW_128 = -3, |
| COSE_Algorithm_AES_KW_192 = -4, |
| COSE_Algorithm_AES_KW_256 = -5, |
| |
| COSE_Algorithm_Direct = -6, |
| |
| COSE_Algorithm_Direct_HKDF_HMAC_SHA_256 = -10, |
| COSE_Algorithm_Direct_HKDF_HMAC_SHA_512 = -11, |
| COSE_Algorithm_Direct_HKDF_AES_128 = -12, |
| COSE_Algorithm_Direct_HKDF_AES_256 = -13, |
| |
| COSE_Algorithm_PS256 = -8, |
| COSE_Algorithm_PS384 = -37, |
| COSE_Algorithm_PS512 = -38, |
| |
| COSE_Algorithm_ECDSA_SHA_256 = -7, |
| COSE_Algorithm_ECDSA_SHA_384 = -35, |
| COSE_Algorithm_ECDSA_SHA_512 = -36, |
| } COSE_Algorithms; |
| |
| typedef enum { |
| COSE_Header_Algorithm = 1, |
| COSE_Header_KID = 4, |
| COSE_Header_IV = 5, |
| } COSE_Header; |
| |
| typedef enum { |
| COSE_Key_Type_EC2 = 2, |
| COSE_Key_Type = 1, |
| COSE_Key_ID = 2, |
| COSE_Parameter_KID = 4, |
| } COSE_Constants; |
| |
| bool COSE_Encrypt_SetContent(HCOSE_ENCRYPT cose, const byte * rgbContent, size_t cbContent, cose_errback * errp); |
| bool COSE_Encrypt_SetNonce(HCOSE_ENCRYPT cose, byte * rgbIV, size_t cbIV); |
| |
| cn_cbor * COSE_Encrypt_map_get_string(HCOSE_ENCRYPT cose, const char * key, int flags, cose_errback * errp); |
| cn_cbor * COSE_Encrypt_map_get_int(HCOSE_ENCRYPT cose, int key, int flags, cose_errback * errp); |
| |
| bool COSE_Encrypt_map_put(HCOSE_ENCRYPT cose, int key, cn_cbor * value, int flags, cose_errback * errp); |
| |
| bool COSE_Encrypt_encrypt(HCOSE_ENCRYPT cose, cose_errback * perror); |
| bool COSE_Encrypt_decrypt(HCOSE_ENCRYPT, HCOSE_RECIPIENT, cose_errback * perr); |
| |
| HCOSE_RECIPIENT COSE_Encrypt_add_shared_secret(HCOSE_ENCRYPT cose, COSE_Algorithms algId, byte * rgbKey, int cbKey, byte * rgbKid, int cbKid, cose_errback * perr); |
| |
| extern bool COSE_Encrypt_AddRecipient(HCOSE_ENCRYPT hMac, HCOSE_RECIPIENT hRecip, cose_errback * perr); |
| HCOSE_RECIPIENT COSE_Encrypt_GetRecipient(HCOSE_ENCRYPT cose, int iRecipient, cose_errback * perr); |
| |
| HCOSE_RECIPIENT COSE_Recipient_Init(CBOR_CONTEXT_COMMA cose_errback * perror); |
| bool COSE_Recipient_Free(HCOSE_RECIPIENT cose); |
| bool COSE_Recipient_SetKey_secret(HCOSE_RECIPIENT h, const byte * rgb, int cb, cose_errback * perr); |
| bool COSE_Recipient_SetKey(HCOSE_RECIPIENT h, const cn_cbor * pKey, cose_errback * perror); |
| |
| bool COSE_Recipient_map_put(HCOSE_RECIPIENT h, int key, cn_cbor * value, int flags, cose_errback * perror); |
| cn_cbor * COSE_Recipient_map_get_string(HCOSE_RECIPIENT cose, const char * key, int flags, cose_errback * errp); |
| cn_cbor * COSE_Recipient_map_get_int(HCOSE_RECIPIENT cose, int key, int flags, cose_errback * errp); |
| |
| |
| // |
| // |
| |
| bool COSE_Mac_SetContent(HCOSE_MAC cose, const byte * rgbContent, size_t cbContent, cose_errback * errp); |
| |
| cn_cbor * COSE_Mac_map_get_int(HCOSE_MAC h, int key, int flags, cose_errback * perror); |
| bool COSE_Mac_map_put(HCOSE_MAC cose, int key, cn_cbor * value, int flags, cose_errback * errp); |
| |
| bool COSE_Mac_encrypt(HCOSE_MAC cose, cose_errback * perror); |
| bool COSE_Mac_validate(HCOSE_MAC, HCOSE_RECIPIENT, cose_errback * perr); |
| |
| bool COSE_Encrypt_SetContent(HCOSE_ENCRYPT cose, const byte * rgbContent, size_t cbContent, cose_errback * errp); |
| |
| HCOSE_RECIPIENT COSE_Mac_add_shared_secret(HCOSE_MAC cose, COSE_Algorithms algId, byte * rgbKey, int cbKey, byte * rgbKid, int cbKid, cose_errback * perr); |
| |
| extern bool COSE_Mac_AddRecipient(HCOSE_MAC hMac, HCOSE_RECIPIENT hRecip, cose_errback * perr); |
| HCOSE_RECIPIENT COSE_Mac_GetRecipient(HCOSE_MAC cose, int iRecipient, cose_errback * perr); |
| |
| // |
| // |
| |
| bool COSE_Sign_SetContent(HCOSE_SIGN cose, const byte * rgbContent, size_t cbContent, cose_errback * errp); |
| HCOSE_SIGNER COSE_Sign_add_signer(HCOSE_SIGN cose, const cn_cbor * pkey, int algId, cose_errback * perr); |
| extern bool COSE_Sign_AddSigner(HCOSE_SIGN hSign, HCOSE_SIGNER hSigner, cose_errback * perr); |
| bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback * perr); |
| HCOSE_SIGNER COSE_Sign_GetSigner(HCOSE_SIGN cose, int iSigner, cose_errback * perr); |
| bool COSE_Sign_validate(HCOSE_SIGN hSign, HCOSE_SIGNER hSigner, cose_errback * perr); |
| cn_cbor * COSE_Sign_map_get_int(HCOSE_SIGN h, int key, int flags, cose_errback * perror); |
| bool COSE_Sign_map_put(HCOSE_SIGN cose, int key, cn_cbor * value, int flags, cose_errback * errp); |
| |
| |
| HCOSE_SIGNER COSE_Signer_Init(CBOR_CONTEXT_COMMA cose_errback * perror); |
| bool COSE_Signer_Free(HCOSE_SIGNER cose); |
| bool COSE_Signer_SetKey(HCOSE_SIGNER hSigner, const cn_cbor * pkey, cose_errback * perr); |
| bool COSE_Signer_map_put(HCOSE_SIGNER cose, int key, cn_cbor * value, int flags, cose_errback * errp); |