blob: b61d4a902e67536a3ba89a1a8535eb828d3d63c9 [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é-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.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 */
24#ifndef POLARSSL_GCM_H
25#define POLARSSL_GCM_H
26
Paul Bakker43aff2a2013-09-09 00:10:27 +020027#include "cipher.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000028
Paul Bakkerfa6a6202013-10-28 18:48:30 +010029#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000030#include <basetsd.h>
Paul Bakkerc2148752013-09-23 15:11:46 +020031typedef UINT32 uint32_t;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000032typedef UINT64 uint64_t;
33#else
Paul Bakker89e80c92012-03-20 13:50:09 +000034#include <stdint.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000035#endif
Paul Bakker89e80c92012-03-20 13:50:09 +000036
37#define GCM_ENCRYPT 1
38#define GCM_DECRYPT 0
39
40#define POLARSSL_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
Paul Bakkerca4ab492012-04-18 14:23:57 +000041#define POLARSSL_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
Paul Bakker89e80c92012-03-20 13:50:09 +000042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Paul Bakker89e80c92012-03-20 13:50:09 +000047/**
48 * \brief GCM context structure
49 */
50typedef struct {
Paul Bakker43aff2a2013-09-09 00:10:27 +020051 cipher_context_t cipher_ctx;/*!< cipher context used */
Paul Bakker89e80c92012-03-20 13:50:09 +000052 uint64_t HL[16]; /*!< Precalculated HTable */
53 uint64_t HH[16]; /*!< Precalculated HTable */
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +020054 uint64_t len; /*!< Total data length */
55 uint64_t add_len; /*!< Total add length */
56 unsigned char base_ectr[16];/*!< First ECTR for tag */
57 unsigned char y[16]; /*!< Y working value */
58 unsigned char buf[16]; /*!< buf working value */
59 int mode; /*!< Encrypt or Decrypt */
Paul Bakker89e80c92012-03-20 13:50:09 +000060}
61gcm_context;
62
Paul Bakker89e80c92012-03-20 13:50:09 +000063/**
64 * \brief GCM initialization (encryption)
65 *
66 * \param ctx GCM context to be initialized
Paul Bakker43aff2a2013-09-09 00:10:27 +020067 * \param cipher cipher to use (a 128-bit block cipher)
Paul Bakker89e80c92012-03-20 13:50:09 +000068 * \param key encryption key
69 * \param keysize must be 128, 192 or 256
70 *
Paul Bakker43aff2a2013-09-09 00:10:27 +020071 * \return 0 if successful, or a cipher specific error code
Paul Bakker89e80c92012-03-20 13:50:09 +000072 */
Paul Bakker43aff2a2013-09-09 00:10:27 +020073int gcm_init( gcm_context *ctx, cipher_id_t cipher, const unsigned char *key,
74 unsigned int keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +000075
76/**
Paul Bakker43aff2a2013-09-09 00:10:27 +020077 * \brief GCM buffer encryption/decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +000078 *
Paul Bakkerca4ab492012-04-18 14:23:57 +000079 * \note On encryption, the output buffer can be the same as the input buffer.
80 * On decryption, the output buffer cannot be the same as input buffer.
81 * If buffers overlap, the output buffer must trail at least 8 bytes
82 * behind the input buffer.
83 *
Paul Bakker89e80c92012-03-20 13:50:09 +000084 * \param ctx GCM context
85 * \param mode GCM_ENCRYPT or GCM_DECRYPT
86 * \param length length of the input data
87 * \param iv initialization vector
88 * \param iv_len length of IV
89 * \param add additional data
90 * \param add_len length of additional data
91 * \param input buffer holding the input data
92 * \param output buffer for holding the output data
93 * \param tag_len length of the tag to generate
94 * \param tag buffer for holding the tag
95 *
96 * \return 0 if successful
97 */
98int gcm_crypt_and_tag( gcm_context *ctx,
99 int mode,
100 size_t length,
101 const unsigned char *iv,
102 size_t iv_len,
103 const unsigned char *add,
104 size_t add_len,
105 const unsigned char *input,
106 unsigned char *output,
107 size_t tag_len,
108 unsigned char *tag );
109
110/**
Paul Bakker43aff2a2013-09-09 00:10:27 +0200111 * \brief GCM buffer authenticated decryption using a block cipher
Paul Bakker89e80c92012-03-20 13:50:09 +0000112 *
Paul Bakkerca4ab492012-04-18 14:23:57 +0000113 * \note On decryption, the output buffer cannot be the same as input buffer.
114 * If buffers overlap, the output buffer must trail at least 8 bytes
115 * behind the input buffer.
116 *
Paul Bakker89e80c92012-03-20 13:50:09 +0000117 * \param ctx GCM context
118 * \param length length of the input data
119 * \param iv initialization vector
120 * \param iv_len length of IV
121 * \param add additional data
122 * \param add_len length of additional data
123 * \param tag buffer holding the tag
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200124 * \param tag_len length of the tag
Paul Bakker89e80c92012-03-20 13:50:09 +0000125 * \param input buffer holding the input data
126 * \param output buffer for holding the output data
127 *
128 * \return 0 if successful and authenticated,
129 * POLARSSL_ERR_GCM_AUTH_FAILED if tag does not match
130 */
131int gcm_auth_decrypt( gcm_context *ctx,
132 size_t length,
133 const unsigned char *iv,
134 size_t iv_len,
135 const unsigned char *add,
136 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200137 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000138 size_t tag_len,
139 const unsigned char *input,
140 unsigned char *output );
141
142/**
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200143 * \brief Generic GCM stream start function
144 *
145 * \param ctx GCM context
146 * \param mode GCM_ENCRYPT or GCM_DECRYPT
147 * \param iv initialization vector
148 * \param iv_len length of IV
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200149 * \param add additional data (or NULL if length is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200150 * \param add_len length of additional data
151 *
152 * \return 0 if successful
153 */
154int gcm_starts( gcm_context *ctx,
155 int mode,
156 const unsigned char *iv,
157 size_t iv_len,
158 const unsigned char *add,
159 size_t add_len );
160
161/**
162 * \brief Generic GCM update function. Encrypts/decrypts using the
163 * given GCM context. Expects input to be a multiple of 16
164 * bytes! Only the last call before gcm_finish() can be less
165 * than 16 bytes!
166 *
167 * \note On decryption, the output buffer cannot be the same as input buffer.
168 * If buffers overlap, the output buffer must trail at least 8 bytes
169 * behind the input buffer.
170 *
171 * \param ctx GCM context
172 * \param length length of the input data
173 * \param input buffer holding the input data
174 * \param output buffer for holding the output data
175 *
176 * \return 0 if successful or POLARSSL_ERR_GCM_BAD_INPUT
177 */
178int gcm_update( gcm_context *ctx,
179 size_t length,
180 const unsigned char *input,
181 unsigned char *output );
182
183/**
184 * \brief Generic GCM finalisation function. Wraps up the GCM stream
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200185 * and generates the tag. The tag can have a maximum length of
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200186 * 16 bytes.
187 *
188 * \param ctx GCM context
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200189 * \param tag buffer for holding the tag (may be NULL if tag_len is 0)
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200190 * \param tag_len length of the tag to generate
191 *
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200192 * \return 0 if successful or POLARSSL_ERR_GCM_BAD_INPUT
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200193 */
194int gcm_finish( gcm_context *ctx,
195 unsigned char *tag,
196 size_t tag_len );
197
198/**
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200199 * \brief Free a GCM context and underlying cipher sub-context
200 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100201 * \param ctx GCM context to free
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200202 */
203void gcm_free( gcm_context *ctx );
204
205/**
Paul Bakker89e80c92012-03-20 13:50:09 +0000206 * \brief Checkup routine
207 *
208 * \return 0 if successful, or 1 if the test failed
209 */
210int gcm_self_test( int verbose );
211
212#ifdef __cplusplus
213}
214#endif
215
216#endif /* gcm.h */