1c87c5fbaSopenharmony_ci/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2c87c5fbaSopenharmony_ci 3c87c5fbaSopenharmony_ci/* 4c87c5fbaSopenharmony_ci * coap_crypto_internal.h -- Structures, Enums & Functions that are not 5c87c5fbaSopenharmony_ci * exposed to application programming 6c87c5fbaSopenharmony_ci * 7c87c5fbaSopenharmony_ci * Copyright (C) 2017-2023 Olaf Bergmann <bergmann@tzi.org> 8c87c5fbaSopenharmony_ci * Copyright (C) 2021-2023 Jon Shallow <supjps-ietf@jpshallow.com> 9c87c5fbaSopenharmony_ci * 10c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause 11c87c5fbaSopenharmony_ci * 12c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see README for terms 13c87c5fbaSopenharmony_ci * of use. 14c87c5fbaSopenharmony_ci */ 15c87c5fbaSopenharmony_ci 16c87c5fbaSopenharmony_ci/** 17c87c5fbaSopenharmony_ci * @file coap_crypto_internal.h 18c87c5fbaSopenharmony_ci * @brief COAP crypto internal information 19c87c5fbaSopenharmony_ci */ 20c87c5fbaSopenharmony_ci 21c87c5fbaSopenharmony_ci#ifndef COAP_CRYPTO_INTERNAL_H_ 22c87c5fbaSopenharmony_ci#define COAP_CRYPTO_INTERNAL_H_ 23c87c5fbaSopenharmony_ci 24c87c5fbaSopenharmony_ci/** 25c87c5fbaSopenharmony_ci * @ingroup internal_api 26c87c5fbaSopenharmony_ci * @defgroup crypto_internal OSCORE Crypto Support 27c87c5fbaSopenharmony_ci * Internal API for interfacing with Crypto libraries 28c87c5fbaSopenharmony_ci * @{ 29c87c5fbaSopenharmony_ci */ 30c87c5fbaSopenharmony_ci 31c87c5fbaSopenharmony_ci#include "oscore/oscore_cose.h" 32c87c5fbaSopenharmony_ci 33c87c5fbaSopenharmony_ci#ifndef COAP_CRYPTO_MAX_KEY_SIZE 34c87c5fbaSopenharmony_ci#define COAP_CRYPTO_MAX_KEY_SIZE (32) 35c87c5fbaSopenharmony_ci#endif /* COAP_CRYPTO_MAX_KEY_SIZE */ 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ci#ifndef COAP_OSCORE_DEFAULT_REPLAY_WINDOW 38c87c5fbaSopenharmony_ci#define COAP_OSCORE_DEFAULT_REPLAY_WINDOW 32 39c87c5fbaSopenharmony_ci#endif /* COAP_OSCORE_DEFAULT_REPLAY_WINDOW */ 40c87c5fbaSopenharmony_ci 41c87c5fbaSopenharmony_ci/** 42c87c5fbaSopenharmony_ci * The structure that holds the Crypto Key. 43c87c5fbaSopenharmony_ci */ 44c87c5fbaSopenharmony_citypedef coap_bin_const_t coap_crypto_key_t; 45c87c5fbaSopenharmony_ci 46c87c5fbaSopenharmony_ci/** 47c87c5fbaSopenharmony_ci * The structure that holds the AES Crypto information 48c87c5fbaSopenharmony_ci */ 49c87c5fbaSopenharmony_citypedef struct coap_crypto_aes_ccm_t { 50c87c5fbaSopenharmony_ci coap_crypto_key_t key; /**< The Key to use */ 51c87c5fbaSopenharmony_ci const uint8_t *nonce; /**< must be exactly 15 - l bytes */ 52c87c5fbaSopenharmony_ci size_t tag_len; /**< The size of the Tag */ 53c87c5fbaSopenharmony_ci size_t l; /**< The number of bytes in the length field */ 54c87c5fbaSopenharmony_ci} coap_crypto_aes_ccm_t; 55c87c5fbaSopenharmony_ci 56c87c5fbaSopenharmony_ci/** 57c87c5fbaSopenharmony_ci * The common structure that holds the Crypto information 58c87c5fbaSopenharmony_ci */ 59c87c5fbaSopenharmony_citypedef struct coap_crypto_param_t { 60c87c5fbaSopenharmony_ci cose_alg_t alg; /**< The COSE algorith to use */ 61c87c5fbaSopenharmony_ci union { 62c87c5fbaSopenharmony_ci coap_crypto_aes_ccm_t aes; /**< Used if AES type encryption */ 63c87c5fbaSopenharmony_ci coap_crypto_key_t key; /**< The key to use */ 64c87c5fbaSopenharmony_ci } params; 65c87c5fbaSopenharmony_ci} coap_crypto_param_t; 66c87c5fbaSopenharmony_ci 67c87c5fbaSopenharmony_ci/** 68c87c5fbaSopenharmony_ci * Check whether the defined cipher algorithm is supported by the underlying 69c87c5fbaSopenharmony_ci * crypto library. 70c87c5fbaSopenharmony_ci * 71c87c5fbaSopenharmony_ci * @param alg The COSE algorithm to check. 72c87c5fbaSopenharmony_ci * 73c87c5fbaSopenharmony_ci * @return @c 1 if there is support, else @c 0. 74c87c5fbaSopenharmony_ci */ 75c87c5fbaSopenharmony_ciint coap_crypto_check_cipher_alg(cose_alg_t alg); 76c87c5fbaSopenharmony_ci 77c87c5fbaSopenharmony_ci/** 78c87c5fbaSopenharmony_ci * Check whether the defined hkdf algorithm is supported by the underlying 79c87c5fbaSopenharmony_ci * crypto library. 80c87c5fbaSopenharmony_ci * 81c87c5fbaSopenharmony_ci * @param hkdf_alg The COSE HKDF algorithm to check. 82c87c5fbaSopenharmony_ci * 83c87c5fbaSopenharmony_ci * @return @c 1 if there is support, else @c 0. 84c87c5fbaSopenharmony_ci */ 85c87c5fbaSopenharmony_ciint coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg); 86c87c5fbaSopenharmony_ci 87c87c5fbaSopenharmony_ci/** 88c87c5fbaSopenharmony_ci * Encrypt the provided plaintext data 89c87c5fbaSopenharmony_ci * 90c87c5fbaSopenharmony_ci * @param params The Encrypt/Decrypt/Hash paramaters. 91c87c5fbaSopenharmony_ci * @param data The data to encrypt. 92c87c5fbaSopenharmony_ci * @param aad The additional AAD information. 93c87c5fbaSopenharmony_ci * @param result Where to put the encrypted data. 94c87c5fbaSopenharmony_ci * @param max_result_len The maximum size for @p result 95c87c5fbaSopenharmony_ci * (updated with actual size). 96c87c5fbaSopenharmony_ci * 97c87c5fbaSopenharmony_ci * @return @c 1 if the data was successfully encrypted, else @c 0. 98c87c5fbaSopenharmony_ci */ 99c87c5fbaSopenharmony_ciint coap_crypto_aead_encrypt(const coap_crypto_param_t *params, 100c87c5fbaSopenharmony_ci coap_bin_const_t *data, 101c87c5fbaSopenharmony_ci coap_bin_const_t *aad, 102c87c5fbaSopenharmony_ci uint8_t *result, 103c87c5fbaSopenharmony_ci size_t *max_result_len); 104c87c5fbaSopenharmony_ci 105c87c5fbaSopenharmony_ci/** 106c87c5fbaSopenharmony_ci * Decrypt the provided encrypted data into plaintext. 107c87c5fbaSopenharmony_ci * 108c87c5fbaSopenharmony_ci * @param params The Encrypt/Decrypt/Hash paramaters. 109c87c5fbaSopenharmony_ci * @param data The data to decrypt. 110c87c5fbaSopenharmony_ci * @param aad The additional AAD information. 111c87c5fbaSopenharmony_ci * @param result Where to put the decrypted data. 112c87c5fbaSopenharmony_ci * @param max_result_len The maximum size for @p result 113c87c5fbaSopenharmony_ci * (updated with actual size). 114c87c5fbaSopenharmony_ci * 115c87c5fbaSopenharmony_ci * @return @c 1 if the data was successfully decrypted, else @c 0. 116c87c5fbaSopenharmony_ci */ 117c87c5fbaSopenharmony_ciint coap_crypto_aead_decrypt(const coap_crypto_param_t *params, 118c87c5fbaSopenharmony_ci coap_bin_const_t *data, 119c87c5fbaSopenharmony_ci coap_bin_const_t *aad, 120c87c5fbaSopenharmony_ci uint8_t *result, 121c87c5fbaSopenharmony_ci size_t *max_result_len); 122c87c5fbaSopenharmony_ci 123c87c5fbaSopenharmony_ci/** 124c87c5fbaSopenharmony_ci * Create a HMAC hash of the provided data. 125c87c5fbaSopenharmony_ci * 126c87c5fbaSopenharmony_ci * @param hmac_alg The COSE HMAC algorithm to use. 127c87c5fbaSopenharmony_ci * @param key The key to use for the hash. 128c87c5fbaSopenharmony_ci * @param data The data to hash. 129c87c5fbaSopenharmony_ci * @param hmac Where to put the created hmac result if successful. 130c87c5fbaSopenharmony_ci * 131c87c5fbaSopenharmony_ci * @return @c 1 if the hmac of the data was successful, else @c 0. 132c87c5fbaSopenharmony_ci * It is the responsibility of the caller to release the 133c87c5fbaSopenharmony_ci * created hmac. 134c87c5fbaSopenharmony_ci */ 135c87c5fbaSopenharmony_ciint coap_crypto_hmac(cose_hmac_alg_t hmac_alg, 136c87c5fbaSopenharmony_ci coap_bin_const_t *key, 137c87c5fbaSopenharmony_ci coap_bin_const_t *data, 138c87c5fbaSopenharmony_ci coap_bin_const_t **hmac); 139c87c5fbaSopenharmony_ci 140c87c5fbaSopenharmony_ci/** 141c87c5fbaSopenharmony_ci * Create a hash of the provided data. 142c87c5fbaSopenharmony_ci * 143c87c5fbaSopenharmony_ci * @param alg The hash algorithm. 144c87c5fbaSopenharmony_ci * @param data The data to hash. 145c87c5fbaSopenharmony_ci * @param hash Where to put the hash result if successful. 146c87c5fbaSopenharmony_ci * 147c87c5fbaSopenharmony_ci * @return @c 1 if the data was successfully hashed, else @c 0. 148c87c5fbaSopenharmony_ci * It is the responsibility of the caller to release the 149c87c5fbaSopenharmony_ci * created hash. 150c87c5fbaSopenharmony_ci */ 151c87c5fbaSopenharmony_ciint coap_crypto_hash(cose_alg_t alg, 152c87c5fbaSopenharmony_ci const coap_bin_const_t *data, 153c87c5fbaSopenharmony_ci coap_bin_const_t **hash); 154c87c5fbaSopenharmony_ci 155c87c5fbaSopenharmony_ci/** @} */ 156c87c5fbaSopenharmony_ci 157c87c5fbaSopenharmony_ci#endif /* COAP_CRYPTO_INTERNAL_H_ */ 158