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 | * |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 17 | * Definition of CCM: |
| 18 | * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf |
| 19 | * RFC 3610 "Counter with CBC-MAC (CCM)" |
| 20 | * |
| 21 | * Related: |
| 22 | * RFC 5116 "An Interface and Algorithms for Authenticated Encryption" |
| 23 | * |
| 24 | * Definition of CCM*: |
| 25 | * IEEE 802.15.4 - IEEE Standard for Local and metropolitan area networks |
| 26 | * Integer representation is fixed most-significant-octet-first order and |
| 27 | * the representation of octets is most-significant-bit-first order. This is |
| 28 | * consistent with RFC 3610. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 29 | */ |
| 30 | /* |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 31 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 32 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 33 | * |
| 34 | * This file is provided under the Apache License 2.0, or the |
| 35 | * GNU General Public License v2.0 or later. |
| 36 | * |
| 37 | * ********** |
| 38 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 39 | * |
| 40 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 41 | * not use this file except in compliance with the License. |
| 42 | * You may obtain a copy of the License at |
| 43 | * |
| 44 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 45 | * |
| 46 | * Unless required by applicable law or agreed to in writing, software |
| 47 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 48 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 49 | * See the License for the specific language governing permissions and |
| 50 | * limitations under the License. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 51 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 52 | * ********** |
| 53 | * |
| 54 | * ********** |
| 55 | * GNU General Public License v2.0 or later: |
| 56 | * |
| 57 | * This program is free software; you can redistribute it and/or modify |
| 58 | * it under the terms of the GNU General Public License as published by |
| 59 | * the Free Software Foundation; either version 2 of the License, or |
| 60 | * (at your option) any later version. |
| 61 | * |
| 62 | * This program is distributed in the hope that it will be useful, |
| 63 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 64 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 65 | * GNU General Public License for more details. |
| 66 | * |
| 67 | * You should have received a copy of the GNU General Public License along |
| 68 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 69 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 70 | * |
| 71 | * ********** |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 72 | */ |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | #ifndef MBEDTLS_CCM_H |
| 75 | #define MBEDTLS_CCM_H |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 76 | |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 77 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 78 | #include "config.h" |
| 79 | #else |
| 80 | #include MBEDTLS_CONFIG_FILE |
| 81 | #endif |
| 82 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 83 | #include "cipher.h" |
| 84 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 85 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ |
| 86 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 87 | |
| 88 | /* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */ |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 89 | #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] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 95 | #if !defined(MBEDTLS_CCM_ALT) |
| 96 | // Regular implementation |
| 97 | // |
| 98 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 99 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 100 | * \brief The CCM context-type definition. The CCM context is passed |
| 101 | * to the APIs called. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 102 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 103 | typedef struct mbedtls_ccm_context |
| 104 | { |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 105 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 106 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | mbedtls_ccm_context; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 108 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 109 | #else /* MBEDTLS_CCM_ALT */ |
| 110 | #include "ccm_alt.h" |
| 111 | #endif /* MBEDTLS_CCM_ALT */ |
| 112 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 113 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 114 | * \brief This function initializes the specified CCM context, |
| 115 | * to make references valid, and prepare the context |
| 116 | * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 117 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 118 | * \param ctx The CCM context to initialize. This must not be \c NULL. |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 119 | */ |
| 120 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); |
| 121 | |
| 122 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 123 | * \brief This function initializes the CCM context set in the |
| 124 | * \p ctx parameter and sets the encryption key. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 125 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 126 | * \param ctx The CCM context to initialize. This must be an initialized |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 127 | * context. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 128 | * \param cipher The 128-bit block cipher to use. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 129 | * \param key The encryption key. This must not be \c NULL. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 130 | * \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] | 131 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 132 | * \return \c 0 on success. |
Rose Zadik | ef87179 | 2018-04-17 10:41:48 +0100 | [diff] [blame] | 133 | * \return A CCM or cipher-specific error code on failure. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 134 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 135 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 136 | mbedtls_cipher_id_t cipher, |
| 137 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 138 | unsigned int keybits ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 139 | |
| 140 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 141 | * \brief This function releases and clears the specified CCM context |
| 142 | * and underlying cipher sub-context. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 143 | * |
k-stachowiak | 9da5d7c | 2018-12-13 17:19:48 +0100 | [diff] [blame] | 144 | * \param ctx The CCM context to clear. If this is \c NULL, the function |
| 145 | * has no effect. Otherwise, this must be initialized. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 146 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 149 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 150 | * \brief This function encrypts a buffer using CCM. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 151 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 152 | * \note The tag is written to a separate buffer. To concatenate |
| 153 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 154 | * Counter with CBC-MAC (CCM)</em>, use |
| 155 | * \p tag = \p output + \p length, and make sure that the |
| 156 | * output buffer is at least \p length + \p tag_len wide. |
| 157 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 158 | * \param ctx The CCM context to use for encryption. This must be |
| 159 | * initialized and bound to a key. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 160 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 161 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 162 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 163 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 164 | * or 13. The length L of the message length field is |
| 165 | * 15 - \p iv_len. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 166 | * \param add The additional data field. If \p add_len is greater than |
| 167 | * zero, \p add must be a readable buffer of at least that |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 168 | * length. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 169 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 170 | * This must be less than `2^16 - 2^8`. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 171 | * \param input The buffer holding the input data. If \p length is greater |
| 172 | * than zero, \p input must be a readable buffer of at least |
| 173 | * that length. |
| 174 | * \param output The buffer holding the output data. If \p length is greater |
| 175 | * than zero, \p output must be a writable buffer of at least |
| 176 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 177 | * \param tag The buffer holding the authentication field. This must be a |
Yonatan Goldschmidt | f9604bb | 2020-09-12 00:19:52 +0300 | [diff] [blame^] | 178 | * writable buffer of at least \p tag_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 179 | * \param tag_len The length of the authentication field to generate in Bytes: |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 180 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 181 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 182 | * \return \c 0 on success. |
Rose Zadik | ef87179 | 2018-04-17 10:41:48 +0100 | [diff] [blame] | 183 | * \return A CCM or cipher-specific error code on failure. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 184 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | 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] | 186 | const unsigned char *iv, size_t iv_len, |
| 187 | const unsigned char *add, size_t add_len, |
| 188 | const unsigned char *input, unsigned char *output, |
| 189 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 190 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 191 | /** |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 192 | * \brief This function encrypts a buffer using CCM*. |
| 193 | * |
| 194 | * \note The tag is written to a separate buffer. To concatenate |
| 195 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 196 | * Counter with CBC-MAC (CCM)</em>, use |
| 197 | * \p tag = \p output + \p length, and make sure that the |
| 198 | * output buffer is at least \p length + \p tag_len wide. |
| 199 | * |
| 200 | * \note When using this function in a variable tag length context, |
| 201 | * the tag length has to be encoded into the \p iv passed to |
| 202 | * this function. |
| 203 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 204 | * \param ctx The CCM context to use for encryption. This must be |
| 205 | * initialized and bound to a key. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 206 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 207 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 208 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 209 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 210 | * or 13. The length L of the message length field is |
| 211 | * 15 - \p iv_len. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 212 | * \param add The additional data field. This must be a readable buffer of |
| 213 | * at least \p add_len Bytes. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 214 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 215 | * This must be less than 2^16 - 2^8. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 216 | * \param input The buffer holding the input data. If \p length is greater |
| 217 | * than zero, \p input must be a readable buffer of at least |
| 218 | * that length. |
| 219 | * \param output The buffer holding the output data. If \p length is greater |
| 220 | * than zero, \p output must be a writable buffer of at least |
| 221 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 222 | * \param tag The buffer holding the authentication field. This must be a |
Yonatan Goldschmidt | f9604bb | 2020-09-12 00:19:52 +0300 | [diff] [blame^] | 223 | * writable buffer of at least \p tag_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 224 | * \param tag_len The length of the authentication field to generate in Bytes: |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 225 | * 0, 4, 6, 8, 10, 12, 14 or 16. |
| 226 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 227 | * \warning Passing \c 0 as \p tag_len means that the message is no |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 228 | * longer authenticated. |
| 229 | * |
| 230 | * \return \c 0 on success. |
| 231 | * \return A CCM or cipher-specific error code on failure. |
| 232 | */ |
| 233 | int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
| 234 | const unsigned char *iv, size_t iv_len, |
| 235 | const unsigned char *add, size_t add_len, |
| 236 | const unsigned char *input, unsigned char *output, |
| 237 | unsigned char *tag, size_t tag_len ); |
| 238 | |
| 239 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 240 | * \brief This function performs a CCM authenticated decryption of a |
| 241 | * buffer. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 242 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 243 | * \param ctx The CCM context to use for decryption. This must be |
| 244 | * initialized and bound to a key. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 245 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 246 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 247 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 248 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 249 | * or 13. The length L of the message length field is |
| 250 | * 15 - \p iv_len. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 251 | * \param add The additional data field. This must be a readable buffer |
| 252 | * of at least that \p add_len Bytes.. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 253 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 254 | * This must be less than 2^16 - 2^8. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 255 | * \param input The buffer holding the input data. If \p length is greater |
| 256 | * than zero, \p input must be a readable buffer of at least |
| 257 | * that length. |
| 258 | * \param output The buffer holding the output data. If \p length is greater |
| 259 | * than zero, \p output must be a writable buffer of at least |
| 260 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 261 | * \param tag The buffer holding the authentication field. This must be a |
| 262 | * readable buffer of at least \p tag_len Bytes. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 263 | * \param tag_len The length of the authentication field to generate in Bytes: |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 264 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 265 | * |
Rose Zadik | 379b95c | 2018-04-17 16:43:00 +0100 | [diff] [blame] | 266 | * \return \c 0 on success. This indicates that the message is authentic. |
| 267 | * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 268 | * \return A cipher-specific error code on calculation failure. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 269 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | 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] | 271 | const unsigned char *iv, size_t iv_len, |
| 272 | const unsigned char *add, size_t add_len, |
| 273 | const unsigned char *input, unsigned char *output, |
| 274 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 275 | |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 276 | /** |
| 277 | * \brief This function performs a CCM* authenticated decryption of a |
| 278 | * buffer. |
| 279 | * |
| 280 | * \note When using this function in a variable tag length context, |
| 281 | * the tag length has to be decoded from \p iv and passed to |
| 282 | * this function as \p tag_len. (\p tag needs to be adjusted |
| 283 | * accordingly.) |
| 284 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 285 | * \param ctx The CCM context to use for decryption. This must be |
| 286 | * initialized and bound to a key. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 287 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 288 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 289 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 290 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 291 | * or 13. The length L of the message length field is |
| 292 | * 15 - \p iv_len. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 293 | * \param add The additional data field. This must be a readable buffer of |
| 294 | * at least that \p add_len Bytes. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 295 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 296 | * This must be less than 2^16 - 2^8. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 297 | * \param input The buffer holding the input data. If \p length is greater |
| 298 | * than zero, \p input must be a readable buffer of at least |
| 299 | * that length. |
| 300 | * \param output The buffer holding the output data. If \p length is greater |
| 301 | * than zero, \p output must be a writable buffer of at least |
| 302 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 303 | * \param tag The buffer holding the authentication field. This must be a |
| 304 | * readable buffer of at least \p tag_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 305 | * \param tag_len The length of the authentication field in Bytes. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 306 | * 0, 4, 6, 8, 10, 12, 14 or 16. |
| 307 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 308 | * \warning Passing \c 0 as \p tag_len means that the message is nos |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 309 | * longer authenticated. |
| 310 | * |
Janos Follath | 143b319 | 2018-05-30 13:57:29 +0100 | [diff] [blame] | 311 | * \return \c 0 on success. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 312 | * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 313 | * \return A cipher-specific error code on calculation failure. |
| 314 | */ |
| 315 | int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
| 316 | const unsigned char *iv, size_t iv_len, |
| 317 | const unsigned char *add, size_t add_len, |
| 318 | const unsigned char *input, unsigned char *output, |
| 319 | const unsigned char *tag, size_t tag_len ); |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 322 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 323 | * \brief The CCM checkup routine. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 324 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 325 | * \return \c 0 on success. |
| 326 | * \return \c 1 on failure. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 327 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | int mbedtls_ccm_self_test( int verbose ); |
| 329 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 330 | |
| 331 | #ifdef __cplusplus |
| 332 | } |
| 333 | #endif |
| 334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | #endif /* MBEDTLS_CCM_H */ |