A simple library to support serving https.
Dependents: oldheating gps motorhome heating
hmac/hmac-sha256.c@9:f354b4859b0b, 2019-09-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Sep 11 07:24:21 2019 +0000
- Revision:
- 9:f354b4859b0b
Got application data to be returned but not encrypted yet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 9:f354b4859b0b | 1 | #include <stdint.h> |
andrewboyson | 9:f354b4859b0b | 2 | |
andrewboyson | 9:f354b4859b0b | 3 | #include "hmac-sha256.h" |
andrewboyson | 9:f354b4859b0b | 4 | #include "sha256.h" |
andrewboyson | 9:f354b4859b0b | 5 | |
andrewboyson | 9:f354b4859b0b | 6 | #define BLOCK_SIZE 64 |
andrewboyson | 9:f354b4859b0b | 7 | #define HASH_SIZE 32 |
andrewboyson | 9:f354b4859b0b | 8 | |
andrewboyson | 9:f354b4859b0b | 9 | void HmacSha256Start(struct HmacSha256Struct* md, const uint8_t* key, int keyLength) |
andrewboyson | 9:f354b4859b0b | 10 | { |
andrewboyson | 9:f354b4859b0b | 11 | //Make the key BLOCK_SIZE bytes long by hashing longer ones or padding shorter one with 0s |
andrewboyson | 9:f354b4859b0b | 12 | if (keyLength > BLOCK_SIZE) |
andrewboyson | 9:f354b4859b0b | 13 | { |
andrewboyson | 9:f354b4859b0b | 14 | Sha256(key, keyLength, md->hmacKey); |
andrewboyson | 9:f354b4859b0b | 15 | for (int i = HASH_SIZE; i < BLOCK_SIZE; i++) md->hmacKey[i] = 0; |
andrewboyson | 9:f354b4859b0b | 16 | } |
andrewboyson | 9:f354b4859b0b | 17 | else |
andrewboyson | 9:f354b4859b0b | 18 | { |
andrewboyson | 9:f354b4859b0b | 19 | for (int i = 0; i < keyLength; i++) md->hmacKey[i] = key[i]; |
andrewboyson | 9:f354b4859b0b | 20 | for (int i = keyLength; i < BLOCK_SIZE; i++) md->hmacKey[i] = 0; |
andrewboyson | 9:f354b4859b0b | 21 | } |
andrewboyson | 9:f354b4859b0b | 22 | Sha256Start(&md->shaStruct); |
andrewboyson | 9:f354b4859b0b | 23 | |
andrewboyson | 9:f354b4859b0b | 24 | //Make the inner and outer padded keys |
andrewboyson | 9:f354b4859b0b | 25 | uint8_t iKeyPad[BLOCK_SIZE]; |
andrewboyson | 9:f354b4859b0b | 26 | for (int i = 0; i < BLOCK_SIZE; i++) |
andrewboyson | 9:f354b4859b0b | 27 | { |
andrewboyson | 9:f354b4859b0b | 28 | iKeyPad[i] = md->hmacKey[i] ^ 0x36; //Inner padded key |
andrewboyson | 9:f354b4859b0b | 29 | } |
andrewboyson | 9:f354b4859b0b | 30 | Sha256Add(&md->shaStruct, iKeyPad, BLOCK_SIZE); |
andrewboyson | 9:f354b4859b0b | 31 | } |
andrewboyson | 9:f354b4859b0b | 32 | void HmacSha256Add(struct HmacSha256Struct* md, const uint8_t* message, int messageLength) |
andrewboyson | 9:f354b4859b0b | 33 | { |
andrewboyson | 9:f354b4859b0b | 34 | Sha256Add(&md->shaStruct, message, messageLength); |
andrewboyson | 9:f354b4859b0b | 35 | } |
andrewboyson | 9:f354b4859b0b | 36 | void HmacSha256Finish(struct HmacSha256Struct* md, uint8_t* mac) |
andrewboyson | 9:f354b4859b0b | 37 | { |
andrewboyson | 9:f354b4859b0b | 38 | //Make the inner and outer padded keys |
andrewboyson | 9:f354b4859b0b | 39 | uint8_t oKeyPad[BLOCK_SIZE]; |
andrewboyson | 9:f354b4859b0b | 40 | for (int i = 0; i < BLOCK_SIZE; i++) |
andrewboyson | 9:f354b4859b0b | 41 | { |
andrewboyson | 9:f354b4859b0b | 42 | oKeyPad[i] = md->hmacKey[i] ^ 0x5c; //Outer padded key |
andrewboyson | 9:f354b4859b0b | 43 | } |
andrewboyson | 9:f354b4859b0b | 44 | |
andrewboyson | 9:f354b4859b0b | 45 | uint8_t innerHash[HASH_SIZE]; |
andrewboyson | 9:f354b4859b0b | 46 | Sha256Finish(&md->shaStruct, innerHash); |
andrewboyson | 9:f354b4859b0b | 47 | |
andrewboyson | 9:f354b4859b0b | 48 | Sha256Start(&md->shaStruct); |
andrewboyson | 9:f354b4859b0b | 49 | Sha256Add(&md->shaStruct, oKeyPad, BLOCK_SIZE); |
andrewboyson | 9:f354b4859b0b | 50 | Sha256Add(&md->shaStruct, innerHash, HASH_SIZE); |
andrewboyson | 9:f354b4859b0b | 51 | |
andrewboyson | 9:f354b4859b0b | 52 | Sha256Finish(&md->shaStruct, mac); |
andrewboyson | 9:f354b4859b0b | 53 | } |
andrewboyson | 9:f354b4859b0b | 54 | |
andrewboyson | 9:f354b4859b0b | 55 | void HmacSha256(const uint8_t* key, int keyLength, const uint8_t* message, int messageLength, uint8_t* mac) |
andrewboyson | 9:f354b4859b0b | 56 | { |
andrewboyson | 9:f354b4859b0b | 57 | struct HmacSha256Struct md; |
andrewboyson | 9:f354b4859b0b | 58 | HmacSha256Start (&md, key, keyLength); |
andrewboyson | 9:f354b4859b0b | 59 | HmacSha256Add (&md, message, messageLength); |
andrewboyson | 9:f354b4859b0b | 60 | HmacSha256Finish(&md, mac); |
andrewboyson | 9:f354b4859b0b | 61 | } |