Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ccm.h
00001 /** 00002 * \file ccm.h 00003 * 00004 * \brief CCM combines Counter mode encryption with CBC-MAC authentication 00005 * for 128-bit block ciphers. 00006 * 00007 * Input to CCM includes the following elements: 00008 * <ul><li>Payload - data that is both authenticated and encrypted.</li> 00009 * <li>Associated data (Adata) - data that is authenticated but not 00010 * encrypted, For example, a header.</li> 00011 * <li>Nonce - A unique value that is assigned to the payload and the 00012 * associated data.</li></ul> 00013 * 00014 */ 00015 /* 00016 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved 00017 * SPDX-License-Identifier: Apache-2.0 00018 * 00019 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00020 * not use this file except in compliance with the License. 00021 * You may obtain a copy of the License at 00022 * 00023 * http://www.apache.org/licenses/LICENSE-2.0 00024 * 00025 * Unless required by applicable law or agreed to in writing, software 00026 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00027 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00028 * See the License for the specific language governing permissions and 00029 * limitations under the License. 00030 * 00031 * This file is part of Mbed TLS (https://tls.mbed.org) 00032 */ 00033 00034 #ifndef MBEDTLS_CCM_H 00035 #define MBEDTLS_CCM_H 00036 00037 #include "cipher.h" 00038 00039 #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ 00040 #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ 00041 #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */ 00042 00043 #if !defined(MBEDTLS_CCM_ALT) 00044 // Regular implementation 00045 // 00046 00047 #ifdef __cplusplus 00048 extern "C" { 00049 #endif 00050 00051 /** 00052 * \brief The CCM context-type definition. The CCM context is passed 00053 * to the APIs called. 00054 */ 00055 typedef struct { 00056 mbedtls_cipher_context_t cipher_ctx ; /*!< The cipher context used. */ 00057 } 00058 mbedtls_ccm_context; 00059 00060 /** 00061 * \brief This function initializes the specified CCM context, 00062 * to make references valid, and prepare the context 00063 * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). 00064 * 00065 * \param ctx The CCM context to initialize. 00066 */ 00067 void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); 00068 00069 /** 00070 * \brief This function initializes the CCM context set in the 00071 * \p ctx parameter and sets the encryption key. 00072 * 00073 * \param ctx The CCM context to initialize. 00074 * \param cipher The 128-bit block cipher to use. 00075 * \param key The encryption key. 00076 * \param keybits The key size in bits. This must be acceptable by the cipher. 00077 * 00078 * \return \c 0 on success, or a cipher-specific error code. 00079 */ 00080 int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, 00081 mbedtls_cipher_id_t cipher, 00082 const unsigned char *key, 00083 unsigned int keybits ); 00084 00085 /** 00086 * \brief This function releases and clears the specified CCM context 00087 * and underlying cipher sub-context. 00088 * 00089 * \param ctx The CCM context to clear. 00090 */ 00091 void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); 00092 00093 /** 00094 * \brief This function encrypts a buffer using CCM. 00095 * 00096 * \param ctx The CCM context to use for encryption. 00097 * \param length The length of the input data in Bytes. 00098 * \param iv Initialization vector (nonce). 00099 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. 00100 * \param add The additional data field. 00101 * \param add_len The length of additional data in Bytes. 00102 * Must be less than 2^16 - 2^8. 00103 * \param input The buffer holding the input data. 00104 * \param output The buffer holding the output data. 00105 * Must be at least \p length Bytes wide. 00106 * \param tag The buffer holding the tag. 00107 * \param tag_len The length of the tag to generate in Bytes: 00108 * 4, 6, 8, 10, 12, 14 or 16. 00109 * 00110 * \note The tag is written to a separate buffer. To concatenate 00111 * the \p tag with the \p output, as done in <em>RFC-3610: 00112 * Counter with CBC-MAC (CCM)</em>, use 00113 * \p tag = \p output + \p length, and make sure that the 00114 * output buffer is at least \p length + \p tag_len wide. 00115 * 00116 * \return \c 0 on success. 00117 */ 00118 int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, 00119 const unsigned char *iv, size_t iv_len, 00120 const unsigned char *add, size_t add_len, 00121 const unsigned char *input, unsigned char *output, 00122 unsigned char *tag, size_t tag_len ); 00123 00124 /** 00125 * \brief This function performs a CCM authenticated decryption of a 00126 * buffer. 00127 * 00128 * \param ctx The CCM context to use for decryption. 00129 * \param length The length of the input data in Bytes. 00130 * \param iv Initialization vector. 00131 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. 00132 * \param add The additional data field. 00133 * \param add_len The length of additional data in Bytes. 00134 * Must be less than 2^16 - 2^8. 00135 * \param input The buffer holding the input data. 00136 * \param output The buffer holding the output data. 00137 * Must be at least \p length Bytes wide. 00138 * \param tag The buffer holding the tag. 00139 * \param tag_len The length of the tag in Bytes. 00140 * 4, 6, 8, 10, 12, 14 or 16. 00141 * 00142 * \return 0 if successful and authenticated, or 00143 * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. 00144 */ 00145 int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, 00146 const unsigned char *iv, size_t iv_len, 00147 const unsigned char *add, size_t add_len, 00148 const unsigned char *input, unsigned char *output, 00149 const unsigned char *tag, size_t tag_len ); 00150 00151 #ifdef __cplusplus 00152 } 00153 #endif 00154 00155 #else /* MBEDTLS_CCM_ALT */ 00156 #include "ccm_alt.h" 00157 #endif /* MBEDTLS_CCM_ALT */ 00158 00159 #ifdef __cplusplus 00160 extern "C" { 00161 #endif 00162 00163 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) 00164 /** 00165 * \brief The CCM checkup routine. 00166 * 00167 * \return \c 0 on success, or \c 1 on failure. 00168 */ 00169 int mbedtls_ccm_self_test( int verbose ); 00170 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ 00171 00172 #ifdef __cplusplus 00173 } 00174 #endif 00175 00176 #endif /* MBEDTLS_CCM_H */
Generated on Tue Jul 12 2022 14:23:30 by
