mirror of
https://github.com/freebsd/freebsd-src
synced 2024-10-15 04:43:53 +00:00
rijndael-alg-fst.[ch]:
- redo updating. rijndael-api-fst.[ch]: - switch to use new low level rijndael api. - stop using u8, u16 and u32. - space cleanup. Tested by: gbde(8) and phk's test program
This commit is contained in:
parent
f3761deee3
commit
5129dcfce7
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=122410
File diff suppressed because it is too large
Load diff
|
@ -1,34 +1,40 @@
|
|||
/* $FreeBSD$ */
|
||||
/* $KAME: rijndael-alg-fst.h,v 1.4 2000/10/02 17:14:26 itojun Exp $ */
|
||||
|
||||
/*
|
||||
* rijndael-alg-fst.h v2.3 April '2000
|
||||
/* $KAME: rijndael-alg-fst.h,v 1.5 2003/07/15 10:47:16 itojun Exp $ */
|
||||
/**
|
||||
* rijndael-alg-fst.h
|
||||
*
|
||||
* Optimised ANSI C code
|
||||
* @version 3.0 (December 2000)
|
||||
*
|
||||
* #define INTERMEDIATE_VALUE_KAT to generate the Intermediate Value Known Answer Test.
|
||||
* Optimised ANSI C code for the Rijndael cipher (now AES)
|
||||
*
|
||||
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
|
||||
* @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
|
||||
* @author Paulo Barreto <paulo.barreto@terra.com.br>
|
||||
*
|
||||
* This code is hereby placed in the public domain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __RIJNDAEL_ALG_FST_H
|
||||
#define __RIJNDAEL_ALG_FST_H
|
||||
|
||||
#define RIJNDAEL_MAXKC (256/32)
|
||||
#define RIJNDAEL_MAXROUNDS 14
|
||||
#define RIJNDAEL_MAXKC (256/32)
|
||||
#define RIJNDAEL_MAXKB (256/8)
|
||||
#define RIJNDAEL_MAXNR 14
|
||||
|
||||
int rijndaelKeySched(u_int8_t k[RIJNDAEL_MAXKC][4], u_int8_t rk[RIJNDAEL_MAXROUNDS+1][4][4], int ROUNDS);
|
||||
|
||||
int rijndaelKeyEncToDec(u_int8_t W[RIJNDAEL_MAXROUNDS+1][4][4], int ROUNDS);
|
||||
|
||||
int rijndaelEncrypt(u_int8_t a[16], u_int8_t b[16], u_int8_t rk[RIJNDAEL_MAXROUNDS+1][4][4], int ROUNDS);
|
||||
|
||||
#ifdef INTERMEDIATE_VALUE_KAT
|
||||
int rijndaelEncryptRound(u_int8_t a[4][4], u_int8_t rk[RIJNDAEL_MAXROUNDS+1][4][4], int ROUNDS, int rounds);
|
||||
#endif /* INTERMEDIATE_VALUE_KAT */
|
||||
|
||||
int rijndaelDecrypt(u_int8_t a[16], u_int8_t b[16], u_int8_t rk[RIJNDAEL_MAXROUNDS+1][4][4], int ROUNDS);
|
||||
|
||||
#ifdef INTERMEDIATE_VALUE_KAT
|
||||
int rijndaelDecryptRound(u_int8_t a[4][4], u_int8_t rk[RIJNDAEL_MAXROUNDS+1][4][4], int ROUNDS, int rounds);
|
||||
#endif /* INTERMEDIATE_VALUE_KAT */
|
||||
int rijndaelKeySetupEnc(u_int32_t rk[/*4*(Nr + 1)*/], const u_int8_t cipherKey[], int keyBits);
|
||||
int rijndaelKeySetupDec(u_int32_t rk[/*4*(Nr + 1)*/], const u_int8_t cipherKey[], int keyBits);
|
||||
void rijndaelEncrypt(const u_int32_t rk[/*4*(Nr + 1)*/], int Nr, const u_int8_t pt[16], u_int8_t ct[16]);
|
||||
void rijndaelDecrypt(const u_int32_t rk[/*4*(Nr + 1)*/], int Nr, const u_int8_t ct[16], u_int8_t pt[16]);
|
||||
|
||||
#endif /* __RIJNDAEL_ALG_FST_H */
|
||||
|
|
|
@ -19,15 +19,15 @@
|
|||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#ifdef _KERNEL
|
||||
#include <sys/systm.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#include <crypto/rijndael/rijndael_local.h>
|
||||
#include <crypto/rijndael/rijndael-alg-fst.h>
|
||||
#include <crypto/rijndael/rijndael-api-fst.h>
|
||||
#include <crypto/rijndael/rijndael_local.h>
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
|
@ -36,10 +36,8 @@ __FBSDID("$FreeBSD$");
|
|||
typedef u_int8_t BYTE;
|
||||
|
||||
int rijndael_makeKey(keyInstance *key, BYTE direction, int keyLen, char *keyMaterial) {
|
||||
u8 k[RIJNDAEL_MAXKC][4];
|
||||
int i;
|
||||
char *keyMat;
|
||||
|
||||
u_int8_t cipherKey[RIJNDAEL_MAXKB];
|
||||
|
||||
if (key == NULL) {
|
||||
return BAD_KEY_INSTANCE;
|
||||
}
|
||||
|
@ -50,28 +48,24 @@ int rijndael_makeKey(keyInstance *key, BYTE direction, int keyLen, char *keyMate
|
|||
return BAD_KEY_DIR;
|
||||
}
|
||||
|
||||
if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) {
|
||||
if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) {
|
||||
key->keyLen = keyLen;
|
||||
} else {
|
||||
return BAD_KEY_MAT;
|
||||
}
|
||||
|
||||
if (keyMaterial != NULL) {
|
||||
bcopy(keyMaterial, key->keyMaterial, keyLen/8);
|
||||
memcpy(key->keyMaterial, keyMaterial, keyLen/8);
|
||||
}
|
||||
|
||||
key->ROUNDS = keyLen/32 + 6;
|
||||
|
||||
/* initialize key schedule: */
|
||||
keyMat = key->keyMaterial;
|
||||
for (i = 0; i < key->keyLen/8; i++) {
|
||||
k[i >> 2][i & 3] = (u8)keyMat[i];
|
||||
memcpy(cipherKey, key->keyMaterial, keyLen/8);
|
||||
if (direction == DIR_ENCRYPT) {
|
||||
key->Nr = rijndaelKeySetupEnc(key->rk, cipherKey, keyLen);
|
||||
} else {
|
||||
key->Nr = rijndaelKeySetupDec(key->rk, cipherKey, keyLen);
|
||||
}
|
||||
rijndaelKeySched(k, key->keySched, key->ROUNDS);
|
||||
if (direction == DIR_DECRYPT) {
|
||||
rijndaelKeyEncToDec(key->keySched, key->ROUNDS);
|
||||
}
|
||||
|
||||
rijndaelKeySetupEnc(key->ek, cipherKey, keyLen);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@ -82,9 +76,9 @@ int rijndael_cipherInit(cipherInstance *cipher, BYTE mode, char *IV) {
|
|||
return BAD_CIPHER_MODE;
|
||||
}
|
||||
if (IV != NULL) {
|
||||
bcopy(IV, cipher->IV, MAX_IV_SIZE);
|
||||
memcpy(cipher->IV, IV, RIJNDAEL_MAX_IV_SIZE);
|
||||
} else {
|
||||
bzero(cipher->IV, MAX_IV_SIZE);
|
||||
memset(cipher->IV, 0, RIJNDAEL_MAX_IV_SIZE);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
@ -92,7 +86,7 @@ int rijndael_cipherInit(cipherInstance *cipher, BYTE mode, char *IV) {
|
|||
int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
|
||||
BYTE *input, int inputLen, BYTE *outBuffer) {
|
||||
int i, k, numBlocks;
|
||||
u8 block[16], iv[4][4];
|
||||
u_int8_t block[16], iv[4][4];
|
||||
|
||||
if (cipher == NULL ||
|
||||
key == NULL ||
|
||||
|
@ -104,68 +98,69 @@ int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
|
|||
}
|
||||
|
||||
numBlocks = inputLen/128;
|
||||
|
||||
|
||||
switch (cipher->mode) {
|
||||
case MODE_ECB:
|
||||
case MODE_ECB:
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
rijndaelEncrypt(input, outBuffer, key->keySched, key->ROUNDS);
|
||||
rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case MODE_CBC:
|
||||
#if 1 /*STRICT_ALIGN*/
|
||||
bcopy(cipher->IV, block, 16);
|
||||
bcopy(input, iv, 16);
|
||||
((u32*)block)[0] ^= ((u32*)iv)[0];
|
||||
((u32*)block)[1] ^= ((u32*)iv)[1];
|
||||
((u32*)block)[2] ^= ((u32*)iv)[2];
|
||||
((u32*)block)[3] ^= ((u32*)iv)[3];
|
||||
memcpy(block, cipher->IV, 16);
|
||||
memcpy(iv, input, 16);
|
||||
((u_int32_t*)block)[0] ^= ((u_int32_t*)iv)[0];
|
||||
((u_int32_t*)block)[1] ^= ((u_int32_t*)iv)[1];
|
||||
((u_int32_t*)block)[2] ^= ((u_int32_t*)iv)[2];
|
||||
((u_int32_t*)block)[3] ^= ((u_int32_t*)iv)[3];
|
||||
#else
|
||||
((u32*)block)[0] = ((u32*)cipher->IV)[0] ^ ((u32*)input)[0];
|
||||
((u32*)block)[1] = ((u32*)cipher->IV)[1] ^ ((u32*)input)[1];
|
||||
((u32*)block)[2] = ((u32*)cipher->IV)[2] ^ ((u32*)input)[2];
|
||||
((u32*)block)[3] = ((u32*)cipher->IV)[3] ^ ((u32*)input)[3];
|
||||
((u_int32_t*)block)[0] = ((u_int32_t*)cipher->IV)[0] ^ ((u_int32_t*)input)[0];
|
||||
((u_int32_t*)block)[1] = ((u_int32_t*)cipher->IV)[1] ^ ((u_int32_t*)input)[1];
|
||||
((u_int32_t*)block)[2] = ((u_int32_t*)cipher->IV)[2] ^ ((u_int32_t*)input)[2];
|
||||
((u_int32_t*)block)[3] = ((u_int32_t*)cipher->IV)[3] ^ ((u_int32_t*)input)[3];
|
||||
#endif
|
||||
rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
|
||||
rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
|
||||
input += 16;
|
||||
for (i = numBlocks - 1; i > 0; i--) {
|
||||
#if 1 /*STRICT_ALIGN*/
|
||||
bcopy(outBuffer, block, 16);
|
||||
bcopy(input, iv, 16);
|
||||
((u32*)block)[0] ^= ((u32*)iv)[0];
|
||||
((u32*)block)[1] ^= ((u32*)iv)[1];
|
||||
((u32*)block)[2] ^= ((u32*)iv)[2];
|
||||
((u32*)block)[3] ^= ((u32*)iv)[3];
|
||||
memcpy(block, outBuffer, 16);
|
||||
memcpy(iv, input, 16);
|
||||
((u_int32_t*)block)[0] ^= ((u_int32_t*)iv)[0];
|
||||
((u_int32_t*)block)[1] ^= ((u_int32_t*)iv)[1];
|
||||
((u_int32_t*)block)[2] ^= ((u_int32_t*)iv)[2];
|
||||
((u_int32_t*)block)[3] ^= ((u_int32_t*)iv)[3];
|
||||
#else
|
||||
((u32*)block)[0] = ((u32*)outBuffer)[0] ^ ((u32*)input)[0];
|
||||
((u32*)block)[1] = ((u32*)outBuffer)[1] ^ ((u32*)input)[1];
|
||||
((u32*)block)[2] = ((u32*)outBuffer)[2] ^ ((u32*)input)[2];
|
||||
((u32*)block)[3] = ((u32*)outBuffer)[3] ^ ((u32*)input)[3];
|
||||
((u_int32_t*)block)[0] = ((u_int32_t*)outBuffer)[0] ^ ((u_int32_t*)input)[0];
|
||||
((u_int32_t*)block)[1] = ((u_int32_t*)outBuffer)[1] ^ ((u_int32_t*)input)[1];
|
||||
((u_int32_t*)block)[2] = ((u_int32_t*)outBuffer)[2] ^ ((u_int32_t*)input)[2];
|
||||
((u_int32_t*)block)[3] = ((u_int32_t*)outBuffer)[3] ^ ((u_int32_t*)input)[3];
|
||||
#endif
|
||||
outBuffer += 16;
|
||||
rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
|
||||
rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
|
||||
input += 16;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case MODE_CFB1:
|
||||
#if 1 /*STRICT_ALIGN*/
|
||||
bcopy(cipher->IV, iv, 16);
|
||||
memcpy(iv, cipher->IV, 16);
|
||||
#else /* !STRICT_ALIGN */
|
||||
*((u32*)iv[0]) = *((u32*)(cipher->IV ));
|
||||
*((u32*)iv[1]) = *((u32*)(cipher->IV+ 4));
|
||||
*((u32*)iv[2]) = *((u32*)(cipher->IV+ 8));
|
||||
*((u32*)iv[3]) = *((u32*)(cipher->IV+12));
|
||||
*((u_int32_t*)iv[0]) = *((u_int32_t*)(cipher->IV ));
|
||||
*((u_int32_t*)iv[1]) = *((u_int32_t*)(cipher->IV+ 4));
|
||||
*((u_int32_t*)iv[2]) = *((u_int32_t*)(cipher->IV+ 8));
|
||||
*((u_int32_t*)iv[3]) = *((u_int32_t*)(cipher->IV+12));
|
||||
#endif /* ?STRICT_ALIGN */
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
for (k = 0; k < 128; k++) {
|
||||
*((u32*) block ) = *((u32*)iv[0]);
|
||||
*((u32*)(block+ 4)) = *((u32*)iv[1]);
|
||||
*((u32*)(block+ 8)) = *((u32*)iv[2]);
|
||||
*((u32*)(block+12)) = *((u32*)iv[3]);
|
||||
rijndaelEncrypt(block, block, key->keySched, key->ROUNDS);
|
||||
*((u_int32_t*) block ) = *((u_int32_t*)iv[0]);
|
||||
*((u_int32_t*)(block+ 4)) = *((u_int32_t*)iv[1]);
|
||||
*((u_int32_t*)(block+ 8)) = *((u_int32_t*)iv[2]);
|
||||
*((u_int32_t*)(block+12)) = *((u_int32_t*)iv[3]);
|
||||
rijndaelEncrypt(key->ek, key->Nr, block,
|
||||
block);
|
||||
outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7);
|
||||
iv[0][0] = (iv[0][0] << 1) | (iv[0][1] >> 7);
|
||||
iv[0][1] = (iv[0][1] << 1) | (iv[0][2] >> 7);
|
||||
|
@ -186,11 +181,11 @@ int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
|
|||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
return BAD_CIPHER_STATE;
|
||||
}
|
||||
|
||||
|
||||
return 128*numBlocks;
|
||||
}
|
||||
|
||||
|
@ -206,7 +201,7 @@ int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
|
|||
int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
|
||||
BYTE *input, int inputOctets, BYTE *outBuffer) {
|
||||
int i, numBlocks, padLen;
|
||||
u8 block[16], *iv, *cp;
|
||||
u_int8_t block[16], *iv, *cp;
|
||||
|
||||
if (cipher == NULL ||
|
||||
key == NULL ||
|
||||
|
@ -220,29 +215,29 @@ int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
|
|||
numBlocks = inputOctets/16;
|
||||
|
||||
switch (cipher->mode) {
|
||||
case MODE_ECB:
|
||||
case MODE_ECB:
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
rijndaelEncrypt(input, outBuffer, key->keySched, key->ROUNDS);
|
||||
rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
}
|
||||
padLen = 16 - (inputOctets - 16*numBlocks);
|
||||
if (padLen <= 0 || padLen > 16)
|
||||
return BAD_CIPHER_STATE;
|
||||
bcopy(input, block, 16 - padLen);
|
||||
memcpy(block, input, 16 - padLen);
|
||||
for (cp = block + 16 - padLen; cp < block + 16; cp++)
|
||||
*cp = padLen;
|
||||
rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
|
||||
rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
|
||||
break;
|
||||
|
||||
case MODE_CBC:
|
||||
iv = cipher->IV;
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
((u32*)block)[0] = ((u32*)input)[0] ^ ((u32*)iv)[0];
|
||||
((u32*)block)[1] = ((u32*)input)[1] ^ ((u32*)iv)[1];
|
||||
((u32*)block)[2] = ((u32*)input)[2] ^ ((u32*)iv)[2];
|
||||
((u32*)block)[3] = ((u32*)input)[3] ^ ((u32*)iv)[3];
|
||||
rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
|
||||
((u_int32_t*)block)[0] = ((u_int32_t*)input)[0] ^ ((u_int32_t*)iv)[0];
|
||||
((u_int32_t*)block)[1] = ((u_int32_t*)input)[1] ^ ((u_int32_t*)iv)[1];
|
||||
((u_int32_t*)block)[2] = ((u_int32_t*)input)[2] ^ ((u_int32_t*)iv)[2];
|
||||
((u_int32_t*)block)[3] = ((u_int32_t*)input)[3] ^ ((u_int32_t*)iv)[3];
|
||||
rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
|
||||
iv = outBuffer;
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
|
@ -256,7 +251,7 @@ int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
|
|||
for (i = 16 - padLen; i < 16; i++) {
|
||||
block[i] = (BYTE)padLen ^ iv[i];
|
||||
}
|
||||
rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
|
||||
rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -269,7 +264,7 @@ int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
|
|||
int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
|
||||
BYTE *input, int inputLen, BYTE *outBuffer) {
|
||||
int i, k, numBlocks;
|
||||
u8 block[16], iv[4][4];
|
||||
u_int8_t block[16], iv[4][4];
|
||||
|
||||
if (cipher == NULL ||
|
||||
key == NULL ||
|
||||
|
@ -283,59 +278,60 @@ int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
|
|||
numBlocks = inputLen/128;
|
||||
|
||||
switch (cipher->mode) {
|
||||
case MODE_ECB:
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
rijndaelDecrypt(input, outBuffer, key->keySched, key->ROUNDS);
|
||||
case MODE_ECB:
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
rijndaelDecrypt(key->rk, key->Nr, input, outBuffer);
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case MODE_CBC:
|
||||
#if 1 /*STRICT_ALIGN */
|
||||
bcopy(cipher->IV, iv, 16);
|
||||
memcpy(iv, cipher->IV, 16);
|
||||
#else
|
||||
*((u32*)iv[0]) = *((u32*)(cipher->IV ));
|
||||
*((u32*)iv[1]) = *((u32*)(cipher->IV+ 4));
|
||||
*((u32*)iv[2]) = *((u32*)(cipher->IV+ 8));
|
||||
*((u32*)iv[3]) = *((u32*)(cipher->IV+12));
|
||||
*((u_int32_t*)iv[0]) = *((u_int32_t*)(cipher->IV ));
|
||||
*((u_int32_t*)iv[1]) = *((u_int32_t*)(cipher->IV+ 4));
|
||||
*((u_int32_t*)iv[2]) = *((u_int32_t*)(cipher->IV+ 8));
|
||||
*((u_int32_t*)iv[3]) = *((u_int32_t*)(cipher->IV+12));
|
||||
#endif
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
rijndaelDecrypt(input, block, key->keySched, key->ROUNDS);
|
||||
((u32*)block)[0] ^= *((u32*)iv[0]);
|
||||
((u32*)block)[1] ^= *((u32*)iv[1]);
|
||||
((u32*)block)[2] ^= *((u32*)iv[2]);
|
||||
((u32*)block)[3] ^= *((u32*)iv[3]);
|
||||
rijndaelDecrypt(key->rk, key->Nr, input, block);
|
||||
((u_int32_t*)block)[0] ^= *((u_int32_t*)iv[0]);
|
||||
((u_int32_t*)block)[1] ^= *((u_int32_t*)iv[1]);
|
||||
((u_int32_t*)block)[2] ^= *((u_int32_t*)iv[2]);
|
||||
((u_int32_t*)block)[3] ^= *((u_int32_t*)iv[3]);
|
||||
#if 1 /*STRICT_ALIGN*/
|
||||
bcopy(input, iv, 16);
|
||||
bcopy(block, outBuffer, 16);
|
||||
memcpy(iv, input, 16);
|
||||
memcpy(outBuffer, block, 16);
|
||||
#else
|
||||
*((u32*)iv[0]) = ((u32*)input)[0]; ((u32*)outBuffer)[0] = ((u32*)block)[0];
|
||||
*((u32*)iv[1]) = ((u32*)input)[1]; ((u32*)outBuffer)[1] = ((u32*)block)[1];
|
||||
*((u32*)iv[2]) = ((u32*)input)[2]; ((u32*)outBuffer)[2] = ((u32*)block)[2];
|
||||
*((u32*)iv[3]) = ((u32*)input)[3]; ((u32*)outBuffer)[3] = ((u32*)block)[3];
|
||||
*((u_int32_t*)iv[0]) = ((u_int32_t*)input)[0]; ((u_int32_t*)outBuffer)[0] = ((u_int32_t*)block)[0];
|
||||
*((u_int32_t*)iv[1]) = ((u_int32_t*)input)[1]; ((u_int32_t*)outBuffer)[1] = ((u_int32_t*)block)[1];
|
||||
*((u_int32_t*)iv[2]) = ((u_int32_t*)input)[2]; ((u_int32_t*)outBuffer)[2] = ((u_int32_t*)block)[2];
|
||||
*((u_int32_t*)iv[3]) = ((u_int32_t*)input)[3]; ((u_int32_t*)outBuffer)[3] = ((u_int32_t*)block)[3];
|
||||
#endif
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case MODE_CFB1:
|
||||
#if 1 /*STRICT_ALIGN */
|
||||
bcopy(cipher->IV, iv, 16);
|
||||
memcpy(iv, cipher->IV, 16);
|
||||
#else
|
||||
*((u32*)iv[0]) = *((u32*)(cipher->IV));
|
||||
*((u32*)iv[1]) = *((u32*)(cipher->IV+ 4));
|
||||
*((u32*)iv[2]) = *((u32*)(cipher->IV+ 8));
|
||||
*((u32*)iv[3]) = *((u32*)(cipher->IV+12));
|
||||
*((u_int32_t*)iv[0]) = *((u_int32_t*)(cipher->IV));
|
||||
*((u_int32_t*)iv[1]) = *((u_int32_t*)(cipher->IV+ 4));
|
||||
*((u_int32_t*)iv[2]) = *((u_int32_t*)(cipher->IV+ 8));
|
||||
*((u_int32_t*)iv[3]) = *((u_int32_t*)(cipher->IV+12));
|
||||
#endif
|
||||
for (i = numBlocks; i > 0; i--) {
|
||||
for (k = 0; k < 128; k++) {
|
||||
*((u32*) block ) = *((u32*)iv[0]);
|
||||
*((u32*)(block+ 4)) = *((u32*)iv[1]);
|
||||
*((u32*)(block+ 8)) = *((u32*)iv[2]);
|
||||
*((u32*)(block+12)) = *((u32*)iv[3]);
|
||||
rijndaelEncrypt(block, block, key->keySched, key->ROUNDS);
|
||||
*((u_int32_t*) block ) = *((u_int32_t*)iv[0]);
|
||||
*((u_int32_t*)(block+ 4)) = *((u_int32_t*)iv[1]);
|
||||
*((u_int32_t*)(block+ 8)) = *((u_int32_t*)iv[2]);
|
||||
*((u_int32_t*)(block+12)) = *((u_int32_t*)iv[3]);
|
||||
rijndaelEncrypt(key->ek, key->Nr, block,
|
||||
block);
|
||||
iv[0][0] = (iv[0][0] << 1) | (iv[0][1] >> 7);
|
||||
iv[0][1] = (iv[0][1] << 1) | (iv[0][2] >> 7);
|
||||
iv[0][2] = (iv[0][2] << 1) | (iv[0][3] >> 7);
|
||||
|
@ -360,15 +356,15 @@ int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
|
|||
default:
|
||||
return BAD_CIPHER_STATE;
|
||||
}
|
||||
|
||||
|
||||
return 128*numBlocks;
|
||||
}
|
||||
|
||||
int rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
|
||||
BYTE *input, int inputOctets, BYTE *outBuffer) {
|
||||
int i, numBlocks, padLen;
|
||||
u8 block[16];
|
||||
u32 iv[4];
|
||||
u_int8_t block[16];
|
||||
u_int32_t iv[4];
|
||||
|
||||
if (cipher == NULL ||
|
||||
key == NULL ||
|
||||
|
@ -387,13 +383,13 @@ int rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
|
|||
switch (cipher->mode) {
|
||||
case MODE_ECB:
|
||||
/* all blocks but last */
|
||||
for (i = numBlocks - 1; i > 0; i--) {
|
||||
rijndaelDecrypt(input, outBuffer, key->keySched, key->ROUNDS);
|
||||
for (i = numBlocks - 1; i > 0; i--) {
|
||||
rijndaelDecrypt(key->rk, key->Nr, input, outBuffer);
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
}
|
||||
/* last block */
|
||||
rijndaelDecrypt(input, block, key->keySched, key->ROUNDS);
|
||||
rijndaelDecrypt(key->rk, key->Nr, input, block);
|
||||
padLen = block[15];
|
||||
if (padLen >= 16) {
|
||||
return BAD_DATA;
|
||||
|
@ -403,29 +399,29 @@ int rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
|
|||
return BAD_DATA;
|
||||
}
|
||||
}
|
||||
bcopy(block, outBuffer, 16 - padLen);
|
||||
memcpy(outBuffer, block, 16 - padLen);
|
||||
break;
|
||||
|
||||
|
||||
case MODE_CBC:
|
||||
bcopy(cipher->IV, iv, 16);
|
||||
memcpy(iv, cipher->IV, 16);
|
||||
/* all blocks but last */
|
||||
for (i = numBlocks - 1; i > 0; i--) {
|
||||
rijndaelDecrypt(input, block, key->keySched, key->ROUNDS);
|
||||
((u32*)block)[0] ^= iv[0];
|
||||
((u32*)block)[1] ^= iv[1];
|
||||
((u32*)block)[2] ^= iv[2];
|
||||
((u32*)block)[3] ^= iv[3];
|
||||
bcopy(input, iv, 16);
|
||||
bcopy(block, outBuffer, 16);
|
||||
rijndaelDecrypt(key->rk, key->Nr, input, block);
|
||||
((u_int32_t*)block)[0] ^= iv[0];
|
||||
((u_int32_t*)block)[1] ^= iv[1];
|
||||
((u_int32_t*)block)[2] ^= iv[2];
|
||||
((u_int32_t*)block)[3] ^= iv[3];
|
||||
memcpy(iv, input, 16);
|
||||
memcpy(outBuffer, block, 16);
|
||||
input += 16;
|
||||
outBuffer += 16;
|
||||
}
|
||||
/* last block */
|
||||
rijndaelDecrypt(input, block, key->keySched, key->ROUNDS);
|
||||
((u32*)block)[0] ^= iv[0];
|
||||
((u32*)block)[1] ^= iv[1];
|
||||
((u32*)block)[2] ^= iv[2];
|
||||
((u32*)block)[3] ^= iv[3];
|
||||
rijndaelDecrypt(key->rk, key->Nr, input, block);
|
||||
((u_int32_t*)block)[0] ^= iv[0];
|
||||
((u_int32_t*)block)[1] ^= iv[1];
|
||||
((u_int32_t*)block)[2] ^= iv[2];
|
||||
((u_int32_t*)block)[3] ^= iv[3];
|
||||
padLen = block[15];
|
||||
if (padLen <= 0 || padLen > 16) {
|
||||
return BAD_DATA;
|
||||
|
@ -435,59 +431,12 @@ int rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
|
|||
return BAD_DATA;
|
||||
}
|
||||
}
|
||||
bcopy(block, outBuffer, 16 - padLen);
|
||||
memcpy(outBuffer, block, 16 - padLen);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
return BAD_CIPHER_STATE;
|
||||
}
|
||||
|
||||
|
||||
return 16*numBlocks - padLen;
|
||||
}
|
||||
|
||||
#ifdef INTERMEDIATE_VALUE_KAT
|
||||
/**
|
||||
* cipherUpdateRounds:
|
||||
*
|
||||
* Encrypts/Decrypts exactly one full block a specified number of rounds.
|
||||
* Only used in the Intermediate Value Known Answer Test.
|
||||
*
|
||||
* Returns:
|
||||
* TRUE - on success
|
||||
* BAD_CIPHER_STATE - cipher in bad state (e.g., not initialized)
|
||||
*/
|
||||
int rijndael_cipherUpdateRounds(cipherInstance *cipher, keyInstance *key,
|
||||
BYTE *input, int inputLen, BYTE *outBuffer, int rounds) {
|
||||
int j;
|
||||
u8 block[4][4];
|
||||
|
||||
if (cipher == NULL || key == NULL) {
|
||||
return BAD_CIPHER_STATE;
|
||||
}
|
||||
|
||||
for (j = 3; j >= 0; j--) {
|
||||
/* parse input stream into rectangular array */
|
||||
*((u32*)block[j]) = *((u32*)(input+4*j));
|
||||
}
|
||||
|
||||
switch (key->direction) {
|
||||
case DIR_ENCRYPT:
|
||||
rijndaelEncryptRound(block, key->keySched, key->ROUNDS, rounds);
|
||||
break;
|
||||
|
||||
case DIR_DECRYPT:
|
||||
rijndaelDecryptRound(block, key->keySched, key->ROUNDS, rounds);
|
||||
break;
|
||||
|
||||
default:
|
||||
return BAD_KEY_DIR;
|
||||
}
|
||||
|
||||
for (j = 3; j >= 0; j--) {
|
||||
/* parse rectangular array into output ciphertext bytes */
|
||||
*((u32*)(outBuffer+4*j)) = *((u32*)block[j]);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
#endif /* INTERMEDIATE_VALUE_KAT */
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*
|
||||
* Optimised ANSI C code
|
||||
*
|
||||
* #define INTERMEDIATE_VALUE_KAT to generate the Intermediate Value Known Answer Test.
|
||||
*/
|
||||
|
||||
#ifndef __RIJNDAEL_API_FST_H
|
||||
|
@ -14,10 +13,7 @@
|
|||
|
||||
#include <crypto/rijndael/rijndael-alg-fst.h>
|
||||
|
||||
/* Defines:
|
||||
Add any additional defines you need
|
||||
*/
|
||||
|
||||
/* Generic Defines */
|
||||
#define DIR_ENCRYPT 0 /* Are we encrpyting? */
|
||||
#define DIR_DECRYPT 1 /* Are we decrpyting? */
|
||||
#define MODE_ECB 1 /* Are we ciphering in ECB mode? */
|
||||
|
@ -25,7 +21,7 @@
|
|||
#define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */
|
||||
#define BITSPERBLOCK 128 /* Default number of bits in a cipher block */
|
||||
|
||||
/* Error Codes - CHANGE POSSIBLE: inclusion of additional error codes */
|
||||
/* Error Codes */
|
||||
#define BAD_KEY_DIR -1 /* Key direction is invalid, e.g., unknown value */
|
||||
#define BAD_KEY_MAT -2 /* Key material not of correct length */
|
||||
#define BAD_KEY_INSTANCE -3 /* Key passed is not valid */
|
||||
|
@ -36,67 +32,42 @@
|
|||
#define BAD_DATA -8 /* Data contents are invalid, e.g., invalid padding */
|
||||
#define BAD_OTHER -9 /* Unknown error */
|
||||
|
||||
/* CHANGE POSSIBLE: inclusion of algorithm specific defines */
|
||||
#define MAX_KEY_SIZE 64 /* # of ASCII char's needed to represent a key */
|
||||
#define MAX_IV_SIZE 16 /* # bytes needed to represent an IV */
|
||||
/* Algorithm-specific Defines */
|
||||
#define RIJNDAEL_MAX_KEY_SIZE 64 /* # of ASCII char's needed to represent a key */
|
||||
#define RIJNDAEL_MAX_IV_SIZE 16 /* # bytes needed to represent an IV */
|
||||
|
||||
/* Typedefs:
|
||||
|
||||
Typedef'ed data storage elements. Add any algorithm specific
|
||||
parameters at the bottom of the structs as appropriate.
|
||||
*/
|
||||
/* Typedefs */
|
||||
|
||||
/* The structure for key information */
|
||||
typedef struct {
|
||||
u_int8_t direction; /* Key used for encrypting or decrypting? */
|
||||
int keyLen; /* Length of the key */
|
||||
char keyMaterial[MAX_KEY_SIZE+1]; /* Raw key data in ASCII, e.g., user input or KAT values */
|
||||
/* The following parameters are algorithm dependent, replace or add as necessary */
|
||||
int ROUNDS; /* key-length-dependent number of rounds */
|
||||
int blockLen; /* block length */
|
||||
union {
|
||||
u_int8_t xkS8[RIJNDAEL_MAXROUNDS+1][4][4]; /* key schedule */
|
||||
u_int32_t xkS32[RIJNDAEL_MAXROUNDS+1][4]; /* key schedule */
|
||||
} xKeySched;
|
||||
#define keySched xKeySched.xkS8
|
||||
char keyMaterial[RIJNDAEL_MAX_KEY_SIZE+1]; /* Raw key data in ASCII, e.g., user input or KAT values */
|
||||
int Nr; /* key-length-dependent number of rounds */
|
||||
u_int32_t rk[4*(RIJNDAEL_MAXNR + 1)]; /* key schedule */
|
||||
u_int32_t ek[4*(RIJNDAEL_MAXNR + 1)]; /* CFB1 key schedule (encryption only) */
|
||||
} keyInstance;
|
||||
|
||||
/* The structure for cipher information */
|
||||
typedef struct { /* changed order of the components */
|
||||
u_int8_t mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
|
||||
u_int8_t IV[MAX_IV_SIZE]; /* A possible Initialization Vector for ciphering */
|
||||
/* Add any algorithm specific parameters needed here */
|
||||
int blockLen; /* Sample: Handles non-128 bit block sizes (if available) */
|
||||
u_int8_t IV[RIJNDAEL_MAX_IV_SIZE]; /* A possible Initialization Vector for ciphering */
|
||||
} cipherInstance;
|
||||
|
||||
/* Function prototypes */
|
||||
/* CHANGED: nothing
|
||||
TODO: implement the following extensions to setup 192-bit and 256-bit block lengths:
|
||||
makeKeyEx(): parameter blockLen added
|
||||
-- this parameter is absolutely necessary if you want to
|
||||
setup the round keys in a variable block length setting
|
||||
cipherInitEx(): parameter blockLen added (for obvious reasons)
|
||||
*/
|
||||
|
||||
int rijndael_makeKey(keyInstance *key, u_int8_t direction, int keyLen, char *keyMaterial);
|
||||
int rijndael_makeKey(keyInstance *, u_int8_t, int, char *);
|
||||
|
||||
int rijndael_cipherInit(cipherInstance *cipher, u_int8_t mode, char *IV);
|
||||
int rijndael_cipherInit(cipherInstance *, u_int8_t, char *);
|
||||
|
||||
int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
|
||||
u_int8_t *input, int inputLen, u_int8_t *outBuffer);
|
||||
int rijndael_blockEncrypt(cipherInstance *, keyInstance *, u_int8_t *, int,
|
||||
u_int8_t *);
|
||||
int rijndael_padEncrypt(cipherInstance *, keyInstance *, u_int8_t *, int,
|
||||
u_int8_t *);
|
||||
|
||||
int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
|
||||
u_int8_t *input, int inputOctets, u_int8_t *outBuffer);
|
||||
|
||||
int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
|
||||
u_int8_t *input, int inputLen, u_int8_t *outBuffer);
|
||||
|
||||
int rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
|
||||
u_int8_t *input, int inputOctets, u_int8_t *outBuffer);
|
||||
|
||||
#ifdef INTERMEDIATE_VALUE_KAT
|
||||
int rijndael_cipherUpdateRounds(cipherInstance *cipher, keyInstance *key,
|
||||
u_int8_t *input, int inputLen, u_int8_t *outBuffer, int Rounds);
|
||||
#endif /* INTERMEDIATE_VALUE_KAT */
|
||||
int rijndael_blockDecrypt(cipherInstance *, keyInstance *, u_int8_t *, int,
|
||||
u_int8_t *);
|
||||
int rijndael_padDecrypt(cipherInstance *, keyInstance *, u_int8_t *, int,
|
||||
u_int8_t *);
|
||||
|
||||
#endif /* __RIJNDAEL_API_FST_H */
|
||||
|
|
Loading…
Reference in a new issue