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

/media/uploads/andrewboyson/heating.sch

/media/uploads/andrewboyson/heating.brd

/media/uploads/andrewboyson/eagle.epf

Committer:
andrewboyson
Date:
Wed Oct 02 20:26:57 2019 +0000
Revision:
78:2c339a59be46
Parent:
75:cc9a1e104e0e
Child:
80:99140e224bd7
Updated crypto library

Who changed what in which revision?

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