UpdatedDecryp
Dependencies: BahlDecrypModified CyaSSL mbed nRF51822
Fork of Decryptulator by
Hashes/sfh_mbed.cpp@13:8b706583610a, 2017-05-09 (annotated)
- Committer:
- vbahl2
- Date:
- Tue May 09 03:06:55 2017 +0000
- Revision:
- 13:8b706583610a
UpdatedDecryp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vbahl2 | 13:8b706583610a | 1 | #include "sfh_mbed.h" |
vbahl2 | 13:8b706583610a | 2 | #include "SlidingWindow.h" |
vbahl2 | 13:8b706583610a | 3 | #include "LookupTable.h" |
vbahl2 | 13:8b706583610a | 4 | |
vbahl2 | 13:8b706583610a | 5 | #define LOOKUP_TABLE_SIZE 3000 |
vbahl2 | 13:8b706583610a | 6 | #define SLIDING_WINDOW_SIZE 300 |
vbahl2 | 13:8b706583610a | 7 | |
vbahl2 | 13:8b706583610a | 8 | //static uint32_t sliding_window_index = 0; |
vbahl2 | 13:8b706583610a | 9 | |
vbahl2 | 13:8b706583610a | 10 | |
vbahl2 | 13:8b706583610a | 11 | uint32_t hash (const char *data, uint16_t len) { |
vbahl2 | 13:8b706583610a | 12 | // This is mostly Paul Hsieh's original code |
vbahl2 | 13:8b706583610a | 13 | uint32_t hash, tmp; |
vbahl2 | 13:8b706583610a | 14 | int rem; |
vbahl2 | 13:8b706583610a | 15 | |
vbahl2 | 13:8b706583610a | 16 | if (len <= 0 || data == 0) { |
vbahl2 | 13:8b706583610a | 17 | return 0; |
vbahl2 | 13:8b706583610a | 18 | } |
vbahl2 | 13:8b706583610a | 19 | |
vbahl2 | 13:8b706583610a | 20 | hash = len; |
vbahl2 | 13:8b706583610a | 21 | rem = len & 3; |
vbahl2 | 13:8b706583610a | 22 | len >>= 2; |
vbahl2 | 13:8b706583610a | 23 | |
vbahl2 | 13:8b706583610a | 24 | /* Main loop */ |
vbahl2 | 13:8b706583610a | 25 | while (len > 0) { |
vbahl2 | 13:8b706583610a | 26 | hash += *((uint16_t *) data); |
vbahl2 | 13:8b706583610a | 27 | |
vbahl2 | 13:8b706583610a | 28 | /* To make a long story short, the C standard states that the |
vbahl2 | 13:8b706583610a | 29 | * shift operator's operands must be promoted to (unsigned) int, |
vbahl2 | 13:8b706583610a | 30 | * which is (usually) 32 bits wide on PC and 16 on Arduino. This |
vbahl2 | 13:8b706583610a | 31 | * results in different behaviour, since part of the result gets |
vbahl2 | 13:8b706583610a | 32 | * truncated on Arduino, so we cast the result to make sure all |
vbahl2 | 13:8b706583610a | 33 | * bits are kept. |
vbahl2 | 13:8b706583610a | 34 | */ |
vbahl2 | 13:8b706583610a | 35 | tmp = ((uint32_t) (*((uint16_t *) (data + 2))) << 11) ^ hash; |
vbahl2 | 13:8b706583610a | 36 | |
vbahl2 | 13:8b706583610a | 37 | hash = (hash << 16) ^ tmp; |
vbahl2 | 13:8b706583610a | 38 | data += 2 * sizeof (uint16_t); |
vbahl2 | 13:8b706583610a | 39 | hash += hash >> 11; |
vbahl2 | 13:8b706583610a | 40 | len--; |
vbahl2 | 13:8b706583610a | 41 | } |
vbahl2 | 13:8b706583610a | 42 | |
vbahl2 | 13:8b706583610a | 43 | /* Handle end cases */ |
vbahl2 | 13:8b706583610a | 44 | switch (rem) { |
vbahl2 | 13:8b706583610a | 45 | case 3: |
vbahl2 | 13:8b706583610a | 46 | hash += * ((uint16_t *) data); |
vbahl2 | 13:8b706583610a | 47 | hash ^= hash << 16; |
vbahl2 | 13:8b706583610a | 48 | hash ^= ((signed char) data[2]) << 18; |
vbahl2 | 13:8b706583610a | 49 | hash += hash >> 11; |
vbahl2 | 13:8b706583610a | 50 | break; |
vbahl2 | 13:8b706583610a | 51 | |
vbahl2 | 13:8b706583610a | 52 | case 2: |
vbahl2 | 13:8b706583610a | 53 | hash += * ((uint16_t *) data); |
vbahl2 | 13:8b706583610a | 54 | hash ^= hash << 11; |
vbahl2 | 13:8b706583610a | 55 | hash += hash >> 17; |
vbahl2 | 13:8b706583610a | 56 | break; |
vbahl2 | 13:8b706583610a | 57 | |
vbahl2 | 13:8b706583610a | 58 | case 1: |
vbahl2 | 13:8b706583610a | 59 | hash += (signed char) * data; |
vbahl2 | 13:8b706583610a | 60 | hash ^= hash << 10; |
vbahl2 | 13:8b706583610a | 61 | hash += hash >> 1; |
vbahl2 | 13:8b706583610a | 62 | } |
vbahl2 | 13:8b706583610a | 63 | |
vbahl2 | 13:8b706583610a | 64 | /* Force "avalanching" of final 127 bits */ |
vbahl2 | 13:8b706583610a | 65 | hash ^= hash << 3; |
vbahl2 | 13:8b706583610a | 66 | hash += hash >> 5; |
vbahl2 | 13:8b706583610a | 67 | hash ^= hash << 4; |
vbahl2 | 13:8b706583610a | 68 | hash += hash >> 17; |
vbahl2 | 13:8b706583610a | 69 | hash ^= hash << 25; |
vbahl2 | 13:8b706583610a | 70 | hash += hash >> 6; |
vbahl2 | 13:8b706583610a | 71 | |
vbahl2 | 13:8b706583610a | 72 | return hash; |
vbahl2 | 13:8b706583610a | 73 | } |