Skip to content

Commit

Permalink
hsr algo (x13 + custom hash)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Oct 10, 2017
1 parent bd7bb9b commit b97c52f
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 1 deletion.
101 changes: 101 additions & 0 deletions stratum/algos/hsr14.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "../sha3/sph_blake.h"
#include "../sha3/sph_bmw.h"
#include "../sha3/sph_groestl.h"
#include "../sha3/sph_jh.h"
#include "../sha3/sph_keccak.h"
#include "../sha3/sph_skein.h"
#include "../sha3/sph_luffa.h"
#include "../sha3/sph_cubehash.h"
#include "../sha3/sph_shavite.h"
#include "../sha3/sph_simd.h"
#include "../sha3/sph_echo.h"
#include "../sha3/sph_hamsi.h"
#include "../sha3/sph_fugue.h"

#include "sm3.h"

#include "common.h"

void hsr_hash(const char* input, char* output, uint32_t len)
{
sph_blake512_context ctx_blake;
sph_bmw512_context ctx_bmw;
sph_groestl512_context ctx_groestl;
sph_skein512_context ctx_skein;
sph_jh512_context ctx_jh;
sph_keccak512_context ctx_keccak;
sph_luffa512_context ctx_luffa1;
sph_cubehash512_context ctx_cubehash1;
sph_shavite512_context ctx_shavite1;
sph_simd512_context ctx_simd1;
sph_echo512_context ctx_echo1;
sm3_ctx_t ctx_sm3;
sph_hamsi512_context ctx_hamsi1;
sph_fugue512_context ctx_fugue1;

uint8_t _ALIGN(128) hash[64];

sph_blake512_init(&ctx_blake);
sph_blake512(&ctx_blake, input, len);
sph_blake512_close(&ctx_blake, hash);

sph_bmw512_init(&ctx_bmw);
sph_bmw512(&ctx_bmw, hash, 64);
sph_bmw512_close(&ctx_bmw, hash);

sph_groestl512_init(&ctx_groestl);
sph_groestl512(&ctx_groestl, hash, 64);
sph_groestl512_close(&ctx_groestl, hash);

sph_skein512_init(&ctx_skein);
sph_skein512(&ctx_skein, hash, 64);
sph_skein512_close (&ctx_skein, hash);

sph_jh512_init(&ctx_jh);
sph_jh512(&ctx_jh, hash, 64);
sph_jh512_close(&ctx_jh, hash);

sph_keccak512_init(&ctx_keccak);
sph_keccak512(&ctx_keccak, hash, 64);
sph_keccak512_close(&ctx_keccak, hash);

sph_luffa512_init(&ctx_luffa1);
sph_luffa512(&ctx_luffa1, hash, 64);
sph_luffa512_close(&ctx_luffa1, hash);

sph_cubehash512_init(&ctx_cubehash1);
sph_cubehash512(&ctx_cubehash1, hash, 64);
sph_cubehash512_close(&ctx_cubehash1, hash);

sph_shavite512_init(&ctx_shavite1);
sph_shavite512(&ctx_shavite1, hash, 64);
sph_shavite512_close(&ctx_shavite1, hash);

sph_simd512_init(&ctx_simd1);
sph_simd512(&ctx_simd1, hash, 64);
sph_simd512_close(&ctx_simd1, hash);

sph_echo512_init (&ctx_echo1);
sph_echo512(&ctx_echo1, hash, 64);
sph_echo512_close(&ctx_echo1, hash);

sm3_init(&ctx_sm3);
sm3_update(&ctx_sm3, hash, 64);
memset(hash, 0, sizeof hash);
sm3_close(&ctx_sm3, hash);

sph_hamsi512_init(&ctx_hamsi1);
sph_hamsi512(&ctx_hamsi1, hash, 64);
sph_hamsi512_close(&ctx_hamsi1, hash);

sph_fugue512_init(&ctx_fugue1);
sph_fugue512(&ctx_fugue1, hash, 64);
sph_fugue512_close(&ctx_fugue1, hash);

memcpy(output, hash, 32);
}
16 changes: 16 additions & 0 deletions stratum/algos/hsr14.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef HSR14_H
#define HSR14_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

void hsr_hash(const char* input, char* output, uint32_t len);

#ifdef __cplusplus
}
#endif

#endif
1 change: 1 addition & 0 deletions stratum/algos/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LDFLAGS=-O2 -lgmp

SOURCES=lyra2re.c lyra2v2.c Lyra2.c lyra2z.c Lyra2z.c Sponge.c \
blake.c scrypt.c c11.c x11.c x13.c sha256.c sha256t.c jha.c keccak.c deep.c tribus.c \
hsr14.c sm3.c \
x14.c x15.c x17.c nist5.c fresh.c quark.c neoscrypt.c scryptn.c qubit.c skein.c groestl.c \
bitcore.c timetravel.c xevan.c bastion.c hmq17.c \
skein2.c zr5.c bmw.c luffa.c pentablake.c whirlpool.c whirlpoolx.c blakecoin.c \
Expand Down
220 changes: 220 additions & 0 deletions stratum/algos/sm3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* [email protected].
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/

#include <string.h>

#include "sm3.h"

