blob: 7ee3a379b2b9107ee27eaaaea3856c5d51c6a8d7 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/**
2 * \file ccm.h
3 *
4 * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02009 *
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020010 * 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_CCM_H
25#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020026
27#include "cipher.h"
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#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é-Gonnarda6916fa2014-05-02 15:17:29 +020031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020036/**
37 * \brief CCM context structure
38 */
39typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020043
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020054int mbedtls_ccm_init( mbedtls_ccm_context *ctx, mbedtls_cipher_id_t cipher,
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020055 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020062void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020063
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020064/**
65 * \brief CCM buffer encryption
66 *
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020067 * \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é-Gonnard00232332014-05-06 15:56:07 +020082 * \note The tag is written to a separate buffer. To get the tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020083 * 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020089int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020090 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é-Gonnard637eb3d2014-05-06 12:13:09 +020094
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020095/**
96 * \brief CCM buffer authenticated decryption
97 *
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020098 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200113 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é-Gonnard637eb3d2014-05-06 12:13:09 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200119/**
120 * \brief Checkup routine
121 *
122 * \return 0 if successful, or 1 if the test failed
123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124int mbedtls_ccm_self_test( int verbose );
125#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200126
127#ifdef __cplusplus
128}
129#endif
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_CCM_H */