220 lines
4.6 KiB
C
220 lines
4.6 KiB
C
/** libkeccak-tiny
|
|
*
|
|
* A single-file implementation of SHA-3 and SHAKE.
|
|
*
|
|
* Implementor: David Leon Gil
|
|
* License: CC0, attribution kindly requested. Blame taken too,
|
|
* but not liability.
|
|
*/
|
|
//#include "sha3.h"
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
/******** The Keccak-f[1600] permutation ********/
|
|
|
|
/*** Constants. ***/
|
|
static const uint8_t rho[24] = \
|
|
{ 1, 3, 6, 10, 15, 21,
|
|
28, 36, 45, 55, 2, 14,
|
|
27, 41, 56, 8, 25, 43,
|
|
62, 18, 39, 61, 20, 44};
|
|
static const uint8_t pi[24] = \
|
|
{10, 7, 11, 17, 18, 3,
|
|
5, 16, 8, 21, 24, 4,
|
|
15, 23, 19, 13, 12, 2,
|
|
20, 14, 22, 9, 6, 1};
|
|
static const uint64_t RC[24] = \
|
|
{1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
|
|
0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
|
|
0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
|
|
0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
|
|
0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
|
|
0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
|
|
|
|
/*** Helper macros to unroll the permutation. ***/
|
|
|
|
#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
|
|
#define Plen 200
|
|
|
|
static inline void keccakf(void* state)
|
|
{
|
|
uint64_t* a = (uint64_t*)state;
|
|
uint64_t b[5] = {0};
|
|
uint64_t t = 0;
|
|
uint8_t x, y;
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
//----------------------- Theta----------------
|
|
for (int x =0 ; x<5; x=x+1)
|
|
{
|
|
b[x] = 0;
|
|
for (int y = 0 ;y<5*5; y=y+5)
|
|
{
|
|
b[x] ^= a[x + y];
|
|
}
|
|
}
|
|
/*
|
|
if(i==0)
|
|
{
|
|
printf("the b is:\n");
|
|
for (int i=0; i < 5; ++i) {
|
|
printf("%08llx ",b[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
*/
|
|
for (int x= 0;x<5;x=x+1)
|
|
{
|
|
for(int y =0;y<5*5;y=y+5)
|
|
{
|
|
a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1);
|
|
}
|
|
}
|
|
|
|
|
|
//---------------------------------------
|
|
// Rho and pi
|
|
t = a[1];
|
|
x = 0;
|
|
|
|
for(int x=0;x<24;x=x+1)
|
|
{
|
|
b[0] = a[pi[x]];
|
|
a[pi[x]] = rol(t, rho[x]);
|
|
t = b[0];
|
|
}
|
|
|
|
// Chi
|
|
for (int y=0;y<5*5 ;y=y+5)
|
|
{
|
|
for(int x=0 ; x <5 ;x=x+1)
|
|
{
|
|
b[x] = a[y + x];
|
|
}
|
|
for(int x =0 ;x <5 ;x=x+1)
|
|
{
|
|
a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]);
|
|
}
|
|
}
|
|
/*
|
|
if(i==0)
|
|
{
|
|
printf("the a is:\n");
|
|
for (int i=0; i < 25; ++i) {
|
|
printf("%08llx ",a[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
*/
|
|
// Iota
|
|
a[0] ^= RC[i];
|
|
}
|
|
}
|
|
|
|
/** The sponge-based hash construction. **/
|
|
static inline int hash(uint8_t* out, size_t outlen,const uint8_t* in, size_t inlen,size_t rate, uint8_t delim) {
|
|
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
|
|
return -1;
|
|
}
|
|
uint8_t a[Plen] = {0};
|
|
|
|
//printf("outlen=%lu,inlen=%lu,rate=%lu ,delim=%d\n",outlen,inlen,rate,delim);
|
|
// Absorb input.
|
|
// foldP(in, inlen, xorin);
|
|
while (inlen >= rate) {
|
|
printf("1\n");
|
|
for (size_t i = 0; i < rate; i += 1)
|
|
{
|
|
a[i] ^= in[i];
|
|
}
|
|
keccakf(a);
|
|
in += rate;
|
|
inlen -= rate;
|
|
}
|
|
// Xor in the DS and pad frame.
|
|
a[inlen] ^= delim;
|
|
a[rate - 1] ^= 0x80;
|
|
/*
|
|
printf("the a is:\n");
|
|
for (int i=0; i < Plen; ++i) {
|
|
printf("%02x ",a[i]);
|
|
}
|
|
printf("\n");
|
|
*/
|
|
// Xor in the last block.
|
|
// xorin(a, in, inlen);
|
|
for (size_t i = 0; i < inlen; i += 1)
|
|
{
|
|
a[i] ^= in[i];
|
|
}
|
|
/*
|
|
printf("the a is:\n");
|
|
for (int i=0; i < Plen; ++i) {
|
|
printf("%02x",a[i]);
|
|
}
|
|
printf("\n");
|
|
*/
|
|
// Apply P
|
|
keccakf(a);
|
|
/*
|
|
printf("the a is:\n");
|
|
for (int i=0; i < Plen; ++i) {
|
|
printf("%02x ",a[i]);
|
|
}
|
|
printf("\n");
|
|
*/
|
|
// Squeeze output.
|
|
// foldP(out, outlen, setout);
|
|
while (outlen >= rate) {
|
|
printf("2\n");
|
|
for (size_t i = 0; i < rate; i += 1)
|
|
{
|
|
out[i] = a[i];
|
|
}
|
|
keccakf(a);
|
|
out += rate;
|
|
inlen -= rate;
|
|
}
|
|
//setout(a, out, outlen);
|
|
for (size_t i = 0; i < outlen; i += 1)
|
|
{
|
|
out[i] = a[i];
|
|
}
|
|
memset(a, 0, 200);
|
|
return 0;
|
|
}
|
|
|
|
int sha3_256(uint8_t* out, size_t outlen,const uint8_t* in, size_t inlen)
|
|
{
|
|
if (outlen > 32) {
|
|
return -1;
|
|
}
|
|
return hash(out, outlen, in, inlen, 200 - (256 / 4), 0x06); //0x01
|
|
}
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
uint8_t md[32];
|
|
uint8_t in[41] = {
|
|
4, 0, 0, 0, 0, 0, 0, 0,
|
|
105, 242, 153, 192, 55, 88, 19, 131, 21, 206, 122, 77, 20, 64, 137, 83, 244, 231, 6, 255, 198, 10, 176, 73, 146, 100, 29, 3, 9, 24, 152, 82,
|
|
1
|
|
};
|
|
sha3_256(md, 32, in, sizeof(in));
|
|
sha3_256(md, 32, md, sizeof(md));
|
|
sha3_256(md, 32, md, sizeof(md));
|
|
|
|
for (int i=0; i < 32; ++i) {
|
|
printf("%02x",md[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
|
|
// nonce:[4, 0, 0, 0, 0, 0, 0, 0]
|
|
// mining_hash:FixedHash([105, 242, 153, 192, 55, 88, 19, 131, 21, 206, 122, 77, 20, 64, 137, 83, 244, 231, 6, 255, 198, 10, 176, 73, 146, 100, 29, 3, 9, 24, 152, 82])
|
|
// pow_data:[1]
|