Controls the central heating system and the lights.

Dependencies:   1-wire clock crypto fram log lpc1768 net web wiz mbed

Committer:
andrewboyson
Date:
Sat Nov 12 10:03:38 2022 +0000
Revision:
8:8ac076ce51af
Parent:
0:22b158d3c76f
Updated LPC1768 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:22b158d3c76f 1 #include <stdio.h>
andrewboyson 0:22b158d3c76f 2 #include <string.h>
andrewboyson 0:22b158d3c76f 3
andrewboyson 0:22b158d3c76f 4 #include "http.h"
andrewboyson 0:22b158d3c76f 5 #include "web-nav-this.h"
andrewboyson 0:22b158d3c76f 6 #include "web-add.h"
andrewboyson 0:22b158d3c76f 7 #include "log.h"
andrewboyson 0:22b158d3c76f 8 #include "bignum.h"
andrewboyson 0:22b158d3c76f 9 #include "rsa.h"
andrewboyson 0:22b158d3c76f 10 #include "sha256.h"
andrewboyson 0:22b158d3c76f 11 #include "hmac-sha256.h"
andrewboyson 0:22b158d3c76f 12 #include "tls-prf.h"
andrewboyson 0:22b158d3c76f 13 #include "aes128cbc.h"
andrewboyson 0:22b158d3c76f 14 #include "sha1.h"
andrewboyson 0:22b158d3c76f 15 #include "pri-key.h"
andrewboyson 0:22b158d3c76f 16
andrewboyson 0:22b158d3c76f 17 void WebSystemHtml()
andrewboyson 0:22b158d3c76f 18 {
andrewboyson 0:22b158d3c76f 19 HttpOk("text/html; charset=UTF-8", "no-cache", NULL, NULL);
andrewboyson 0:22b158d3c76f 20 WebAddHeader("System", "settings.css", "system.js");
andrewboyson 0:22b158d3c76f 21 WebAddNav(SYSTEM_PAGE);
andrewboyson 0:22b158d3c76f 22 WebAddH1("System");
andrewboyson 0:22b158d3c76f 23
andrewboyson 0:22b158d3c76f 24 WebAddH2("TFTP");
andrewboyson 0:22b158d3c76f 25 WebAddAjaxInput ("Server url", 5, "ajax-server-name", "tftpserver" );
andrewboyson 0:22b158d3c76f 26 WebAddAjaxInput ("File (strftime)", 11, "ajax-file-name", "tftpfilename" );
andrewboyson 0:22b158d3c76f 27 WebAddAjaxInput ("Interval (secs) 0=no", 5, "ajax-read-interval", "tftpreadint" );
andrewboyson 0:22b158d3c76f 28 WebAddAjaxInput ("Records per backup 0=no", 5, "ajax-write-size", "tftpwriteint" );
andrewboyson 0:22b158d3c76f 29 WebAddAjaxLabelled("Count", "ajax-count" );
andrewboyson 0:22b158d3c76f 30 WebAddAjaxLabelled("Started", "ajax-start-time" );
andrewboyson 0:22b158d3c76f 31
andrewboyson 0:22b158d3c76f 32 WebAddH2("FRAM");
andrewboyson 0:22b158d3c76f 33 WebAddAjaxLabelled("Used", "ajax-fram-used" );
andrewboyson 0:22b158d3c76f 34
andrewboyson 0:22b158d3c76f 35 WebAddH2("Compiler");
andrewboyson 0:22b158d3c76f 36 WebAddLabelledInt("Version Vvvbbbb", __ARMCC_VERSION);
andrewboyson 0:22b158d3c76f 37
andrewboyson 0:22b158d3c76f 38 WebAddH2("AES 128 test");
andrewboyson 0:22b158d3c76f 39 uint8_t aeskey[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
andrewboyson 0:22b158d3c76f 40 uint8_t in[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
andrewboyson 0:22b158d3c76f 41 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
andrewboyson 0:22b158d3c76f 42 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
andrewboyson 0:22b158d3c76f 43 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
andrewboyson 0:22b158d3c76f 44 uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
andrewboyson 0:22b158d3c76f 45 /*
andrewboyson 0:22b158d3c76f 46 uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
andrewboyson 0:22b158d3c76f 47 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
andrewboyson 0:22b158d3c76f 48 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
andrewboyson 0:22b158d3c76f 49 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
andrewboyson 0:22b158d3c76f 50 */
andrewboyson 0:22b158d3c76f 51
andrewboyson 0:22b158d3c76f 52 Aes128CbcDecrypt(aeskey, iv, in, 64);
andrewboyson 0:22b158d3c76f 53 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 54 HttpAddBytesAsHex(in, sizeof(in));
andrewboyson 0:22b158d3c76f 55 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 56
andrewboyson 0:22b158d3c76f 57 WebAddH2("SHA 256 stream test");
andrewboyson 0:22b158d3c76f 58 uint8_t hash[32];
andrewboyson 0:22b158d3c76f 59 char* input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
andrewboyson 0:22b158d3c76f 60 struct Sha256State sha256state;
andrewboyson 0:22b158d3c76f 61 Sha256Start (&sha256state);
andrewboyson 0:22b158d3c76f 62 Sha256Add (&sha256state, (uint8_t*)input, strlen(input));
andrewboyson 0:22b158d3c76f 63 Sha256Finish(&sha256state, (uint8_t*)hash);
andrewboyson 0:22b158d3c76f 64
andrewboyson 0:22b158d3c76f 65 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 66 HttpAddBytesAsHex(hash, sizeof(hash));
andrewboyson 0:22b158d3c76f 67 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 68
andrewboyson 0:22b158d3c76f 69 WebAddH2("SHA1 stream test");
andrewboyson 0:22b158d3c76f 70 struct Sha1State sha1state;
andrewboyson 0:22b158d3c76f 71 Sha1Start (&sha1state);
andrewboyson 0:22b158d3c76f 72 Sha1Add (&sha1state, (uint8_t*)input, strlen(input));
andrewboyson 0:22b158d3c76f 73 Sha1Finish(&sha1state, (uint8_t*)hash);
andrewboyson 0:22b158d3c76f 74
andrewboyson 0:22b158d3c76f 75 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 76 HttpAddBytesAsHex(hash, SHA1_HASH_SIZE);
andrewboyson 0:22b158d3c76f 77 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 78
andrewboyson 0:22b158d3c76f 79 WebAddH2("HMAC SHA 256 test");
andrewboyson 0:22b158d3c76f 80 char* key = "Jefe";
andrewboyson 0:22b158d3c76f 81 char* msg = "what do ya want for nothing?";
andrewboyson 0:22b158d3c76f 82 uint8_t mac[32];
andrewboyson 0:22b158d3c76f 83 HmacSha256((uint8_t*)key, strlen(key), (uint8_t*)msg, strlen(msg), mac);
andrewboyson 0:22b158d3c76f 84 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 85 HttpAddBytesAsHex(mac, sizeof(mac));
andrewboyson 0:22b158d3c76f 86 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 87
andrewboyson 0:22b158d3c76f 88 WebAddH2("HMAC SHA 256 stream test");
andrewboyson 0:22b158d3c76f 89 struct HmacSha256Struct hmacState;
andrewboyson 0:22b158d3c76f 90 HmacSha256Start (&hmacState, (uint8_t*)key, strlen(key));
andrewboyson 0:22b158d3c76f 91 HmacSha256Add (&hmacState, (uint8_t*)msg, strlen(msg));
andrewboyson 0:22b158d3c76f 92 HmacSha256Finish(&hmacState, mac);
andrewboyson 0:22b158d3c76f 93 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 94 HttpAddBytesAsHex(mac, sizeof(mac));
andrewboyson 0:22b158d3c76f 95 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 96
andrewboyson 0:22b158d3c76f 97 WebAddH2("Verify data test");
andrewboyson 0:22b158d3c76f 98 uint8_t masterSecret [48] = {0x91, 0x6a, 0xbf, 0x9d, 0xa5, 0x59, 0x73, 0xe1,
andrewboyson 0:22b158d3c76f 99 0x36, 0x14, 0xae, 0x0a, 0x3f, 0x5d, 0x3f, 0x37,
andrewboyson 0:22b158d3c76f 100 0xb0, 0x23, 0xba, 0x12, 0x9a, 0xee, 0x02, 0xcc,
andrewboyson 0:22b158d3c76f 101 0x91, 0x34, 0x33, 0x81, 0x27, 0xcd, 0x70, 0x49,
andrewboyson 0:22b158d3c76f 102 0x78, 0x1c, 0x8e, 0x19, 0xfc, 0x1e, 0xb2, 0xa7,
andrewboyson 0:22b158d3c76f 103 0x38, 0x7a, 0xc0, 0x6a, 0xe2, 0x37, 0x34, 0x4c};
andrewboyson 0:22b158d3c76f 104
andrewboyson 0:22b158d3c76f 105 uint8_t hashHandshakes[32] = {0xb2, 0x01, 0x7b, 0xa2, 0x8d, 0x0e, 0x27, 0xf0,
andrewboyson 0:22b158d3c76f 106 0x3a, 0xe3, 0x27, 0x45, 0x6b, 0x6f, 0xf0, 0x0b,
andrewboyson 0:22b158d3c76f 107 0x4d, 0x5b, 0xbf, 0x0e, 0xf7, 0xcd, 0xa8, 0x3c,
andrewboyson 0:22b158d3c76f 108 0xe1, 0x02, 0x9b, 0x52, 0x1c, 0x3e, 0x7c, 0x35};
andrewboyson 0:22b158d3c76f 109 uint8_t verifyData[12];
andrewboyson 0:22b158d3c76f 110 TlsPrfServerFinished(masterSecret, hashHandshakes, verifyData);
andrewboyson 0:22b158d3c76f 111
andrewboyson 0:22b158d3c76f 112 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 113 HttpAddBytesAsHex(verifyData, sizeof(verifyData));
andrewboyson 0:22b158d3c76f 114 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 115
andrewboyson 0:22b158d3c76f 116
andrewboyson 0:22b158d3c76f 117 WebAddH2("Key test");
andrewboyson 0:22b158d3c76f 118
andrewboyson 0:22b158d3c76f 119 uint8_t clientRandom[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
andrewboyson 0:22b158d3c76f 120 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
andrewboyson 0:22b158d3c76f 121 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
andrewboyson 0:22b158d3c76f 122 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
andrewboyson 0:22b158d3c76f 123
andrewboyson 0:22b158d3c76f 124 uint8_t serverRandom[] = { 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
andrewboyson 0:22b158d3c76f 125 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
andrewboyson 0:22b158d3c76f 126 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
andrewboyson 0:22b158d3c76f 127 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f };
andrewboyson 0:22b158d3c76f 128
andrewboyson 0:22b158d3c76f 129 uint8_t client_MAC_key_20[20];
andrewboyson 0:22b158d3c76f 130 uint8_t server_MAC_key_20[20];
andrewboyson 0:22b158d3c76f 131 uint8_t client_key_16[16];
andrewboyson 0:22b158d3c76f 132 uint8_t server_key_16[16];
andrewboyson 0:22b158d3c76f 133 TlsPrfKeysAes128Sha1(masterSecret, clientRandom, serverRandom, client_MAC_key_20, server_MAC_key_20, client_key_16, server_key_16);
andrewboyson 0:22b158d3c76f 134 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 135 HttpAddBytesAsHex(client_MAC_key_20, sizeof(client_MAC_key_20)); HttpAddText("\r\n");
andrewboyson 0:22b158d3c76f 136 HttpAddBytesAsHex(server_MAC_key_20, sizeof(server_MAC_key_20)); HttpAddText("\r\n");
andrewboyson 0:22b158d3c76f 137 HttpAddBytesAsHex( client_key_16, sizeof( client_key_16)); HttpAddText("\r\n");
andrewboyson 0:22b158d3c76f 138 HttpAddBytesAsHex( server_key_16, sizeof( server_key_16));
andrewboyson 0:22b158d3c76f 139 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 140
andrewboyson 0:22b158d3c76f 141
andrewboyson 0:22b158d3c76f 142
andrewboyson 0:22b158d3c76f 143 char* m =
andrewboyson 0:22b158d3c76f 144 "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
andrewboyson 0:22b158d3c76f 145 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
andrewboyson 0:22b158d3c76f 146 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00302130"
andrewboyson 0:22b158d3c76f 147 "0906052B0E03021A05000414A9993E364706816ABA3E25717850C26C9CD0D89D";
andrewboyson 0:22b158d3c76f 148
andrewboyson 0:22b158d3c76f 149 uint32_t message [32];
andrewboyson 0:22b158d3c76f 150 BnParseHex1024(message, m);
andrewboyson 0:22b158d3c76f 151
andrewboyson 0:22b158d3c76f 152 /*
andrewboyson 0:22b158d3c76f 153 WebAddH2("RSA test");
andrewboyson 0:22b158d3c76f 154
andrewboyson 0:22b158d3c76f 155 char* n =
andrewboyson 0:22b158d3c76f 156 "E08973398DD8F5F5E88776397F4EB005BB5383DE0FB7ABDC7DC775290D052E6D"
andrewboyson 0:22b158d3c76f 157 "12DFA68626D4D26FAA5829FC97ECFA82510F3080BEB1509E4644F12CBBD832CF"
andrewboyson 0:22b158d3c76f 158 "C6686F07D9B060ACBEEE34096A13F5F7050593DF5EBA3556D961FF197FC981E6"
andrewboyson 0:22b158d3c76f 159 "F86CEA874070EFAC6D2C749F2DFA553AB9997702A648528C4EF357385774575F";
andrewboyson 0:22b158d3c76f 160
andrewboyson 0:22b158d3c76f 161 char* d =
andrewboyson 0:22b158d3c76f 162 "00A403C327477634346CA686B57949014B2E8AD2C862B2C7D748096A8B91F736"
andrewboyson 0:22b158d3c76f 163 "F275D6E8CD15906027314735644D95CD6763CEB49F56AC2F376E1CEE0EBF282D"
andrewboyson 0:22b158d3c76f 164 "F439906F34D86E085BD5656AD841F313D72D395EFE33CBFF29E4030B3D05A28F"
andrewboyson 0:22b158d3c76f 165 "B7F18EA27637B07957D32F2BDE8706227D04665EC91BAF8B1AC3EC9144AB7F21";
andrewboyson 0:22b158d3c76f 166
andrewboyson 0:22b158d3c76f 167 uint32_t exponent[32];
andrewboyson 0:22b158d3c76f 168 uint32_t modulus [32];
andrewboyson 0:22b158d3c76f 169
andrewboyson 0:22b158d3c76f 170 BnParseHex1024(exponent, d);
andrewboyson 0:22b158d3c76f 171 BnParseHex1024(modulus, n);
andrewboyson 0:22b158d3c76f 172
andrewboyson 0:22b158d3c76f 173 static int ticket = 0;
andrewboyson 0:22b158d3c76f 174 static bool started = false;
andrewboyson 0:22b158d3c76f 175 if (!started)
andrewboyson 0:22b158d3c76f 176 {
andrewboyson 0:22b158d3c76f 177 ticket = RsaSlowStart(message, exponent, modulus);
andrewboyson 0:22b158d3c76f 178 started = true;
andrewboyson 0:22b158d3c76f 179 }
andrewboyson 0:22b158d3c76f 180
andrewboyson 0:22b158d3c76f 181 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 182 HttpAddText("Message\r\n");
andrewboyson 0:22b158d3c76f 183 BnAsHttp1024(message);
andrewboyson 0:22b158d3c76f 184 HttpAddText("\r\nExponent\r\n");
andrewboyson 0:22b158d3c76f 185 BnAsHttp1024(exponent);
andrewboyson 0:22b158d3c76f 186 HttpAddText("\r\nModulus\r\n");
andrewboyson 0:22b158d3c76f 187 BnAsHttp1024(modulus);
andrewboyson 0:22b158d3c76f 188 HttpAddText("\r\nResult\r\n");
andrewboyson 0:22b158d3c76f 189 if (ticket >= 0)
andrewboyson 0:22b158d3c76f 190 {
andrewboyson 0:22b158d3c76f 191 if (RsaSlowFinished(ticket)) HttpAddText("Finished\r\n");
andrewboyson 0:22b158d3c76f 192 else HttpAddText("Not finished\r\n");
andrewboyson 0:22b158d3c76f 193 BnAsHttp1024(RsaSlowResult(ticket));
andrewboyson 0:22b158d3c76f 194 }
andrewboyson 0:22b158d3c76f 195 else
andrewboyson 0:22b158d3c76f 196 {
andrewboyson 0:22b158d3c76f 197 HttpAddText("No ticket available to calculate result\r\n");
andrewboyson 0:22b158d3c76f 198 }
andrewboyson 0:22b158d3c76f 199 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 200 */
andrewboyson 0:22b158d3c76f 201
andrewboyson 0:22b158d3c76f 202 WebAddH2("RSA test");
andrewboyson 0:22b158d3c76f 203 static int rsaTicket = 0;
andrewboyson 0:22b158d3c76f 204 static bool rsaSlowStarted = false;
andrewboyson 0:22b158d3c76f 205 if (!rsaSlowStarted)
andrewboyson 0:22b158d3c76f 206 {
andrewboyson 0:22b158d3c76f 207 rsaTicket = PriKeyDecryptStart((uint8_t*)message);
andrewboyson 0:22b158d3c76f 208 rsaSlowStarted = true;
andrewboyson 0:22b158d3c76f 209 }
andrewboyson 0:22b158d3c76f 210
andrewboyson 0:22b158d3c76f 211 HttpAddText("<code>");
andrewboyson 0:22b158d3c76f 212 HttpAddText("Message\r\n");
andrewboyson 0:22b158d3c76f 213 BnAsHttp1024(message);
andrewboyson 0:22b158d3c76f 214 HttpAddText("\r\nResult\r\n");
andrewboyson 0:22b158d3c76f 215 if (rsaTicket >= 0)
andrewboyson 0:22b158d3c76f 216 {
andrewboyson 0:22b158d3c76f 217 if (PriKeyDecryptFinished(rsaTicket)) HttpAddText("Finished\r\n");
andrewboyson 0:22b158d3c76f 218 else HttpAddText("Not finished\r\n");
andrewboyson 0:22b158d3c76f 219 BnAsHttp1024((uint32_t*)PriKeyDecryptResultLittleEndian(rsaTicket));
andrewboyson 0:22b158d3c76f 220 }
andrewboyson 0:22b158d3c76f 221 else
andrewboyson 0:22b158d3c76f 222 {
andrewboyson 0:22b158d3c76f 223 HttpAddText("No ticket available to calculate result\r\n");
andrewboyson 0:22b158d3c76f 224 }
andrewboyson 0:22b158d3c76f 225 HttpAddText("</code>");
andrewboyson 0:22b158d3c76f 226
andrewboyson 0:22b158d3c76f 227 WebAddEnd();
andrewboyson 0:22b158d3c76f 228 }