Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file ccm.h |
| 3 | * |
Rose Zadik | 477dce1 | 2018-04-17 16:31:22 +0100 | [diff] [blame] | 4 | * \brief This file provides an API for the CCM authenticated encryption |
| 5 | * mode for block ciphers. |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 6 | * |
| 7 | * CCM combines Counter mode encryption with CBC-MAC authentication |
| 8 | * for 128-bit block ciphers. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 9 | * |
| 10 | * Input to CCM includes the following elements: |
| 11 | * <ul><li>Payload - data that is both authenticated and encrypted.</li> |
| 12 | * <li>Associated data (Adata) - data that is authenticated but not |
| 13 | * encrypted, For example, a header.</li> |
| 14 | * <li>Nonce - A unique value that is assigned to the payload and the |
| 15 | * associated data.</li></ul> |
| 16 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 17 | */ |
| 18 | /* |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 19 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 20 | * SPDX-License-Identifier: Apache-2.0 |
| 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 23 | * not use this file except in compliance with the License. |
| 24 | * You may obtain a copy of the License at |
| 25 | * |
| 26 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | * |
| 28 | * Unless required by applicable law or agreed to in writing, software |
| 29 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 30 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | * See the License for the specific language governing permissions and |
| 32 | * limitations under the License. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 33 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 34 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 35 | */ |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #ifndef MBEDTLS_CCM_H |
| 38 | #define MBEDTLS_CCM_H |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 39 | |
| 40 | #include "cipher.h" |
| 41 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 42 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ |
| 43 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
| 44 | #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 45 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 51 | #if !defined(MBEDTLS_CCM_ALT) |
| 52 | // Regular implementation |
| 53 | // |
| 54 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 55 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 56 | * \brief The CCM context-type definition. The CCM context is passed |
| 57 | * to the APIs called. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 58 | */ |
| 59 | typedef struct { |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 60 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 61 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_ccm_context; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 63 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 64 | #else /* MBEDTLS_CCM_ALT */ |
| 65 | #include "ccm_alt.h" |
| 66 | #endif /* MBEDTLS_CCM_ALT */ |
| 67 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 68 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 69 | * \brief This function initializes the specified CCM context, |
| 70 | * to make references valid, and prepare the context |
| 71 | * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 72 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 73 | * \param ctx The CCM context to initialize. |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 74 | */ |
| 75 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); |
| 76 | |
| 77 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 78 | * \brief This function initializes the CCM context set in the |
| 79 | * \p ctx parameter and sets the encryption key. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 80 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 81 | * \param ctx The CCM context to initialize. |
| 82 | * \param cipher The 128-bit block cipher to use. |
| 83 | * \param key The encryption key. |
| 84 | * \param keybits The key size in bits. This must be acceptable by the cipher. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 85 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 86 | * \return \c 0 on success. |
Rose Zadik | ef87179 | 2018-04-17 10:41:48 +0100 | [diff] [blame] | 87 | * \return A CCM or cipher-specific error code on failure. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 88 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 89 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 90 | mbedtls_cipher_id_t cipher, |
| 91 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 92 | unsigned int keybits ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 95 | * \brief This function releases and clears the specified CCM context |
| 96 | * and underlying cipher sub-context. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 97 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 98 | * \param ctx The CCM context to clear. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 99 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 101 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 102 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 103 | * \brief This function encrypts a buffer using CCM. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 104 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 105 | * \note The tag is written to a separate buffer. To concatenate |
| 106 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 107 | * Counter with CBC-MAC (CCM)</em>, use |
| 108 | * \p tag = \p output + \p length, and make sure that the |
| 109 | * output buffer is at least \p length + \p tag_len wide. |
| 110 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 111 | * \param ctx The CCM context to use for encryption. |
| 112 | * \param length The length of the input data in Bytes. |
| 113 | * \param iv Initialization vector (nonce). |
| 114 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 115 | * \param add The additional data field. |
| 116 | * \param add_len The length of additional data in Bytes. |
| 117 | * Must be less than 2^16 - 2^8. |
| 118 | * \param input The buffer holding the input data. |
| 119 | * \param output The buffer holding the output data. |
| 120 | * Must be at least \p length Bytes wide. |
| 121 | * \param tag The buffer holding the tag. |
| 122 | * \param tag_len The length of the tag to generate in Bytes: |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 123 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 124 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 125 | * \return \c 0 on success. |
Rose Zadik | ef87179 | 2018-04-17 10:41:48 +0100 | [diff] [blame] | 126 | * \return A CCM or cipher-specific error code on failure. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 127 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 129 | const unsigned char *iv, size_t iv_len, |
| 130 | const unsigned char *add, size_t add_len, |
| 131 | const unsigned char *input, unsigned char *output, |
| 132 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 133 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 134 | /** |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame^] | 135 | * \brief This function encrypts a buffer using CCM*. |
| 136 | * |
| 137 | * \note The tag is written to a separate buffer. To concatenate |
| 138 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 139 | * Counter with CBC-MAC (CCM)</em>, use |
| 140 | * \p tag = \p output + \p length, and make sure that the |
| 141 | * output buffer is at least \p length + \p tag_len wide. |
| 142 | * |
| 143 | * \note When using this function in a variable tag length context, |
| 144 | * the tag length has to be encoded into the \p iv passed to |
| 145 | * this function. |
| 146 | * |
| 147 | * \param ctx The CCM context to use for encryption. |
| 148 | * \param length The length of the input data in Bytes. |
| 149 | * \param iv Initialization vector (nonce). |
| 150 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 151 | * \param add The additional data field. |
| 152 | * \param add_len The length of additional data in Bytes. |
| 153 | * Must be less than 2^16 - 2^8. |
| 154 | * \param input The buffer holding the input data. |
| 155 | * \param output The buffer holding the output data. |
| 156 | * Must be at least \p length Bytes wide. |
| 157 | * \param tag The buffer holding the tag. |
| 158 | * \param tag_len The length of the tag to generate in Bytes: |
| 159 | * 0, 4, 6, 8, 10, 12, 14 or 16. |
| 160 | * |
| 161 | * \warning Passing 0 as \p tag_len means that the message is no |
| 162 | * longer authenticated. |
| 163 | * |
| 164 | * \return \c 0 on success. |
| 165 | * \return A CCM or cipher-specific error code on failure. |
| 166 | */ |
| 167 | int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
| 168 | const unsigned char *iv, size_t iv_len, |
| 169 | const unsigned char *add, size_t add_len, |
| 170 | const unsigned char *input, unsigned char *output, |
| 171 | unsigned char *tag, size_t tag_len ); |
| 172 | |
| 173 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 174 | * \brief This function performs a CCM authenticated decryption of a |
| 175 | * buffer. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 176 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 177 | * \param ctx The CCM context to use for decryption. |
| 178 | * \param length The length of the input data in Bytes. |
| 179 | * \param iv Initialization vector. |
| 180 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 181 | * \param add The additional data field. |
| 182 | * \param add_len The length of additional data in Bytes. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 183 | * Must be less than 2^16 - 2^8. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 184 | * \param input The buffer holding the input data. |
| 185 | * \param output The buffer holding the output data. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 186 | * Must be at least \p length Bytes wide. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 187 | * \param tag The buffer holding the tag. |
| 188 | * \param tag_len The length of the tag in Bytes. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 189 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 190 | * |
Rose Zadik | 379b95c | 2018-04-17 16:43:00 +0100 | [diff] [blame] | 191 | * \return \c 0 on success. This indicates that the message is authentic. |
| 192 | * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 193 | * \return A cipher-specific error code on calculation failure. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 194 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 196 | const unsigned char *iv, size_t iv_len, |
| 197 | const unsigned char *add, size_t add_len, |
| 198 | const unsigned char *input, unsigned char *output, |
| 199 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 200 | |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame^] | 201 | /** |
| 202 | * \brief This function performs a CCM* authenticated decryption of a |
| 203 | * buffer. |
| 204 | * |
| 205 | * \note When using this function in a variable tag length context, |
| 206 | * the tag length has to be decoded from \p iv and passed to |
| 207 | * this function as \p tag_len. (\p tag needs to be adjusted |
| 208 | * accordingly.) |
| 209 | * |
| 210 | * \param ctx The CCM context to use for decryption. |
| 211 | * \param length The length of the input data in Bytes. |
| 212 | * \param iv Initialization vector. |
| 213 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 214 | * \param add The additional data field. |
| 215 | * \param add_len The length of additional data in Bytes. |
| 216 | * Must be less than 2^16 - 2^8. |
| 217 | * \param input The buffer holding the input data. |
| 218 | * \param output The buffer holding the output data. |
| 219 | * Must be at least \p length Bytes wide. |
| 220 | * \param tag The buffer holding the tag. |
| 221 | * \param tag_len The length of the tag in Bytes. |
| 222 | * 0, 4, 6, 8, 10, 12, 14 or 16. |
| 223 | * |
| 224 | * \warning Passing 0 as \p tag_len means that the message is no |
| 225 | * longer authenticated. |
| 226 | * |
| 227 | * \return \c 0 on success. This indicates that the message is |
| 228 | * authentic. |
| 229 | * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 230 | * \return A cipher-specific error code on calculation failure. |
| 231 | */ |
| 232 | int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
| 233 | const unsigned char *iv, size_t iv_len, |
| 234 | const unsigned char *add, size_t add_len, |
| 235 | const unsigned char *input, unsigned char *output, |
| 236 | const unsigned char *tag, size_t tag_len ); |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 239 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 240 | * \brief The CCM checkup routine. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 241 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 242 | * \return \c 0 on success. |
| 243 | * \return \c 1 on failure. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 244 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | int mbedtls_ccm_self_test( int verbose ); |
| 246 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 247 | |
| 248 | #ifdef __cplusplus |
| 249 | } |
| 250 | #endif |
| 251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | #endif /* MBEDTLS_CCM_H */ |