blob: 8f3b565757f7ecd6b08b4f989914b4a9422680f4 [file] [log] [blame]
Paul Bakker89e80c92012-03-20 13:50:09 +00001/**
2 * \file gcm.h
3 *
Paul Bakker43aff2a2013-09-09 00:10:27 +02004 * \brief Galois/Counter mode for 128-bit block ciphers
Paul Bakker89e80c92012-03-20 13:50:09 +00005 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakker89e80c92012-03-20 13:50:09 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker89e80c92012-03-20 13:50:09 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_GCM_H
24#define MBEDTLS_GCM_H
Paul Bakker89e80c92012-03-20 13:50:09 +000025
Paul Bakker43aff2a2013-09-09 00:10:27 +020026#include "cipher.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000027
28#include <stdint.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#define MBEDTLS_GCM_ENCRYPT 1
31#define MBEDTLS_GCM_DECRYPT 0
Paul Bakker89e80c92012-03-20 13:50:09 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
34#define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
Paul Bakker89e80c92012-03-20 13:50:09 +000035
Jaeden Amero15263302017-09-21 12:53:48 +010036#if !defined(MBEDTLS_GCM_ALT)
37
Paul Bakker407a0da2013-06-27 14:29:21 +020038#ifdef __cplusplus
39extern "C" {
40#endif
41
Paul Bakker89e80c92012-03-20 13:50:09 +000042/**
43 * \brief GCM context structure
44 */
45typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
Paul Bakker89e80c92012-03-20 13:50:09 +000047 uint64_t HL[16]; /*!< Precalculated HTable */
48 uint64_t HH[16]; /*!< Precalculated HTable */
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +020049 uint64_t len; /*!< Total data length */
50 uint64_t add_len; /*!< Total add length */
51 unsigned char base_ectr[16];/*!< First ECTR for tag */
52 unsigned char y[16]; /*!< Y working value */
53 unsigned char buf[16]; /*!< buf working value */
54 int mode; /*!< Encrypt or Decrypt */
Paul Bakker89e80c92012-03-20 13:50:09 +000055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +000057
Paul Bakker89e80c92012-03-20 13:50:09 +000058/**
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020059 * \brief Initialize GCM context (just makes references valid)
60 * Makes the context ready for mbedtls_gcm_setkey() or
61 * mbedtls_gcm_free().
62 *
63 * \param ctx GCM context to initialize
64 */
65void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
66
67/**
Paul Bakker89e80c92012-03-20 13:50:09 +000068 * \brief GCM initialization (encryption)
69 *
70 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020071 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000072 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020073 * \param keybits must be 128, 192 or 256
Paul Bakker89e80c92012-03-20 13:50:09 +000074 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020075 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000076 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020077int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
78 mbedtls_cipher_id_t cipher,
79 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020080 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +000081
82/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020083 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000084 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000085 * \note On encryption, the output buffer can be the same as the input buffer.
86 * On decryption, the output buffer cannot be the same as input buffer.
87 * If buffers overlap, the output buffer must trail at least 8 bytes
88 * behind the input buffer.
89 *
Paul Bakker89e80c92012-03-20 13:50:09 +000090 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakker89e80c92012-03-20 13:50:09 +000092 * \param length length of the input data
93 * \param iv initialization vector
94 * \param iv_len length of IV
95 * \param add additional data
96 * \param add_len length of additional data
97 * \param input buffer holding the input data
98 * \param output buffer for holding the output data
99 * \param tag_len length of the tag to generate
100 * \param tag buffer for holding the tag
101 *
102 * \return 0 if successful
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000105 int mode,
106 size_t length,
107 const unsigned char *iv,
108 size_t iv_len,
109 const unsigned char *add,
110 size_t add_len,
111 const unsigned char *input,
112 unsigned char *output,
113 size_t tag_len,
114 unsigned char *tag );
115
116/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200117 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000118 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000119 * \note On decryption, the output buffer cannot be the same as input buffer.
120 * If buffers overlap, the output buffer must trail at least 8 bytes
121 * behind the input buffer.
122 *
Paul Bakker89e80c92012-03-20 13:50:09 +0000123 * \param ctx GCM context
124 * \param length length of the input data
125 * \param iv initialization vector
126 * \param iv_len length of IV
127 * \param add additional data
128 * \param add_len length of additional data
129 * \param tag buffer holding the tag
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200130 * \param tag_len length of the tag
Paul Bakker89e80c92012-03-20 13:50:09 +0000131 * \param input buffer holding the input data
132 * \param output buffer for holding the output data
133 *
134 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
Paul Bakker89e80c92012-03-20 13:50:09 +0000136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000138 size_t length,
139 const unsigned char *iv,
140 size_t iv_len,
141 const unsigned char *add,
142 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200143 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000144 size_t tag_len,
145 const unsigned char *input,
146 unsigned char *output );
147
148/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200149 * \brief Generic GCM stream start function
150 *
151 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200153 * \param iv initialization vector
154 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200155 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200156 * \param add_len length of additional data
157 *
158 * \return 0 if successful
159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200161 int mode,
162 const unsigned char *iv,
163 size_t iv_len,
164 const unsigned char *add,
165 size_t add_len );
166
167/**
168 * \brief Generic GCM update function. Encrypts/decrypts using the
169 * given GCM context. Expects input to be a multiple of 16
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * bytes! Only the last call before mbedtls_gcm_finish() can be less
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200171 * than 16 bytes!
172 *
173 * \note On decryption, the output buffer cannot be the same as input buffer.
174 * If buffers overlap, the output buffer must trail at least 8 bytes
175 * behind the input buffer.
176 *
177 * \param ctx GCM context
178 * \param length length of the input data
179 * \param input buffer holding the input data
180 * \param output buffer for holding the output data
181 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200185 size_t length,
186 const unsigned char *input,
187 unsigned char *output );
188
189/**
190 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200191 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200192 * 16 bytes.
193 *
194 * \param ctx GCM context
Andres AG821da842016-09-26 10:09:30 +0100195 * \param tag buffer for holding the tag
196 * \param tag_len length of the tag to generate (must be at least 4)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200197 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200201 unsigned char *tag,
202 size_t tag_len );
203
204/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200205 * \brief Free a GCM context and underlying cipher sub-context
206 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100207 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200210
Jaeden Amero15263302017-09-21 12:53:48 +0100211#ifdef __cplusplus
212}
213#endif
214
215#else /* !MBEDTLS_GCM_ALT */
216#include "gcm_alt.h"
217#endif /* !MBEDTLS_GCM_ALT */
218
219#ifdef __cplusplus
220extern "C" {
221#endif
222
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200223/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000224 * \brief Checkup routine
225 *
226 * \return 0 if successful, or 1 if the test failed
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000229
230#ifdef __cplusplus
231}
232#endif
233
Jaeden Amero15263302017-09-21 12:53:48 +0100234
Paul Bakker89e80c92012-03-20 13:50:09 +0000235#endif /* gcm.h */