blob: 1e153f28197fbd0b02514c6993280b241e176dd2 [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/**
58 * \brief GCM initialization (encryption)
59 *
60 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020061 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000062 * \param key encryption key
63 * \param keysize must be 128, 192 or 256
64 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020065 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_gcm_init( mbedtls_gcm_context *ctx, mbedtls_cipher_id_t cipher, const unsigned char *key,
Paul Bakker43aff2a2013-09-09 00:10:27 +020068 unsigned int keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +000069
70/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020071 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000072 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000073 * \note On encryption, the output buffer can be the same as the input buffer.
74 * On decryption, the output buffer cannot be the same as input buffer.
75 * If buffers overlap, the output buffer must trail at least 8 bytes
76 * behind the input buffer.
77 *
Paul Bakker89e80c92012-03-20 13:50:09 +000078 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakker89e80c92012-03-20 13:50:09 +000080 * \param length length of the input data
81 * \param iv initialization vector
82 * \param iv_len length of IV
83 * \param add additional data
84 * \param add_len length of additional data
85 * \param input buffer holding the input data
86 * \param output buffer for holding the output data
87 * \param tag_len length of the tag to generate
88 * \param tag buffer for holding the tag
89 *
90 * \return 0 if successful
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +000093 int mode,
94 size_t length,
95 const unsigned char *iv,
96 size_t iv_len,
97 const unsigned char *add,
98 size_t add_len,
99 const unsigned char *input,
100 unsigned char *output,
101 size_t tag_len,
102 unsigned char *tag );
103
104/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200105 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000106 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000107 * \note On decryption, the output buffer cannot be the same as input buffer.
108 * If buffers overlap, the output buffer must trail at least 8 bytes
109 * behind the input buffer.
110 *
Paul Bakker89e80c92012-03-20 13:50:09 +0000111 * \param ctx GCM context
112 * \param length length of the input data
113 * \param iv initialization vector
114 * \param iv_len length of IV
115 * \param add additional data
116 * \param add_len length of additional data
117 * \param tag buffer holding the tag
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200118 * \param tag_len length of the tag
Paul Bakker89e80c92012-03-20 13:50:09 +0000119 * \param input buffer holding the input data
120 * \param output buffer for holding the output data
121 *
122 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
Paul Bakker89e80c92012-03-20 13:50:09 +0000124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000126 size_t length,
127 const unsigned char *iv,
128 size_t iv_len,
129 const unsigned char *add,
130 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200131 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000132 size_t tag_len,
133 const unsigned char *input,
134 unsigned char *output );
135
136/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200137 * \brief Generic GCM stream start function
138 *
139 * \param ctx GCM context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200141 * \param iv initialization vector
142 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200143 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200144 * \param add_len length of additional data
145 *
146 * \return 0 if successful
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200149 int mode,
150 const unsigned char *iv,
151 size_t iv_len,
152 const unsigned char *add,
153 size_t add_len );
154
155/**
156 * \brief Generic GCM update function. Encrypts/decrypts using the
157 * given GCM context. Expects input to be a multiple of 16
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 * bytes! Only the last call before mbedtls_gcm_finish() can be less
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200159 * than 16 bytes!
160 *
161 * \note On decryption, the output buffer cannot be the same as input buffer.
162 * If buffers overlap, the output buffer must trail at least 8 bytes
163 * behind the input buffer.
164 *
165 * \param ctx GCM context
166 * \param length length of the input data
167 * \param input buffer holding the input data
168 * \param output buffer for holding the output data
169 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200173 size_t length,
174 const unsigned char *input,
175 unsigned char *output );
176
177/**
178 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200179 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200180 * 16 bytes.
181 *
182 * \param ctx GCM context
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200183 * \param tag buffer for holding the tag (may be NULL if tag_len is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200184 * \param tag_len length of the tag to generate
185 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200189 unsigned char *tag,
190 size_t tag_len );
191
192/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200193 * \brief Free a GCM context and underlying cipher sub-context
194 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100195 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200198
199/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000200 * \brief Checkup routine
201 *
202 * \return 0 if successful, or 1 if the test failed
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000205
206#ifdef __cplusplus
207}
208#endif
209
210#endif /* gcm.h */