A simple library to support serving https.
Dependents: oldheating gps motorhome heating
hmac/hmac-sha1.c@7:94ef5824c3c0, 2019-09-05 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Sep 05 12:58:41 2019 +0000
- Revision:
- 7:94ef5824c3c0
- Child:
- 9:f354b4859b0b
Client handshake is now verified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 7:94ef5824c3c0 | 1 | #include <stdint.h> |
andrewboyson | 7:94ef5824c3c0 | 2 | |
andrewboyson | 7:94ef5824c3c0 | 3 | #include "hmac-sha1.h" |
andrewboyson | 7:94ef5824c3c0 | 4 | #include "sha1.h" |
andrewboyson | 7:94ef5824c3c0 | 5 | |
andrewboyson | 7:94ef5824c3c0 | 6 | #define BLOCK_SIZE 64 |
andrewboyson | 7:94ef5824c3c0 | 7 | #define HASH_SIZE 20 |
andrewboyson | 7:94ef5824c3c0 | 8 | |
andrewboyson | 7:94ef5824c3c0 | 9 | void HmacSha1(const uint8_t* key, int keyLength, const uint8_t* message, int messageLength, uint8_t* mac) |
andrewboyson | 7:94ef5824c3c0 | 10 | { |
andrewboyson | 7:94ef5824c3c0 | 11 | uint8_t hmacKey[BLOCK_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 12 | |
andrewboyson | 7:94ef5824c3c0 | 13 | //Make the key BLOCK_SIZE bytes long by hashing longer ones or padding shorter one with 0s |
andrewboyson | 7:94ef5824c3c0 | 14 | if (keyLength > BLOCK_SIZE) |
andrewboyson | 7:94ef5824c3c0 | 15 | { |
andrewboyson | 7:94ef5824c3c0 | 16 | Sha1(key, keyLength, hmacKey); |
andrewboyson | 7:94ef5824c3c0 | 17 | for (int i = HASH_SIZE; i < BLOCK_SIZE; i++) hmacKey[i] = 0; |
andrewboyson | 7:94ef5824c3c0 | 18 | } |
andrewboyson | 7:94ef5824c3c0 | 19 | else |
andrewboyson | 7:94ef5824c3c0 | 20 | { |
andrewboyson | 7:94ef5824c3c0 | 21 | for (int i = 0; i < keyLength; i++) hmacKey[i] = key[i]; |
andrewboyson | 7:94ef5824c3c0 | 22 | for (int i = keyLength; i < BLOCK_SIZE; i++) hmacKey[i] = 0; |
andrewboyson | 7:94ef5824c3c0 | 23 | } |
andrewboyson | 7:94ef5824c3c0 | 24 | |
andrewboyson | 7:94ef5824c3c0 | 25 | //Make the inner and outer padded keys |
andrewboyson | 7:94ef5824c3c0 | 26 | uint8_t oKeyPad[BLOCK_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 27 | uint8_t iKeyPad[BLOCK_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 28 | for (int i = 0; i < BLOCK_SIZE; i++) |
andrewboyson | 7:94ef5824c3c0 | 29 | { |
andrewboyson | 7:94ef5824c3c0 | 30 | oKeyPad[i] = hmacKey[i] ^ 0x5c; //Outer padded key |
andrewboyson | 7:94ef5824c3c0 | 31 | iKeyPad[i] = hmacKey[i] ^ 0x36; //Inner padded key |
andrewboyson | 7:94ef5824c3c0 | 32 | } |
andrewboyson | 7:94ef5824c3c0 | 33 | |
andrewboyson | 7:94ef5824c3c0 | 34 | //hash(o_key_pad + hash(i_key_pad + message) where + is concatenation |
andrewboyson | 7:94ef5824c3c0 | 35 | uint8_t* tempi = alloca(BLOCK_SIZE + messageLength); |
andrewboyson | 7:94ef5824c3c0 | 36 | for (int i = 0; i < BLOCK_SIZE; i++) tempi[i ] = iKeyPad[i]; |
andrewboyson | 7:94ef5824c3c0 | 37 | for (int i = 0; i < messageLength; i++) tempi[i + BLOCK_SIZE] = message[i]; |
andrewboyson | 7:94ef5824c3c0 | 38 | uint8_t tmpHash[HASH_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 39 | Sha1(tempi, BLOCK_SIZE + messageLength, tmpHash); |
andrewboyson | 7:94ef5824c3c0 | 40 | |
andrewboyson | 7:94ef5824c3c0 | 41 | uint8_t tmpMsg[BLOCK_SIZE + HASH_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 42 | for (int i = 0; i < BLOCK_SIZE; i++) tmpMsg[i ] = oKeyPad[i]; |
andrewboyson | 7:94ef5824c3c0 | 43 | for (int i = 0; i < HASH_SIZE; i++) tmpMsg[i + BLOCK_SIZE] = tmpHash[i]; |
andrewboyson | 7:94ef5824c3c0 | 44 | Sha1(tmpMsg, BLOCK_SIZE + HASH_SIZE, mac); |
andrewboyson | 7:94ef5824c3c0 | 45 | } |
andrewboyson | 7:94ef5824c3c0 | 46 | void HmacSha1Start(struct HmacSha1Struct* md, const uint8_t* key, int keyLength) |
andrewboyson | 7:94ef5824c3c0 | 47 | { |
andrewboyson | 7:94ef5824c3c0 | 48 | //Make the key BLOCK_SIZE bytes long by hashing longer ones or padding shorter one with 0s |
andrewboyson | 7:94ef5824c3c0 | 49 | if (keyLength > BLOCK_SIZE) |
andrewboyson | 7:94ef5824c3c0 | 50 | { |
andrewboyson | 7:94ef5824c3c0 | 51 | Sha1(key, keyLength, md->hmacKey); |
andrewboyson | 7:94ef5824c3c0 | 52 | for (int i = HASH_SIZE; i < BLOCK_SIZE; i++) md->hmacKey[i] = 0; |
andrewboyson | 7:94ef5824c3c0 | 53 | } |
andrewboyson | 7:94ef5824c3c0 | 54 | else |
andrewboyson | 7:94ef5824c3c0 | 55 | { |
andrewboyson | 7:94ef5824c3c0 | 56 | for (int i = 0; i < keyLength; i++) md->hmacKey[i] = key[i]; |
andrewboyson | 7:94ef5824c3c0 | 57 | for (int i = keyLength; i < BLOCK_SIZE; i++) md->hmacKey[i] = 0; |
andrewboyson | 7:94ef5824c3c0 | 58 | } |
andrewboyson | 7:94ef5824c3c0 | 59 | Sha1Start(&md->shaStruct); |
andrewboyson | 7:94ef5824c3c0 | 60 | |
andrewboyson | 7:94ef5824c3c0 | 61 | //Make the inner and outer padded keys |
andrewboyson | 7:94ef5824c3c0 | 62 | uint8_t iKeyPad[BLOCK_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 63 | for (int i = 0; i < BLOCK_SIZE; i++) |
andrewboyson | 7:94ef5824c3c0 | 64 | { |
andrewboyson | 7:94ef5824c3c0 | 65 | iKeyPad[i] = md->hmacKey[i] ^ 0x36; //Inner padded key |
andrewboyson | 7:94ef5824c3c0 | 66 | } |
andrewboyson | 7:94ef5824c3c0 | 67 | Sha1Add(&md->shaStruct, iKeyPad, BLOCK_SIZE); |
andrewboyson | 7:94ef5824c3c0 | 68 | } |
andrewboyson | 7:94ef5824c3c0 | 69 | void HmacSha1Add(struct HmacSha1Struct* md, const uint8_t* message, int messageLength) |
andrewboyson | 7:94ef5824c3c0 | 70 | { |
andrewboyson | 7:94ef5824c3c0 | 71 | Sha1Add(&md->shaStruct, message, messageLength); |
andrewboyson | 7:94ef5824c3c0 | 72 | } |
andrewboyson | 7:94ef5824c3c0 | 73 | void HmacSha1Finish(struct HmacSha1Struct* md, uint8_t* mac) |
andrewboyson | 7:94ef5824c3c0 | 74 | { |
andrewboyson | 7:94ef5824c3c0 | 75 | //Make the inner and outer padded keys |
andrewboyson | 7:94ef5824c3c0 | 76 | uint8_t oKeyPad[BLOCK_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 77 | for (int i = 0; i < BLOCK_SIZE; i++) |
andrewboyson | 7:94ef5824c3c0 | 78 | { |
andrewboyson | 7:94ef5824c3c0 | 79 | oKeyPad[i] = md->hmacKey[i] ^ 0x5c; //Outer padded key |
andrewboyson | 7:94ef5824c3c0 | 80 | } |
andrewboyson | 7:94ef5824c3c0 | 81 | |
andrewboyson | 7:94ef5824c3c0 | 82 | uint8_t innerHash[HASH_SIZE]; |
andrewboyson | 7:94ef5824c3c0 | 83 | Sha1Finish(&md->shaStruct, innerHash); |
andrewboyson | 7:94ef5824c3c0 | 84 | |
andrewboyson | 7:94ef5824c3c0 | 85 | Sha1Start(&md->shaStruct); |
andrewboyson | 7:94ef5824c3c0 | 86 | Sha1Add(&md->shaStruct, oKeyPad, BLOCK_SIZE); |
andrewboyson | 7:94ef5824c3c0 | 87 | Sha1Add(&md->shaStruct, innerHash, HASH_SIZE); |
andrewboyson | 7:94ef5824c3c0 | 88 | |
andrewboyson | 7:94ef5824c3c0 | 89 | Sha1Finish(&md->shaStruct, mac); |
andrewboyson | 7:94ef5824c3c0 | 90 | } |