Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/des.h@0:796d0f61a05b, 2018-09-27 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /** |
HannesTschofenig | 0:796d0f61a05b | 2 | * \file des.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief DES block cipher |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2006-2014, 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_DES_H |
HannesTschofenig | 0:796d0f61a05b | 28 | #define POLARSSL_DES_H |
HannesTschofenig | 0:796d0f61a05b | 29 | |
HannesTschofenig | 0:796d0f61a05b | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 31 | #include "config.h" |
HannesTschofenig | 0:796d0f61a05b | 32 | #else |
HannesTschofenig | 0:796d0f61a05b | 33 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 34 | #endif |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include <basetsd.h> |
HannesTschofenig | 0:796d0f61a05b | 40 | typedef UINT32 uint32_t; |
HannesTschofenig | 0:796d0f61a05b | 41 | #else |
HannesTschofenig | 0:796d0f61a05b | 42 | #include <inttypes.h> |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #define DES_ENCRYPT 1 |
HannesTschofenig | 0:796d0f61a05b | 46 | #define DES_DECRYPT 0 |
HannesTschofenig | 0:796d0f61a05b | 47 | |
HannesTschofenig | 0:796d0f61a05b | 48 | #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */ |
HannesTschofenig | 0:796d0f61a05b | 49 | |
HannesTschofenig | 0:796d0f61a05b | 50 | #define DES_KEY_SIZE 8 |
HannesTschofenig | 0:796d0f61a05b | 51 | |
HannesTschofenig | 0:796d0f61a05b | 52 | #if !defined(POLARSSL_DES_ALT) |
HannesTschofenig | 0:796d0f61a05b | 53 | // Regular implementation |
HannesTschofenig | 0:796d0f61a05b | 54 | // |
HannesTschofenig | 0:796d0f61a05b | 55 | |
HannesTschofenig | 0:796d0f61a05b | 56 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 57 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 58 | #endif |
HannesTschofenig | 0:796d0f61a05b | 59 | |
HannesTschofenig | 0:796d0f61a05b | 60 | /** |
HannesTschofenig | 0:796d0f61a05b | 61 | * \brief DES context structure |
HannesTschofenig | 0:796d0f61a05b | 62 | */ |
HannesTschofenig | 0:796d0f61a05b | 63 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 64 | { |
HannesTschofenig | 0:796d0f61a05b | 65 | int mode; /*!< encrypt/decrypt */ |
HannesTschofenig | 0:796d0f61a05b | 66 | uint32_t sk[32]; /*!< DES subkeys */ |
HannesTschofenig | 0:796d0f61a05b | 67 | } |
HannesTschofenig | 0:796d0f61a05b | 68 | des_context; |
HannesTschofenig | 0:796d0f61a05b | 69 | |
HannesTschofenig | 0:796d0f61a05b | 70 | /** |
HannesTschofenig | 0:796d0f61a05b | 71 | * \brief Triple-DES context structure |
HannesTschofenig | 0:796d0f61a05b | 72 | */ |
HannesTschofenig | 0:796d0f61a05b | 73 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 74 | { |
HannesTschofenig | 0:796d0f61a05b | 75 | int mode; /*!< encrypt/decrypt */ |
HannesTschofenig | 0:796d0f61a05b | 76 | uint32_t sk[96]; /*!< 3DES subkeys */ |
HannesTschofenig | 0:796d0f61a05b | 77 | } |
HannesTschofenig | 0:796d0f61a05b | 78 | des3_context; |
HannesTschofenig | 0:796d0f61a05b | 79 | |
HannesTschofenig | 0:796d0f61a05b | 80 | /** |
HannesTschofenig | 0:796d0f61a05b | 81 | * \brief Set key parity on the given key to odd. |
HannesTschofenig | 0:796d0f61a05b | 82 | * |
HannesTschofenig | 0:796d0f61a05b | 83 | * DES keys are 56 bits long, but each byte is padded with |
HannesTschofenig | 0:796d0f61a05b | 84 | * a parity bit to allow verification. |
HannesTschofenig | 0:796d0f61a05b | 85 | * |
HannesTschofenig | 0:796d0f61a05b | 86 | * \param key 8-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 87 | */ |
HannesTschofenig | 0:796d0f61a05b | 88 | void des_key_set_parity( unsigned char key[DES_KEY_SIZE] ); |
HannesTschofenig | 0:796d0f61a05b | 89 | |
HannesTschofenig | 0:796d0f61a05b | 90 | /** |
HannesTschofenig | 0:796d0f61a05b | 91 | * \brief Check that key parity on the given key is odd. |
HannesTschofenig | 0:796d0f61a05b | 92 | * |
HannesTschofenig | 0:796d0f61a05b | 93 | * DES keys are 56 bits long, but each byte is padded with |
HannesTschofenig | 0:796d0f61a05b | 94 | * a parity bit to allow verification. |
HannesTschofenig | 0:796d0f61a05b | 95 | * |
HannesTschofenig | 0:796d0f61a05b | 96 | * \param key 8-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 97 | * |
HannesTschofenig | 0:796d0f61a05b | 98 | * \return 0 is parity was ok, 1 if parity was not correct. |
HannesTschofenig | 0:796d0f61a05b | 99 | */ |
HannesTschofenig | 0:796d0f61a05b | 100 | int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] ); |
HannesTschofenig | 0:796d0f61a05b | 101 | |
HannesTschofenig | 0:796d0f61a05b | 102 | /** |
HannesTschofenig | 0:796d0f61a05b | 103 | * \brief Check that key is not a weak or semi-weak DES key |
HannesTschofenig | 0:796d0f61a05b | 104 | * |
HannesTschofenig | 0:796d0f61a05b | 105 | * \param key 8-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 106 | * |
HannesTschofenig | 0:796d0f61a05b | 107 | * \return 0 if no weak key was found, 1 if a weak key was identified. |
HannesTschofenig | 0:796d0f61a05b | 108 | */ |
HannesTschofenig | 0:796d0f61a05b | 109 | int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] ); |
HannesTschofenig | 0:796d0f61a05b | 110 | |
HannesTschofenig | 0:796d0f61a05b | 111 | /** |
HannesTschofenig | 0:796d0f61a05b | 112 | * \brief DES key schedule (56-bit, encryption) |
HannesTschofenig | 0:796d0f61a05b | 113 | * |
HannesTschofenig | 0:796d0f61a05b | 114 | * \param ctx DES context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 115 | * \param key 8-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 116 | * |
HannesTschofenig | 0:796d0f61a05b | 117 | * \return 0 |
HannesTschofenig | 0:796d0f61a05b | 118 | */ |
HannesTschofenig | 0:796d0f61a05b | 119 | int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); |
HannesTschofenig | 0:796d0f61a05b | 120 | |
HannesTschofenig | 0:796d0f61a05b | 121 | /** |
HannesTschofenig | 0:796d0f61a05b | 122 | * \brief DES key schedule (56-bit, decryption) |
HannesTschofenig | 0:796d0f61a05b | 123 | * |
HannesTschofenig | 0:796d0f61a05b | 124 | * \param ctx DES context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 125 | * \param key 8-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 126 | * |
HannesTschofenig | 0:796d0f61a05b | 127 | * \return 0 |
HannesTschofenig | 0:796d0f61a05b | 128 | */ |
HannesTschofenig | 0:796d0f61a05b | 129 | int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); |
HannesTschofenig | 0:796d0f61a05b | 130 | |
HannesTschofenig | 0:796d0f61a05b | 131 | /** |
HannesTschofenig | 0:796d0f61a05b | 132 | * \brief Triple-DES key schedule (112-bit, encryption) |
HannesTschofenig | 0:796d0f61a05b | 133 | * |
HannesTschofenig | 0:796d0f61a05b | 134 | * \param ctx 3DES context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 135 | * \param key 16-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 136 | * |
HannesTschofenig | 0:796d0f61a05b | 137 | * \return 0 |
HannesTschofenig | 0:796d0f61a05b | 138 | */ |
HannesTschofenig | 0:796d0f61a05b | 139 | int des3_set2key_enc( des3_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 140 | const unsigned char key[DES_KEY_SIZE * 2] ); |
HannesTschofenig | 0:796d0f61a05b | 141 | |
HannesTschofenig | 0:796d0f61a05b | 142 | /** |
HannesTschofenig | 0:796d0f61a05b | 143 | * \brief Triple-DES key schedule (112-bit, decryption) |
HannesTschofenig | 0:796d0f61a05b | 144 | * |
HannesTschofenig | 0:796d0f61a05b | 145 | * \param ctx 3DES context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 146 | * \param key 16-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 147 | * |
HannesTschofenig | 0:796d0f61a05b | 148 | * \return 0 |
HannesTschofenig | 0:796d0f61a05b | 149 | */ |
HannesTschofenig | 0:796d0f61a05b | 150 | int des3_set2key_dec( des3_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 151 | const unsigned char key[DES_KEY_SIZE * 2] ); |
HannesTschofenig | 0:796d0f61a05b | 152 | |
HannesTschofenig | 0:796d0f61a05b | 153 | /** |
HannesTschofenig | 0:796d0f61a05b | 154 | * \brief Triple-DES key schedule (168-bit, encryption) |
HannesTschofenig | 0:796d0f61a05b | 155 | * |
HannesTschofenig | 0:796d0f61a05b | 156 | * \param ctx 3DES context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 157 | * \param key 24-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 158 | * |
HannesTschofenig | 0:796d0f61a05b | 159 | * \return 0 |
HannesTschofenig | 0:796d0f61a05b | 160 | */ |
HannesTschofenig | 0:796d0f61a05b | 161 | int des3_set3key_enc( des3_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 162 | const unsigned char key[DES_KEY_SIZE * 3] ); |
HannesTschofenig | 0:796d0f61a05b | 163 | |
HannesTschofenig | 0:796d0f61a05b | 164 | /** |
HannesTschofenig | 0:796d0f61a05b | 165 | * \brief Triple-DES key schedule (168-bit, decryption) |
HannesTschofenig | 0:796d0f61a05b | 166 | * |
HannesTschofenig | 0:796d0f61a05b | 167 | * \param ctx 3DES context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 168 | * \param key 24-byte secret key |
HannesTschofenig | 0:796d0f61a05b | 169 | * |
HannesTschofenig | 0:796d0f61a05b | 170 | * \return 0 |
HannesTschofenig | 0:796d0f61a05b | 171 | */ |
HannesTschofenig | 0:796d0f61a05b | 172 | int des3_set3key_dec( des3_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 173 | const unsigned char key[DES_KEY_SIZE * 3] ); |
HannesTschofenig | 0:796d0f61a05b | 174 | |
HannesTschofenig | 0:796d0f61a05b | 175 | /** |
HannesTschofenig | 0:796d0f61a05b | 176 | * \brief DES-ECB block encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 177 | * |
HannesTschofenig | 0:796d0f61a05b | 178 | * \param ctx DES context |
HannesTschofenig | 0:796d0f61a05b | 179 | * \param input 64-bit input block |
HannesTschofenig | 0:796d0f61a05b | 180 | * \param output 64-bit output block |
HannesTschofenig | 0:796d0f61a05b | 181 | * |
HannesTschofenig | 0:796d0f61a05b | 182 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 183 | */ |
HannesTschofenig | 0:796d0f61a05b | 184 | int des_crypt_ecb( des_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 185 | const unsigned char input[8], |
HannesTschofenig | 0:796d0f61a05b | 186 | unsigned char output[8] ); |
HannesTschofenig | 0:796d0f61a05b | 187 | |
HannesTschofenig | 0:796d0f61a05b | 188 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
HannesTschofenig | 0:796d0f61a05b | 189 | /** |
HannesTschofenig | 0:796d0f61a05b | 190 | * \brief DES-CBC buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 191 | * |
HannesTschofenig | 0:796d0f61a05b | 192 | * \param ctx DES context |
HannesTschofenig | 0:796d0f61a05b | 193 | * \param mode DES_ENCRYPT or DES_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 194 | * \param length length of the input data |
HannesTschofenig | 0:796d0f61a05b | 195 | * \param iv initialization vector (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 196 | * \param input buffer holding the input data |
HannesTschofenig | 0:796d0f61a05b | 197 | * \param output buffer holding the output data |
HannesTschofenig | 0:796d0f61a05b | 198 | */ |
HannesTschofenig | 0:796d0f61a05b | 199 | int des_crypt_cbc( des_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 200 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 201 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 202 | unsigned char iv[8], |
HannesTschofenig | 0:796d0f61a05b | 203 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 204 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 205 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
HannesTschofenig | 0:796d0f61a05b | 206 | |
HannesTschofenig | 0:796d0f61a05b | 207 | /** |
HannesTschofenig | 0:796d0f61a05b | 208 | * \brief 3DES-ECB block encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 209 | * |
HannesTschofenig | 0:796d0f61a05b | 210 | * \param ctx 3DES context |
HannesTschofenig | 0:796d0f61a05b | 211 | * \param input 64-bit input block |
HannesTschofenig | 0:796d0f61a05b | 212 | * \param output 64-bit output block |
HannesTschofenig | 0:796d0f61a05b | 213 | * |
HannesTschofenig | 0:796d0f61a05b | 214 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 215 | */ |
HannesTschofenig | 0:796d0f61a05b | 216 | int des3_crypt_ecb( des3_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 217 | const unsigned char input[8], |
HannesTschofenig | 0:796d0f61a05b | 218 | unsigned char output[8] ); |
HannesTschofenig | 0:796d0f61a05b | 219 | |
HannesTschofenig | 0:796d0f61a05b | 220 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
HannesTschofenig | 0:796d0f61a05b | 221 | /** |
HannesTschofenig | 0:796d0f61a05b | 222 | * \brief 3DES-CBC buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 223 | * |
HannesTschofenig | 0:796d0f61a05b | 224 | * \param ctx 3DES context |
HannesTschofenig | 0:796d0f61a05b | 225 | * \param mode DES_ENCRYPT or DES_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 226 | * \param length length of the input data |
HannesTschofenig | 0:796d0f61a05b | 227 | * \param iv initialization vector (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 228 | * \param input buffer holding the input data |
HannesTschofenig | 0:796d0f61a05b | 229 | * \param output buffer holding the output data |
HannesTschofenig | 0:796d0f61a05b | 230 | * |
HannesTschofenig | 0:796d0f61a05b | 231 | * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 232 | */ |
HannesTschofenig | 0:796d0f61a05b | 233 | int des3_crypt_cbc( des3_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 234 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 235 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 236 | unsigned char iv[8], |
HannesTschofenig | 0:796d0f61a05b | 237 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 238 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 239 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
HannesTschofenig | 0:796d0f61a05b | 240 | |
HannesTschofenig | 0:796d0f61a05b | 241 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 242 | } |
HannesTschofenig | 0:796d0f61a05b | 243 | #endif |
HannesTschofenig | 0:796d0f61a05b | 244 | |
HannesTschofenig | 0:796d0f61a05b | 245 | #else /* POLARSSL_DES_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 246 | #include "des_alt.h" |
HannesTschofenig | 0:796d0f61a05b | 247 | #endif /* POLARSSL_DES_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 248 | |
HannesTschofenig | 0:796d0f61a05b | 249 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 250 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 251 | #endif |
HannesTschofenig | 0:796d0f61a05b | 252 | |
HannesTschofenig | 0:796d0f61a05b | 253 | /** |
HannesTschofenig | 0:796d0f61a05b | 254 | * \brief Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 255 | * |
HannesTschofenig | 0:796d0f61a05b | 256 | * \return 0 if successful, or 1 if the test failed |
HannesTschofenig | 0:796d0f61a05b | 257 | */ |
HannesTschofenig | 0:796d0f61a05b | 258 | int des_self_test( int verbose ); |
HannesTschofenig | 0:796d0f61a05b | 259 | |
HannesTschofenig | 0:796d0f61a05b | 260 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 261 | } |
HannesTschofenig | 0:796d0f61a05b | 262 | #endif |
HannesTschofenig | 0:796d0f61a05b | 263 | |
HannesTschofenig | 0:796d0f61a05b | 264 | #endif /* des.h */ |
HannesTschofenig | 0:796d0f61a05b | 265 | |
HannesTschofenig | 0:796d0f61a05b | 266 |