| 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 | 
|  | 5 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | *  Copyright (C) 2014, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 7 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | *  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] | 9 | * | 
| Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 10 | *  This program is free software; you can redistribute it and/or modify | 
|  | 11 | *  it under the terms of the GNU General Public License as published by | 
|  | 12 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 13 | *  (at your option) any later version. | 
|  | 14 | * | 
|  | 15 | *  This program is distributed in the hope that it will be useful, | 
|  | 16 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 17 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 18 | *  GNU General Public License for more details. | 
|  | 19 | * | 
|  | 20 | *  You should have received a copy of the GNU General Public License along | 
|  | 21 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 22 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 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. */ | 
| Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 31 |  | 
|  | 32 | #ifdef __cplusplus | 
|  | 33 | extern "C" { | 
|  | 34 | #endif | 
|  | 35 |  | 
| Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 36 | /** | 
|  | 37 | * \brief          CCM context structure | 
|  | 38 | */ | 
|  | 39 | typedef struct { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_cipher_context_t cipher_ctx;    /*!< cipher context used */ | 
| Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 41 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_ccm_context; | 
| Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 43 |  | 
|  | 44 | /** | 
|  | 45 | * \brief           CCM initialization (encryption and decryption) | 
|  | 46 | * | 
|  | 47 | * \param ctx       CCM context to be initialized | 
|  | 48 | * \param cipher    cipher to use (a 128-bit block cipher) | 
|  | 49 | * \param key       encryption key | 
|  | 50 | * \param keysize   key size in bits (must be acceptable by the cipher) | 
|  | 51 | * | 
|  | 52 | * \return          0 if successful, or a cipher specific error code | 
|  | 53 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | int mbedtls_ccm_init( mbedtls_ccm_context *ctx, mbedtls_cipher_id_t cipher, | 
| Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 55 | const unsigned char *key, unsigned int keysize ); | 
|  | 56 |  | 
|  | 57 | /** | 
|  | 58 | * \brief           Free a CCM context and underlying cipher sub-context | 
|  | 59 | * | 
|  | 60 | * \param ctx       CCM context to free | 
|  | 61 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); | 
| Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 63 |  | 
| Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 64 | /** | 
|  | 65 | * \brief           CCM buffer encryption | 
|  | 66 | * | 
| Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 67 | * \param ctx       CCM context | 
|  | 68 | * \param length    length of the input data in bytes | 
|  | 69 | * \param iv        nonce (initialization vector) | 
|  | 70 | * \param iv_len    length of IV in bytes | 
|  | 71 | *                  must be 2, 3, 4, 5, 6, 7 or 8 | 
|  | 72 | * \param add       additional data | 
|  | 73 | * \param add_len   length of additional data in bytes | 
|  | 74 | *                  must be less than 2^16 - 2^8 | 
|  | 75 | * \param input     buffer holding the input data | 
|  | 76 | * \param output    buffer for holding the output data | 
|  | 77 | *                  must be at least 'length' bytes wide | 
|  | 78 | * \param tag       buffer for holding the tag | 
|  | 79 | * \param tag_len   length of the tag to generate in bytes | 
|  | 80 | *                  must be 4, 6, 8, 10, 14 or 16 | 
|  | 81 | * | 
| Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 82 | * \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] | 83 | *                  concatenated with the output as in the CCM spec, use | 
|  | 84 | *                  tag = output + length and make sure the output buffer is | 
|  | 85 | *                  at least length + tag_len wide. | 
|  | 86 | * | 
|  | 87 | * \return          0 if successful | 
|  | 88 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | 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] | 90 | const unsigned char *iv, size_t iv_len, | 
|  | 91 | const unsigned char *add, size_t add_len, | 
|  | 92 | const unsigned char *input, unsigned char *output, | 
|  | 93 | unsigned char *tag, size_t tag_len ); | 
| Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 94 |  | 
| Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 95 | /** | 
|  | 96 | * \brief           CCM buffer authenticated decryption | 
|  | 97 | * | 
| Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 98 | * \param ctx       CCM context | 
|  | 99 | * \param length    length of the input data | 
|  | 100 | * \param iv        initialization vector | 
|  | 101 | * \param iv_len    length of IV | 
|  | 102 | * \param add       additional data | 
|  | 103 | * \param add_len   length of additional data | 
|  | 104 | * \param input     buffer holding the input data | 
|  | 105 | * \param output    buffer for holding the output data | 
|  | 106 | * \param tag       buffer holding the tag | 
|  | 107 | * \param tag_len   length of the tag | 
|  | 108 | * | 
|  | 109 | * \return         0 if successful and authenticated, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | *                 MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match | 
| Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 111 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | 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] | 113 | const unsigned char *iv, size_t iv_len, | 
|  | 114 | const unsigned char *add, size_t add_len, | 
|  | 115 | const unsigned char *input, unsigned char *output, | 
|  | 116 | const unsigned char *tag, size_t tag_len ); | 
| 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 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) | 
| Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 119 | /** | 
|  | 120 | * \brief          Checkup routine | 
|  | 121 | * | 
|  | 122 | * \return         0 if successful, or 1 if the test failed | 
|  | 123 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | int mbedtls_ccm_self_test( int verbose ); | 
|  | 125 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ | 
| Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 126 |  | 
|  | 127 | #ifdef __cplusplus | 
|  | 128 | } | 
|  | 129 | #endif | 
|  | 130 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | #endif /* MBEDTLS_CCM_H */ |