avr-crypto-lib/shabal/shabal.c

122 lines
2.9 KiB
C
Raw Permalink Normal View History

2009-04-20 12:35:04 +00:00
/* shabal.c */
/*
This file is part of the AVR-Crypto-Lib.
2015-02-06 02:43:31 +00:00
Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
2009-04-20 12:35:04 +00:00
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/>.
*/
/*
* \file shabal.c
* \author Daniel Otte
2015-02-06 02:43:31 +00:00
* \email bg@nerilex.org
2009-04-20 12:35:04 +00:00
* \date 2009-04-17
* \license GPLv3 or later
*
*/
#include <stdint.h>
#include "shabal.h"
#include <string.h>
#define SHABAL_O1 13
#define SHABAL_O2 9
#define SHABAL_O3 6
static inline
uint32_t shabal_u(uint32_t a){
return (a<<1)+a; /* a*3 */
}
static inline
uint32_t shabal_v(uint32_t a){
return (a<<2)+a; /* a*5 */
}
#define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
static
void shabal_p(shabal_ctx_t *ctx, const void *m){
2009-04-20 12:35:04 +00:00
uint8_t i,j;
for(i=0;i<16;++i){
ctx->b[i] = ROTL32(ctx->b[i],17);
}
2009-04-27 17:47:36 +00:00
for(j=0;j<SHABAL_P;j++){
2009-04-20 12:35:04 +00:00
for(i=0;i<16;++i){
2009-04-27 17:47:36 +00:00
ctx->a[(i+j*16)%SHABAL_R] =
shabal_u(ctx->a[(i+j*16)%SHABAL_R]
^ shabal_v(ROTL32(ctx->a[(i+j*16+SHABAL_R-1)%SHABAL_R],15))
2009-04-20 12:35:04 +00:00
^ ctx->c[(8-i+16)%16])
^ ctx->b[(i+SHABAL_O1)%16]
^ ((ctx->b[(i+SHABAL_O2)%16]) & ~(ctx->b[(i+SHABAL_O3)%16]))
^ ((uint32_t*)m)[i];
2009-04-27 17:47:36 +00:00
ctx->b[i] = ROTL32(ctx->b[i], 1) ^ ~(ctx->a[(i+j*16)%SHABAL_R]);
2009-04-20 12:35:04 +00:00
}
}
for(j=0;j<36;++j){
ctx->a[j%SHABAL_R] += ctx->c[(j+3)%16];
}
}
void shabal_nextBlock(shabal_ctx_t *ctx, const void *block){
2009-04-20 12:35:04 +00:00
uint8_t i;
uint32_t *t;
2009-04-20 12:35:04 +00:00
for(i=0;i<16;++i){
ctx->b[i] += ((uint32_t*)block)[i];
}
ctx->a[0] ^= ctx->w.w32[0];
ctx->a[1] ^= ctx->w.w32[1];
shabal_p(ctx, block);
for(i=0;i<16;++i){
ctx->c[i] -= ((uint32_t*)block)[i];
}
ctx->w.w64++;
t = ctx->c;
ctx->c = ctx->b;
ctx->b = t;
}
void shabal_lastBlock(shabal_ctx_t *ctx, const void *block, uint16_t length_b){
2009-04-20 12:35:04 +00:00
uint8_t i,j;
uint32_t *t;
2009-04-20 12:35:04 +00:00
uint8_t buffer[64];
while(length_b>=SHABAL_BLOCKSIZE){
shabal_nextBlock(ctx, block);
block = (uint8_t*)block + SHABAL_BLOCKSIZE_B;
length_b -= SHABAL_BLOCKSIZE;
}
memset(buffer, 0, 64);
memcpy(buffer, block, (length_b+7)/8);
buffer[length_b/8] |= 0x80>>(length_b%8);
for(i=0;i<16;++i){
ctx->b[i] += ((uint32_t*)buffer)[i];
}
2009-04-27 17:47:36 +00:00
for(j=0; j<4;++j){
2009-04-20 12:35:04 +00:00
ctx->a[0] ^= ctx->w.w32[0];
ctx->a[1] ^= ctx->w.w32[1];
shabal_p(ctx, buffer);
t = ctx->c;
ctx->c = ctx->b;
ctx->b = t;
}
}
void shabal_ctx2hash(void *dest, const shabal_ctx_t *ctx, uint16_t outlength_b){
2009-04-20 12:35:04 +00:00
memcpy(dest, &(ctx->c[16-outlength_b/32]), outlength_b/8);
}