blob: 59128d8b15f5acee684ec322efbc7a79554d52a2 [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
Paul Bakker407a0da2013-06-27 14:29:21 +020036#ifdef __cplusplus
37extern "C" {
38#endif
39
Paul Bakker89e80c92012-03-20 13:50:09 +000040/**
41 * \brief GCM context structure
42 */
43typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
Paul Bakker89e80c92012-03-20 13:50:09 +000045 uint64_t HL[16]; /*!< Precalculated HTable */
46 uint64_t HH[16]; /*!< Precalculated HTable */
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +020047 uint64_t len; /*!< Total data length */
48 uint64_t add_len; /*!< Total add length */
49 unsigned char base_ectr[16];/*!< First ECTR for tag */
50 unsigned char y[16]; /*!< Y working value */
51 unsigned char buf[16]; /*!< buf working value */
52 int mode; /*!< Encrypt or Decrypt */
Paul Bakker89e80c92012-03-20 13:50:09 +000053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +000055
Paul Bakker89e80c92012-03-20 13:50:09 +000056/**
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020057 * \brief Initialize GCM context (just makes references valid)
58 * Makes the context ready for mbedtls_gcm_setkey() or
59 * mbedtls_gcm_free().
60 *
61 * \param ctx GCM context to initialize
62 */
63void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
64
65/**
Paul Bakker89e80c92012-03-20 13:50:09 +000066 * \brief GCM initialization (encryption)
67 *
68 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020069 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000070 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020071 * \param keybits must be 128, 192 or 256
Paul Bakker89e80c92012-03-20 13:50:09 +000072 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020073 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000074 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020075int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
76 mbedtls_cipher_id_t cipher,
77 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020078 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +000079
80/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020081 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000082 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000083 * \note On encryption, the output buffer can be the same as the input buffer.
84 * On decryption, the output buffer cannot be the same as input buffer.
85 * If buffers overlap, the output buffer must trail at least 8 bytes
86 * behind the input buffer.
87 *
Gilles Peskinebfc3b742018-06-01 17:55:41 +020088 * \warning When this function performs a decryption, it outputs the
89 * authentication tag and does not verify that the data is
90 * authentic. You should use this function to perform encryption
91 * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
Paul Bakker89e80c92012-03-20 13:50:09 +000092 *
Gilles Peskinebfc3b742018-06-01 17:55:41 +020093 * \param ctx The GCM context to use for encryption or decryption.
94 * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
95 * #MBEDTLS_GCM_DECRYPT. Note that during decryption, the
96 * tag is not verified. You should use this function only
97 * to encrypt data, and use mbedtls_gcm_auth_decrypt()
98 * to decrypt.
99 * \param length The length of the input data, which is equal to the length
100 * of the output data.
101 * \param iv The initialization vector.
102 * \param iv_len The length of the IV.
103 * \param add The buffer holding the additional data.
104 * \param add_len The length of the additional data.
105 * \param input The buffer holding the input data. Its size is \b length.
106 * \param output The buffer for holding the output data. It must have room
107 * for \b length bytes.
108 * \param tag_len The length of the tag to generate.
109 * \param tag The buffer for holding the tag.
110 *
111 * \return \c 0 if the encryption or decryption was performed
112 * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
113 * this does not indicate that the data is authentic.
114 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
115 * \return A cipher-specific error code if the encryption or
116 * decryption failed.
Paul Bakker89e80c92012-03-20 13:50:09 +0000117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000119 int mode,
120 size_t length,
121 const unsigned char *iv,
122 size_t iv_len,
123 const unsigned char *add,
124 size_t add_len,
125 const unsigned char *input,
126 unsigned char *output,
127 size_t tag_len,
128 unsigned char *tag );
129
130/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200131 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000132 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000133 * \note On decryption, the output buffer cannot be the same as input buffer.
134 * If buffers overlap, the output buffer must trail at least 8 bytes
135 * behind the input buffer.
136 *
Gilles Peskinebfc3b742018-06-01 17:55:41 +0200137 * \param ctx The GCM context.
138 * \param length The length of the ciphertext to decrypt, which is also
139 * the length of the decrypted plaintext.
140 * \param iv The initialization vector.
141 * \param iv_len The length of the IV.
142 * \param add The buffer holding the additional data.
143 * \param add_len The length of the additional data.
144 * \param tag The buffer holding the tag to verify.
145 * \param tag_len The length of the tag to verify.
146 * \param input The buffer holding the ciphertext. Its size is \b length.
147 * \param output The buffer for holding the decrypted plaintext. It must
148 * have room for \b length bytes.
Paul Bakker89e80c92012-03-20 13:50:09 +0000149 *
Gilles Peskinebfc3b742018-06-01 17:55:41 +0200150 * \return \c 0 if successful and authenticated.
151 * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
152 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
153 * \return A cipher-specific error code if the decryption failed.
Paul Bakker89e80c92012-03-20 13:50:09 +0000154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000156 size_t length,
157 const unsigned char *iv,
158 size_t iv_len,
159 const unsigned char *add,
160 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200161 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000162 size_t tag_len,
163 const unsigned char *input,
164 unsigned char *output );
165
166/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200167 * \brief Generic GCM stream start function
168 *
169 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200171 * \param iv initialization vector
172 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200173 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200174 * \param add_len length of additional data
175 *
176 * \return 0 if successful
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200179 int mode,
180 const unsigned char *iv,
181 size_t iv_len,
182 const unsigned char *add,
183 size_t add_len );
184
185/**
186 * \brief Generic GCM update function. Encrypts/decrypts using the
187 * given GCM context. Expects input to be a multiple of 16
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 * bytes! Only the last call before mbedtls_gcm_finish() can be less
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200189 * than 16 bytes!
190 *
191 * \note On decryption, the output buffer cannot be the same as input buffer.
192 * If buffers overlap, the output buffer must trail at least 8 bytes
193 * behind the input buffer.
194 *
195 * \param ctx GCM context
196 * \param length length of the input data
197 * \param input buffer holding the input data
198 * \param output buffer for holding the output data
199 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200203 size_t length,
204 const unsigned char *input,
205 unsigned char *output );
206
207/**
208 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200209 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200210 * 16 bytes.
211 *
212 * \param ctx GCM context
Andres AG6c052082016-09-26 10:09:30 +0100213 * \param tag buffer for holding the tag
214 * \param tag_len length of the tag to generate (must be at least 4)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200215 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200219 unsigned char *tag,
220 size_t tag_len );
221
222/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200223 * \brief Free a GCM context and underlying cipher sub-context
224 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100225 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200228
229/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000230 * \brief Checkup routine
231 *
232 * \return 0 if successful, or 1 if the test failed
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000235
236#ifdef __cplusplus
237}
238#endif
239
240#endif /* gcm.h */