Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Tue Sep 24 18:11:02 2019 +0000
Revision:
10:e269fd7b9500
Parent:
8:5e66a6b4b38c
Child:
14:03a0b8fd6ddc
Got padlock and some application data through. Now need to use China remainder theorem to speed up decryption and things up and the session id to avoid having to do the decryption.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 5:ee5489ee1117 1 #include <stdlib.h>
andrewboyson 5:ee5489ee1117 2
andrewboyson 5:ee5489ee1117 3 #include "tls-connection.h"
andrewboyson 5:ee5489ee1117 4 #include "mstimer.h"
andrewboyson 5:ee5489ee1117 5
andrewboyson 5:ee5489ee1117 6 #define MAX_CONNECTIONS 4
andrewboyson 5:ee5489ee1117 7
andrewboyson 5:ee5489ee1117 8 static struct TlsConnection connections[MAX_CONNECTIONS];
andrewboyson 5:ee5489ee1117 9
andrewboyson 8:5e66a6b4b38c 10 struct TlsConnection* TlsConnectionGetNext()
andrewboyson 8:5e66a6b4b38c 11 {
andrewboyson 8:5e66a6b4b38c 12 static struct TlsConnection* p = connections + MAX_CONNECTIONS - 1; //Initialise to last element
andrewboyson 8:5e66a6b4b38c 13 p++;
andrewboyson 8:5e66a6b4b38c 14 if (p >= connections + MAX_CONNECTIONS) p = connections;
andrewboyson 8:5e66a6b4b38c 15 return p;
andrewboyson 8:5e66a6b4b38c 16 }
andrewboyson 8:5e66a6b4b38c 17
andrewboyson 5:ee5489ee1117 18 static void zeroConnection(struct TlsConnection* p)
andrewboyson 5:ee5489ee1117 19 {
andrewboyson 10:e269fd7b9500 20 p->id = 0;
andrewboyson 10:e269fd7b9500 21 p->lastUsed = 0;
andrewboyson 10:e269fd7b9500 22 p->toDo = 0;
andrewboyson 10:e269fd7b9500 23 p->sessionId = 0;
andrewboyson 8:5e66a6b4b38c 24 Sha256Start(&p->handshakeSha); //This just clears any information previously calculated
andrewboyson 6:819c17738dc2 25 p->clientEncrypted = false;
andrewboyson 6:819c17738dc2 26 p->serverEncrypted = false;
andrewboyson 6:819c17738dc2 27 for (int i = 0; i < TLS_RANDOM_SIZE; i++) p->clientRandom[i] = 0;
andrewboyson 6:819c17738dc2 28 for (int i = 0; i < TLS_RANDOM_SIZE; i++) p->serverRandom[i] = 0;
andrewboyson 8:5e66a6b4b38c 29 for (int i = 0; i < TLS_DEFERRED_CONTENT_SIZE; i++) p->deferredContent[i] = 0;
andrewboyson 10:e269fd7b9500 30 p->clientSequence = 0;
andrewboyson 10:e269fd7b9500 31 p->serverSequence = 0;
andrewboyson 10:e269fd7b9500 32 p->clientPositionInStreamOffset = 0;
andrewboyson 10:e269fd7b9500 33 p->serverPositionInStreamOffset = 0;
andrewboyson 5:ee5489ee1117 34 }
andrewboyson 5:ee5489ee1117 35
andrewboyson 10:e269fd7b9500 36 struct TlsConnection* TlsConnectionNew(int id) //Never fails so never returns NULL
andrewboyson 5:ee5489ee1117 37 {
andrewboyson 5:ee5489ee1117 38 struct TlsConnection* p;
andrewboyson 5:ee5489ee1117 39
andrewboyson 5:ee5489ee1117 40 //Look for an existing connection
andrewboyson 5:ee5489ee1117 41 for (p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 42 {
andrewboyson 10:e269fd7b9500 43 if (p->id == id) goto end;
andrewboyson 5:ee5489ee1117 44 }
andrewboyson 5:ee5489ee1117 45
andrewboyson 5:ee5489ee1117 46 //look for an empty connection
andrewboyson 5:ee5489ee1117 47 {
andrewboyson 5:ee5489ee1117 48 struct TlsConnection* pOldest = 0;
andrewboyson 5:ee5489ee1117 49 uint32_t ageOldest = 0;
andrewboyson 5:ee5489ee1117 50 for (p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 51 {
andrewboyson 5:ee5489ee1117 52 if (!p->id) goto end;
andrewboyson 5:ee5489ee1117 53
andrewboyson 5:ee5489ee1117 54 //Otherwise record the oldest and keep going
andrewboyson 5:ee5489ee1117 55 uint32_t age = MsTimerCount - p->lastUsed;
andrewboyson 5:ee5489ee1117 56 if (age >= ageOldest)
andrewboyson 5:ee5489ee1117 57 {
andrewboyson 5:ee5489ee1117 58 ageOldest = age;
andrewboyson 5:ee5489ee1117 59 pOldest = p;
andrewboyson 5:ee5489ee1117 60 }
andrewboyson 5:ee5489ee1117 61 }
andrewboyson 5:ee5489ee1117 62 //No empty ones found so use the oldest
andrewboyson 5:ee5489ee1117 63 p = pOldest;
andrewboyson 5:ee5489ee1117 64 }
andrewboyson 10:e269fd7b9500 65
andrewboyson 5:ee5489ee1117 66
andrewboyson 5:ee5489ee1117 67 end:
andrewboyson 5:ee5489ee1117 68 zeroConnection(p);
andrewboyson 10:e269fd7b9500 69 p->id = id;
andrewboyson 5:ee5489ee1117 70 p->lastUsed = MsTimerCount;
andrewboyson 5:ee5489ee1117 71 return p;
andrewboyson 5:ee5489ee1117 72 }
andrewboyson 10:e269fd7b9500 73 struct TlsConnection* TlsConnectionOrNull(int id)
andrewboyson 5:ee5489ee1117 74 {
andrewboyson 5:ee5489ee1117 75 for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 76 {
andrewboyson 10:e269fd7b9500 77 if (p->id == id)
andrewboyson 5:ee5489ee1117 78 {
andrewboyson 5:ee5489ee1117 79 p->lastUsed = MsTimerCount;
andrewboyson 5:ee5489ee1117 80 return p;
andrewboyson 5:ee5489ee1117 81 }
andrewboyson 5:ee5489ee1117 82 }
andrewboyson 5:ee5489ee1117 83 return NULL;
andrewboyson 5:ee5489ee1117 84 }
andrewboyson 10:e269fd7b9500 85 void TlsConnectionReset(int id)
andrewboyson 5:ee5489ee1117 86 {
andrewboyson 5:ee5489ee1117 87 for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 88 {
andrewboyson 10:e269fd7b9500 89 if (p->id == id) zeroConnection(p);
andrewboyson 5:ee5489ee1117 90 }
andrewboyson 5:ee5489ee1117 91 }
andrewboyson 8:5e66a6b4b38c 92