Moved this to src/

This commit is contained in:
Sean Bowe
2015-12-27 17:40:11 -07:00
parent 38767db686
commit 5fa9d9f438
4 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
# Makefile
# 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
BINARY = kctest
OBJS = keccak.o main.o
DIST = readable_keccak
CC = gcc
CFLAGS = -Wall -O
LIBS =
LDFLAGS =
INCLUDES =
$(BINARY): $(OBJS)
$(CC) $(LDFLAGS) -o $(BINARY) $(OBJS) $(LIBS)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
rm -rf $(DIST).tgz $(OBJS) $(BINARY) *~
dist: clean
cd ..; tar cfvz $(DIST)/$(DIST).tgz $(DIST)/*

View File

@@ -0,0 +1,109 @@
// keccak.c
// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
// A baseline Keccak (3rd round) implementation.
#include "keccak.h"
const uint64_t keccakf_rndc[24] =
{
0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008
};
const int keccakf_rotc[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
};
const int keccakf_piln[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
};
// update the state with given number of rounds
void keccakf(uint64_t st[25], int rounds)
{
int i, j, round;
uint64_t t, bc[5];
for (round = 0; round < rounds; round++) {
// Theta
for (i = 0; i < 5; i++)
bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
for (i = 0; i < 5; i++) {
t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
for (j = 0; j < 25; j += 5)
st[j + i] ^= t;
}
// Rho Pi
t = st[1];
for (i = 0; i < 24; i++) {
j = keccakf_piln[i];
bc[0] = st[j];
st[j] = ROTL64(t, keccakf_rotc[i]);
t = bc[0];
}
// Chi
for (j = 0; j < 25; j += 5) {
for (i = 0; i < 5; i++)
bc[i] = st[j + i];
for (i = 0; i < 5; i++)
st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
}
// Iota
st[0] ^= keccakf_rndc[round];
}
}
// compute a keccak hash (md) of given byte length from "in"
int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen)
{
uint64_t st[25];
//uint8_t temp[144];
int i, rsiz, rsizw;
rsiz = 200 - 2 * mdlen;
rsizw = rsiz / 8;
memset(st, 0, sizeof(st));
/*
for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) {
for (i = 0; i < rsizw; i++)
st[i] ^= ((uint64_t *) in)[i];
keccakf(st, KECCAK_ROUNDS);
}
// last block and padding
memcpy(temp, in, inlen);
temp[inlen++] = 1;
memset(temp + inlen, 0, rsiz - inlen);
temp[rsiz - 1] |= 0x80;
*/
for (i = 0; i < rsizw; i++) {
st[i] ^= ((uint64_t *) in)[i];
}
keccakf(st, KECCAK_ROUNDS);
memcpy(md, st, mdlen);
return 0;
}

View File

@@ -0,0 +1,25 @@
// keccak.h
// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
#ifndef KECCAK_H
#define KECCAK_H
#include <stdint.h>
#include <string.h>
#ifndef KECCAK_ROUNDS
#define KECCAK_ROUNDS 24
#endif
#ifndef ROTL64
#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
#endif
// compute a keccak hash (md) of given byte length from "in"
int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen);
// update the state
void keccakf(uint64_t st[25], int norounds);
#endif

162
src/readable_keccak/main.c Normal file
View File

