A simple library to support serving https.
Dependents: oldheating gps motorhome heating
bignum/bn-async.c@0:be515c9019e3, 2019-07-25 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jul 25 21:16:24 2019 +0000
- Revision:
- 0:be515c9019e3
- Child:
- 2:82268409e83f
Pulled together existing modules from https and big numbers into this one. Added TLS PRF module.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:be515c9019e3 | 5 | |
andrewboyson | 0:be515c9019e3 | 6 | static uint32_t m[32]; |
andrewboyson | 0:be515c9019e3 | 7 | static uint32_t e[32]; |
andrewboyson | 0:be515c9019e3 | 8 | static uint32_t n[32]; |
andrewboyson | 0:be515c9019e3 | 9 | static uint32_t* r; //This is supplied by the requester and must be static |
andrewboyson | 0:be515c9019e3 | 10 | |
andrewboyson | 0:be515c9019e3 | 11 | int BnExpModStatus = BIGNUM_CALC_NONE; |
andrewboyson | 0:be515c9019e3 | 12 | int BnExpModProgress = 0; |
andrewboyson | 0:be515c9019e3 | 13 | uint64_t BnMulHr = 0; |
andrewboyson | 0:be515c9019e3 | 14 | uint64_t BnModHr = 0; |
andrewboyson | 0:be515c9019e3 | 15 | |
andrewboyson | 0:be515c9019e3 | 16 | void BnExpModStart(uint32_t* message, uint32_t* exponent, uint32_t* modulus, uint32_t* result) |
andrewboyson | 0:be515c9019e3 | 17 | { |
andrewboyson | 0:be515c9019e3 | 18 | if (BnIse1024(exponent)) return; |
andrewboyson | 0:be515c9019e3 | 19 | BnRem1024(1024, message, modulus, m); //message = message % modulus; |
andrewboyson | 0:be515c9019e3 | 20 | BnCpy1024(e, exponent); |
andrewboyson | 0:be515c9019e3 | 21 | BnCpy1024(n, modulus); |
andrewboyson | 0:be515c9019e3 | 22 | r = result; |
andrewboyson | 0:be515c9019e3 | 23 | BnZer1024(r); |
andrewboyson | 0:be515c9019e3 | 24 | r[0] = 1; |
andrewboyson | 0:be515c9019e3 | 25 | |
andrewboyson | 0:be515c9019e3 | 26 | BnMulHr = 0; |
andrewboyson | 0:be515c9019e3 | 27 | BnModHr = 0; |
andrewboyson | 0:be515c9019e3 | 28 | BnExpModStatus = BIGNUM_CALC_STARTED; |
andrewboyson | 0:be515c9019e3 | 29 | BnExpModProgress = 0; |
andrewboyson | 0:be515c9019e3 | 30 | } |
andrewboyson | 0:be515c9019e3 | 31 | void BnAsyncMain() |
andrewboyson | 0:be515c9019e3 | 32 | { |
andrewboyson | 0:be515c9019e3 | 33 | /* |
andrewboyson | 0:be515c9019e3 | 34 | UInt64 result = 1; |
andrewboyson | 0:be515c9019e3 | 35 | message = message % modulus; |
andrewboyson | 0:be515c9019e3 | 36 | while (exponent != 0) |
andrewboyson | 0:be515c9019e3 | 37 | { |
andrewboyson | 0:be515c9019e3 | 38 | if ((exponent & 1) == 1) result = (result * message) % modulus; |
andrewboyson | 0:be515c9019e3 | 39 | message = (message * message) % modulus; |
andrewboyson | 0:be515c9019e3 | 40 | exponent = exponent >> 1; |
andrewboyson | 0:be515c9019e3 | 41 | } |
andrewboyson | 0:be515c9019e3 | 42 | return result; |
andrewboyson | 0:be515c9019e3 | 43 | */ |
andrewboyson | 0:be515c9019e3 | 44 | if (BnExpModStatus != BIGNUM_CALC_STARTED) return; |
andrewboyson | 0:be515c9019e3 | 45 | if (BnIne1024(r)) BnExpModProgress++; |
andrewboyson | 0:be515c9019e3 | 46 | |
andrewboyson | 0:be515c9019e3 | 47 | uint32_t temp[64]; |
andrewboyson | 0:be515c9019e3 | 48 | |
andrewboyson | 0:be515c9019e3 | 49 | uint32_t hrBefore; |
andrewboyson | 0:be515c9019e3 | 50 | if (e[0] & 1) |
andrewboyson | 0:be515c9019e3 | 51 | { |
andrewboyson | 0:be515c9019e3 | 52 | hrBefore = HrTimerCount(); Bn1024Mul2048(r, m, temp); BnMulHr += HrTimerCount() - hrBefore; |
andrewboyson | 0:be515c9019e3 | 53 | hrBefore = HrTimerCount(); BnRem1024(2048, temp, n, r); BnModHr += HrTimerCount() - hrBefore; |
andrewboyson | 0:be515c9019e3 | 54 | } |
andrewboyson | 0:be515c9019e3 | 55 | hrBefore = HrTimerCount(); Bn1024Mul2048(m, m, temp); BnMulHr += HrTimerCount() - hrBefore; |
andrewboyson | 0:be515c9019e3 | 56 | hrBefore = HrTimerCount(); BnRem1024(2048, temp, n, m); BnModHr += HrTimerCount() - hrBefore; |
andrewboyson | 0:be515c9019e3 | 57 | |
andrewboyson | 0:be515c9019e3 | 58 | BnShr1024(e, false); |
andrewboyson | 0:be515c9019e3 | 59 | if (BnIse1024(e)) BnExpModStatus = BIGNUM_CALC_FINISHED; |
andrewboyson | 0:be515c9019e3 | 60 | } |