void sm3_init(sm3_ctx_t *ctx)
{
ctx->digest[0] = 0x7380166F;
ctx->digest[1] = 0x4914B2B9;
ctx->digest[2] = 0x172442D7;
ctx->digest[3] = 0xDA8A0600;
ctx->digest[4] = 0xA96F30BC;
ctx->digest[5] = 0x163138AA;
ctx->digest[6] = 0xE38DEE4D;
ctx->digest[7] = 0xB0FB0E4E;

ctx->nblocks = 0;
ctx->num = 0;
}

void sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
{
if (ctx->num) {
unsigned int left = SM3_BLOCK_SIZE - ctx->num;
if (data_len < left) {
memcpy(ctx->block + ctx->num, data, data_len);
ctx->num += data_len;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sm3_compress(ctx->digest, ctx->block);
ctx->nblocks++;
data += left;
data_len -= left;
}
}
while (data_len >= SM3_BLOCK_SIZE) {
sm3_compress(ctx->digest, data);
ctx->nblocks++;
data += SM3_BLOCK_SIZE;
data_len -= SM3_BLOCK_SIZE;
}
ctx->num = data_len;
if (data_len) {
memcpy(ctx->block, data, data_len);
}
}

void sm3_close(void *cc, void *dst)
{
sm3_final(cc, dst);
memset(cc, 0, sizeof(sm3_ctx_t));
}

void sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
{
int i;
uint32_t *pdigest = (uint32_t *)digest;
uint32_t *count = (uint32_t *)(ctx->block + SM3_BLOCK_SIZE - 8);

ctx->block[ctx->num] = 0x80;

if (ctx->num + 9 <= SM3_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 1);
sm3_compress(ctx->digest, ctx->block);
memset(ctx->block, 0, SM3_BLOCK_SIZE - 8);
}

count[0] = cpu_to_be32((ctx->nblocks) >> 23);
count[1] = cpu_to_be32((ctx->nblocks << 9) + (ctx->num << 3));

sm3_compress(ctx->digest, ctx->block);
for (i = 0; i < sizeof(ctx->digest)/sizeof(ctx->digest[0]); i++) {
pdigest[i] = cpu_to_be32(ctx->digest[i]);
}
}

#define ROTATELEFT(X,n) (((X)<<(n)) | ((X)>>(32-(n))))

#define P0(x) ((x) ^ ROTATELEFT((x),9) ^ ROTATELEFT((x),17))
#define P1(x) ((x) ^ ROTATELEFT((x),15) ^ ROTATELEFT((x),23))

#define FF0(x,y,z) ( (x) ^ (y) ^ (z))
#define FF1(x,y,z) (((x) & (y)) | ( (x) & (z)) | ( (y) & (z)))

#define GG0(x,y,z) ( (x) ^ (y) ^ (z))
#define GG1(x,y,z) (((x) & (y)) | ( (~(x)) & (z)) )


void sm3_compress(uint32_t digest[8], const unsigned char block[64])
{
int j;
uint32_t W[68], W1[64];
const uint32_t *pblock = (const uint32_t *)block;

uint32_t A = digest[0];
uint32_t B = digest[1];
uint32_t C = digest[2];
uint32_t D = digest[3];
uint32_t E = digest[4];
uint32_t F = digest[5];
uint32_t G = digest[6];
uint32_t H = digest[7];
uint32_t SS1,SS2,TT1,TT2,T[64];

for (j = 0; j < 16; j++) {
W[j] = cpu_to_be32(pblock[j]);
}
for (j = 16; j < 68; j++) {
W[j] = P1( W[j-16] ^ W[j-9] ^ ROTATELEFT(W[j-3],15)) ^ ROTATELEFT(W[j - 13],7 ) ^ W[j-6];;
}
for( j = 0; j < 64; j++) {
W1[j] = W[j] ^ W[j+4];
}

for(j =0; j < 16; j++) {

T[j] = 0x79CC4519;
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
SS2 = SS1 ^ ROTATELEFT(A,12);
TT1 = FF0(A,B,C) + D + SS2 + W1[j];
TT2 = GG0(E,F,G) + H + SS1 + W[j];
D = C;
C = ROTATELEFT(B,9);
B = A;
A = TT1;
H = G;
G = ROTATELEFT(F,19);
F = E;
E = P0(TT2);
}

for(j =16; j < 64; j++) {

T[j] = 0x7A879D8A;
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
SS2 = SS1 ^ ROTATELEFT(A,12);
TT1 = FF1(A,B,C) + D + SS2 + W1[j];
TT2 = GG1(E,F,G) + H + SS1 + W[j];
D = C;
C = ROTATELEFT(B,9);
B = A;
A = TT1;
H = G;
G = ROTATELEFT(F,19);
F = E;
E = P0(TT2);
}

digest[0] ^= A;
digest[1] ^= B;
digest[2] ^= C;
digest[3] ^= D;
digest[4] ^= E;
digest[5] ^= F;
digest[6] ^= G;
digest[7] ^= H;
}

void sm3(const unsigned char *msg, size_t msglen,
unsigned char dgst[SM3_DIGEST_LENGTH])
{
sm3_ctx_t ctx;

sm3_init(&ctx);
sm3_update(&ctx, msg, msglen);
sm3_final(&ctx, dgst);

memset(&ctx, 0, sizeof(sm3_ctx_t));
}
Loading

0 comments on commit b97c52f

Please sign in to comment.