@@ -0,0 +1,162 @@
// main.c
// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
#include <stdio.h>
#include <string.h>
#include "keccak.h"
// test each length
typedef struct {
int mdlen;
char *msgstr;
uint8_t md[64];
} test_triplet_t;
// returns zero on success, nonzero + stderr messages on failure
int keccak_test()
{
test_triplet_t testvec[4] = {
{
28, "Keccak-224 Test Hash", {
0x30, 0x04, 0x5B, 0x34, 0x94, 0x6E, 0x1B, 0x2E,
0x09, 0x16, 0x13, 0x36, 0x2F, 0xD2, 0x2A, 0xA0,
0x8E, 0x2B, 0xEA, 0xFE, 0xC5, 0xE8, 0xDA, 0xEE,
0x42, 0xC2, 0xE6, 0x65 }
}, {
32, "Keccak-256 Test Hash", {
0xA8, 0xD7, 0x1B, 0x07, 0xF4, 0xAF, 0x26, 0xA4,
0xFF, 0x21, 0x02, 0x7F, 0x62, 0xFF, 0x60, 0x26,
0x7F, 0xF9, 0x55, 0xC9, 0x63, 0xF0, 0x42, 0xC4,
0x6D, 0xA5, 0x2E, 0xE3, 0xCF, 0xAF, 0x3D, 0x3C }
}, {
48, "Keccak-384 Test Hash", {
0xE2, 0x13, 0xFD, 0x74, 0xAF, 0x0C, 0x5F, 0xF9,
0x1B, 0x42, 0x3C, 0x8B, 0xCE, 0xEC, 0xD7, 0x01,
0xF8, 0xDD, 0x64, 0xEC, 0x18, 0xFD, 0x6F, 0x92,
0x60, 0xFC, 0x9E, 0xC1, 0xED, 0xBD, 0x22, 0x30,
0xA6, 0x90, 0x86, 0x65, 0xBC, 0xD9, 0xFB, 0xF4,
0x1A, 0x99, 0xA1, 0x8A, 0x7D, 0x9E, 0x44, 0x6E }
}, {
64, "Keccak-512 Test Hash", {
0x96, 0xEE, 0x47, 0x18, 0xDC, 0xBA, 0x3C, 0x74,
0x61, 0x9B, 0xA1, 0xFA, 0x7F, 0x57, 0xDF, 0xE7,
0x76, 0x9D, 0x3F, 0x66, 0x98, 0xA8, 0xB3, 0x3F,
0xA1, 0x01, 0x83, 0x89, 0x70, 0xA1, 0x31, 0xE6,
0x21, 0xCC, 0xFD, 0x05, 0xFE, 0xFF, 0xBC, 0x11,
0x80, 0xF2, 0x63, 0xC2, 0x7F, 0x1A, 0xDA, 0xB4,
0x60, 0x95, 0xD6, 0xF1, 0x25, 0x33, 0x14, 0x72,
0x4B, 0x5C, 0xBF, 0x78, 0x28, 0x65, 0x8E, 0x6A }
}
};
int i, fails;
uint8_t md[64];
fails = 0;
for (i = 0; i < 4; i++) {
keccak((uint8_t *) testvec[i].msgstr,
strlen(testvec[i].msgstr),
md, testvec[i].mdlen);
if (memcmp(md, testvec[i].md, testvec[i].mdlen)) {
fails++;
fprintf(stderr, "Keccak-%d FAILED.", testvec[i].mdlen * 8);
}
}
return fails;
}
// main
int main(int argc, char **argv)
{
/*
if (keccak_test() == 0)
printf("Keccak self-test OK!\n");
return 0;
*/
uint8_t in[144] = {
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B, 0xFF,
0xBB, 0x3B, 0x1B, 0x0B
};
uint8_t md[32] = {0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
keccak(in, 144, md, 32);
for (int i = 0; i < 32; i++) {
printf("%02x ", md[i]);
}
/*
uint64_t st[25] = {
0xabcdef0123456789,
0x9abcdef012345678,
0x89abcdef01234567,
0x789abcdef0123456,
0x6789abcdef012345,
0x56789abcdef01234,
0x456789abcdef0123,
0x3456789abcdef012,
0x23456789abcdef01,
0x123456789abcdef0,
0x0123456789abcdef,
0xf0123456789abcde,
0xef0123456789abcd,
0xdef0123456789abc,
0xcdef0123456789ab,
0xbcdef0123456789a,
0xabcdef0123456789,
0x9abcdef012345678,
0x89abcdef01234567,
0x789abcdef0123456,
0x6789abcdef012345,
0x56789abcdef01234,
0x456789abcdef0123,
0x3456789abcdef012,
0x23456789abcdef01
};
keccakf(st, 24);
if(st[24] == 0x3300e5d2414b6a93) {
printf("wow good job\n");
}
*/
}