Path: blob/master/libs/tomcrypt/src/hashes/sha2/sha256.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file sha256.c12LTC_SHA256 by Tom St Denis13*/1415#ifdef LTC_SHA2561617const struct ltc_hash_descriptor sha256_desc =18{19"sha256",200,2132,2264,2324/* OID */25{ 2, 16, 840, 1, 101, 3, 4, 2, 1, },269,2728&sha256_init,29&sha256_process,30&sha256_done,31&sha256_test,32NULL33};3435#ifdef LTC_SMALL_CODE36/* the K array */37static const ulong32 K[64] = {380x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,390x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,400x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,410xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,420x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,430xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,440x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,450x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,460xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,470xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,480x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,490x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,500x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL51};52#endif5354/* Various logical functions */55#define Ch(x,y,z) (z ^ (x & (y ^ z)))56#define Maj(x,y,z) (((x | y) & z) | (x & y))57#define S(x, n) RORc((x),(n))58#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))59#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))60#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))61#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))62#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))6364/* compress 512-bits */65#ifdef LTC_CLEAN_STACK66static int _sha256_compress(hash_state * md, unsigned char *buf)67#else68static int sha256_compress(hash_state * md, unsigned char *buf)69#endif70{71ulong32 S[8], W[64], t0, t1;72#ifdef LTC_SMALL_CODE73ulong32 t;74#endif75int i;7677/* copy state into S */78for (i = 0; i < 8; i++) {79S[i] = md->sha256.state[i];80}8182/* copy the state into 512-bits into W[0..15] */83for (i = 0; i < 16; i++) {84LOAD32H(W[i], buf + (4*i));85}8687/* fill W[16..63] */88for (i = 16; i < 64; i++) {89W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];90}9192/* Compress */93#ifdef LTC_SMALL_CODE94#define RND(a,b,c,d,e,f,g,h,i) \95t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \96t1 = Sigma0(a) + Maj(a, b, c); \97d += t0; \98h = t0 + t1;99100for (i = 0; i < 64; ++i) {101RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);102t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];103S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;104}105#else106#define RND(a,b,c,d,e,f,g,h,i,ki) \107t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \108t1 = Sigma0(a) + Maj(a, b, c); \109d += t0; \110h = t0 + t1;111112RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);113RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);114RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);115RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);116RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);117RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);118RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);119RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);120RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);121RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);122RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);123RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);124RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);125RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);126RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);127RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);128RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);129RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);130RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);131RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);132RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);133RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);134RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);135RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);136RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);137RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);138RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);139RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);140RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);141RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);142RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);143RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);144RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);145RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);146RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);147RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);148RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);149RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);150RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);151RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);152RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);153RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);154RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);155RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);156RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);157RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);158RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);159RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);160RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);161RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);162RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);163RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);164RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);165RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);166RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);167RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);168RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);169RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);170RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);171RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);172RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);173RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);174RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);175RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);176177#undef RND178179#endif180181/* feedback */182for (i = 0; i < 8; i++) {183md->sha256.state[i] = md->sha256.state[i] + S[i];184}185return CRYPT_OK;186}187188#ifdef LTC_CLEAN_STACK189static int sha256_compress(hash_state * md, unsigned char *buf)190{191int err;192err = _sha256_compress(md, buf);193burn_stack(sizeof(ulong32) * 74);194return err;195}196#endif197198/**199Initialize the hash state200@param md The hash state you wish to initialize201@return CRYPT_OK if successful202*/203int sha256_init(hash_state * md)204{205LTC_ARGCHK(md != NULL);206207md->sha256.curlen = 0;208md->sha256.length = 0;209md->sha256.state[0] = 0x6A09E667UL;210md->sha256.state[1] = 0xBB67AE85UL;211md->sha256.state[2] = 0x3C6EF372UL;212md->sha256.state[3] = 0xA54FF53AUL;213md->sha256.state[4] = 0x510E527FUL;214md->sha256.state[5] = 0x9B05688CUL;215md->sha256.state[6] = 0x1F83D9ABUL;216md->sha256.state[7] = 0x5BE0CD19UL;217return CRYPT_OK;218}219220/**221Process a block of memory though the hash222@param md The hash state223@param in The data to hash224@param inlen The length of the data (octets)225@return CRYPT_OK if successful226*/227HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)228229/**230Terminate the hash to get the digest231@param md The hash state232@param out [out] The destination of the hash (32 bytes)233@return CRYPT_OK if successful234*/235int sha256_done(hash_state * md, unsigned char *out)236{237int i;238239LTC_ARGCHK(md != NULL);240LTC_ARGCHK(out != NULL);241242if (md->sha256.curlen >= sizeof(md->sha256.buf)) {243return CRYPT_INVALID_ARG;244}245246247/* increase the length of the message */248md->sha256.length += md->sha256.curlen * 8;249250/* append the '1' bit */251md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;252253/* if the length is currently above 56 bytes we append zeros254* then compress. Then we can fall back to padding zeros and length255* encoding like normal.256*/257if (md->sha256.curlen > 56) {258while (md->sha256.curlen < 64) {259md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;260}261sha256_compress(md, md->sha256.buf);262md->sha256.curlen = 0;263}264265/* pad upto 56 bytes of zeroes */266while (md->sha256.curlen < 56) {267md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;268}269270/* store length */271STORE64H(md->sha256.length, md->sha256.buf+56);272sha256_compress(md, md->sha256.buf);273274/* copy output */275for (i = 0; i < 8; i++) {276STORE32H(md->sha256.state[i], out+(4*i));277}278#ifdef LTC_CLEAN_STACK279zeromem(md, sizeof(hash_state));280#endif281return CRYPT_OK;282}283284/**285Self-test the hash286@return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled287*/288int sha256_test(void)289{290#ifndef LTC_TEST291return CRYPT_NOP;292#else293static const struct {294const char *msg;295unsigned char hash[32];296} tests[] = {297{ "abc",298{ 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,2990x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,3000xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,3010xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }302},303{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",304{ 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,3050xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,3060xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,3070xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }308},309};310311int i;312unsigned char tmp[32];313hash_state md;314315for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {316sha256_init(&md);317sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));318sha256_done(&md, tmp);319if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) {320return CRYPT_FAIL_TESTVECTOR;321}322}323return CRYPT_OK;324#endif325}326327#endif328329330