A simple library to support serving https.

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Apr 01 12:48:52 2020 +0000
Revision:
24:cb43290fc439
Parent:
9:f354b4859b0b
Added check so that if the client closes the TCP connection before the TLS connection is established then respond that we have finished and the TCP connection is to be closed.

Who changed what in which revision?

UserRevisionLine numberNew 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 HmacSha1Start(struct HmacSha1Struct* md, const uint8_t* key, int keyLength)
andrewboyson 7:94ef5824c3c0 10 {
andrewboyson 7:94ef5824c3c0 11 //Make the key BLOCK_SIZE bytes long by hashing longer ones or padding shorter one with 0s
andrewboyson 7:94ef5824c3c0 12 if (keyLength > BLOCK_SIZE)
andrewboyson 7:94ef5824c3c0 13 {
andrewboyson 7:94ef5824c3c0 14 Sha1(key, keyLength, md->hmacKey);
andrewboyson 7:94ef5824c3c0 15 for (int i = HASH_SIZE; i < BLOCK_SIZE; i++) md->hmacKey[i] = 0;
andrewboyson 7:94ef5824c3c0 16 }
andrewboyson 7:94ef5824c3c0 17 else
andrewboyson 7:94ef5824c3c0 18 {
andrewboyson 7:94ef5824c3c0 19 for (int i = 0; i < keyLength; i++) md->hmacKey[i] = key[i];
andrewboyson 7:94ef5824c3c0 20 for (int i = keyLength; i < BLOCK_SIZE; i++) md->hmacKey[i] = 0;
andrewboyson 7:94ef5824c3c0 21 }
andrewboyson 7:94ef5824c3c0 22 Sha1Start(&md->shaStruct);
andrewboyson 7:94ef5824c3c0 23
andrewboyson 7:94ef5824c3c0 24 //Make the inner and outer padded keys
andrewboyson 7:94ef5824c3c0 25 uint8_t iKeyPad[BLOCK_SIZE];
andrewboyson 7:94ef5824c3c0 26 for (int i = 0; i < BLOCK_SIZE; i++)
andrewboyson 7:94ef5824c3c0 27 {
andrewboyson 7:94ef5824c3c0 28 iKeyPad[i] = md->hmacKey[i] ^ 0x36; //Inner padded key
andrewboyson 7:94ef5824c3c0 29 }
andrewboyson 7:94ef5824c3c0 30 Sha1Add(&md->shaStruct, iKeyPad, BLOCK_SIZE);
andrewboyson 7:94ef5824c3c0 31 }
andrewboyson 7:94ef5824c3c0 32 void HmacSha1Add(struct HmacSha1Struct* md, const uint8_t* message, int messageLength)
andrewboyson 7:94ef5824c3c0 33 {
andrewboyson 7:94ef5824c3c0 34 Sha1Add(&md->shaStruct, message, messageLength);
andrewboyson 7:94ef5824c3c0 35 }
andrewboyson 7:94ef5824c3c0 36 void HmacSha1Finish(struct HmacSha1Struct* md, uint8_t* mac)
andrewboyson 7:94ef5824c3c0 37 {
andrewboyson 7:94ef5824c3c0 38 //Make the inner and outer padded keys
andrewboyson 7:94ef5824c3c0 39 uint8_t oKeyPad[BLOCK_SIZE];
andrewboyson 7:94ef5824c3c0 40 for (int i = 0; i < BLOCK_SIZE; i++)
andrewboyson 7:94ef5824c3c0 41 {
andrewboyson 7:94ef5824c3c0 42 oKeyPad[i] = md->hmacKey[i] ^ 0x5c; //Outer padded key
andrewboyson 7:94ef5824c3c0 43 }
andrewboyson 7:94ef5824c3c0 44
andrewboyson 7:94ef5824c3c0 45 uint8_t innerHash[HASH_SIZE];
andrewboyson 7:94ef5824c3c0 46 Sha1Finish(&md->shaStruct, innerHash);
andrewboyson 7:94ef5824c3c0 47
andrewboyson 7:94ef5824c3c0 48 Sha1Start(&md->shaStruct);
andrewboyson 7:94ef5824c3c0 49 Sha1Add(&md->shaStruct, oKeyPad, BLOCK_SIZE);
andrewboyson 7:94ef5824c3c0 50 Sha1Add(&md->shaStruct, innerHash, HASH_SIZE);
andrewboyson 7:94ef5824c3c0 51
andrewboyson 7:94ef5824c3c0 52 Sha1Finish(&md->shaStruct, mac);
andrewboyson 9:f354b4859b0b 53 }
andrewboyson 9:f354b4859b0b 54 void HmacSha1(const uint8_t* key, int keyLength, const uint8_t* message, int messageLength, uint8_t* mac)
andrewboyson 9:f354b4859b0b 55 {
andrewboyson 9:f354b4859b0b 56 struct HmacSha1Struct md;
andrewboyson 9:f354b4859b0b 57 HmacSha1Start (&md, key, keyLength);
andrewboyson 9:f354b4859b0b 58 HmacSha1Add (&md, message, messageLength);
andrewboyson 9:f354b4859b0b 59 HmacSha1Finish(&md, mac);
andrewboyson 7:94ef5824c3c0 60 }