Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file ccm.h |
| 3 | * |
| 4 | * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 21 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | * 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] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_CCM_H |
| 25 | #define MBEDTLS_CCM_H |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 26 | |
| 27 | #include "cipher.h" |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */ |
| 30 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 31 | #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] | 32 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_CCM_ALT) |
| 34 | // Regular implementation |
| 35 | // |
| 36 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 41 | /** |
| 42 | * \brief CCM context structure |
| 43 | */ |
| 44 | typedef struct { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 46 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | mbedtls_ccm_context; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 50 | * \brief Initialize CCM context (just makes references valid) |
| 51 | * Makes the context ready for mbedtls_ccm_setkey() or |
| 52 | * mbedtls_ccm_free(). |
| 53 | * |
| 54 | * \param ctx CCM context to initialize |
| 55 | */ |
| 56 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); |
| 57 | |
| 58 | /** |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 59 | * \brief CCM initialization (encryption and decryption) |
| 60 | * |
| 61 | * \param ctx CCM context to be initialized |
| 62 | * \param cipher cipher to use (a 128-bit block cipher) |
| 63 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 64 | * \param keybits key size in bits (must be acceptable by the cipher) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 65 | * |
| 66 | * \return 0 if successful, or a cipher specific error code |
| 67 | */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 68 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
| 69 | mbedtls_cipher_id_t cipher, |
| 70 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 71 | unsigned int keybits ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * \brief Free a CCM context and underlying cipher sub-context |
| 75 | * |
| 76 | * \param ctx CCM context to free |
| 77 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 79 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 80 | /** |
| 81 | * \brief CCM buffer encryption |
| 82 | * |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 83 | * \param ctx CCM context |
| 84 | * \param length length of the input data in bytes |
| 85 | * \param iv nonce (initialization vector) |
| 86 | * \param iv_len length of IV in bytes |
| 87 | * must be 2, 3, 4, 5, 6, 7 or 8 |
| 88 | * \param add additional data |
| 89 | * \param add_len length of additional data in bytes |
| 90 | * must be less than 2^16 - 2^8 |
| 91 | * \param input buffer holding the input data |
| 92 | * \param output buffer for holding the output data |
| 93 | * must be at least 'length' bytes wide |
| 94 | * \param tag buffer for holding the tag |
| 95 | * \param tag_len length of the tag to generate in bytes |
| 96 | * must be 4, 6, 8, 10, 14 or 16 |
| 97 | * |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 98 | * \note The tag is written to a separate buffer. To get the tag |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 99 | * concatenated with the output as in the CCM spec, use |
| 100 | * tag = output + length and make sure the output buffer is |
| 101 | * at least length + tag_len wide. |
| 102 | * |
| 103 | * \return 0 if successful |
| 104 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | 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] | 106 | const unsigned char *iv, size_t iv_len, |
| 107 | const unsigned char *add, size_t add_len, |
| 108 | const unsigned char *input, unsigned char *output, |
| 109 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 111 | /** |
| 112 | * \brief CCM buffer authenticated decryption |
| 113 | * |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 114 | * \param ctx CCM context |
| 115 | * \param length length of the input data |
| 116 | * \param iv initialization vector |
| 117 | * \param iv_len length of IV |
| 118 | * \param add additional data |
| 119 | * \param add_len length of additional data |
| 120 | * \param input buffer holding the input data |
| 121 | * \param output buffer for holding the output data |
| 122 | * \param tag buffer holding the tag |
| 123 | * \param tag_len length of the tag |
| 124 | * |
| 125 | * \return 0 if successful and authenticated, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 127 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | 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] | 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 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 133 | |
Steven Cooreman | 222e2ff | 2017-04-04 11:37:15 +0200 | [diff] [blame] | 134 | #ifdef __cplusplus |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #else /* !MBEDTLS_CCM_ALT */ |
| 139 | #include "ccm_alt.h" |
| 140 | #endif /* !MBEDTLS_CCM_ALT */ |
| 141 | |
| 142 | #ifdef __cplusplus |
| 143 | extern "C" { |
| 144 | #endif |
| 145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 147 | /** |
| 148 | * \brief Checkup routine |
| 149 | * |
| 150 | * \return 0 if successful, or 1 if the test failed |
| 151 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | int mbedtls_ccm_self_test( int verbose ); |
| 153 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 154 | |
| 155 | #ifdef __cplusplus |
| 156 | } |
| 157 | #endif |
| 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_CCM_H */ |