CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Crypto/sha256.cpp
Views: 1401
/*1* FIPS-180-2 compliant SHA-256 implementation2*3* Copyright (C) 2001-2003 Christophe Devine4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*/1920#include <string.h>2122#include "sha256.h"2324#define GET_uint32_t(n,b,i) \25{ \26(n) = ( (uint32_t) (b)[(i) ] << 24 ) \27| ( (uint32_t) (b)[(i) + 1] << 16 ) \28| ( (uint32_t) (b)[(i) + 2] << 8 ) \29| ( (uint32_t) (b)[(i) + 3] ); \30}3132#define PUT_uint32_t(n,b,i) \33{ \34(b)[(i) ] = (uint8_t) ( (n) >> 24 ); \35(b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \36(b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \37(b)[(i) + 3] = (uint8_t) ( (n) ); \38}3940void sha256_starts( sha256_context *ctx )41{42ctx->total[0] = 0;43ctx->total[1] = 0;4445ctx->state[0] = 0x6A09E667;46ctx->state[1] = 0xBB67AE85;47ctx->state[2] = 0x3C6EF372;48ctx->state[3] = 0xA54FF53A;49ctx->state[4] = 0x510E527F;50ctx->state[5] = 0x9B05688C;51ctx->state[6] = 0x1F83D9AB;52ctx->state[7] = 0x5BE0CD19;53}5455void sha256_process( sha256_context *ctx, const uint8_t data[64] )56{57uint32_t temp1, temp2, W[64];58uint32_t A, B, C, D, E, F, G, H;5960GET_uint32_t( W[0], data, 0 );61GET_uint32_t( W[1], data, 4 );62GET_uint32_t( W[2], data, 8 );63GET_uint32_t( W[3], data, 12 );64GET_uint32_t( W[4], data, 16 );65GET_uint32_t( W[5], data, 20 );66GET_uint32_t( W[6], data, 24 );67GET_uint32_t( W[7], data, 28 );68GET_uint32_t( W[8], data, 32 );69GET_uint32_t( W[9], data, 36 );70GET_uint32_t( W[10], data, 40 );71GET_uint32_t( W[11], data, 44 );72GET_uint32_t( W[12], data, 48 );73GET_uint32_t( W[13], data, 52 );74GET_uint32_t( W[14], data, 56 );75GET_uint32_t( W[15], data, 60 );7677#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)78#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))7980#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))81#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))8283#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))84#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))8586#define F0(x,y,z) ((x & y) | (z & (x | y)))87#define F1(x,y,z) (z ^ (x & (y ^ z)))8889#define R(t) \90( \91W[t] = S1(W[t - 2]) + W[t - 7] + \92S0(W[t - 15]) + W[t - 16] \93)9495#define P(a,b,c,d,e,f,g,h,x,K) \96{ \97temp1 = h + S3(e) + F1(e,f,g) + K + x; \98temp2 = S2(a) + F0(a,b,c); \99d += temp1; h = temp1 + temp2; \100}101102A = ctx->state[0];103B = ctx->state[1];104C = ctx->state[2];105D = ctx->state[3];106E = ctx->state[4];107F = ctx->state[5];108G = ctx->state[6];109H = ctx->state[7];110111P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );112P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );113P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );114P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );115P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );116P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );117P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );118P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );119P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );120P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );121P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );122P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );123P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );124P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );125P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );126P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );127P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );128P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );129P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );130P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );131P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );132P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );133P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );134P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );135P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );136P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );137P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );138P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );139P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );140P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );141P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );142P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );143P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );144P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );145P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );146P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );147P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );148P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );149P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );150P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );151P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );152P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );153P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );154P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );155P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );156P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );157P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );158P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );159P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );160P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );161P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );162P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );163P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );164P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );165P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );166P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );167P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );168P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );169P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );170P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );171P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );172P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );173P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );174P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );175176ctx->state[0] += A;177ctx->state[1] += B;178ctx->state[2] += C;179ctx->state[3] += D;180ctx->state[4] += E;181ctx->state[5] += F;182ctx->state[6] += G;183ctx->state[7] += H;184}185186void sha256_update( sha256_context *ctx, const uint8_t *input, uint32_t length )187{188uint32_t left, fill;189190if( ! length ) return;191192left = ctx->total[0] & 0x3F;193fill = 64 - left;194195ctx->total[0] += length;196ctx->total[0] &= 0xFFFFFFFF;197198if( ctx->total[0] < length )199ctx->total[1]++;200201if( left && length >= fill )202{203memcpy( (void *) (ctx->buffer + left),204(void *) input, fill );205sha256_process( ctx, ctx->buffer );206length -= fill;207input += fill;208left = 0;209}210211while( length >= 64 )212{213sha256_process( ctx, input );214length -= 64;215input += 64;216}217218if( length )219{220memcpy( (void *) (ctx->buffer + left),221(void *) input, length );222}223}224225static const uint8_t sha256_padding[64] =226{2270x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,2280, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,2290, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,2300, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0231};232233void sha256_finish( sha256_context *ctx, uint8_t digest[32] )234{235uint32_t last, padn;236uint32_t high, low;237uint8_t msglen[8];238239high = ( ctx->total[0] >> 29 )240| ( ctx->total[1] << 3 );241low = ( ctx->total[0] << 3 );242243PUT_uint32_t( high, msglen, 0 );244PUT_uint32_t( low, msglen, 4 );245246last = ctx->total[0] & 0x3F;247padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );248249sha256_update( ctx, sha256_padding, padn );250sha256_update( ctx, msglen, 8 );251252PUT_uint32_t( ctx->state[0], digest, 0 );253PUT_uint32_t( ctx->state[1], digest, 4 );254PUT_uint32_t( ctx->state[2], digest, 8 );255PUT_uint32_t( ctx->state[3], digest, 12 );256PUT_uint32_t( ctx->state[4], digest, 16 );257PUT_uint32_t( ctx->state[5], digest, 20 );258PUT_uint32_t( ctx->state[6], digest, 24 );259PUT_uint32_t( ctx->state[7], digest, 28 );260}261262263264