blob: 14b04d2e39d509c6fbe44de48a562fc28f747338 [file] [log] [blame]
Paul Bakker89e80c92012-03-20 13:50:09 +00001/**
2 * \file gcm.h
3 *
4 * \brief Galois/Counter mode for AES
5 *
6 * Copyright (C) 2006-2012, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_GCM_H
28#define POLARSSL_GCM_H
29
30#include "aes.h"
31
32#include <stdint.h>
33
34#define GCM_ENCRYPT 1
35#define GCM_DECRYPT 0
36
37#define POLARSSL_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
38
39/**
40 * \brief GCM context structure
41 */
42typedef struct {
43 aes_context aes_ctx; /*!< AES context used */
44 uint64_t HL[16]; /*!< Precalculated HTable */
45 uint64_t HH[16]; /*!< Precalculated HTable */
46}
47gcm_context;
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * \brief GCM initialization (encryption)
55 *
56 * \param ctx GCM context to be initialized
57 * \param key encryption key
58 * \param keysize must be 128, 192 or 256
59 *
60 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
61 */
62int gcm_init( gcm_context *ctx, const unsigned char *key, unsigned int keysize );
63
64/**
65 * \brief GCM buffer encryption/decryption using AES
66 *
67 * \param ctx GCM context
68 * \param mode GCM_ENCRYPT or GCM_DECRYPT
69 * \param length length of the input data
70 * \param iv initialization vector
71 * \param iv_len length of IV
72 * \param add additional data
73 * \param add_len length of additional data
74 * \param input buffer holding the input data
75 * \param output buffer for holding the output data
76 * \param tag_len length of the tag to generate
77 * \param tag buffer for holding the tag
78 *
79 * \return 0 if successful
80 */
81int gcm_crypt_and_tag( gcm_context *ctx,
82 int mode,
83 size_t length,
84 const unsigned char *iv,
85 size_t iv_len,
86 const unsigned char *add,
87 size_t add_len,
88 const unsigned char *input,
89 unsigned char *output,
90 size_t tag_len,
91 unsigned char *tag );
92
93/**
94 * \brief GCM buffer authenticated decryption using AES
95 *
96 * \param ctx GCM context
97 * \param length length of the input data
98 * \param iv initialization vector
99 * \param iv_len length of IV
100 * \param add additional data
101 * \param add_len length of additional data
102 * \param tag buffer holding the tag
103 * \param tag_len length of the tag
104 * \param input buffer holding the input data
105 * \param output buffer for holding the output data
106 *
107 * \return 0 if successful and authenticated,
108 * POLARSSL_ERR_GCM_AUTH_FAILED if tag does not match
109 */
110int gcm_auth_decrypt( gcm_context *ctx,
111 size_t length,
112 const unsigned char *iv,
113 size_t iv_len,
114 const unsigned char *add,
115 size_t add_len,
116 const unsigned char *tag,
117 size_t tag_len,
118 const unsigned char *input,
119 unsigned char *output );
120
121/**
122 * \brief Checkup routine
123 *
124 * \return 0 if successful, or 1 if the test failed
125 */
126int gcm_self_test( int verbose );
127
128#ifdef __cplusplus
129}
130#endif
131
132#endif /* gcm.h */