blob: acd94adb88052c760e81c2e593e1412fc121a178 [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020023 */
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
Steven Cooreman222e2ff2017-04-04 11:37:15 +020032#if !defined(MBEDTLS_CCM_ALT)
33// Regular implementation
34//
35
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020036#ifdef __cplusplus
37extern "C" {
38#endif
39
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020040/**
41 * \brief CCM context structure
42 */
43typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020047
48/**
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020049 * \brief Initialize CCM context (just makes references valid)
50 * Makes the context ready for mbedtls_ccm_setkey() or
51 * mbedtls_ccm_free().
52 *
53 * \param ctx CCM context to initialize
54 */
55void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
56
57/**
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020058 * \brief CCM initialization (encryption and decryption)
59 *
60 * \param ctx CCM context to be initialized
61 * \param cipher cipher to use (a 128-bit block cipher)
62 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020063 * \param keybits key size in bits (must be acceptable by the cipher)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020064 *
65 * \return 0 if successful, or a cipher specific error code
66 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020067int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
68 mbedtls_cipher_id_t cipher,
69 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020070 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020071
72/**
73 * \brief Free a CCM context and underlying cipher sub-context
74 *
75 * \param ctx CCM context to free
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020078
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020079/**
80 * \brief CCM buffer encryption
81 *
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020082 * \param ctx CCM context
83 * \param length length of the input data in bytes
84 * \param iv nonce (initialization vector)
85 * \param iv_len length of IV in bytes
86 * must be 2, 3, 4, 5, 6, 7 or 8
87 * \param add additional data
88 * \param add_len length of additional data in bytes
89 * must be less than 2^16 - 2^8
90 * \param input buffer holding the input data
91 * \param output buffer for holding the output data
92 * must be at least 'length' bytes wide
93 * \param tag buffer for holding the tag
94 * \param tag_len length of the tag to generate in bytes
95 * must be 4, 6, 8, 10, 14 or 16
96 *
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020097 * \note The tag is written to a separate buffer. To get the tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020098 * concatenated with the output as in the CCM spec, use
99 * tag = output + length and make sure the output buffer is
100 * at least length + tag_len wide.
101 *
102 * \return 0 if successful
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200105 const unsigned char *iv, size_t iv_len,
106 const unsigned char *add, size_t add_len,
107 const unsigned char *input, unsigned char *output,
108 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200109
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200110/**
111 * \brief CCM buffer authenticated decryption
112 *
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200113 * \param ctx CCM context
114 * \param length length of the input data
115 * \param iv initialization vector
116 * \param iv_len length of IV
117 * \param add additional data
118 * \param add_len length of additional data
119 * \param input buffer holding the input data
120 * \param output buffer for holding the output data
121 * \param tag buffer holding the tag
122 * \param tag_len length of the tag
123 *
124 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200128 const unsigned char *iv, size_t iv_len,
129 const unsigned char *add, size_t add_len,
130 const unsigned char *input, unsigned char *output,
131 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200132
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200133#ifdef __cplusplus
134}
135#endif
136
137#else /* !MBEDTLS_CCM_ALT */
138#include "ccm_alt.h"
139#endif /* !MBEDTLS_CCM_ALT */
140
141#ifdef __cplusplus
142extern "C" {
143#endif
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200146/**
147 * \brief Checkup routine
148 *
149 * \return 0 if successful, or 1 if the test failed
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_ccm_self_test( int verbose );
152#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200153
154#ifdef __cplusplus
155}
156#endif
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#endif /* MBEDTLS_CCM_H */