Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed LoRaWAN-lib SX1276Lib
system/crypto/aes.h@12:8d057a5bf72e, 2020-01-30 (annotated)
- Committer:
- amirchaudhary
- Date:
- Thu Jan 30 13:51:38 2020 +0000
- Revision:
- 12:8d057a5bf72e
- Parent:
- 1:352f608c3337
Analog Values Reading (Emerg Lights)
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| mluis | 1:352f608c3337 | 1 | /* | 
| mluis | 1:352f608c3337 | 2 | --------------------------------------------------------------------------- | 
| mluis | 1:352f608c3337 | 3 | Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved. | 
| mluis | 1:352f608c3337 | 4 | |
| mluis | 1:352f608c3337 | 5 | LICENSE TERMS | 
| mluis | 1:352f608c3337 | 6 | |
| mluis | 1:352f608c3337 | 7 | The redistribution and use of this software (with or without changes) | 
| mluis | 1:352f608c3337 | 8 | is allowed without the payment of fees or royalties provided that: | 
| mluis | 1:352f608c3337 | 9 | |
| mluis | 1:352f608c3337 | 10 | 1. source code distributions include the above copyright notice, this | 
| mluis | 1:352f608c3337 | 11 | list of conditions and the following disclaimer; | 
| mluis | 1:352f608c3337 | 12 | |
| mluis | 1:352f608c3337 | 13 | 2. binary distributions include the above copyright notice, this list | 
| mluis | 1:352f608c3337 | 14 | of conditions and the following disclaimer in their documentation; | 
| mluis | 1:352f608c3337 | 15 | |
| mluis | 1:352f608c3337 | 16 | 3. the name of the copyright holder is not used to endorse products | 
| mluis | 1:352f608c3337 | 17 | built using this software without specific written permission. | 
| mluis | 1:352f608c3337 | 18 | |
| mluis | 1:352f608c3337 | 19 | DISCLAIMER | 
| mluis | 1:352f608c3337 | 20 | |
| mluis | 1:352f608c3337 | 21 | This software is provided 'as is' with no explicit or implied warranties | 
| mluis | 1:352f608c3337 | 22 | in respect of its properties, including, but not limited to, correctness | 
| mluis | 1:352f608c3337 | 23 | and/or fitness for purpose. | 
| mluis | 1:352f608c3337 | 24 | --------------------------------------------------------------------------- | 
| mluis | 1:352f608c3337 | 25 | Issue 09/09/2006 | 
| mluis | 1:352f608c3337 | 26 | |
| mluis | 1:352f608c3337 | 27 | This is an AES implementation that uses only 8-bit byte operations on the | 
| mluis | 1:352f608c3337 | 28 | cipher state. | 
| mluis | 1:352f608c3337 | 29 | */ | 
| mluis | 1:352f608c3337 | 30 | |
| mluis | 1:352f608c3337 | 31 | #ifndef AES_H | 
| mluis | 1:352f608c3337 | 32 | #define AES_H | 
| mluis | 1:352f608c3337 | 33 | |
| mluis | 1:352f608c3337 | 34 | #if 1 | 
| mluis | 1:352f608c3337 | 35 | # define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */ | 
| mluis | 1:352f608c3337 | 36 | #endif | 
| mluis | 1:352f608c3337 | 37 | #if 0 | 
| mluis | 1:352f608c3337 | 38 | # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */ | 
| mluis | 1:352f608c3337 | 39 | #endif | 
| mluis | 1:352f608c3337 | 40 | #if 0 | 
| mluis | 1:352f608c3337 | 41 | # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */ | 
| mluis | 1:352f608c3337 | 42 | #endif | 
| mluis | 1:352f608c3337 | 43 | #if 0 | 
| mluis | 1:352f608c3337 | 44 | # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */ | 
| mluis | 1:352f608c3337 | 45 | #endif | 
| mluis | 1:352f608c3337 | 46 | #if 0 | 
| mluis | 1:352f608c3337 | 47 | # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */ | 
| mluis | 1:352f608c3337 | 48 | #endif | 
| mluis | 1:352f608c3337 | 49 | #if 0 | 
| mluis | 1:352f608c3337 | 50 | # define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */ | 
| mluis | 1:352f608c3337 | 51 | #endif | 
| mluis | 1:352f608c3337 | 52 | |
| mluis | 1:352f608c3337 | 53 | #define N_ROW 4 | 
| mluis | 1:352f608c3337 | 54 | #define N_COL 4 | 
| mluis | 1:352f608c3337 | 55 | #define N_BLOCK (N_ROW * N_COL) | 
| mluis | 1:352f608c3337 | 56 | #define N_MAX_ROUNDS 14 | 
| mluis | 1:352f608c3337 | 57 | |
| mluis | 1:352f608c3337 | 58 | typedef uint8_t return_type; | 
| mluis | 1:352f608c3337 | 59 | |
| mluis | 1:352f608c3337 | 60 | /* Warning: The key length for 256 bit keys overflows a byte | 
| mluis | 1:352f608c3337 | 61 | (see comment below) | 
| mluis | 1:352f608c3337 | 62 | */ | 
| mluis | 1:352f608c3337 | 63 | |
| mluis | 1:352f608c3337 | 64 | typedef uint8_t length_type; | 
| mluis | 1:352f608c3337 | 65 | |
| mluis | 1:352f608c3337 | 66 | typedef struct | 
| mluis | 1:352f608c3337 | 67 | { uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK]; | 
| mluis | 1:352f608c3337 | 68 | uint8_t rnd; | 
| mluis | 1:352f608c3337 | 69 | } aes_context; | 
| mluis | 1:352f608c3337 | 70 | |
| mluis | 1:352f608c3337 | 71 | /* The following calls are for a precomputed key schedule | 
| mluis | 1:352f608c3337 | 72 | |
| mluis | 1:352f608c3337 | 73 | NOTE: If the length_type used for the key length is an | 
| mluis | 1:352f608c3337 | 74 | unsigned 8-bit character, a key length of 256 bits must | 
| mluis | 1:352f608c3337 | 75 | be entered as a length in bytes (valid inputs are hence | 
| mluis | 1:352f608c3337 | 76 | 128, 192, 16, 24 and 32). | 
| mluis | 1:352f608c3337 | 77 | */ | 
| mluis | 1:352f608c3337 | 78 | |
| mluis | 1:352f608c3337 | 79 | #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED ) | 
| mluis | 1:352f608c3337 | 80 | |
| mluis | 1:352f608c3337 | 81 | return_type aes_set_key( const uint8_t key[], | 
| mluis | 1:352f608c3337 | 82 | length_type keylen, | 
| mluis | 1:352f608c3337 | 83 | aes_context ctx[1] ); | 
| mluis | 1:352f608c3337 | 84 | #endif | 
| mluis | 1:352f608c3337 | 85 | |
| mluis | 1:352f608c3337 | 86 | #if defined( AES_ENC_PREKEYED ) | 
| mluis | 1:352f608c3337 | 87 | |
| mluis | 1:352f608c3337 | 88 | return_type aes_encrypt( const uint8_t in[N_BLOCK], | 
| mluis | 1:352f608c3337 | 89 | uint8_t out[N_BLOCK], | 
| mluis | 1:352f608c3337 | 90 | const aes_context ctx[1] ); | 
| mluis | 1:352f608c3337 | 91 | |
| mluis | 1:352f608c3337 | 92 | return_type aes_cbc_encrypt( const uint8_t *in, | 
| mluis | 1:352f608c3337 | 93 | uint8_t *out, | 
| mluis | 1:352f608c3337 | 94 | int32_t n_block, | 
| mluis | 1:352f608c3337 | 95 | uint8_t iv[N_BLOCK], | 
| mluis | 1:352f608c3337 | 96 | const aes_context ctx[1] ); | 
| mluis | 1:352f608c3337 | 97 | #endif | 
| mluis | 1:352f608c3337 | 98 | |
| mluis | 1:352f608c3337 | 99 | #if defined( AES_DEC_PREKEYED ) | 
| mluis | 1:352f608c3337 | 100 | |
| mluis | 1:352f608c3337 | 101 | return_type aes_decrypt( const uint8_t in[N_BLOCK], | 
| mluis | 1:352f608c3337 | 102 | uint8_t out[N_BLOCK], | 
| mluis | 1:352f608c3337 | 103 | const aes_context ctx[1] ); | 
| mluis | 1:352f608c3337 | 104 | |
| mluis | 1:352f608c3337 | 105 | return_type aes_cbc_decrypt( const uint8_t *in, | 
| mluis | 1:352f608c3337 | 106 | uint8_t *out, | 
| mluis | 1:352f608c3337 | 107 | int32_t n_block, | 
| mluis | 1:352f608c3337 | 108 | uint8_t iv[N_BLOCK], | 
| mluis | 1:352f608c3337 | 109 | const aes_context ctx[1] ); | 
| mluis | 1:352f608c3337 | 110 | #endif | 
| mluis | 1:352f608c3337 | 111 | |
| mluis | 1:352f608c3337 | 112 | /* The following calls are for 'on the fly' keying. In this case the | 
| mluis | 1:352f608c3337 | 113 | encryption and decryption keys are different. | 
| mluis | 1:352f608c3337 | 114 | |
| mluis | 1:352f608c3337 | 115 | The encryption subroutines take a key in an array of bytes in | 
| mluis | 1:352f608c3337 | 116 | key[L] where L is 16, 24 or 32 bytes for key lengths of 128, | 
| mluis | 1:352f608c3337 | 117 | 192, and 256 bits respectively. They then encrypts the input | 
| mluis | 1:352f608c3337 | 118 | data, in[] with this key and put the reult in the output array | 
| mluis | 1:352f608c3337 | 119 | out[]. In addition, the second key array, o_key[L], is used | 
| mluis | 1:352f608c3337 | 120 | to output the key that is needed by the decryption subroutine | 
| mluis | 1:352f608c3337 | 121 | to reverse the encryption operation. The two key arrays can | 
| mluis | 1:352f608c3337 | 122 | be the same array but in this case the original key will be | 
| mluis | 1:352f608c3337 | 123 | overwritten. | 
| mluis | 1:352f608c3337 | 124 | |
| mluis | 1:352f608c3337 | 125 | In the same way, the decryption subroutines output keys that | 
| mluis | 1:352f608c3337 | 126 | can be used to reverse their effect when used for encryption. | 
| mluis | 1:352f608c3337 | 127 | |
| mluis | 1:352f608c3337 | 128 | Only 128 and 256 bit keys are supported in these 'on the fly' | 
| mluis | 1:352f608c3337 | 129 | modes. | 
| mluis | 1:352f608c3337 | 130 | */ | 
| mluis | 1:352f608c3337 | 131 | |
| mluis | 1:352f608c3337 | 132 | #if defined( AES_ENC_128_OTFK ) | 
| mluis | 1:352f608c3337 | 133 | void aes_encrypt_128( const uint8_t in[N_BLOCK], | 
| mluis | 1:352f608c3337 | 134 | uint8_t out[N_BLOCK], | 
| mluis | 1:352f608c3337 | 135 | const uint8_t key[N_BLOCK], | 
| mluis | 1:352f608c3337 | 136 | uint8_t o_key[N_BLOCK] ); | 
| mluis | 1:352f608c3337 | 137 | #endif | 
| mluis | 1:352f608c3337 | 138 | |
| mluis | 1:352f608c3337 | 139 | #if defined( AES_DEC_128_OTFK ) | 
| mluis | 1:352f608c3337 | 140 | void aes_decrypt_128( const uint8_t in[N_BLOCK], | 
| mluis | 1:352f608c3337 | 141 | uint8_t out[N_BLOCK], | 
| mluis | 1:352f608c3337 | 142 | const uint8_t key[N_BLOCK], | 
| mluis | 1:352f608c3337 | 143 | uint8_t o_key[N_BLOCK] ); | 
| mluis | 1:352f608c3337 | 144 | #endif | 
| mluis | 1:352f608c3337 | 145 | |
| mluis | 1:352f608c3337 | 146 | #if defined( AES_ENC_256_OTFK ) | 
| mluis | 1:352f608c3337 | 147 | void aes_encrypt_256( const uint8_t in[N_BLOCK], | 
| mluis | 1:352f608c3337 | 148 | uint8_t out[N_BLOCK], | 
| mluis | 1:352f608c3337 | 149 | const uint8_t key[2 * N_BLOCK], | 
| mluis | 1:352f608c3337 | 150 | uint8_t o_key[2 * N_BLOCK] ); | 
| mluis | 1:352f608c3337 | 151 | #endif | 
| mluis | 1:352f608c3337 | 152 | |
| mluis | 1:352f608c3337 | 153 | #if defined( AES_DEC_256_OTFK ) | 
| mluis | 1:352f608c3337 | 154 | void aes_decrypt_256( const uint8_t in[N_BLOCK], | 
| mluis | 1:352f608c3337 | 155 | uint8_t out[N_BLOCK], | 
| mluis | 1:352f608c3337 | 156 | const uint8_t key[2 * N_BLOCK], | 
| mluis | 1:352f608c3337 | 157 | uint8_t o_key[2 * N_BLOCK] ); | 
| mluis | 1:352f608c3337 | 158 | #endif | 
| mluis | 1:352f608c3337 | 159 | |
| mluis | 1:352f608c3337 | 160 | #endif |