A simple library to support serving https.
Dependents: oldheating gps motorhome heating
aes-gcm/aes.h@6:819c17738dc2, 2019-09-01 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Sep 01 18:15:12 2019 +0000
- Revision:
- 6:819c17738dc2
Making progress - now have decryption working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 6:819c17738dc2 | 1 | /****************************************************************************** |
andrewboyson | 6:819c17738dc2 | 2 | * |
andrewboyson | 6:819c17738dc2 | 3 | * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL |
andrewboyson | 6:819c17738dc2 | 4 | * |
andrewboyson | 6:819c17738dc2 | 5 | * This is a simple and straightforward implementation of the AES Rijndael |
andrewboyson | 6:819c17738dc2 | 6 | * 128-bit block cipher designed by Vincent Rijmen and Joan Daemen. The focus |
andrewboyson | 6:819c17738dc2 | 7 | * of this work was correctness & accuracy. It is written in 'C' without any |
andrewboyson | 6:819c17738dc2 | 8 | * particular focus upon optimization or speed. It should be endian (memory |
andrewboyson | 6:819c17738dc2 | 9 | * byte order) neutral since the few places that care are handled explicitly. |
andrewboyson | 6:819c17738dc2 | 10 | * |
andrewboyson | 6:819c17738dc2 | 11 | * This implementation of Rijndael was created by Steven M. Gibson of GRC.com. |
andrewboyson | 6:819c17738dc2 | 12 | * |
andrewboyson | 6:819c17738dc2 | 13 | * It is intended for general purpose use, but was written in support of GRC's |
andrewboyson | 6:819c17738dc2 | 14 | * reference implementation of the SQRL (Secure Quick Reliable Login) client. |
andrewboyson | 6:819c17738dc2 | 15 | * |
andrewboyson | 6:819c17738dc2 | 16 | * See: http://csrc.nist.gov/archive/aes/rijndael/wsdindex.html |
andrewboyson | 6:819c17738dc2 | 17 | * |
andrewboyson | 6:819c17738dc2 | 18 | * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE |
andrewboyson | 6:819c17738dc2 | 19 | * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK. |
andrewboyson | 6:819c17738dc2 | 20 | * |
andrewboyson | 6:819c17738dc2 | 21 | *******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 22 | |
andrewboyson | 6:819c17738dc2 | 23 | #ifndef AES_HEADER |
andrewboyson | 6:819c17738dc2 | 24 | #define AES_HEADER |
andrewboyson | 6:819c17738dc2 | 25 | |
andrewboyson | 6:819c17738dc2 | 26 | /******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 27 | #define AES_DECRYPTION 0 // whether AES decryption is supported |
andrewboyson | 6:819c17738dc2 | 28 | /******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 29 | |
andrewboyson | 6:819c17738dc2 | 30 | #include <string.h> |
andrewboyson | 6:819c17738dc2 | 31 | |
andrewboyson | 6:819c17738dc2 | 32 | #define ENCRYPT 1 // specify whether we're encrypting |
andrewboyson | 6:819c17738dc2 | 33 | #define DECRYPT 0 // or decrypting |
andrewboyson | 6:819c17738dc2 | 34 | |
andrewboyson | 6:819c17738dc2 | 35 | #if defined(_MSC_VER) |
andrewboyson | 6:819c17738dc2 | 36 | #include <basetsd.h> |
andrewboyson | 6:819c17738dc2 | 37 | typedef UINT32 uint32_t; |
andrewboyson | 6:819c17738dc2 | 38 | #else |
andrewboyson | 6:819c17738dc2 | 39 | #include <inttypes.h> |
andrewboyson | 6:819c17738dc2 | 40 | #endif |
andrewboyson | 6:819c17738dc2 | 41 | |
andrewboyson | 6:819c17738dc2 | 42 | typedef unsigned char uchar; // add some convienent shorter types |
andrewboyson | 6:819c17738dc2 | 43 | typedef unsigned int uint; |
andrewboyson | 6:819c17738dc2 | 44 | |
andrewboyson | 6:819c17738dc2 | 45 | |
andrewboyson | 6:819c17738dc2 | 46 | /****************************************************************************** |
andrewboyson | 6:819c17738dc2 | 47 | * AES_INIT_KEYGEN_TABLES : MUST be called once before any AES use |
andrewboyson | 6:819c17738dc2 | 48 | ******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 49 | void aes_init_keygen_tables( void ); |
andrewboyson | 6:819c17738dc2 | 50 | |
andrewboyson | 6:819c17738dc2 | 51 | |
andrewboyson | 6:819c17738dc2 | 52 | /****************************************************************************** |
andrewboyson | 6:819c17738dc2 | 53 | * AES_CONTEXT : cipher context / holds inter-call data |
andrewboyson | 6:819c17738dc2 | 54 | ******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 55 | typedef struct { |
andrewboyson | 6:819c17738dc2 | 56 | int mode; // 1 for Encryption, 0 for Decryption |
andrewboyson | 6:819c17738dc2 | 57 | int rounds; // keysize-based rounds count |
andrewboyson | 6:819c17738dc2 | 58 | uint32_t *rk; // pointer to current round key |
andrewboyson | 6:819c17738dc2 | 59 | uint32_t buf[68]; // key expansion buffer |
andrewboyson | 6:819c17738dc2 | 60 | } aes_context; |
andrewboyson | 6:819c17738dc2 | 61 | |
andrewboyson | 6:819c17738dc2 | 62 | |
andrewboyson | 6:819c17738dc2 | 63 | /****************************************************************************** |
andrewboyson | 6:819c17738dc2 | 64 | * AES_SETKEY : called to expand the key for encryption or decryption |
andrewboyson | 6:819c17738dc2 | 65 | ******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 66 | int aes_setkey( aes_context *ctx, // pointer to context |
andrewboyson | 6:819c17738dc2 | 67 | int mode, // 1 or 0 for Encrypt/Decrypt |
andrewboyson | 6:819c17738dc2 | 68 | const uchar *key, // AES input key |
andrewboyson | 6:819c17738dc2 | 69 | uint keysize ); // 128, 192 or 256 bits |
andrewboyson | 6:819c17738dc2 | 70 | // returns 0 for success |
andrewboyson | 6:819c17738dc2 | 71 | |
andrewboyson | 6:819c17738dc2 | 72 | /****************************************************************************** |
andrewboyson | 6:819c17738dc2 | 73 | * AES_CIPHER : called to encrypt or decrypt ONE 128-bit block of data |
andrewboyson | 6:819c17738dc2 | 74 | ******************************************************************************/ |
andrewboyson | 6:819c17738dc2 | 75 | int aes_cipher( aes_context *ctx, // pointer to context |
andrewboyson | 6:819c17738dc2 | 76 | const uchar input[16], // 128-bit block to en/decipher |
andrewboyson | 6:819c17738dc2 | 77 | uchar output[16] ); // 128-bit output result block |
andrewboyson | 6:819c17738dc2 | 78 | // returns 0 for success |
andrewboyson | 6:819c17738dc2 | 79 | |
andrewboyson | 6:819c17738dc2 | 80 | #endif /* AES_HEADER */ |