A simple library to support serving https.

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Jul 31 15:12:34 2019 +0000
Revision:
2:82268409e83f
Parent:
0:be515c9019e3
A lot of tidying. Not working yet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:be515c9019e3 1 #include <stdint.h>
andrewboyson 0:be515c9019e3 2 #include <stdbool.h>
andrewboyson 0:be515c9019e3 3 #include "bignum.h"
andrewboyson 0:be515c9019e3 4 #include "hrtimer.h"
andrewboyson 2:82268409e83f 5 #include "log.h"
andrewboyson 0:be515c9019e3 6
andrewboyson 2:82268409e83f 7 #define MAX_COUNT 4
andrewboyson 2:82268409e83f 8
andrewboyson 2:82268409e83f 9 static uint32_t m[MAX_COUNT][32];
andrewboyson 2:82268409e83f 10 static uint32_t e[MAX_COUNT][32];
andrewboyson 2:82268409e83f 11 static uint32_t n[MAX_COUNT][32];
andrewboyson 2:82268409e83f 12 static uint32_t r[MAX_COUNT][32];
andrewboyson 2:82268409e83f 13
andrewboyson 2:82268409e83f 14 int BnExpModStatus [MAX_COUNT];
andrewboyson 2:82268409e83f 15 int BnExpModProgress[MAX_COUNT];
andrewboyson 2:82268409e83f 16 uint64_t BnMulHr [MAX_COUNT];
andrewboyson 2:82268409e83f 17 uint64_t BnModHr [MAX_COUNT];
andrewboyson 0:be515c9019e3 18
andrewboyson 2:82268409e83f 19 uint32_t* BnExpModGetResult(int slot)
andrewboyson 2:82268409e83f 20 {
andrewboyson 2:82268409e83f 21 return r[slot];
andrewboyson 2:82268409e83f 22 }
andrewboyson 2:82268409e83f 23 void BnExpModClear(int slot) //This is for security - call it as soon as you no longer need the result.
andrewboyson 2:82268409e83f 24 {
andrewboyson 2:82268409e83f 25 BnExpModStatus[slot] = BIGNUM_CALC_NONE;
andrewboyson 2:82268409e83f 26 BnZer1024(m[slot]);
andrewboyson 2:82268409e83f 27 BnZer1024(e[slot]);
andrewboyson 2:82268409e83f 28 BnZer1024(n[slot]);
andrewboyson 2:82268409e83f 29 BnZer1024(r[slot]);
andrewboyson 2:82268409e83f 30 }
andrewboyson 2:82268409e83f 31 int BnExpModStart(uint32_t* message, uint32_t* exponent, uint32_t* modulus) //Returns the slot or -1 on failure - you must check!
andrewboyson 0:be515c9019e3 32 {
andrewboyson 2:82268409e83f 33 //If the exponent is empty then bomb out
andrewboyson 2:82268409e83f 34 if (BnIse1024(exponent))
andrewboyson 2:82268409e83f 35 {
andrewboyson 2:82268409e83f 36 LogTime("BnExpModStart - empty exponent\r\n");
andrewboyson 2:82268409e83f 37 return -1;
andrewboyson 2:82268409e83f 38 }
andrewboyson 2:82268409e83f 39
andrewboyson 2:82268409e83f 40 //Look for an empty slot
andrewboyson 2:82268409e83f 41 int slot = 0;
andrewboyson 2:82268409e83f 42 while (slot < MAX_COUNT)
andrewboyson 2:82268409e83f 43 {
andrewboyson 2:82268409e83f 44 if (BnExpModStatus[slot] == BIGNUM_CALC_NONE) goto found;
andrewboyson 2:82268409e83f 45 slot++;
andrewboyson 2:82268409e83f 46 }
andrewboyson 0:be515c9019e3 47
andrewboyson 2:82268409e83f 48 //Look for a slot whch has been used and not cleared
andrewboyson 2:82268409e83f 49 slot = 0;
andrewboyson 2:82268409e83f 50 while (slot < MAX_COUNT)
andrewboyson 2:82268409e83f 51 {
andrewboyson 2:82268409e83f 52 if (BnExpModStatus[slot] == BIGNUM_CALC_FINISHED) goto found;
andrewboyson 2:82268409e83f 53 slot++;
andrewboyson 2:82268409e83f 54 }
andrewboyson 2:82268409e83f 55
andrewboyson 2:82268409e83f 56 //No available slot so bomb out
andrewboyson 2:82268409e83f 57 LogTimeF("BnExpModStart - no available slots out of %d\r\n", MAX_COUNT);
andrewboyson 2:82268409e83f 58 return -1;
andrewboyson 2:82268409e83f 59
andrewboyson 2:82268409e83f 60 //Start the calculation
andrewboyson 2:82268409e83f 61 found:
andrewboyson 2:82268409e83f 62 BnRem1024(1024, message, modulus, m[slot]); //message = message % modulus;
andrewboyson 2:82268409e83f 63 BnCpy1024(e[slot], exponent);
andrewboyson 2:82268409e83f 64 BnCpy1024(n[slot], modulus);
andrewboyson 2:82268409e83f 65 BnZer1024(r[slot]);
andrewboyson 2:82268409e83f 66 r[slot][0] = 1;
andrewboyson 2:82268409e83f 67
andrewboyson 2:82268409e83f 68 BnMulHr[slot] = 0;
andrewboyson 2:82268409e83f 69 BnModHr[slot] = 0;
andrewboyson 2:82268409e83f 70 BnExpModStatus[slot] = BIGNUM_CALC_STARTED;
andrewboyson 2:82268409e83f 71 BnExpModProgress[slot] = 0;
andrewboyson 2:82268409e83f 72 return slot;
andrewboyson 0:be515c9019e3 73 }
andrewboyson 0:be515c9019e3 74 void BnAsyncMain()
andrewboyson 0:be515c9019e3 75 {
andrewboyson 0:be515c9019e3 76 /*
andrewboyson 0:be515c9019e3 77 UInt64 result = 1;
andrewboyson 0:be515c9019e3 78 message = message % modulus;
andrewboyson 0:be515c9019e3 79 while (exponent != 0)
andrewboyson 0:be515c9019e3 80 {
andrewboyson 0:be515c9019e3 81 if ((exponent & 1) == 1) result = (result * message) % modulus;
andrewboyson 0:be515c9019e3 82 message = (message * message) % modulus;
andrewboyson 0:be515c9019e3 83 exponent = exponent >> 1;
andrewboyson 0:be515c9019e3 84 }
andrewboyson 0:be515c9019e3 85 return result;
andrewboyson 0:be515c9019e3 86 */
andrewboyson 2:82268409e83f 87 int slot = 0;
andrewboyson 2:82268409e83f 88 while (slot < MAX_COUNT)
andrewboyson 2:82268409e83f 89 {
andrewboyson 2:82268409e83f 90 if (BnExpModStatus[slot] == BIGNUM_CALC_STARTED) goto found;
andrewboyson 2:82268409e83f 91 slot++;
andrewboyson 2:82268409e83f 92 }
andrewboyson 2:82268409e83f 93 return;
andrewboyson 2:82268409e83f 94
andrewboyson 2:82268409e83f 95 found:
andrewboyson 2:82268409e83f 96 if (BnIne1024(r[slot])) BnExpModProgress[slot]++;
andrewboyson 0:be515c9019e3 97
andrewboyson 0:be515c9019e3 98 uint32_t temp[64];
andrewboyson 0:be515c9019e3 99
andrewboyson 0:be515c9019e3 100 uint32_t hrBefore;
andrewboyson 2:82268409e83f 101 if (e[slot][0] & 1)
andrewboyson 0:be515c9019e3 102 {
andrewboyson 2:82268409e83f 103 hrBefore = HrTimerCount(); Bn1024Mul2048(r[slot], m[slot], temp); BnMulHr[slot] += HrTimerCount() - hrBefore;
andrewboyson 2:82268409e83f 104 hrBefore = HrTimerCount(); BnRem1024(2048, temp, n[slot], r[slot]); BnModHr[slot] += HrTimerCount() - hrBefore;
andrewboyson 0:be515c9019e3 105 }
andrewboyson 2:82268409e83f 106 hrBefore = HrTimerCount(); Bn1024Mul2048(m[slot], m[slot], temp); BnMulHr[slot] += HrTimerCount() - hrBefore;
andrewboyson 2:82268409e83f 107 hrBefore = HrTimerCount(); BnRem1024(2048, temp, n[slot], m[slot]); BnModHr[slot] += HrTimerCount() - hrBefore;
andrewboyson 0:be515c9019e3 108
andrewboyson 2:82268409e83f 109 BnShr1024(e[slot], false);
andrewboyson 2:82268409e83f 110 if (BnIse1024(e[slot])) BnExpModStatus[slot] = BIGNUM_CALC_FINISHED;
andrewboyson 2:82268409e83f 111 }
andrewboyson 2:82268409e83f 112
andrewboyson 2:82268409e83f 113 void BnAsyncInit(void)
andrewboyson 2:82268409e83f 114 {
andrewboyson 2:82268409e83f 115 for (int i = 0; i < MAX_COUNT; i++) BnExpModStatus[i] = BIGNUM_CALC_NONE;
andrewboyson 0:be515c9019e3 116 }