Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * \file ccm.h |
| 3 | * |
| 4 | * \brief This file provides an API for the CCM authenticated encryption |
| 5 | * mode for block ciphers. |
| 6 | * |
| 7 | * CCM combines Counter mode encryption with CBC-MAC authentication |
| 8 | * for 128-bit block ciphers. |
| 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 | * |
| 17 | */ |
| 18 | /* |
| 19 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 20 | * SPDX-License-Identifier: Apache-2.0 |
| 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 23 | * not use this file except in compliance with the License. |
| 24 | * You may obtain a copy of the License at |
| 25 | * |
| 26 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | * |
| 28 | * Unless required by applicable law or agreed to in writing, software |
| 29 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 30 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | * See the License for the specific language governing permissions and |
| 32 | * limitations under the License. |
| 33 | * |
| 34 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 35 | */ |
| 36 | |
| 37 | #ifndef MBEDCRYPTO_CCM_H |
| 38 | #define MBEDCRYPTO_CCM_H |
| 39 | |
| 40 | #include "cipher.h" |
| 41 | |
| 42 | #define MBEDCRYPTO_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */ |
| 43 | #define MBEDCRYPTO_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
| 44 | #define MBEDCRYPTO_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */ |
| 45 | |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | #if !defined(MBEDCRYPTO_CCM_ALT) |
| 52 | // Regular implementation |
| 53 | // |
| 54 | |
| 55 | /** |
| 56 | * \brief The CCM context-type definition. The CCM context is passed |
| 57 | * to the APIs called. |
| 58 | */ |
| 59 | typedef struct { |
| 60 | mbedcrypto_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
| 61 | } |
| 62 | mbedcrypto_ccm_context; |
| 63 | |
| 64 | #else /* MBEDCRYPTO_CCM_ALT */ |
| 65 | #include "ccm_alt.h" |
| 66 | #endif /* MBEDCRYPTO_CCM_ALT */ |
| 67 | |
| 68 | /** |
| 69 | * \brief This function initializes the specified CCM context, |
| 70 | * to make references valid, and prepare the context |
| 71 | * for mbedcrypto_ccm_setkey() or mbedcrypto_ccm_free(). |
| 72 | * |
| 73 | * \param ctx The CCM context to initialize. |
| 74 | */ |
| 75 | void mbedcrypto_ccm_init( mbedcrypto_ccm_context *ctx ); |
| 76 | |
| 77 | /** |
| 78 | * \brief This function initializes the CCM context set in the |
| 79 | * \p ctx parameter and sets the encryption key. |
| 80 | * |
| 81 | * \param ctx The CCM context to initialize. |
| 82 | * \param cipher The 128-bit block cipher to use. |
| 83 | * \param key The encryption key. |
| 84 | * \param keybits The key size in bits. This must be acceptable by the cipher. |
| 85 | * |
| 86 | * \return \c 0 on success. |
| 87 | * \return A CCM or cipher-specific error code on failure. |
| 88 | */ |
| 89 | int mbedcrypto_ccm_setkey( mbedcrypto_ccm_context *ctx, |
| 90 | mbedcrypto_cipher_id_t cipher, |
| 91 | const unsigned char *key, |
| 92 | unsigned int keybits ); |
| 93 | |
| 94 | /** |
| 95 | * \brief This function releases and clears the specified CCM context |
| 96 | * and underlying cipher sub-context. |
| 97 | * |
| 98 | * \param ctx The CCM context to clear. |
| 99 | */ |
| 100 | void mbedcrypto_ccm_free( mbedcrypto_ccm_context *ctx ); |
| 101 | |
| 102 | /** |
| 103 | * \brief This function encrypts a buffer using CCM. |
| 104 | * |
| 105 | * |
| 106 | * \note The tag is written to a separate buffer. To concatenate |
| 107 | * the \p tag with the \p output, as done in <em>RFC-3610: |
| 108 | * Counter with CBC-MAC (CCM)</em>, use |
| 109 | * \p tag = \p output + \p length, and make sure that the |
| 110 | * output buffer is at least \p length + \p tag_len wide. |
| 111 | * |
| 112 | * \param ctx The CCM context to use for encryption. |
| 113 | * \param length The length of the input data in Bytes. |
| 114 | * \param iv Initialization vector (nonce). |
| 115 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 116 | * \param add The additional data field. |
| 117 | * \param add_len The length of additional data in Bytes. |
| 118 | * Must be less than 2^16 - 2^8. |
| 119 | * \param input The buffer holding the input data. |
| 120 | * \param output The buffer holding the output data. |
| 121 | * Must be at least \p length Bytes wide. |
| 122 | * \param tag The buffer holding the tag. |
| 123 | * \param tag_len The length of the tag to generate in Bytes: |
| 124 | * 4, 6, 8, 10, 12, 14 or 16. |
| 125 | * |
| 126 | * \return \c 0 on success. |
| 127 | * \return A CCM or cipher-specific error code on failure. |
| 128 | */ |
| 129 | int mbedcrypto_ccm_encrypt_and_tag( mbedcrypto_ccm_context *ctx, size_t length, |
| 130 | const unsigned char *iv, size_t iv_len, |
| 131 | const unsigned char *add, size_t add_len, |
| 132 | const unsigned char *input, unsigned char *output, |
| 133 | unsigned char *tag, size_t tag_len ); |
| 134 | |
| 135 | /** |
| 136 | * \brief This function performs a CCM authenticated decryption of a |
| 137 | * buffer. |
| 138 | * |
| 139 | * \param ctx The CCM context to use for decryption. |
| 140 | * \param length The length of the input data in Bytes. |
| 141 | * \param iv Initialization vector. |
| 142 | * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13. |
| 143 | * \param add The additional data field. |
| 144 | * \param add_len The length of additional data in Bytes. |
| 145 | * Must be less than 2^16 - 2^8. |
| 146 | * \param input The buffer holding the input data. |
| 147 | * \param output The buffer holding the output data. |
| 148 | * Must be at least \p length Bytes wide. |
| 149 | * \param tag The buffer holding the tag. |
| 150 | * \param tag_len The length of the tag in Bytes. |
| 151 | * 4, 6, 8, 10, 12, 14 or 16. |
| 152 | * |
| 153 | * \return \c 0 on success. This indicates that the message is authentic. |
| 154 | * \return #MBEDCRYPTO_ERR_CCM_AUTH_FAILED if the tag does not match. |
| 155 | * \return A cipher-specific error code on calculation failure. |
| 156 | */ |
| 157 | int mbedcrypto_ccm_auth_decrypt( mbedcrypto_ccm_context *ctx, size_t length, |
| 158 | const unsigned char *iv, size_t iv_len, |
| 159 | const unsigned char *add, size_t add_len, |
| 160 | const unsigned char *input, unsigned char *output, |
| 161 | const unsigned char *tag, size_t tag_len ); |
| 162 | |
| 163 | |
| 164 | #if defined(MBEDCRYPTO_SELF_TEST) && defined(MBEDCRYPTO_AES_C) |
| 165 | /** |
| 166 | * \brief The CCM checkup routine. |
| 167 | * |
| 168 | * \return \c 0 on success. |
| 169 | * \return \c 1 on failure. |
| 170 | */ |
| 171 | int mbedcrypto_ccm_self_test( int verbose ); |
| 172 | #endif /* MBEDCRYPTO_SELF_TEST && MBEDCRYPTO_AES_C */ |
| 173 | |
| 174 | #ifdef __cplusplus |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #endif /* MBEDCRYPTO_CCM_H */ |