A simple library to support serving https.
Dependents: oldheating gps motorhome heating
Diff: bignum/bn-async.c
- Revision:
- 2:82268409e83f
- Parent:
- 0:be515c9019e3
--- a/bignum/bn-async.c Fri Jul 26 13:49:52 2019 +0000 +++ b/bignum/bn-async.c Wed Jul 31 15:12:34 2019 +0000 @@ -2,31 +2,74 @@ #include <stdbool.h> #include "bignum.h" #include "hrtimer.h" +#include "log.h" -static uint32_t m[32]; -static uint32_t e[32]; -static uint32_t n[32]; -static uint32_t* r; //This is supplied by the requester and must be static +#define MAX_COUNT 4 + +static uint32_t m[MAX_COUNT][32]; +static uint32_t e[MAX_COUNT][32]; +static uint32_t n[MAX_COUNT][32]; +static uint32_t r[MAX_COUNT][32]; + +int BnExpModStatus [MAX_COUNT]; +int BnExpModProgress[MAX_COUNT]; +uint64_t BnMulHr [MAX_COUNT]; +uint64_t BnModHr [MAX_COUNT]; -int BnExpModStatus = BIGNUM_CALC_NONE; -int BnExpModProgress = 0; -uint64_t BnMulHr = 0; -uint64_t BnModHr = 0; - -void BnExpModStart(uint32_t* message, uint32_t* exponent, uint32_t* modulus, uint32_t* result) +uint32_t* BnExpModGetResult(int slot) +{ + return r[slot]; +} +void BnExpModClear(int slot) //This is for security - call it as soon as you no longer need the result. +{ + BnExpModStatus[slot] = BIGNUM_CALC_NONE; + BnZer1024(m[slot]); + BnZer1024(e[slot]); + BnZer1024(n[slot]); + BnZer1024(r[slot]); +} +int BnExpModStart(uint32_t* message, uint32_t* exponent, uint32_t* modulus) //Returns the slot or -1 on failure - you must check! { - if (BnIse1024(exponent)) return; - BnRem1024(1024, message, modulus, m); //message = message % modulus; - BnCpy1024(e, exponent); - BnCpy1024(n, modulus); - r = result; - BnZer1024(r); - r[0] = 1; + //If the exponent is empty then bomb out + if (BnIse1024(exponent)) + { + LogTime("BnExpModStart - empty exponent\r\n"); + return -1; + } + + //Look for an empty slot + int slot = 0; + while (slot < MAX_COUNT) + { + if (BnExpModStatus[slot] == BIGNUM_CALC_NONE) goto found; + slot++; + } - BnMulHr = 0; - BnModHr = 0; - BnExpModStatus = BIGNUM_CALC_STARTED; - BnExpModProgress = 0; + //Look for a slot whch has been used and not cleared + slot = 0; + while (slot < MAX_COUNT) + { + if (BnExpModStatus[slot] == BIGNUM_CALC_FINISHED) goto found; + slot++; + } + + //No available slot so bomb out + LogTimeF("BnExpModStart - no available slots out of %d\r\n", MAX_COUNT); + return -1; + + //Start the calculation +found: + BnRem1024(1024, message, modulus, m[slot]); //message = message % modulus; + BnCpy1024(e[slot], exponent); + BnCpy1024(n[slot], modulus); + BnZer1024(r[slot]); + r[slot][0] = 1; + + BnMulHr[slot] = 0; + BnModHr[slot] = 0; + BnExpModStatus[slot] = BIGNUM_CALC_STARTED; + BnExpModProgress[slot] = 0; + return slot; } void BnAsyncMain() { @@ -41,20 +84,33 @@ } return result; */ - if (BnExpModStatus != BIGNUM_CALC_STARTED) return; - if (BnIne1024(r)) BnExpModProgress++; + int slot = 0; + while (slot < MAX_COUNT) + { + if (BnExpModStatus[slot] == BIGNUM_CALC_STARTED) goto found; + slot++; + } + return; + +found: + if (BnIne1024(r[slot])) BnExpModProgress[slot]++; uint32_t temp[64]; uint32_t hrBefore; - if (e[0] & 1) + if (e[slot][0] & 1) { - hrBefore = HrTimerCount(); Bn1024Mul2048(r, m, temp); BnMulHr += HrTimerCount() - hrBefore; - hrBefore = HrTimerCount(); BnRem1024(2048, temp, n, r); BnModHr += HrTimerCount() - hrBefore; + hrBefore = HrTimerCount(); Bn1024Mul2048(r[slot], m[slot], temp); BnMulHr[slot] += HrTimerCount() - hrBefore; + hrBefore = HrTimerCount(); BnRem1024(2048, temp, n[slot], r[slot]); BnModHr[slot] += HrTimerCount() - hrBefore; } - hrBefore = HrTimerCount(); Bn1024Mul2048(m, m, temp); BnMulHr += HrTimerCount() - hrBefore; - hrBefore = HrTimerCount(); BnRem1024(2048, temp, n, m); BnModHr += HrTimerCount() - hrBefore; + hrBefore = HrTimerCount(); Bn1024Mul2048(m[slot], m[slot], temp); BnMulHr[slot] += HrTimerCount() - hrBefore; + hrBefore = HrTimerCount(); BnRem1024(2048, temp, n[slot], m[slot]); BnModHr[slot] += HrTimerCount() - hrBefore; - BnShr1024(e, false); - if (BnIse1024(e)) BnExpModStatus = BIGNUM_CALC_FINISHED; + BnShr1024(e[slot], false); + if (BnIse1024(e[slot])) BnExpModStatus[slot] = BIGNUM_CALC_FINISHED; +} + +void BnAsyncInit(void) +{ + for (int i = 0; i < MAX_COUNT; i++) BnExpModStatus[i] = BIGNUM_CALC_NONE; } \ No newline at end of file