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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 30 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 31 | * 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] | 32 | */ |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #ifndef MBEDTLS_CCM_H |
| 35 | #define MBEDTLS_CCM_H |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 36 | |
| 37 | #include "cipher.h" |
| 38 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 39 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ |
| 40 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
| 41 | #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] | 42 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 43 | #if !defined(MBEDTLS_CCM_ALT) |
| 44 | // Regular implementation |
| 45 | // |
| 46 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 51 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 52 | * \brief The CCM context-type definition. The CCM context is passed |
| 53 | * to the APIs called. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 54 | */ |
| 55 | typedef struct { |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 56 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 57 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | mbedtls_ccm_context; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 61 | * \brief This function initializes the specified CCM context, |
| 62 | * to make references valid, and prepare the context |
| 63 | * for mbedtls_ccm_setkey() or mbedtls_ccm_free(). |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 64 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 65 | * \param ctx The CCM context to initialize. |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 66 | */ |
| 67 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); |
| 68 | |
| 69 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 70 | * \brief This function initializes the CCM context set in the |
| 71 | * \p ctx parameter and sets the encryption key. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 72 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 73 | * \param ctx The CCM context to initialize. |
| 74 | * \param cipher The 128-bit block cipher to use. |
| 75 | * \param key The encryption key. |
| 76 | * \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] | 77 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 78 | * \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] | 79 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 80 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 81 | mbedtls_cipher_id_t cipher, |
| 82 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 83 | unsigned int keybits ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 84 | |
| 85 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 86 | * \brief This function releases and clears the specified CCM context |
| 87 | * and underlying cipher sub-context. |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 88 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 89 | * \param ctx The CCM context to clear. |
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 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 93 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 94 | * \brief This function encrypts a buffer using CCM. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 95 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 96 | * \param ctx The CCM context to use for encryption. |
| 97 | * \param length The length of the input data in Bytes. |
| 98 | * \param iv Initialization vector (nonce). |
| 99 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 100 | * \param add The additional data field. |
| 101 | * \param add_len The length of additional data in Bytes. |
| 102 | * Must be less than 2^16 - 2^8. |
| 103 | * \param input The buffer holding the input data. |
| 104 | * \param output The buffer holding the output data. |
| 105 | * Must be at least \p length Bytes wide. |
| 106 | * \param tag The buffer holding the tag. |
| 107 | * \param tag_len The length of the tag to generate in Bytes: |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame^] | 108 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 109 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 110 | * \note The tag is written to a separate buffer. To concatenate |
| 111 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 112 | * Counter with CBC-MAC (CCM)</em>, use |
| 113 | * \p tag = \p output + \p length, and make sure that the |
| 114 | * 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] | 115 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 116 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 117 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | 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] | 119 | const unsigned char *iv, size_t iv_len, |
| 120 | const unsigned char *add, size_t add_len, |
| 121 | const unsigned char *input, unsigned char *output, |
| 122 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 123 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 124 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 125 | * \brief This function performs a CCM authenticated decryption of a |
| 126 | * buffer. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 127 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 128 | * \param ctx The CCM context to use for decryption. |
| 129 | * \param length The length of the input data in Bytes. |
| 130 | * \param iv Initialization vector. |
| 131 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 132 | * \param add The additional data field. |
| 133 | * \param add_len The length of additional data in Bytes. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame^] | 134 | * Must be less than 2^16 - 2^8. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 135 | * \param input The buffer holding the input data. |
| 136 | * \param output The buffer holding the output data. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame^] | 137 | * Must be at least \p length Bytes wide. |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 138 | * \param tag The buffer holding the tag. |
| 139 | * \param tag_len The length of the tag in Bytes. |
Mathieu Briand | ffb6efd | 2018-02-07 10:29:27 +0100 | [diff] [blame^] | 140 | * 4, 6, 8, 10, 12, 14 or 16. |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 141 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 142 | * \return 0 if successful and authenticated, or |
| 143 | * #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] | 144 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | 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] | 146 | const unsigned char *iv, size_t iv_len, |
| 147 | const unsigned char *add, size_t add_len, |
| 148 | const unsigned char *input, unsigned char *output, |
| 149 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 150 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 151 | #ifdef __cplusplus |
| 152 | } |
| 153 | #endif |
| 154 | |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 155 | #else /* MBEDTLS_CCM_ALT */ |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 156 | #include "ccm_alt.h" |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 157 | #endif /* MBEDTLS_CCM_ALT */ |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 158 | |
| 159 | #ifdef __cplusplus |
| 160 | extern "C" { |
| 161 | #endif |
| 162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 164 | /** |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 165 | * \brief The CCM checkup routine. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 166 | * |
Rose Zadik | eecdbea | 2018-01-24 12:56:53 +0000 | [diff] [blame] | 167 | * \return \c 0 on success, or \c 1 on failure. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 168 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | int mbedtls_ccm_self_test( int verbose ); |
| 170 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 171 | |
| 172 | #ifdef __cplusplus |
| 173 | } |
| 174 | #endif |
| 175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | #endif /* MBEDTLS_CCM_H */ |