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 This file provides an API for the CCM authenticated encryption 00005 * mode for block ciphers. 00006 * 00007 * CCM combines Counter mode encryption with CBC-MAC authentication 00008 * for 128-bit block ciphers. 00009 * 00010 * Input to CCM includes the following elements: 00011 * <ul><li>Payload - data that is both authenticated and encrypted.</li> 00012 * <li>Associated data (Adata) - data that is authenticated but not 00013 * encrypted, For example, a header.</li> 00014 * <li>Nonce - A unique value that is assigned to the payload and the 00015 * associated data.</li></ul> 00016 * 00017 * Definition of CCM: 00018 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf 00019 * RFC 3610 "Counter with CBC-MAC (CCM)" 00020 * 00021 * Related: 00022 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption" 00023 * 00024 * Definition of CCM*: 00025 * IEEE 802.15.4 - IEEE Standard for Local and metropolitan area networks 00026 * Integer representation is fixed most-significant-octet-first order and 00027 * the representation of octets is most-significant-bit-first order. This is 00028 * consistent with RFC 3610. 00029 */ 00030 /* 00031 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved 00032 * SPDX-License-Identifier: Apache-2.0 00033 * 00034 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00035 * not use this file except in compliance with the License. 00036 * You may obtain a copy of the License at 00037 * 00038 * http://www.apache.org/licenses/LICENSE-2.0 00039 * 00040 * Unless required by applicable law or agreed to in writing, software 00041 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00042 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00043 * See the License for the specific language governing permissions and 00044 * limitations under the License. 00045 * 00046 * This file is part of Mbed TLS (https://tls.mbed.org) 00047 */ 00048 00049 #ifndef MBEDTLS_CCM_H 00050 #define MBEDTLS_CCM_H 00051 00052 #include "cipher.h" 00053 00054 #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ 00055 #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ 00056 #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */ 00057 00058 00059 #ifdef __cplusplus 00060 extern "C" { 00061 #endif 00062 00063 #if !defined(MBEDTLS_CCM_ALT) 00064 // Regular implementation 00065 // 00066 00067 /** 00068 * \brief The CCM context-type definition. The CCM context is passed 00069 * to the APIs called. 00070 */ 00071 typedef struct mbedtls_ccm_context 00072 { 00073 mbedtls_cipher_context_t cipher_ctx ; /*!< The cipher context used. */ 00074 } 00075 mbedtls_ccm_context; 00076 00077 #else /* MBEDTLS_CCM_ALT */ 00078 #include "ccm_alt.h" 00079 #endif /* MBEDTLS_CCM_ALT */ 00080 00081 /** 00082 * \brief This function initializes the specified CCM context, 00083 * to make references valid, and prepare the context 00084 * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). 00085 * 00086 * \param ctx The CCM context to initialize. 00087 */ 00088 void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); 00089 00090 /** 00091 * \brief This function initializes the CCM context set in the 00092 * \p ctx parameter and sets the encryption key. 00093 * 00094 * \param ctx The CCM context to initialize. 00095 * \param cipher The 128-bit block cipher to use. 00096 * \param key The encryption key. 00097 * \param keybits The key size in bits. This must be acceptable by the cipher. 00098 * 00099 * \return \c 0 on success. 00100 * \return A CCM or cipher-specific error code on failure. 00101 */ 00102 int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, 00103 mbedtls_cipher_id_t cipher, 00104 const unsigned char *key, 00105 unsigned int keybits ); 00106 00107 /** 00108 * \brief This function releases and clears the specified CCM context 00109 * and underlying cipher sub-context. 00110 * 00111 * \param ctx The CCM context to clear. 00112 */ 00113 void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); 00114 00115 /** 00116 * \brief This function encrypts a buffer using CCM. 00117 * 00118 * \note The tag is written to a separate buffer. To concatenate 00119 * the \p tag with the \p output, as done in <em>RFC-3610: 00120 * Counter with CBC-MAC (CCM)</em>, use 00121 * \p tag = \p output + \p length, and make sure that the 00122 * output buffer is at least \p length + \p tag_len wide. 00123 * 00124 * \param ctx The CCM context to use for encryption. 00125 * \param length The length of the input data in Bytes. 00126 * \param iv Initialization vector (nonce). 00127 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, 00128 * or 13. The length L of the message length field is 00129 * 15 - \p iv_len. 00130 * \param add The additional data field. 00131 * \param add_len The length of additional data in Bytes. 00132 * Must be less than 2^16 - 2^8. 00133 * \param input The buffer holding the input data. 00134 * \param output The buffer holding the output data. 00135 * Must be at least \p length Bytes wide. 00136 * \param tag The buffer holding the authentication field. 00137 * \param tag_len The length of the authentication field to generate in Bytes: 00138 * 4, 6, 8, 10, 12, 14 or 16. 00139 * 00140 * \return \c 0 on success. 00141 * \return A CCM or cipher-specific error code on failure. 00142 */ 00143 int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, 00144 const unsigned char *iv, size_t iv_len, 00145 const unsigned char *add, size_t add_len, 00146 const unsigned char *input, unsigned char *output, 00147 unsigned char *tag, size_t tag_len ); 00148 00149 /** 00150 * \brief This function encrypts a buffer using CCM*. 00151 * 00152 * \note The tag is written to a separate buffer. To concatenate 00153 * the \p tag with the \p output, as done in <em>RFC-3610: 00154 * Counter with CBC-MAC (CCM)</em>, use 00155 * \p tag = \p output + \p length, and make sure that the 00156 * output buffer is at least \p length + \p tag_len wide. 00157 * 00158 * \note When using this function in a variable tag length context, 00159 * the tag length has to be encoded into the \p iv passed to 00160 * this function. 00161 * 00162 * \param ctx The CCM context to use for encryption. 00163 * \param length The length of the input data in Bytes. 00164 * \param iv Initialization vector (nonce). 00165 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, 00166 * or 13. The length L of the message length field is 00167 * 15 - \p iv_len. 00168 * \param add The additional data field. 00169 * \param add_len The length of additional data in Bytes. 00170 * Must be less than 2^16 - 2^8. 00171 * \param input The buffer holding the input data. 00172 * \param output The buffer holding the output data. 00173 * Must be at least \p length Bytes wide. 00174 * \param tag The buffer holding the authentication field. 00175 * \param tag_len The length of the authentication field to generate in Bytes: 00176 * 0, 4, 6, 8, 10, 12, 14 or 16. 00177 * 00178 * \warning Passing 0 as \p tag_len means that the message is no 00179 * longer authenticated. 00180 * 00181 * \return \c 0 on success. 00182 * \return A CCM or cipher-specific error code on failure. 00183 */ 00184 int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, 00185 const unsigned char *iv, size_t iv_len, 00186 const unsigned char *add, size_t add_len, 00187 const unsigned char *input, unsigned char *output, 00188 unsigned char *tag, size_t tag_len ); 00189 00190 /** 00191 * \brief This function performs a CCM authenticated decryption of a 00192 * buffer. 00193 * 00194 * \param ctx The CCM context to use for decryption. 00195 * \param length The length of the input data in Bytes. 00196 * \param iv Initialization vector (nonce). 00197 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, 00198 * or 13. The length L of the message length field is 00199 * 15 - \p iv_len. 00200 * \param add The additional data field. 00201 * \param add_len The length of additional data in Bytes. 00202 * Must be less than 2^16 - 2^8. 00203 * \param input The buffer holding the input data. 00204 * \param output The buffer holding the output data. 00205 * Must be at least \p length Bytes wide. 00206 * \param tag The buffer holding the authentication field. 00207 * \param tag_len The length of the authentication field in Bytes. 00208 * 4, 6, 8, 10, 12, 14 or 16. 00209 * 00210 * \return \c 0 on success. This indicates that the message is authentic. 00211 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. 00212 * \return A cipher-specific error code on calculation failure. 00213 */ 00214 int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, 00215 const unsigned char *iv, size_t iv_len, 00216 const unsigned char *add, size_t add_len, 00217 const unsigned char *input, unsigned char *output, 00218 const unsigned char *tag, size_t tag_len ); 00219 00220 /** 00221 * \brief This function performs a CCM* authenticated decryption of a 00222 * buffer. 00223 * 00224 * \note When using this function in a variable tag length context, 00225 * the tag length has to be decoded from \p iv and passed to 00226 * this function as \p tag_len. (\p tag needs to be adjusted 00227 * accordingly.) 00228 * 00229 * \param ctx The CCM context to use for decryption. 00230 * \param length The length of the input data in Bytes. 00231 * \param iv Initialization vector (nonce). 00232 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, 00233 * or 13. The length L of the message length field is 00234 * 15 - \p iv_len. 00235 * \param add The additional data field. 00236 * \param add_len The length of additional data in Bytes. 00237 * Must be less than 2^16 - 2^8. 00238 * \param input The buffer holding the input data. 00239 * \param output The buffer holding the output data. 00240 * Must be at least \p length Bytes wide. 00241 * \param tag The buffer holding the authentication field. 00242 * \param tag_len The length of the authentication field in Bytes. 00243 * 0, 4, 6, 8, 10, 12, 14 or 16. 00244 * 00245 * \warning Passing 0 as \p tag_len means that the message is no 00246 * longer authenticated. 00247 * 00248 * \return \c 0 on success. 00249 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. 00250 * \return A cipher-specific error code on calculation failure. 00251 */ 00252 int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, 00253 const unsigned char *iv, size_t iv_len, 00254 const unsigned char *add, size_t add_len, 00255 const unsigned char *input, unsigned char *output, 00256 const unsigned char *tag, size_t tag_len ); 00257 00258 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) 00259 /** 00260 * \brief The CCM checkup routine. 00261 * 00262 * \return \c 0 on success. 00263 * \return \c 1 on failure. 00264 */ 00265 int mbedtls_ccm_self_test( int verbose ); 00266 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ 00267 00268 #ifdef __cplusplus 00269 } 00270 #endif 00271 00272 #endif /* MBEDTLS_CCM_H */
Generated on Tue Jul 12 2022 20:52:39 by
1.7.2