Small AES_ECB with T-box

Dependents:   BLE_LED_ADC_BBC BLE_BBC_LSM303 BLE_BBC_LSM303_MAG BLE_BBC_LSM303_MAG ... more

Committer:
razueroh
Date:
Thu Oct 11 17:09:23 2012 +0000
Revision:
2:4997f825ee95
Parent:
1:0075ec28b9dd
Documentation fixes

Who changed what in which revision?

UserRevisionLine numberNew 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 2:4997f825ee95 53 * @return -1 if the key size is not valid; 0 otherwise.
razueroh 0:27d3a972ad80 54 */
razueroh 0:27d3a972ad80 55 int aesSetKey(AES* aes, const unsigned char *key, unsigned int len, int dir);
razueroh 0:27d3a972ad80 56
razueroh 0:27d3a972ad80 57 /**
razueroh 0:27d3a972ad80 58 * @fn void aesEncrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock)
razueroh 0:27d3a972ad80 59 * @brief Encrypts a 16-byte block
razueroh 0:27d3a972ad80 60 * @param aes The AES struct
razueroh 0:27d3a972ad80 61 * @param inBlock The 16-byte input block
razueroh 0:27d3a972ad80 62 * @param outBlock The 16-byte output block
razueroh 0:27d3a972ad80 63 */
razueroh 0:27d3a972ad80 64 void aesEncrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock);
razueroh 0:27d3a972ad80 65
razueroh 0:27d3a972ad80 66 /**
razueroh 0:27d3a972ad80 67 * @fn void aesDecrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock)
razueroh 0:27d3a972ad80 68 * @brief Decrypts a 16-byte block
razueroh 0:27d3a972ad80 69 * @param aes The AES struct
razueroh 0:27d3a972ad80 70 * @param inBlock The 16-byte input block
razueroh 0:27d3a972ad80 71 * @param outBlock The 16-byte output block
razueroh 0:27d3a972ad80 72 */
razueroh 0:27d3a972ad80 73 void aesDecrypt(AES* aes, const unsigned char *inBlock, unsigned char *outBlock);
razueroh 0:27d3a972ad80 74
razueroh 0:27d3a972ad80 75 /**
razueroh 0:27d3a972ad80 76 * @fn void aesEcbEncrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize)
razueroh 0:27d3a972ad80 77 * @brief Encrypts a n-byte block with AES-ECB
razueroh 0:27d3a972ad80 78 * @param aes The AES struct
razueroh 0:27d3a972ad80 79 * @param output The input data
razueroh 0:27d3a972ad80 80 * @param input The output data
razueroh 0:27d3a972ad80 81 * @param inputSize The input data size in bytes
razueroh 0:27d3a972ad80 82 */
razueroh 0:27d3a972ad80 83 void aesEcbEncrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize);
razueroh 0:27d3a972ad80 84
razueroh 0:27d3a972ad80 85 /**
razueroh 0:27d3a972ad80 86 * @fn void aesEcbDecrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize)
razueroh 0:27d3a972ad80 87 * @brief Decrypts a n-bytes block with AES-ECB
razueroh 0:27d3a972ad80 88 * @param aes The AES struct
razueroh 0:27d3a972ad80 89 * @param output The input data
razueroh 0:27d3a972ad80 90 * @param input The output data
razueroh 0:27d3a972ad80 91 * @param inputSize The input data size in bytes
razueroh 0:27d3a972ad80 92 */
razueroh 0:27d3a972ad80 93 void aesEcbDecrypt(AES* aes, unsigned char *output, const unsigned char *input, unsigned int inputSize);
razueroh 0:27d3a972ad80 94
razueroh 0:27d3a972ad80 95 /**
razueroh 0:27d3a972ad80 96 * @fn unsigned int byteReverseWord32(unsigned int value)
razueroh 0:27d3a972ad80 97 * @brief Converts a big-endian 32-bit word to little-endian format
razueroh 0:27d3a972ad80 98 * @param value The big-endian 32-bit word
razueroh 2:4997f825ee95 99 * @return The little-endian 32-bit word
razueroh 0:27d3a972ad80 100 */
razueroh 0:27d3a972ad80 101 unsigned int byteReverseWord32(unsigned int value);
razueroh 0:27d3a972ad80 102
razueroh 0:27d3a972ad80 103 /**
razueroh 0:27d3a972ad80 104 * @fn void byteReverseWords(unsigned int *out, const unsigned int *in, unsigned int byteCount)
razueroh 0:27d3a972ad80 105 * @brief Converts a big-endian block to little-endian format
razueroh 0:27d3a972ad80 106 * @param out The output block
razueroh 0:27d3a972ad80 107 * @param in The input block
razueroh 0:27d3a972ad80 108 * @param byteCount The Number of bytes
razueroh 0:27d3a972ad80 109 */
razueroh 0:27d3a972ad80 110 void byteReverseWords(unsigned int *out, const unsigned int *in, unsigned int byteCount);
razueroh 0:27d3a972ad80 111
razueroh 0:27d3a972ad80 112 /**
razueroh 0:27d3a972ad80 113 * @fn unsigned int rotlFixed(unsigned int x, unsigned int y)
razueroh 0:27d3a972ad80 114 * @brief Rotates a 32-bit word n-bytes to the left
razueroh 0:27d3a972ad80 115 * @param x The32-bit word
razueroh 0:27d3a972ad80 116 * @param y The number of bytes to rotate
razueroh 2:4997f825ee95 117 * @return The rotated 32-bit word
razueroh 0:27d3a972ad80 118 */
razueroh 0:27d3a972ad80 119 unsigned int rotlFixed(unsigned int x, unsigned int y);
razueroh 0:27d3a972ad80 120
razueroh 0:27d3a972ad80 121 /**
razueroh 0:27d3a972ad80 122 * @fn unsigned int rotrFixed(unsigned int x, unsigned int y)
razueroh 0:27d3a972ad80 123 * @brief Rotates a 32-bit word n-bytes to the right
razueroh 0:27d3a972ad80 124 * @param x The 32-bit word
razueroh 0:27d3a972ad80 125 * @param y The number of bytes to rotate
razueroh 2:4997f825ee95 126 * @return The rotated 32-bit word
razueroh 0:27d3a972ad80 127 */
razueroh 0:27d3a972ad80 128 unsigned int rotrFixed(unsigned int x, unsigned int y);
razueroh 0:27d3a972ad80 129
razueroh 0:27d3a972ad80 130
razueroh 0:27d3a972ad80 131 #ifdef __cplusplus
razueroh 0:27d3a972ad80 132 } /* extern "C" */
razueroh 0:27d3a972ad80 133 #endif
razueroh 0:27d3a972ad80 134
razueroh 0:27d3a972ad80 135 #endif