Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Sep 11 07:24:21 2019 +0000
Revision:
9:f354b4859b0b
Child:
13:0a80b49a5e78
Got application data to be returned but not encrypted yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 9:f354b4859b0b 1 #include <stdint.h>
andrewboyson 9:f354b4859b0b 2 #include "prf.h"
andrewboyson 9:f354b4859b0b 3
andrewboyson 9:f354b4859b0b 4 void TlsPrfMasterSecret(uint8_t * preMasterSecret, uint8_t* clientRandom, uint8_t* serverRandom, uint8_t* output48)
andrewboyson 9:f354b4859b0b 5 {
andrewboyson 9:f354b4859b0b 6 uint8_t* seed = alloca(13 + 32 + 32);
andrewboyson 9:f354b4859b0b 7 for (int i = 0; i < 13; i++) seed[i ] = "master secret"[i];
andrewboyson 9:f354b4859b0b 8 for (int i = 0; i < 32; i++) seed[i + 13 ] = clientRandom[i];
andrewboyson 9:f354b4859b0b 9 for (int i = 0; i < 32; i++) seed[i + 13 + 32] = serverRandom[i];
andrewboyson 9:f354b4859b0b 10
andrewboyson 9:f354b4859b0b 11 uint8_t* hash = alloca(64);
andrewboyson 9:f354b4859b0b 12 PrfHmacSha256(preMasterSecret, 48, seed, 13 + 32 + 32, 2, hash); //2 iterations will generate 64 bytes
andrewboyson 9:f354b4859b0b 13 for (int i = 0; i < 48; i++) output48[i] = hash[i]; //just take the first 48 bytes
andrewboyson 9:f354b4859b0b 14 }
andrewboyson 9:f354b4859b0b 15
andrewboyson 9:f354b4859b0b 16 void TlsPrfKeys(uint8_t * masterSecret, uint8_t* clientRandom, uint8_t* serverRandom, uint8_t* client_MAC_key_20,
andrewboyson 9:f354b4859b0b 17 uint8_t* server_MAC_key_20,
andrewboyson 9:f354b4859b0b 18 uint8_t* client_key_16,
andrewboyson 9:f354b4859b0b 19 uint8_t* server_key_16)
andrewboyson 9:f354b4859b0b 20 {
andrewboyson 9:f354b4859b0b 21 uint8_t* seed = alloca(13 + 32 + 32);
andrewboyson 9:f354b4859b0b 22 for (int i = 0; i < 13; i++) seed[i ] = "key expansion"[i];
andrewboyson 9:f354b4859b0b 23 for (int i = 0; i < 32; i++) seed[i + 13 ] = serverRandom[i]; //Notice the order relative to the master secret algorithm
andrewboyson 9:f354b4859b0b 24 for (int i = 0; i < 32; i++) seed[i + 13 + 32] = clientRandom[i];
andrewboyson 9:f354b4859b0b 25
andrewboyson 9:f354b4859b0b 26 uint8_t* hash = alloca(128); //4 iterations of 32 bytes
andrewboyson 9:f354b4859b0b 27 PrfHmacSha256(masterSecret, 48, seed, 13 + 32 + 32, 4, hash); //4 iteration will generate the keys required
andrewboyson 9:f354b4859b0b 28 for (int i = 0; i < 20; i++) client_MAC_key_20[i] = hash[i ];
andrewboyson 9:f354b4859b0b 29 for (int i = 0; i < 20; i++) server_MAC_key_20[i] = hash[i + 20];
andrewboyson 9:f354b4859b0b 30 for (int i = 0; i < 16; i++) client_key_16[i] = hash[i + 40];
andrewboyson 9:f354b4859b0b 31 for (int i = 0; i < 16; i++) server_key_16[i] = hash[i + 56];
andrewboyson 9:f354b4859b0b 32 }
andrewboyson 9:f354b4859b0b 33
andrewboyson 9:f354b4859b0b 34 void TlsPrfServerFinished(uint8_t * masterSecret, uint8_t* handshakeHash, uint8_t* output12)
andrewboyson 9:f354b4859b0b 35 {
andrewboyson 9:f354b4859b0b 36 uint8_t* seed = alloca(15 + 32);
andrewboyson 9:f354b4859b0b 37 for (int i = 0; i < 15; i++) seed[i ] = "server finished"[i];
andrewboyson 9:f354b4859b0b 38 for (int i = 0; i < 32; i++) seed[i + 15] = handshakeHash[i];
andrewboyson 9:f354b4859b0b 39
andrewboyson 9:f354b4859b0b 40 uint8_t* hash = alloca(32);
andrewboyson 9:f354b4859b0b 41 PrfHmacSha256(masterSecret, 48, seed, 15 + 32, 1, hash);
andrewboyson 9:f354b4859b0b 42 for (int i = 0; i < 12; i++) output12[i] = hash[i];
andrewboyson 9:f354b4859b0b 43 }
andrewboyson 9:f354b4859b0b 44 void TlsPrfClientFinished(uint8_t * masterSecret, uint8_t* handshakeHash, uint8_t* output12)
andrewboyson 9:f354b4859b0b 45 {
andrewboyson 9:f354b4859b0b 46 uint8_t* seed = alloca(15 + 32);
andrewboyson 9:f354b4859b0b 47 for (int i = 0; i < 15; i++) seed[i ] = "client finished"[i];
andrewboyson 9:f354b4859b0b 48 for (int i = 0; i < 32; i++) seed[i + 15] = handshakeHash[i];
andrewboyson 9:f354b4859b0b 49
andrewboyson 9:f354b4859b0b 50 uint8_t* hash = alloca(32);
andrewboyson 9:f354b4859b0b 51 PrfHmacSha256(masterSecret, 48, seed, 15 + 32, 1, hash);
andrewboyson 9:f354b4859b0b 52 for (int i = 0; i < 12; i++) output12[i] = hash[i];
andrewboyson 9:f354b4859b0b 53 }