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 | * |
| 6 | * Copyright (C) 2014, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_CCM_H |
| 28 | #define POLARSSL_CCM_H |
| 29 | |
| 30 | #include "cipher.h" |
| 31 | |
| 32 | #define POLARSSL_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */ |
| 33 | #define POLARSSL_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 39 | /** |
| 40 | * \brief CCM context structure |
| 41 | */ |
| 42 | typedef struct { |
| 43 | cipher_context_t cipher_ctx; /*!< cipher context used */ |
| 44 | } |
| 45 | ccm_context; |
| 46 | |
| 47 | /** |
| 48 | * \brief CCM initialization (encryption and decryption) |
| 49 | * |
| 50 | * \param ctx CCM context to be initialized |
| 51 | * \param cipher cipher to use (a 128-bit block cipher) |
| 52 | * \param key encryption key |
| 53 | * \param keysize key size in bits (must be acceptable by the cipher) |
| 54 | * |
| 55 | * \return 0 if successful, or a cipher specific error code |
| 56 | */ |
| 57 | int ccm_init( ccm_context *ctx, cipher_id_t cipher, |
| 58 | const unsigned char *key, unsigned int keysize ); |
| 59 | |
| 60 | /** |
| 61 | * \brief Free a CCM context and underlying cipher sub-context |
| 62 | * |
| 63 | * \param ctx CCM context to free |
| 64 | */ |
| 65 | void ccm_free( ccm_context *ctx ); |
| 66 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 67 | /** |
| 68 | * \brief CCM buffer encryption |
| 69 | * |
| 70 | * \todo Document if input/output buffers can be the same |
| 71 | * |
| 72 | * \param ctx CCM context |
| 73 | * \param length length of the input data in bytes |
| 74 | * \param iv nonce (initialization vector) |
| 75 | * \param iv_len length of IV in bytes |
| 76 | * must be 2, 3, 4, 5, 6, 7 or 8 |
| 77 | * \param add additional data |
| 78 | * \param add_len length of additional data in bytes |
| 79 | * must be less than 2^16 - 2^8 |
| 80 | * \param input buffer holding the input data |
| 81 | * \param output buffer for holding the output data |
| 82 | * must be at least 'length' bytes wide |
| 83 | * \param tag buffer for holding the tag |
| 84 | * \param tag_len length of the tag to generate in bytes |
| 85 | * must be 4, 6, 8, 10, 14 or 16 |
| 86 | * |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 87 | * \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] | 88 | * concatenated with the output as in the CCM spec, use |
| 89 | * tag = output + length and make sure the output buffer is |
| 90 | * at least length + tag_len wide. |
| 91 | * |
| 92 | * \return 0 if successful |
| 93 | */ |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 94 | int ccm_encrypt_and_tag( ccm_context *ctx, size_t length, |
| 95 | const unsigned char *iv, size_t iv_len, |
| 96 | const unsigned char *add, size_t add_len, |
| 97 | const unsigned char *input, unsigned char *output, |
| 98 | unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 99 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame^] | 100 | /** |
| 101 | * \brief CCM buffer authenticated decryption |
| 102 | * |
| 103 | * \todo Document if input/output buffers can be the same |
| 104 | * |
| 105 | * \param ctx CCM context |
| 106 | * \param length length of the input data |
| 107 | * \param iv initialization vector |
| 108 | * \param iv_len length of IV |
| 109 | * \param add additional data |
| 110 | * \param add_len length of additional data |
| 111 | * \param input buffer holding the input data |
| 112 | * \param output buffer for holding the output data |
| 113 | * \param tag buffer holding the tag |
| 114 | * \param tag_len length of the tag |
| 115 | * |
| 116 | * \return 0 if successful and authenticated, |
| 117 | * POLARSSL_ERR_CCM_AUTH_FAILED if tag does not match |
| 118 | */ |
| 119 | int ccm_auth_decrypt( ccm_context *ctx, size_t length, |
| 120 | const unsigned char *iv, size_t iv_len, |
| 121 | const unsigned char *add, size_t add_len, |
| 122 | const unsigned char *input, unsigned char *output, |
| 123 | const unsigned char *tag, size_t tag_len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 124 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 125 | #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) |
| 126 | /** |
| 127 | * \brief Checkup routine |
| 128 | * |
| 129 | * \return 0 if successful, or 1 if the test failed |
| 130 | */ |
| 131 | int ccm_self_test( int verbose ); |
| 132 | #endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */ |
| 133 | |
| 134 | #ifdef __cplusplus |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #endif /* POLARSSL_CGM_H */ |