Small AES_ECB with T-box
Dependents: BLE_LED_ADC_BBC BLE_BBC_LSM303 BLE_BBC_LSM303_MAG BLE_BBC_LSM303_MAG ... more
small_aes.h@1:0075ec28b9dd, 2012-10-10 (annotated)
- Committer:
- razueroh
- Date:
- Wed Oct 10 22:46:20 2012 +0000
- Revision:
- 1:0075ec28b9dd
- Parent:
- 0:27d3a972ad80
- Child:
- 2:4997f825ee95
Added string.h for compatibility
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
razueroh | 0:27d3a972ad80 | 1 | /* small_aes.h |
razueroh | 0:27d3a972ad80 | 2 | * |
razueroh | 0:27d3a972ad80 | 3 | * Copyright (c) 2012 Rafael Azuero Hurtado - German Londoño Paez |
razueroh | 0:27d3a972ad80 | 4 | * |
razueroh | 0:27d3a972ad80 | 5 | * This program is free software: you can redistribute it and/or modify |
razueroh | 0:27d3a972ad80 | 6 | * it under the terms of the GNU General Public License as published by |
razueroh | 0:27d3a972ad80 | 7 | * the Free Software Foundation, either version 3 of the License, or |
razueroh | 0:27d3a972ad80 | 8 | * (at your option) any later version. |
razueroh | 0:27d3a972ad80 | 9 | * |
razueroh | 0:27d3a972ad80 | 10 | * This program is distributed in the hope that it will be useful, |
razueroh | 0:27d3a972ad80 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
razueroh | 0:27d3a972ad80 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
razueroh | 0:27d3a972ad80 | 13 | * GNU General Public License for more details. |
razueroh | 0:27d3a972ad80 | 14 | * |
razueroh | 0:27d3a972ad80 | 15 | * You should have received a copy of the GNU General Public License |
razueroh | 0:27d3a972ad80 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
razueroh | 0:27d3a972ad80 | 17 | */ |
razueroh | 0:27d3a972ad80 | 18 | |
razueroh | 0:27d3a972ad80 | 19 | /** |
razueroh | 0:27d3a972ad80 | 20 | * @file small_aes.h |
razueroh | 0:27d3a972ad80 | 21 | * @brief Small AES with T-box |
razueroh | 0:27d3a972ad80 | 22 | */ |
razueroh | 0:27d3a972ad80 | 23 | |
razueroh | 0:27d3a972ad80 | 24 | #ifndef _SMALL_AES_H_ |
razueroh | 0:27d3a972ad80 | 25 | #define _SMALL_AES_H_ |
razueroh | 0:27d3a972ad80 | 26 | |
razueroh | 1:0075ec28b9dd | 27 | #include "string.h" |
razueroh | 0:27d3a972ad80 | 28 | |
razueroh | 0:27d3a972ad80 | 29 | #ifdef __cplusplus |
razueroh | 0:27d3a972ad80 | 30 | extern "C" { |
razueroh | 0:27d3a972ad80 | 31 | #endif |
razueroh | 0:27d3a972ad80 | 32 | |
razueroh | 0:27d3a972ad80 | 33 | enum { |
razueroh | 0:27d3a972ad80 | 34 | SMALL_AES_ENCRYPTION = 0, |
razueroh | 0:27d3a972ad80 | 35 | SMALL_AES_DECRYPTION = 1, |
razueroh | 0:27d3a972ad80 | 36 | SMALL_AES_BLOCK_SIZE = 16 |
razueroh | 0:27d3a972ad80 | 37 | }; |
razueroh | 0:27d3a972ad80 | 38 | |
razueroh | 0:27d3a972ad80 | 39 | typedef struct AES { |
razueroh | 0:27d3a972ad80 | 40 | unsigned int key[60]; |
razueroh | 0:27d3a972ad80 | 41 | unsigned int rounds; |
razueroh | 0:27d3a972ad80 | 42 | |
razueroh | 0:27d3a972ad80 | 43 | unsigned int reg[SMALL_AES_BLOCK_SIZE / sizeof(unsigned int)]; |
razueroh | 0:27d3a972ad80 | 44 | } AES; |
razueroh | 0:27d3a972ad80 | 45 | |
razueroh | 0:27d3a972ad80 | 46 | /** |
razueroh | 0:27d3a972ad80 | 47 | * @fn int aesSetKey(AES* aes, const unsigned char *key, unsigned int len, int dir) |
razueroh | 0:27d3a972ad80 | 48 | * @brief Sets AES key |
razueroh | 0:27d3a972ad80 | 49 | * @param aes The AES struct |
razueroh | 0:27d3a972ad80 | 50 | * @param key The key bytes |
razueroh | 0:27d3a972ad80 | 51 | * @param len The Key length |
razueroh | 0:27d3a972ad80 | 52 | * @param dir If encrypt or decrypt |
razueroh | 0:27d3a972ad80 | 53 | */ |
razueroh | 0:27d3a972ad80 | 54 | int aesSetKey(AES* aes, const unsigned char *key, unsigned int len, int dir); |
razueroh | 0:27d3a972ad80 | 55 | |
razueroh | 0:27d3a972ad80 | 56 | /** |
razueroh | 0:27d3a972ad80 | 57 | * @fn void aesEncrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock) |
razueroh | 0:27d3a972ad80 | 58 | * @brief Encrypts a 16-byte block |
razueroh | 0:27d3a972ad80 | 59 | * @param aes The AES struct |
razueroh | 0:27d3a972ad80 | 60 | * @param inBlock The 16-byte input block |
razueroh | 0:27d3a972ad80 | 61 | * @param outBlock The 16-byte output block |
razueroh | 0:27d3a972ad80 | 62 | */ |
razueroh | 0:27d3a972ad80 | 63 | void aesEncrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock); |
razueroh | 0:27d3a972ad80 | 64 | |
razueroh | 0:27d3a972ad80 | 65 | /** |
razueroh | 0:27d3a972ad80 | 66 | * @fn void aesDecrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock) |
razueroh | 0:27d3a972ad80 | 67 | * @brief Decrypts a 16-byte block |
razueroh | 0:27d3a972ad80 | 68 | * @param aes The AES struct |
razueroh | 0:27d3a972ad80 | 69 | * @param inBlock The 16-byte input block |
razueroh | 0:27d3a972ad80 | 70 | * @param outBlock The 16-byte output block |
razueroh | 0:27d3a972ad80 | 71 | */ |
razueroh | 0:27d3a972ad80 | 72 | void aesDecrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock); |
razueroh | 0:27d3a972ad80 | 73 | |
razueroh | 0:27d3a972ad80 | 74 | /** |
razueroh | 0:27d3a972ad80 | 75 | * @fn void aesEcbEncrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize) |
razueroh | 0:27d3a972ad80 | 76 | * @brief Encrypts a n-byte block with AES-ECB |
razueroh | 0:27d3a972ad80 | 77 | * @param aes The AES struct |
razueroh | 0:27d3a972ad80 | 78 | * @param output The input data |
razueroh | 0:27d3a972ad80 | 79 | * @param input The output data |
razueroh | 0:27d3a972ad80 | 80 | * @param inputSize The input data size in bytes |
razueroh | 0:27d3a972ad80 | 81 | */ |
razueroh | 0:27d3a972ad80 | 82 | void aesEcbEncrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize); |
razueroh | 0:27d3a972ad80 | 83 | |
razueroh | 0:27d3a972ad80 | 84 | /** |
razueroh | 0:27d3a972ad80 | 85 | * @fn void aesEcbDecrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize) |
razueroh | 0:27d3a972ad80 | 86 | * @brief Decrypts a n-bytes block with AES-ECB |
razueroh | 0:27d3a972ad80 | 87 | * @param aes The AES struct |
razueroh | 0:27d3a972ad80 | 88 | * @param output The input data |
razueroh | 0:27d3a972ad80 | 89 | * @param input The output data |
razueroh | 0:27d3a972ad80 | 90 | * @param inputSize The input data size in bytes |
razueroh | 0:27d3a972ad80 | 91 | */ |
razueroh | 0:27d3a972ad80 | 92 | void aesEcbDecrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize); |
razueroh | 0:27d3a972ad80 | 93 | |
razueroh | 0:27d3a972ad80 | 94 | /** |
razueroh | 0:27d3a972ad80 | 95 | * @fn unsigned int byteReverseWord32(unsigned int value) |
razueroh | 0:27d3a972ad80 | 96 | * @brief Converts a big-endian 32-bit word to little-endian format |
razueroh | 0:27d3a972ad80 | 97 | * @param value The big-endian 32-bit word |
razueroh | 0:27d3a972ad80 | 98 | */ |
razueroh | 0:27d3a972ad80 | 99 | unsigned int byteReverseWord32(unsigned int value); |
razueroh | 0:27d3a972ad80 | 100 | |
razueroh | 0:27d3a972ad80 | 101 | /** |
razueroh | 0:27d3a972ad80 | 102 | * @fn void byteReverseWords(unsigned int *out, const unsigned int *in, unsigned int byteCount) |
razueroh | 0:27d3a972ad80 | 103 | * @brief Converts a big-endian block to little-endian format |
razueroh | 0:27d3a972ad80 | 104 | * @param out The output block |
razueroh | 0:27d3a972ad80 | 105 | * @param in The input block |
razueroh | 0:27d3a972ad80 | 106 | * @param byteCount The Number of bytes |
razueroh | 0:27d3a972ad80 | 107 | */ |
razueroh | 0:27d3a972ad80 | 108 | void byteReverseWords(unsigned int *out, const unsigned int *in, unsigned int byteCount); |
razueroh | 0:27d3a972ad80 | 109 | |
razueroh | 0:27d3a972ad80 | 110 | /** |
razueroh | 0:27d3a972ad80 | 111 | * @fn unsigned int rotlFixed(unsigned int x, unsigned int y) |
razueroh | 0:27d3a972ad80 | 112 | * @brief Rotates a 32-bit word n-bytes to the left |
razueroh | 0:27d3a972ad80 | 113 | * @param x The32-bit word |
razueroh | 0:27d3a972ad80 | 114 | * @param y The number of bytes to rotate |
razueroh | 0:27d3a972ad80 | 115 | */ |
razueroh | 0:27d3a972ad80 | 116 | unsigned int rotlFixed(unsigned int x, unsigned int y); |
razueroh | 0:27d3a972ad80 | 117 | |
razueroh | 0:27d3a972ad80 | 118 | /** |
razueroh | 0:27d3a972ad80 | 119 | * @fn unsigned int rotrFixed(unsigned int x, unsigned int y) |
razueroh | 0:27d3a972ad80 | 120 | * @brief Rotates a 32-bit word n-bytes to the right |
razueroh | 0:27d3a972ad80 | 121 | * @param x The 32-bit word |
razueroh | 0:27d3a972ad80 | 122 | * @param y The number of bytes to rotate |
razueroh | 0:27d3a972ad80 | 123 | */ |
razueroh | 0:27d3a972ad80 | 124 | unsigned int rotrFixed(unsigned int x, unsigned int y); |
razueroh | 0:27d3a972ad80 | 125 | |
razueroh | 0:27d3a972ad80 | 126 | |
razueroh | 0:27d3a972ad80 | 127 | #ifdef __cplusplus |
razueroh | 0:27d3a972ad80 | 128 | } /* extern "C" */ |
razueroh | 0:27d3a972ad80 | 129 | #endif |
razueroh | 0:27d3a972ad80 | 130 | |
razueroh | 0:27d3a972ad80 | 131 | #endif |