A simple library to support serving https.
Dependents: oldheating gps motorhome heating
rsa/rsa-slow.c@10:e269fd7b9500, 2019-09-24 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Sep 24 18:11:02 2019 +0000
- Revision:
- 10:e269fd7b9500
- Parent:
- bignum/bn-async.c@2:82268409e83f
- Child:
- 12:2c342345b3db
Got padlock and some application data through. Now need to use China remainder theorem to speed up decryption and things up and the session id to avoid having to do the decryption.
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 | 10:e269fd7b9500 | 3 | #include "rsa.h" |
andrewboyson | 0:be515c9019e3 | 4 | #include "bignum.h" |
andrewboyson | 0:be515c9019e3 | 5 | #include "hrtimer.h" |
andrewboyson | 2:82268409e83f | 6 | #include "log.h" |
andrewboyson | 0:be515c9019e3 | 7 | |
andrewboyson | 10:e269fd7b9500 | 8 | #define STATUS_NONE 0 |
andrewboyson | 10:e269fd7b9500 | 9 | #define STATUS_STARTED 1 |
andrewboyson | 10:e269fd7b9500 | 10 | #define STATUS_FINISHED 2 |
andrewboyson | 10:e269fd7b9500 | 11 | |
andrewboyson | 2:82268409e83f | 12 | #define MAX_COUNT 4 |
andrewboyson | 2:82268409e83f | 13 | |
andrewboyson | 2:82268409e83f | 14 | static uint32_t m[MAX_COUNT][32]; |
andrewboyson | 2:82268409e83f | 15 | static uint32_t e[MAX_COUNT][32]; |
andrewboyson | 2:82268409e83f | 16 | static uint32_t n[MAX_COUNT][32]; |
andrewboyson | 2:82268409e83f | 17 | static uint32_t r[MAX_COUNT][32]; |
andrewboyson | 2:82268409e83f | 18 | |
andrewboyson | 10:e269fd7b9500 | 19 | static int status[MAX_COUNT]; |
andrewboyson | 0:be515c9019e3 | 20 | |
andrewboyson | 10:e269fd7b9500 | 21 | bool RsaSlowFinished(int slot) |
andrewboyson | 10:e269fd7b9500 | 22 | { |
andrewboyson | 10:e269fd7b9500 | 23 | return status[slot] == STATUS_FINISHED; |
andrewboyson | 10:e269fd7b9500 | 24 | } |
andrewboyson | 10:e269fd7b9500 | 25 | |
andrewboyson | 10:e269fd7b9500 | 26 | uint32_t* RsaSlowResult(int slot) |
andrewboyson | 2:82268409e83f | 27 | { |
andrewboyson | 2:82268409e83f | 28 | return r[slot]; |
andrewboyson | 2:82268409e83f | 29 | } |
andrewboyson | 10:e269fd7b9500 | 30 | void RsaSlowClear(int slot) //This is for security - call it as soon as you no longer need the result. |
andrewboyson | 2:82268409e83f | 31 | { |
andrewboyson | 10:e269fd7b9500 | 32 | status[slot] = STATUS_NONE; |
andrewboyson | 2:82268409e83f | 33 | BnZer1024(m[slot]); |
andrewboyson | 2:82268409e83f | 34 | BnZer1024(e[slot]); |
andrewboyson | 2:82268409e83f | 35 | BnZer1024(n[slot]); |
andrewboyson | 2:82268409e83f | 36 | BnZer1024(r[slot]); |
andrewboyson | 2:82268409e83f | 37 | } |
andrewboyson | 10:e269fd7b9500 | 38 | int RsaSlowStart(uint32_t* message, uint32_t* exponent, uint32_t* modulus) //Returns the slot or -1 on failure - you must check! |
andrewboyson | 0:be515c9019e3 | 39 | { |
andrewboyson | 2:82268409e83f | 40 | //If the exponent is empty then bomb out |
andrewboyson | 2:82268409e83f | 41 | if (BnIse1024(exponent)) |
andrewboyson | 2:82268409e83f | 42 | { |
andrewboyson | 10:e269fd7b9500 | 43 | LogTime("RsaSlowStart - empty exponent\r\n"); |
andrewboyson | 2:82268409e83f | 44 | return -1; |
andrewboyson | 2:82268409e83f | 45 | } |
andrewboyson | 2:82268409e83f | 46 | |
andrewboyson | 2:82268409e83f | 47 | //Look for an empty slot |
andrewboyson | 2:82268409e83f | 48 | int slot = 0; |
andrewboyson | 2:82268409e83f | 49 | while (slot < MAX_COUNT) |
andrewboyson | 2:82268409e83f | 50 | { |
andrewboyson | 10:e269fd7b9500 | 51 | if (status[slot] == STATUS_NONE) goto found; |
andrewboyson | 2:82268409e83f | 52 | slot++; |
andrewboyson | 2:82268409e83f | 53 | } |
andrewboyson | 0:be515c9019e3 | 54 | |
andrewboyson | 2:82268409e83f | 55 | //Look for a slot whch has been used and not cleared |
andrewboyson | 2:82268409e83f | 56 | slot = 0; |
andrewboyson | 2:82268409e83f | 57 | while (slot < MAX_COUNT) |
andrewboyson | 2:82268409e83f | 58 | { |
andrewboyson | 10:e269fd7b9500 | 59 | if (status[slot] == STATUS_FINISHED) goto found; |
andrewboyson | 2:82268409e83f | 60 | slot++; |
andrewboyson | 2:82268409e83f | 61 | } |
andrewboyson | 2:82268409e83f | 62 | |
andrewboyson | 2:82268409e83f | 63 | //No available slot so bomb out |
andrewboyson | 10:e269fd7b9500 | 64 | LogTimeF("RsaSlowStart - no available slots out of %d\r\n", MAX_COUNT); |
andrewboyson | 2:82268409e83f | 65 | return -1; |
andrewboyson | 2:82268409e83f | 66 | |
andrewboyson | 2:82268409e83f | 67 | //Start the calculation |
andrewboyson | 2:82268409e83f | 68 | found: |
andrewboyson | 10:e269fd7b9500 | 69 | BnModExpStart1024(m[slot], e[slot], n[slot], r[slot], message, exponent, modulus); |
andrewboyson | 10:e269fd7b9500 | 70 | status[slot] = STATUS_STARTED; |
andrewboyson | 2:82268409e83f | 71 | return slot; |
andrewboyson | 0:be515c9019e3 | 72 | } |
andrewboyson | 10:e269fd7b9500 | 73 | void RsaSlowMain() |
andrewboyson | 0:be515c9019e3 | 74 | { |
andrewboyson | 2:82268409e83f | 75 | int slot = 0; |
andrewboyson | 2:82268409e83f | 76 | while (slot < MAX_COUNT) |
andrewboyson | 2:82268409e83f | 77 | { |
andrewboyson | 10:e269fd7b9500 | 78 | if (status[slot] == STATUS_STARTED) goto found; |
andrewboyson | 2:82268409e83f | 79 | slot++; |
andrewboyson | 2:82268409e83f | 80 | } |
andrewboyson | 2:82268409e83f | 81 | return; |
andrewboyson | 2:82268409e83f | 82 | |
andrewboyson | 2:82268409e83f | 83 | found: |
andrewboyson | 10:e269fd7b9500 | 84 | ; |
andrewboyson | 10:e269fd7b9500 | 85 | bool finished = BnModExpIterate1024(m[slot], e[slot], n[slot], r[slot]); |
andrewboyson | 10:e269fd7b9500 | 86 | |
andrewboyson | 10:e269fd7b9500 | 87 | if (finished) status[slot] = STATUS_FINISHED; |
andrewboyson | 2:82268409e83f | 88 | } |
andrewboyson | 2:82268409e83f | 89 | |
andrewboyson | 10:e269fd7b9500 | 90 | void RsaSlowInit(void) |
andrewboyson | 2:82268409e83f | 91 | { |
andrewboyson | 10:e269fd7b9500 | 92 | for (int i = 0; i < MAX_COUNT; i++) status[i] = STATUS_NONE; |
andrewboyson | 0:be515c9019e3 | 93 | } |