blob: c6e9d26c53a8720f6fd362ca02b830505107f924 [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker89e80c92012-03-20 13:50:09 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker89e80c92012-03-20 13:50:09 +00009 *
Paul Bakker89e80c92012-03-20 13:50:09 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
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
Paul Bakker407a0da2013-06-27 14:29:21 +020037#ifdef __cplusplus
38extern "C" {
39#endif
40
Paul Bakker89e80c92012-03-20 13:50:09 +000041/**
42 * \brief GCM context structure
43 */
44typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
Paul Bakker89e80c92012-03-20 13:50:09 +000046 uint64_t HL[16]; /*!< Precalculated HTable */
47 uint64_t HH[16]; /*!< Precalculated HTable */
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +020048 uint64_t len; /*!< Total data length */
49 uint64_t add_len; /*!< Total add length */
50 unsigned char base_ectr[16];/*!< First ECTR for tag */
51 unsigned char y[16]; /*!< Y working value */
52 unsigned char buf[16]; /*!< buf working value */
53 int mode; /*!< Encrypt or Decrypt */
Paul Bakker89e80c92012-03-20 13:50:09 +000054}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +000056
Paul Bakker89e80c92012-03-20 13:50:09 +000057/**
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020058 * \brief Initialize GCM context (just makes references valid)
59 * Makes the context ready for mbedtls_gcm_setkey() or
60 * mbedtls_gcm_free().
61 *
62 * \param ctx GCM context to initialize
63 */
64void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
65
66/**
Paul Bakker89e80c92012-03-20 13:50:09 +000067 * \brief GCM initialization (encryption)
68 *
69 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020070 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000071 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020072 * \param keybits must be 128, 192 or 256
Paul Bakker89e80c92012-03-20 13:50:09 +000073 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020074 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000075 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020076int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
77 mbedtls_cipher_id_t cipher,
78 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020079 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +000080
81/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020082 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000083 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000084 * \note On encryption, the output buffer can be the same as the input buffer.
85 * On decryption, the output buffer cannot be the same as input buffer.
86 * If buffers overlap, the output buffer must trail at least 8 bytes
87 * behind the input buffer.
88 *
Paul Bakker89e80c92012-03-20 13:50:09 +000089 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakker89e80c92012-03-20 13:50:09 +000091 * \param length length of the input data
92 * \param iv initialization vector
93 * \param iv_len length of IV
94 * \param add additional data
95 * \param add_len length of additional data
96 * \param input buffer holding the input data
97 * \param output buffer for holding the output data
98 * \param tag_len length of the tag to generate
99 * \param tag buffer for holding the tag
100 *
101 * \return 0 if successful
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000104 int mode,
105 size_t length,
106 const unsigned char *iv,
107 size_t iv_len,
108 const unsigned char *add,
109 size_t add_len,
110 const unsigned char *input,
111 unsigned char *output,
112 size_t tag_len,
113 unsigned char *tag );
114
115/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200116 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000117 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000118 * \note On decryption, the output buffer cannot be the same as input buffer.
119 * If buffers overlap, the output buffer must trail at least 8 bytes
120 * behind the input buffer.
121 *
Paul Bakker89e80c92012-03-20 13:50:09 +0000122 * \param ctx GCM context
123 * \param length length of the input data
124 * \param iv initialization vector
125 * \param iv_len length of IV
126 * \param add additional data
127 * \param add_len length of additional data
128 * \param tag buffer holding the tag
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200129 * \param tag_len length of the tag
Paul Bakker89e80c92012-03-20 13:50:09 +0000130 * \param input buffer holding the input data
131 * \param output buffer for holding the output data
132 *
133 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
Paul Bakker89e80c92012-03-20 13:50:09 +0000135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000137 size_t length,
138 const unsigned char *iv,
139 size_t iv_len,
140 const unsigned char *add,
141 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200142 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000143 size_t tag_len,
144 const unsigned char *input,
145 unsigned char *output );
146
147/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200148 * \brief Generic GCM stream start function
149 *
150 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200152 * \param iv initialization vector
153 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200154 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200155 * \param add_len length of additional data
156 *
157 * \return 0 if successful
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200160 int mode,
161 const unsigned char *iv,
162 size_t iv_len,
163 const unsigned char *add,
164 size_t add_len );
165
166/**
167 * \brief Generic GCM update function. Encrypts/decrypts using the
168 * given GCM context. Expects input to be a multiple of 16
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 * bytes! Only the last call before mbedtls_gcm_finish() can be less
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200170 * than 16 bytes!
171 *
172 * \note On decryption, the output buffer cannot be the same as input buffer.
173 * If buffers overlap, the output buffer must trail at least 8 bytes
174 * behind the input buffer.
175 *
176 * \param ctx GCM context
177 * \param length length of the input data
178 * \param input buffer holding the input data
179 * \param output buffer for holding the output data
180 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200184 size_t length,
185 const unsigned char *input,
186 unsigned char *output );
187
188/**
189 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200190 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200191 * 16 bytes.
192 *
193 * \param ctx GCM context
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200194 * \param tag buffer for holding the tag (may be NULL if tag_len is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200195 * \param tag_len length of the tag to generate
196 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200200 unsigned char *tag,
201 size_t tag_len );
202
203/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200204 * \brief Free a GCM context and underlying cipher sub-context
205 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100206 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200209
210/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000211 * \brief Checkup routine
212 *
213 * \return 0 if successful, or 1 if the test failed
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000216
217#ifdef __cplusplus
218}
219#endif
220
221#endif /* gcm.h */