Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /* mbed Microcontroller Library
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2015-2016 Nuvoton
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 5 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 6 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 13 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 14 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 15 */
sahilmgandhi 18:6a4db94011d3 16
sahilmgandhi 18:6a4db94011d3 17 #ifndef MBEDTLS_DES_ALT_H
sahilmgandhi 18:6a4db94011d3 18 #define MBEDTLS_DES_ALT_H
sahilmgandhi 18:6a4db94011d3 19
sahilmgandhi 18:6a4db94011d3 20 #if !defined(MBEDTLS_CONFIG_FILE)
sahilmgandhi 18:6a4db94011d3 21 #include "mbedtls/config.h"
sahilmgandhi 18:6a4db94011d3 22 #else
sahilmgandhi 18:6a4db94011d3 23 #include MBEDTLS_CONFIG_FILE
sahilmgandhi 18:6a4db94011d3 24 #endif
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 #if defined(MBEDTLS_DES_ALT)
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 #include <stddef.h>
sahilmgandhi 18:6a4db94011d3 29 #include <stdint.h>
sahilmgandhi 18:6a4db94011d3 30 #include "des.h"
sahilmgandhi 18:6a4db94011d3 31 #include "des_alt_sw.h"
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 34 extern "C" {
sahilmgandhi 18:6a4db94011d3 35 #endif
sahilmgandhi 18:6a4db94011d3 36
sahilmgandhi 18:6a4db94011d3 37 /**
sahilmgandhi 18:6a4db94011d3 38 * \brief DES context structure
sahilmgandhi 18:6a4db94011d3 39 */
sahilmgandhi 18:6a4db94011d3 40 typedef struct
sahilmgandhi 18:6a4db94011d3 41 {
sahilmgandhi 18:6a4db94011d3 42 int enc; /*!< 0: dec, 1: enc */
sahilmgandhi 18:6a4db94011d3 43 uint16_t keyopt;
sahilmgandhi 18:6a4db94011d3 44 uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
sahilmgandhi 18:6a4db94011d3 45 }
sahilmgandhi 18:6a4db94011d3 46 mbedtls_des_context;
sahilmgandhi 18:6a4db94011d3 47
sahilmgandhi 18:6a4db94011d3 48 /**
sahilmgandhi 18:6a4db94011d3 49 * \brief Triple-DES context structure
sahilmgandhi 18:6a4db94011d3 50 */
sahilmgandhi 18:6a4db94011d3 51 typedef struct
sahilmgandhi 18:6a4db94011d3 52 {
sahilmgandhi 18:6a4db94011d3 53 int enc; /*!< 0: dec, 1: enc */
sahilmgandhi 18:6a4db94011d3 54 uint16_t keyopt;
sahilmgandhi 18:6a4db94011d3 55 uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
sahilmgandhi 18:6a4db94011d3 56 }
sahilmgandhi 18:6a4db94011d3 57 mbedtls_des3_context;
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 /**
sahilmgandhi 18:6a4db94011d3 60 * \brief Initialize DES context
sahilmgandhi 18:6a4db94011d3 61 *
sahilmgandhi 18:6a4db94011d3 62 * \param ctx DES context to be initialized
sahilmgandhi 18:6a4db94011d3 63 */
sahilmgandhi 18:6a4db94011d3 64 void mbedtls_des_init( mbedtls_des_context *ctx );
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 /**
sahilmgandhi 18:6a4db94011d3 67 * \brief Clear DES context
sahilmgandhi 18:6a4db94011d3 68 *
sahilmgandhi 18:6a4db94011d3 69 * \param ctx DES context to be cleared
sahilmgandhi 18:6a4db94011d3 70 */
sahilmgandhi 18:6a4db94011d3 71 void mbedtls_des_free( mbedtls_des_context *ctx );
sahilmgandhi 18:6a4db94011d3 72
sahilmgandhi 18:6a4db94011d3 73 /**
sahilmgandhi 18:6a4db94011d3 74 * \brief Initialize Triple-DES context
sahilmgandhi 18:6a4db94011d3 75 *
sahilmgandhi 18:6a4db94011d3 76 * \param ctx DES3 context to be initialized
sahilmgandhi 18:6a4db94011d3 77 */
sahilmgandhi 18:6a4db94011d3 78 void mbedtls_des3_init( mbedtls_des3_context *ctx );
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 /**
sahilmgandhi 18:6a4db94011d3 81 * \brief Clear Triple-DES context
sahilmgandhi 18:6a4db94011d3 82 *
sahilmgandhi 18:6a4db94011d3 83 * \param ctx DES3 context to be cleared
sahilmgandhi 18:6a4db94011d3 84 */
sahilmgandhi 18:6a4db94011d3 85 void mbedtls_des3_free( mbedtls_des3_context *ctx );
sahilmgandhi 18:6a4db94011d3 86
sahilmgandhi 18:6a4db94011d3 87 /**
sahilmgandhi 18:6a4db94011d3 88 * \brief Set key parity on the given key to odd.
sahilmgandhi 18:6a4db94011d3 89 *
sahilmgandhi 18:6a4db94011d3 90 * DES keys are 56 bits long, but each byte is padded with
sahilmgandhi 18:6a4db94011d3 91 * a parity bit to allow verification.
sahilmgandhi 18:6a4db94011d3 92 *
sahilmgandhi 18:6a4db94011d3 93 * \param key 8-byte secret key
sahilmgandhi 18:6a4db94011d3 94 */
sahilmgandhi 18:6a4db94011d3 95 void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
sahilmgandhi 18:6a4db94011d3 96
sahilmgandhi 18:6a4db94011d3 97 /**
sahilmgandhi 18:6a4db94011d3 98 * \brief Check that key parity on the given key is odd.
sahilmgandhi 18:6a4db94011d3 99 *
sahilmgandhi 18:6a4db94011d3 100 * DES keys are 56 bits long, but each byte is padded with
sahilmgandhi 18:6a4db94011d3 101 * a parity bit to allow verification.
sahilmgandhi 18:6a4db94011d3 102 *
sahilmgandhi 18:6a4db94011d3 103 * \param key 8-byte secret key
sahilmgandhi 18:6a4db94011d3 104 *
sahilmgandhi 18:6a4db94011d3 105 * \return 0 is parity was ok, 1 if parity was not correct.
sahilmgandhi 18:6a4db94011d3 106 */
sahilmgandhi 18:6a4db94011d3 107 int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
sahilmgandhi 18:6a4db94011d3 108
sahilmgandhi 18:6a4db94011d3 109 /**
sahilmgandhi 18:6a4db94011d3 110 * \brief Check that key is not a weak or semi-weak DES key
sahilmgandhi 18:6a4db94011d3 111 *
sahilmgandhi 18:6a4db94011d3 112 * \param key 8-byte secret key
sahilmgandhi 18:6a4db94011d3 113 *
sahilmgandhi 18:6a4db94011d3 114 * \return 0 if no weak key was found, 1 if a weak key was identified.
sahilmgandhi 18:6a4db94011d3 115 */
sahilmgandhi 18:6a4db94011d3 116 int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
sahilmgandhi 18:6a4db94011d3 117
sahilmgandhi 18:6a4db94011d3 118 /**
sahilmgandhi 18:6a4db94011d3 119 * \brief DES key schedule (56-bit, encryption)
sahilmgandhi 18:6a4db94011d3 120 *
sahilmgandhi 18:6a4db94011d3 121 * \param ctx DES context to be initialized
sahilmgandhi 18:6a4db94011d3 122 * \param key 8-byte secret key
sahilmgandhi 18:6a4db94011d3 123 *
sahilmgandhi 18:6a4db94011d3 124 * \return 0
sahilmgandhi 18:6a4db94011d3 125 */
sahilmgandhi 18:6a4db94011d3 126 int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
sahilmgandhi 18:6a4db94011d3 127
sahilmgandhi 18:6a4db94011d3 128 /**
sahilmgandhi 18:6a4db94011d3 129 * \brief DES key schedule (56-bit, decryption)
sahilmgandhi 18:6a4db94011d3 130 *
sahilmgandhi 18:6a4db94011d3 131 * \param ctx DES context to be initialized
sahilmgandhi 18:6a4db94011d3 132 * \param key 8-byte secret key
sahilmgandhi 18:6a4db94011d3 133 *
sahilmgandhi 18:6a4db94011d3 134 * \return 0
sahilmgandhi 18:6a4db94011d3 135 */
sahilmgandhi 18:6a4db94011d3 136 int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
sahilmgandhi 18:6a4db94011d3 137
sahilmgandhi 18:6a4db94011d3 138 /**
sahilmgandhi 18:6a4db94011d3 139 * \brief Triple-DES key schedule (112-bit, encryption)
sahilmgandhi 18:6a4db94011d3 140 *
sahilmgandhi 18:6a4db94011d3 141 * \param ctx 3DES context to be initialized
sahilmgandhi 18:6a4db94011d3 142 * \param key 16-byte secret key
sahilmgandhi 18:6a4db94011d3 143 *
sahilmgandhi 18:6a4db94011d3 144 * \return 0
sahilmgandhi 18:6a4db94011d3 145 */
sahilmgandhi 18:6a4db94011d3 146 int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
sahilmgandhi 18:6a4db94011d3 147 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
sahilmgandhi 18:6a4db94011d3 148
sahilmgandhi 18:6a4db94011d3 149 /**
sahilmgandhi 18:6a4db94011d3 150 * \brief Triple-DES key schedule (112-bit, decryption)
sahilmgandhi 18:6a4db94011d3 151 *
sahilmgandhi 18:6a4db94011d3 152 * \param ctx 3DES context to be initialized
sahilmgandhi 18:6a4db94011d3 153 * \param key 16-byte secret key
sahilmgandhi 18:6a4db94011d3 154 *
sahilmgandhi 18:6a4db94011d3 155 * \return 0
sahilmgandhi 18:6a4db94011d3 156 */
sahilmgandhi 18:6a4db94011d3 157 int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
sahilmgandhi 18:6a4db94011d3 158 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
sahilmgandhi 18:6a4db94011d3 159
sahilmgandhi 18:6a4db94011d3 160 /**
sahilmgandhi 18:6a4db94011d3 161 * \brief Triple-DES key schedule (168-bit, encryption)
sahilmgandhi 18:6a4db94011d3 162 *
sahilmgandhi 18:6a4db94011d3 163 * \param ctx 3DES context to be initialized
sahilmgandhi 18:6a4db94011d3 164 * \param key 24-byte secret key
sahilmgandhi 18:6a4db94011d3 165 *
sahilmgandhi 18:6a4db94011d3 166 * \return 0
sahilmgandhi 18:6a4db94011d3 167 */
sahilmgandhi 18:6a4db94011d3 168 int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
sahilmgandhi 18:6a4db94011d3 169 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
sahilmgandhi 18:6a4db94011d3 170
sahilmgandhi 18:6a4db94011d3 171 /**
sahilmgandhi 18:6a4db94011d3 172 * \brief Triple-DES key schedule (168-bit, decryption)
sahilmgandhi 18:6a4db94011d3 173 *
sahilmgandhi 18:6a4db94011d3 174 * \param ctx 3DES context to be initialized
sahilmgandhi 18:6a4db94011d3 175 * \param key 24-byte secret key
sahilmgandhi 18:6a4db94011d3 176 *
sahilmgandhi 18:6a4db94011d3 177 * \return 0
sahilmgandhi 18:6a4db94011d3 178 */
sahilmgandhi 18:6a4db94011d3 179 int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
sahilmgandhi 18:6a4db94011d3 180 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
sahilmgandhi 18:6a4db94011d3 181
sahilmgandhi 18:6a4db94011d3 182 /**
sahilmgandhi 18:6a4db94011d3 183 * \brief DES-ECB block encryption/decryption
sahilmgandhi 18:6a4db94011d3 184 *
sahilmgandhi 18:6a4db94011d3 185 * \param ctx DES context
sahilmgandhi 18:6a4db94011d3 186 * \param input 64-bit input block
sahilmgandhi 18:6a4db94011d3 187 * \param output 64-bit output block
sahilmgandhi 18:6a4db94011d3 188 *
sahilmgandhi 18:6a4db94011d3 189 * \return 0 if successful
sahilmgandhi 18:6a4db94011d3 190 */
sahilmgandhi 18:6a4db94011d3 191 int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
sahilmgandhi 18:6a4db94011d3 192 const unsigned char input[8],
sahilmgandhi 18:6a4db94011d3 193 unsigned char output[8] );
sahilmgandhi 18:6a4db94011d3 194
sahilmgandhi 18:6a4db94011d3 195 #if defined(MBEDTLS_CIPHER_MODE_CBC)
sahilmgandhi 18:6a4db94011d3 196 /**
sahilmgandhi 18:6a4db94011d3 197 * \brief DES-CBC buffer encryption/decryption
sahilmgandhi 18:6a4db94011d3 198 *
sahilmgandhi 18:6a4db94011d3 199 * \note Upon exit, the content of the IV is updated so that you can
sahilmgandhi 18:6a4db94011d3 200 * call the function same function again on the following
sahilmgandhi 18:6a4db94011d3 201 * block(s) of data and get the same result as if it was
sahilmgandhi 18:6a4db94011d3 202 * encrypted in one call. This allows a "streaming" usage.
sahilmgandhi 18:6a4db94011d3 203 * If on the other hand you need to retain the contents of the
sahilmgandhi 18:6a4db94011d3 204 * IV, you should either save it manually or use the cipher
sahilmgandhi 18:6a4db94011d3 205 * module instead.
sahilmgandhi 18:6a4db94011d3 206 *
sahilmgandhi 18:6a4db94011d3 207 * \param ctx DES context
sahilmgandhi 18:6a4db94011d3 208 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
sahilmgandhi 18:6a4db94011d3 209 * \param length length of the input data
sahilmgandhi 18:6a4db94011d3 210 * \param iv initialization vector (updated after use)
sahilmgandhi 18:6a4db94011d3 211 * \param input buffer holding the input data
sahilmgandhi 18:6a4db94011d3 212 * \param output buffer holding the output data
sahilmgandhi 18:6a4db94011d3 213 */
sahilmgandhi 18:6a4db94011d3 214 int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
sahilmgandhi 18:6a4db94011d3 215 int mode,
sahilmgandhi 18:6a4db94011d3 216 size_t length,
sahilmgandhi 18:6a4db94011d3 217 unsigned char iv[8],
sahilmgandhi 18:6a4db94011d3 218 const unsigned char *input,
sahilmgandhi 18:6a4db94011d3 219 unsigned char *output );
sahilmgandhi 18:6a4db94011d3 220 #endif /* MBEDTLS_CIPHER_MODE_CBC */
sahilmgandhi 18:6a4db94011d3 221
sahilmgandhi 18:6a4db94011d3 222 /**
sahilmgandhi 18:6a4db94011d3 223 * \brief 3DES-ECB block encryption/decryption
sahilmgandhi 18:6a4db94011d3 224 *
sahilmgandhi 18:6a4db94011d3 225 * \param ctx 3DES context
sahilmgandhi 18:6a4db94011d3 226 * \param input 64-bit input block
sahilmgandhi 18:6a4db94011d3 227 * \param output 64-bit output block
sahilmgandhi 18:6a4db94011d3 228 *
sahilmgandhi 18:6a4db94011d3 229 * \return 0 if successful
sahilmgandhi 18:6a4db94011d3 230 */
sahilmgandhi 18:6a4db94011d3 231 int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
sahilmgandhi 18:6a4db94011d3 232 const unsigned char input[8],
sahilmgandhi 18:6a4db94011d3 233 unsigned char output[8] );
sahilmgandhi 18:6a4db94011d3 234
sahilmgandhi 18:6a4db94011d3 235 #if defined(MBEDTLS_CIPHER_MODE_CBC)
sahilmgandhi 18:6a4db94011d3 236 /**
sahilmgandhi 18:6a4db94011d3 237 * \brief 3DES-CBC buffer encryption/decryption
sahilmgandhi 18:6a4db94011d3 238 *
sahilmgandhi 18:6a4db94011d3 239 * \note Upon exit, the content of the IV is updated so that you can
sahilmgandhi 18:6a4db94011d3 240 * call the function same function again on the following
sahilmgandhi 18:6a4db94011d3 241 * block(s) of data and get the same result as if it was
sahilmgandhi 18:6a4db94011d3 242 * encrypted in one call. This allows a "streaming" usage.
sahilmgandhi 18:6a4db94011d3 243 * If on the other hand you need to retain the contents of the
sahilmgandhi 18:6a4db94011d3 244 * IV, you should either save it manually or use the cipher
sahilmgandhi 18:6a4db94011d3 245 * module instead.
sahilmgandhi 18:6a4db94011d3 246 *
sahilmgandhi 18:6a4db94011d3 247 * \param ctx 3DES context
sahilmgandhi 18:6a4db94011d3 248 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
sahilmgandhi 18:6a4db94011d3 249 * \param length length of the input data
sahilmgandhi 18:6a4db94011d3 250 * \param iv initialization vector (updated after use)
sahilmgandhi 18:6a4db94011d3 251 * \param input buffer holding the input data
sahilmgandhi 18:6a4db94011d3 252 * \param output buffer holding the output data
sahilmgandhi 18:6a4db94011d3 253 *
sahilmgandhi 18:6a4db94011d3 254 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
sahilmgandhi 18:6a4db94011d3 255 */
sahilmgandhi 18:6a4db94011d3 256 int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
sahilmgandhi 18:6a4db94011d3 257 int mode,
sahilmgandhi 18:6a4db94011d3 258 size_t length,
sahilmgandhi 18:6a4db94011d3 259 unsigned char iv[8],
sahilmgandhi 18:6a4db94011d3 260 const unsigned char *input,
sahilmgandhi 18:6a4db94011d3 261 unsigned char *output );
sahilmgandhi 18:6a4db94011d3 262 #endif /* MBEDTLS_CIPHER_MODE_CBC */
sahilmgandhi 18:6a4db94011d3 263
sahilmgandhi 18:6a4db94011d3 264 /**
sahilmgandhi 18:6a4db94011d3 265 * \brief Internal function for key expansion.
sahilmgandhi 18:6a4db94011d3 266 * (Only exposed to allow overriding it,
sahilmgandhi 18:6a4db94011d3 267 * see MBEDTLS_DES_SETKEY_ALT)
sahilmgandhi 18:6a4db94011d3 268 *
sahilmgandhi 18:6a4db94011d3 269 * \param SK Round keys
sahilmgandhi 18:6a4db94011d3 270 * \param key Base key
sahilmgandhi 18:6a4db94011d3 271 */
sahilmgandhi 18:6a4db94011d3 272 void mbedtls_des_setkey( uint32_t SK[32],
sahilmgandhi 18:6a4db94011d3 273 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
sahilmgandhi 18:6a4db94011d3 274 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 275 }
sahilmgandhi 18:6a4db94011d3 276 #endif
sahilmgandhi 18:6a4db94011d3 277
sahilmgandhi 18:6a4db94011d3 278 #endif /* MBEDTLS_DES_ALT */
sahilmgandhi 18:6a4db94011d3 279
sahilmgandhi 18:6a4db94011d3 280 #endif /* des_alt.h */