2008-05-26 19:13:21 +00:00
|
|
|
/* shabea.c */
|
|
|
|
/*
|
2008-09-21 21:22:23 +00:00
|
|
|
* This file is part of AnonAccess, an access system which can be used
|
|
|
|
* to open door or doing other things with an anonymity featured
|
|
|
|
* account managment.
|
|
|
|
* Copyright (C) 2006, 2007, 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-05-26 19:13:21 +00:00
|
|
|
|
2007-06-18 04:50:39 +00:00
|
|
|
/**
|
|
|
|
* \file shabea.c
|
|
|
|
* \author Daniel Otte
|
|
|
|
* \date 2007-06-07
|
2008-04-16 01:02:42 +00:00
|
|
|
* \brief SHABEA - a SHA Based Encryption Algorithm implementation
|
2007-06-18 04:50:39 +00:00
|
|
|
* \par License
|
|
|
|
* GPL
|
|
|
|
*
|
|
|
|
* SHABEAn-r where n is the blocksize and r the number of round used
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "sha256.h"
|
|
|
|
|
|
|
|
#include "config.h"
|
2008-07-21 02:40:18 +00:00
|
|
|
#include "memxor.h"
|
|
|
|
|
2007-06-18 04:50:39 +00:00
|
|
|
|
|
|
|
/*
|
2007-12-20 02:15:53 +00:00
|
|
|
* SHABEA256-n
|
2007-06-18 04:50:39 +00:00
|
|
|
*/
|
2007-12-20 02:15:53 +00:00
|
|
|
|
2008-09-21 21:22:23 +00:00
|
|
|
#define SHABEA_BLOCKSIZE 256
|
|
|
|
#define SHABEA_BLOCKSIZEB (SHABEA_BLOCKSIZE/8)
|
|
|
|
#define SHABEA_HALFSIZEB (SHABEA_BLOCKSIZEB/2)
|
|
|
|
#define SHABEA_HALFSIZE (SHABEA_BLOCKSIZE/2)
|
2007-12-20 02:15:53 +00:00
|
|
|
|
|
|
|
#define L ((uint8_t*)block+ 0)
|
|
|
|
#define R ((uint8_t*)block+16)
|
2008-07-09 05:41:43 +00:00
|
|
|
void shabea256(void * block, void * key, uint16_t keysize_b, uint8_t enc, uint8_t rounds){
|
2008-09-21 21:22:23 +00:00
|
|
|
uint8_t r; /**/
|
|
|
|
uint8_t tb[SHABEA_HALFSIZEB+2+(keysize_b+7)/8]; /**/
|
2007-06-18 04:50:39 +00:00
|
|
|
uint16_t kbs; /* bytes used for the key / temporary block */
|
|
|
|
sha256_hash_t hash;
|
2008-09-21 21:22:23 +00:00
|
|
|
uint8_t termcond;
|
|
|
|
int8_t dir;
|
|
|
|
if(enc){
|
|
|
|
r = 0;
|
|
|
|
termcond = rounds-1;
|
|
|
|
dir = 1;
|
|
|
|
} else {
|
|
|
|
r = rounds-1;
|
|
|
|
termcond = 0;
|
|
|
|
dir = -1;
|
|
|
|
}
|
2008-07-09 05:41:43 +00:00
|
|
|
kbs = (keysize_b+7)/8;
|
2008-09-21 21:22:23 +00:00
|
|
|
memcpy(tb+SHABEA_HALFSIZEB+2, key, kbs); /* copy key to temporary block */
|
|
|
|
tb[SHABEA_HALFSIZEB+0] = 0; /* set round counter high value to zero */
|
2007-06-18 04:50:39 +00:00
|
|
|
|
2008-09-21 21:22:23 +00:00
|
|
|
for(;;r+=dir){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */
|
|
|
|
memcpy(tb, R, SHABEA_HALFSIZEB); /* copy right half into tb */
|
|
|
|
tb[SHABEA_HALFSIZEB+1] = r;
|
|
|
|
sha256(&hash, tb, SHABEA_HALFSIZE+16+keysize_b);
|
|
|
|
if(r!=termcond){
|
2007-06-18 04:50:39 +00:00
|
|
|
/* swap */
|
2008-09-21 21:22:23 +00:00
|
|
|
memxor(hash, L, SHABEA_HALFSIZEB);
|
|
|
|
memcpy(L, R, SHABEA_HALFSIZEB);
|
|
|
|
memcpy(R, hash, SHABEA_HALFSIZEB);
|
2007-06-18 04:50:39 +00:00
|
|
|
} else {
|
2008-09-21 21:22:23 +00:00
|
|
|
/* last round */
|
2007-06-18 04:50:39 +00:00
|
|
|
/* no swap */
|
2008-09-21 21:22:23 +00:00
|
|
|
memxor(L, hash, SHABEA_HALFSIZEB);
|
|
|
|
return;
|
2007-06-18 04:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|