Exportable version of WizziLab's modem driver.
src/kal_crypto.cpp@67:e458db8402dc, 2021-10-29 (annotated)
- Committer:
- marin_wizzi
- Date:
- Fri Oct 29 13:54:43 2021 +0000
- Revision:
- 67:e458db8402dc
- Parent:
- 56:67e3d9608403
had to be commited for Scanner test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jeej | 13:10589aaf8853 | 1 | /// @copyright |
Jeej | 13:10589aaf8853 | 2 | /// ========================================================================={{{ |
Jeej | 13:10589aaf8853 | 3 | /// Copyright (c) 2017 WizziLab / |
Jeej | 13:10589aaf8853 | 4 | /// All rights reserved / |
Jeej | 13:10589aaf8853 | 5 | /// / |
Jeej | 13:10589aaf8853 | 6 | /// IMPORTANT: This Software may not be modified, copied or distributed unless / |
Jeej | 13:10589aaf8853 | 7 | /// embedded on a WizziLab product. Other than for the foregoing purpose, this / |
Jeej | 13:10589aaf8853 | 8 | /// Software and/or its documentation may not be used, reproduced, copied, / |
Jeej | 13:10589aaf8853 | 9 | /// prepared derivative works of, modified, performed, distributed, displayed / |
Jeej | 13:10589aaf8853 | 10 | /// or sold for any purpose. For the sole purpose of embedding this Software / |
Jeej | 13:10589aaf8853 | 11 | /// on a WizziLab product, copy, modification and distribution of this / |
Jeej | 13:10589aaf8853 | 12 | /// Software is granted provided that the following conditions are respected: / |
Jeej | 13:10589aaf8853 | 13 | /// / |
Jeej | 13:10589aaf8853 | 14 | /// * Redistributions of source code must retain the above copyright notice, / |
Jeej | 13:10589aaf8853 | 15 | /// this list of conditions and the following disclaimer / |
Jeej | 13:10589aaf8853 | 16 | /// / |
Jeej | 13:10589aaf8853 | 17 | /// * Redistributions in binary form must reproduce the above copyright / |
Jeej | 13:10589aaf8853 | 18 | /// notice, this list of conditions and the following disclaimer in the / |
Jeej | 13:10589aaf8853 | 19 | /// documentation and/or other materials provided with the distribution. / |
Jeej | 13:10589aaf8853 | 20 | /// / |
Jeej | 13:10589aaf8853 | 21 | /// * The name of WizziLab can not be used to endorse or promote products / |
Jeej | 13:10589aaf8853 | 22 | /// derived from this software without specific prior written permission. / |
Jeej | 13:10589aaf8853 | 23 | /// / |
Jeej | 13:10589aaf8853 | 24 | /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS / |
Jeej | 13:10589aaf8853 | 25 | /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED / |
Jeej | 13:10589aaf8853 | 26 | /// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR / |
Jeej | 13:10589aaf8853 | 27 | /// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR / |
Jeej | 13:10589aaf8853 | 28 | /// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, / |
Jeej | 13:10589aaf8853 | 29 | /// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, / |
Jeej | 13:10589aaf8853 | 30 | /// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, / |
Jeej | 13:10589aaf8853 | 31 | /// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY / |
Jeej | 13:10589aaf8853 | 32 | /// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING / |
Jeej | 13:10589aaf8853 | 33 | /// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS / |
Jeej | 13:10589aaf8853 | 34 | /// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. / |
Jeej | 13:10589aaf8853 | 35 | /// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, / |
Jeej | 13:10589aaf8853 | 36 | /// ENHANCEMENTS OR MODIFICATIONS. / |
Jeej | 13:10589aaf8853 | 37 | /// / |
Jeej | 13:10589aaf8853 | 38 | /// Should you have any questions regarding your right to use this Software, / |
Jeej | 13:10589aaf8853 | 39 | /// contact WizziLab at www.wizzilab.com. / |
Jeej | 13:10589aaf8853 | 40 | /// / |
Jeej | 13:10589aaf8853 | 41 | /// =========================================================================}}} |
Jeej | 13:10589aaf8853 | 42 | /// @endcopyright |
Jeej | 13:10589aaf8853 | 43 | /// |
Jeej | 13:10589aaf8853 | 44 | /// ======================================================================= |
Jeej | 13:10589aaf8853 | 45 | /// |
Jeej | 13:10589aaf8853 | 46 | /// @file kal_crypto.c |
Jeej | 13:10589aaf8853 | 47 | /// @brief Crypto Utilities |
Jeej | 13:10589aaf8853 | 48 | /// |
Jeej | 13:10589aaf8853 | 49 | /// ======================================================================= |
Jeej | 13:10589aaf8853 | 50 | |
Jeej | 13:10589aaf8853 | 51 | #include "WizziDebug.h" |
Jeej | 13:10589aaf8853 | 52 | #include "kal_crypto.h" |
Jeej | 13:10589aaf8853 | 53 | |
Jeej | 13:10589aaf8853 | 54 | // ====================================================================== |
Jeej | 13:10589aaf8853 | 55 | // |
Jeej | 13:10589aaf8853 | 56 | // |
Jeej | 13:10589aaf8853 | 57 | // SHA-2 256 Tool-suite. |
Jeej | 13:10589aaf8853 | 58 | // (From Brad Conte's Licence-free implementation) |
Jeej | 13:10589aaf8853 | 59 | // |
Jeej | 13:10589aaf8853 | 60 | // |
Jeej | 13:10589aaf8853 | 61 | // ====================================================================== |
Jeej | 13:10589aaf8853 | 62 | |
Jeej | 13:10589aaf8853 | 63 | // DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it |
Jeej | 13:10589aaf8853 | 64 | #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c; |
Jeej | 13:10589aaf8853 | 65 | #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) |
Jeej | 13:10589aaf8853 | 66 | #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) |
Jeej | 13:10589aaf8853 | 67 | |
Jeej | 13:10589aaf8853 | 68 | #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) |
Jeej | 13:10589aaf8853 | 69 | #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
Jeej | 13:10589aaf8853 | 70 | #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) |
Jeej | 13:10589aaf8853 | 71 | #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) |
Jeej | 13:10589aaf8853 | 72 | #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) |
Jeej | 13:10589aaf8853 | 73 | #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) |
Jeej | 13:10589aaf8853 | 74 | |
Jeej | 13:10589aaf8853 | 75 | // ======================================================================= |
Jeej | 13:10589aaf8853 | 76 | // kal_sha256_ctx_t |
Jeej | 13:10589aaf8853 | 77 | // ----------------------------------------------------------------------- |
Jeej | 13:10589aaf8853 | 78 | /// SHA256 context |
Jeej | 13:10589aaf8853 | 79 | // ======================================================================= |
Jeej | 13:10589aaf8853 | 80 | typedef struct |
Jeej | 13:10589aaf8853 | 81 | { |
Jeej | 13:10589aaf8853 | 82 | u8 data[64]; |
Jeej | 13:10589aaf8853 | 83 | uint datalen; |
Jeej | 13:10589aaf8853 | 84 | uint bitlen[2]; |
Jeej | 13:10589aaf8853 | 85 | uint state[8]; |
Jeej | 13:10589aaf8853 | 86 | |
Jeej | 13:10589aaf8853 | 87 | } kal_sha256_ctx_t; |
Jeej | 13:10589aaf8853 | 88 | |
Jeej | 13:10589aaf8853 | 89 | kal_sha256_ctx_t* g_kal_sha_ctx = (kal_sha256_ctx_t*)NULL; |
Jeej | 13:10589aaf8853 | 90 | |
Jeej | 13:10589aaf8853 | 91 | //====================================================================== |
Jeej | 13:10589aaf8853 | 92 | // k_kal_sha |
Jeej | 13:10589aaf8853 | 93 | //---------------------------------------------------------------------- |
Jeej | 13:10589aaf8853 | 94 | /// @brief Unique constant table used for SHA256 |
Jeej | 13:10589aaf8853 | 95 | //====================================================================== |
Jeej | 13:10589aaf8853 | 96 | const uint k_kal_sha[64] = |
Jeej | 13:10589aaf8853 | 97 | { |
Jeej | 13:10589aaf8853 | 98 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, |
Jeej | 13:10589aaf8853 | 99 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, |
Jeej | 13:10589aaf8853 | 100 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, |
Jeej | 13:10589aaf8853 | 101 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, |
Jeej | 13:10589aaf8853 | 102 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, |
Jeej | 13:10589aaf8853 | 103 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, |
Jeej | 13:10589aaf8853 | 104 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, |
Jeej | 13:10589aaf8853 | 105 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
Jeej | 13:10589aaf8853 | 106 | }; |
Jeej | 13:10589aaf8853 | 107 | |
Jeej | 13:10589aaf8853 | 108 | //====================================================================== |
Jeej | 13:10589aaf8853 | 109 | // kal_sha256_init |
Jeej | 13:10589aaf8853 | 110 | //---------------------------------------------------------------------- |
Jeej | 13:10589aaf8853 | 111 | /// @brief Initialize an SHA Hash generation. To be called before any |
Jeej | 13:10589aaf8853 | 112 | /// other kal_sha256_update/final functions. |
Jeej | 13:10589aaf8853 | 113 | //====================================================================== |
Jeej | 56:67e3d9608403 | 114 | void kal_sha256_init(void) |
Jeej | 13:10589aaf8853 | 115 | { |
Jeej | 13:10589aaf8853 | 116 | // Garbage collection |
Jeej | 13:10589aaf8853 | 117 | if (g_kal_sha_ctx) FREE(g_kal_sha_ctx); |
Jeej | 13:10589aaf8853 | 118 | g_kal_sha_ctx = (kal_sha256_ctx_t*) MALLOC(sizeof(kal_sha256_ctx_t)); |
Jeej | 13:10589aaf8853 | 119 | g_kal_sha_ctx->datalen = 0; |
Jeej | 13:10589aaf8853 | 120 | g_kal_sha_ctx->bitlen[0] = 0; |
Jeej | 13:10589aaf8853 | 121 | g_kal_sha_ctx->bitlen[1] = 0; |
Jeej | 13:10589aaf8853 | 122 | g_kal_sha_ctx->state[0] = 0x6a09e667; |
Jeej | 13:10589aaf8853 | 123 | g_kal_sha_ctx->state[1] = 0xbb67ae85; |
Jeej | 13:10589aaf8853 | 124 | g_kal_sha_ctx->state[2] = 0x3c6ef372; |
Jeej | 13:10589aaf8853 | 125 | g_kal_sha_ctx->state[3] = 0xa54ff53a; |
Jeej | 13:10589aaf8853 | 126 | g_kal_sha_ctx->state[4] = 0x510e527f; |
Jeej | 13:10589aaf8853 | 127 | g_kal_sha_ctx->state[5] = 0x9b05688c; |
Jeej | 13:10589aaf8853 | 128 | g_kal_sha_ctx->state[6] = 0x1f83d9ab; |
Jeej | 13:10589aaf8853 | 129 | g_kal_sha_ctx->state[7] = 0x5be0cd19; |
Jeej | 13:10589aaf8853 | 130 | } |
Jeej | 13:10589aaf8853 | 131 | |
Jeej | 13:10589aaf8853 | 132 | //====================================================================== |
Jeej | 13:10589aaf8853 | 133 | // kal_sha256_transform |
Jeej | 13:10589aaf8853 | 134 | //---------------------------------------------------------------------- |
Jeej | 13:10589aaf8853 | 135 | /// @brief SHA core hashing function. |
Jeej | 13:10589aaf8853 | 136 | /// @param data stream (awaits 64 valid bytes) |
Jeej | 13:10589aaf8853 | 137 | //====================================================================== |
Jeej | 56:67e3d9608403 | 138 | static void kal_sha256_transform(u8 data[]) |
Jeej | 13:10589aaf8853 | 139 | { |
Jeej | 13:10589aaf8853 | 140 | uint a,b,c,d,e,f,g,h,i,j,t1,t2,m[64]; |
Jeej | 13:10589aaf8853 | 141 | |
Jeej | 13:10589aaf8853 | 142 | for (i=0,j=0; i < 16; ++i, j += 4) |
Jeej | 13:10589aaf8853 | 143 | m[i] = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) | (data[j+3]); |
Jeej | 13:10589aaf8853 | 144 | for ( ; i < 64; ++i) |
Jeej | 13:10589aaf8853 | 145 | m[i] = SIG1(m[i-2]) + m[i-7] + SIG0(m[i-15]) + m[i-16]; |
Jeej | 13:10589aaf8853 | 146 | |
Jeej | 13:10589aaf8853 | 147 | a = g_kal_sha_ctx->state[0]; |
Jeej | 13:10589aaf8853 | 148 | b = g_kal_sha_ctx->state[1]; |
Jeej | 13:10589aaf8853 | 149 | c = g_kal_sha_ctx->state[2]; |
Jeej | 13:10589aaf8853 | 150 | d = g_kal_sha_ctx->state[3]; |
Jeej | 13:10589aaf8853 | 151 | e = g_kal_sha_ctx->state[4]; |
Jeej | 13:10589aaf8853 | 152 | f = g_kal_sha_ctx->state[5]; |
Jeej | 13:10589aaf8853 | 153 | g = g_kal_sha_ctx->state[6]; |
Jeej | 13:10589aaf8853 | 154 | h = g_kal_sha_ctx->state[7]; |
Jeej | 13:10589aaf8853 | 155 | |
Jeej | 13:10589aaf8853 | 156 | for (i = 0; i < 64; ++i) { |
Jeej | 13:10589aaf8853 | 157 | t1 = h + EP1(e) + CH(e,f,g) + k_kal_sha[i] + m[i]; |
Jeej | 13:10589aaf8853 | 158 | t2 = EP0(a) + MAJ(a,b,c); |
Jeej | 13:10589aaf8853 | 159 | h = g; |
Jeej | 13:10589aaf8853 | 160 | g = f; |
Jeej | 13:10589aaf8853 | 161 | f = e; |
Jeej | 13:10589aaf8853 | 162 | e = d + t1; |
Jeej | 13:10589aaf8853 | 163 | d = c; |
Jeej | 13:10589aaf8853 | 164 | c = b; |
Jeej | 13:10589aaf8853 | 165 | b = a; |
Jeej | 13:10589aaf8853 | 166 | a = t1 + t2; |
Jeej | 13:10589aaf8853 | 167 | } |
Jeej | 13:10589aaf8853 | 168 | |
Jeej | 13:10589aaf8853 | 169 | g_kal_sha_ctx->state[0] += a; |
Jeej | 13:10589aaf8853 | 170 | g_kal_sha_ctx->state[1] += b; |
Jeej | 13:10589aaf8853 | 171 | g_kal_sha_ctx->state[2] += c; |
Jeej | 13:10589aaf8853 | 172 | g_kal_sha_ctx->state[3] += d; |
Jeej | 13:10589aaf8853 | 173 | g_kal_sha_ctx->state[4] += e; |
Jeej | 13:10589aaf8853 | 174 | g_kal_sha_ctx->state[5] += f; |
Jeej | 13:10589aaf8853 | 175 | g_kal_sha_ctx->state[6] += g; |
Jeej | 13:10589aaf8853 | 176 | g_kal_sha_ctx->state[7] += h; |
Jeej | 13:10589aaf8853 | 177 | } |
Jeej | 13:10589aaf8853 | 178 | |
Jeej | 13:10589aaf8853 | 179 | //====================================================================== |
Jeej | 13:10589aaf8853 | 180 | // kal_sha256_update |
Jeej | 13:10589aaf8853 | 181 | //---------------------------------------------------------------------- |
Jeej | 13:10589aaf8853 | 182 | /// @brief Used to 'push' new data into the hash calculation. |
Jeej | 13:10589aaf8853 | 183 | /// @param data pointer to the (char) data stream to hash. |
Jeej | 13:10589aaf8853 | 184 | /// @param len data stream length in bytes |
Jeej | 13:10589aaf8853 | 185 | //====================================================================== |
Jeej | 56:67e3d9608403 | 186 | void kal_sha256_update(u8 data[], uint len) |
Jeej | 13:10589aaf8853 | 187 | { |
Jeej | 13:10589aaf8853 | 188 | uint i; |
Jeej | 13:10589aaf8853 | 189 | |
Jeej | 13:10589aaf8853 | 190 | for (i=0; i < len; ++i) { |
Jeej | 13:10589aaf8853 | 191 | g_kal_sha_ctx->data[g_kal_sha_ctx->datalen] = data[i]; |
Jeej | 13:10589aaf8853 | 192 | g_kal_sha_ctx->datalen++; |
Jeej | 13:10589aaf8853 | 193 | if (g_kal_sha_ctx->datalen == 64) { |
Jeej | 13:10589aaf8853 | 194 | kal_sha256_transform(g_kal_sha_ctx->data); |
Jeej | 13:10589aaf8853 | 195 | DBL_INT_ADD(g_kal_sha_ctx->bitlen[0],g_kal_sha_ctx->bitlen[1],512); |
Jeej | 13:10589aaf8853 | 196 | g_kal_sha_ctx->datalen = 0; |
Jeej | 13:10589aaf8853 | 197 | } |
Jeej | 13:10589aaf8853 | 198 | } |
Jeej | 13:10589aaf8853 | 199 | } |
Jeej | 13:10589aaf8853 | 200 | |
Jeej | 13:10589aaf8853 | 201 | //====================================================================== |
Jeej | 13:10589aaf8853 | 202 | // kal_sha256_final |
Jeej | 13:10589aaf8853 | 203 | //---------------------------------------------------------------------- |
Jeej | 13:10589aaf8853 | 204 | /// @brief To be called when all data has been pushed into the hash |
Jeej | 13:10589aaf8853 | 205 | /// generator. Finalize and outputs resulting SHA hash. |
Jeej | 13:10589aaf8853 | 206 | /// @param hash Pointer to the Output (char) buffer. Fills 32-bytes. |
Jeej | 13:10589aaf8853 | 207 | //====================================================================== |
Jeej | 56:67e3d9608403 | 208 | void kal_sha256_final(u8 hash[]) |
Jeej | 13:10589aaf8853 | 209 | { |
Jeej | 13:10589aaf8853 | 210 | uint i; |
Jeej | 13:10589aaf8853 | 211 | |
Jeej | 13:10589aaf8853 | 212 | i = g_kal_sha_ctx->datalen; |
Jeej | 13:10589aaf8853 | 213 | |
Jeej | 13:10589aaf8853 | 214 | // Pad whatever data is left in the buffer. |
Jeej | 13:10589aaf8853 | 215 | if (g_kal_sha_ctx->datalen < 56) { |
Jeej | 13:10589aaf8853 | 216 | g_kal_sha_ctx->data[i++] = 0x80; |
Jeej | 13:10589aaf8853 | 217 | while (i < 56) |
Jeej | 13:10589aaf8853 | 218 | g_kal_sha_ctx->data[i++] = 0x00; |
Jeej | 13:10589aaf8853 | 219 | } |
Jeej | 13:10589aaf8853 | 220 | else { |
Jeej | 13:10589aaf8853 | 221 | g_kal_sha_ctx->data[i++] = 0x80; |
Jeej | 13:10589aaf8853 | 222 | while (i < 64) |
Jeej | 13:10589aaf8853 | 223 | g_kal_sha_ctx->data[i++] = 0x00; |
Jeej | 13:10589aaf8853 | 224 | kal_sha256_transform(g_kal_sha_ctx->data); |
Jeej | 13:10589aaf8853 | 225 | memset(g_kal_sha_ctx->data,0,56); |
Jeej | 13:10589aaf8853 | 226 | } |
Jeej | 13:10589aaf8853 | 227 | |
Jeej | 13:10589aaf8853 | 228 | // Append to the padding the total message's length in bits and transform. |
Jeej | 13:10589aaf8853 | 229 | DBL_INT_ADD(g_kal_sha_ctx->bitlen[0],g_kal_sha_ctx->bitlen[1],g_kal_sha_ctx->datalen * 8); |
Jeej | 13:10589aaf8853 | 230 | g_kal_sha_ctx->data[63] = g_kal_sha_ctx->bitlen[0]; |
Jeej | 13:10589aaf8853 | 231 | g_kal_sha_ctx->data[62] = g_kal_sha_ctx->bitlen[0] >> 8; |
Jeej | 13:10589aaf8853 | 232 | g_kal_sha_ctx->data[61] = g_kal_sha_ctx->bitlen[0] >> 16; |
Jeej | 13:10589aaf8853 | 233 | g_kal_sha_ctx->data[60] = g_kal_sha_ctx->bitlen[0] >> 24; |
Jeej | 13:10589aaf8853 | 234 | g_kal_sha_ctx->data[59] = g_kal_sha_ctx->bitlen[1]; |
Jeej | 13:10589aaf8853 | 235 | g_kal_sha_ctx->data[58] = g_kal_sha_ctx->bitlen[1] >> 8; |
Jeej | 13:10589aaf8853 | 236 | g_kal_sha_ctx->data[57] = g_kal_sha_ctx->bitlen[1] >> 16; |
Jeej | 13:10589aaf8853 | 237 | g_kal_sha_ctx->data[56] = g_kal_sha_ctx->bitlen[1] >> 24; |
Jeej | 13:10589aaf8853 | 238 | kal_sha256_transform(g_kal_sha_ctx->data); |
Jeej | 13:10589aaf8853 | 239 | |
Jeej | 13:10589aaf8853 | 240 | // Since this implementation uses little endian byte ordering and SHA uses big endian, |
Jeej | 13:10589aaf8853 | 241 | // reverse all the bytes when copying the final state to the output hash. |
Jeej | 13:10589aaf8853 | 242 | for (i=0; i < 4; ++i) { |
Jeej | 13:10589aaf8853 | 243 | hash[i] = (g_kal_sha_ctx->state[0] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 244 | hash[i+4] = (g_kal_sha_ctx->state[1] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 245 | hash[i+8] = (g_kal_sha_ctx->state[2] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 246 | hash[i+12] = (g_kal_sha_ctx->state[3] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 247 | hash[i+16] = (g_kal_sha_ctx->state[4] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 248 | hash[i+20] = (g_kal_sha_ctx->state[5] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 249 | hash[i+24] = (g_kal_sha_ctx->state[6] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 250 | hash[i+28] = (g_kal_sha_ctx->state[7] >> (24-i*8)) & 0x000000ff; |
Jeej | 13:10589aaf8853 | 251 | } |
Jeej | 13:10589aaf8853 | 252 | FREE(g_kal_sha_ctx); |
Jeej | 13:10589aaf8853 | 253 | g_kal_sha_ctx = (kal_sha256_ctx_t*)NULL; |
Jeej | 13:10589aaf8853 | 254 | } |