RadioShuttle Lib for the STM32 L4 Heltec Board
Dependents: Turtle_RadioShuttle
util/sha256.c@13:591254bed18b, 2019-04-14 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Sun Apr 14 18:35:26 2019 +0200
- Revision:
- 13:591254bed18b
- Parent:
- 0:0c31756924a2
Updated RadioStatus to be in common with mbed and Arduino
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 0:0c31756924a2 | 1 | /********************************************************************* |
Helmut64 | 0:0c31756924a2 | 2 | * Filename: sha256.c |
Helmut64 | 0:0c31756924a2 | 3 | * Author: Brad Conte (brad AT bradconte.com) |
Helmut64 | 0:0c31756924a2 | 4 | * Copyright: |
Helmut64 | 0:0c31756924a2 | 5 | * Disclaimer: This code is presented "as is" without any guarantees. |
Helmut64 | 0:0c31756924a2 | 6 | * Details: Implementation of the SHA-256 hashing algorithm. |
Helmut64 | 0:0c31756924a2 | 7 | SHA-256 is one of the three algorithms in the SHA2 |
Helmut64 | 0:0c31756924a2 | 8 | specification. The others, SHA-384 and SHA-512, are not |
Helmut64 | 0:0c31756924a2 | 9 | offered in this implementation. |
Helmut64 | 0:0c31756924a2 | 10 | Algorithm specification can be found here: |
Helmut64 | 0:0c31756924a2 | 11 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf |
Helmut64 | 0:0c31756924a2 | 12 | This implementation uses little endian byte order. |
Helmut64 | 0:0c31756924a2 | 13 | *********************************************************************/ |
Helmut64 | 0:0c31756924a2 | 14 | |
Helmut64 | 0:0c31756924a2 | 15 | /*************************** HEADER FILES ***************************/ |
Helmut64 | 0:0c31756924a2 | 16 | #include <stdlib.h> |
Helmut64 | 0:0c31756924a2 | 17 | #include <string.h> |
Helmut64 | 0:0c31756924a2 | 18 | #include "sha256.h" |
Helmut64 | 0:0c31756924a2 | 19 | |
Helmut64 | 0:0c31756924a2 | 20 | /****************************** MACROS ******************************/ |
Helmut64 | 0:0c31756924a2 | 21 | #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) |
Helmut64 | 0:0c31756924a2 | 22 | #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) |
Helmut64 | 0:0c31756924a2 | 23 | |
Helmut64 | 0:0c31756924a2 | 24 | #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) |
Helmut64 | 0:0c31756924a2 | 25 | #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
Helmut64 | 0:0c31756924a2 | 26 | #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) |
Helmut64 | 0:0c31756924a2 | 27 | #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) |
Helmut64 | 0:0c31756924a2 | 28 | #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) |
Helmut64 | 0:0c31756924a2 | 29 | #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) |
Helmut64 | 0:0c31756924a2 | 30 | |
Helmut64 | 0:0c31756924a2 | 31 | /**************************** VARIABLES *****************************/ |
Helmut64 | 0:0c31756924a2 | 32 | static const WORD k[64] = { |
Helmut64 | 0:0c31756924a2 | 33 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, |
Helmut64 | 0:0c31756924a2 | 34 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, |
Helmut64 | 0:0c31756924a2 | 35 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, |
Helmut64 | 0:0c31756924a2 | 36 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, |
Helmut64 | 0:0c31756924a2 | 37 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, |
Helmut64 | 0:0c31756924a2 | 38 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, |
Helmut64 | 0:0c31756924a2 | 39 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, |
Helmut64 | 0:0c31756924a2 | 40 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
Helmut64 | 0:0c31756924a2 | 41 | }; |
Helmut64 | 0:0c31756924a2 | 42 | |
Helmut64 | 0:0c31756924a2 | 43 | /*********************** FUNCTION DEFINITIONS ***********************/ |
Helmut64 | 0:0c31756924a2 | 44 | void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) |
Helmut64 | 0:0c31756924a2 | 45 | { |
Helmut64 | 0:0c31756924a2 | 46 | WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; |
Helmut64 | 0:0c31756924a2 | 47 | |
Helmut64 | 0:0c31756924a2 | 48 | for (i = 0, j = 0; i < 16; ++i, j += 4) |
Helmut64 | 0:0c31756924a2 | 49 | m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); |
Helmut64 | 0:0c31756924a2 | 50 | for ( ; i < 64; ++i) |
Helmut64 | 0:0c31756924a2 | 51 | m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; |
Helmut64 | 0:0c31756924a2 | 52 | |
Helmut64 | 0:0c31756924a2 | 53 | a = ctx->state[0]; |
Helmut64 | 0:0c31756924a2 | 54 | b = ctx->state[1]; |
Helmut64 | 0:0c31756924a2 | 55 | c = ctx->state[2]; |
Helmut64 | 0:0c31756924a2 | 56 | d = ctx->state[3]; |
Helmut64 | 0:0c31756924a2 | 57 | e = ctx->state[4]; |
Helmut64 | 0:0c31756924a2 | 58 | f = ctx->state[5]; |
Helmut64 | 0:0c31756924a2 | 59 | g = ctx->state[6]; |
Helmut64 | 0:0c31756924a2 | 60 | h = ctx->state[7]; |
Helmut64 | 0:0c31756924a2 | 61 | |
Helmut64 | 0:0c31756924a2 | 62 | for (i = 0; i < 64; ++i) { |
Helmut64 | 0:0c31756924a2 | 63 | t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; |
Helmut64 | 0:0c31756924a2 | 64 | t2 = EP0(a) + MAJ(a,b,c); |
Helmut64 | 0:0c31756924a2 | 65 | h = g; |
Helmut64 | 0:0c31756924a2 | 66 | g = f; |
Helmut64 | 0:0c31756924a2 | 67 | f = e; |
Helmut64 | 0:0c31756924a2 | 68 | e = d + t1; |
Helmut64 | 0:0c31756924a2 | 69 | d = c; |
Helmut64 | 0:0c31756924a2 | 70 | c = b; |
Helmut64 | 0:0c31756924a2 | 71 | b = a; |
Helmut64 | 0:0c31756924a2 | 72 | a = t1 + t2; |
Helmut64 | 0:0c31756924a2 | 73 | } |
Helmut64 | 0:0c31756924a2 | 74 | |
Helmut64 | 0:0c31756924a2 | 75 | ctx->state[0] += a; |
Helmut64 | 0:0c31756924a2 | 76 | ctx->state[1] += b; |
Helmut64 | 0:0c31756924a2 | 77 | ctx->state[2] += c; |
Helmut64 | 0:0c31756924a2 | 78 | ctx->state[3] += d; |
Helmut64 | 0:0c31756924a2 | 79 | ctx->state[4] += e; |
Helmut64 | 0:0c31756924a2 | 80 | ctx->state[5] += f; |
Helmut64 | 0:0c31756924a2 | 81 | ctx->state[6] += g; |
Helmut64 | 0:0c31756924a2 | 82 | ctx->state[7] += h; |
Helmut64 | 0:0c31756924a2 | 83 | } |
Helmut64 | 0:0c31756924a2 | 84 | |
Helmut64 | 0:0c31756924a2 | 85 | void sha256_init(SHA256_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 86 | { |
Helmut64 | 0:0c31756924a2 | 87 | ctx->datalen = 0; |
Helmut64 | 0:0c31756924a2 | 88 | ctx->bitlen = 0; |
Helmut64 | 0:0c31756924a2 | 89 | ctx->state[0] = 0x6a09e667; |
Helmut64 | 0:0c31756924a2 | 90 | ctx->state[1] = 0xbb67ae85; |
Helmut64 | 0:0c31756924a2 | 91 | ctx->state[2] = 0x3c6ef372; |
Helmut64 | 0:0c31756924a2 | 92 | ctx->state[3] = 0xa54ff53a; |
Helmut64 | 0:0c31756924a2 | 93 | ctx->state[4] = 0x510e527f; |
Helmut64 | 0:0c31756924a2 | 94 | ctx->state[5] = 0x9b05688c; |
Helmut64 | 0:0c31756924a2 | 95 | ctx->state[6] = 0x1f83d9ab; |
Helmut64 | 0:0c31756924a2 | 96 | ctx->state[7] = 0x5be0cd19; |
Helmut64 | 0:0c31756924a2 | 97 | } |
Helmut64 | 0:0c31756924a2 | 98 | |
Helmut64 | 0:0c31756924a2 | 99 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len) |
Helmut64 | 0:0c31756924a2 | 100 | { |
Helmut64 | 0:0c31756924a2 | 101 | WORD i; |
Helmut64 | 0:0c31756924a2 | 102 | |
Helmut64 | 0:0c31756924a2 | 103 | for (i = 0; i < len; ++i) { |
Helmut64 | 0:0c31756924a2 | 104 | ctx->data[ctx->datalen] = data[i]; |
Helmut64 | 0:0c31756924a2 | 105 | ctx->datalen++; |
Helmut64 | 0:0c31756924a2 | 106 | if (ctx->datalen == 64) { |
Helmut64 | 0:0c31756924a2 | 107 | sha256_transform(ctx, ctx->data); |
Helmut64 | 0:0c31756924a2 | 108 | ctx->bitlen += 512; |
Helmut64 | 0:0c31756924a2 | 109 | ctx->datalen = 0; |
Helmut64 | 0:0c31756924a2 | 110 | } |
Helmut64 | 0:0c31756924a2 | 111 | } |
Helmut64 | 0:0c31756924a2 | 112 | } |
Helmut64 | 0:0c31756924a2 | 113 | |
Helmut64 | 0:0c31756924a2 | 114 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]) |
Helmut64 | 0:0c31756924a2 | 115 | { |
Helmut64 | 0:0c31756924a2 | 116 | WORD i; |
Helmut64 | 0:0c31756924a2 | 117 | |
Helmut64 | 0:0c31756924a2 | 118 | i = ctx->datalen; |
Helmut64 | 0:0c31756924a2 | 119 | |
Helmut64 | 0:0c31756924a2 | 120 | // Pad whatever data is left in the buffer. |
Helmut64 | 0:0c31756924a2 | 121 | if (ctx->datalen < 56) { |
Helmut64 | 0:0c31756924a2 | 122 | ctx->data[i++] = 0x80; |
Helmut64 | 0:0c31756924a2 | 123 | while (i < 56) |
Helmut64 | 0:0c31756924a2 | 124 | ctx->data[i++] = 0x00; |
Helmut64 | 0:0c31756924a2 | 125 | } |
Helmut64 | 0:0c31756924a2 | 126 | else { |
Helmut64 | 0:0c31756924a2 | 127 | ctx->data[i++] = 0x80; |
Helmut64 | 0:0c31756924a2 | 128 | while (i < 64) |
Helmut64 | 0:0c31756924a2 | 129 | ctx->data[i++] = 0x00; |
Helmut64 | 0:0c31756924a2 | 130 | sha256_transform(ctx, ctx->data); |
Helmut64 | 0:0c31756924a2 | 131 | memset(ctx->data, 0, 56); |
Helmut64 | 0:0c31756924a2 | 132 | } |
Helmut64 | 0:0c31756924a2 | 133 | |
Helmut64 | 0:0c31756924a2 | 134 | // Append to the padding the total message's length in bits and transform. |
Helmut64 | 0:0c31756924a2 | 135 | ctx->bitlen += ctx->datalen * 8; |
Helmut64 | 0:0c31756924a2 | 136 | ctx->data[63] = ctx->bitlen; |
Helmut64 | 0:0c31756924a2 | 137 | ctx->data[62] = ctx->bitlen >> 8; |
Helmut64 | 0:0c31756924a2 | 138 | ctx->data[61] = ctx->bitlen >> 16; |
Helmut64 | 0:0c31756924a2 | 139 | ctx->data[60] = ctx->bitlen >> 24; |
Helmut64 | 0:0c31756924a2 | 140 | ctx->data[59] = ctx->bitlen >> 32; |
Helmut64 | 0:0c31756924a2 | 141 | ctx->data[58] = ctx->bitlen >> 40; |
Helmut64 | 0:0c31756924a2 | 142 | ctx->data[57] = ctx->bitlen >> 48; |
Helmut64 | 0:0c31756924a2 | 143 | ctx->data[56] = ctx->bitlen >> 56; |
Helmut64 | 0:0c31756924a2 | 144 | sha256_transform(ctx, ctx->data); |
Helmut64 | 0:0c31756924a2 | 145 | |
Helmut64 | 0:0c31756924a2 | 146 | // Since this implementation uses little endian byte ordering and SHA uses big endian, |
Helmut64 | 0:0c31756924a2 | 147 | // reverse all the bytes when copying the final state to the output hash. |
Helmut64 | 0:0c31756924a2 | 148 | for (i = 0; i < 4; ++i) { |
Helmut64 | 0:0c31756924a2 | 149 | hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 150 | hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 151 | hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 152 | hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 153 | hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 154 | hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 155 | hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 156 | hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; |
Helmut64 | 0:0c31756924a2 | 157 | } |
Helmut64 | 0:0c31756924a2 | 158 | } |