2009-05-14 14:28:59 +00:00
|
|
|
/* twister-large.c */
|
2008-12-30 13:46:17 +00:00
|
|
|
/*
|
2009-02-04 13:50:15 +00:00
|
|
|
This file is part of the AVR-Crypto-Lib.
|
2008-12-30 13:46:17 +00:00
|
|
|
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2008-12-22 03:19:07 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "memxor.h"
|
|
|
|
#include "twister.h"
|
2009-05-14 14:28:59 +00:00
|
|
|
#include "twister-large.h"
|
2008-12-22 03:19:07 +00:00
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
2008-12-22 03:19:07 +00:00
|
|
|
|
|
|
|
static
|
2009-05-14 14:28:59 +00:00
|
|
|
void checksum_update(twister_large_ctx_t* ctx, uint8_t col){
|
2008-12-22 03:19:07 +00:00
|
|
|
uint8_t i, col2;
|
|
|
|
uint8_t carry = 0;
|
|
|
|
int sum = 0;
|
|
|
|
|
|
|
|
col2 = (col+1)%8;
|
|
|
|
|
|
|
|
for( i=0; i<8; i++ )
|
|
|
|
{
|
|
|
|
sum = (int) ctx->checksum[col2][7-i]
|
|
|
|
+ (int) ctx->state.s[7-i][0] /* col or 0 ???*/
|
|
|
|
+ carry;
|
|
|
|
ctx->checksum[col][7-i] ^= (uint8_t)sum;
|
|
|
|
carry = sum>>8;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-05-14 14:28:59 +00:00
|
|
|
void twister_large_init(twister_large_ctx_t* ctx, uint16_t hashsize_b){
|
2008-12-22 03:19:07 +00:00
|
|
|
memset(ctx->state.s, 0, 64);
|
|
|
|
memset(ctx->checksum, 0, 64);
|
|
|
|
ctx->state.counter=0xffffffffffffffffLL;
|
|
|
|
ctx->state.s[0][7] = hashsize_b>>8;
|
|
|
|
ctx->state.s[1][7] = hashsize_b&0xff;
|
|
|
|
ctx->state.length_counter_b = 0;
|
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-05-14 14:28:59 +00:00
|
|
|
void twister_large_nextBlock(twister_large_ctx_t* ctx, const void* msg){
|
2008-12-22 03:19:07 +00:00
|
|
|
uint8_t tmp[8][8];
|
|
|
|
|
|
|
|
/* 1st maxi round */
|
|
|
|
memcpy(tmp, ctx->state.s, 64);
|
|
|
|
checksum_update(ctx, 0);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
|
|
|
|
checksum_update(ctx, 1);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
|
|
|
|
checksum_update(ctx, 2);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
memxor(ctx->state.s, tmp, 64);
|
|
|
|
|
|
|
|
/* 2nd maxi round */
|
|
|
|
memcpy(tmp, ctx->state.s, 64);
|
|
|
|
checksum_update(ctx, 3);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
|
|
|
|
twister_blank_round(&(ctx->state));
|
|
|
|
|
|
|
|
checksum_update(ctx, 4);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
memxor(ctx->state.s, tmp, 64);
|
|
|
|
|
|
|
|
/* 3rd maxi round */
|
|
|
|
memcpy(tmp, ctx->state.s, 64);
|
|
|
|
checksum_update(ctx, 5);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
|
|
|
|
checksum_update(ctx, 6);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
msg = (uint8_t*)msg + 8;
|
|
|
|
|
|
|
|
checksum_update(ctx, 7);
|
|
|
|
twister_mini_round(&(ctx->state), msg);
|
|
|
|
|
|
|
|
twister_blank_round(&(ctx->state));
|
|
|
|
memxor(ctx->state.s, tmp, 64);
|
|
|
|
ctx->state.length_counter_b += 512;
|
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-05-14 14:28:59 +00:00
|
|
|
void twister_inject_chksum(twister_large_ctx_t* ctx, uint8_t col){
|
2008-12-22 03:19:07 +00:00
|
|
|
*((uint64_t*)(&ctx->state.s[7][0])) ^= *((uint64_t*)(&ctx->checksum[col][0]));
|
|
|
|
twister_blank_round(&ctx->state);
|
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-05-14 14:28:59 +00:00
|
|
|
void twister_large_lastBlock(twister_large_ctx_t* ctx, const void* msg, uint16_t length_b){
|
2008-12-22 03:19:07 +00:00
|
|
|
uint8_t tmp[64];
|
2009-05-14 09:38:02 +00:00
|
|
|
while(length_b>=512){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_nextBlock(ctx, msg);
|
2008-12-22 03:19:07 +00:00
|
|
|
msg = ((uint8_t*)msg)+64;
|
|
|
|
length_b -= 512;
|
|
|
|
}
|
|
|
|
memset(tmp, 0, 64);
|
|
|
|
memcpy(tmp, msg, (length_b+7)/8);
|
|
|
|
tmp[length_b/8] |= 0x80 >> (length_b&0x07);
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_nextBlock(ctx, tmp);
|
2008-12-22 03:19:07 +00:00
|
|
|
ctx->state.length_counter_b -= 512 - length_b;
|
|
|
|
twister_mini_round(&(ctx->state), &(ctx->state.length_counter_b));
|
|
|
|
|
|
|
|
memcpy(tmp, ctx->state.s, 64);
|
|
|
|
twister_inject_chksum(ctx, 0);
|
|
|
|
twister_inject_chksum(ctx, 1);
|
|
|
|
twister_inject_chksum(ctx, 2);
|
|
|
|
memxor(ctx->state.s, tmp, 64);
|
|
|
|
|
|
|
|
memcpy(tmp, ctx->state.s, 64);
|
|
|
|
twister_inject_chksum(ctx, 3);
|
|
|
|
twister_inject_chksum(ctx, 4);
|
|
|
|
twister_inject_chksum(ctx, 5);
|
|
|
|
memxor(ctx->state.s, tmp, 64);
|
|
|
|
|
|
|
|
memcpy(tmp, ctx->state.s, 64);
|
|
|
|
twister_inject_chksum(ctx, 6);
|
|
|
|
twister_inject_chksum(ctx, 7);
|
|
|
|
twister_blank_round(&(ctx->state));
|
|
|
|
memxor(ctx->state.s, tmp, 64);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-05-14 14:28:59 +00:00
|
|
|
void twister_large_ctx2hash(void* dest, twister_large_ctx_t* ctx, uint16_t hashsize_b){
|
2008-12-22 03:19:07 +00:00
|
|
|
twister_ctx2hash(dest, &(ctx->state), hashsize_b);
|
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
/*********************************************************************/
|
2008-12-22 03:19:07 +00:00
|
|
|
|
|
|
|
void twister384_init(twister384_ctx_t* ctx){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_init(ctx, 384);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-01-07 19:13:06 +00:00
|
|
|
void twister384_nextBlock(twister384_ctx_t* ctx, const void* msg){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_nextBlock(ctx, msg);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-01-07 19:13:06 +00:00
|
|
|
void twister384_lastBlock(twister384_ctx_t* ctx, const void* msg, uint16_t length_b){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_lastBlock(ctx, msg, length_b);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2008-12-22 03:19:07 +00:00
|
|
|
void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_ctx2hash(dest, ctx, 384);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-01-07 19:13:06 +00:00
|
|
|
void twister384(void* dest, const void* msg, uint32_t msg_length_b){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_ctx_t ctx;
|
|
|
|
twister_large_init(&ctx, 384);
|
2008-12-22 03:19:07 +00:00
|
|
|
while(msg_length_b >=512){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_nextBlock(&ctx, msg);
|
2008-12-22 03:19:07 +00:00
|
|
|
msg = (uint8_t*)msg + 512/8;
|
|
|
|
msg_length_b -= 512;
|
|
|
|
}
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_lastBlock(&ctx, msg, msg_length_b);
|
|
|
|
twister_large_ctx2hash(dest, &ctx, 384);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
/*********************************************************************/
|
|
|
|
|
2008-12-22 03:19:07 +00:00
|
|
|
|
|
|
|
void twister512_init(twister512_ctx_t* ctx){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_init(ctx, 512);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-01-07 19:13:06 +00:00
|
|
|
void twister512_nextBlock(twister512_ctx_t* ctx, const void* msg){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_nextBlock(ctx, msg);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-01-07 19:13:06 +00:00
|
|
|
void twister512_lastBlock(twister512_ctx_t* ctx, const void* msg, uint16_t length_b){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_lastBlock(ctx, msg, length_b);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2008-12-22 03:19:07 +00:00
|
|
|
void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_ctx2hash(dest, ctx, 512);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:20:22 +00:00
|
|
|
/*********************************************************************/
|
|
|
|
|
2009-01-07 19:13:06 +00:00
|
|
|
void twister512(void* dest, const void* msg, uint32_t msg_length_b){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_ctx_t ctx;
|
|
|
|
twister_large_init(&ctx, 512);
|
2008-12-22 03:19:07 +00:00
|
|
|
while(msg_length_b >=512){
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_nextBlock(&ctx, msg);
|
2008-12-22 03:19:07 +00:00
|
|
|
msg = (uint8_t*)msg + 512/8;
|
|
|
|
msg_length_b -= 512;
|
|
|
|
}
|
2009-05-14 14:28:59 +00:00
|
|
|
twister_large_lastBlock(&ctx, msg, msg_length_b);
|
|
|
|
twister_large_ctx2hash(dest, &ctx, 512);
|
2008-12-22 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|