Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file ccm.h |
| 3 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 4 | * \brief CCM combines Counter mode encryption with CBC-MAC authentication |
| 5 | * for 128-bit block ciphers. |
| 6 | * |
| 7 | * Input to CCM includes the following elements: |
| 8 | * <ul><li>Payload - data that is both authenticated and encrypted.</li> |
| 9 | * <li>Associated data (Adata) - data that is authenticated but not |
| 10 | * encrypted, For example, a header.</li> |
| 11 | * <li>Nonce - A unique value that is assigned to the payload and the |
| 12 | * associated data.</li></ul> |
| 13 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 14 | */ |
| 15 | /* |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 16 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 17 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 18 | * |
| 19 | * This file is provided under the Apache License 2.0, or the |
| 20 | * GNU General Public License v2.0 or later. |
| 21 | * |
| 22 | * ********** |
| 23 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 24 | * |
| 25 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 26 | * not use this file except in compliance with the License. |
| 27 | * You may obtain a copy of the License at |
| 28 | * |
| 29 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 30 | * |
| 31 | * Unless required by applicable law or agreed to in writing, software |
| 32 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 33 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | * See the License for the specific language governing permissions and |
| 35 | * limitations under the License. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 36 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 37 | * ********** |
| 38 | * |
| 39 | * ********** |
| 40 | * GNU General Public License v2.0 or later: |
| 41 | * |
| 42 | * This program is free software; you can redistribute it and/or modify |
| 43 | * it under the terms of the GNU General Public License as published by |
| 44 | * the Free Software Foundation; either version 2 of the License, or |
| 45 | * (at your option) any later version. |
| 46 | * |
| 47 | * This program is distributed in the hope that it will be useful, |
| 48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 50 | * GNU General Public License for more details. |
| 51 | * |
| 52 | * You should have received a copy of the GNU General Public License along |
| 53 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 54 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 55 | * |
| 56 | * ********** |
| 57 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 58 | * 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] | 59 | */ |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #ifndef MBEDTLS_CCM_H |
| 62 | #define MBEDTLS_CCM_H |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 63 | |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 64 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 65 | #include "config.h" |
| 66 | #else |
| 67 | #include MBEDTLS_CONFIG_FILE |
| 68 | #endif |
| 69 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 70 | #include "cipher.h" |
| 71 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 72 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ |
| 73 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
| 74 | #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] | 75 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 76 | #if !defined(MBEDTLS_CCM_ALT) |
| 77 | // Regular implementation |
| 78 | // |
| 79 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 80 | #ifdef __cplusplus |
| 81 | extern "C" { |
| 82 | #endif |
| 83 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 84 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 85 | * \brief The CCM context-type definition. The CCM context is passed |
| 86 | * to the APIs called. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 87 | */ |
| 88 | typedef struct { |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 89 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 90 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | mbedtls_ccm_context; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 94 | * \brief This function initializes the specified CCM context, |
| 95 | * to make references valid, and prepare the context |
| 96 | * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 97 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 98 | * \param ctx The CCM context to initialize. |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 99 | */ |
| 100 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); |
| 101 | |
| 102 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 103 | * \brief This function initializes the CCM context set in the |
| 104 | * \p ctx parameter and sets the encryption key. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 105 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 106 | * \param ctx The CCM context to initialize. |
| 107 | * \param cipher The 128-bit block cipher to use. |
| 108 | * \param key The encryption key. |
| 109 | * \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] | 110 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 111 | * \return \c 0 on success, or a cipher-specific error code. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 112 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 113 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 114 | mbedtls_cipher_id_t cipher, |
| 115 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 116 | unsigned int keybits ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 117 | |
| 118 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 119 | * \brief This function releases and clears the specified CCM context |
| 120 | * and underlying cipher sub-context. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 121 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 122 | * \param ctx The CCM context to clear. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 123 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 125 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 126 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 127 | * \brief This function encrypts a buffer using CCM. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 128 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 129 | * \param ctx The CCM context to use for encryption. |
| 130 | * \param length The length of the input data in Bytes. |
| 131 | * \param iv Initialization vector (nonce). |
| 132 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 133 | * \param add The additional data field. |
| 134 | * \param add_len The length of additional data in Bytes. |
| 135 | * Must be less than 2^16 - 2^8. |
| 136 | * \param input The buffer holding the input data. |
| 137 | * \param output The buffer holding the output data. |
| 138 | * Must be at least \p length Bytes wide. |
| 139 | * \param tag The buffer holding the tag. |
| 140 | * \param tag_len The length of the tag to generate in Bytes: |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 141 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 142 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 143 | * \note The tag is written to a separate buffer. To concatenate |
| 144 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 145 | * Counter with CBC-MAC (CCM)</em>, use |
| 146 | * \p tag = \p output + \p length, and make sure that the |
| 147 | * output buffer is at least \p length + \p tag_len wide. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 148 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 149 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 150 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | 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] | 152 | const unsigned char *iv, size_t iv_len, |
| 153 | const unsigned char *add, size_t add_len, |
| 154 | const unsigned char *input, unsigned char *output, |
| 155 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 156 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 157 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 158 | * \brief This function performs a CCM authenticated decryption of a |
| 159 | * buffer. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 160 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 161 | * \param ctx The CCM context to use for decryption. |
| 162 | * \param length The length of the input data in Bytes. |
| 163 | * \param iv Initialization vector. |
| 164 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 165 | * \param add The additional data field. |
| 166 | * \param add_len The length of additional data in Bytes. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 167 | * Must be less than 2^16 - 2^8. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 168 | * \param input The buffer holding the input data. |
| 169 | * \param output The buffer holding the output data. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 170 | * Must be at least \p length Bytes wide. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 171 | * \param tag The buffer holding the tag. |
| 172 | * \param tag_len The length of the tag in Bytes. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame] | 173 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 174 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 175 | * \return 0 if successful and authenticated, or |
| 176 | * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 177 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | 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] | 179 | const unsigned char *iv, size_t iv_len, |
| 180 | const unsigned char *add, size_t add_len, |
| 181 | const unsigned char *input, unsigned char *output, |
| 182 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 183 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 184 | #ifdef __cplusplus |
| 185 | } |
| 186 | #endif |
| 187 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 188 | #else /* MBEDTLS_CCM_ALT */ |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 189 | #include "ccm_alt.h" |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 190 | #endif /* MBEDTLS_CCM_ALT */ |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 191 | |
| 192 | #ifdef __cplusplus |
| 193 | extern "C" { |
| 194 | #endif |
| 195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 197 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 198 | * \brief The CCM checkup routine. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 199 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 200 | * \return \c 0 on success, or \c 1 on failure. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 201 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | int mbedtls_ccm_self_test( int verbose ); |
| 203 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 204 | |
| 205 | #ifdef __cplusplus |
| 206 | } |
| 207 | #endif |
| 208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | #endif /* MBEDTLS_CCM_H */ |