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