blob: c7f01c316fab49e3cd92d34bd6640bb119addcb2 [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. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010035#define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
Paul Bakker89e80c92012-03-20 13:50:09 +000037
Jaeden Amero15263302017-09-21 12:53:48 +010038#if !defined(MBEDTLS_GCM_ALT)
39
Paul Bakker407a0da2013-06-27 14:29:21 +020040#ifdef __cplusplus
41extern "C" {
42#endif
43
Paul Bakker89e80c92012-03-20 13:50:09 +000044/**
45 * \brief GCM context structure
46 */
47typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
Paul Bakker89e80c92012-03-20 13:50:09 +000049 uint64_t HL[16]; /*!< Precalculated HTable */
50 uint64_t HH[16]; /*!< Precalculated HTable */
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +020051 uint64_t len; /*!< Total data length */
52 uint64_t add_len; /*!< Total add length */
53 unsigned char base_ectr[16];/*!< First ECTR for tag */
54 unsigned char y[16]; /*!< Y working value */
55 unsigned char buf[16]; /*!< buf working value */
56 int mode; /*!< Encrypt or Decrypt */
Paul Bakker89e80c92012-03-20 13:50:09 +000057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +000059
Paul Bakker89e80c92012-03-20 13:50:09 +000060/**
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020061 * \brief Initialize GCM context (just makes references valid)
62 * Makes the context ready for mbedtls_gcm_setkey() or
63 * mbedtls_gcm_free().
64 *
65 * \param ctx GCM context to initialize
66 */
67void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
68
69/**
Paul Bakker89e80c92012-03-20 13:50:09 +000070 * \brief GCM initialization (encryption)
71 *
72 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020073 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000074 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020075 * \param keybits must be 128, 192 or 256
Paul Bakker89e80c92012-03-20 13:50:09 +000076 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020077 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000078 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020079int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
80 mbedtls_cipher_id_t cipher,
81 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020082 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +000083
84/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020085 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000086 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000087 * \note On encryption, the output buffer can be the same as the input buffer.
88 * On decryption, the output buffer cannot be the same as input buffer.
89 * If buffers overlap, the output buffer must trail at least 8 bytes
90 * behind the input buffer.
91 *
Paul Bakker89e80c92012-03-20 13:50:09 +000092 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakker89e80c92012-03-20 13:50:09 +000094 * \param length length of the input data
95 * \param iv initialization vector
96 * \param iv_len length of IV
97 * \param add additional data
98 * \param add_len length of additional data
99 * \param input buffer holding the input data
100 * \param output buffer for holding the output data
101 * \param tag_len length of the tag to generate
102 * \param tag buffer for holding the tag
103 *
104 * \return 0 if successful
105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000107 int mode,
108 size_t length,
109 const unsigned char *iv,
110 size_t iv_len,
111 const unsigned char *add,
112 size_t add_len,
113 const unsigned char *input,
114 unsigned char *output,
115 size_t tag_len,
116 unsigned char *tag );
117
118/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200119 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000120 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000121 * \note On decryption, the output buffer cannot be the same as input buffer.
122 * If buffers overlap, the output buffer must trail at least 8 bytes
123 * behind the input buffer.
124 *
Paul Bakker89e80c92012-03-20 13:50:09 +0000125 * \param ctx GCM context
126 * \param length length of the input data
127 * \param iv initialization vector
128 * \param iv_len length of IV
129 * \param add additional data
130 * \param add_len length of additional data
131 * \param tag buffer holding the tag
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200132 * \param tag_len length of the tag
Paul Bakker89e80c92012-03-20 13:50:09 +0000133 * \param input buffer holding the input data
134 * \param output buffer for holding the output data
135 *
136 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
Paul Bakker89e80c92012-03-20 13:50:09 +0000138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000140 size_t length,
141 const unsigned char *iv,
142 size_t iv_len,
143 const unsigned char *add,
144 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200145 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000146 size_t tag_len,
147 const unsigned char *input,
148 unsigned char *output );
149
150/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200151 * \brief Generic GCM stream start function
152 *
153 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200155 * \param iv initialization vector
156 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200157 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200158 * \param add_len length of additional data
159 *
160 * \return 0 if successful
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200163 int mode,
164 const unsigned char *iv,
165 size_t iv_len,
166 const unsigned char *add,
167 size_t add_len );
168
169/**
170 * \brief Generic GCM update function. Encrypts/decrypts using the
171 * given GCM context. Expects input to be a multiple of 16
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 * bytes! Only the last call before mbedtls_gcm_finish() can be less
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200173 * than 16 bytes!
174 *
175 * \note On decryption, the output buffer cannot be the same as input buffer.
176 * If buffers overlap, the output buffer must trail at least 8 bytes
177 * behind the input buffer.
178 *
179 * \param ctx GCM context
180 * \param length length of the input data
181 * \param input buffer holding the input data
182 * \param output buffer for holding the output data
183 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200187 size_t length,
188 const unsigned char *input,
189 unsigned char *output );
190
191/**
192 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200193 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200194 * 16 bytes.
195 *
196 * \param ctx GCM context
Andres AG821da842016-09-26 10:09:30 +0100197 * \param tag buffer for holding the tag
198 * \param tag_len length of the tag to generate (must be at least 4)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200199 *
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_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200203 unsigned char *tag,
204 size_t tag_len );
205
206/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200207 * \brief Free a GCM context and underlying cipher sub-context
208 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100209 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200212
Jaeden Amero15263302017-09-21 12:53:48 +0100213#ifdef __cplusplus
214}
215#endif
216
217#else /* !MBEDTLS_GCM_ALT */
218#include "gcm_alt.h"
219#endif /* !MBEDTLS_GCM_ALT */
220
221#ifdef __cplusplus
222extern "C" {
223#endif
224
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200225/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000226 * \brief Checkup routine
227 *
228 * \return 0 if successful, or 1 if the test failed
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000231
232#ifdef __cplusplus
233}
234#endif
235
Jaeden Amero15263302017-09-21 12:53:48 +0100236
Paul Bakker89e80c92012-03-20 13:50:09 +0000237#endif /* gcm.h */