WizziLab / modem_ref

Dependents:   modem_ref_helper

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers kal_crypto.cpp Source File

kal_crypto.cpp

00001 /// @copyright
00002 /// ========================================================================={{{
00003 /// Copyright (c) 2017 WizziLab                                                /
00004 /// All rights reserved                                                        /
00005 ///                                                                            /
00006 /// IMPORTANT: This Software may not be modified, copied or distributed unless /
00007 /// embedded on a WizziLab product. Other than for the foregoing purpose, this /
00008 /// Software and/or its documentation may not be used, reproduced, copied,     /
00009 /// prepared derivative works of, modified, performed, distributed, displayed  /
00010 /// or sold for any purpose. For the sole purpose of embedding this Software   /
00011 /// on a WizziLab product, copy, modification and distribution of this         /
00012 /// Software is granted provided that the following conditions are respected:  /
00013 ///                                                                            /
00014 /// *  Redistributions of source code must retain the above copyright notice,  /
00015 ///    this list of conditions and the following disclaimer                    /
00016 ///                                                                            /
00017 /// *  Redistributions in binary form must reproduce the above copyright       /
00018 ///    notice, this list of conditions and the following disclaimer in the     /
00019 ///    documentation and/or other materials provided with the distribution.    /
00020 ///                                                                            /
00021 /// *  The name of WizziLab can not be used to endorse or promote products     /
00022 ///    derived from this software without specific prior written permission.   /
00023 ///                                                                            /
00024 /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS        /
00025 /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  /
00026 /// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR /
00027 /// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR          /
00028 /// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      /
00029 /// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        /
00030 /// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,            /
00031 /// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     /
00032 /// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING    /
00033 /// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS         /
00034 /// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.               /
00035 /// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,       /
00036 /// ENHANCEMENTS OR MODIFICATIONS.                                             /
00037 ///                                                                            /
00038 /// Should you have any questions regarding your right to use this Software,   /
00039 /// contact WizziLab at www.wizzilab.com.                                      /
00040 ///                                                                            /
00041 /// =========================================================================}}}
00042 /// @endcopyright
00043 ///
00044 /// =======================================================================
00045 ///
00046 /// @file           kal_crypto.c
00047 /// @brief          Crypto Utilities
00048 ///
00049 /// =======================================================================
00050 
00051 #include "WizziDebug.h"
00052 #include "kal_crypto.h"
00053 
00054 // ======================================================================
00055 //
00056 //
00057 //                        SHA-2 256 Tool-suite. 
00058 //          (From Brad Conte's Licence-free implementation)
00059 //
00060 //
00061 // ======================================================================
00062 
00063 // DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it
00064 #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c;
00065 #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
00066 #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
00067 
00068 #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
00069 #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
00070 #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
00071 #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
00072 #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
00073 #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
00074 
00075 // =======================================================================
00076 // kal_sha256_ctx_t
00077 // -----------------------------------------------------------------------
00078 /// SHA256 context
00079 // =======================================================================
00080 typedef struct 
00081 {
00082     u8 data[64];
00083     uint datalen;
00084     uint bitlen[2];
00085     uint state[8];
00086 
00087 } kal_sha256_ctx_t;
00088 
00089 kal_sha256_ctx_t* g_kal_sha_ctx = (kal_sha256_ctx_t*)NULL;
00090 
00091 //======================================================================
00092 // k_kal_sha
00093 //----------------------------------------------------------------------
00094 /// @brief Unique constant table used for SHA256 
00095 //======================================================================
00096 const uint k_kal_sha[64] = 
00097 {
00098    0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
00099    0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
00100    0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
00101    0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
00102    0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
00103    0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
00104    0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
00105    0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
00106 };
00107 
00108 //======================================================================
00109 // kal_sha256_init
00110 //----------------------------------------------------------------------
00111 /// @brief Initialize an SHA Hash generation. To be called before any
00112 ///        other kal_sha256_update/final functions.
00113 //======================================================================
00114 void kal_sha256_init(void)
00115 {
00116     // Garbage collection
00117     if (g_kal_sha_ctx) FREE(g_kal_sha_ctx);
00118     g_kal_sha_ctx = (kal_sha256_ctx_t*) MALLOC(sizeof(kal_sha256_ctx_t));
00119     g_kal_sha_ctx->datalen = 0;
00120     g_kal_sha_ctx->bitlen[0] = 0;
00121     g_kal_sha_ctx->bitlen[1] = 0;
00122     g_kal_sha_ctx->state[0] = 0x6a09e667;
00123     g_kal_sha_ctx->state[1] = 0xbb67ae85;
00124     g_kal_sha_ctx->state[2] = 0x3c6ef372;
00125     g_kal_sha_ctx->state[3] = 0xa54ff53a;
00126     g_kal_sha_ctx->state[4] = 0x510e527f;
00127     g_kal_sha_ctx->state[5] = 0x9b05688c;
00128     g_kal_sha_ctx->state[6] = 0x1f83d9ab;
00129     g_kal_sha_ctx->state[7] = 0x5be0cd19;
00130 }
00131 
00132 //======================================================================
00133 // kal_sha256_transform
00134 //----------------------------------------------------------------------
00135 /// @brief SHA core hashing function.
00136 /// @param data stream (awaits 64 valid bytes)
00137 //======================================================================
00138 static void kal_sha256_transform(u8 data[])
00139 {
00140     uint a,b,c,d,e,f,g,h,i,j,t1,t2,m[64];
00141 
00142     for (i=0,j=0; i < 16; ++i, j += 4)
00143         m[i] = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) | (data[j+3]);
00144     for ( ; i < 64; ++i)
00145         m[i] = SIG1(m[i-2]) + m[i-7] + SIG0(m[i-15]) + m[i-16];
00146 
00147     a = g_kal_sha_ctx->state[0];
00148     b = g_kal_sha_ctx->state[1];
00149     c = g_kal_sha_ctx->state[2];
00150     d = g_kal_sha_ctx->state[3];
00151     e = g_kal_sha_ctx->state[4];
00152     f = g_kal_sha_ctx->state[5];
00153     g = g_kal_sha_ctx->state[6];
00154     h = g_kal_sha_ctx->state[7];
00155 
00156     for (i = 0; i < 64; ++i) {
00157         t1 = h + EP1(e) + CH(e,f,g) + k_kal_sha[i] + m[i];
00158         t2 = EP0(a) + MAJ(a,b,c);
00159         h = g;
00160         g = f;
00161         f = e;
00162         e = d + t1;
00163         d = c;
00164         c = b;
00165         b = a;
00166         a = t1 + t2;
00167     }
00168 
00169     g_kal_sha_ctx->state[0] += a;
00170     g_kal_sha_ctx->state[1] += b;
00171     g_kal_sha_ctx->state[2] += c;
00172     g_kal_sha_ctx->state[3] += d;
00173     g_kal_sha_ctx->state[4] += e;
00174     g_kal_sha_ctx->state[5] += f;
00175     g_kal_sha_ctx->state[6] += g;
00176     g_kal_sha_ctx->state[7] += h;
00177 }
00178 
00179 //======================================================================
00180 // kal_sha256_update
00181 //----------------------------------------------------------------------
00182 /// @brief Used to 'push' new data into the hash calculation.
00183 /// @param data pointer to the (char) data stream to hash.
00184 /// @param len data stream length in bytes
00185 //======================================================================
00186 void kal_sha256_update(u8 data[], uint len)
00187 {
00188    uint i;
00189 
00190    for (i=0; i < len; ++i) {
00191       g_kal_sha_ctx->data[g_kal_sha_ctx->datalen] = data[i];
00192       g_kal_sha_ctx->datalen++;
00193       if (g_kal_sha_ctx->datalen == 64) {
00194          kal_sha256_transform(g_kal_sha_ctx->data);
00195          DBL_INT_ADD(g_kal_sha_ctx->bitlen[0],g_kal_sha_ctx->bitlen[1],512);
00196          g_kal_sha_ctx->datalen = 0;
00197       }
00198    }
00199 }
00200 
00201 //======================================================================
00202 // kal_sha256_final
00203 //----------------------------------------------------------------------
00204 /// @brief To be called when all data has been pushed into the hash
00205 ///        generator. Finalize and outputs resulting SHA hash.
00206 /// @param hash Pointer to the Output (char) buffer. Fills 32-bytes.
00207 //======================================================================
00208 void kal_sha256_final(u8 hash[])
00209 {
00210     uint i;
00211 
00212     i = g_kal_sha_ctx->datalen;
00213 
00214     // Pad whatever data is left in the buffer.
00215     if (g_kal_sha_ctx->datalen < 56) {
00216         g_kal_sha_ctx->data[i++] = 0x80;
00217         while (i < 56)
00218             g_kal_sha_ctx->data[i++] = 0x00;
00219     }
00220     else {
00221         g_kal_sha_ctx->data[i++] = 0x80;
00222         while (i < 64)
00223             g_kal_sha_ctx->data[i++] = 0x00;
00224         kal_sha256_transform(g_kal_sha_ctx->data);
00225         memset(g_kal_sha_ctx->data,0,56);
00226     }
00227 
00228     // Append to the padding the total message's length in bits and transform.
00229     DBL_INT_ADD(g_kal_sha_ctx->bitlen[0],g_kal_sha_ctx->bitlen[1],g_kal_sha_ctx->datalen * 8);
00230     g_kal_sha_ctx->data[63] = g_kal_sha_ctx->bitlen[0];
00231     g_kal_sha_ctx->data[62] = g_kal_sha_ctx->bitlen[0] >> 8;
00232     g_kal_sha_ctx->data[61] = g_kal_sha_ctx->bitlen[0] >> 16;
00233     g_kal_sha_ctx->data[60] = g_kal_sha_ctx->bitlen[0] >> 24;
00234     g_kal_sha_ctx->data[59] = g_kal_sha_ctx->bitlen[1];
00235     g_kal_sha_ctx->data[58] = g_kal_sha_ctx->bitlen[1] >> 8;
00236     g_kal_sha_ctx->data[57] = g_kal_sha_ctx->bitlen[1] >> 16;
00237     g_kal_sha_ctx->data[56] = g_kal_sha_ctx->bitlen[1] >> 24;
00238     kal_sha256_transform(g_kal_sha_ctx->data);
00239 
00240     // Since this implementation uses little endian byte ordering and SHA uses big endian,
00241     // reverse all the bytes when copying the final state to the output hash.
00242     for (i=0; i < 4; ++i) {
00243         hash[i]    = (g_kal_sha_ctx->state[0] >> (24-i*8)) & 0x000000ff;
00244         hash[i+4]  = (g_kal_sha_ctx->state[1] >> (24-i*8)) & 0x000000ff;
00245         hash[i+8]  = (g_kal_sha_ctx->state[2] >> (24-i*8)) & 0x000000ff;
00246         hash[i+12] = (g_kal_sha_ctx->state[3] >> (24-i*8)) & 0x000000ff;
00247         hash[i+16] = (g_kal_sha_ctx->state[4] >> (24-i*8)) & 0x000000ff;
00248         hash[i+20] = (g_kal_sha_ctx->state[5] >> (24-i*8)) & 0x000000ff;
00249         hash[i+24] = (g_kal_sha_ctx->state[6] >> (24-i*8)) & 0x000000ff;
00250         hash[i+28] = (g_kal_sha_ctx->state[7] >> (24-i*8)) & 0x000000ff;
00251     }
00252     FREE(g_kal_sha_ctx);
00253     g_kal_sha_ctx = (kal_sha256_ctx_t*)NULL;
00254 }