mirror of
https://github.com/rvtr/ntr_bootrom.git
synced 2025-10-31 07:11:11 -04:00
159 lines
2.7 KiB
C
159 lines
2.7 KiB
C
//*******************************************************************
|
||
// IRIS-SUBPƒ‚ƒjƒ^ƒvƒ<76>ƒOƒ‰ƒ€ BlowfishŠÖ<C5A0>”
|
||
//*******************************************************************
|
||
#include "Blowfish.h"
|
||
|
||
#define MAXKEYBYTES 56 /* 448 bits */
|
||
#define N 16
|
||
|
||
|
||
//BLOWFISH_CTX blowfishCardTable;
|
||
//BLOWFISH_CTX blowfishEepromTable;
|
||
|
||
|
||
static unsigned int F(const BLOWFISH_CTX *ctx, unsigned int x);
|
||
|
||
|
||
void InitBlowfish(BLOWFISH_CTX *ctx, const unsigned char *key, int keyLen)
|
||
{
|
||
int i, j, k;
|
||
unsigned int data, datal, datar;
|
||
|
||
#ifndef DISABLE_SECURE_CODE
|
||
|
||
j = 0;
|
||
for (i = 0; i < N + 2; ++i) {
|
||
data = 0x00000000;
|
||
for (k = 0; k < 4; ++k) {
|
||
data = (data << 8) | key[j];
|
||
j = j + 1;
|
||
if (j >= keyLen)
|
||
j = 0;
|
||
}
|
||
ctx->P[i] = ctx->P[i] ^ data;
|
||
}
|
||
|
||
datal = 0x00000000;
|
||
datar = 0x00000000;
|
||
|
||
for (i = 0; i < N + 2; i += 2) {
|
||
EncryptByBlowfish(ctx, &datal, &datar);
|
||
ctx->P[i] = datal;
|
||
ctx->P[i + 1] = datar;
|
||
}
|
||
|
||
for (i = 0; i < 4; ++i) {
|
||
for (j = 0; j < 256; j += 2) {
|
||
EncryptByBlowfish(ctx, &datal, &datar);
|
||
ctx->S[i][j] = datal;
|
||
ctx->S[i][j + 1] = datar;
|
||
}
|
||
}
|
||
|
||
#endif // DISABLE_SECURE_CODE
|
||
|
||
}
|
||
|
||
|
||
void EncryptByBlowfish(const BLOWFISH_CTX *ctx, unsigned int *xl, unsigned int *xr)
|
||
{
|
||
unsigned int Xl;
|
||
unsigned int Xr;
|
||
unsigned int temp;
|
||
int i;
|
||
|
||
#if !defined(DISABLE_SECURE_CODE) & !defined(DISABLE_ENCRYPT)
|
||
|
||
Xl = *xl;
|
||
Xr = *xr;
|
||
|
||
for (i = 0; i < N; ++i) {
|
||
Xl = Xl ^ ctx->P[i];
|
||
Xr = F(ctx, Xl) ^ Xr;
|
||
|
||
temp = Xl;
|
||
|
||
Xl = Xr;
|
||
Xr = temp;
|
||
}
|
||
|
||
temp = Xl;
|
||
Xl = Xr;
|
||
Xr = temp;
|
||
|
||
Xr = Xr ^ ctx->P[N];
|
||
Xl = Xl ^ ctx->P[N + 1];
|
||
|
||
*xl = Xl;
|
||
*xr = Xr;
|
||
|
||
#endif // DISABLE_SECURE_CODE
|
||
|
||
}
|
||
|
||
void DecryptByBlowfish(const BLOWFISH_CTX *ctx, unsigned int *xl, unsigned int *xr)
|
||
{
|
||
unsigned int Xl;
|
||
unsigned int Xr;
|
||
unsigned int temp;
|
||
int i;
|
||
|
||
#ifndef DISABLE_SECURE_CODE
|
||
|
||
Xl = *xl;
|
||
Xr = *xr;
|
||
|
||
for (i = N + 1; i > 1; --i) {
|
||
Xl = Xl ^ ctx->P[i];
|
||
Xr = F(ctx, Xl) ^ Xr;
|
||
|
||
/* Exchange Xl and Xr */
|
||
temp = Xl;
|
||
Xl = Xr;
|
||
Xr = temp;
|
||
}
|
||
|
||
/* Exchange Xl and Xr */
|
||
temp = Xl;
|
||
Xl = Xr;
|
||
Xr = temp;
|
||
|
||
Xr = Xr ^ ctx->P[1];
|
||
Xl = Xl ^ ctx->P[0];
|
||
|
||
*xl = Xl;
|
||
*xr = Xr;
|
||
|
||
#endif // DISABLE_SECURE_CODE
|
||
|
||
}
|
||
|
||
|
||
unsigned int F(const BLOWFISH_CTX *ctx, unsigned int x) {
|
||
unsigned int a, b, c, d;
|
||
int y;
|
||
|
||
#ifndef DISABLE_SECURE_CODE
|
||
|
||
d = x & 0x00FF;
|
||
x >>= 8;
|
||
|
||
c = x & 0x00FF;
|
||
x >>= 8;
|
||
|
||
b = x & 0x00FF;
|
||
x >>= 8;
|
||
|
||
a = x & 0x00FF;
|
||
|
||
y = ctx->S[0][a] + ctx->S[1][b];
|
||
|
||
y = y ^ ctx->S[2][c];
|
||
y = y + ctx->S[3][d];
|
||
|
||
return y;
|
||
|
||
#endif // DISABLE_SECURE_CODE
|
||
|
||
}
|