Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /**
HannesTschofenig 0:796d0f61a05b 2 * \file gcm.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief Galois/Counter mode for 128-bit block ciphers
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * Copyright (C) 2006-2013, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 7 *
HannesTschofenig 0:796d0f61a05b 8 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 12 *
HannesTschofenig 0:796d0f61a05b 13 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 14 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 15 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 16 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 17 *
HannesTschofenig 0:796d0f61a05b 18 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 21 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 22 *
HannesTschofenig 0:796d0f61a05b 23 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 24 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 26 */
HannesTschofenig 0:796d0f61a05b 27 #ifndef POLARSSL_GCM_H
HannesTschofenig 0:796d0f61a05b 28 #define POLARSSL_GCM_H
HannesTschofenig 0:796d0f61a05b 29
HannesTschofenig 0:796d0f61a05b 30 #include "cipher.h"
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
HannesTschofenig 0:796d0f61a05b 33 #include <basetsd.h>
HannesTschofenig 0:796d0f61a05b 34 typedef UINT32 uint32_t;
HannesTschofenig 0:796d0f61a05b 35 typedef UINT64 uint64_t;
HannesTschofenig 0:796d0f61a05b 36 #else
HannesTschofenig 0:796d0f61a05b 37 #include <stdint.h>
HannesTschofenig 0:796d0f61a05b 38 #endif
HannesTschofenig 0:796d0f61a05b 39
HannesTschofenig 0:796d0f61a05b 40 #define GCM_ENCRYPT 1
HannesTschofenig 0:796d0f61a05b 41 #define GCM_DECRYPT 0
HannesTschofenig 0:796d0f61a05b 42
HannesTschofenig 0:796d0f61a05b 43 #define POLARSSL_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
HannesTschofenig 0:796d0f61a05b 44 #define POLARSSL_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
HannesTschofenig 0:796d0f61a05b 45
HannesTschofenig 0:796d0f61a05b 46 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 47 extern "C" {
HannesTschofenig 0:796d0f61a05b 48 #endif
HannesTschofenig 0:796d0f61a05b 49
HannesTschofenig 0:796d0f61a05b 50 /**
HannesTschofenig 0:796d0f61a05b 51 * \brief GCM context structure
HannesTschofenig 0:796d0f61a05b 52 */
HannesTschofenig 0:796d0f61a05b 53 typedef struct {
HannesTschofenig 0:796d0f61a05b 54 cipher_context_t cipher_ctx;/*!< cipher context used */
HannesTschofenig 0:796d0f61a05b 55 uint64_t HL[16]; /*!< Precalculated HTable */
HannesTschofenig 0:796d0f61a05b 56 uint64_t HH[16]; /*!< Precalculated HTable */
HannesTschofenig 0:796d0f61a05b 57 uint64_t len; /*!< Total data length */
HannesTschofenig 0:796d0f61a05b 58 uint64_t add_len; /*!< Total add length */
HannesTschofenig 0:796d0f61a05b 59 unsigned char base_ectr[16];/*!< First ECTR for tag */
HannesTschofenig 0:796d0f61a05b 60 unsigned char y[16]; /*!< Y working value */
HannesTschofenig 0:796d0f61a05b 61 unsigned char buf[16]; /*!< buf working value */
HannesTschofenig 0:796d0f61a05b 62 int mode; /*!< Encrypt or Decrypt */
HannesTschofenig 0:796d0f61a05b 63 }
HannesTschofenig 0:796d0f61a05b 64 gcm_context;
HannesTschofenig 0:796d0f61a05b 65
HannesTschofenig 0:796d0f61a05b 66 /**
HannesTschofenig 0:796d0f61a05b 67 * \brief GCM initialization (encryption)
HannesTschofenig 0:796d0f61a05b 68 *
HannesTschofenig 0:796d0f61a05b 69 * \param ctx GCM context to be initialized
HannesTschofenig 0:796d0f61a05b 70 * \param cipher cipher to use (a 128-bit block cipher)
HannesTschofenig 0:796d0f61a05b 71 * \param key encryption key
HannesTschofenig 0:796d0f61a05b 72 * \param keysize must be 128, 192 or 256
HannesTschofenig 0:796d0f61a05b 73 *
HannesTschofenig 0:796d0f61a05b 74 * \return 0 if successful, or a cipher specific error code
HannesTschofenig 0:796d0f61a05b 75 */
HannesTschofenig 0:796d0f61a05b 76 int gcm_init( gcm_context *ctx, cipher_id_t cipher, const unsigned char *key,
HannesTschofenig 0:796d0f61a05b 77 unsigned int keysize );
HannesTschofenig 0:796d0f61a05b 78
HannesTschofenig 0:796d0f61a05b 79 /**
HannesTschofenig 0:796d0f61a05b 80 * \brief GCM buffer encryption/decryption using a block cipher
HannesTschofenig 0:796d0f61a05b 81 *
HannesTschofenig 0:796d0f61a05b 82 * \note On encryption, the output buffer can be the same as the input buffer.
HannesTschofenig 0:796d0f61a05b 83 * On decryption, the output buffer cannot be the same as input buffer.
HannesTschofenig 0:796d0f61a05b 84 * If buffers overlap, the output buffer must trail at least 8 bytes
HannesTschofenig 0:796d0f61a05b 85 * behind the input buffer.
HannesTschofenig 0:796d0f61a05b 86 *
HannesTschofenig 0:796d0f61a05b 87 * \param ctx GCM context
HannesTschofenig 0:796d0f61a05b 88 * \param mode GCM_ENCRYPT or GCM_DECRYPT
HannesTschofenig 0:796d0f61a05b 89 * \param length length of the input data
HannesTschofenig 0:796d0f61a05b 90 * \param iv initialization vector
HannesTschofenig 0:796d0f61a05b 91 * \param iv_len length of IV
HannesTschofenig 0:796d0f61a05b 92 * \param add additional data
HannesTschofenig 0:796d0f61a05b 93 * \param add_len length of additional data
HannesTschofenig 0:796d0f61a05b 94 * \param input buffer holding the input data
HannesTschofenig 0:796d0f61a05b 95 * \param output buffer for holding the output data
HannesTschofenig 0:796d0f61a05b 96 * \param tag_len length of the tag to generate
HannesTschofenig 0:796d0f61a05b 97 * \param tag buffer for holding the tag
HannesTschofenig 0:796d0f61a05b 98 *
HannesTschofenig 0:796d0f61a05b 99 * \return 0 if successful
HannesTschofenig 0:796d0f61a05b 100 */
HannesTschofenig 0:796d0f61a05b 101 int gcm_crypt_and_tag( gcm_context *ctx,
HannesTschofenig 0:796d0f61a05b 102 int mode,
HannesTschofenig 0:796d0f61a05b 103 size_t length,
HannesTschofenig 0:796d0f61a05b 104 const unsigned char *iv,
HannesTschofenig 0:796d0f61a05b 105 size_t iv_len,
HannesTschofenig 0:796d0f61a05b 106 const unsigned char *add,
HannesTschofenig 0:796d0f61a05b 107 size_t add_len,
HannesTschofenig 0:796d0f61a05b 108 const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 109 unsigned char *output,
HannesTschofenig 0:796d0f61a05b 110 size_t tag_len,
HannesTschofenig 0:796d0f61a05b 111 unsigned char *tag );
HannesTschofenig 0:796d0f61a05b 112
HannesTschofenig 0:796d0f61a05b 113 /**
HannesTschofenig 0:796d0f61a05b 114 * \brief GCM buffer authenticated decryption using a block cipher
HannesTschofenig 0:796d0f61a05b 115 *
HannesTschofenig 0:796d0f61a05b 116 * \note On decryption, the output buffer cannot be the same as input buffer.
HannesTschofenig 0:796d0f61a05b 117 * If buffers overlap, the output buffer must trail at least 8 bytes
HannesTschofenig 0:796d0f61a05b 118 * behind the input buffer.
HannesTschofenig 0:796d0f61a05b 119 *
HannesTschofenig 0:796d0f61a05b 120 * \param ctx GCM context
HannesTschofenig 0:796d0f61a05b 121 * \param length length of the input data
HannesTschofenig 0:796d0f61a05b 122 * \param iv initialization vector
HannesTschofenig 0:796d0f61a05b 123 * \param iv_len length of IV
HannesTschofenig 0:796d0f61a05b 124 * \param add additional data
HannesTschofenig 0:796d0f61a05b 125 * \param add_len length of additional data
HannesTschofenig 0:796d0f61a05b 126 * \param tag buffer holding the tag
HannesTschofenig 0:796d0f61a05b 127 * \param tag_len length of the tag
HannesTschofenig 0:796d0f61a05b 128 * \param input buffer holding the input data
HannesTschofenig 0:796d0f61a05b 129 * \param output buffer for holding the output data
HannesTschofenig 0:796d0f61a05b 130 *
HannesTschofenig 0:796d0f61a05b 131 * \return 0 if successful and authenticated,
HannesTschofenig 0:796d0f61a05b 132 * POLARSSL_ERR_GCM_AUTH_FAILED if tag does not match
HannesTschofenig 0:796d0f61a05b 133 */
HannesTschofenig 0:796d0f61a05b 134 int gcm_auth_decrypt( gcm_context *ctx,
HannesTschofenig 0:796d0f61a05b 135 size_t length,
HannesTschofenig 0:796d0f61a05b 136 const unsigned char *iv,
HannesTschofenig 0:796d0f61a05b 137 size_t iv_len,
HannesTschofenig 0:796d0f61a05b 138 const unsigned char *add,
HannesTschofenig 0:796d0f61a05b 139 size_t add_len,
HannesTschofenig 0:796d0f61a05b 140 const unsigned char *tag,
HannesTschofenig 0:796d0f61a05b 141 size_t tag_len,
HannesTschofenig 0:796d0f61a05b 142 const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 143 unsigned char *output );
HannesTschofenig 0:796d0f61a05b 144
HannesTschofenig 0:796d0f61a05b 145 /**
HannesTschofenig 0:796d0f61a05b 146 * \brief Generic GCM stream start function
HannesTschofenig 0:796d0f61a05b 147 *
HannesTschofenig 0:796d0f61a05b 148 * \param ctx GCM context
HannesTschofenig 0:796d0f61a05b 149 * \param mode GCM_ENCRYPT or GCM_DECRYPT
HannesTschofenig 0:796d0f61a05b 150 * \param iv initialization vector
HannesTschofenig 0:796d0f61a05b 151 * \param iv_len length of IV
HannesTschofenig 0:796d0f61a05b 152 * \param add additional data (or NULL if length is 0)
HannesTschofenig 0:796d0f61a05b 153 * \param add_len length of additional data
HannesTschofenig 0:796d0f61a05b 154 *
HannesTschofenig 0:796d0f61a05b 155 * \return 0 if successful
HannesTschofenig 0:796d0f61a05b 156 */
HannesTschofenig 0:796d0f61a05b 157 int gcm_starts( gcm_context *ctx,
HannesTschofenig 0:796d0f61a05b 158 int mode,
HannesTschofenig 0:796d0f61a05b 159 const unsigned char *iv,
HannesTschofenig 0:796d0f61a05b 160 size_t iv_len,
HannesTschofenig 0:796d0f61a05b 161 const unsigned char *add,
HannesTschofenig 0:796d0f61a05b 162 size_t add_len );
HannesTschofenig 0:796d0f61a05b 163
HannesTschofenig 0:796d0f61a05b 164 /**
HannesTschofenig 0:796d0f61a05b 165 * \brief Generic GCM update function. Encrypts/decrypts using the
HannesTschofenig 0:796d0f61a05b 166 * given GCM context. Expects input to be a multiple of 16
HannesTschofenig 0:796d0f61a05b 167 * bytes! Only the last call before gcm_finish() can be less
HannesTschofenig 0:796d0f61a05b 168 * than 16 bytes!
HannesTschofenig 0:796d0f61a05b 169 *
HannesTschofenig 0:796d0f61a05b 170 * \note On decryption, the output buffer cannot be the same as input buffer.
HannesTschofenig 0:796d0f61a05b 171 * If buffers overlap, the output buffer must trail at least 8 bytes
HannesTschofenig 0:796d0f61a05b 172 * behind the input buffer.
HannesTschofenig 0:796d0f61a05b 173 *
HannesTschofenig 0:796d0f61a05b 174 * \param ctx GCM context
HannesTschofenig 0:796d0f61a05b 175 * \param length length of the input data
HannesTschofenig 0:796d0f61a05b 176 * \param input buffer holding the input data
HannesTschofenig 0:796d0f61a05b 177 * \param output buffer for holding the output data
HannesTschofenig 0:796d0f61a05b 178 *
HannesTschofenig 0:796d0f61a05b 179 * \return 0 if successful or POLARSSL_ERR_GCM_BAD_INPUT
HannesTschofenig 0:796d0f61a05b 180 */
HannesTschofenig 0:796d0f61a05b 181 int gcm_update( gcm_context *ctx,
HannesTschofenig 0:796d0f61a05b 182 size_t length,
HannesTschofenig 0:796d0f61a05b 183 const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 184 unsigned char *output );
HannesTschofenig 0:796d0f61a05b 185
HannesTschofenig 0:796d0f61a05b 186 /**
HannesTschofenig 0:796d0f61a05b 187 * \brief Generic GCM finalisation function. Wraps up the GCM stream
HannesTschofenig 0:796d0f61a05b 188 * and generates the tag. The tag can have a maximum length of
HannesTschofenig 0:796d0f61a05b 189 * 16 bytes.
HannesTschofenig 0:796d0f61a05b 190 *
HannesTschofenig 0:796d0f61a05b 191 * \param ctx GCM context
HannesTschofenig 0:796d0f61a05b 192 * \param tag buffer for holding the tag (may be NULL if tag_len is 0)
HannesTschofenig 0:796d0f61a05b 193 * \param tag_len length of the tag to generate
HannesTschofenig 0:796d0f61a05b 194 *
HannesTschofenig 0:796d0f61a05b 195 * \return 0 if successful or POLARSSL_ERR_GCM_BAD_INPUT
HannesTschofenig 0:796d0f61a05b 196 */
HannesTschofenig 0:796d0f61a05b 197 int gcm_finish( gcm_context *ctx,
HannesTschofenig 0:796d0f61a05b 198 unsigned char *tag,
HannesTschofenig 0:796d0f61a05b 199 size_t tag_len );
HannesTschofenig 0:796d0f61a05b 200
HannesTschofenig 0:796d0f61a05b 201 /**
HannesTschofenig 0:796d0f61a05b 202 * \brief Free a GCM context and underlying cipher sub-context
HannesTschofenig 0:796d0f61a05b 203 *
HannesTschofenig 0:796d0f61a05b 204 * \param ctx GCM context to free
HannesTschofenig 0:796d0f61a05b 205 */
HannesTschofenig 0:796d0f61a05b 206 void gcm_free( gcm_context *ctx );
HannesTschofenig 0:796d0f61a05b 207
HannesTschofenig 0:796d0f61a05b 208 /**
HannesTschofenig 0:796d0f61a05b 209 * \brief Checkup routine
HannesTschofenig 0:796d0f61a05b 210 *
HannesTschofenig 0:796d0f61a05b 211 * \return 0 if successful, or 1 if the test failed
HannesTschofenig 0:796d0f61a05b 212 */
HannesTschofenig 0:796d0f61a05b 213 int gcm_self_test( int verbose );
HannesTschofenig 0:796d0f61a05b 214
HannesTschofenig 0:796d0f61a05b 215 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 216 }
HannesTschofenig 0:796d0f61a05b 217 #endif
HannesTschofenig 0:796d0f61a05b 218
HannesTschofenig 0:796d0f61a05b 219 #endif /* gcm.h */
HannesTschofenig 0:796d0f61a05b 220
HannesTschofenig 0:796d0f61a05b 221