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 | */ |
| 24 | #ifndef POLARSSL_CCM_H |
| 25 | #define POLARSSL_CCM_H |
| 26 | |
| 27 | #include "cipher.h" |
| 28 | |
| 29 | #define POLARSSL_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */ |
| 30 | #define POLARSSL_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
| 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 { |
| 40 | cipher_context_t cipher_ctx; /*!< cipher context used */ |
| 41 | } |
| 42 | ccm_context; |
| 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 | */ |
| 54 | int ccm_init( ccm_context *ctx, cipher_id_t cipher, |
| 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 | */ |
| 62 | void ccm_free( ccm_context *ctx ); |
| 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 | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 89 | int ccm_encrypt_and_tag( ccm_context *ctx, size_t length, |
| 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, |
| 110 | * POLARSSL_ERR_CCM_AUTH_FAILED if tag does not match |
| 111 | */ |
| 112 | int ccm_auth_decrypt( ccm_context *ctx, size_t length, |
| 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 | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 118 | #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) |
| 119 | /** |
| 120 | * \brief Checkup routine |
| 121 | * |
| 122 | * \return 0 if successful, or 1 if the test failed |
| 123 | */ |
| 124 | int ccm_self_test( int verbose ); |
| 125 | #endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */ |
| 126 | |
| 127 | #ifdef __cplusplus |
| 128 | } |
| 129 | #endif |
| 130 | |
Manuel Pégourié-Gonnard | 9b66990 | 2015-03-06 16:52:46 +0000 | [diff] [blame] | 131 | #endif /* POLARSSL_CCM_H */ |