blob: f1019861dcbffa0e986ce93aabf0c5262facc228 [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
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.
Paul Bakker89e80c92012-03-20 13:50:09 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker89e80c92012-03-20 13:50:09 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_GCM_H
25#define MBEDTLS_GCM_H
Paul Bakker89e80c92012-03-20 13:50:09 +000026
Paul Bakker43aff2a2013-09-09 00:10:27 +020027#include "cipher.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000028
29#include <stdint.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define MBEDTLS_GCM_ENCRYPT 1
32#define MBEDTLS_GCM_DECRYPT 0
Paul Bakker89e80c92012-03-20 13:50:09 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
35#define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
Paul Bakker89e80c92012-03-20 13:50:09 +000036
Jaeden Amero15263302017-09-21 12:53:48 +010037#if !defined(MBEDTLS_GCM_ALT)
38
Paul Bakker407a0da2013-06-27 14:29:21 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Paul Bakker89e80c92012-03-20 13:50:09 +000043/**
44 * \brief GCM context structure
45 */
46typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
Paul Bakker89e80c92012-03-20 13:50:09 +000048 uint64_t HL[16]; /*!< Precalculated HTable */
49 uint64_t HH[16]; /*!< Precalculated HTable */
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +020050 uint64_t len; /*!< Total data length */
51 uint64_t add_len; /*!< Total add length */
52 unsigned char base_ectr[16];/*!< First ECTR for tag */
53 unsigned char y[16]; /*!< Y working value */
54 unsigned char buf[16]; /*!< buf working value */
55 int mode; /*!< Encrypt or Decrypt */
Paul Bakker89e80c92012-03-20 13:50:09 +000056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +000058
Paul Bakker89e80c92012-03-20 13:50:09 +000059/**
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020060 * \brief Initialize GCM context (just makes references valid)
61 * Makes the context ready for mbedtls_gcm_setkey() or
62 * mbedtls_gcm_free().
63 *
64 * \param ctx GCM context to initialize
65 */
66void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
67
68/**
Paul Bakker89e80c92012-03-20 13:50:09 +000069 * \brief GCM initialization (encryption)
70 *
71 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020072 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000073 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020074 * \param keybits must be 128, 192 or 256
Paul Bakker89e80c92012-03-20 13:50:09 +000075 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020076 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000077 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020078int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
79 mbedtls_cipher_id_t cipher,
80 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020081 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +000082
83/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020084 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000085 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000086 * \note On encryption, the output buffer can be the same as the input buffer.
87 * On decryption, the output buffer cannot be the same as input buffer.
88 * If buffers overlap, the output buffer must trail at least 8 bytes
89 * behind the input buffer.
90 *
Paul Bakker89e80c92012-03-20 13:50:09 +000091 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakker89e80c92012-03-20 13:50:09 +000093 * \param length length of the input data
94 * \param iv initialization vector
95 * \param iv_len length of IV
96 * \param add additional data
97 * \param add_len length of additional data
98 * \param input buffer holding the input data
99 * \param output buffer for holding the output data
100 * \param tag_len length of the tag to generate
101 * \param tag buffer for holding the tag
102 *
103 * \return 0 if successful
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000106 int mode,
107 size_t length,
108 const unsigned char *iv,
109 size_t iv_len,
110 const unsigned char *add,
111 size_t add_len,
112 const unsigned char *input,
113 unsigned char *output,
114 size_t tag_len,
115 unsigned char *tag );
116
117/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200118 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000119 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000120 * \note On decryption, the output buffer cannot be the same as input buffer.
121 * If buffers overlap, the output buffer must trail at least 8 bytes
122 * behind the input buffer.
123 *
Paul Bakker89e80c92012-03-20 13:50:09 +0000124 * \param ctx GCM context
125 * \param length length of the input data
126 * \param iv initialization vector
127 * \param iv_len length of IV
128 * \param add additional data
129 * \param add_len length of additional data
130 * \param tag buffer holding the tag
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200131 * \param tag_len length of the tag
Paul Bakker89e80c92012-03-20 13:50:09 +0000132 * \param input buffer holding the input data
133 * \param output buffer for holding the output data
134 *
135 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
Paul Bakker89e80c92012-03-20 13:50:09 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000139 size_t length,
140 const unsigned char *iv,
141 size_t iv_len,
142 const unsigned char *add,
143 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200144 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000145 size_t tag_len,
146 const unsigned char *input,
147 unsigned char *output );
148
149/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200150 * \brief Generic GCM stream start function
151 *
152 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200154 * \param iv initialization vector
155 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200156 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200157 * \param add_len length of additional data
158 *
159 * \return 0 if successful
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200162 int mode,
163 const unsigned char *iv,
164 size_t iv_len,
165 const unsigned char *add,
166 size_t add_len );
167
168/**
169 * \brief Generic GCM update function. Encrypts/decrypts using the
170 * given GCM context. Expects input to be a multiple of 16
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 * bytes! Only the last call before mbedtls_gcm_finish() can be less
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200172 * than 16 bytes!
173 *
174 * \note On decryption, the output buffer cannot be the same as input buffer.
175 * If buffers overlap, the output buffer must trail at least 8 bytes
176 * behind the input buffer.
177 *
178 * \param ctx GCM context
179 * \param length length of the input data
180 * \param input buffer holding the input data
181 * \param output buffer for holding the output data
182 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200186 size_t length,
187 const unsigned char *input,
188 unsigned char *output );
189
190/**
191 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200192 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200193 * 16 bytes.
194 *
195 * \param ctx GCM context
Andres AG821da842016-09-26 10:09:30 +0100196 * \param tag buffer for holding the tag
197 * \param tag_len length of the tag to generate (must be at least 4)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200198 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200202 unsigned char *tag,
203 size_t tag_len );
204
205/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200206 * \brief Free a GCM context and underlying cipher sub-context
207 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100208 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200211
Jaeden Amero15263302017-09-21 12:53:48 +0100212#ifdef __cplusplus
213}
214#endif
215
216#else /* !MBEDTLS_GCM_ALT */
217#include "gcm_alt.h"
218#endif /* !MBEDTLS_GCM_ALT */
219
220#ifdef __cplusplus
221extern "C" {
222#endif
223
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200224/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000225 * \brief Checkup routine
226 *
227 * \return 0 if successful, or 1 if the test failed
228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000230
231#ifdef __cplusplus
232}
233#endif
234
Jaeden Amero15263302017-09-21 12:53:48 +0100235
Paul Bakker89e80c92012-03-20 13:50:09 +0000236#endif /* gcm.h */