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 | /* |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 31 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
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 | * ********** |
| 72 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 73 | * 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] | 74 | */ |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | #ifndef MBEDTLS_CCM_H |
| 77 | #define MBEDTLS_CCM_H |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 78 | |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 79 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 80 | #include "config.h" |
| 81 | #else |
| 82 | #include MBEDTLS_CONFIG_FILE |
| 83 | #endif |
| 84 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 85 | #include "cipher.h" |
| 86 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 87 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ |
| 88 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 89 | |
| 90 | /* 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] | 91 | #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] | 92 | |
| 93 | #ifdef __cplusplus |
| 94 | extern "C" { |
| 95 | #endif |
| 96 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 97 | #if !defined(MBEDTLS_CCM_ALT) |
| 98 | // Regular implementation |
| 99 | // |
| 100 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 101 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 102 | * \brief The CCM context-type definition. The CCM context is passed |
| 103 | * to the APIs called. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 104 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 105 | typedef struct mbedtls_ccm_context |
| 106 | { |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 107 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 108 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_ccm_context; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 110 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 111 | #else /* MBEDTLS_CCM_ALT */ |
| 112 | #include "ccm_alt.h" |
| 113 | #endif /* MBEDTLS_CCM_ALT */ |
| 114 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 115 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 116 | * \brief This function initializes the specified CCM context, |
| 117 | * to make references valid, and prepare the context |
| 118 | * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 119 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 120 | * \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] | 121 | */ |
| 122 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); |
| 123 | |
| 124 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 125 | * \brief This function initializes the CCM context set in the |
| 126 | * \p ctx parameter and sets the encryption key. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 127 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 128 | * \param ctx The CCM context to initialize. This must be an initialized |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 129 | * context. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 130 | * \param cipher The 128-bit block cipher to use. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 131 | * \param key The encryption key. This must not be \c NULL. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 132 | * \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] | 133 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 134 | * \return \c 0 on success. |
Rose Zadik | ef87179 | 2018-04-17 10:41:48 +0100 | [diff] [blame] | 135 | * \return A CCM or cipher-specific error code on failure. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 136 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 137 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 138 | mbedtls_cipher_id_t cipher, |
| 139 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 140 | unsigned int keybits ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 143 | * \brief This function releases and clears the specified CCM context |
| 144 | * and underlying cipher sub-context. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 145 | * |
k-stachowiak | 9da5d7c | 2018-12-13 17:19:48 +0100 | [diff] [blame] | 146 | * \param ctx The CCM context to clear. If this is \c NULL, the function |
| 147 | * has no effect. Otherwise, this must be initialized. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 148 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 151 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 152 | * \brief This function encrypts a buffer using CCM. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 153 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 154 | * \note The tag is written to a separate buffer. To concatenate |
| 155 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 156 | * Counter with CBC-MAC (CCM)</em>, use |
| 157 | * \p tag = \p output + \p length, and make sure that the |
| 158 | * output buffer is at least \p length + \p tag_len wide. |
| 159 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 160 | * \param ctx The CCM context to use for encryption. This must be |
| 161 | * initialized and bound to a key. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 162 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 163 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 164 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 165 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 166 | * or 13. The length L of the message length field is |
| 167 | * 15 - \p iv_len. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 168 | * \param add The additional data field. If \p add_len is greater than |
| 169 | * zero, \p add must be a readable buffer of at least that |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 170 | * length. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 171 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 172 | * This must be less than `2^16 - 2^8`. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 173 | * \param input The buffer holding the input data. If \p length is greater |
| 174 | * than zero, \p input must be a readable buffer of at least |
| 175 | * that length. |
| 176 | * \param output The buffer holding the output data. If \p length is greater |
| 177 | * than zero, \p output must be a writable buffer of at least |
| 178 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 179 | * \param tag The buffer holding the authentication field. This must be a |
| 180 | * readable buffer of at least \p tag_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 181 | * \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] | 182 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 183 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 184 | * \return \c 0 on success. |
Rose Zadik | ef87179 | 2018-04-17 10:41:48 +0100 | [diff] [blame] | 185 | * \return A CCM or cipher-specific error code on failure. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 186 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | 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] | 188 | const unsigned char *iv, size_t iv_len, |
| 189 | const unsigned char *add, size_t add_len, |
| 190 | const unsigned char *input, unsigned char *output, |
| 191 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 192 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 193 | /** |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 194 | * \brief This function encrypts a buffer using CCM*. |
| 195 | * |
| 196 | * \note The tag is written to a separate buffer. To concatenate |
| 197 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 198 | * Counter with CBC-MAC (CCM)</em>, use |
| 199 | * \p tag = \p output + \p length, and make sure that the |
| 200 | * output buffer is at least \p length + \p tag_len wide. |
| 201 | * |
| 202 | * \note When using this function in a variable tag length context, |
| 203 | * the tag length has to be encoded into the \p iv passed to |
| 204 | * this function. |
| 205 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 206 | * \param ctx The CCM context to use for encryption. This must be |
| 207 | * initialized and bound to a key. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 208 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 209 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 210 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 211 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 212 | * or 13. The length L of the message length field is |
| 213 | * 15 - \p iv_len. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 214 | * \param add The additional data field. This must be a readable buffer of |
| 215 | * at least \p add_len Bytes. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 216 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 217 | * This must be less than 2^16 - 2^8. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 218 | * \param input The buffer holding the input data. If \p length is greater |
| 219 | * than zero, \p input must be a readable buffer of at least |
| 220 | * that length. |
| 221 | * \param output The buffer holding the output data. If \p length is greater |
| 222 | * than zero, \p output must be a writable buffer of at least |
| 223 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 224 | * \param tag The buffer holding the authentication field. This must be a |
| 225 | * readable buffer of at least \p tag_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 226 | * \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] | 227 | * 0, 4, 6, 8, 10, 12, 14 or 16. |
| 228 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 229 | * \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] | 230 | * longer authenticated. |
| 231 | * |
| 232 | * \return \c 0 on success. |
| 233 | * \return A CCM or cipher-specific error code on failure. |
| 234 | */ |
| 235 | int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
| 236 | const unsigned char *iv, size_t iv_len, |
| 237 | const unsigned char *add, size_t add_len, |
| 238 | const unsigned char *input, unsigned char *output, |
| 239 | unsigned char *tag, size_t tag_len ); |
| 240 | |
| 241 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 242 | * \brief This function performs a CCM authenticated decryption of a |
| 243 | * buffer. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 244 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 245 | * \param ctx The CCM context to use for decryption. This must be |
| 246 | * initialized and bound to a key. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 247 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 248 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 249 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 250 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 251 | * or 13. The length L of the message length field is |
| 252 | * 15 - \p iv_len. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 253 | * \param add The additional data field. This must be a readable buffer |
| 254 | * of at least that \p add_len Bytes.. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 255 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 256 | * This must be less than 2^16 - 2^8. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 257 | * \param input The buffer holding the input data. If \p length is greater |
| 258 | * than zero, \p input must be a readable buffer of at least |
| 259 | * that length. |
| 260 | * \param output The buffer holding the output data. If \p length is greater |
| 261 | * than zero, \p output must be a writable buffer of at least |
| 262 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 263 | * \param tag The buffer holding the authentication field. This must be a |
| 264 | * readable buffer of at least \p tag_len Bytes. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 265 | * \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] | 266 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 267 | * |
Rose Zadik | 379b95c | 2018-04-17 16:43:00 +0100 | [diff] [blame] | 268 | * \return \c 0 on success. This indicates that the message is authentic. |
| 269 | * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 270 | * \return A cipher-specific error code on calculation failure. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 271 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | 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] | 273 | const unsigned char *iv, size_t iv_len, |
| 274 | const unsigned char *add, size_t add_len, |
| 275 | const unsigned char *input, unsigned char *output, |
| 276 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 277 | |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 278 | /** |
| 279 | * \brief This function performs a CCM* authenticated decryption of a |
| 280 | * buffer. |
| 281 | * |
| 282 | * \note When using this function in a variable tag length context, |
| 283 | * the tag length has to be decoded from \p iv and passed to |
| 284 | * this function as \p tag_len. (\p tag needs to be adjusted |
| 285 | * accordingly.) |
| 286 | * |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 287 | * \param ctx The CCM context to use for decryption. This must be |
| 288 | * initialized and bound to a key. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 289 | * \param length The length of the input data in Bytes. |
k-stachowiak | 6adb057 | 2018-12-18 10:22:34 +0100 | [diff] [blame] | 290 | * \param iv The initialization vector (nonce). This must be a readable |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 291 | * buffer of at least \p iv_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 292 | * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, |
| 293 | * or 13. The length L of the message length field is |
| 294 | * 15 - \p iv_len. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 295 | * \param add The additional data field. This must be a readable buffer of |
| 296 | * at least that \p add_len Bytes. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 297 | * \param add_len The length of additional data in Bytes. |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 298 | * This must be less than 2^16 - 2^8. |
k-stachowiak | 12f0d5c | 2018-12-11 16:25:08 +0100 | [diff] [blame] | 299 | * \param input The buffer holding the input data. If \p length is greater |
| 300 | * than zero, \p input must be a readable buffer of at least |
| 301 | * that length. |
| 302 | * \param output The buffer holding the output data. If \p length is greater |
| 303 | * than zero, \p output must be a writable buffer of at least |
| 304 | * that length. |
k-stachowiak | 247a782 | 2018-12-19 13:36:03 +0100 | [diff] [blame] | 305 | * \param tag The buffer holding the authentication field. This must be a |
| 306 | * readable buffer of at least \p tag_len Bytes. |
Janos Follath | 6b4bd3d | 2018-05-29 11:30:26 +0100 | [diff] [blame] | 307 | * \param tag_len The length of the authentication field in Bytes. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 308 | * 0, 4, 6, 8, 10, 12, 14 or 16. |
| 309 | * |
k-stachowiak | b92f933 | 2018-12-13 11:13:51 +0100 | [diff] [blame] | 310 | * \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] | 311 | * longer authenticated. |
| 312 | * |
Janos Follath | 143b319 | 2018-05-30 13:57:29 +0100 | [diff] [blame] | 313 | * \return \c 0 on success. |
Janos Follath | 5dc8cfa | 2018-04-27 14:45:49 +0100 | [diff] [blame] | 314 | * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 315 | * \return A cipher-specific error code on calculation failure. |
| 316 | */ |
| 317 | int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
| 318 | const unsigned char *iv, size_t iv_len, |
| 319 | const unsigned char *add, size_t add_len, |
| 320 | const unsigned char *input, unsigned char *output, |
| 321 | const unsigned char *tag, size_t tag_len ); |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 324 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 325 | * \brief The CCM checkup routine. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 326 | * |
Rose Zadik | 4ee9d24 | 2018-03-26 17:18:44 +0100 | [diff] [blame] | 327 | * \return \c 0 on success. |
| 328 | * \return \c 1 on failure. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 329 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | int mbedtls_ccm_self_test( int verbose ); |
| 331 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 332 | |
| 333 | #ifdef __cplusplus |
| 334 | } |
| 335 | #endif |
| 336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | #endif /* MBEDTLS_CCM_H */ |