Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 * \file aes_alt.h
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * \brief AES block cipher
sahilmgandhi 18:6a4db94011d3 5 *
sahilmgandhi 18:6a4db94011d3 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
sahilmgandhi 18:6a4db94011d3 7 * SPDX-License-Identifier: Apache-2.0
sahilmgandhi 18:6a4db94011d3 8 *
sahilmgandhi 18:6a4db94011d3 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
sahilmgandhi 18:6a4db94011d3 10 * not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 11 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 12 *
sahilmgandhi 18:6a4db94011d3 13 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 14 *
sahilmgandhi 18:6a4db94011d3 15 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
sahilmgandhi 18:6a4db94011d3 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 18 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 19 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 20 *
sahilmgandhi 18:6a4db94011d3 21 * This file is part of mbed TLS (https://tls.mbed.org)
sahilmgandhi 18:6a4db94011d3 22 */
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 #if defined(MBEDTLS_AES_ALT)
sahilmgandhi 18:6a4db94011d3 25 // Regular implementation
sahilmgandhi 18:6a4db94011d3 26 //
sahilmgandhi 18:6a4db94011d3 27 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 28 extern "C" {
sahilmgandhi 18:6a4db94011d3 29 #endif
sahilmgandhi 18:6a4db94011d3 30
sahilmgandhi 18:6a4db94011d3 31 /**
sahilmgandhi 18:6a4db94011d3 32 * \brief AES context structure
sahilmgandhi 18:6a4db94011d3 33 *
sahilmgandhi 18:6a4db94011d3 34 * \note buf is able to hold 32 extra bytes, which can be used:
sahilmgandhi 18:6a4db94011d3 35 * - for alignment purposes if VIA padlock is used, and/or
sahilmgandhi 18:6a4db94011d3 36 * - to simplify key expansion in the 256-bit case by
sahilmgandhi 18:6a4db94011d3 37 * generating an extra round key
sahilmgandhi 18:6a4db94011d3 38 */
sahilmgandhi 18:6a4db94011d3 39 typedef struct
sahilmgandhi 18:6a4db94011d3 40 {
sahilmgandhi 18:6a4db94011d3 41 uint32_t keySize;
sahilmgandhi 18:6a4db94011d3 42 uint32_t encDec;
sahilmgandhi 18:6a4db94011d3 43 uint32_t opMode;
sahilmgandhi 18:6a4db94011d3 44 uint32_t channel;
sahilmgandhi 18:6a4db94011d3 45 uint32_t swapType;
sahilmgandhi 18:6a4db94011d3 46 uint32_t *iv;
sahilmgandhi 18:6a4db94011d3 47 unsigned char prv_iv[16];
sahilmgandhi 18:6a4db94011d3 48 #if 1
sahilmgandhi 18:6a4db94011d3 49 uint32_t buf[8];
sahilmgandhi 18:6a4db94011d3 50 /* For comparsion with software AES for correctness */
sahilmgandhi 18:6a4db94011d3 51 #else
sahilmgandhi 18:6a4db94011d3 52 uint32_t buf[68]; /*!< unaligned data */
sahilmgandhi 18:6a4db94011d3 53 int nr; /*!< number of rounds */
sahilmgandhi 18:6a4db94011d3 54 uint32_t *rk; /*!< AES round keys */
sahilmgandhi 18:6a4db94011d3 55 #endif
sahilmgandhi 18:6a4db94011d3 56 }
sahilmgandhi 18:6a4db94011d3 57 mbedtls_aes_context;
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 /**
sahilmgandhi 18:6a4db94011d3 60 * \brief Initialize AES context
sahilmgandhi 18:6a4db94011d3 61 *
sahilmgandhi 18:6a4db94011d3 62 * \param ctx AES context to be initialized
sahilmgandhi 18:6a4db94011d3 63 */
sahilmgandhi 18:6a4db94011d3 64 void mbedtls_aes_init( mbedtls_aes_context *ctx );
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 /**
sahilmgandhi 18:6a4db94011d3 67 * \brief Clear AES context
sahilmgandhi 18:6a4db94011d3 68 *
sahilmgandhi 18:6a4db94011d3 69 * \param ctx AES context to be cleared
sahilmgandhi 18:6a4db94011d3 70 */
sahilmgandhi 18:6a4db94011d3 71 void mbedtls_aes_free( mbedtls_aes_context *ctx );
sahilmgandhi 18:6a4db94011d3 72
sahilmgandhi 18:6a4db94011d3 73 /**
sahilmgandhi 18:6a4db94011d3 74 * \brief AES key schedule (encryption)
sahilmgandhi 18:6a4db94011d3 75 *
sahilmgandhi 18:6a4db94011d3 76 * \param ctx AES context to be initialized
sahilmgandhi 18:6a4db94011d3 77 * \param key encryption key
sahilmgandhi 18:6a4db94011d3 78 * \param keybits must be 128, 192 or 256
sahilmgandhi 18:6a4db94011d3 79 *
sahilmgandhi 18:6a4db94011d3 80 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
sahilmgandhi 18:6a4db94011d3 81 */
sahilmgandhi 18:6a4db94011d3 82 int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
sahilmgandhi 18:6a4db94011d3 83 unsigned int keybits );
sahilmgandhi 18:6a4db94011d3 84
sahilmgandhi 18:6a4db94011d3 85 /**
sahilmgandhi 18:6a4db94011d3 86 * \brief AES key schedule (decryption)
sahilmgandhi 18:6a4db94011d3 87 *
sahilmgandhi 18:6a4db94011d3 88 * \param ctx AES context to be initialized
sahilmgandhi 18:6a4db94011d3 89 * \param key decryption key
sahilmgandhi 18:6a4db94011d3 90 * \param keybits must be 128, 192 or 256
sahilmgandhi 18:6a4db94011d3 91 *
sahilmgandhi 18:6a4db94011d3 92 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
sahilmgandhi 18:6a4db94011d3 93 */
sahilmgandhi 18:6a4db94011d3 94 int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
sahilmgandhi 18:6a4db94011d3 95 unsigned int keybits );
sahilmgandhi 18:6a4db94011d3 96
sahilmgandhi 18:6a4db94011d3 97 /**
sahilmgandhi 18:6a4db94011d3 98 * \brief AES-ECB block encryption/decryption
sahilmgandhi 18:6a4db94011d3 99 *
sahilmgandhi 18:6a4db94011d3 100 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 101 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
sahilmgandhi 18:6a4db94011d3 102 * \param input 16-byte input block
sahilmgandhi 18:6a4db94011d3 103 * \param output 16-byte output block
sahilmgandhi 18:6a4db94011d3 104 *
sahilmgandhi 18:6a4db94011d3 105 * \return 0 if successful
sahilmgandhi 18:6a4db94011d3 106 */
sahilmgandhi 18:6a4db94011d3 107 int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 108 int mode,
sahilmgandhi 18:6a4db94011d3 109 const unsigned char input[16],
sahilmgandhi 18:6a4db94011d3 110 unsigned char output[16] );
sahilmgandhi 18:6a4db94011d3 111
sahilmgandhi 18:6a4db94011d3 112 #if defined(MBEDTLS_CIPHER_MODE_CBC)
sahilmgandhi 18:6a4db94011d3 113 /**
sahilmgandhi 18:6a4db94011d3 114 * \brief AES-CBC buffer encryption/decryption
sahilmgandhi 18:6a4db94011d3 115 * Length should be a multiple of the block
sahilmgandhi 18:6a4db94011d3 116 * size (16 bytes)
sahilmgandhi 18:6a4db94011d3 117 *
sahilmgandhi 18:6a4db94011d3 118 * \note Upon exit, the content of the IV is updated so that you can
sahilmgandhi 18:6a4db94011d3 119 * call the function same function again on the following
sahilmgandhi 18:6a4db94011d3 120 * block(s) of data and get the same result as if it was
sahilmgandhi 18:6a4db94011d3 121 * encrypted in one call. This allows a "streaming" usage.
sahilmgandhi 18:6a4db94011d3 122 * If on the other hand you need to retain the contents of the
sahilmgandhi 18:6a4db94011d3 123 * IV, you should either save it manually or use the cipher
sahilmgandhi 18:6a4db94011d3 124 * module instead.
sahilmgandhi 18:6a4db94011d3 125 *
sahilmgandhi 18:6a4db94011d3 126 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 127 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
sahilmgandhi 18:6a4db94011d3 128 * \param length length of the input data
sahilmgandhi 18:6a4db94011d3 129 * \param iv initialization vector (updated after use)
sahilmgandhi 18:6a4db94011d3 130 * \param input buffer holding the input data
sahilmgandhi 18:6a4db94011d3 131 * \param output buffer holding the output data
sahilmgandhi 18:6a4db94011d3 132 *
sahilmgandhi 18:6a4db94011d3 133 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
sahilmgandhi 18:6a4db94011d3 134 */
sahilmgandhi 18:6a4db94011d3 135 int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 136 int mode,
sahilmgandhi 18:6a4db94011d3 137 size_t length,
sahilmgandhi 18:6a4db94011d3 138 unsigned char iv[16],
sahilmgandhi 18:6a4db94011d3 139 const unsigned char *input,
sahilmgandhi 18:6a4db94011d3 140 unsigned char *output );
sahilmgandhi 18:6a4db94011d3 141 #endif /* MBEDTLS_CIPHER_MODE_CBC */
sahilmgandhi 18:6a4db94011d3 142
sahilmgandhi 18:6a4db94011d3 143 #if defined(MBEDTLS_CIPHER_MODE_CFB)
sahilmgandhi 18:6a4db94011d3 144 /**
sahilmgandhi 18:6a4db94011d3 145 * \brief AES-CFB128 buffer encryption/decryption.
sahilmgandhi 18:6a4db94011d3 146 *
sahilmgandhi 18:6a4db94011d3 147 * Note: Due to the nature of CFB you should use the same key schedule for
sahilmgandhi 18:6a4db94011d3 148 * both encryption and decryption. So a context initialized with
sahilmgandhi 18:6a4db94011d3 149 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
sahilmgandhi 18:6a4db94011d3 150 *
sahilmgandhi 18:6a4db94011d3 151 * \note Upon exit, the content of the IV is updated so that you can
sahilmgandhi 18:6a4db94011d3 152 * call the function same function again on the following
sahilmgandhi 18:6a4db94011d3 153 * block(s) of data and get the same result as if it was
sahilmgandhi 18:6a4db94011d3 154 * encrypted in one call. This allows a "streaming" usage.
sahilmgandhi 18:6a4db94011d3 155 * If on the other hand you need to retain the contents of the
sahilmgandhi 18:6a4db94011d3 156 * IV, you should either save it manually or use the cipher
sahilmgandhi 18:6a4db94011d3 157 * module instead.
sahilmgandhi 18:6a4db94011d3 158 *
sahilmgandhi 18:6a4db94011d3 159 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 160 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
sahilmgandhi 18:6a4db94011d3 161 * \param length length of the input data
sahilmgandhi 18:6a4db94011d3 162 * \param iv_off offset in IV (updated after use)
sahilmgandhi 18:6a4db94011d3 163 * \param iv initialization vector (updated after use)
sahilmgandhi 18:6a4db94011d3 164 * \param input buffer holding the input data
sahilmgandhi 18:6a4db94011d3 165 * \param output buffer holding the output data
sahilmgandhi 18:6a4db94011d3 166 *
sahilmgandhi 18:6a4db94011d3 167 * \return 0 if successful
sahilmgandhi 18:6a4db94011d3 168 */
sahilmgandhi 18:6a4db94011d3 169 int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 170 int mode,
sahilmgandhi 18:6a4db94011d3 171 size_t length,
sahilmgandhi 18:6a4db94011d3 172 size_t *iv_off,
sahilmgandhi 18:6a4db94011d3 173 unsigned char iv[16],
sahilmgandhi 18:6a4db94011d3 174 const unsigned char *input,
sahilmgandhi 18:6a4db94011d3 175 unsigned char *output );
sahilmgandhi 18:6a4db94011d3 176
sahilmgandhi 18:6a4db94011d3 177 /**
sahilmgandhi 18:6a4db94011d3 178 * \brief AES-CFB8 buffer encryption/decryption.
sahilmgandhi 18:6a4db94011d3 179 *
sahilmgandhi 18:6a4db94011d3 180 * Note: Due to the nature of CFB you should use the same key schedule for
sahilmgandhi 18:6a4db94011d3 181 * both encryption and decryption. So a context initialized with
sahilmgandhi 18:6a4db94011d3 182 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
sahilmgandhi 18:6a4db94011d3 183 *
sahilmgandhi 18:6a4db94011d3 184 * \note Upon exit, the content of the IV is updated so that you can
sahilmgandhi 18:6a4db94011d3 185 * call the function same function again on the following
sahilmgandhi 18:6a4db94011d3 186 * block(s) of data and get the same result as if it was
sahilmgandhi 18:6a4db94011d3 187 * encrypted in one call. This allows a "streaming" usage.
sahilmgandhi 18:6a4db94011d3 188 * If on the other hand you need to retain the contents of the
sahilmgandhi 18:6a4db94011d3 189 * IV, you should either save it manually or use the cipher
sahilmgandhi 18:6a4db94011d3 190 * module instead.
sahilmgandhi 18:6a4db94011d3 191 *
sahilmgandhi 18:6a4db94011d3 192 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 193 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
sahilmgandhi 18:6a4db94011d3 194 * \param length length of the input data
sahilmgandhi 18:6a4db94011d3 195 * \param iv initialization vector (updated after use)
sahilmgandhi 18:6a4db94011d3 196 * \param input buffer holding the input data
sahilmgandhi 18:6a4db94011d3 197 * \param output buffer holding the output data
sahilmgandhi 18:6a4db94011d3 198 *
sahilmgandhi 18:6a4db94011d3 199 * \return 0 if successful
sahilmgandhi 18:6a4db94011d3 200 */
sahilmgandhi 18:6a4db94011d3 201 int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 202 int mode,
sahilmgandhi 18:6a4db94011d3 203 size_t length,
sahilmgandhi 18:6a4db94011d3 204 unsigned char iv[16],
sahilmgandhi 18:6a4db94011d3 205 const unsigned char *input,
sahilmgandhi 18:6a4db94011d3 206 unsigned char *output );
sahilmgandhi 18:6a4db94011d3 207 #endif /*MBEDTLS_CIPHER_MODE_CFB */
sahilmgandhi 18:6a4db94011d3 208
sahilmgandhi 18:6a4db94011d3 209 #if defined(MBEDTLS_CIPHER_MODE_CTR)
sahilmgandhi 18:6a4db94011d3 210 /**
sahilmgandhi 18:6a4db94011d3 211 * \brief AES-CTR buffer encryption/decryption
sahilmgandhi 18:6a4db94011d3 212 *
sahilmgandhi 18:6a4db94011d3 213 * Warning: You have to keep the maximum use of your counter in mind!
sahilmgandhi 18:6a4db94011d3 214 *
sahilmgandhi 18:6a4db94011d3 215 * Note: Due to the nature of CTR you should use the same key schedule for
sahilmgandhi 18:6a4db94011d3 216 * both encryption and decryption. So a context initialized with
sahilmgandhi 18:6a4db94011d3 217 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
sahilmgandhi 18:6a4db94011d3 218 *
sahilmgandhi 18:6a4db94011d3 219 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 220 * \param length The length of the data
sahilmgandhi 18:6a4db94011d3 221 * \param nc_off The offset in the current stream_block (for resuming
sahilmgandhi 18:6a4db94011d3 222 * within current cipher stream). The offset pointer to
sahilmgandhi 18:6a4db94011d3 223 * should be 0 at the start of a stream.
sahilmgandhi 18:6a4db94011d3 224 * \param nonce_counter The 128-bit nonce and counter.
sahilmgandhi 18:6a4db94011d3 225 * \param stream_block The saved stream-block for resuming. Is overwritten
sahilmgandhi 18:6a4db94011d3 226 * by the function.
sahilmgandhi 18:6a4db94011d3 227 * \param input The input data stream
sahilmgandhi 18:6a4db94011d3 228 * \param output The output data stream
sahilmgandhi 18:6a4db94011d3 229 *
sahilmgandhi 18:6a4db94011d3 230 * \return 0 if successful
sahilmgandhi 18:6a4db94011d3 231 */
sahilmgandhi 18:6a4db94011d3 232 int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 233 size_t length,
sahilmgandhi 18:6a4db94011d3 234 size_t *nc_off,
sahilmgandhi 18:6a4db94011d3 235 unsigned char nonce_counter[16],
sahilmgandhi 18:6a4db94011d3 236 unsigned char stream_block[16],
sahilmgandhi 18:6a4db94011d3 237 const unsigned char *input,
sahilmgandhi 18:6a4db94011d3 238 unsigned char *output );
sahilmgandhi 18:6a4db94011d3 239 #endif /* MBEDTLS_CIPHER_MODE_CTR */
sahilmgandhi 18:6a4db94011d3 240
sahilmgandhi 18:6a4db94011d3 241 /**
sahilmgandhi 18:6a4db94011d3 242 * \brief Internal AES block encryption function
sahilmgandhi 18:6a4db94011d3 243 * (Only exposed to allow overriding it,
sahilmgandhi 18:6a4db94011d3 244 * see MBEDTLS_AES_ENCRYPT_ALT)
sahilmgandhi 18:6a4db94011d3 245 *
sahilmgandhi 18:6a4db94011d3 246 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 247 * \param input Plaintext block
sahilmgandhi 18:6a4db94011d3 248 * \param output Output (ciphertext) block
sahilmgandhi 18:6a4db94011d3 249 */
sahilmgandhi 18:6a4db94011d3 250 void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 251 const unsigned char input[16],
sahilmgandhi 18:6a4db94011d3 252 unsigned char output[16] );
sahilmgandhi 18:6a4db94011d3 253
sahilmgandhi 18:6a4db94011d3 254 /**
sahilmgandhi 18:6a4db94011d3 255 * \brief Internal AES block decryption function
sahilmgandhi 18:6a4db94011d3 256 * (Only exposed to allow overriding it,
sahilmgandhi 18:6a4db94011d3 257 * see MBEDTLS_AES_DECRYPT_ALT)
sahilmgandhi 18:6a4db94011d3 258 *
sahilmgandhi 18:6a4db94011d3 259 * \param ctx AES context
sahilmgandhi 18:6a4db94011d3 260 * \param input Ciphertext block
sahilmgandhi 18:6a4db94011d3 261 * \param output Output (plaintext) block
sahilmgandhi 18:6a4db94011d3 262 */
sahilmgandhi 18:6a4db94011d3 263 void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
sahilmgandhi 18:6a4db94011d3 264 const unsigned char input[16],
sahilmgandhi 18:6a4db94011d3 265 unsigned char output[16] );
sahilmgandhi 18:6a4db94011d3 266
sahilmgandhi 18:6a4db94011d3 267 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 268 }
sahilmgandhi 18:6a4db94011d3 269 #endif
sahilmgandhi 18:6a4db94011d3 270
sahilmgandhi 18:6a4db94011d3 271
sahilmgandhi 18:6a4db94011d3 272 #endif /* MBEDTLS_AES_ALT */
sahilmgandhi 18:6a4db94011d3 273
sahilmgandhi 18:6a4db94011d3 274