Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2017-13861/sha256.c
Views: 11780
/*********************************************************************1* Filename: sha256.c2* Author: Brad Conte (brad AT bradconte.com)3* Copyright:4* Disclaimer: This code is presented "as is" without any guarantees.5* Details: Implementation of the SHA-256 hashing algorithm.6SHA-256 is one of the three algorithms in the SHA27specification. The others, SHA-384 and SHA-512, are not8offered in this implementation.9Algorithm specification can be found here:10* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf11This implementation uses little endian byte order.12*********************************************************************/1314/*************************** HEADER FILES ***************************/15#include <stdlib.h>16#include <memory.h>17#include "sha256.h"1819/****************************** MACROS ******************************/20#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))21#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))2223#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))24#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))25#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))26#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))27#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))28#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))2930/**************************** VARIABLES *****************************/31static const WORD k[64] = {320x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,330xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,340xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,350x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,360x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,370xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,380x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,390x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f240};4142/*********************** FUNCTION DEFINITIONS ***********************/43void sha256_transform(SHA256_CTX *ctx, const BYTE data[])44{45WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];4647for (i = 0, j = 0; i < 16; ++i, j += 4)48m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);49for ( ; i < 64; ++i)50m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];5152a = ctx->state[0];53b = ctx->state[1];54c = ctx->state[2];55d = ctx->state[3];56e = ctx->state[4];57f = ctx->state[5];58g = ctx->state[6];59h = ctx->state[7];6061for (i = 0; i < 64; ++i) {62t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];63t2 = EP0(a) + MAJ(a,b,c);64h = g;65g = f;66f = e;67e = d + t1;68d = c;69c = b;70b = a;71a = t1 + t2;72}7374ctx->state[0] += a;75ctx->state[1] += b;76ctx->state[2] += c;77ctx->state[3] += d;78ctx->state[4] += e;79ctx->state[5] += f;80ctx->state[6] += g;81ctx->state[7] += h;82}8384void sha256_init(SHA256_CTX *ctx)85{86ctx->datalen = 0;87ctx->bitlen = 0;88ctx->state[0] = 0x6a09e667;89ctx->state[1] = 0xbb67ae85;90ctx->state[2] = 0x3c6ef372;91ctx->state[3] = 0xa54ff53a;92ctx->state[4] = 0x510e527f;93ctx->state[5] = 0x9b05688c;94ctx->state[6] = 0x1f83d9ab;95ctx->state[7] = 0x5be0cd19;96}9798void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)99{100WORD i;101102for (i = 0; i < len; ++i) {103ctx->data[ctx->datalen] = data[i];104ctx->datalen++;105if (ctx->datalen == 64) {106sha256_transform(ctx, ctx->data);107ctx->bitlen += 512;108ctx->datalen = 0;109}110}111}112113void sha256_final(SHA256_CTX *ctx, BYTE hash[])114{115WORD i;116117i = ctx->datalen;118119// Pad whatever data is left in the buffer.120if (ctx->datalen < 56) {121ctx->data[i++] = 0x80;122while (i < 56)123ctx->data[i++] = 0x00;124}125else {126ctx->data[i++] = 0x80;127while (i < 64)128ctx->data[i++] = 0x00;129sha256_transform(ctx, ctx->data);130memset(ctx->data, 0, 56);131}132133// Append to the padding the total message's length in bits and transform.134ctx->bitlen += ctx->datalen * 8;135ctx->data[63] = ctx->bitlen;136ctx->data[62] = ctx->bitlen >> 8;137ctx->data[61] = ctx->bitlen >> 16;138ctx->data[60] = ctx->bitlen >> 24;139ctx->data[59] = ctx->bitlen >> 32;140ctx->data[58] = ctx->bitlen >> 40;141ctx->data[57] = ctx->bitlen >> 48;142ctx->data[56] = ctx->bitlen >> 56;143sha256_transform(ctx, ctx->data);144145// Since this implementation uses little endian byte ordering and SHA uses big endian,146// reverse all the bytes when copying the final state to the output hash.147for (i = 0; i < 4; ++i) {148hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;149hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;150hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;151hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;152hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;153hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;154hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;155hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;156}157}158159