Another merge from beecrypt-3.0.0.
CVS patchset: 6876 CVS date: 2003/05/28 15:09:08
This commit is contained in:
parent
0148a73a15
commit
a3a09742fe
|
@ -22,6 +22,7 @@ config.m4
|
|||
config.status
|
||||
config.sub
|
||||
configure
|
||||
depcomp
|
||||
doxygen
|
||||
libtool
|
||||
listobjs
|
||||
|
|
|
@ -19,6 +19,9 @@
|
|||
|
||||
/*!\file aes.c
|
||||
* \brief AES block cipher, as specified by NIST FIPS 197.
|
||||
*
|
||||
* Based on Brian Gladman's AES implementation.
|
||||
*
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup BC_aes_m BC_m
|
||||
*/
|
||||
|
@ -786,15 +789,15 @@ int aesSetup(aesParam* ap, const byte* key, size_t keybits, cipherOperation op)
|
|||
while (1)
|
||||
{
|
||||
t = rk[3];
|
||||
rk[4] = rk[0] ^
|
||||
(_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
t = (_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
(_ae4[(t >> 8) & 0xff] & 0x00ff0000U) ^
|
||||
(_ae4[(t ) & 0xff] & 0x0000ff00U) ^
|
||||
(_ae4[(t >> 24) ] & 0x000000ffU) ^
|
||||
_arc[i];
|
||||
rk[5] = rk[1] ^ rk[4];
|
||||
rk[6] = rk[2] ^ rk[5];
|
||||
rk[7] = rk[3] ^ rk[6];
|
||||
rk[4] = (t ^= rk[0]);
|
||||
rk[5] = (t ^= rk[1]);
|
||||
rk[6] = (t ^= rk[2]);
|
||||
rk[7] = (t ^= rk[3]);
|
||||
if (++i == 10)
|
||||
break;
|
||||
rk += 4;
|
||||
|
@ -805,19 +808,19 @@ int aesSetup(aesParam* ap, const byte* key, size_t keybits, cipherOperation op)
|
|||
while (1)
|
||||
{
|
||||
t = rk[5];
|
||||
rk[ 6] = rk[0] ^
|
||||
(_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
t = (_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
(_ae4[(t >> 8) & 0xff] & 0x00ff0000U) ^
|
||||
(_ae4[(t ) & 0xff] & 0x0000ff00U) ^
|
||||
(_ae4[(t >> 24) ] & 0x000000ffU) ^
|
||||
_arc[i];
|
||||
rk[ 7] = rk[1] ^ rk[ 6];
|
||||
rk[ 8] = rk[2] ^ rk[ 7];
|
||||
rk[ 9] = rk[3] ^ rk[ 8];
|
||||
rk[6] = (t ^= rk[0]);
|
||||
rk[7] = (t ^= rk[1]);
|
||||
rk[8] = (t ^= rk[2]);
|
||||
rk[9] = (t ^= rk[3]);
|
||||
if (++i == 8)
|
||||
break;
|
||||
rk[10] = rk[4] ^ rk[ 9];
|
||||
rk[11] = rk[5] ^ rk[10];
|
||||
rk[10] = (t ^= rk[4]);
|
||||
rk[11] = (t ^= rk[5]);
|
||||
rk += 6;
|
||||
}
|
||||
}
|
||||
|
@ -826,26 +829,25 @@ int aesSetup(aesParam* ap, const byte* key, size_t keybits, cipherOperation op)
|
|||
while (1)
|
||||
{
|
||||
t = rk[7];
|
||||
rk[8] = rk[0] ^
|
||||
(_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
t = (_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
(_ae4[(t >> 8) & 0xff] & 0x00ff0000U) ^
|
||||
(_ae4[(t ) & 0xff] & 0x0000ff00U) ^
|
||||
(_ae4[(t >> 24) ] & 0x000000ffU) ^
|
||||
_arc[i];
|
||||
rk[ 9] = rk[1] ^ rk[ 8];
|
||||
rk[10] = rk[2] ^ rk[ 9];
|
||||
rk[11] = rk[3] ^ rk[10];
|
||||
rk[8] = (t ^= rk[0]);
|
||||
rk[9] = (t ^= rk[1]);
|
||||
rk[10] = (t ^= rk[2]);
|
||||
rk[11] = (t ^= rk[3]);
|
||||
if (++i == 7)
|
||||
break;
|
||||
t = rk[7];
|
||||
rk[12] = rk[4] ^
|
||||
(_ae4[(t >> 16) & 0xff] & 0xff000000U) ^
|
||||
(_ae4[(t >> 8) & 0xff] & 0x00ff0000U) ^
|
||||
(_ae4[(t ) & 0xff] & 0x0000ff00U) ^
|
||||
(_ae4[(t >> 24) ] & 0x000000ffU);
|
||||
rk[13] = rk[5] ^ rk[12];
|
||||
rk[14] = rk[6] ^ rk[13];
|
||||
rk[15] = rk[7] ^ rk[14];
|
||||
t = (_ae4[(t >> 24) ] & 0xff000000U) ^
|
||||
(_ae4[(t >> 16) & 0xff] & 0x00ff0000U) ^
|
||||
(_ae4[(t >> 8) & 0xff] & 0x0000ff00U) ^
|
||||
(_ae4[(t ) & 0xff] & 0x000000ffU);
|
||||
rk[12] = (t ^= rk[4]);
|
||||
rk[13] = (t ^= rk[5]);
|
||||
rk[14] = (t ^= rk[6]);
|
||||
rk[15] = (t ^= rk[7]);
|
||||
rk += 8;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -172,6 +172,49 @@ dnl return carry
|
|||
C_FUNCTION_END(mpsub)
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mpmultwo)
|
||||
.prologue
|
||||
alloc saved_pfs = ar.pfs,2,6,0,8
|
||||
mov saved_lc = ar.lc
|
||||
mov saved_pr = pr
|
||||
sub sze = in0,r0,1;;
|
||||
|
||||
dnl adjust addresses
|
||||
shladd dst = sze,3,in1
|
||||
shladd src = sze,3,in1
|
||||
|
||||
dnl prepare modulo-scheduled loop
|
||||
mov ar.lc = sze
|
||||
mov ar.ec = 2
|
||||
mov pr.rot = ((1 << 16) | (1 << 19));;
|
||||
|
||||
.body
|
||||
LOCAL(mpmultwo):
|
||||
.pred.rel.mutex p20,p22
|
||||
(p16) ld8 r32 = [src],-8
|
||||
(p20) add r36 = r33,r33
|
||||
(p22) add r36 = r33,r33,1
|
||||
;;
|
||||
(p20) cmp.leu p19,p21 = r33,r36
|
||||
(p22) cmp.ltu p19,p21 = r33,r36
|
||||
(p18) st8 [dst] = r37,-8
|
||||
br.ctop.dptk LOCAL(mpmultwo);;
|
||||
|
||||
dnl loop epilogue: final store
|
||||
(p18) st8 [dst] = r37,-8
|
||||
|
||||
dnl return carry
|
||||
.pred.rel.mutex p20,p22
|
||||
(p20) add ret0 = r0,r0
|
||||
(p22) add ret0 = r0,r0,1
|
||||
;;
|
||||
mov pr = saved_pr, -1
|
||||
mov ar.lc = saved_lc
|
||||
mov ar.pfs = saved_pfs
|
||||
br.ret.sptk b0
|
||||
C_FUNCTION_END(mpmultwo)
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mpsetmul)
|
||||
.prologue
|
||||
alloc saved_pfs = ar.pfs,4,4,0,8
|
||||
|
@ -266,17 +309,20 @@ C_FUNCTION_END(mpaddmul)
|
|||
|
||||
divert(-1)
|
||||
C_FUNCTION_BEGIN(mpaddsqrtrc)
|
||||
alloc saved_pfs = ar.pfs,4,4,0,8
|
||||
mov saved_pr = pr
|
||||
.prologue
|
||||
alloc saved_pfs = ar.pfs,4,12,0,16
|
||||
mov saved_lc = ar.lc
|
||||
mov saved_pr = pr
|
||||
|
||||
setf.sig f6 = in3
|
||||
sub sze = in0,r0,1;;
|
||||
|
||||
dnl adjust addresses
|
||||
shladd dst = sze,4,in1
|
||||
shladd src = sze,3,in2
|
||||
shladd alt = sze,4,in1;;
|
||||
dnl use two addresses for dst, and two for src
|
||||
shladd ? = sze,4,in1
|
||||
shladd ? = sze,4,in1
|
||||
shladd ? = sze,3,in2
|
||||
shladd ? = sze,3,in2;;
|
||||
|
||||
dnl prepare the rotate-in carry
|
||||
mov r32 = r0
|
||||
|
@ -284,25 +330,29 @@ dnl prepare the rotate-in carry
|
|||
dnl prepare modulo-scheduled loop
|
||||
mov ar.lc = sze
|
||||
mov ar.ec = 5
|
||||
mov pr.rot = ((1 << 16) | (1 << 21));;
|
||||
mov pr.rot = ((1 << 16) | (1 << 22));;
|
||||
|
||||
.body
|
||||
LOCAL(mpaddsqrtrc_loop):
|
||||
(p16) ldf8 f32 = [src],-8
|
||||
(p16) ldf8 f36 = [alt],-8
|
||||
(p17) xma.lu f34 = f33,f33,f37
|
||||
(p17) xma.hu f38 = f33,f33,f37
|
||||
(p18) getf.sig r37 = f35
|
||||
(p18) getf.sig r33 = f39
|
||||
(p?) add lo to carry
|
||||
(p?+?) add lo to carry+1
|
||||
(p?) cmpleu lo
|
||||
(p?+?) cmpltu lo
|
||||
(p?) add hi to carry
|
||||
(p?+?) add hi to carry+1
|
||||
(p16) ld8 r?? = [alt],-8
|
||||
(p18) getf.sig r32 = f35
|
||||
(p18) getf.sig r35 = f39
|
||||
(p18) ld8 rlo = [alt],-8
|
||||
.pred.rel.mutex p25,p29
|
||||
(p25) add r33 = r33,r??
|
||||
(p29) add r37 = r37,r??,1
|
||||
.pred.rel.mutex p27,p31
|
||||
(p27) add hi to carry
|
||||
(p31) add hi to carry+1
|
||||
;;
|
||||
(p16) ld8 r42 = [alt],-8
|
||||
(p25) cmpleu p24,p28 = lo
|
||||
(p29) cmpltu p24,p28 = lo
|
||||
(p20) st8 lo
|
||||
(p?) cmpleu hi
|
||||
(p?+?) cmpltu hi
|
||||
(p27) cmpleu p26,p30 = hi
|
||||
(p31) cmpltu p26,p30 = hi
|
||||
(p21) st8 hi
|
||||
;;
|
||||
br.ctop.dptk LOCAL(mpaddsqrtrc_loop);;
|
||||
|
@ -310,9 +360,9 @@ LOCAL(mpaddsqrtrc_loop):
|
|||
dnl loop epilogue: final store
|
||||
(p21) st8 [dst] = r36,-8
|
||||
|
||||
dnl return carry
|
||||
(p24) add ret0 = r35,r0
|
||||
(p26) add ret0 = r35,r0,1
|
||||
|
||||
mov pr = saved_pr, -1
|
||||
mov ar.lc = saved_lc
|
||||
mov ar.pfs = saved_pfs
|
||||
br.ret.sptk b0
|
||||
C_FUNCTION_END(mpaddsqrtrc)
|
||||
divert(0)
|
||||
|
|
|
@ -82,8 +82,10 @@
|
|||
# define ASM_MPADDSQRTRC
|
||||
# elif defined(OPTIMIZE_IA64)
|
||||
# define ASM_MPZERO
|
||||
# define ASM_MPCOPY
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPMULTWO
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# elif defined(OPTIMIZE_M68K)
|
||||
|
|
|
@ -17,9 +17,13 @@ ltmain.sh
|
|||
*.lo
|
||||
stamp-h
|
||||
stamp-h.in
|
||||
beetest
|
||||
base64bug
|
||||
beetest
|
||||
benchbc
|
||||
benchhf
|
||||
benchme
|
||||
openpgp
|
||||
testaes
|
||||
testblowfish
|
||||
testdldp
|
||||
testdsa
|
||||
|
@ -27,6 +31,7 @@ testhmacmd5
|
|||
testhmacsha1
|
||||
testmd5
|
||||
testmp
|
||||
testmpinv
|
||||
testrsa
|
||||
testsha1
|
||||
testsha256
|
||||
|
|
|
@ -25,9 +25,9 @@ AUTOMAKE_OPTIONS = gnu no-dependencies
|
|||
INCLUDES = -I$(top_srcdir)
|
||||
LDADD = $(top_builddir)/libbeecrypt.la
|
||||
|
||||
TESTS = testmd5 testsha1 testsha256 testhmacmd5 testhmacsha1 testmp testmpinv testrsa testdldp
|
||||
TESTS = testmd5 testsha1 testsha256 testhmacmd5 testhmacsha1 testaes testblowfish testmp testmpinv testrsa testdldp
|
||||
|
||||
check_PROGRAMS = testmd5 testsha1 testsha256 testhmacmd5 testhmacsha1 testmp testmpinv testrsa testdldp
|
||||
check_PROGRAMS = testmd5 testsha1 testsha256 testhmacmd5 testhmacsha1 testaes testblowfish testmp testmpinv testrsa testdldp
|
||||
|
||||
testmd5_SOURCES = testmd5.c
|
||||
|
||||
|
@ -39,6 +39,10 @@ testhmacmd5_SOURCES = testhmacmd5.c
|
|||
|
||||
testhmacsha1_SOURCES = testhmacsha1.c
|
||||
|
||||
testaes_SOURCES = testaes.c testutil.c
|
||||
|
||||
testblowfish_SOURCES = testblowfish.c testutil.c
|
||||
|
||||
testmp_SOURCES = testmp.c
|
||||
|
||||
testmpinv_SOURCES = testmpinv.c
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright (c) 2003 Bob Deblier
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
/*!\file testaes.c
|
||||
* \brief Unit test program for the Blowfish cipher.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup UNIT_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
|
||||
#include "beecrypt.h"
|
||||
#include "aes.h"
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
extern int fromhex(byte*, const char*);
|
||||
|
||||
struct vector
|
||||
{
|
||||
char* key;
|
||||
char* input;
|
||||
char* expect;
|
||||
cipherOperation op;
|
||||
};
|
||||
|
||||
#define NVECTORS 6
|
||||
|
||||
struct vector table[NVECTORS] = {
|
||||
{ "000102030405060708090a0b0c0d0e0f",
|
||||
"00112233445566778899aabbccddeeff",
|
||||
"69c4e0d86a7b0430d8cdb78070b4c55a",
|
||||
ENCRYPT },
|
||||
{ "000102030405060708090a0b0c0d0e0f",
|
||||
"69c4e0d86a7b0430d8cdb78070b4c55a",
|
||||
"00112233445566778899aabbccddeeff",
|
||||
DECRYPT },
|
||||
{ "000102030405060708090a0b0c0d0e0f1011121314151617",
|
||||
"00112233445566778899aabbccddeeff",
|
||||
"dda97ca4864cdfe06eaf70a0ec0d7191",
|
||||
ENCRYPT },
|
||||
{ "000102030405060708090a0b0c0d0e0f1011121314151617",
|
||||
"dda97ca4864cdfe06eaf70a0ec0d7191",
|
||||
"00112233445566778899aabbccddeeff",
|
||||
DECRYPT },
|
||||
{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"00112233445566778899aabbccddeeff",
|
||||
"8ea2b7ca516745bfeafc49904b496089",
|
||||
ENCRYPT },
|
||||
{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"8ea2b7ca516745bfeafc49904b496089",
|
||||
"00112233445566778899aabbccddeeff",
|
||||
DECRYPT }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, failures = 0;
|
||||
aesParam param;
|
||||
byte key[32];
|
||||
byte src[16];
|
||||
byte dst[16];
|
||||
byte chk[16];
|
||||
size_t keybits;
|
||||
|
||||
for (i = 0; i < NVECTORS; i++)
|
||||
{
|
||||
keybits = fromhex(key, table[i].key) << 3;
|
||||
|
||||
if (aesSetup(¶m, key, keybits, table[i].op))
|
||||
return -1;
|
||||
|
||||
fromhex(src, table[i].input);
|
||||
fromhex(chk, table[i].expect);
|
||||
|
||||
switch (table[i].op)
|
||||
{
|
||||
case ENCRYPT:
|
||||
if (aesEncrypt(¶m, (uint32_t*) dst, (const uint32_t*) src))
|
||||
return -1;
|
||||
break;
|
||||
case DECRYPT:
|
||||
if (aesDecrypt(¶m, (uint32_t*) dst, (const uint32_t*) src))
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (memcmp(dst, chk, 16))
|
||||
{
|
||||
printf("failed vector %d\n", i+1);
|
||||
failures++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return failures;
|
||||
}
|
|
@ -24,72 +24,84 @@
|
|||
*/
|
||||
|
||||
#include "system.h"
|
||||
|
||||
#include "blowfish.h"
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
extern int fromhex(byte*, const char*);
|
||||
|
||||
struct vector
|
||||
{
|
||||
byte key[8];
|
||||
byte input[8];
|
||||
byte expect[8];
|
||||
char* key;
|
||||
char* input;
|
||||
char* expect;
|
||||
cipherOperation op;
|
||||
};
|
||||
|
||||
#define NVECTORS 4
|
||||
|
||||
struct vector table[NVECTORS] = {
|
||||
{ "\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
"\x4E\xF9\x97\x45\x61\x98\xDD\x78"
|
||||
},
|
||||
{ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||
"\x51\x86\x6F\xD5\xB8\x5E\xCB\x8A"
|
||||
},
|
||||
{ "\x30\x00\x00\x00\x00\x00\x00\x00",
|
||||
"\x10\x00\x00\x00\x00\x00\x00\x01",
|
||||
"\x7D\x85\x6F\x9A\x61\x30\x63\xF2"
|
||||
},
|
||||
{ "\x11\x11\x11\x11\x11\x11\x11\x11",
|
||||
"\x11\x11\x11\x11\x11\x11\x11\x11",
|
||||
"\x24\x66\xDD\x87\x8B\x96\x3C\x9D"
|
||||
},
|
||||
{ "0000000000000000",
|
||||
"0000000000000000",
|
||||
"4ef997456198dd78",
|
||||
ENCRYPT },
|
||||
{ "ffffffffffffffff",
|
||||
"ffffffffffffffff",
|
||||
"51866fd5B85ecb8a",
|
||||
ENCRYPT },
|
||||
{ "3000000000000000",
|
||||
"1000000000000001",
|
||||
"7d856f9a613063f2",
|
||||
ENCRYPT },
|
||||
{ "1111111111111111",
|
||||
"1111111111111111",
|
||||
"2466dd878b963c9d",
|
||||
ENCRYPT }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, failures = 0;
|
||||
blowfishParam param;
|
||||
byte ciphertext[8];
|
||||
byte key[56];
|
||||
byte src[8];
|
||||
byte dst[8];
|
||||
byte chk[8];
|
||||
size_t keybits;
|
||||
|
||||
for (i = 0; i < NVECTORS; i++)
|
||||
{
|
||||
if (blowfishSetup(¶m, table[i].key, 64, ENCRYPT))
|
||||
return -1;
|
||||
if (blowfishEncrypt(¶m, (uint32_t*) ciphertext, (const uint32_t*) table[i].input))
|
||||
return -1;
|
||||
for (i = 0; i < NVECTORS; i++)
|
||||
{
|
||||
keybits = fromhex(key, table[i].key) << 3;
|
||||
|
||||
if (blowfishSetup(¶m, key, keybits, table[i].op))
|
||||
return -1;
|
||||
|
||||
fromhex(src, table[i].input);
|
||||
fromhex(chk, table[i].expect);
|
||||
|
||||
switch (table[i].op)
|
||||
{
|
||||
case NOCRYPT:
|
||||
return -1;
|
||||
break;
|
||||
case ENCRYPT:
|
||||
if (blowfishEncrypt(¶m, (uint32_t*) dst, (const uint32_t*) src))
|
||||
return -1;
|
||||
break;
|
||||
case DECRYPT:
|
||||
if (blowfishDecrypt(¶m, (uint32_t*) dst, (const uint32_t*) src))
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (memcmp(dst, chk, 8))
|
||||
{
|
||||
printf("failed vector %d\n", i+1);
|
||||
failures++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (memcmp(ciphertext, table[i].expect, 8))
|
||||
{
|
||||
int j;
|
||||
printf("failed test vector %d\n", i+1);
|
||||
printf("key:\n");
|
||||
for (j = 0; j < 8; j++)
|
||||
printf("%02x", table[i].key[j]);
|
||||
printf("\ncleartext:\n");
|
||||
for (j = 0; j < 8; j++)
|
||||
printf("%02x", table[i].input[j]);
|
||||
printf("\nexpected ciphertext:\n");
|
||||
for (j = 0; j < 8; j++)
|
||||
printf("%02x", table[i].expect[j]);
|
||||
printf("\nciphertext:\n");
|
||||
for (j = 0; j < 8; j++)
|
||||
printf("%02x", ciphertext[j]);
|
||||
printf("\n");
|
||||
|
||||
failures++;
|
||||
}
|
||||
else
|
||||
printf("ok\n");
|
||||
}
|
||||
return failures;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2003 Bob Deblier
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
|
||||
#include "beecrypt.h"
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
int fromhex(byte* data, const char* hexdata)
|
||||
{
|
||||
int length = strlen(hexdata);
|
||||
int count = 0, index = 0;
|
||||
byte b = 0;
|
||||
char ch;
|
||||
|
||||
if (length & 1)
|
||||
count = 1;
|
||||
|
||||
while (index++ < length)
|
||||
{
|
||||
ch = *(hexdata++);
|
||||
|
||||
b <<= 4;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
b += (ch - '0');
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
b += (ch - 'A') + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
b += (ch - 'a') + 10;
|
||||
|
||||
count++;
|
||||
if (count == 2)
|
||||
{
|
||||
*(data++) = b;
|
||||
b = 0;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
return (length+1) >> 1;
|
||||
}
|
Loading…
Reference in New Issue