Mbed OS Reference
Loading...
Searching...
No Matches
gcm.h
Go to the documentation of this file.
1/**
2 * \file gcm.h
3 *
4 * \brief This file contains GCM definitions and functions.
5 *
6 * The Galois/Counter Mode (GCM) for 128-bit block ciphers is defined
7 * in <em>D. McGrew, J. Viega, The Galois/Counter Mode of Operation
8 * (GCM), Natl. Inst. Stand. Technol.</em>
9 *
10 * For more information on GCM, see <em>NIST SP 800-38D: Recommendation for
11 * Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC</em>.
12 *
13 */
14/*
15 * Copyright The Mbed TLS Contributors
16 * SPDX-License-Identifier: Apache-2.0
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31#ifndef MBEDTLS_GCM_H
32#define MBEDTLS_GCM_H
33
34#if !defined(MBEDTLS_CONFIG_FILE)
35#include "mbedtls/config.h"
36#else
37#include MBEDTLS_CONFIG_FILE
38#endif
39
40#include "mbedtls/cipher.h"
41
42#include <stdint.h>
43
44/**
45 * \addtogroup mbedtls
46 * \{
47 * \defgroup mbedtls_gcm_module Galois/Counter Mode
48 * \{
49 */
50
51#define MBEDTLS_GCM_ENCRYPT 1 ///< Encrypt operation
52#define MBEDTLS_GCM_DECRYPT 0 ///< Decrypt operation
53
54#define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
55
56/* MBEDTLS_ERR_GCM_HW_ACCEL_FAILED is deprecated and should not be used. */
57#define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */
58
59#define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65#if !defined(MBEDTLS_GCM_ALT)
66
67/**
68 * \brief The GCM context structure.
69 */
70typedef struct mbedtls_gcm_context
71{
72 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
73 uint64_t HL[16]; /*!< Precalculated HTable low. */
74 uint64_t HH[16]; /*!< Precalculated HTable high. */
75 uint64_t len; /*!< The total length of the encrypted data. */
76 uint64_t add_len; /*!< The total length of the additional data. */
77 unsigned char base_ectr[16]; /*!< The first ECTR for tag. */
78 unsigned char y[16]; /*!< The Y working value. */
79 unsigned char buf[16]; /*!< The buf working value. */
80 int mode; /*!< The operation to perform:
81 #MBEDTLS_GCM_ENCRYPT or
82 #MBEDTLS_GCM_DECRYPT. */
83}
85
86#else /* !MBEDTLS_GCM_ALT */
87#include "gcm_alt.h"
88#endif /* !MBEDTLS_GCM_ALT */
89
90/**
91 * \brief This function initializes the specified GCM context,
92 * to make references valid, and prepares the context
93 * for mbedtls_gcm_setkey() or mbedtls_gcm_free().
94 *
95 * The function does not bind the GCM context to a particular
96 * cipher, nor set the key. For this purpose, use
97 * mbedtls_gcm_setkey().
98 *
99 * \param ctx The GCM context to initialize. This must not be \c NULL.
100 */
102
103/**
104 * \brief This function associates a GCM context with a
105 * cipher algorithm and a key.
106 *
107 * \param ctx The GCM context. This must be initialized.
108 * \param cipher The 128-bit block cipher to use.
109 * \param key The encryption key. This must be a readable buffer of at
110 * least \p keybits bits.
111 * \param keybits The key size in bits. Valid options are:
112 * <ul><li>128 bits</li>
113 * <li>192 bits</li>
114 * <li>256 bits</li></ul>
115 *
116 * \return \c 0 on success.
117 * \return A cipher-specific error code on failure.
118 */
120 mbedtls_cipher_id_t cipher,
121 const unsigned char *key,
122 unsigned int keybits );
123
124/**
125 * \brief This function performs GCM encryption or decryption of a buffer.
126 *
127 * \note For encryption, the output buffer can be the same as the
128 * input buffer. For decryption, the output buffer cannot be
129 * the same as input buffer. If the buffers overlap, the output
130 * buffer must trail at least 8 Bytes behind the input buffer.
131 *
132 * \warning When this function performs a decryption, it outputs the
133 * authentication tag and does not verify that the data is
134 * authentic. You should use this function to perform encryption
135 * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
136 *
137 * \param ctx The GCM context to use for encryption or decryption. This
138 * must be initialized.
139 * \param mode The operation to perform:
140 * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
141 * The ciphertext is written to \p output and the
142 * authentication tag is written to \p tag.
143 * - #MBEDTLS_GCM_DECRYPT to perform decryption.
144 * The plaintext is written to \p output and the
145 * authentication tag is written to \p tag.
146 * Note that this mode is not recommended, because it does
147 * not verify the authenticity of the data. For this reason,
148 * you should use mbedtls_gcm_auth_decrypt() instead of
149 * calling this function in decryption mode.
150 * \param length The length of the input data, which is equal to the length
151 * of the output data.
152 * \param iv The initialization vector. This must be a readable buffer of
153 * at least \p iv_len Bytes.
154 * \param iv_len The length of the IV.
155 * \param add The buffer holding the additional data. This must be of at
156 * least that size in Bytes.
157 * \param add_len The length of the additional data.
158 * \param input The buffer holding the input data. If \p length is greater
159 * than zero, this must be a readable buffer of at least that
160 * size in Bytes.
161 * \param output The buffer for holding the output data. If \p length is greater
162 * than zero, this must be a writable buffer of at least that
163 * size in Bytes.
164 * \param tag_len The length of the tag to generate.
165 * \param tag The buffer for holding the tag. This must be a writable
166 * buffer of at least \p tag_len Bytes.
167 *
168 * \return \c 0 if the encryption or decryption was performed
169 * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
170 * this does not indicate that the data is authentic.
171 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
172 * not valid or a cipher-specific error code if the encryption
173 * or decryption failed.
174 */
176 int mode,
177 size_t length,
178 const unsigned char *iv,
179 size_t iv_len,
180 const unsigned char *add,
181 size_t add_len,
182 const unsigned char *input,
183 unsigned char *output,
184 size_t tag_len,
185 unsigned char *tag );
186
187/**
188 * \brief This function performs a GCM authenticated decryption of a
189 * buffer.
190 *
191 * \note For decryption, the output buffer cannot be the same as
192 * input buffer. If the buffers overlap, the output buffer
193 * must trail at least 8 Bytes behind the input buffer.
194 *
195 * \param ctx The GCM context. This must be initialized.
196 * \param length The length of the ciphertext to decrypt, which is also
197 * the length of the decrypted plaintext.
198 * \param iv The initialization vector. This must be a readable buffer
199 * of at least \p iv_len Bytes.
200 * \param iv_len The length of the IV.
201 * \param add The buffer holding the additional data. This must be of at
202 * least that size in Bytes.
203 * \param add_len The length of the additional data.
204 * \param tag The buffer holding the tag to verify. This must be a
205 * readable buffer of at least \p tag_len Bytes.
206 * \param tag_len The length of the tag to verify.
207 * \param input The buffer holding the ciphertext. If \p length is greater
208 * than zero, this must be a readable buffer of at least that
209 * size.
210 * \param output The buffer for holding the decrypted plaintext. If \p length
211 * is greater than zero, this must be a writable buffer of at
212 * least that size.
213 *
214 * \return \c 0 if successful and authenticated.
215 * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
216 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
217 * not valid or a cipher-specific error code if the decryption
218 * failed.
219 */
221 size_t length,
222 const unsigned char *iv,
223 size_t iv_len,
224 const unsigned char *add,
225 size_t add_len,
226 const unsigned char *tag,
227 size_t tag_len,
228 const unsigned char *input,
229 unsigned char *output );
230
231/**
232 * \brief This function starts a GCM encryption or decryption
233 * operation.
234 *
235 * \param ctx The GCM context. This must be initialized.
236 * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
237 * #MBEDTLS_GCM_DECRYPT.
238 * \param iv The initialization vector. This must be a readable buffer of
239 * at least \p iv_len Bytes.
240 * \param iv_len The length of the IV.
241 * \param add The buffer holding the additional data, or \c NULL
242 * if \p add_len is \c 0.
243 * \param add_len The length of the additional data. If \c 0,
244 * \p add may be \c NULL.
245 *
246 * \return \c 0 on success.
247 */
249 int mode,
250 const unsigned char *iv,
251 size_t iv_len,
252 const unsigned char *add,
253 size_t add_len );
254
255/**
256 * \brief This function feeds an input buffer into an ongoing GCM
257 * encryption or decryption operation.
258 *
259 * ` The function expects input to be a multiple of 16
260 * Bytes. Only the last call before calling
261 * mbedtls_gcm_finish() can be less than 16 Bytes.
262 *
263 * \note For decryption, the output buffer cannot be the same as
264 * input buffer. If the buffers overlap, the output buffer
265 * must trail at least 8 Bytes behind the input buffer.
266 *
267 * \param ctx The GCM context. This must be initialized.
268 * \param length The length of the input data. This must be a multiple of
269 * 16 except in the last call before mbedtls_gcm_finish().
270 * \param input The buffer holding the input data. If \p length is greater
271 * than zero, this must be a readable buffer of at least that
272 * size in Bytes.
273 * \param output The buffer for holding the output data. If \p length is
274 * greater than zero, this must be a writable buffer of at
275 * least that size in Bytes.
276 *
277 * \return \c 0 on success.
278 * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
279 */
281 size_t length,
282 const unsigned char *input,
283 unsigned char *output );
284
285/**
286 * \brief This function finishes the GCM operation and generates
287 * the authentication tag.
288 *
289 * It wraps up the GCM stream, and generates the
290 * tag. The tag can have a maximum length of 16 Bytes.
291 *
292 * \param ctx The GCM context. This must be initialized.
293 * \param tag The buffer for holding the tag. This must be a writable
294 * buffer of at least \p tag_len Bytes.
295 * \param tag_len The length of the tag to generate. This must be at least
296 * four.
297 *
298 * \return \c 0 on success.
299 * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
300 */
302 unsigned char *tag,
303 size_t tag_len );
304
305/**
306 * \brief This function clears a GCM context and the underlying
307 * cipher sub-context.
308 *
309 * \param ctx The GCM context to clear. If this is \c NULL, the call has
310 * no effect. Otherwise, this must be initialized.
311 */
313
314#if defined(MBEDTLS_SELF_TEST)
315
316/**
317 * \brief The GCM checkup routine.
318 *
319 * \return \c 0 on success.
320 * \return \c 1 on failure.
321 */
322int mbedtls_gcm_self_test( int verbose );
323
324#endif /* MBEDTLS_SELF_TEST */
325
326/// \}
327/// \}
328
329#ifdef __cplusplus
330}
331#endif
332
333
334#endif /* gcm.h */
This file contains an abstraction interface for use with the cipher primitives provided by the librar...
Configuration options (set of defines)
mbedtls_cipher_id_t
Supported cipher types.
Definition: cipher.h:89
int mbedtls_gcm_starts(mbedtls_gcm_context *ctx, int mode, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len)
This function starts a GCM encryption or decryption operation.
int mbedtls_gcm_crypt_and_tag(mbedtls_gcm_context *ctx, int mode, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, size_t tag_len, unsigned char *tag)
This function performs GCM encryption or decryption of a buffer.
int mbedtls_gcm_finish(mbedtls_gcm_context *ctx, unsigned char *tag, size_t tag_len)
This function finishes the GCM operation and generates the authentication tag.
int mbedtls_gcm_update(mbedtls_gcm_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
This function feeds an input buffer into an ongoing GCM encryption or decryption operation.
void mbedtls_gcm_init(mbedtls_gcm_context *ctx)
This function initializes the specified GCM context, to make references valid, and prepares the conte...
int mbedtls_gcm_setkey(mbedtls_gcm_context *ctx, mbedtls_cipher_id_t cipher, const unsigned char *key, unsigned int keybits)
This function associates a GCM context with a cipher algorithm and a key.
void mbedtls_gcm_free(mbedtls_gcm_context *ctx)
This function clears a GCM context and the underlying cipher sub-context.
int mbedtls_gcm_auth_decrypt(mbedtls_gcm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *tag, size_t tag_len, const unsigned char *input, unsigned char *output)
This function performs a GCM authenticated decryption of a buffer.
Generic cipher context.
Definition: cipher.h:318
The GCM context structure.
Definition: gcm.h:71
uint64_t add_len
Definition: gcm.h:76
uint64_t HH[16]
Definition: gcm.h:74
unsigned char y[16]
Definition: gcm.h:78
mbedtls_cipher_context_t cipher_ctx
Definition: gcm.h:72
uint64_t HL[16]
Definition: gcm.h:73
unsigned char buf[16]
Definition: gcm.h:79
uint64_t len
Definition: gcm.h:75
unsigned char base_ectr[16]
Definition: gcm.h:77