beecrypt-3.0.0 merge: (mostly) grand renaming, mpw et al.
CVS patchset: 6782 CVS date: 2003/04/27 23:06:37
This commit is contained in:
parent
18eea94d34
commit
6099f7ee0e
|
@ -9,6 +9,8 @@ Modular exponentiations, (1024 bits ^ 1024 bits) mod 1024 bits, 100 times, sorte
|
|||
|
||||
Note: since this operation is where servers really hurt (i.e. in SSL), it pays to make this operation as fast as possible.
|
||||
|
||||
BeeCrypt 2.3.0 | Visual Studio .NET | Windows XP Pro | Pentium 4M 1700 | 512 MB: 1.3 sec
|
||||
BeeCrypt 2.3.0 | gcc 3.2.1 | RedHat Linux 8.0 | Pentium 4M 1700 | 512 MB: 1.4 sec
|
||||
BeeCrypt 2.1.0 | Visual C 6.0 | Windows 2000 | Athlon 1200 | 128 MB: 1.7 sec
|
||||
BeeCrypt 2.1.0 | Visual C 6.0 | Windows Millenium | Athlon 1000 | 128 MB: 1.9 sec
|
||||
BeeCrypt 2.1.0 | Compaq C 6.3-025 | Tru64 Unix 5.1 | Alpha EV6.7 667 | 2 GB: 2.3 sec
|
||||
|
|
|
@ -2,10 +2,20 @@
|
|||
|
||||
Included in the library are:
|
||||
- entropy sources for initializing pseudo-random generators
|
||||
- pseudo-random generators: FIPS-186, Mersenne Twister
|
||||
- block ciphers: AES, Blowfish
|
||||
- hash functions: MD5, SHA-1, SHA-256
|
||||
- keyed hash functions: HMAC-MD5, HMAC-SHA-1, HMAC-SHA-256
|
||||
- pseudo-random generators
|
||||
- FIPS-186
|
||||
- Mersenne Twister
|
||||
- block ciphers
|
||||
- AES
|
||||
- Blowfish
|
||||
- hash functions
|
||||
- MD5
|
||||
- SHA-1
|
||||
- SHA-256
|
||||
- keyed hash functions (a.k.a. message authentication codes)
|
||||
- HMAC-MD5
|
||||
- HMAC-SHA-1
|
||||
- HMAC-SHA-256
|
||||
- multi-precision integer library, with assembler-optimized routines
|
||||
for several processors
|
||||
- probabilistic primality testing, with optimized small prime trial
|
||||
|
@ -23,9 +33,9 @@ Planned for the near future are:
|
|||
on certain 32-bit platforms)
|
||||
- compliance with and compliance statements for IEEE P1363
|
||||
- more blockciphers (Twofish, ... )
|
||||
- more hash functions (RIPEMD-160, SHA-384, SHA-512, HAVAL, Tiger)
|
||||
- RSA signatures as specified by RFC-2440.
|
||||
- Elliptic Curves (ECDSA, ... )
|
||||
- more hash functions (RIPEMD-160, SHA-384, SHA-512, HAVAL, Tiger)
|
||||
- RSA signatures as specified by RFC-2440.
|
||||
- Elliptic Curves (ECDSA, ... )
|
||||
- more blockcipher modes (OFB, ... )
|
||||
|
||||
The library has been tested on the following platforms:
|
||||
|
@ -41,9 +51,9 @@ The library has been tested on the following platforms:
|
|||
- includes assembler optimization for Sparc v8
|
||||
- Linux glibc 2.x x86
|
||||
- includes assembler optimization (tuned for Pentium and higher)
|
||||
- Solaris 2.6/2.7/2.8 sparc (with Forte or gnu compilers)
|
||||
- Solaris 2.[6789] sparc (with Forte or gnu compilers)
|
||||
- includes assembler optimization for Sparc v8, v8plus and v9
|
||||
- Solaris 2.7/2.8 x86 (with Forte or gnu compilers)
|
||||
- Solaris 2.[78] x86 (with Forte or gnu compilers)
|
||||
- includes assembler optimization (tuned for Pentium and higher)
|
||||
- Tru64 Unix alpha
|
||||
- Win32 (Windows 95, 98, NT 4.0, 2000)
|
||||
|
@ -52,7 +62,12 @@ The library has been tested on the following platforms:
|
|||
The library is currently in the process of being ported to:
|
||||
- Darwin (apparently Apple has decided to change the PPC assembler format;
|
||||
this will need a fix)
|
||||
- Cygwin (Can't seem to get the DLL to build properly with the current version of the autotools)
|
||||
- Cygwin (the DLL builds now, but needs to be tested)
|
||||
|
||||
The structures in the library are geared towards exchange with Java
|
||||
and its security and cryptography classes. This library can also be
|
||||
accessed from Java by installing BeeCrypt for Java, a JCE 1.2 crypto
|
||||
provider and the counterpart of this library.
|
||||
|
||||
*/
|
||||
|
||||
|
|
250
beecrypt/aes.c
250
beecrypt/aes.c
|
@ -36,7 +36,7 @@
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ae0[256] = {
|
||||
const uint32_t _ae0[256] = {
|
||||
0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d,
|
||||
0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
|
||||
0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d,
|
||||
|
@ -106,7 +106,7 @@ const uint32 _ae0[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ae1[256] = {
|
||||
const uint32_t _ae1[256] = {
|
||||
0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b,
|
||||
0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
|
||||
0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b,
|
||||
|
@ -176,7 +176,7 @@ const uint32 _ae1[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ae2[256] = {
|
||||
const uint32_t _ae2[256] = {
|
||||
0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b,
|
||||
0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
|
||||
0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b,
|
||||
|
@ -246,7 +246,7 @@ const uint32 _ae2[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ae3[256] = {
|
||||
const uint32_t _ae3[256] = {
|
||||
0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6,
|
||||
0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
|
||||
0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56,
|
||||
|
@ -316,7 +316,7 @@ const uint32 _ae3[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ae4[256] = {
|
||||
const uint32_t _ae4[256] = {
|
||||
0x63636363, 0x7c7c7c7c, 0x77777777, 0x7b7b7b7b,
|
||||
0xf2f2f2f2, 0x6b6b6b6b, 0x6f6f6f6f, 0xc5c5c5c5,
|
||||
0x30303030, 0x01010101, 0x67676767, 0x2b2b2b2b,
|
||||
|
@ -386,7 +386,7 @@ const uint32 _ae4[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ad0[256] = {
|
||||
const uint32_t _ad0[256] = {
|
||||
0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96,
|
||||
0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
|
||||
0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25,
|
||||
|
@ -456,7 +456,7 @@ const uint32 _ad0[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ad1[256] = {
|
||||
const uint32_t _ad1[256] = {
|
||||
0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e,
|
||||
0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
|
||||
0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c,
|
||||
|
@ -526,7 +526,7 @@ const uint32 _ad1[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ad2[256] = {
|
||||
const uint32_t _ad2[256] = {
|
||||
0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27,
|
||||
0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
|
||||
0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502,
|
||||
|
@ -596,7 +596,7 @@ const uint32 _ad2[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ad3[256] = {
|
||||
const uint32_t _ad3[256] = {
|
||||
0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a,
|
||||
0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
|
||||
0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5,
|
||||
|
@ -666,7 +666,7 @@ const uint32 _ad3[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
const uint32 _ad4[256] = {
|
||||
const uint32_t _ad4[256] = {
|
||||
0x52525252, 0x09090909, 0x6a6a6a6a, 0xd5d5d5d5,
|
||||
0x30303030, 0x36363636, 0xa5a5a5a5, 0x38383838,
|
||||
0xbfbfbfbf, 0x40404040, 0xa3a3a3a3, 0x9e9e9e9e,
|
||||
|
@ -737,31 +737,22 @@ const uint32 _ad4[256] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const uint32 _arc[] = {
|
||||
static const uint32_t _arc[] = {
|
||||
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
||||
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
||||
0x1B000000, 0x36000000
|
||||
};
|
||||
|
||||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const blockMode aesModes[2] =
|
||||
{
|
||||
{ /* ECB */ (blockModeEncrypt) aesECBEncrypt, (blockModeDecrypt) aesECBDecrypt },
|
||||
{ /* CBC */ (blockModeEncrypt) aesCBCEncrypt, (blockModeDecrypt) aesCBCDecrypt }
|
||||
};
|
||||
|
||||
/*@-sizeoftype@*/
|
||||
const blockCipher aes = { "AES", sizeof(aesParam), 16, 128, 256, 64, (blockCipherSetup) aesSetup, (blockCipherSetIV) aesSetIV, (blockCipherEncrypt) aesEncrypt, (blockCipherDecrypt) aesDecrypt, aesModes };
|
||||
const blockCipher aes = { "AES", sizeof(aesParam), 16, 128, 256, 64, (blockCipherSetup) aesSetup, (blockCipherSetIV) aesSetIV, (blockCipherEncrypt) aesEncrypt, (blockCipherDecrypt) aesDecrypt, (blockCipherFeedback) aesFeedback };
|
||||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int aesSetup(aesParam* ap, const uint32* key, int keybits, cipherOperation op)
|
||||
int aesSetup(aesParam* ap, const byte* key, size_t keybits, cipherOperation op)
|
||||
{
|
||||
if (((keybits & 63) == 0) && (keybits >= 128) && (keybits <= 256))
|
||||
{
|
||||
register uint32* rk, t, i, j;
|
||||
register uint32_t* rk, t, i, j;
|
||||
|
||||
/* clear fdback/iv */
|
||||
ap->fdback[0] = 0;
|
||||
|
@ -769,9 +760,19 @@ int aesSetup(aesParam* ap, const uint32* key, int keybits, cipherOperation op)
|
|||
ap->fdback[2] = 0;
|
||||
ap->fdback[3] = 0;
|
||||
|
||||
ap->nr = 6 + ((uint32)keybits >> 5);
|
||||
ap->nr = 6 + ((uint32_t)keybits >> 5);
|
||||
|
||||
mp32copy(((uint32)keybits >> 5), rk = ap->k, key);
|
||||
rk = ap->k;
|
||||
|
||||
/* endian-less encoding */
|
||||
for (i = 0; i < (keybits >> 5); i++)
|
||||
{
|
||||
rk[i] = (key[0] << 24) |
|
||||
(key[1] << 16) |
|
||||
(key[2] << 8) |
|
||||
(key[3] );
|
||||
key += 4;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
|
||||
|
@ -890,22 +891,12 @@ int aesSetup(aesParam* ap, const uint32* key, int keybits, cipherOperation op)
|
|||
|
||||
#ifndef ASM_AESSETIV
|
||||
/*@-boundsread@*/
|
||||
int aesSetIV(aesParam* ap, const uint32* iv)
|
||||
int aesSetIV(aesParam* ap, const byte* iv)
|
||||
{
|
||||
if (iv)
|
||||
{
|
||||
ap->fdback[0] = iv[0];
|
||||
ap->fdback[1] = iv[1];
|
||||
ap->fdback[2] = iv[2];
|
||||
ap->fdback[3] = iv[3];
|
||||
}
|
||||
memcpy(ap->fdback, iv, 16);
|
||||
else
|
||||
{
|
||||
ap->fdback[0] = 0;
|
||||
ap->fdback[1] = 0;
|
||||
ap->fdback[2] = 0;
|
||||
ap->fdback[3] = 0;
|
||||
}
|
||||
memset(ap->fdback, 0, 16);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -992,11 +983,11 @@ int aesSetIV(aesParam* ap, const uint32* iv)
|
|||
|
||||
#ifndef ASM_AESENCRYPT
|
||||
/*@-boundswrite@*/
|
||||
int aesEncrypt(aesParam* ap, uint32* dst, const uint32* src)
|
||||
int aesEncrypt(aesParam* ap, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
register uint32 s0, s1, s2, s3;
|
||||
register uint32 t0, t1, t2, t3;
|
||||
register uint32* rk = ap->k;
|
||||
register uint32_t s0, s1, s2, s3;
|
||||
register uint32_t t0, t1, t2, t3;
|
||||
register uint32_t* rk = ap->k;
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
s0 = src[0] ^ rk[0];
|
||||
|
@ -1134,9 +1125,9 @@ int aesEncrypt(aesParam* ap, uint32* dst, const uint32* src)
|
|||
/*@-boundswrite@*/
|
||||
int aesDecrypt(aesParam* ap, uint32* dst, const uint32* src)
|
||||
{
|
||||
register uint32 s0, s1, s2, s3;
|
||||
register uint32 t0, t1, t2, t3;
|
||||
register uint32* rk = ap->k;
|
||||
register uint32_t s0, s1, s2, s3;
|
||||
register uint32_t t0, t1, t2, t3;
|
||||
register uint32_t* rk = ap->k;
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
s0 = src[0] ^ rk[0];
|
||||
|
@ -1192,172 +1183,7 @@ int aesDecrypt(aesParam* ap, uint32* dst, const uint32* src)
|
|||
/*@=boundswrite@*/
|
||||
#endif
|
||||
|
||||
#ifndef ASM_AESECBENCRYPT
|
||||
int aesECBEncrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
uint32_t* aesFeedback(aesParam* ap)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
(void) aesEncrypt(ap, dst, src);
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
}
|
||||
return 0;
|
||||
return ap->fdback;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ASM_AESECBDECRYPT
|
||||
int aesECBDecrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
(void) aesDecrypt(ap, dst, src);
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ASM_AESCBCENCRYPT
|
||||
/*@-boundswrite@*/
|
||||
int aesCBCEncrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
dst[0] = src[0] ^ ap->fdback[0];
|
||||
dst[1] = src[1] ^ ap->fdback[1];
|
||||
dst[2] = src[2] ^ ap->fdback[2];
|
||||
dst[3] = src[3] ^ ap->fdback[3];
|
||||
|
||||
(void) aesEncrypt(ap, dst, dst);
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
dst[0] = src[0] ^ dst[-4];
|
||||
dst[1] = src[1] ^ dst[-3];
|
||||
dst[2] = src[2] ^ dst[-2];
|
||||
dst[3] = src[3] ^ dst[-1];
|
||||
|
||||
(void) aesEncrypt(ap, dst, dst);
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
ap->fdback[0] = dst[-4];
|
||||
ap->fdback[1] = dst[-3];
|
||||
ap->fdback[2] = dst[-2];
|
||||
ap->fdback[3] = dst[-1];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
#endif
|
||||
|
||||
#ifndef ASM_AESCBCDECRYPT
|
||||
/*@-boundsread@*/
|
||||
int aesCBCDecrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
if (src == dst)
|
||||
{
|
||||
register uint32 fb0 = src[0];
|
||||
register uint32 fb1 = src[1];
|
||||
register uint32 fb2 = src[2];
|
||||
register uint32 fb3 = src[3];
|
||||
|
||||
(void) aesDecrypt(ap, dst, src);
|
||||
|
||||
dst[0] ^= ap->fdback[0];
|
||||
dst[1] ^= ap->fdback[1];
|
||||
dst[2] ^= ap->fdback[2];
|
||||
dst[3] ^= ap->fdback[3];
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
register int src0 = src[0];
|
||||
register int src1 = src[1];
|
||||
register int src2 = src[2];
|
||||
register int src3 = src[3];
|
||||
|
||||
(void) aesDecrypt(ap, dst, src);
|
||||
|
||||
dst[0] ^= fb0;
|
||||
dst[1] ^= fb1;
|
||||
dst[2] ^= fb2;
|
||||
dst[3] ^= fb3;
|
||||
|
||||
fb0 = src0;
|
||||
fb1 = src1;
|
||||
fb2 = src2;
|
||||
fb3 = src3;
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
ap->fdback[0] = fb0;
|
||||
ap->fdback[1] = fb1;
|
||||
ap->fdback[2] = fb2;
|
||||
ap->fdback[3] = fb3;
|
||||
}
|
||||
else
|
||||
{
|
||||
(void) aesDecrypt(ap, dst, src);
|
||||
|
||||
dst[0] ^= ap->fdback[0];
|
||||
dst[1] ^= ap->fdback[1];
|
||||
dst[0] ^= ap->fdback[0];
|
||||
dst[1] ^= ap->fdback[1];
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
(void) aesDecrypt(ap, dst, src);
|
||||
|
||||
dst[0] ^= src[-4];
|
||||
dst[1] ^= src[-3];
|
||||
dst[2] ^= src[-2];
|
||||
dst[3] ^= src[-1];
|
||||
|
||||
dst += 4;
|
||||
src += 4;
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
ap->fdback[0] = src[-4];
|
||||
ap->fdback[1] = src[-3];
|
||||
ap->fdback[2] = src[-2];
|
||||
ap->fdback[3] = src[-1];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*@=boundsread@*/
|
||||
#endif
|
||||
|
|
|
@ -31,9 +31,9 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32 k[64];
|
||||
uint32 nr;
|
||||
uint32 fdback[4];
|
||||
uint32_t k[64];
|
||||
uint32_t nr;
|
||||
uint32_t fdback[4];
|
||||
} aesParam;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -46,26 +46,67 @@ extern "C" {
|
|||
extern const BEECRYPTAPI blockCipher aes;
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
* The cipher's setup function.
|
||||
*
|
||||
* This function expands the key depending on whether the ENCRYPT or DECRYPT
|
||||
* operation was selected.
|
||||
*
|
||||
* @param ap parameter block
|
||||
* @param key key value
|
||||
* @param keybits number of bits in the key (128, 192 or 256)
|
||||
* @param op ENCRYPT or DECRYPT.
|
||||
* @retval 0 on success, -1 on failure.
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesSetup (aesParam* ap, const uint32* key, int keybits, cipherOperation op)
|
||||
int aesSetup (aesParam* ap, const byte* key, size_t keybits, cipherOperation op)
|
||||
/*@modifies ap @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
* The Initialization Vector setup function.
|
||||
*
|
||||
* This function is only necessary in block chaining or feedback modes.
|
||||
*
|
||||
* @param ap parameter block
|
||||
* @param iv initialization vector (or NULL)
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesSetIV (aesParam* ap, const uint32* iv)
|
||||
int aesSetIV (aesParam* ap, /*@null@*/ const byte* iv)
|
||||
/*@modifies ap @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
* The raw encryption function.
|
||||
*
|
||||
* This function encrypts one block of data; the size of a block is 128 bits.
|
||||
*
|
||||
* @param ap parameter block
|
||||
* @param dst ciphertext (aligned on 32-bit boundary)
|
||||
* @param src cleartext (aligned on 32-bit boundary)
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesEncrypt(aesParam* ap, uint32* dst, const uint32* src)
|
||||
int aesEncrypt(aesParam* ap, uint32_t* dst, const uint32_t* src)
|
||||
/*@modifies dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
* The raw decryption function.
|
||||
*
|
||||
* This function decrypts one block of data; the size of a block is 128 bits.
|
||||
*
|
||||
* @param ap parameter block
|
||||
* @param dst cleartext (aligned on 32-bit boundary)
|
||||
* @param src ciphertext (aligned on 32-bit boundary)
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesDecrypt(aesParam* ap, uint32_t* dst, const uint32_t* src)
|
||||
/*@modifies dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
|
@ -73,40 +114,8 @@ int aesEncrypt(aesParam* ap, uint32* dst, const uint32* src)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesDecrypt(aesParam* ap, uint32* dst, const uint32* src)
|
||||
/*@modifies dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesECBEncrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesECBDecrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesCBCEncrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies ap, dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_aes_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int aesCBCDecrypt(aesParam* ap, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies ap, dst @*/;
|
||||
uint32_t* aesFeedback(aesParam* ap)
|
||||
/*@modifies ap @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -42,14 +42,6 @@ extern "C" {
|
|||
# if defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686)
|
||||
# define ASM_AESENCRYPT
|
||||
# define ASM_AESDECRYPT
|
||||
# define ASM_AESECBENCRYPT
|
||||
# define ASM_AESECBDECRYPT
|
||||
# endif
|
||||
# if defined(OPTIMIZE_POWERPC)
|
||||
# define ASM_AESENCRYPT
|
||||
# define ASM_AESDECRYPT
|
||||
# define ASM_AESECBENCRYPT
|
||||
# define ASM_AESECBDECRYPT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,15 +1,7 @@
|
|||
/*@-sizeoftype@*/
|
||||
/**
|
||||
* \file beecrypt.c
|
||||
*
|
||||
* BeeCrypt library hooks & stubs, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999, 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -23,7 +15,12 @@
|
|||
* 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 beecrypt.c
|
||||
* \brief BeeCrypt API.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup ES_m PRNG_m HASH_m HMAC_m BC_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
|
@ -126,7 +123,7 @@ const entropySource* entropySourceDefault()
|
|||
}
|
||||
|
||||
/*@-boundsread@*/
|
||||
int entropyGatherNext(uint32* data, int size)
|
||||
int entropyGatherNext(byte* data, size_t size)
|
||||
{
|
||||
const char* selection = getenv("BEECRYPT_ENTROPY");
|
||||
|
||||
|
@ -254,7 +251,7 @@ int randomGeneratorContextFree(randomGeneratorContext* ctxt)
|
|||
/*@=nullstate@*/
|
||||
}
|
||||
|
||||
int randomGeneratorContextNext(randomGeneratorContext* ctxt, uint32* data, int size)
|
||||
int randomGeneratorContextNext(randomGeneratorContext* ctxt, byte* data, size_t size)
|
||||
{
|
||||
return ctxt->rng->next(ctxt->param, data, size);
|
||||
}
|
||||
|
@ -370,7 +367,7 @@ int hashFunctionContextReset(hashFunctionContext* ctxt)
|
|||
return ctxt->algo->reset(ctxt->param);
|
||||
}
|
||||
|
||||
int hashFunctionContextUpdate(hashFunctionContext* ctxt, const byte* data, int size)
|
||||
int hashFunctionContextUpdate(hashFunctionContext* ctxt, const byte* data, size_t size)
|
||||
{
|
||||
if (ctxt == (hashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -405,7 +402,7 @@ int hashFunctionContextUpdateMC(hashFunctionContext* ctxt, const memchunk* m)
|
|||
}
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int hashFunctionContextUpdateMP32(hashFunctionContext* ctxt, const mpnumber* n)
|
||||
int hashFunctionContextUpdateMP(hashFunctionContext* ctxt, const mpnumber* n)
|
||||
{
|
||||
if (ctxt == (hashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -418,22 +415,25 @@ int hashFunctionContextUpdateMP32(hashFunctionContext* ctxt, const mpnumber* n)
|
|||
|
||||
if (n != (mpnumber*) 0)
|
||||
{
|
||||
register int rc = -1;
|
||||
register byte* temp = (byte*) malloc((n->size << 2) + 1);
|
||||
int rc;
|
||||
byte* tmp = (byte*) malloc(MP_WORDS_TO_BYTES(n->size) + 1);
|
||||
|
||||
if (tmp == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
/*@-nullpass -nullderef -nullptrarith @*/ /* FIX: temp may be NULL */
|
||||
if (mp32msbset(n->size, n->data))
|
||||
if (mpmsbset(n->size, n->data))
|
||||
{
|
||||
temp[0] = 0;
|
||||
(void) encodeInts((javaint*) n->data, temp+1, n->size);
|
||||
rc = ctxt->algo->update(ctxt->param, temp, (n->size << 2) + 1);
|
||||
tmp[0] = 0;
|
||||
i2osp(tmp+1, MP_WORDS_TO_BYTES(n->size), n->data, n->size);
|
||||
rc = ctxt->algo->update(ctxt->param, tmp, MP_WORDS_TO_BYTES(n->size) + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
(void) encodeInts((javaint*) n->data, temp, n->size);
|
||||
rc = ctxt->algo->update(ctxt->param, temp, n->size << 2);
|
||||
i2osp(tmp, MP_WORDS_TO_BYTES(n->size), n->data, n->size);
|
||||
rc = ctxt->algo->update(ctxt->param, tmp, MP_WORDS_TO_BYTES(n->size));
|
||||
}
|
||||
free(temp);
|
||||
free(tmp);
|
||||
/*@=nullpass =nullderef =nullptrarith @*/
|
||||
|
||||
return rc;
|
||||
|
@ -442,7 +442,7 @@ int hashFunctionContextUpdateMP32(hashFunctionContext* ctxt, const mpnumber* n)
|
|||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
int hashFunctionContextDigest(hashFunctionContext* ctxt, mpnumber* dig)
|
||||
int hashFunctionContextDigest(hashFunctionContext* ctxt, byte *digest)
|
||||
{
|
||||
if (ctxt == (hashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -453,28 +453,59 @@ int hashFunctionContextDigest(hashFunctionContext* ctxt, mpnumber* dig)
|
|||
if (ctxt->param == (hashFunctionParam*) 0)
|
||||
return -1;
|
||||
|
||||
if (dig != (mpnumber*) 0)
|
||||
{
|
||||
mpnsize(dig, (ctxt->algo->digestsize + 3) >> 2);
|
||||
if (digest == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
return ctxt->algo->digest(ctxt->param, dig->data);
|
||||
return ctxt->algo->digest(ctxt->param, digest);
|
||||
}
|
||||
|
||||
int hashFunctionContextDigestMP(hashFunctionContext* ctxt, mpnumber* d)
|
||||
{
|
||||
if (ctxt == (hashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
||||
if (ctxt->algo == (hashFunction*) 0)
|
||||
return -1;
|
||||
|
||||
if (ctxt->param == (hashFunctionParam*) 0)
|
||||
return -1;
|
||||
|
||||
if (d != (mpnumber*) 0)
|
||||
{
|
||||
int rc;
|
||||
|
||||
byte *digest = (byte*) malloc(ctxt->algo->digestsize);
|
||||
|
||||
if (digest == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
if (ctxt->algo->digest(ctxt->param, digest))
|
||||
{
|
||||
free(digest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = os2ip(d->data, d->size, digest, ctxt->algo->digestsize);
|
||||
|
||||
free(digest);
|
||||
|
||||
return rc;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int hashFunctionContextDigestMatch(hashFunctionContext* ctxt, const mpnumber* match)
|
||||
int hashFunctionContextDigestMatch(hashFunctionContext* ctxt, const mpnumber* d)
|
||||
{
|
||||
register int rc = 0;
|
||||
int rc = 0;
|
||||
|
||||
mpnumber dig;
|
||||
mpnumber match;
|
||||
|
||||
mpnzero(&dig);
|
||||
mpnzero(&match);
|
||||
|
||||
if (hashFunctionContextDigest(ctxt, &dig) == 0)
|
||||
if (dig.size == match->size)
|
||||
rc = mp32eq(dig.size, dig.data, match->data);
|
||||
if (hashFunctionContextDigestMP(ctxt, &match) == 0)
|
||||
rc = mpeqx(d->size, d->data, match.size, match.data);
|
||||
|
||||
mpnfree(&dig);
|
||||
mpnfree(&match);
|
||||
|
||||
/*@-mustfree@*/ /* dig.data is OK */
|
||||
return rc;
|
||||
|
@ -581,7 +612,7 @@ int keyedHashFunctionContextFree(keyedHashFunctionContext* ctxt)
|
|||
/*@=nullstate@*/
|
||||
}
|
||||
|
||||
int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const uint32* key, int keybits)
|
||||
int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const byte* key, size_t keybits)
|
||||
{
|
||||
if (ctxt == (keyedHashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -592,7 +623,7 @@ int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const uint32*
|
|||
if (ctxt->param == (keyedHashFunctionParam*) 0)
|
||||
return -1;
|
||||
|
||||
if (key == (uint32*) 0)
|
||||
if (key == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
return ctxt->algo->setup(ctxt->param, key, keybits);
|
||||
|
@ -612,7 +643,7 @@ int keyedHashFunctionContextReset(keyedHashFunctionContext* ctxt)
|
|||
return ctxt->algo->reset(ctxt->param);
|
||||
}
|
||||
|
||||
int keyedHashFunctionContextUpdate(keyedHashFunctionContext* ctxt, const byte* data, int size)
|
||||
int keyedHashFunctionContextUpdate(keyedHashFunctionContext* ctxt, const byte* data, size_t size)
|
||||
{
|
||||
if (ctxt == (keyedHashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -647,7 +678,7 @@ int keyedHashFunctionContextUpdateMC(keyedHashFunctionContext* ctxt, const memch
|
|||
}
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int keyedHashFunctionContextUpdateMP32(keyedHashFunctionContext* ctxt, const mpnumber* n)
|
||||
int keyedHashFunctionContextUpdateMP(keyedHashFunctionContext* ctxt, const mpnumber* n)
|
||||
{
|
||||
if (ctxt == (keyedHashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -664,7 +695,7 @@ int keyedHashFunctionContextUpdateMP32(keyedHashFunctionContext* ctxt, const mpn
|
|||
register byte* temp = (byte*) malloc((n->size << 2) + 1);
|
||||
|
||||
/*@-nullpass -nullderef -nullptrarith @*/ /* FIX: temp may be NULL */
|
||||
if (mp32msbset(n->size, n->data))
|
||||
if (mpmsbset(n->size, n->data))
|
||||
{
|
||||
temp[0] = 0;
|
||||
(void) encodeInts((javaint*) n->data, temp+1, n->size);
|
||||
|
@ -684,7 +715,7 @@ int keyedHashFunctionContextUpdateMP32(keyedHashFunctionContext* ctxt, const mpn
|
|||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, mpnumber* dig)
|
||||
int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, byte* digest)
|
||||
{
|
||||
if (ctxt == (keyedHashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
@ -695,29 +726,59 @@ int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, mpnumber* dig
|
|||
if (ctxt->param == (keyedHashFunctionParam*) 0)
|
||||
return -1;
|
||||
|
||||
if (dig != (mpnumber*) 0)
|
||||
{
|
||||
mpnsize(dig, (ctxt->algo->digestsize + 3) >> 2);
|
||||
if (digest == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
return ctxt->algo->digest(ctxt->param, dig->data);
|
||||
return ctxt->algo->digest(ctxt->param, digest);
|
||||
}
|
||||
|
||||
int keyedHashFunctionContextDigestMP(keyedHashFunctionContext* ctxt, mpnumber* d)
|
||||
{
|
||||
if (ctxt == (keyedHashFunctionContext*) 0)
|
||||
return -1;
|
||||
|
||||
if (ctxt->algo == (keyedHashFunction*) 0)
|
||||
return -1;
|
||||
|
||||
if (ctxt->param == (keyedHashFunctionParam*) 0)
|
||||
return -1;
|
||||
|
||||
if (d != (mpnumber*) 0)
|
||||
{
|
||||
int rc;
|
||||
|
||||
byte *digest = (byte*) malloc(ctxt->algo->digestsize);
|
||||
|
||||
if (digest == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
if (ctxt->algo->digest(ctxt->param, digest))
|
||||
{
|
||||
free(digest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = os2ip(d->data, d->size, digest, ctxt->algo->digestsize);
|
||||
|
||||
free(digest);
|
||||
|
||||
return rc;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int keyedHashFunctionContextDigestMatch(keyedHashFunctionContext* ctxt, const mpnumber* match)
|
||||
int keyedHashFunctionContextDigestMatch(keyedHashFunctionContext* ctxt, const mpnumber* d)
|
||||
{
|
||||
register int rc = 0;
|
||||
int rc = 0;
|
||||
|
||||
mpnumber dig;
|
||||
mpnumber match;
|
||||
|
||||
mpnzero(&dig);
|
||||
mpnzero(&match);
|
||||
|
||||
if (keyedHashFunctionContextDigest(ctxt, &dig) == 0)
|
||||
if (dig.size == match->size)
|
||||
|
||||
rc = mp32eq(dig.size, dig.data, match->data);
|
||||
if (keyedHashFunctionContextDigestMP(ctxt, &match) == 0)
|
||||
rc = mpeqx(d->size, d->data, match.size, match.data);
|
||||
|
||||
mpnfree(&dig);
|
||||
mpnfree(&match);
|
||||
|
||||
/*@-mustfree@*/ /* dig.data is OK */
|
||||
return rc;
|
||||
|
@ -803,7 +864,7 @@ int blockCipherContextInit(blockCipherContext* ctxt, const blockCipher* ciph)
|
|||
/*@=nullstate@*/
|
||||
}
|
||||
|
||||
int blockCipherContextSetup(blockCipherContext* ctxt, const uint32* key, int keybits, cipherOperation op)
|
||||
int blockCipherContextSetup(blockCipherContext* ctxt, const byte* key, size_t keybits, cipherOperation op)
|
||||
{
|
||||
if (ctxt == (blockCipherContext*) 0)
|
||||
return -1;
|
||||
|
@ -814,13 +875,13 @@ int blockCipherContextSetup(blockCipherContext* ctxt, const uint32* key, int key
|
|||
if (ctxt->param == (blockCipherParam*) 0)
|
||||
return -1;
|
||||
|
||||
if (key == (uint32*) 0)
|
||||
if (key == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
return ctxt->algo->setup(ctxt->param, key, keybits, op);
|
||||
}
|
||||
|
||||
int blockCipherContextSetIV(blockCipherContext* ctxt, const uint32* iv)
|
||||
int blockCipherContextSetIV(blockCipherContext* ctxt, const byte* iv)
|
||||
{
|
||||
if (ctxt == (blockCipherContext*) 0)
|
||||
return -1;
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
|
||||
#include "memchunk.h"
|
||||
#include "mpnumber.h"
|
||||
#include "mp.h"
|
||||
|
||||
/** \name Entropy sources */
|
||||
/*@{*/
|
||||
|
@ -42,7 +43,7 @@
|
|||
* @param size no. of ints of data
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*entropyNext) (/*@out@*/ uint32* data, int size)
|
||||
typedef int (*entropyNext) (/*@out@*/ byte* data, size_t size)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/** \ingroup ES_m
|
||||
|
@ -107,7 +108,7 @@ const entropySource* entropySourceDefault(void)
|
|||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int entropyGatherNext(uint32* data, int size)
|
||||
int entropyGatherNext(byte* data, size_t size)
|
||||
/*@*/;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -140,7 +141,7 @@ typedef int (*randomGeneratorSetup) (randomGeneratorParam* param)
|
|||
* @param size no. of ints of entropy
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*randomGeneratorSeed) (randomGeneratorParam* param, const uint32* data, int size)
|
||||
typedef int (*randomGeneratorSeed) (randomGeneratorParam* param, const byte* data, size_t size)
|
||||
/*@modifies *param @*/;
|
||||
|
||||
/** \ingroup PRNG_m
|
||||
|
@ -152,7 +153,7 @@ typedef int (*randomGeneratorSeed) (randomGeneratorParam* param, const uint32* d
|
|||
* @param size no. of ints of data
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*randomGeneratorNext) (randomGeneratorParam* param, /*@out@*/ uint32* data, int size)
|
||||
typedef int (*randomGeneratorNext) (randomGeneratorParam* param, /*@out@*/ byte* data, size_t size)
|
||||
/*@modifies *param, *data @*/;
|
||||
|
||||
/** \ingroup PRNG_m
|
||||
|
@ -267,7 +268,7 @@ int randomGeneratorContextFree(/*@special@*/ randomGeneratorContext* ctxt)
|
|||
/*@modifies ctxt->rng, ctxt->param @*/;
|
||||
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int randomGeneratorContextNext(randomGeneratorContext* ctxt, /*@out@*/ uint32* data, int size)
|
||||
int randomGeneratorContextNext(randomGeneratorContext* ctxt, /*@out@*/ byte* data, size_t size)
|
||||
/*@modifies ctxt->param, *data @*/;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -300,7 +301,7 @@ typedef int (*hashFunctionReset) (hashFunctionParam* param)
|
|||
* @param size no. of bytes
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*hashFunctionUpdate) (hashFunctionParam* param, const byte* data, int size)
|
||||
typedef int (*hashFunctionUpdate) (hashFunctionParam* param, const byte* data, size_t size)
|
||||
/*@modifies *param @*/;
|
||||
|
||||
/** \ingroup HASH_m
|
||||
|
@ -317,7 +318,7 @@ typedef int (*hashFunctionUpdate) (hashFunctionParam* param, const byte* data, i
|
|||
* @retval data digest
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*hashFunctionDigest) (hashFunctionParam* param, /*@out@*/ uint32* data)
|
||||
typedef int (*hashFunctionDigest) (hashFunctionParam* param, /*@out@*/ byte* data)
|
||||
/*@modifies *param, *data @*/;
|
||||
|
||||
/** \ingroup HASH_m
|
||||
|
@ -421,7 +422,7 @@ int hashFunctionContextReset(hashFunctionContext* ctxt)
|
|||
/** \ingroup HASH_m
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int hashFunctionContextUpdate(hashFunctionContext* ctxt, const byte* data, int size)
|
||||
int hashFunctionContextUpdate(hashFunctionContext* ctxt, const byte* data, size_t size)
|
||||
/*@modifies ctxt */;
|
||||
|
||||
/** \ingroup HASH_m
|
||||
|
@ -433,13 +434,13 @@ int hashFunctionContextUpdateMC(hashFunctionContext* ctxt, const memchunk* m)
|
|||
/** \ingroup HASH_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hashFunctionContextUpdateMP32(hashFunctionContext* ctxt, const mpnumber* n)
|
||||
int hashFunctionContextUpdateMP(hashFunctionContext* ctxt, const mpnumber* n)
|
||||
/*@modifies ctxt */;
|
||||
|
||||
/** \ingroup HASH_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hashFunctionContextDigest(hashFunctionContext* ctxt, mpnumber* dig)
|
||||
int hashFunctionContextDigest(hashFunctionContext* ctxt, byte* dig)
|
||||
/*@modifies ctxt, *dig */;
|
||||
|
||||
/** \ingroup HASH_m
|
||||
|
@ -472,7 +473,7 @@ typedef void keyedHashFunctionParam;
|
|||
* @param keybits no. bits in secret key
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*keyedHashFunctionSetup) (keyedHashFunctionParam* param, const uint32* key, int keybits)
|
||||
typedef int (*keyedHashFunctionSetup) (keyedHashFunctionParam* param, const byte* key, size_t keybits)
|
||||
/*@modifies *param @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
|
@ -492,7 +493,7 @@ typedef int (*keyedHashFunctionReset) (keyedHashFunctionParam* param)
|
|||
* @param size no. of bytes
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*keyedHashFunctionUpdate) (keyedHashFunctionParam* param, const byte* data, int size)
|
||||
typedef int (*keyedHashFunctionUpdate) (keyedHashFunctionParam* param, const byte* data, size_t size)
|
||||
/*@modifies *param @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
|
@ -509,7 +510,7 @@ typedef int (*keyedHashFunctionUpdate) (keyedHashFunctionParam* param, const byt
|
|||
* @retval data digest (or authentication code)
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*keyedHashFunctionDigest) (keyedHashFunctionParam* param, /*@out@*/ uint32* data)
|
||||
typedef int (*keyedHashFunctionDigest) (keyedHashFunctionParam* param, /*@out@*/ byte* data)
|
||||
/*@modifies *param, *data @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
|
@ -612,7 +613,7 @@ int keyedHashFunctionContextFree(/*@special@*/ keyedHashFunctionContext* ctxt)
|
|||
/** \ingroup HMAC_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const uint32* key, int keybits)
|
||||
int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const byte* key, size_t keybits)
|
||||
/*@modifies ctxt @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
|
@ -624,7 +625,7 @@ int keyedHashFunctionContextReset(keyedHashFunctionContext* ctxt)
|
|||
/** \ingroup HMAC_m
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int keyedHashFunctionContextUpdate(keyedHashFunctionContext* ctxt, const byte* data, int size)
|
||||
int keyedHashFunctionContextUpdate(keyedHashFunctionContext* ctxt, const byte* data, size_t size)
|
||||
/*@modifies ctxt @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
|
@ -636,14 +637,14 @@ int keyedHashFunctionContextUpdateMC(keyedHashFunctionContext* ctxt, const memch
|
|||
/** \ingroup HMAC_m
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int keyedHashFunctionContextUpdateMP32(keyedHashFunctionContext* ctxt, const mpnumber* n)
|
||||
int keyedHashFunctionContextUpdateMP(keyedHashFunctionContext* ctxt, const mpnumber* n)
|
||||
/*@modifies ctxt @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, mpnumber* dig)
|
||||
/*@modifies ctxt, *dig @*/;
|
||||
int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, byte* d)
|
||||
/*@modifies ctxt, *d @*/;
|
||||
|
||||
/** \ingroup HMAC_m
|
||||
*/
|
||||
|
@ -672,17 +673,6 @@ typedef enum
|
|||
DECRYPT
|
||||
} cipherOperation;
|
||||
|
||||
/** \ingroup BC_m
|
||||
* Block cipher modes.
|
||||
*/
|
||||
/*@-enummemuse@*/
|
||||
typedef enum
|
||||
{
|
||||
ECB,
|
||||
CBC
|
||||
} cipherMode;
|
||||
/*@=enummemuse@*/
|
||||
|
||||
/** \ingroup BC_m
|
||||
* @param param blockcipher parameters
|
||||
* @param size no. of ints
|
||||
|
@ -690,7 +680,7 @@ typedef enum
|
|||
* @param src plaintext block
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*blockModeEncrypt) (blockCipherParam* param, int count, uint32* dst, const uint32* src)
|
||||
typedef int (*blockModeEncrypt) (blockCipherParam* param, int count, byte* dst, const byte* src)
|
||||
/*@modifies *param, *dst @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
|
@ -700,7 +690,7 @@ typedef int (*blockModeEncrypt) (blockCipherParam* param, int count, uint32* dst
|
|||
* @param src ciphertext block
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*blockModeDecrypt) (blockCipherParam* param, int count, uint32* dst, const uint32* src)
|
||||
typedef int (*blockModeDecrypt) (blockCipherParam* param, int count, byte* dst, const byte* src)
|
||||
/*@modifies *param, *dst @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
|
@ -724,7 +714,7 @@ typedef struct
|
|||
* @param cipherOperation
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*blockCipherSetup) (blockCipherParam* param, const uint32* key, int keybits, cipherOperation cipherOperation)
|
||||
typedef int (*blockCipherSetup) (blockCipherParam* param, const byte* key, size_t keybits, cipherOperation cipherOperation)
|
||||
/*@modifies param @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
|
@ -733,7 +723,7 @@ typedef int (*blockCipherSetup) (blockCipherParam* param, const uint32* key, int
|
|||
* @param data iv data
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*blockCipherSetIV) (blockCipherParam* param, const uint32* data)
|
||||
typedef int (*blockCipherSetIV) (blockCipherParam* param, const byte* data)
|
||||
/*@modifies param @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
|
@ -745,7 +735,7 @@ typedef int (*blockCipherSetIV) (blockCipherParam* param, const uint32* data)
|
|||
* @param src plaintext block
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*blockCipherEncrypt) (blockCipherParam* param, uint32* dst, const uint32* src)
|
||||
typedef int (*blockCipherEncrypt) (blockCipherParam* param, byte* dst, const byte* src)
|
||||
/*@modifies param, dst @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
|
@ -757,9 +747,11 @@ typedef int (*blockCipherEncrypt) (blockCipherParam* param, uint32* dst, const u
|
|||
* @param src ciphertext block
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
typedef int (*blockCipherDecrypt) (blockCipherParam* param, uint32* dst, const uint32* src)
|
||||
typedef int (*blockCipherDecrypt) (blockCipherParam* param, byte* dst, const byte* src)
|
||||
/*@modifies param, dst @*/;
|
||||
|
||||
typedef uint32_t* (*blockCipherFeedback)(blockCipherParam*);
|
||||
|
||||
/** \ingroup BC_m
|
||||
* Methods and parameters for block ciphers.
|
||||
* Specific block ciphers MAY be written to be multithread-safe.
|
||||
|
@ -777,8 +769,7 @@ typedef struct
|
|||
const blockCipherSetIV setiv;
|
||||
const blockCipherEncrypt encrypt;
|
||||
const blockCipherDecrypt decrypt;
|
||||
/*@dependent@*/
|
||||
const blockMode* mode;
|
||||
const blockCipherFeedback getfb;
|
||||
} blockCipher;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -852,13 +843,13 @@ int blockCipherContextInit(blockCipherContext* ctxt, /*@observer@*/ /*@dependent
|
|||
/** \ingroup BC_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int blockCipherContextSetup(blockCipherContext* ctxt, const uint32* key, int keybits, cipherOperation op)
|
||||
int blockCipherContextSetup(blockCipherContext* ctxt, const byte* key, size_t keybits, cipherOperation op)
|
||||
/*@modifies ctxt @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int blockCipherContextSetIV(blockCipherContext* ctxt, const uint32* iv)
|
||||
int blockCipherContextSetIV(blockCipherContext* ctxt, const byte* iv)
|
||||
/*@modifies ctxt @*/;
|
||||
|
||||
/** \ingroup BC_m
|
||||
|
|
|
@ -60,7 +60,7 @@ int testVectorInvMod(const dlkp_p* keypair)
|
|||
|
||||
mpbmulmod_w(&keypair->param.n, size, temp, size, temp+size, temp, temp+2*size);
|
||||
|
||||
rc = mp32isone(size, temp);
|
||||
rc = mpisone(size, temp);
|
||||
|
||||
free(temp);
|
||||
|
||||
|
@ -80,7 +80,7 @@ int testVectorExpMod(const dlkp_p* keypair)
|
|||
|
||||
mpbnpowmod(&keypair->param.p, &keypair->param.g, &keypair->x, &y);
|
||||
|
||||
rc = mp32eqx(y.size, y.data, keypair->y.size, keypair->y.data);
|
||||
rc = mpeqx(y.size, y.data, keypair->y.size, keypair->y.data);
|
||||
|
||||
mpnfree(&y);
|
||||
|
||||
|
@ -258,7 +258,7 @@ int testVectorDLDP()
|
|||
|
||||
/* we have the parameters, now see if g^q == 1 */
|
||||
mpbnpowmod(&dp.p, &dp.g, (mpnumber*) &dp.q, &gq);
|
||||
result = mp32isone(gq.size, gq.data);
|
||||
result = mpisone(gq.size, gq.data);
|
||||
|
||||
mpnfree(&gq);
|
||||
dldp_pFree(&dp);
|
||||
|
@ -280,7 +280,7 @@ int testVectorMD5()
|
|||
md5Update(¶m, (const unsigned char*) "abc", 3);
|
||||
md5Digest(¶m, digest);
|
||||
|
||||
return mp32eq(4, expect, digest);
|
||||
return mpeq(4, expect, digest);
|
||||
}
|
||||
|
||||
int testVectorSHA1()
|
||||
|
@ -293,7 +293,7 @@ int testVectorSHA1()
|
|||
sha1Update(¶m, (const unsigned char*) "abc", 3);
|
||||
sha1Digest(¶m, digest);
|
||||
|
||||
return mp32eq(5, expect, digest);
|
||||
return mpeq(5, expect, digest);
|
||||
}
|
||||
|
||||
int testVectorSHA256()
|
||||
|
@ -306,7 +306,7 @@ int testVectorSHA256()
|
|||
sha256Update(¶m, (const unsigned char*) "abc", 3);
|
||||
sha256Digest(¶m, digest);
|
||||
|
||||
return mp32eq(8, expect, digest);
|
||||
return mpeq(8, expect, digest);
|
||||
}
|
||||
|
||||
uint32 keyValue[] =
|
||||
|
@ -630,9 +630,9 @@ void testDLParams()
|
|||
ttime = ((double)(tstop - tstart)) / CLOCKS_PER_SEC;
|
||||
fprintf(stdout, "\tdone in %.3f seconds\n", ttime);
|
||||
#endif
|
||||
fprintf(stdout, "P = "); fflush(stdout); mp32println(stdout, dp.p.size, dp.p.modl);
|
||||
fprintf(stdout, "Q = "); fflush(stdout); mp32println(stdout, dp.q.size, dp.q.modl);
|
||||
fprintf(stdout, "G = "); fflush(stdout); mp32println(stdout, dp.g.size, dp.g.data);
|
||||
fprintf(stdout, "P = "); fflush(stdout); mpprintln(stdout, dp.p.size, dp.p.modl);
|
||||
fprintf(stdout, "Q = "); fflush(stdout); mpprintln(stdout, dp.q.size, dp.q.modl);
|
||||
fprintf(stdout, "G = "); fflush(stdout); mpprintln(stdout, dp.g.size, dp.g.data);
|
||||
dldp_pFree(&dp);
|
||||
|
||||
fprintf(stdout, "Generating P (768 bits) Q (512 bits) G with order (P-1)\n");
|
||||
|
@ -645,10 +645,10 @@ void testDLParams()
|
|||
ttime = ((double)(tstop - tstart)) / CLOCKS_PER_SEC;
|
||||
fprintf(stdout, "\tdone in %.3f seconds\n", ttime);
|
||||
#endif
|
||||
fprintf(stdout, "P = "); fflush(stdout); mp32println(stdout, dp.p.size, dp.p.modl);
|
||||
fprintf(stdout, "Q = "); fflush(stdout); mp32println(stdout, dp.q.size, dp.q.modl);
|
||||
fprintf(stdout, "G = "); fflush(stdout); mp32println(stdout, dp.g.size, dp.g.data);
|
||||
fprintf(stdout, "N = "); fflush(stdout); mp32println(stdout, dp.n.size, dp.n.modl);
|
||||
fprintf(stdout, "P = "); fflush(stdout); mpprintln(stdout, dp.p.size, dp.p.modl);
|
||||
fprintf(stdout, "Q = "); fflush(stdout); mpprintln(stdout, dp.q.size, dp.q.modl);
|
||||
fprintf(stdout, "G = "); fflush(stdout); mpprintln(stdout, dp.g.size, dp.g.data);
|
||||
fprintf(stdout, "N = "); fflush(stdout); mpprintln(stdout, dp.n.size, dp.n.modl);
|
||||
dldp_pFree(&dp);
|
||||
|
||||
randomGeneratorContextFree(&rc);
|
||||
|
|
|
@ -30,6 +30,118 @@
|
|||
#include "mp.h"
|
||||
#include "debug.h"
|
||||
|
||||
/*!\addtogroup BC_m
|
||||
* \{
|
||||
*/
|
||||
|
||||
int blockEncryptECB(const blockCipher* bc, blockCipherParam* bp, int nblocks, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
/* assumes that every blockcipher's blocksize is a multiple of 32 bits */
|
||||
register int blockwords = bc->blocksize >> 2;
|
||||
|
||||
while (nblocks > 0)
|
||||
{
|
||||
bc->encrypt(bp, dst, src);
|
||||
|
||||
dst += blockwords;
|
||||
src += blockwords;
|
||||
|
||||
nblocks--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int blockDecryptECB(const blockCipher* bc, blockCipherParam* bp, int nblocks, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
/* assumes that every blockcipher's blocksize is a multiple of 32 bits */
|
||||
register int blockwords = bc->blocksize >> 2;
|
||||
|
||||
while (nblocks > 0)
|
||||
{
|
||||
bc->decrypt(bp, dst, src);
|
||||
|
||||
dst += blockwords;
|
||||
src += blockwords;
|
||||
|
||||
nblocks--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int blockEncryptCBC(const blockCipher* bc, blockCipherParam* bp, int nblocks, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
/* assumes that every blockcipher's blocksize is a multiple of 32 bits */
|
||||
register int blockwords = bc->blocksize >> 2;
|
||||
register uint32_t* fdback = bc->getfb(bp);
|
||||
|
||||
if (nblocks > 0)
|
||||
{
|
||||
register int i;
|
||||
|
||||
for (i = 0; i < blockwords; i++)
|
||||
dst[i] = src[i] ^ fdback[i];
|
||||
|
||||
bc->encrypt(bp, dst, dst);
|
||||
|
||||
dst += blockwords;
|
||||
src += blockwords;
|
||||
|
||||
nblocks--;
|
||||
|
||||
while (nblocks > 0)
|
||||
{
|
||||
for (i = 0; i < blockwords; i++)
|
||||
dst[i] = src[i] ^ dst[i-blockwords];
|
||||
|
||||
bc->encrypt(bp, dst, dst);
|
||||
|
||||
dst += blockwords;
|
||||
src += blockwords;
|
||||
|
||||
nblocks--;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockwords; i++)
|
||||
fdback[i] = dst[i-blockwords];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int blockDecryptCBC(const blockCipher* bc, blockCipherParam* bp, int nblocks, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
/* assumes that every blockcipher's blocksize is a multiple of 32 bits */
|
||||
register int blockwords = bc->blocksize >> 2;
|
||||
register uint32_t* fdback = bc->getfb(bp);
|
||||
register uint32_t* buf = (uint32_t*) malloc(blockwords * sizeof(uint32_t));
|
||||
|
||||
if (buf)
|
||||
{
|
||||
while (nblocks > 0)
|
||||
{
|
||||
register uint32_t tmp;
|
||||
register int i;
|
||||
|
||||
bc->decrypt(bp, buf, src);
|
||||
|
||||
for (i = 0; i < blockwords; i++)
|
||||
{
|
||||
tmp = src[i];
|
||||
dst[i] = buf[i] ^ fdback[i];
|
||||
fdback[i] = tmp;
|
||||
}
|
||||
|
||||
dst += blockwords;
|
||||
src += blockwords;
|
||||
|
||||
nblocks--;
|
||||
}
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef DYING
|
||||
int blockEncrypt(const blockCipher* bc, blockCipherParam* bp, cipherMode mode, int blocks, uint32* dst, const uint32* src)
|
||||
{
|
||||
if (bc->mode)
|
||||
|
@ -65,3 +177,4 @@ int blockDecrypt(const blockCipher* bc, blockCipherParam* bp, cipherMode mode, i
|
|||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -35,29 +35,55 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
/**
|
||||
* Encrypts multiple blocks in Electronic Code Book (ECB) mode.
|
||||
* @param bc blockcipher context
|
||||
* @param bp blockcipher parameters
|
||||
* @param mode ECB or CBC
|
||||
* @param blocks no. blocks to encrypt
|
||||
* @retval dst ciphertext block
|
||||
* @param src plaintext block
|
||||
* @return 0 on success, -1 on failure
|
||||
* @param nblocks number of blocks to be encrypted.
|
||||
* @retval dst ciphertext data (aligned on a 32-bit boundary).
|
||||
* @param src cleartext data (aligned on a 32-bit boundary).
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int blockEncrypt(const blockCipher* bc, blockCipherParam* bp, cipherMode mode, int blocks, /*@out@*/ uint32* dst, const uint32* src)
|
||||
int blockEncryptECB(const blockCipher* bc, blockCipherParam* bp, int nblocks, /*@out@*/ uint32_t* dst, const uint32_t* src)
|
||||
/*@modifies bp, dst @*/;
|
||||
|
||||
/**
|
||||
* Decrypts multiple blocks in Electronic Code Book (ECB) mode.
|
||||
* @param bc blockcipher context
|
||||
* @param bp blockcipher parameters
|
||||
* @param mode ECB or CBC
|
||||
* @param blocks no. blocks to decrypt
|
||||
* @retval dst plaintext block
|
||||
* @param src ciphertext block
|
||||
* @return 0 on success, -1 on failure
|
||||
* @param nblocks number of blocks to be encrypted.
|
||||
* @retval dst ciphertext data (aligned on a 32-bit boundary).
|
||||
* @param src cleartext data (aligned on a 32-bit boundary).
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int blockDecrypt(const blockCipher* bc, blockCipherParam* bp, cipherMode mode, int blocks, /*@out@*/ uint32* dst, const uint32* src)
|
||||
int blockDecryptECB(const blockCipher* bc, blockCipherParam* bp, int nblocks, /*@out@*/ uint32_t* dst, const uint32_t* src)
|
||||
/*@modifies bp, dst @*/;
|
||||
|
||||
/**
|
||||
* Encrypts multiple blocks in Cipher Block Chaining (CBC) mode.
|
||||
* @param bc blockcipher context
|
||||
* @param bp blockcipher parameters
|
||||
* @param nblocks number of blocks to be encrypted.
|
||||
* @retval dst ciphertext data (aligned on a 32-bit boundary).
|
||||
* @param src cleartext data (aligned on a 32-bit boundary).
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int blockEncryptCBC(const blockCipher* bc, blockCipherParam* bp, int nblocks, /*@out@*/ uint32_t* dst, const uint32_t* src)
|
||||
/*@modifies bp, dst @*/;
|
||||
|
||||
/**
|
||||
* Decrypts multiple blocks in Cipher Block Chaining (CBC) mode.
|
||||
* @param bc blockcipher context
|
||||
* @param bp blockcipher parameters
|
||||
* @param nblocks number of blocks to be encrypted.
|
||||
* @retval dst ciphertext data (aligned on a 32-bit boundary).
|
||||
* @param src cleartext data (aligned on a 32-bit boundary).
|
||||
* @retval 0 on success.
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int blockDecryptCBC(const blockCipher* bc, blockCipherParam* bp, int nblocks, /*@out@*/ uint32_t* dst, const uint32_t* src)
|
||||
/*@modifies bp, dst @*/;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static uint32 _bf_p[BLOWFISHPSIZE] = {
|
||||
static uint32_t _bf_p[BLOWFISHPSIZE] = {
|
||||
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
|
||||
0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
|
||||
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
|
||||
|
@ -53,7 +53,7 @@ static uint32 _bf_p[BLOWFISHPSIZE] = {
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static uint32 _bf_s[1024] = {
|
||||
static uint32_t _bf_s[1024] = {
|
||||
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
|
||||
0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
|
||||
0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
|
||||
|
@ -315,45 +315,35 @@ static uint32 _bf_s[1024] = {
|
|||
#define EROUND(l,r) l ^= *(p++); r ^= ((s[((l>>24)&0xff)+0x000]+s[((l>>16)&0xff)+0x100])^s[((l>>8)&0xff)+0x200])+s[((l>>0)&0xff)+0x300]
|
||||
#define DROUND(l,r) l ^= *(p--); r ^= ((s[((l>>24)&0xff)+0x000]+s[((l>>16)&0xff)+0x100])^s[((l>>8)&0xff)+0x200])+s[((l>>0)&0xff)+0x300]
|
||||
|
||||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const blockMode blowfishModes[2] =
|
||||
{
|
||||
{ /* ECB */ (blockModeEncrypt) blowfishECBEncrypt, (blockModeDecrypt) blowfishECBDecrypt },
|
||||
{ /* CBC */ (blockModeEncrypt) blowfishCBCEncrypt, (blockModeDecrypt) blowfishCBCDecrypt }
|
||||
};
|
||||
|
||||
/*@-sizeoftype@*/
|
||||
const blockCipher blowfish = { "Blowfish", sizeof(blowfishParam), 8, 64, 448, 32, (blockCipherSetup) blowfishSetup, (blockCipherSetIV) blowfishSetIV, (blockCipherEncrypt) blowfishEncrypt, (blockCipherDecrypt) blowfishDecrypt, blowfishModes };
|
||||
const blockCipher blowfish = { "Blowfish", sizeof(blowfishParam), 8, 64, 448, 32, (blockCipherSetup) blowfishSetup, (blockCipherSetIV) blowfishSetIV, (blockCipherEncrypt) blowfishEncrypt, (blockCipherDecrypt) blowfishDecrypt, (blockCipherFeedback) blowfishFeedback };
|
||||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-boundsread@*/
|
||||
int blowfishSetup(blowfishParam* bp, const uint32* key, int keybits, /*@unused@*/ cipherOperation op)
|
||||
int blowfishSetup(blowfishParam* bp, const byte* key, size_t keybits, /*@unused@*/ cipherOperation op)
|
||||
{
|
||||
if (((keybits & 7) == 0) && (keybits >= 64) && (keybits <= 448))
|
||||
if (((keybits & 7) == 0) && (keybits >= 32) && (keybits <= 448))
|
||||
{
|
||||
register uint32* p = bp->p;
|
||||
register uint32* s = bp->s;
|
||||
register int i;
|
||||
register uint32_t* p = bp->p;
|
||||
register uint32_t* s = bp->s;
|
||||
register int i, j, k;
|
||||
|
||||
uint32 work[2];
|
||||
uint32_t tmp, work[2];
|
||||
|
||||
memcpy(p, _bf_p, BLOWFISHPSIZE * sizeof(*p));
|
||||
memcpy(s, _bf_s, 1024 * sizeof(*s));
|
||||
|
||||
if ((keybits & 31) == 0)
|
||||
for (i = 0, k = 0; i < BLOWFISHPSIZE; i++)
|
||||
{
|
||||
register int keywords = (((uint32)keybits) >> 5); /* i.e. in 32 bit words */
|
||||
|
||||
for (i = 0; i < BLOWFISHPSIZE; i++)
|
||||
tmp = 0;
|
||||
for (j = 0; j < 4; j++)
|
||||
{
|
||||
/* key is stored in 32 bit words in host-endian format; no swap necessary */
|
||||
p[i] ^= key[i % keywords];
|
||||
tmp <<= 8;
|
||||
tmp |= key[k++];
|
||||
if (k >= (keybits >> 3))
|
||||
k = 0;
|
||||
}
|
||||
p[i] = _bf_p[i] ^ tmp;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
work[0] = work[1] = 0;
|
||||
|
||||
|
@ -393,18 +383,12 @@ int blowfishSetup(blowfishParam* bp, const uint32* key, int keybits, /*@unused@*
|
|||
|
||||
#ifndef ASM_BLOWFISHSETIV
|
||||
/*@-boundsread@*/
|
||||
int blowfishSetIV(blowfishParam* bp, const uint32* iv)
|
||||
int blowfishSetIV(blowfishParam* bp, const byte* iv)
|
||||
{
|
||||
if (iv)
|
||||
{
|
||||
bp->fdback[0] = iv[0];
|
||||
bp->fdback[1] = iv[1];
|
||||
}
|
||||
memcpy(bp->fdback, iv, sizeof(bp->fdback));
|
||||
else
|
||||
{
|
||||
bp->fdback[0] = 0;
|
||||
bp->fdback[1] = 0;
|
||||
}
|
||||
memset(bp->fdback, 0, sizeof(bp->fdback));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -413,15 +397,15 @@ int blowfishSetIV(blowfishParam* bp, const uint32* iv)
|
|||
|
||||
#ifndef ASM_BLOWFISHENCRYPT
|
||||
/*@-boundswrite@*/
|
||||
int blowfishEncrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
||||
int blowfishEncrypt(blowfishParam* bp, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
#if WORDS_BIGENDIAN
|
||||
register uint32 xl = src[0], xr = src[1];
|
||||
register uint32_t xl = src[0], xr = src[1];
|
||||
#else
|
||||
register uint32 xl = swapu32(src[0]), xr = swapu32(src[1]);
|
||||
register uint32_t xl = swapu32(src[0]), xr = swapu32(src[1]);
|
||||
#endif
|
||||
register uint32* p = bp->p;
|
||||
register uint32* s = bp->s;
|
||||
register uint32_t* p = bp->p;
|
||||
register uint32_t* s = bp->s;
|
||||
|
||||
EROUND(xl, xr); EROUND(xr, xl);
|
||||
EROUND(xl, xr); EROUND(xr, xl);
|
||||
|
@ -447,15 +431,15 @@ int blowfishEncrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
|||
|
||||
#ifndef ASM_BLOWFISHDECRYPT
|
||||
/*@-boundswrite@*/
|
||||
int blowfishDecrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
||||
int blowfishDecrypt(blowfishParam* bp, uint32_t* dst, const uint32_t* src)
|
||||
{
|
||||
#if WORDS_BIGENDIAN
|
||||
register uint32 xl = src[0], xr = src[1];
|
||||
register uint32_t xl = src[0], xr = src[1];
|
||||
#else
|
||||
register uint32 xl = swapu32(src[0]), xr = swapu32(src[1]);
|
||||
register uint32_t xl = swapu32(src[0]), xr = swapu32(src[1]);
|
||||
#endif
|
||||
register uint32* p = bp->p+BLOWFISHPSIZE-1;
|
||||
register uint32* s = bp->s;
|
||||
register uint32_t* p = bp->p+BLOWFISHPSIZE-1;
|
||||
register uint32_t* s = bp->s;
|
||||
|
||||
DROUND(xl, xr); DROUND(xr, xl);
|
||||
DROUND(xl, xr); DROUND(xr, xl);
|
||||
|
@ -479,148 +463,7 @@ int blowfishDecrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
|||
/*@=boundswrite@*/
|
||||
#endif
|
||||
|
||||
#ifndef ASM_BLOWFISHECBENCRYPT
|
||||
int blowfishECBEncrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
uint32_t* blowfishFeedback(blowfishParam* bp)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
(void) blowfishEncrypt(bp, dst, src);
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
}
|
||||
return 0;
|
||||
return bp->fdback;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ASM_BLOWFISHECBDECRYPT
|
||||
int blowfishECBDecrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
(void) blowfishDecrypt(bp, dst, src);
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ASM_BLOWFISHCBCENCRYPT
|
||||
/*@-boundswrite@*/
|
||||
int blowfishCBCEncrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
dst[0] = src[0] ^ bp->fdback[0];
|
||||
dst[1] = src[1] ^ bp->fdback[1];
|
||||
|
||||
(void) blowfishEncrypt(bp, dst, dst);
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
dst[0] = src[0] ^ dst[-2];
|
||||
dst[1] = src[1] ^ dst[-1];
|
||||
|
||||
(void) blowfishEncrypt(bp, dst, dst);
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
bp->fdback[0] = dst[-2];
|
||||
bp->fdback[1] = dst[-1];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
#endif
|
||||
|
||||
#ifndef ASM_BLOWFISHCBCDECRYPT
|
||||
/*@-boundsread@*/
|
||||
int blowfishCBCDecrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
if (src == dst)
|
||||
{
|
||||
register uint32 fb0 = src[0];
|
||||
register uint32 fb1 = src[1];
|
||||
|
||||
(void) blowfishDecrypt(bp, dst, src);
|
||||
|
||||
dst[0] ^= bp->fdback[0];
|
||||
dst[1] ^= bp->fdback[1];
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
register int src0 = src[0];
|
||||
register int src1 = src[1];
|
||||
|
||||
(void) blowfishDecrypt(bp, dst, src);
|
||||
|
||||
dst[0] ^= fb0;
|
||||
dst[1] ^= fb1;
|
||||
|
||||
fb0 = src0;
|
||||
fb1 = src1;
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
bp->fdback[0] = fb0;
|
||||
bp->fdback[1] = fb1;
|
||||
}
|
||||
else
|
||||
{
|
||||
(void) blowfishDecrypt(bp, dst, src);
|
||||
|
||||
dst[0] ^= bp->fdback[0];
|
||||
dst[1] ^= bp->fdback[1];
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
(void) blowfishDecrypt(bp, dst, src);
|
||||
|
||||
dst[0] ^= src[-2];
|
||||
dst[1] ^= src[-1];
|
||||
|
||||
dst += 2;
|
||||
src += 2;
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
bp->fdback[0] = src[-2];
|
||||
bp->fdback[1] = src[-1];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*@=boundsread@*/
|
||||
#endif
|
||||
|
|
|
@ -38,9 +38,9 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32 p[BLOWFISHPSIZE];
|
||||
uint32 s[1024];
|
||||
uint32 fdback[2];
|
||||
uint32_t p[BLOWFISHPSIZE];
|
||||
uint32_t s[1024];
|
||||
uint32_t fdback[2];
|
||||
} blowfishParam;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -56,16 +56,16 @@ extern const BEECRYPTAPI blockCipher blowfish;
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishSetup (blowfishParam* bp, const uint32* key, int keybits, cipherOperation op)
|
||||
/*@modifies bp */;
|
||||
int blowfishSetup (blowfishParam* bp, const byte* key, size_t keybits, cipherOperation op)
|
||||
/*@modifies bp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishSetIV (blowfishParam* bp, const uint32* iv)
|
||||
/*@modifies bp */;
|
||||
int blowfishSetIV (blowfishParam* bp, const byte* iv)
|
||||
/*@modifies bp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
|
@ -73,7 +73,7 @@ int blowfishSetIV (blowfishParam* bp, const uint32* iv)
|
|||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishEncrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
||||
/*@modifies bp, dst */;
|
||||
/*@modifies bp, dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
|
@ -81,39 +81,15 @@ int blowfishEncrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
|||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishDecrypt(blowfishParam* bp, uint32* dst, const uint32* src)
|
||||
/*@modifies bp, dst */;
|
||||
/*@modifies bp, dst @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishECBEncrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies bp, dst */;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishECBDecrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies bp, dst */;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishCBCEncrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies bp, dst */;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/** \ingroup BC_blowfish_m
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int blowfishCBCDecrypt(blowfishParam* bp, int count, uint32* dst, const uint32* src)
|
||||
/*@modifies bp, dst */;
|
||||
uint32_t* blowfishFeedback(blowfishParam* bp)
|
||||
/*@*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -50,16 +50,25 @@ extern "C" {
|
|||
# if defined(OPTIMIZE_POWERPC)
|
||||
# define ASM_BLOWFISHENCRYPT
|
||||
# define ASM_BLOWFISHDECRYPT
|
||||
# define ASM_BLOWFISHECBENCRYPT
|
||||
# define ASM_BLOWFISHECBDECRYPT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__IBMC__)
|
||||
# if defined(OPTIMIZE_POWERPC)
|
||||
# define ASM_BLOWFISHENCRYPT
|
||||
# define ASM_BLOWFISHDECRYPT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
# if defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686)
|
||||
# define ASM_BLOWFISHENCRYPT
|
||||
# define ASM_BLOWFISHDECRYPT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
||||
#if defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686)
|
||||
#define ASM_BLOWFISHENCRYPT
|
||||
#define ASM_BLOWFISHDECRYPT
|
||||
#endif
|
||||
/* nothing here yet */
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -191,29 +191,34 @@ static int dhaes_pContextSetup(dhaes_pContext* ctxt, const mpnumber* privkey, co
|
|||
register int rc;
|
||||
|
||||
mpnumber secret;
|
||||
mpnumber digest;
|
||||
|
||||
byte* digest = (byte*) malloc(ctxt->hash.algo->digestsize);
|
||||
|
||||
if (digest == (byte*) 0)
|
||||
return -1;
|
||||
|
||||
/* compute the shared secret, Diffie-Hellman style */
|
||||
mpnzero(&secret);
|
||||
if (dlsvdp_pDHSecret(&ctxt->param, privkey, pubkey, &secret))
|
||||
/*@-mustfree@*/ /* FIX: secret.data leak? */
|
||||
{
|
||||
mpnfree(&secret);
|
||||
free(digest);
|
||||
return -1;
|
||||
/*@=mustfree@*/
|
||||
}
|
||||
|
||||
/* compute the hash of the message (ephemeral public) key and the shared secret */
|
||||
mpnzero(&digest);
|
||||
(void) hashFunctionContextReset (&ctxt->hash);
|
||||
(void) hashFunctionContextUpdateMP32(&ctxt->hash, message);
|
||||
(void) hashFunctionContextUpdateMP32(&ctxt->hash, &secret);
|
||||
(void) hashFunctionContextUpdateMP(&ctxt->hash, message);
|
||||
(void) hashFunctionContextUpdateMP(&ctxt->hash, &secret);
|
||||
(void) hashFunctionContextDigest (&ctxt->hash, &digest);
|
||||
|
||||
/* we don't need the secret anymore */
|
||||
mpnwipe(&secret);
|
||||
mpnfree(&secret);
|
||||
|
||||
/**
|
||||
/*
|
||||
* NOTE: blockciphers and keyed hash functions take keys with sizes
|
||||
* specified in bits and key data passed in 32-bit words.
|
||||
* specified in bits and key data passed in bytes.
|
||||
*
|
||||
* Both blockcipher and keyed hash function have a min and max key size.
|
||||
*
|
||||
|
@ -223,10 +228,10 @@ static int dhaes_pContextSetup(dhaes_pContext* ctxt, const mpnumber* privkey, co
|
|||
*/
|
||||
|
||||
/*@-usedef@*/ /* LCL: digest already set */
|
||||
if (digest.size > 0)
|
||||
if (ctxt->hash.algo->digestsize > 0)
|
||||
{
|
||||
uint32* mackey = digest.data;
|
||||
uint32* cipherkey = digest.data + (((uint32)(ctxt->mackeybits + 31)) >> 5);
|
||||
byte* mackey = digest;
|
||||
byte* cipherkey = digest + (((uint32)(ctxt->mackeybits + 7)) >> 3);
|
||||
|
||||
if ((rc = keyedHashFunctionContextSetup(&ctxt->mac, mackey, ctxt->mackeybits)))
|
||||
goto setup_end;
|
||||
|
@ -241,8 +246,9 @@ static int dhaes_pContextSetup(dhaes_pContext* ctxt, const mpnumber* privkey, co
|
|||
/*@=usedef@*/
|
||||
|
||||
setup_end:
|
||||
mpnwipe(&digest);
|
||||
mpnfree(&digest);
|
||||
/* wipe digest for good measure */
|
||||
memset(digest, 0, ctxt->hash.algo->digestsize);
|
||||
free(digest);
|
||||
|
||||
/*@-mustfree@*/ /* {secret,digest}.data are OK */
|
||||
return rc;
|
||||
|
@ -270,7 +276,7 @@ memchunk* dhaes_pContextEncrypt(dhaes_pContext* ctxt, mpnumber* ephemeralPublicK
|
|||
goto encrypt_end;
|
||||
|
||||
/* encrypt the memchunk in CBC mode */
|
||||
if (blockEncrypt(ctxt->cipher.algo, ctxt->cipher.param, CBC, paddedtext->size / ctxt->cipher.algo->blocksize, (uint32*) paddedtext->data, (const uint32*) paddedtext->data))
|
||||
if (blockEncryptCBC(ctxt->cipher.algo, ctxt->cipher.param, paddedtext->size / ctxt->cipher.algo->blocksize, (uint32_t*) paddedtext->data, (const uint32_t*) paddedtext->data))
|
||||
{
|
||||
free(paddedtext->data);
|
||||
free(paddedtext);
|
||||
|
@ -285,7 +291,7 @@ memchunk* dhaes_pContextEncrypt(dhaes_pContext* ctxt, mpnumber* ephemeralPublicK
|
|||
goto encrypt_end;
|
||||
}
|
||||
|
||||
if (keyedHashFunctionContextDigest(&ctxt->mac, mac))
|
||||
if (keyedHashFunctionContextDigestMP(&ctxt->mac, mac))
|
||||
{
|
||||
free(paddedtext->data);
|
||||
free(paddedtext);
|
||||
|
@ -320,9 +326,7 @@ memchunk* dhaes_pContextDecrypt(dhaes_pContext* ctxt, const mpnumber* ephemeralP
|
|||
goto decrypt_end;
|
||||
|
||||
/* decrypt the memchunk with CBC mode */
|
||||
/*@-sizeoftype@*/
|
||||
paddedtext = (memchunk*) calloc(1, sizeof(memchunk));
|
||||
/*@=sizeoftype@*/
|
||||
paddedtext = (memchunk*) calloc(1, sizeof(*paddedtext));
|
||||
|
||||
if (paddedtext == (memchunk*) 0)
|
||||
goto decrypt_end;
|
||||
|
@ -338,7 +342,7 @@ memchunk* dhaes_pContextDecrypt(dhaes_pContext* ctxt, const mpnumber* ephemeralP
|
|||
goto decrypt_end;
|
||||
}
|
||||
|
||||
if (blockDecrypt(ctxt->cipher.algo, ctxt->cipher.param, CBC, paddedtext->size / ctxt->cipher.algo->blocksize, (uint32*) paddedtext->data, (const uint32*) ciphertext->data))
|
||||
if (blockDecryptCBC(ctxt->cipher.algo, ctxt->cipher.param, paddedtext->size / ctxt->cipher.algo->blocksize, (uint32_t*) paddedtext->data, (const uint32_t*) ciphertext->data))
|
||||
{
|
||||
free(paddedtext->data);
|
||||
free(paddedtext);
|
||||
|
|
|
@ -35,26 +35,26 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
const dldp_p* param;
|
||||
const hashFunction* hash;
|
||||
const blockCipher* cipher;
|
||||
const dldp_p* param;
|
||||
const hashFunction* hash;
|
||||
const blockCipher* cipher;
|
||||
const keyedHashFunction* mac;
|
||||
int cipherkeybits;
|
||||
int mackeybits;
|
||||
int cipherkeybits;
|
||||
int mackeybits;
|
||||
} dhaes_pParameters;
|
||||
|
||||
/**
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
dldp_p param;
|
||||
mpnumber pub;
|
||||
mpnumber pri;
|
||||
hashFunctionContext hash;
|
||||
blockCipherContext cipher;
|
||||
dldp_p param;
|
||||
mpnumber pub;
|
||||
mpnumber pri;
|
||||
hashFunctionContext hash;
|
||||
blockCipherContext cipher;
|
||||
keyedHashFunctionContext mac;
|
||||
int cipherkeybits;
|
||||
int mackeybits;
|
||||
int cipherkeybits;
|
||||
int mackeybits;
|
||||
} dhaes_pContext;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
100
beecrypt/dldp.c
100
beecrypt/dldp.c
|
@ -35,12 +35,12 @@
|
|||
|
||||
/**
|
||||
*/
|
||||
static int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, /*@out@*/ uint32* wksp)
|
||||
static int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, /*@out@*/ mpw* wksp)
|
||||
/*@modifies dp->g, wksp @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
static int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, /*@out@*/ uint32* wksp)
|
||||
static int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, /*@out@*/ mpw* wksp)
|
||||
/*@modifies dp->g, wksp @*/;
|
||||
|
||||
int dldp_pPrivate(const dldp_p* dp, randomGeneratorContext* rgc, mpnumber* x)
|
||||
|
@ -81,9 +81,9 @@ int dldp_pPair(const dldp_p* dp, randomGeneratorContext* rgc, mpnumber* x, mpnum
|
|||
|
||||
int dldp_pEqual(const dldp_p* a, const dldp_p* b)
|
||||
{
|
||||
return mp32eqx(a->p.size, a->p.modl, b->p.size, b->p.modl) &&
|
||||
mp32eqx(a->q.size, a->q.modl, b->q.size, b->q.modl) &&
|
||||
mp32eqx(a->g.size, a->g.data, b->g.size, b->g.data);
|
||||
return mpeqx(a->p.size, a->p.modl, b->p.size, b->p.modl) &&
|
||||
mpeqx(a->q.size, a->q.modl, b->q.size, b->q.modl) &&
|
||||
mpeqx(a->g.size, a->g.data, b->g.size, b->g.data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,32 +92,32 @@ int dldp_pEqual(const dldp_p* a, const dldp_p* b)
|
|||
static int dldp_pValidate(const dldp_p* dp, randomGeneratorContext* rgc)
|
||||
/*@*/
|
||||
{
|
||||
register uint32 size = dp->p.size;
|
||||
register uint32* temp = (uint32*) malloc((8*size+2) * sizeof(*temp));
|
||||
register size_t size = dp->p.size;
|
||||
register mpw* temp = (mpw*) malloc((8*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
/* check that p > 2 and p odd, then run miller-rabin test with t 50 */
|
||||
if (mp32even(dp->p.size, dp->p.modl))
|
||||
if (mpeven(dp->p.size, dp->p.modl))
|
||||
{
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mp32pmilrab_w(&dp->p, rgc, 50, temp) == 0)
|
||||
if (mppmilrab_w(&dp->p, rgc, 50, temp) == 0)
|
||||
{
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* check that q > 2 and q odd, then run miller-rabin test with t 50 */
|
||||
if (mp32even(dp->q.size, dp->q.modl))
|
||||
if (mpeven(dp->q.size, dp->q.modl))
|
||||
{
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mp32pmilrab_w(&dp->q, rgc, 50, temp) == 0)
|
||||
if (mppmilrab_w(&dp->q, rgc, 50, temp) == 0)
|
||||
{
|
||||
free(temp);
|
||||
return 0;
|
||||
|
@ -126,10 +126,10 @@ static int dldp_pValidate(const dldp_p* dp, randomGeneratorContext* rgc)
|
|||
free(temp);
|
||||
|
||||
/* check that 1 < g < p */
|
||||
if (mp32leone(dp->g.size, dp->g.data))
|
||||
if (mpleone(dp->g.size, dp->g.data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(dp->g.size, dp->g.data, dp->p.size, dp->p.modl))
|
||||
if (mpgex(dp->g.size, dp->g.data, dp->p.size, dp->p.modl))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
|
@ -172,24 +172,25 @@ int dldp_pCopy(dldp_p* dst, const dldp_p* src)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int dldp_pgoqMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32 qsize, int cofactor)
|
||||
int dldp_pgoqMake(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits, size_t qbits, int cofactor)
|
||||
{
|
||||
/*
|
||||
* Generate parameters as described by IEEE P1363, A.16.1
|
||||
*/
|
||||
|
||||
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
|
||||
register size_t psize = MP_BITS_TO_WORDS(pbits + MP_WBITS - 1);
|
||||
register mpw* temp = (mpw*) malloc((8*psize+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
/* first generate q */
|
||||
/*@-globs@*/
|
||||
mp32prnd_w(&dp->q, rgc, qsize, mp32ptrials(qsize << 5), (const mpnumber*) 0, temp);
|
||||
mpprnd_w(&dp->q, rgc, qbits, mpptrials(qbits), (const mpnumber*) 0, temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/* generate p with the appropriate congruences */
|
||||
/*@-globs@*/
|
||||
mp32prndconone_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), &dp->q, (const mpnumber*) 0, &dp->r, cofactor, temp);
|
||||
mpprndconone_w(&dp->p, rgc, pbits, mpptrials(pbits), &dp->q, (const mpnumber*) 0, &dp->r, cofactor, temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/* clear n */
|
||||
|
@ -208,26 +209,27 @@ int dldp_pgoqMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32
|
|||
}
|
||||
|
||||
/*@-bounds@*/
|
||||
int dldp_pgoqMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
||||
int dldp_pgoqMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, size_t bits)
|
||||
{
|
||||
/*
|
||||
* Generate parameters with a safe prime; p = 2q+1 i.e. r=2
|
||||
*
|
||||
*/
|
||||
|
||||
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
|
||||
register size_t size = MP_BITS_TO_WORDS(bits + MP_WBITS - 1);
|
||||
register mpw* temp = (mpw*) malloc((8*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
/* generate p */
|
||||
/*@-globs@*/
|
||||
mp32prndsafe_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), temp);
|
||||
mpprndsafe_w(&dp->p, rgc, bits, mpptrials(bits), temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/* set q */
|
||||
mp32copy(psize, temp, dp->p.modl);
|
||||
mp32divtwo(psize, temp);
|
||||
mpbset(&dp->q, psize, temp);
|
||||
mpcopy(size, temp, dp->p.modl);
|
||||
mpdivtwo(size, temp);
|
||||
mpbset(&dp->q, size, temp);
|
||||
|
||||
/* set r = 2 */
|
||||
mpnsetw(&dp->r, 2);
|
||||
|
@ -245,13 +247,13 @@ int dldp_pgoqMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
|||
}
|
||||
/*@=bounds@*/
|
||||
|
||||
int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
||||
int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, mpw* wksp)
|
||||
{
|
||||
/*
|
||||
* Randomly determine a generator over the subgroup with order q
|
||||
*/
|
||||
|
||||
register uint32 size = dp->p.size;
|
||||
register size_t size = dp->p.size;
|
||||
|
||||
mpnfree(&dp->g);
|
||||
mpnsize(&dp->g, size);
|
||||
|
@ -264,7 +266,7 @@ int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
|||
/* first compute h^r mod p (stored in g) */
|
||||
mpbpowmod_w(&dp->p, size, dp->g.data, dp->r.size, dp->r.data, dp->g.data, wksp);
|
||||
|
||||
if (mp32isone(size, dp->g.data))
|
||||
if (mpisone(size, dp->g.data))
|
||||
continue;
|
||||
|
||||
return 0;
|
||||
|
@ -274,8 +276,8 @@ int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
|||
|
||||
int dldp_pgoqGenerator(dldp_p* dp, randomGeneratorContext* rgc)
|
||||
{
|
||||
register uint32 size = dp->p.size;
|
||||
register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(*temp));
|
||||
register size_t size = dp->p.size;
|
||||
register mpw* temp = (mpw*) malloc((4*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
@ -304,24 +306,25 @@ int dldp_pgoqValidate(const dldp_p* dp, randomGeneratorContext* rgc, /*@unused@*
|
|||
return 1;
|
||||
}
|
||||
|
||||
int dldp_pgonMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32 qsize)
|
||||
int dldp_pgonMake(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits, size_t qbits)
|
||||
{
|
||||
/*
|
||||
* Generate parameters with a prime p such that p = qr+1, with q prime, and r = 2s, with s prime
|
||||
*/
|
||||
|
||||
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
|
||||
register size_t psize = MP_BITS_TO_WORDS(pbits + MP_WBITS - 1);
|
||||
register mpw* temp = (mpw*) malloc((8*psize+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
/* generate q */
|
||||
/*@-globs@*/
|
||||
mp32prnd_w(&dp->q, rgc, qsize, mp32ptrials(qsize << 5), (const mpnumber*) 0, temp);
|
||||
mpprnd_w(&dp->q, rgc, qbits, mpptrials(qbits), (const mpnumber*) 0, temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/* generate p with the appropriate congruences */
|
||||
/*@-globs@*/
|
||||
mp32prndconone_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), &dp->q, (const mpnumber*) 0, &dp->r, 2, temp);
|
||||
mpprndconone_w(&dp->p, rgc, pbits, mpptrials(pbits), &dp->q, (const mpnumber*) 0, &dp->r, 2, temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/* set n */
|
||||
|
@ -337,19 +340,20 @@ int dldp_pgonMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32
|
|||
return -1;
|
||||
}
|
||||
|
||||
int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
||||
int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits)
|
||||
{
|
||||
/*
|
||||
* Generate parameters with a safe prime; i.e. p = 2q+1, where q is prime
|
||||
*/
|
||||
|
||||
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
|
||||
register size_t psize = MP_BITS_TO_WORDS(pbits + MP_WBITS - 1);
|
||||
register mpw* temp = (mpw*) malloc((8*psize+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
/* generate safe p */
|
||||
/*@-globs@*/
|
||||
mp32prndsafe_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), temp);
|
||||
mpprndsafe_w(&dp->p, rgc, pbits, mpptrials(pbits), temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/* set n */
|
||||
|
@ -357,8 +361,8 @@ int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
|||
mpbset(&dp->n, psize, temp);
|
||||
|
||||
/* set q */
|
||||
mp32copy(psize, temp, dp->p.modl);
|
||||
mp32divtwo(psize, temp);
|
||||
mpcopy(psize, temp, dp->p.modl);
|
||||
mpdivtwo(psize, temp);
|
||||
mpbset(&dp->q, psize, temp);
|
||||
|
||||
/* set r = 2 */
|
||||
|
@ -373,9 +377,9 @@ int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
|||
return -1;
|
||||
}
|
||||
|
||||
int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
||||
int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, mpw* wksp)
|
||||
{
|
||||
register uint32 size = dp->p.size;
|
||||
register size_t size = dp->p.size;
|
||||
|
||||
mpnfree(&dp->g);
|
||||
mpnsize(&dp->g, size);
|
||||
|
@ -384,7 +388,7 @@ int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
|||
{
|
||||
mpbrnd_w(&dp->p, rgc, dp->g.data, wksp);
|
||||
|
||||
if (mp32istwo(dp->r.size, dp->r.data))
|
||||
if (mpistwo(dp->r.size, dp->r.data))
|
||||
{
|
||||
/*
|
||||
* A little math here: the only element in the group which has order 2 is (p-1);
|
||||
|
@ -395,7 +399,7 @@ int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
|||
|
||||
/* check g^q mod p*/
|
||||
mpbpowmod_w(&dp->p, size, dp->g.data, dp->q.size, dp->q.modl, wksp, wksp+size);
|
||||
if (mp32isone(size, wksp))
|
||||
if (mpisone(size, wksp))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
@ -410,25 +414,25 @@ int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
|||
*/
|
||||
|
||||
/* compute s = r/2 */
|
||||
mp32setx(size, wksp, dp->r.size, dp->r.data);
|
||||
mp32divtwo(size, wksp);
|
||||
mpsetx(size, wksp, dp->r.size, dp->r.data);
|
||||
mpdivtwo(size, wksp);
|
||||
|
||||
/* compute t = g^s mod p */
|
||||
mpbpowmod_w(&dp->p, size, dp->g.data, size, wksp, wksp+size, wksp+2*size);
|
||||
/* compute t^2 mod p = g^2s mod p = g^r mod p*/
|
||||
mpbsqrmod_w(&dp->p, size, wksp+size, wksp+size, wksp+2*size);
|
||||
if (mp32isone(size, wksp+size))
|
||||
if (mpisone(size, wksp+size))
|
||||
continue;
|
||||
|
||||
/* compute t^q mod p = g^qs mod p */
|
||||
mpbpowmod_w(&dp->p, size, wksp, dp->q.size, dp->q.modl, wksp+size, wksp+2*size);
|
||||
if (mp32isone(size, wksp+size))
|
||||
if (mpisone(size, wksp+size))
|
||||
continue;
|
||||
|
||||
/* compute g^2q mod p */
|
||||
mpbpowmod_w(&dp->p, size, dp->g.data, dp->q.size, dp->q.modl, wksp, wksp+size);
|
||||
mpbsqrmod_w(&dp->p, size, wksp, wksp+size, wksp+2*size);
|
||||
if (mp32isone(size, wksp+size))
|
||||
if (mpisone(size, wksp+size))
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -440,8 +444,8 @@ int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
|
|||
|
||||
int dldp_pgonGenerator(dldp_p* dp, randomGeneratorContext* rgc)
|
||||
{
|
||||
register uint32 psize = dp->p.size;
|
||||
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
|
||||
register size_t psize = dp->p.size;
|
||||
register mpw* temp = (mpw*) malloc((8*psize+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
|
|
@ -121,13 +121,13 @@ int dldp_pEqual(const dldp_p* a, const dldp_p* b)
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int dldp_pgoqMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32 qsize, int cofactor)
|
||||
int dldp_pgoqMake(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits, size_t qbits, int cofactor)
|
||||
/*@modifies dp->p, dp->q, dp->r, dp->g, dp->n, rgc @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int dldp_pgoqMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
||||
int dldp_pgoqMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits)
|
||||
/*@modifies dp->p, dp->q, dp->r, dp->g, dp->n, rgc @*/;
|
||||
|
||||
/**
|
||||
|
@ -149,13 +149,13 @@ int dldp_pgoqValidate(const dldp_p*, randomGeneratorContext* rgc, int cofactor)
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int dldp_pgonMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32 qsize)
|
||||
int dldp_pgonMake(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits, size_t qbits)
|
||||
/*@modifies dp->p, dp->q, dp->r, dp->g, dp->n, rgc @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
|
||||
int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, size_t pbits)
|
||||
/*@modifies dp->p, dp->q, dp->r, dp->g, dp->n, rgc @*/;
|
||||
|
||||
/**
|
||||
|
|
|
@ -65,7 +65,7 @@ int dlpk_pCopy(dlpk_p* dst, const dlpk_p* src)
|
|||
int dlpk_pEqual(const dlpk_p* a, const dlpk_p* b)
|
||||
{
|
||||
return dldp_pEqual(&a->param, &b->param) &&
|
||||
mp32eqx(a->y.size, a->y.data, b->y.size, b->y.data);
|
||||
mpeqx(a->y.size, a->y.data, b->y.size, b->y.data);
|
||||
}
|
||||
|
||||
int dlpk_pgoqValidate(const dlpk_p* pk, randomGeneratorContext* rgc, int cofactor)
|
||||
|
@ -75,10 +75,10 @@ int dlpk_pgoqValidate(const dlpk_p* pk, randomGeneratorContext* rgc, int cofacto
|
|||
if (rc <= 0)
|
||||
return rc;
|
||||
|
||||
if (mp32leone(pk->y.size, pk->y.data))
|
||||
if (mpleone(pk->y.size, pk->y.data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(pk->y.size, pk->y.data, pk->param.p.size, pk->param.p.modl))
|
||||
if (mpgex(pk->y.size, pk->y.data, pk->param.p.size, pk->param.p.modl))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
|
@ -91,10 +91,10 @@ int dlpk_pgonValidate(const dlpk_p* pk, randomGeneratorContext* rgc)
|
|||
if (rc <= 0)
|
||||
return rc;
|
||||
|
||||
if (mp32leone(pk->y.size, pk->y.data))
|
||||
if (mpleone(pk->y.size, pk->y.data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(pk->y.size, pk->y.data, pk->param.p.size, pk->param.p.modl))
|
||||
if (mpgex(pk->y.size, pk->y.data, pk->param.p.size, pk->param.p.modl))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
|
|
|
@ -35,6 +35,17 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
/**
|
||||
* Computes the shared secret.
|
||||
*
|
||||
* Equation:
|
||||
*
|
||||
* \li \f$s=y^{x}\ \textrm{mod}\ p\f$
|
||||
*
|
||||
* @param dp domain parameters
|
||||
* @param x private value
|
||||
* @param y public value (of the peer)
|
||||
* @param s computed secret value
|
||||
* @retval 0 on success, -1 on failure.
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int dlsvdp_pDHSecret(const dldp_p* dp, const mpnumber* x, const mpnumber* y, mpnumber* s)
|
||||
|
|
|
@ -50,22 +50,23 @@
|
|||
/*@-boundswrite@*/
|
||||
int dsasign(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, randomGeneratorContext* rgc, const mpnumber* hm, const mpnumber* x, mpnumber* r, mpnumber* s)
|
||||
{
|
||||
register uint32 psize = p->size;
|
||||
register uint32 qsize = q->size;
|
||||
register size_t psize = p->size;
|
||||
register size_t qsize = q->size;
|
||||
|
||||
register uint32* ptemp;
|
||||
register uint32* qtemp;
|
||||
register mpw* ptemp;
|
||||
register mpw* qtemp;
|
||||
|
||||
register uint32* pwksp;
|
||||
register uint32* qwksp;
|
||||
register mpw* pwksp;
|
||||
register mpw* qwksp;
|
||||
|
||||
register int rc = -1; /* assume failure */
|
||||
register int rc = -1;
|
||||
|
||||
ptemp = (uint32*) malloc((5*psize+2) * sizeof(*ptemp));
|
||||
if (ptemp == NULL)
|
||||
ptemp = (mpw*) malloc((5*psize+2) * sizeof(*ptemp));
|
||||
if (ptemp == (mpw*) 0)
|
||||
return rc;
|
||||
qtemp = (uint32*) malloc((14*qsize+11) * sizeof(*qtemp));
|
||||
if (qtemp == NULL) {
|
||||
qtemp = (mpw*) malloc((14*qsize+11) * sizeof(*qtemp));
|
||||
if (qtemp == (mpw*) 0)
|
||||
{
|
||||
free(ptemp);
|
||||
return rc;
|
||||
}
|
||||
|
@ -80,22 +81,12 @@ int dsasign(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, randomGen
|
|||
/* get a random k, invertible modulo q */
|
||||
mpbrndinv_w(q, rgc, qtemp, qtemp+qsize, qwksp);
|
||||
|
||||
/* FIPS 186 test vectors
|
||||
qtemp[0] = 0x358dad57;
|
||||
qtemp[1] = 0x1462710f;
|
||||
qtemp[2] = 0x50e254cf;
|
||||
qtemp[3] = 0x1a376b2b;
|
||||
qtemp[4] = 0xdeaadfbf;
|
||||
|
||||
mpbinv_w(q, qsize, qtemp, qtemp+qsize, qwksp);
|
||||
*/
|
||||
|
||||
/* g^k mod p */
|
||||
mpbpowmod_w(p, g->size, g->data, qsize, qtemp, ptemp, pwksp);
|
||||
|
||||
/* (g^k mod p) mod q - simple modulo */
|
||||
mp32nmod(qtemp+2*qsize, psize, ptemp, qsize, q->modl, pwksp);
|
||||
mp32copy(qsize, r->data, qtemp+psize+qsize);
|
||||
mpnmod(qtemp+2*qsize, psize, ptemp, qsize, q->modl, pwksp);
|
||||
mpcopy(qsize, r->data, qtemp+psize+qsize);
|
||||
|
||||
/* allocate s */
|
||||
mpnfree(s);
|
||||
|
@ -121,35 +112,35 @@ int dsasign(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, randomGen
|
|||
|
||||
int dsavrfy(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
|
||||
{
|
||||
register uint32 psize = p->size;
|
||||
register uint32 qsize = q->size;
|
||||
register size_t psize = p->size;
|
||||
register size_t qsize = q->size;
|
||||
|
||||
register uint32* ptemp;
|
||||
register uint32* qtemp;
|
||||
register mpw* ptemp;
|
||||
register mpw* qtemp;
|
||||
|
||||
register uint32* pwksp;
|
||||
register mpw* pwksp;
|
||||
register uint32* qwksp;
|
||||
|
||||
register int rc = 0; /* XXX shouldn't this be -1 ?*/
|
||||
register int rc = 0;
|
||||
|
||||
if (mp32z(r->size, r->data))
|
||||
if (mpz(r->size, r->data))
|
||||
return rc;
|
||||
|
||||
if (mp32gex(r->size, r->data, qsize, q->modl))
|
||||
if (mpgex(r->size, r->data, qsize, q->modl))
|
||||
return rc;
|
||||
|
||||
if (mp32z(s->size, s->data))
|
||||
if (mpz(s->size, s->data))
|
||||
return rc;
|
||||
|
||||
if (mp32gex(s->size, s->data, qsize, q->modl))
|
||||
if (mpgex(s->size, s->data, qsize, q->modl))
|
||||
return rc;
|
||||
|
||||
ptemp = (uint32*) malloc((6*psize+2) * sizeof(*ptemp));
|
||||
if (ptemp == NULL)
|
||||
ptemp = (mpw*) malloc((6*psize+2) * sizeof(*ptemp));
|
||||
if (ptemp == (mpw*) 0)
|
||||
return rc;
|
||||
|
||||
qtemp = (uint32*) malloc((13*qsize+11) * sizeof(*qtemp));
|
||||
if (qtemp == NULL) {
|
||||
qtemp = (mpw*) malloc((13*qsize+11) * sizeof(*qtemp));
|
||||
if (qtemp == (mpw*) 0) {
|
||||
free(ptemp);
|
||||
return rc;
|
||||
}
|
||||
|
@ -176,9 +167,9 @@ int dsavrfy(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, const mpn
|
|||
mpbmulmod_w(p, psize, ptemp, psize, ptemp+psize, ptemp, pwksp);
|
||||
|
||||
/* modulo q */
|
||||
mp32nmod(ptemp+psize, psize, ptemp, qsize, q->modl, pwksp);
|
||||
mpnmod(ptemp+psize, psize, ptemp, qsize, q->modl, pwksp);
|
||||
|
||||
rc = mp32eqx(r->size, r->data, psize, ptemp+psize);
|
||||
rc = mpeqx(r->size, r->data, psize, ptemp+psize);
|
||||
}
|
||||
|
||||
free(qtemp);
|
||||
|
|
|
@ -35,12 +35,49 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
/**
|
||||
* The raw DSA signing function.
|
||||
*
|
||||
* Signing equations:
|
||||
*
|
||||
* \li \f$r=(g^{k}\ \textrm{mod}\ p)\ \textrm{mod}\ q\f$
|
||||
* \li \f$s=k^{-1}(h(m)+xr)\ \textrm{mod}\ q\f$
|
||||
*
|
||||
* @param p The prime.
|
||||
* @param q The cofactor.
|
||||
* @param g The generator.
|
||||
* @param rgc The pseudo-random generator context.
|
||||
* @param hm The hash to be signed.
|
||||
* @param x The private key value.
|
||||
* @param r The signature's \e r value.
|
||||
* @param s The signature's \e r value.
|
||||
* @retval 0 on success, -1 on failure.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int dsasign(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, randomGeneratorContext* rgc, const mpnumber* hm, const mpnumber* x, mpnumber* r, mpnumber* s)
|
||||
/*@modifies r->size, r->data, *r->data, s->size, s->data @*/;
|
||||
|
||||
/**
|
||||
* The raw DSA verification function.
|
||||
*
|
||||
* Verifying equations:
|
||||
* \li Check \f$0<r<q\f$ and \f$0<s<q\f$
|
||||
* \li \f$w=s^{-1}\ \textrm{mod}\ q\f$
|
||||
* \li \f$u_1=w \cdot h(m)\ \textrm{mod}\ q\f$
|
||||
* \li \f$u_2=rw\ \textrm{mod}\ q\f$
|
||||
* \li \f$v=(g^{u_1}y^{u_2}\ \textrm{mod}\ p)\ \textrm{mod}\ q\f$
|
||||
* \li Check \f$v=r\f$
|
||||
*
|
||||
* @warning The return type of this function should be a boolean, but since
|
||||
* that type isn't portable, an int is used.
|
||||
*
|
||||
* @param p The prime.
|
||||
* @param q The cofactor.
|
||||
* @param g The generator.
|
||||
* @param hm The digest to be verified.
|
||||
* @param y The public key value.
|
||||
* @param r The signature's r value.
|
||||
* @param s The signature's r value.
|
||||
* @retval 0 on failure, 1 on success.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int dsavrfy(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
|
||||
|
|
|
@ -62,8 +62,8 @@
|
|||
|
||||
int elgv1sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomGeneratorContext* rgc, const mpnumber* hm, const mpnumber* x, mpnumber* r, mpnumber* s)
|
||||
{
|
||||
register uint32 size = p->size;
|
||||
register uint32* temp = (uint32*) malloc((13*size+11) * sizeof(*temp));
|
||||
register size_t size = p->size;
|
||||
register mpw* temp = (mpw*) malloc((13*size+11) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
@ -79,8 +79,8 @@ int elgv1sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomG
|
|||
mpbmulmod_w(n, x->size, x->data, r->size, r->data, temp, temp+2*size);
|
||||
|
||||
/* compute -(x*r) mod n */
|
||||
mp32neg(size, temp);
|
||||
(void) mp32add(size, temp, n->modl);
|
||||
mpneg(size, temp);
|
||||
(void) mpadd(size, temp, n->modl);
|
||||
|
||||
/* compute h(m) - x*r mod n */
|
||||
mpbaddmod_w(n, hm->size, hm->data, size, temp, temp, temp+2*size);
|
||||
|
@ -99,22 +99,22 @@ int elgv1sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomG
|
|||
|
||||
int elgv1vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
|
||||
{
|
||||
register uint32 size = p->size;
|
||||
register uint32* temp;
|
||||
register size_t size = p->size;
|
||||
register mpw* temp;
|
||||
|
||||
if (mp32z(r->size, r->data))
|
||||
if (mpz(r->size, r->data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(r->size, r->data, size, p->modl))
|
||||
if (mpgex(r->size, r->data, size, p->modl))
|
||||
return 0;
|
||||
|
||||
if (mp32z(s->size, s->data))
|
||||
if (mpz(s->size, s->data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(s->size, s->data, n->size, n->modl))
|
||||
if (mpgex(s->size, s->data, n->size, n->modl))
|
||||
return 0;
|
||||
|
||||
temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
|
||||
temp = (mpw*) malloc((6*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
@ -126,13 +126,13 @@ int elgv1vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const m
|
|||
/* compute u2 = r^s mod p */
|
||||
mpbpowmod_w(p, r->size, r->data, s->size, s->data, temp+size, temp+2*size);
|
||||
|
||||
/* compute v2 = u1*u2 mod p */
|
||||
/* compute v1 = u1*u2 mod p */
|
||||
mpbmulmod_w(p, size, temp, size, temp+size, temp+size, temp+2*size);
|
||||
|
||||
/* compute v1 = g^h(m) mod p */
|
||||
/* compute v2 = g^h(m) mod p */
|
||||
mpbpowmod_w(p, g->size, g->data, hm->size, hm->data, temp, temp+2*size);
|
||||
|
||||
rc = mp32eq(size, temp, temp+size);
|
||||
rc = mpeq(size, temp, temp+size);
|
||||
|
||||
free(temp);
|
||||
|
||||
|
@ -143,8 +143,8 @@ int elgv1vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const m
|
|||
|
||||
int elgv3sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomGeneratorContext* rgc, const mpnumber* hm, const mpnumber* x, mpnumber* r, mpnumber* s)
|
||||
{
|
||||
register uint32 size = p->size;
|
||||
register uint32* temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
|
||||
register size_t size = p->size;
|
||||
register mpw* temp = (mpw*) malloc((6*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
@ -176,22 +176,22 @@ int elgv3sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomG
|
|||
|
||||
int elgv3vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
|
||||
{
|
||||
register uint32 size = p->size;
|
||||
register uint32* temp;
|
||||
register size_t size = p->size;
|
||||
register mpw* temp;
|
||||
|
||||
if (mp32z(r->size, r->data))
|
||||
if (mpz(r->size, r->data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(r->size, r->data, size, p->modl))
|
||||
if (mpgex(r->size, r->data, size, p->modl))
|
||||
return 0;
|
||||
|
||||
if (mp32z(s->size, s->data))
|
||||
if (mpz(s->size, s->data))
|
||||
return 0;
|
||||
|
||||
if (mp32gex(s->size, s->data, n->size, n->modl))
|
||||
if (mpgex(s->size, s->data, n->size, n->modl))
|
||||
return 0;
|
||||
|
||||
temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
|
||||
temp = (mpw*) malloc((6*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
@ -209,7 +209,7 @@ int elgv3vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const m
|
|||
/* compute v1 = g^s mod p */
|
||||
mpbpowmod_w(p, g->size, g->data, s->size, s->data, temp, temp+2*size);
|
||||
|
||||
rc = mp32eq(size, temp, temp+size);
|
||||
rc = mpeq(size, temp, temp+size);
|
||||
|
||||
free(temp);
|
||||
|
||||
|
|
|
@ -35,24 +35,96 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
/**
|
||||
* The raw ElGamal signing funcion, variant 1.
|
||||
*
|
||||
* Signing equations:
|
||||
*
|
||||
* \li \f$r=g^{k}\ \textrm{mod}\ p\f$
|
||||
* \li \f$s=k^{-1}(h(m)-xr)\ \textrm{mod}\ (p-1)\f$
|
||||
*
|
||||
* @param p The prime.
|
||||
* @param n The reducer mod (p-1).
|
||||
* @param g The generator.
|
||||
* @param rgc The pseudo-random generat
|
||||
* @param hm The hash to be signed.
|
||||
* @param x The private key value.
|
||||
* @param r The signature's \e r value.
|
||||
* @param s The signature's \e r value.
|
||||
* @retval 0 on success, -1 on failure.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int elgv1sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomGeneratorContext*, const mpnumber* hm, const mpnumber* x, mpnumber* r, mpnumber* s)
|
||||
/*@modifies r, s */;
|
||||
|
||||
/**
|
||||
* The raw ElGamal signing funcion, variant 3.
|
||||
*
|
||||
* Signing equations:
|
||||
*
|
||||
* \li \f$r=g^{k}\ \textrm{mod}\ p\f$
|
||||
* \li \f$s=xr+kh(m)\ \textrm{mod}\ (p-1)\f$
|
||||
*
|
||||
* @param p The prime.
|
||||
* @param n The reducer mod (p-1).
|
||||
* @param g The generator.
|
||||
* @param rgc The pseudo-random generat
|
||||
* @param hm The hash to be signed.
|
||||
* @param x The private key value.
|
||||
* @param r The signature's \e r value.
|
||||
* @param s The signature's \e r value.
|
||||
* @retval 0 on success, -1 on failure.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int elgv3sign(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, randomGeneratorContext*, const mpnumber* hm, const mpnumber* x, mpnumber* r, mpnumber* s)
|
||||
/*@modifies r, s */;
|
||||
|
||||
/**
|
||||
* The raw ElGamal verification funcion, variant 1.
|
||||
*
|
||||
* Verifying equations:
|
||||
*
|
||||
* \li Check \f$0<r<p\f$ and \f$0<s<(p-1)\f$
|
||||
* \li \f$v_1=y^{r}r^{s}\ \textrm{mod}\ p\f$
|
||||
* \li \f$v_2=g^{h(m)}\ \textrm{mod}\ p\f$
|
||||
* \li Check \f$v_1=v_2\f$
|
||||
*
|
||||
* @warning The return type of this function should be a boolean, but since
|
||||
* that type isn't as portable, an int is used.
|
||||
*
|
||||
* @param p The prime.
|
||||
* @param n The reducer mod (p-1).
|
||||
* @param g The generator.
|
||||
* @param hm The hash to be signed.
|
||||
* @param y The public key value.
|
||||
* @param r The signature's \e r value.
|
||||
* @param s The signature's \e r value.
|
||||
* @retval 1 on success, 0 on failure.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int elgv1vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
* The raw ElGamal verification funcion, variant 3.
|
||||
*
|
||||
* Verifying equations:
|
||||
*
|
||||
* \li Check \f$0<r<p\f$ and \f$0<s<(p-1)\f$
|
||||
* \li \f$v_1=g^{s}\ \textrm{mod}\ p\f$
|
||||
* \li \f$v_2=y^{r}r^{h(m)}\ \textrm{mod}\ p\f$
|
||||
* \li Check \f$v_1=v_2\f$
|
||||
*
|
||||
* \warning The return type of this function should be a boolean, but since
|
||||
* that type isn't as portable, an int is used.
|
||||
*
|
||||
* @param p The prime.
|
||||
* @param n The reducer mod (p-1).
|
||||
* @param g The generator.
|
||||
* @param hm The hash to be signed.
|
||||
* @param y The public key value.
|
||||
* @param r The signature's \e r value.
|
||||
* @param s The signature's \e r value.
|
||||
* @retval 1 on success, 0 on failure.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int elgv3vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
|
||||
|
|
|
@ -28,19 +28,19 @@
|
|||
#include "debug.h"
|
||||
|
||||
/*@-shiftimplementation@*/
|
||||
int16 swap16(int16 n)
|
||||
int16_t swap16(int16_t n)
|
||||
{
|
||||
return ( ((n & 0xff) << 8) |
|
||||
((n & 0xff00) >> 8) );
|
||||
}
|
||||
|
||||
uint16 swapu16(uint16 n)
|
||||
uint16_t swapu16(uint16_t n)
|
||||
{
|
||||
return ( ((n & 0xffU) << 8) |
|
||||
((n & 0xff00U) >> 8) );
|
||||
}
|
||||
|
||||
int32 swap32(int32 n)
|
||||
int32_t swap32(int32_t n)
|
||||
{
|
||||
return ( ((n & 0xff) << 24) |
|
||||
((n & 0xff00) << 8) |
|
||||
|
@ -48,7 +48,7 @@ int32 swap32(int32 n)
|
|||
((n & 0xff000000) >> 24) );
|
||||
}
|
||||
|
||||
uint32 swapu32(uint32 n)
|
||||
uint32_t swapu32(uint32_t n)
|
||||
{
|
||||
return ( ((n & 0xffU) << 24) |
|
||||
((n & 0xff00U) << 8) |
|
||||
|
@ -56,7 +56,7 @@ uint32 swapu32(uint32 n)
|
|||
((n & 0xff000000U) >> 24) );
|
||||
}
|
||||
|
||||
int64 swap64(int64 n)
|
||||
int64_t swap64(int64_t n)
|
||||
{
|
||||
#if HAVE_LONG_LONG
|
||||
return ( ((n & 0xffLL) << 56) |
|
||||
|
@ -121,36 +121,6 @@ int encodeLong(javalong l, byte* data)
|
|||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int encodeFloat(javafloat f, byte* data)
|
||||
{
|
||||
#if (!WORDS_BIGENDIAN)
|
||||
register const byte* src = ((const byte*) &f) + 3;
|
||||
register int i;
|
||||
for (i = 0; i < 4; i++)
|
||||
data[i] = *(src--);
|
||||
#else
|
||||
memcpy(data, &f, 4);
|
||||
#endif
|
||||
return 4;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int encodeDouble(javadouble d, byte* data)
|
||||
{
|
||||
#if (!WORDS_BIGENDIAN)
|
||||
register const byte* src = ((byte*) &d) + 7;
|
||||
register int i;
|
||||
for (i = 0; i < 8; i++)
|
||||
data[i] = *(src--);
|
||||
#else
|
||||
memcpy(data, &d, 8);
|
||||
#endif
|
||||
return 8;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int encodeChar(javachar c, byte* data)
|
||||
{
|
||||
|
@ -297,36 +267,6 @@ int decodeLong(javalong* l, const byte* data)
|
|||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int decodeFloat(javafloat* f, const byte* data)
|
||||
{
|
||||
#if (WORDS_BIGENDIAN)
|
||||
memcpy(f, data, 4);
|
||||
#else
|
||||
register byte *dst = ((byte*) f) + 3;
|
||||
register int i;
|
||||
for (i = 0; i < 4; i++)
|
||||
*(dst--) = data[i];
|
||||
#endif
|
||||
return 4;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int decodeDouble(javadouble* d, const byte* data)
|
||||
{
|
||||
#if (WORDS_BIGENDIAN)
|
||||
memcpy(d, data, 8);
|
||||
#else
|
||||
register byte *dst = ((byte*) d) + 7;
|
||||
register int i;
|
||||
for (i = 0; i < 8; i++)
|
||||
*(dst--) = data[i];
|
||||
#endif
|
||||
return 8;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int decodeChar(javachar* c, const byte* data)
|
||||
{
|
||||
|
@ -367,7 +307,7 @@ int decodeIntsPartial(javaint* i, const byte* data, int bytecount)
|
|||
#if (WORDS_BIGENDIAN)
|
||||
memcpy(i, data, rc);
|
||||
if (rc & 0x3)
|
||||
memset(i + (rc >> 2), 0, 4 - (rc & 0x3));
|
||||
memset(((byte*) i) + rc, 0, 4 - (rc & 0x3));
|
||||
#else
|
||||
javaint tmp;
|
||||
while (bytecount >= 4)
|
||||
|
|
|
@ -31,19 +31,19 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
inline int16 swap16(int16 n)
|
||||
inline int16_t swap16(int16_t n)
|
||||
{
|
||||
return ( ((n & 0xff) << 8) |
|
||||
((n & 0xff00) >> 8) );
|
||||
}
|
||||
|
||||
inline uint16 swapu16(uint16 n)
|
||||
inline uint16_t swapu16(uint16_t n)
|
||||
{
|
||||
return ( ((n & 0xffU) << 8) |
|
||||
((n & 0xff00U) >> 8) );
|
||||
}
|
||||
|
||||
inline int32 swap32(int32 n)
|
||||
inline int32_t swap32(int32_t n)
|
||||
{
|
||||
#if (SIZEOF_LONG == 4)
|
||||
return ( ((n & 0xff) << 24) |
|
||||
|
@ -58,7 +58,7 @@ inline int32 swap32(int32 n)
|
|||
#endif
|
||||
}
|
||||
|
||||
inline uint32 swapu32(uint32 n)
|
||||
inline uint32_t swapu32(uint32_t n)
|
||||
{
|
||||
#if (SIZEOF_UNSIGNED_LONG == 4)
|
||||
return ( ((n & 0xffU) << 24) |
|
||||
|
@ -73,7 +73,7 @@ inline uint32 swapu32(uint32 n)
|
|||
#endif
|
||||
}
|
||||
|
||||
inline int64 swap64(int64 n)
|
||||
inline int64_t swap64(int64_t n)
|
||||
{
|
||||
#if HAVE_LONG_LONG
|
||||
return ( ((n & 0xffLL) << 56) |
|
||||
|
@ -100,27 +100,27 @@ inline int64 swap64(int64 n)
|
|||
|
||||
/**
|
||||
*/
|
||||
int16 swap16 (int16 n)
|
||||
int16_t swap16 (int16_t n)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
uint16 swapu16(uint16 n)
|
||||
uint16_t swapu16(uint16_t n)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
int32 swap32 (int32 n)
|
||||
int32_t swap32 (int32_t n)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
uint32 swapu32(uint32 n)
|
||||
uint32_t swapu32(uint32_t n)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
int64 swap64 (int64 n)
|
||||
int64_t swap64 (int64_t n)
|
||||
/*@*/;
|
||||
/*@=exportlocal@*/
|
||||
#endif
|
||||
|
@ -160,18 +160,6 @@ BEECRYPTAPI /*@unused@*/
|
|||
int encodeChar(javachar c, /*@out@*/ byte* data)
|
||||
/*@modifies data */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int encodeFloat(javafloat f, /*@out@*/ byte* data)
|
||||
/*@modifies data */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int encodeDouble(javadouble d, /*@out@*/ byte* data)
|
||||
/*@modifies data */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
|
@ -226,18 +214,6 @@ BEECRYPTAPI /*@unused@*/
|
|||
int decodeChar(/*@out@*/ javachar* c, const byte* data)
|
||||
/*@modifies c */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int decodeFloat(/*@out@*/ javafloat* f, const byte* data)
|
||||
/*@modifies f */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int decodeDouble(/*@out@*/ javadouble* d, const byte* data)
|
||||
/*@modifies d */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
|
|
|
@ -166,7 +166,7 @@ static int entropy_noise_filter(void* sampledata, int samplecount, int samplesiz
|
|||
{
|
||||
case 1:
|
||||
{
|
||||
uint8* samples = (uint8*) sampledata;
|
||||
uint8_t* samples = (uint8_t*) sampledata;
|
||||
|
||||
switch (channels)
|
||||
{
|
||||
|
@ -240,7 +240,7 @@ static int entropy_noise_filter(void* sampledata, int samplecount, int samplesiz
|
|||
|
||||
case 2:
|
||||
{
|
||||
uint16* samples = (uint16*) sampledata;
|
||||
uint16_t* samples = (uint16_t*) sampledata;
|
||||
|
||||
switch (channels)
|
||||
{
|
||||
|
@ -341,17 +341,17 @@ static int entropy_noise_filter(void* sampledata, int samplecount, int samplesiz
|
|||
*/
|
||||
|
||||
#if WIN32
|
||||
static int entropy_noise_gather(HWAVEIN wavein, int samplesize, int channels, int swap, int timeout, uint32 *data, int size)
|
||||
static int entropy_noise_gather(HWAVEIN wavein, int samplesize, int channels, int swap, int timeout, byte* data, size_t size)
|
||||
#else
|
||||
/*@-boundswrite@*/
|
||||
/*@-mustmod@*/ /* data is modified, annotations incorrect */
|
||||
static int entropy_noise_gather(int fd, int samplesize, int channels, int swap, int timeout, /*@out@*/ uint32 *data, int size)
|
||||
static int entropy_noise_gather(int fd, int samplesize, int channels, int swap, int timeout, /*@out@*/ byte* data, size_t size)
|
||||
/*@globals errno, fileSystem @*/
|
||||
/*@modifies data, errno, fileSystem @*/
|
||||
#endif
|
||||
{
|
||||
uint32 randombits = ((unsigned)size) << 5;
|
||||
uint32 temp = 0;
|
||||
size_t randombits = ((unsigned)size) << 5;
|
||||
byte temp = 0;
|
||||
int rc, i;
|
||||
|
||||
byte* sampledata = (byte*) malloc(1024 * samplesize * channels);
|
||||
|
@ -521,7 +521,7 @@ static int entropy_noise_gather(int fd, int samplesize, int channels, int swap,
|
|||
{
|
||||
case 1:
|
||||
{
|
||||
uint8* samples = (uint8*) sampledata;
|
||||
uint8_t* samples = (uint8_t*) sampledata;
|
||||
|
||||
for (i = 0; randombits && (i < 1024); i += 2)
|
||||
{
|
||||
|
@ -539,7 +539,7 @@ static int entropy_noise_gather(int fd, int samplesize, int channels, int swap,
|
|||
|
||||
case 2:
|
||||
{
|
||||
uint16* samples = (uint16*) sampledata;
|
||||
uint16_t* samples = (uint16_t*) sampledata;
|
||||
|
||||
for (i = 0; randombits && (i < 1024); i += 2)
|
||||
{
|
||||
|
@ -580,7 +580,7 @@ static int entropy_noise_gather(int fd, int samplesize, int channels, int swap,
|
|||
#endif
|
||||
|
||||
#if WIN32
|
||||
int entropy_wavein(uint32* data, int size)
|
||||
int entropy_wavein(byte* data, size_t size)
|
||||
{
|
||||
const char *timeout_env = getenv("BEECRYPT_ENTROPY_WAVEIN_TIMEOUT");
|
||||
|
||||
|
@ -697,10 +697,9 @@ int entropy_wavein(uint32* data, int size)
|
|||
return rc;
|
||||
}
|
||||
|
||||
int entropy_console(uint32* data, int size)
|
||||
int entropy_console(byte* data, size_t size)
|
||||
{
|
||||
register uint32 randombits = size << 5;
|
||||
register uint32 temp = 0;
|
||||
register size_t randombits = size << 3;
|
||||
|
||||
HANDLE hStdin;
|
||||
DWORD inRet;
|
||||
|
@ -733,13 +732,9 @@ int entropy_console(uint32* data, int size)
|
|||
}
|
||||
|
||||
/* get 8 bits from the sample */
|
||||
temp <<= 8;
|
||||
/* discard the 2 lowest bits */
|
||||
temp |= (uint32)(hrtsample.LowPart >> 2);
|
||||
*(data++) = (byte)(hrtsample.LowPart >> 2);
|
||||
randombits -= 8;
|
||||
|
||||
if (!(randombits & 0x1f))
|
||||
*(data++) = temp;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -756,7 +751,7 @@ int entropy_console(uint32* data, int size)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int entropy_wincrypt(uint32* data, int size)
|
||||
int entropy_wincrypt(byte* data, size_t size)
|
||||
{
|
||||
HCRYPTPROV hCrypt;
|
||||
DWORD provType = PROV_RSA_FULL;
|
||||
|
@ -779,7 +774,7 @@ int entropy_wincrypt(uint32* data, int size)
|
|||
#endif
|
||||
}
|
||||
|
||||
rc = CryptGenRandom(hCrypt, size << 2, (BYTE*) data);
|
||||
rc = CryptGenRandom(hCrypt, size, (BYTE*) data);
|
||||
|
||||
CryptReleaseContext(hCrypt, 0);
|
||||
|
||||
|
@ -976,11 +971,9 @@ static int opendevice(const char *device)
|
|||
* @return
|
||||
*/
|
||||
/*@-boundswrite@*/
|
||||
static int entropy_randombits(int fd, int timeout, uint32* data, int size)
|
||||
static int entropy_randombits(int fd, int timeout, byte* data, size_t size)
|
||||
/*@*/
|
||||
{
|
||||
register byte* bytedata = (byte*) data;
|
||||
register int bytesize = (((unsigned)size) << 2);
|
||||
register int rc;
|
||||
|
||||
#if ENABLE_AIO
|
||||
|
@ -1000,17 +993,17 @@ static int entropy_randombits(int fd, int timeout, uint32* data, int size)
|
|||
#endif
|
||||
|
||||
/*@-branchstate@*/
|
||||
while (bytesize)
|
||||
while (size)
|
||||
{
|
||||
#if ENABLE_AIO
|
||||
/*@-mustfree@*/ /* my_aiocb.aio_buf is OK */
|
||||
my_aiocb.aio_buf = bytedata;
|
||||
my_aiocb.aio_buf = data;
|
||||
/*@=mustfree@*/
|
||||
my_aiocb.aio_nbytes = bytesize;
|
||||
my_aiocb.aio_nbytes = size;
|
||||
|
||||
rc = aio_read(&my_aiocb);
|
||||
#else
|
||||
rc = read(fd, bytedata, bytesize);
|
||||
rc = read(fd, data, size);
|
||||
#endif
|
||||
|
||||
if (rc < 0)
|
||||
|
@ -1072,8 +1065,8 @@ static int entropy_randombits(int fd, int timeout, uint32* data, int size)
|
|||
return -1;
|
||||
#endif
|
||||
|
||||
bytedata += rc;
|
||||
bytesize -= rc;
|
||||
data += rc;
|
||||
size -= rc;
|
||||
}
|
||||
/*@=branchstate@*/
|
||||
return 0;
|
||||
|
@ -1089,12 +1082,10 @@ static int entropy_randombits(int fd, int timeout, uint32* data, int size)
|
|||
* @return
|
||||
*/
|
||||
/*@-boundswrite@*/
|
||||
static int entropy_ttybits(int fd, uint32* data, int size)
|
||||
static int entropy_ttybits(int fd, byte* data, size_t size)
|
||||
/*@globals fileSystem @*/
|
||||
/*@modifies fileSystem @*/
|
||||
{
|
||||
uint32 randombits = ((unsigned)size) << 5;
|
||||
uint32 temp = 0;
|
||||
byte dummy;
|
||||
|
||||
#if HAVE_TERMIOS_H
|
||||
|
@ -1102,14 +1093,14 @@ static int entropy_ttybits(int fd, uint32* data, int size)
|
|||
#elif HAVE_TERMIO_H
|
||||
struct termio tio_save, tio_set;
|
||||
#else
|
||||
# need alternative
|
||||
# error need alternative
|
||||
#endif
|
||||
#if HAVE_GETHRTIME
|
||||
hrtime_t hrtsample;
|
||||
#elif HAVE_GETTIMEOFDAY
|
||||
struct timeval tvsample;
|
||||
#else
|
||||
# error Need alternative high-precision timer
|
||||
# error need alternative high-precision timer
|
||||
#endif
|
||||
|
||||
printf("please press random keys on your keyboard\n");
|
||||
|
@ -1166,7 +1157,7 @@ static int entropy_ttybits(int fd, uint32* data, int size)
|
|||
# error Need alternative tty control library
|
||||
#endif
|
||||
|
||||
while (randombits)
|
||||
while (size)
|
||||
{
|
||||
if (read(fd, &dummy, 1) < 0)
|
||||
{
|
||||
|
@ -1178,23 +1169,19 @@ static int entropy_ttybits(int fd, uint32* data, int size)
|
|||
printf("."); (void) fflush(stdout);
|
||||
#if HAVE_GETHRTIME
|
||||
hrtsample = gethrtime();
|
||||
/* get 16 bits from the sample */
|
||||
temp <<= 16;
|
||||
/* discard the 10 lowest bits i.e. 1024 nanoseconds */
|
||||
temp |= (uint16)(hrtsample >> 10);
|
||||
randombits -= 16;
|
||||
/* discard the 10 lowest bits i.e. 1024 nanoseconds of a sample */
|
||||
temp |= (uint16_t)(hrtsample >> 10);
|
||||
*(data++) = (byte)(hrtsample >> 10);
|
||||
size--;
|
||||
#elif HAVE_GETTIMEOFDAY
|
||||
/* discard the 4 lowest bits i.e. 4 microseconds */
|
||||
(void) gettimeofday(&tvsample, 0);
|
||||
/* get 8 bits from the sample */
|
||||
temp <<= 8;
|
||||
temp |= (uint8)(((unsigned)tvsample.tv_usec) >> 2);
|
||||
randombits -= 8;
|
||||
*(data++) = (byte)(((unsigned)tvsample.tv_usec) >> 2);
|
||||
size--;
|
||||
#else
|
||||
# error Need alternative high-precision timer sample
|
||||
#endif
|
||||
if (!(randombits & 0x1f))
|
||||
*(data++) = temp;
|
||||
}
|
||||
|
||||
printf("\nthanks\n");
|
||||
|
@ -1320,7 +1307,7 @@ dev_audio_end:
|
|||
#endif
|
||||
|
||||
#if HAVE_DEV_DSP
|
||||
int entropy_dev_dsp(uint32 *data, int size)
|
||||
int entropy_dev_dsp(byte *data, size_t size)
|
||||
/*@globals dev_dsp_fd @*/
|
||||
/*@modifies dev_dsp_fd @*/
|
||||
{
|
||||
|
@ -1455,7 +1442,7 @@ dev_dsp_end:
|
|||
#endif
|
||||
|
||||
#if HAVE_DEV_RANDOM
|
||||
int entropy_dev_random(uint32* data, int size)
|
||||
int entropy_dev_random(byte* data, size_t size)
|
||||
/*@globals dev_random_fd @*/
|
||||
/*@modifies dev_random_fd @*/
|
||||
{
|
||||
|
@ -1503,7 +1490,7 @@ dev_random_end:
|
|||
#endif
|
||||
|
||||
#if HAVE_DEV_URANDOM
|
||||
int entropy_dev_urandom(uint32* data, int size)
|
||||
int entropy_dev_urandom(byte* data, size_t size)
|
||||
/*@globals dev_urandom_fd @*/
|
||||
/*@modifies dev_urandom_fd @*/
|
||||
{
|
||||
|
@ -1551,7 +1538,7 @@ dev_urandom_end:
|
|||
#endif
|
||||
|
||||
#if HAVE_DEV_TTY
|
||||
int entropy_dev_tty(uint32* data, int size)
|
||||
int entropy_dev_tty(byte* data, size_t size)
|
||||
/*@globals dev_tty_fd @*/
|
||||
/*@modifies dev_tty_fd @*/
|
||||
{
|
||||
|
|
|
@ -45,17 +45,17 @@ BEECRYPTAPI
|
|||
int entropy_provider_cleanup(void);
|
||||
|
||||
BEECRYPTAPI
|
||||
int entropy_wavein(uint32* data, int size);
|
||||
int entropy_wavein(byte* data, size_t size);
|
||||
BEECRYPTAPI
|
||||
int entropy_console(uint32* data, int size);
|
||||
int entropy_console(byte* data, size_t size);
|
||||
BEECRYPTAPI
|
||||
int entropy_wincrypt(uint32* data, int size);
|
||||
int entropy_wincrypt(byte* data, size_t size);
|
||||
#else
|
||||
|
||||
#if HAVE_DEV_AUDIO
|
||||
/** \ingroup ES_audio_m ES_m
|
||||
*/
|
||||
int entropy_dev_audio (uint32* data, int size)
|
||||
int entropy_dev_audio (byte* data, size_t size)
|
||||
/*@globals fileSystem, internalState @*/
|
||||
/*@modifies data, fileSystem, internalState @*/;
|
||||
#endif
|
||||
|
@ -63,7 +63,7 @@ int entropy_dev_audio (uint32* data, int size)
|
|||
#if HAVE_DEV_DSP
|
||||
/** \ingroup ES_dsp_m ES_m
|
||||
*/
|
||||
int entropy_dev_dsp (uint32* data, int size)
|
||||
int entropy_dev_dsp (byte* data, size_t size)
|
||||
/*@globals fileSystem, internalState @*/
|
||||
/*@modifies data, fileSystem, internalState @*/;
|
||||
#endif
|
||||
|
@ -71,7 +71,7 @@ int entropy_dev_dsp (uint32* data, int size)
|
|||
#if HAVE_DEV_RANDOM
|
||||
/** \ingroup ES_random_m ES_m
|
||||
*/
|
||||
int entropy_dev_random(uint32* data, int size)
|
||||
int entropy_dev_random(byte* data, size_t size)
|
||||
/*@globals fileSystem, internalState @*/
|
||||
/*@modifies data, fileSystem, internalState @*/;
|
||||
#endif
|
||||
|
@ -79,7 +79,7 @@ int entropy_dev_random(uint32* data, int size)
|
|||
#if HAVE_DEV_URANDOM
|
||||
/** \ingroup ES_urandom_m ES_m
|
||||
*/
|
||||
int entropy_dev_urandom(uint32* data, int size)
|
||||
int entropy_dev_urandom(byte* data, size_t size)
|
||||
/*@globals fileSystem, internalState @*/
|
||||
/*@modifies data, fileSystem, internalState @*/;
|
||||
#endif
|
||||
|
@ -87,7 +87,7 @@ int entropy_dev_urandom(uint32* data, int size)
|
|||
#if HAVE_DEV_TTY
|
||||
/** \ingroup ES_tty_m ES_m
|
||||
*/
|
||||
int entropy_dev_tty (uint32* data, int size)
|
||||
int entropy_dev_tty (byte* data, size_t size)
|
||||
/*@globals fileSystem, internalState @*/
|
||||
/*@modifies data, fileSystem, internalState @*/;
|
||||
#endif
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static uint32 fips186hinit[5] = { 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0x67452301 };
|
||||
static uint32_t fips186hinit[5] = { 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U, 0x67452301U };
|
||||
|
||||
/*@-sizeoftype@*/
|
||||
const randomGenerator fips186prng = { "FIPS 186", sizeof(fips186Param), (const randomGeneratorSetup) fips186Setup, (const randomGeneratorSeed) fips186Seed, (const randomGeneratorNext) fips186Next, (const randomGeneratorCleanup) fips186Cleanup };
|
||||
|
@ -47,7 +47,7 @@ const randomGenerator fips186prng = { "FIPS 186", sizeof(fips186Param), (const r
|
|||
static int fips186init(register sha1Param* p)
|
||||
/*@modifies p @*/
|
||||
{
|
||||
mp32copy(5, p->h, fips186hinit);
|
||||
memcpy(p->h, fips186hinit, sizeof(p->h));
|
||||
return 0;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
@ -75,14 +75,14 @@ int fips186Setup(fips186Param* fp)
|
|||
# endif
|
||||
#endif
|
||||
|
||||
fp->digestsize = 0;
|
||||
fp->digestremain = 0;
|
||||
|
||||
return entropyGatherNext(fp->state, FIPS186_STATE_SIZE);
|
||||
return entropyGatherNext((byte*) fp->state, MP_WORDS_TO_BYTES(FIPS186_STATE_SIZE));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fips186Seed(fips186Param* fp, const uint32* data, int size)
|
||||
int fips186Seed(fips186Param* fp, const byte* data, size_t size)
|
||||
{
|
||||
if (fp)
|
||||
{
|
||||
|
@ -103,7 +103,17 @@ int fips186Seed(fips186Param* fp, const uint32* data, int size)
|
|||
# endif
|
||||
#endif
|
||||
if (data)
|
||||
(void) mp32addx(FIPS186_STATE_SIZE, fp->state, size, data);
|
||||
{
|
||||
mpw seed[FIPS186_STATE_SIZE];
|
||||
|
||||
/* if there's too much data, cut off at what we can deal with */
|
||||
if (size > MP_WORDS_TO_BYTES(FIPS186_STATE_SIZE))
|
||||
size = MP_WORDS_TO_BYTES(FIPS186_STATE_SIZE);
|
||||
|
||||
/* convert to multi-precision integer, and add to the state */
|
||||
if (os2ip(seed, FIPS186_STATE_SIZE, data, size) == 0)
|
||||
mpadd(FIPS186_STATE_SIZE, fp->state, seed);
|
||||
}
|
||||
#ifdef _REENTRANT
|
||||
# if WIN32
|
||||
if (!ReleaseMutex(fp->lock))
|
||||
|
@ -126,10 +136,12 @@ int fips186Seed(fips186Param* fp, const uint32* data, int size)
|
|||
}
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int fips186Next(fips186Param* fp, uint32* data, int size)
|
||||
int fips186Next(fips186Param* fp, byte* data, size_t size)
|
||||
{
|
||||
if (fp)
|
||||
{
|
||||
mpw dig[FIPS186_STATE_SIZE];
|
||||
|
||||
#ifdef _REENTRANT
|
||||
# if WIN32
|
||||
if (WaitForSingleObject(fp->lock, INFINITE) != WAIT_OBJECT_0)
|
||||
|
@ -146,27 +158,32 @@ int fips186Next(fips186Param* fp, uint32* data, int size)
|
|||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
while (size > 0)
|
||||
{
|
||||
register uint32 copy;
|
||||
register size_t copy;
|
||||
|
||||
if (fp->digestsize == 0)
|
||||
if (fp->digestremain == 0)
|
||||
{
|
||||
(void) fips186init(&fp->param);
|
||||
/* copy the 512 bits of state data into the sha1Param */
|
||||
mp32copy(FIPS186_STATE_SIZE, fp->param.data, fp->state);
|
||||
memcpy(fp->param.data, fp->state, MP_WORDS_TO_BYTES(FIPS186_STATE_SIZE));
|
||||
/* process the data */
|
||||
sha1Process(&fp->param);
|
||||
/* set state to state + digest + 1 mod 2^512 */
|
||||
(void) mp32addx(FIPS186_STATE_SIZE, fp->state, 5, fp->param.h);
|
||||
(void) mp32addw(FIPS186_STATE_SIZE, fp->state, 1);
|
||||
encodeInts(fp->param.h, fp->digest, 5);
|
||||
if (os2ip(dig, FIPS186_STATE_SIZE, fp->digest, 20) == 0)
|
||||
{
|
||||
/* set state to state + digest + 1 mod 2^512 */
|
||||
mpadd (FIPS186_STATE_SIZE, fp->state, dig);
|
||||
mpaddw(FIPS186_STATE_SIZE, fp->state, 1);
|
||||
}
|
||||
/* else shouldn't occur */
|
||||
/* we now have 5 words of pseudo-random data */
|
||||
fp->digestsize = 5;
|
||||
fp->digestremain = 20;
|
||||
}
|
||||
|
||||
copy = (size > fp->digestsize) ? fp->digestsize : size;
|
||||
mp32copy(copy, data, fp->param.h + 5 - fp->digestsize);
|
||||
fp->digestsize -= copy;
|
||||
copy = (size > fp->digestremain) ? fp->digestremain : size;
|
||||
memcpy(data, fp->digest+20-fp->digestremain, copy);
|
||||
fp->digestremain -= copy;
|
||||
size -= copy;
|
||||
data += copy;
|
||||
}
|
||||
|
|
|
@ -47,7 +47,13 @@
|
|||
|
||||
#include "sha1.h"
|
||||
|
||||
#define FIPS186_STATE_SIZE 16
|
||||
#if (MP_WBYTES == 8)
|
||||
# define FIPS186_STATE_SIZE 8
|
||||
#elif (MP_WBYTES == 4)
|
||||
# define FIPS186_STATE_SIZE 16
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
|
||||
/**
|
||||
*/
|
||||
|
@ -67,8 +73,9 @@ typedef struct
|
|||
# endif
|
||||
#endif
|
||||
sha1Param param;
|
||||
uint32 state[FIPS186_STATE_SIZE];
|
||||
int digestsize;
|
||||
mpw state[FIPS186_STATE_SIZE];
|
||||
byte digest[20];
|
||||
int digestremain;
|
||||
} fips186Param;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -92,7 +99,7 @@ int fips186Setup (fips186Param* fp)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int fips186Seed (fips186Param* fp, const uint32* data, int size)
|
||||
int fips186Seed (fips186Param* fp, const byte* data, size_t size)
|
||||
/*@modifies fp */;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
|
@ -100,7 +107,7 @@ int fips186Seed (fips186Param* fp, const uint32* data, int size)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int fips186Next (fips186Param* fp, uint32* data, int size)
|
||||
int fips186Next (fips186Param* fp, byte* data, size_t size)
|
||||
/*@modifies fp, data */;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
|
|
|
@ -32,8 +32,8 @@
|
|||
.text
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32zero)
|
||||
LABEL(mp32zero)
|
||||
C_FUNCTION_BEGIN(mpzero)
|
||||
LABEL(mpzero)
|
||||
pushl %edi
|
||||
|
||||
movl 8(%esp),%ecx
|
||||
|
@ -44,11 +44,11 @@ LABEL(mp32zero)
|
|||
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32zero, LOCAL(mp32zero_size))
|
||||
C_FUNCTION_END(mpzero, LOCAL(mpzero_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32fill)
|
||||
LABEL(mp32fill)
|
||||
C_FUNCTION_BEGIN(mpfill)
|
||||
LABEL(mpfill)
|
||||
pushl %edi
|
||||
|
||||
movl 8(%esp),%ecx
|
||||
|
@ -59,32 +59,32 @@ LABEL(mp32fill)
|
|||
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32fill, LOCAL(mp32fill_size))
|
||||
C_FUNCTION_END(mpfill, LOCAL(mpfill_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32even)
|
||||
LABEL(mp32even)
|
||||
C_FUNCTION_BEGIN(mpeven)
|
||||
LABEL(mpeven)
|
||||
movl 4(%esp),%ecx
|
||||
movl 8(%esp),%eax
|
||||
movl -4(%eax,%ecx,4),%eax
|
||||
notl %eax
|
||||
andl $1,%eax
|
||||
ret
|
||||
C_FUNCTION_END(mp32even, LOCAL(mp32even_size))
|
||||
C_FUNCTION_END(mpeven, LOCAL(mpeven_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32odd)
|
||||
LABEL(mp32odd)
|
||||
C_FUNCTION_BEGIN(mpodd)
|
||||
LABEL(mpodd)
|
||||
movl 4(%esp),%ecx
|
||||
movl 8(%esp),%eax
|
||||
movl -4(%eax,%ecx,4),%eax
|
||||
andl $1,%eax
|
||||
ret
|
||||
C_FUNCTION_END(mp32odd, LOCAL(mp32odd_size))
|
||||
C_FUNCTION_END(mpodd, LOCAL(mpodd_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addw)
|
||||
LABEL(mp32addw)
|
||||
C_FUNCTION_BEGIN(mpaddw)
|
||||
LABEL(mpaddw)
|
||||
pushl %edi
|
||||
|
||||
movl 8(%esp),%ecx
|
||||
|
@ -95,26 +95,26 @@ LABEL(mp32addw)
|
|||
leal -4(%edi,%ecx,4),%edi
|
||||
addl %eax,(%edi)
|
||||
decl %ecx
|
||||
jz LOCAL(mp32addw_skip)
|
||||
jz LOCAL(mpaddw_skip)
|
||||
leal -4(%edi),%edi
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32addw_loop):
|
||||
LOCAL(mpaddw_loop):
|
||||
adcl %edx,(%edi)
|
||||
leal -4(%edi),%edi
|
||||
decl %ecx
|
||||
jnz LOCAL(mp32addw_loop)
|
||||
LOCAL(mp32addw_skip):
|
||||
jnz LOCAL(mpaddw_loop)
|
||||
LOCAL(mpaddw_skip):
|
||||
sbbl %eax,%eax
|
||||
negl %eax
|
||||
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32addw, LOCAL(mp32addw_size))
|
||||
C_FUNCTION_END(mpaddw, LOCAL(mpaddw_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32subw)
|
||||
LABEL(mp32subw)
|
||||
C_FUNCTION_BEGIN(mpsubw)
|
||||
LABEL(mpsubw)
|
||||
pushl %edi
|
||||
|
||||
movl 8(%esp),%ecx
|
||||
|
@ -125,25 +125,25 @@ LABEL(mp32subw)
|
|||
leal -4(%edi,%ecx,4),%edi
|
||||
subl %eax,(%edi)
|
||||
decl %ecx
|
||||
jz LOCAL(mp32subw_skip)
|
||||
jz LOCAL(mpsubw_skip)
|
||||
leal -4(%edi),%edi
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32subw_loop):
|
||||
LOCAL(mpsubw_loop):
|
||||
sbbl %edx,(%edi)
|
||||
leal -4(%edi),%edi
|
||||
decl %ecx
|
||||
jnz LOCAL(mp32subw_loop)
|
||||
LOCAL(mp32subw_skip):
|
||||
jnz LOCAL(mpsubw_loop)
|
||||
LOCAL(mpsubw_skip):
|
||||
sbbl %eax,%eax
|
||||
negl %eax
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32subw, LOCAL(mp32subw_size))
|
||||
C_FUNCTION_END(mpsubw, LOCAL(mpsubw_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32add)
|
||||
LABEL(mp32add)
|
||||
C_FUNCTION_BEGIN(mpadd)
|
||||
LABEL(mpadd)
|
||||
pushl %edi
|
||||
pushl %esi
|
||||
|
||||
|
@ -155,11 +155,11 @@ LABEL(mp32add)
|
|||
decl %ecx
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32add_loop):
|
||||
LOCAL(mpadd_loop):
|
||||
movl (%esi,%ecx,4),%eax
|
||||
adcl %eax,(%edi,%ecx,4)
|
||||
decl %ecx
|
||||
jns LOCAL(mp32add_loop)
|
||||
jns LOCAL(mpadd_loop)
|
||||
|
||||
sbbl %eax,%eax
|
||||
negl %eax
|
||||
|
@ -167,11 +167,11 @@ LOCAL(mp32add_loop):
|
|||
popl %esi
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32add, LOCAL(mp32add_size))
|
||||
C_FUNCTION_END(mpadd, LOCAL(mpadd_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32sub)
|
||||
LABEL(mp32sub)
|
||||
C_FUNCTION_BEGIN(mpsub)
|
||||
LABEL(mpsub)
|
||||
pushl %edi
|
||||
pushl %esi
|
||||
|
||||
|
@ -183,22 +183,22 @@ LABEL(mp32sub)
|
|||
decl %ecx
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32sub_loop):
|
||||
LOCAL(mpsub_loop):
|
||||
movl (%esi,%ecx,4),%eax
|
||||
sbbl %eax,(%edi,%ecx,4)
|
||||
decl %ecx
|
||||
jns LOCAL(mp32sub_loop)
|
||||
jns LOCAL(mpsub_loop)
|
||||
|
||||
sbbl %eax,%eax
|
||||
negl %eax
|
||||
popl %esi
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32sub, LOCAL(mp32sub_size))
|
||||
C_FUNCTION_END(mpsub, LOCAL(mpsub_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32divtwo)
|
||||
LABEL(mp32divtwo)
|
||||
C_FUNCTION_BEGIN(mpdivtwo)
|
||||
LABEL(mpdivtwo)
|
||||
pushl %edi
|
||||
|
||||
movl 8(%esp),%ecx
|
||||
|
@ -209,18 +209,18 @@ LABEL(mp32divtwo)
|
|||
clc
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32divtwo_loop):
|
||||
LOCAL(mpdivtwo_loop):
|
||||
rcrl $1,(%edi,%ecx,4)
|
||||
inc %ecx
|
||||
jnz LOCAL(mp32divtwo_loop)
|
||||
jnz LOCAL(mpdivtwo_loop)
|
||||
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32divtwo, LOCAL(mp32divtwo_size))
|
||||
C_FUNCTION_END(mpdivtwo, LOCAL(mpdivtwo_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32multwo)
|
||||
LABEL(mp32multwo)
|
||||
C_FUNCTION_BEGIN(mpmultwo)
|
||||
LABEL(mpmultwo)
|
||||
pushl %edi
|
||||
|
||||
movl 8(%esp),%ecx
|
||||
|
@ -230,21 +230,21 @@ LABEL(mp32multwo)
|
|||
decl %ecx
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32multwo_loop):
|
||||
LOCAL(mpmultwo_loop):
|
||||
rcll $1,(%edi,%ecx,4)
|
||||
decl %ecx
|
||||
jns LOCAL(mp32multwo_loop)
|
||||
jns LOCAL(mpmultwo_loop)
|
||||
|
||||
sbbl %eax,%eax
|
||||
negl %eax
|
||||
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32multwo, LOCAL(mp32multwo_size))
|
||||
C_FUNCTION_END(mpmultwo, LOCAL(mpmultwo_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32setmul)
|
||||
LABEL(mp32setmul)
|
||||
C_FUNCTION_BEGIN(mpsetmul)
|
||||
LABEL(mpsetmul)
|
||||
pushl %edi
|
||||
pushl %esi
|
||||
pushl %ebx
|
||||
|
@ -259,7 +259,7 @@ LABEL(mp32setmul)
|
|||
decl %ecx
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32setmul_loop):
|
||||
LOCAL(mpsetmul_loop):
|
||||
movl %edx,%ebx
|
||||
movl (%esi,%ecx,4),%eax
|
||||
mull %ebp
|
||||
|
@ -267,7 +267,7 @@ LOCAL(mp32setmul_loop):
|
|||
adcl $0,%edx
|
||||
movl %eax,(%edi,%ecx,4)
|
||||
decl %ecx
|
||||
jns LOCAL(mp32setmul_loop)
|
||||
jns LOCAL(mpsetmul_loop)
|
||||
|
||||
movl %edx,%eax
|
||||
|
||||
|
@ -276,11 +276,11 @@ LOCAL(mp32setmul_loop):
|
|||
popl %esi
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32setmul, LOCAL(mp32setmul_size))
|
||||
C_FUNCTION_END(mpsetmul, LOCAL(mpsetmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addmul)
|
||||
LABEL(mp32addmul)
|
||||
C_FUNCTION_BEGIN(mpaddmul)
|
||||
LABEL(mpaddmul)
|
||||
pushl %edi
|
||||
pushl %esi
|
||||
pushl %ebx
|
||||
|
@ -295,7 +295,7 @@ LABEL(mp32addmul)
|
|||
decl %ecx
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32addmul_loop):
|
||||
LOCAL(mpaddmul_loop):
|
||||
movl %edx,%ebx
|
||||
movl (%esi,%ecx,4),%eax
|
||||
mull %ebp
|
||||
|
@ -305,7 +305,7 @@ LOCAL(mp32addmul_loop):
|
|||
adcl $0,%edx
|
||||
movl %eax,(%edi,%ecx,4)
|
||||
decl %ecx
|
||||
jns LOCAL(mp32addmul_loop)
|
||||
jns LOCAL(mpaddmul_loop)
|
||||
|
||||
movl %edx,%eax
|
||||
|
||||
|
@ -314,11 +314,11 @@ LOCAL(mp32addmul_loop):
|
|||
popl %esi
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32addmul, LOCAL(mp32addmul_size))
|
||||
C_FUNCTION_END(mpaddmul, LOCAL(mpaddmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addsqrtrc)
|
||||
LABEL(mp32addsqrtrc)
|
||||
C_FUNCTION_BEGIN(mpaddsqrtrc)
|
||||
LABEL(mpaddsqrtrc)
|
||||
pushl %edi
|
||||
pushl %esi
|
||||
pushl %ebx
|
||||
|
@ -331,7 +331,7 @@ LABEL(mp32addsqrtrc)
|
|||
decl %ecx
|
||||
|
||||
.align 4
|
||||
LOCAL(mp32addsqrtrc_loop):
|
||||
LOCAL(mpaddsqrtrc_loop):
|
||||
movl (%esi,%ecx,4),%eax
|
||||
mull %eax
|
||||
addl %ebx,%eax
|
||||
|
@ -341,7 +341,7 @@ LOCAL(mp32addsqrtrc_loop):
|
|||
sbbl %ebx,%ebx
|
||||
negl %ebx
|
||||
decl %ecx
|
||||
jns LOCAL(mp32addsqrtrc_loop)
|
||||
jns LOCAL(mpaddsqrtrc_loop)
|
||||
|
||||
movl %ebx,%eax
|
||||
|
||||
|
@ -349,4 +349,4 @@ LOCAL(mp32addsqrtrc_loop):
|
|||
popl %esi
|
||||
popl %edi
|
||||
ret
|
||||
C_FUNCTION_END(mp32addsqrtrc, LOCAL(mp32addsqrtrc_size))
|
||||
C_FUNCTION_END(mpaddsqrtrc, LOCAL(mpaddsqrtrc_size))
|
||||
|
|
|
@ -49,10 +49,10 @@
|
|||
.explicit
|
||||
|
||||
.align 32
|
||||
.global mp32zero#
|
||||
.proc mp32zero#
|
||||
.global mpzero#
|
||||
.proc mpzero#
|
||||
|
||||
mp32zero:
|
||||
mpzero:
|
||||
alloc saved_pfs = ar.pfs,2,0,0,0
|
||||
mov saved_lc = ar.lc
|
||||
sub size = in0,r0,1;;
|
||||
|
@ -65,14 +65,14 @@ mp32zero:
|
|||
mov ar.lc = saved_lc
|
||||
mov ar.pfs = saved_pfs
|
||||
br.ret.sptk b0
|
||||
.endp mp32zero#
|
||||
.endp mpzero#
|
||||
|
||||
|
||||
.align 32
|
||||
.global mp32copy#
|
||||
.proc mp32copy#
|
||||
.global mpcopy#
|
||||
.proc mpcopy#
|
||||
|
||||
mp32copy:
|
||||
mpcopy:
|
||||
alloc saved_pfs = ar.pfs,3,5,0,8
|
||||
mov saved_lc = ar.lc
|
||||
sub size = in0,r0,1
|
||||
|
@ -88,15 +88,15 @@ mp32copy:
|
|||
mov ar.lc = saved_lc
|
||||
mov ar.pfs = saved_pfs
|
||||
br.ret.sptk b0
|
||||
.endp mp32copy#
|
||||
.endp mpcopy#
|
||||
|
||||
|
||||
.if 0
|
||||
.align 32
|
||||
.global mp32z
|
||||
.type mp32z,@function
|
||||
.global mpz
|
||||
.type mpz,@function
|
||||
|
||||
mp32z:
|
||||
mpz:
|
||||
alloc r14 = ar.pfs,2,6,0,8
|
||||
mov r15 = ar.lc
|
||||
sub r16= in0,r0,1
|
||||
|
@ -115,10 +115,10 @@ mp32z:
|
|||
|
||||
|
||||
.align 32
|
||||
.global mp32add#
|
||||
.proc mp32add#
|
||||
.global mpadd#
|
||||
.proc mpadd#
|
||||
|
||||
mp32add:
|
||||
mpadd:
|
||||
alloc r14 = ar.pfs,3,0,0,0
|
||||
mov r15 = ar.lc
|
||||
# adjust size by -1
|
||||
|
@ -142,14 +142,14 @@ mp32add:
|
|||
mov ar.lc = r15
|
||||
mov ar.pfs = r14
|
||||
br.ret.sptk b0
|
||||
.endp mp32add#
|
||||
.endp mpadd#
|
||||
|
||||
|
||||
.align 32
|
||||
.global mp32sub#
|
||||
.proc mp32sub#
|
||||
.global mpsub#
|
||||
.proc mpsub#
|
||||
|
||||
mp32sub:
|
||||
mpsub:
|
||||
alloc r14 = ar.pfs,3,0,0,0
|
||||
mov r15 = ar.lc
|
||||
# adjust size by -1
|
||||
|
@ -173,16 +173,16 @@ mp32sub:
|
|||
mov ar.lc = r15
|
||||
mov ar.pfs = r14
|
||||
br.ret.sptk b0
|
||||
.endp mp32sub#
|
||||
.endp mpsub#
|
||||
|
||||
|
||||
.if 0
|
||||
|
||||
.align 32
|
||||
.global mp32setmul#
|
||||
.proc mp32setmul#
|
||||
.global mpsetmul#
|
||||
.proc mpsetmul#
|
||||
|
||||
mp32setmul:
|
||||
mpsetmul:
|
||||
alloc r14 = ar.pfs,4,0,0,0
|
||||
mov r15 = ar.lc
|
||||
# load mul
|
||||
|
@ -209,14 +209,14 @@ mp32setmul:
|
|||
mov ar.lc = r15
|
||||
mov ar.pfs = r14
|
||||
br.ret.sptk b0
|
||||
.endp mp32setmul#
|
||||
.endp mpsetmul#
|
||||
|
||||
|
||||
.align 32
|
||||
.global mp32addmul#
|
||||
.proc mp32addmul#
|
||||
.global mpaddmul#
|
||||
.proc mpaddmul#
|
||||
|
||||
mp32addmul:
|
||||
mpaddmul:
|
||||
alloc saved_pfs = ar.pfs,4,0,0,0
|
||||
mov saved_lc = ar.lc
|
||||
# load mul
|
||||
|
@ -245,16 +245,16 @@ mp32addmul:
|
|||
mov ar.lc = r15
|
||||
mov ar.pfs = r14
|
||||
br.ret.sptk b0
|
||||
.endp mp32addmul#
|
||||
.endp mpaddmul#
|
||||
|
||||
.endif
|
||||
|
||||
|
||||
.if 0
|
||||
.align 16
|
||||
.global mp32addsqrtrc#
|
||||
.proc mp32addsqrtrc#
|
||||
.global mpaddsqrtrc#
|
||||
.proc mpaddsqrtrc#
|
||||
|
||||
mp32addsqrtrc:
|
||||
.endp mp32addsqrtrc#
|
||||
mpaddsqrtrc:
|
||||
.endp mpaddsqrtrc#
|
||||
.endif
|
||||
|
|
|
@ -32,8 +32,8 @@
|
|||
.text
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addw)
|
||||
LABEL(mp32addw)
|
||||
C_FUNCTION_BEGIN(mpaddw)
|
||||
LABEL(mpaddw)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
|
@ -41,21 +41,21 @@ LABEL(mp32addw)
|
|||
lwzu r6,-4(r4)
|
||||
addc r6,r6,r5
|
||||
stw r6,0(r4)
|
||||
bdz LOCAL(mp32addw_skip)
|
||||
LOCAL(mp32addw_loop):
|
||||
bdz LOCAL(mpaddw_skip)
|
||||
LOCAL(mpaddw_loop):
|
||||
lwzu r6,-4(r4)
|
||||
adde r6,r0,r6
|
||||
stw r6,0(r4)
|
||||
bdnz LOCAL(mp32addw_loop)
|
||||
LOCAL(mp32addw_skip):
|
||||
bdnz LOCAL(mpaddw_loop)
|
||||
LOCAL(mpaddw_skip):
|
||||
/* return the carry */
|
||||
addze r3,r0
|
||||
blr
|
||||
C_FUNCTION_END(mp32addw, LOCAL(mp32addw_size))
|
||||
C_FUNCTION_END(mpaddw, LOCAL(mpaddw_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32subw)
|
||||
LABEL(mp32subw)
|
||||
C_FUNCTION_BEGIN(mpsubw)
|
||||
LABEL(mpsubw)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
|
@ -63,22 +63,22 @@ LABEL(mp32subw)
|
|||
lwz r6,-4(r4)
|
||||
subfc r6,r5,r6
|
||||
stwu r6,-4(r4)
|
||||
bdz LOCAL(mp32subw_skip)
|
||||
LOCAL(mp32subw_loop):
|
||||
bdz LOCAL(mpsubw_skip)
|
||||
LOCAL(mpsubw_loop):
|
||||
lwz r6,-4(r4)
|
||||
subfe r6,r0,r6
|
||||
stwu r6, -4(r4)
|
||||
bdnz LOCAL(mp32subw_loop)
|
||||
LOCAL(mp32subw_skip):
|
||||
bdnz LOCAL(mpsubw_loop)
|
||||
LOCAL(mpsubw_skip):
|
||||
/* return the carry */
|
||||
subfe r3,r0,r0
|
||||
neg r3,r3
|
||||
blr
|
||||
C_FUNCTION_END(mp32subw, LOCAL(mp32subw_size))
|
||||
C_FUNCTION_END(mpsubw, LOCAL(mpsubw_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32add)
|
||||
LABEL(mp32add)
|
||||
C_FUNCTION_BEGIN(mpadd)
|
||||
LABEL(mpadd)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
|
@ -88,22 +88,22 @@ LABEL(mp32add)
|
|||
lwzu r7,-4(r5)
|
||||
addc r6,r7,r6
|
||||
stwu r6,-4(r4)
|
||||
bdz LOCAL(mp32add_skip)
|
||||
LOCAL(mp32add_loop):
|
||||
bdz LOCAL(mpadd_skip)
|
||||
LOCAL(mpadd_loop):
|
||||
lwz r6,-4(r4)
|
||||
lwzu r7,-4(r5)
|
||||
adde r6,r7,r6
|
||||
stwu r6,-4(r4)
|
||||
bdnz LOCAL(mp32add_loop)
|
||||
LOCAL(mp32add_skip):
|
||||
bdnz LOCAL(mpadd_loop)
|
||||
LOCAL(mpadd_skip):
|
||||
/* return the carry */
|
||||
addze r3,r0
|
||||
blr
|
||||
C_FUNCTION_END(mp32add, LOCAL(mp32add_size))
|
||||
C_FUNCTION_END(mpadd, LOCAL(mpadd_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32sub)
|
||||
LABEL(mp32sub)
|
||||
C_FUNCTION_BEGIN(mpsub)
|
||||
LABEL(mpsub)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
|
@ -113,23 +113,23 @@ LABEL(mp32sub)
|
|||
lwzu r7,-4(r5)
|
||||
subfc r6,r7,r6
|
||||
stwu r6,-4(r4)
|
||||
bdz LOCAL(mp32sub_skip)
|
||||
LOCAL(mp32sub_loop):
|
||||
bdz LOCAL(mpsub_skip)
|
||||
LOCAL(mpsub_loop):
|
||||
lwz r6,-4(r4)
|
||||
lwzu r7,-4(r5)
|
||||
subfe r6,r7,r6
|
||||
stwu r6,-4(r4)
|
||||
bdnz LOCAL(mp32sub_loop)
|
||||
LOCAL(mp32sub_skip):
|
||||
bdnz LOCAL(mpsub_loop)
|
||||
LOCAL(mpsub_skip):
|
||||
/* return the carry */
|
||||
subfe r3,r0,r0
|
||||
neg r3,r3
|
||||
blr
|
||||
C_FUNCTION_END(mp32sub, LOCAL(mp32sub_size))
|
||||
C_FUNCTION_END(mpsub, LOCAL(mpsub_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32multwo)
|
||||
LABEL(mp32multwo)
|
||||
C_FUNCTION_BEGIN(mpmultwo)
|
||||
LABEL(mpmultwo)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
|
@ -137,46 +137,46 @@ LABEL(mp32multwo)
|
|||
lwz r6,-4(r4)
|
||||
addc r6,r6,r6
|
||||
stwu r6,-4(r4)
|
||||
bdz LOCAL(mp32multwo_skip)
|
||||
LOCAL(mp32multwo_loop):
|
||||
bdz LOCAL(mpmultwo_skip)
|
||||
LOCAL(mpmultwo_loop):
|
||||
lwz r6,-4(r4)
|
||||
adde r6,r6,r6
|
||||
stwu r6,-4(r4)
|
||||
bdnz LOCAL(mp32multwo_loop)
|
||||
LOCAL(mp32multwo_skip):
|
||||
bdnz LOCAL(mpmultwo_loop)
|
||||
LOCAL(mpmultwo_skip):
|
||||
/* return the carry */
|
||||
addze r3,r0
|
||||
blr
|
||||
C_FUNCTION_END(mp32multwo, LOCAL(mp32multwo_size))
|
||||
C_FUNCTION_END(mpmultwo, LOCAL(mpmultwo_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32setmul)
|
||||
LABEL(mp32setmul)
|
||||
C_FUNCTION_BEGIN(mpsetmul)
|
||||
LABEL(mpsetmul)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
add r5,r5,r0
|
||||
li r3,0
|
||||
LOCAL(mp32setmul_loop):
|
||||
LOCAL(mpsetmul_loop):
|
||||
lwzu r7,-4(r5)
|
||||
mullw r8,r7,r6
|
||||
addc r8,r8,r3
|
||||
mulhwu r9,r7,r6
|
||||
addze r3,r9
|
||||
stwu r8,-4(r4)
|
||||
bdnz LOCAL(mp32setmul_loop)
|
||||
bdnz LOCAL(mpsetmul_loop)
|
||||
blr
|
||||
C_FUNCTION_END(mp32setmul, LOCAL(mp32setmul_size))
|
||||
C_FUNCTION_END(mpsetmul, LOCAL(mpsetmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addmul)
|
||||
LABEL(mp32addmul)
|
||||
C_FUNCTION_BEGIN(mpaddmul)
|
||||
LABEL(mpaddmul)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
add r5,r5,r0
|
||||
li r3,0
|
||||
LOCAL(mp32addmul_loop):
|
||||
LOCAL(mpaddmul_loop):
|
||||
lwzu r8,-4(r5)
|
||||
lwzu r7,-4(r4)
|
||||
mullw r9,r8,r6
|
||||
|
@ -186,20 +186,20 @@ LOCAL(mp32addmul_loop):
|
|||
addc r9,r9,r7
|
||||
addze r3,r3
|
||||
stw r9,0(r4)
|
||||
bdnz LOCAL(mp32addmul_loop)
|
||||
bdnz LOCAL(mpaddmul_loop)
|
||||
blr
|
||||
C_FUNCTION_END(mp32addmul, LOCAL(mp32addmul_size))
|
||||
C_FUNCTION_END(mpaddmul, LOCAL(mpaddmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addsqrtrc)
|
||||
LABEL(mp32addsqrtrc)
|
||||
C_FUNCTION_BEGIN(mpaddsqrtrc)
|
||||
LABEL(mpaddsqrtrc)
|
||||
mtctr r3
|
||||
slwi r0,r3,2
|
||||
add r4,r4,r0
|
||||
add r5,r5,r0
|
||||
add r4,r4,r0
|
||||
li r3,0
|
||||
LOCAL(mp32addsqrtrc_loop):
|
||||
LOCAL(mpaddsqrtrc_loop):
|
||||
lwzu r0,-4(r5)
|
||||
lwz r6,-8(r4)
|
||||
lwz r7,-4(r4)
|
||||
|
@ -213,6 +213,6 @@ LOCAL(mp32addsqrtrc_loop):
|
|||
addze r3,r3
|
||||
stw r7,-4(r4)
|
||||
stwu r6,-8(r4)
|
||||
bdnz LOCAL(mp32addsqrtrc_loop)
|
||||
bdnz LOCAL(mpaddsqrtrc_loop)
|
||||
blr
|
||||
C_FUNCTION_END(mp32addsqrtrc, LOCAL(mp32addsqrtrc_size))
|
||||
C_FUNCTION_END(mpaddsqrtrc, LOCAL(mpaddsqrtrc_size))
|
||||
|
|
|
@ -31,30 +31,30 @@
|
|||
|
||||
.text
|
||||
|
||||
C_FUNCTION_BEGIN(mp32setmul)
|
||||
LABEL(mp32setmul)
|
||||
C_FUNCTION_BEGIN(mpsetmul)
|
||||
LABEL(mpsetmul)
|
||||
|
||||
.register %g2,#scratch
|
||||
|
||||
sll %o0,2,%g1
|
||||
dec 4,%o2
|
||||
clr %o0
|
||||
LOCAL(mp32setmul_loop):
|
||||
LOCAL(mpsetmul_loop):
|
||||
ld [%o2+%g1],%g2
|
||||
umul %o3,%g2,%g2
|
||||
rd %y,%g3
|
||||
addcc %o0,%g2,%g2
|
||||
addx %g0,%g3,%o0
|
||||
deccc 4,%g1
|
||||
bnz LOCAL(mp32setmul_loop)
|
||||
bnz LOCAL(mpsetmul_loop)
|
||||
st %g2,[%o1+%g1]
|
||||
retl
|
||||
nop
|
||||
C_FUNCTION_END(mp32setmul, LOCAL(mp32setmul_size))
|
||||
C_FUNCTION_END(mpsetmul, LOCAL(mpsetmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addmul)
|
||||
LABEL(mp32addmul)
|
||||
C_FUNCTION_BEGIN(mpaddmul)
|
||||
LABEL(mpaddmul)
|
||||
|
||||
.register %g2,#scratch
|
||||
|
||||
|
@ -63,7 +63,7 @@ LABEL(mp32addmul)
|
|||
dec 4,%o1
|
||||
dec 4,%o2
|
||||
clr %o0
|
||||
LOCAL(mp32addmul_loop):
|
||||
LOCAL(mpaddmul_loop):
|
||||
ld [%o2+%g1],%g2
|
||||
ld [%o1+%g1],%g3
|
||||
umul %o3,%g2,%g2
|
||||
|
@ -73,15 +73,15 @@ LOCAL(mp32addmul_loop):
|
|||
addcc %g2,%g3,%g2
|
||||
addx %g0,%g4,%o0
|
||||
deccc 4,%g1
|
||||
bnz LOCAL(mp32addmul_loop)
|
||||
bnz LOCAL(mpaddmul_loop)
|
||||
st %g2,[%o4+%g1]
|
||||
retl
|
||||
nop
|
||||
C_FUNCTION_END(mp32addmul, LOCAL(mp32addmul_size))
|
||||
C_FUNCTION_END(mpaddmul, LOCAL(mpaddmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addsqrtrc)
|
||||
LABEL(mp32addsqrtrc)
|
||||
C_FUNCTION_BEGIN(mpaddsqrtrc)
|
||||
LABEL(mpaddsqrtrc)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -92,7 +92,7 @@ LABEL(mp32addsqrtrc)
|
|||
add %o1,%g1,%o1
|
||||
dec 8,%o1
|
||||
clr %o0
|
||||
LOCAL(mp32addsqrtrc_loop):
|
||||
LOCAL(mpaddsqrtrc_loop):
|
||||
ld [%o2+%g1],%g2
|
||||
ldd [%o1],%o4
|
||||
umul %g2,%g2,%g3
|
||||
|
@ -107,8 +107,8 @@ LOCAL(mp32addsqrtrc_loop):
|
|||
addx %o3,%g0,%o0
|
||||
std %o4,[%o1]
|
||||
deccc 4,%g1
|
||||
bnz LOCAL(mp32addsqrtrc_loop)
|
||||
bnz LOCAL(mpaddsqrtrc_loop)
|
||||
sub %o1,8,%o1
|
||||
retl
|
||||
nop
|
||||
C_FUNCTION_END(mp32addsqrtrc, LOCAL(mp32addsqrtrc_size))
|
||||
C_FUNCTION_END(mpaddsqrtrc, LOCAL(mpaddsqrtrc_size))
|
||||
|
|
|
@ -31,8 +31,8 @@
|
|||
|
||||
.text
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addw)
|
||||
LABEL(mp32addw)
|
||||
C_FUNCTION_BEGIN(mpaddw)
|
||||
LABEL(mpaddw)
|
||||
|
||||
.register %g2,#scratch
|
||||
|
||||
|
@ -42,22 +42,22 @@ LABEL(mp32addw)
|
|||
lduw [%o1+%g1],%g2
|
||||
addcc %g2,%o2,%g2
|
||||
stw %g2,[%o1+%g1]
|
||||
brz,pn %g1,LOCAL(mp32addw_skip)
|
||||
brz,pn %g1,LOCAL(mpaddw_skip)
|
||||
dec 4,%g1
|
||||
LOCAL(mp32addw_loop):
|
||||
LOCAL(mpaddw_loop):
|
||||
lduw [%o1+%g1],%g2
|
||||
addccc %g2,%g0,%g2
|
||||
stw %g2,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32addw_loop)
|
||||
brnz,pt %g1,LOCAL(mpaddw_loop)
|
||||
dec 4,%g1
|
||||
LOCAL(mp32addw_skip):
|
||||
LOCAL(mpaddw_skip):
|
||||
retl
|
||||
movcs %icc,1,%o0
|
||||
C_FUNCTION_END(mp32addw, LOCAL(mp32addw_size))
|
||||
C_FUNCTION_END(mpaddw, LOCAL(mpaddw_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32subw)
|
||||
LABEL(mp32subw)
|
||||
C_FUNCTION_BEGIN(mpsubw)
|
||||
LABEL(mpsubw)
|
||||
|
||||
.register %g2,#scratch
|
||||
|
||||
|
@ -67,22 +67,22 @@ LABEL(mp32subw)
|
|||
lduw [%o1+%g1],%g2
|
||||
subcc %g2,%o2,%g2
|
||||
stw %g2,[%o1+%g1]
|
||||
brz,pn %g1,LOCAL(mp32subw_skip)
|
||||
brz,pn %g1,LOCAL(mpsubw_skip)
|
||||
dec 4,%g1
|
||||
LOCAL(mp32subw_loop):
|
||||
LOCAL(mpsubw_loop):
|
||||
lduw [%o1+%g1],%g2
|
||||
subccc %g2,%g0,%g2
|
||||
stw %g2,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32subw_loop)
|
||||
brnz,pt %g1,LOCAL(mpsubw_loop)
|
||||
dec 4,%g1
|
||||
LOCAL(mp32subw_skip):
|
||||
LOCAL(mpsubw_skip):
|
||||
retl
|
||||
movcs %icc,1,%o0
|
||||
C_FUNCTION_END(mp32subw, LOCAL(mp32subw_size))
|
||||
C_FUNCTION_END(mpsubw, LOCAL(mpsubw_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32add)
|
||||
LABEL(mp32add)
|
||||
C_FUNCTION_BEGIN(mpadd)
|
||||
LABEL(mpadd)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -90,20 +90,20 @@ LABEL(mp32add)
|
|||
sll %o0,2,%g1
|
||||
dec 4,%g1
|
||||
addcc %g0,%g0,%o0
|
||||
LOCAL(mp32add_loop):
|
||||
LOCAL(mpadd_loop):
|
||||
lduw [%o1+%g1],%g2
|
||||
lduw [%o2+%g1],%g3
|
||||
addccc %g2,%g3,%g4
|
||||
stw %g4,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32add_loop)
|
||||
brnz,pt %g1,LOCAL(mpadd_loop)
|
||||
dec 4,%g1
|
||||
retl
|
||||
movcs %icc,1,%o0
|
||||
C_FUNCTION_END(mp32add, LOCAL(mp32add_size))
|
||||
C_FUNCTION_END(mpadd, LOCAL(mpadd_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32sub)
|
||||
LABEL(mp32sub)
|
||||
C_FUNCTION_BEGIN(mpsub)
|
||||
LABEL(mpsub)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -111,20 +111,20 @@ LABEL(mp32sub)
|
|||
sll %o0,2,%g1
|
||||
dec 4,%g1
|
||||
addcc %g0,%g0,%o0
|
||||
LOCAL(mp32sub_loop):
|
||||
LOCAL(mpsub_loop):
|
||||
lduw [%o1+%g1],%g2
|
||||
lduw [%o2+%g1],%g3
|
||||
subccc %g2,%g3,%g4
|
||||
stw %g4,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32sub_loop)
|
||||
brnz,pt %g1,LOCAL(mpsub_loop)
|
||||
dec 4,%g1
|
||||
retl
|
||||
movcs %icc,1,%o0
|
||||
C_FUNCTION_END(mp32sub, LOCAL(mp32sub_size))
|
||||
C_FUNCTION_END(mpsub, LOCAL(mpsub_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32multwo)
|
||||
LABEL(mp32multwo)
|
||||
C_FUNCTION_BEGIN(mpmultwo)
|
||||
LABEL(mpmultwo)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -132,19 +132,19 @@ LABEL(mp32multwo)
|
|||
sll %o0,2,%g1
|
||||
dec 4,%g1
|
||||
addcc %g0,%g0,%o0
|
||||
LOCAL(mp32multwo_loop):
|
||||
LOCAL(mpmultwo_loop):
|
||||
lduw [%o1+%g1],%g2
|
||||
addccc %g2,%g2,%g3
|
||||
stw %g3,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32multwo_loop)
|
||||
brnz,pt %g1,LOCAL(mpmultwo_loop)
|
||||
dec 4,%g1
|
||||
retl
|
||||
movcs %icc,1,%o0
|
||||
C_FUNCTION_END(mp32multwo, LOCAL(mp32multwo_size))
|
||||
C_FUNCTION_END(mpmultwo, LOCAL(mpmultwo_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32setmul)
|
||||
LABEL(mp32setmul)
|
||||
C_FUNCTION_BEGIN(mpsetmul)
|
||||
LABEL(mpsetmul)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -152,21 +152,21 @@ LABEL(mp32setmul)
|
|||
sll %o0,2,%g1
|
||||
dec 4,%g1
|
||||
clr %o0
|
||||
LOCAL(mp32setmul_loop):
|
||||
LOCAL(mpsetmul_loop):
|
||||
lduw [%o2+%g1],%g2
|
||||
srlx %o0,32,%o0
|
||||
mulx %o3,%g2,%g3
|
||||
add %o0,%g3,%o0
|
||||
stw %o0,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32setmul_loop)
|
||||
brnz,pt %g1,LOCAL(mpsetmul_loop)
|
||||
dec 4,%g1
|
||||
retl
|
||||
srlx %o0,32,%o0
|
||||
C_FUNCTION_END(mp32setmul, LOCAL(mp32setmul_size))
|
||||
C_FUNCTION_END(mpsetmul, LOCAL(mpsetmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addmul)
|
||||
LABEL(mp32addmul)
|
||||
C_FUNCTION_BEGIN(mpaddmul)
|
||||
LABEL(mpaddmul)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -174,7 +174,7 @@ LABEL(mp32addmul)
|
|||
sll %o0,2,%g1
|
||||
dec 4,%g1
|
||||
clr %o0
|
||||
LOCAL(mp32addmul_loop):
|
||||
LOCAL(mpaddmul_loop):
|
||||
lduw [%o2+%g1],%g2
|
||||
lduw [%o1+%g1],%g4
|
||||
srlx %o0,32,%o0
|
||||
|
@ -182,15 +182,15 @@ LOCAL(mp32addmul_loop):
|
|||
add %o0,%g3,%o0
|
||||
add %o0,%g4,%o0
|
||||
stw %o0,[%o1+%g1]
|
||||
brnz,pt %g1,LOCAL(mp32addmul_loop)
|
||||
brnz,pt %g1,LOCAL(mpaddmul_loop)
|
||||
dec 4,%g1
|
||||
retl
|
||||
srlx %o0,32,%o0
|
||||
C_FUNCTION_END(mp32addmul, LOCAL(mp32addmul_size))
|
||||
C_FUNCTION_END(mpaddmul, LOCAL(mpaddmul_size))
|
||||
|
||||
|
||||
C_FUNCTION_BEGIN(mp32addsqrtrc)
|
||||
LABEL(mp32addsqrtrc)
|
||||
C_FUNCTION_BEGIN(mpaddsqrtrc)
|
||||
LABEL(mpaddsqrtrc)
|
||||
|
||||
.register %g2,#scratch
|
||||
.register %g3,#scratch
|
||||
|
@ -200,7 +200,7 @@ LABEL(mp32addsqrtrc)
|
|||
add %o1,%g1,%o1
|
||||
add %o1,%g1,%o1
|
||||
clr %o0
|
||||
LOCAL(mp32addsqrtrc_loop):
|
||||
LOCAL(mpaddsqrtrc_loop):
|
||||
/* load from o1 into g4 as xuint; simulate xuint carry by doing an xuint comparison; carry if result smaller than initial value */
|
||||
lduw [%o2+%g1],%g2
|
||||
ldx [%o1],%g4
|
||||
|
@ -212,8 +212,8 @@ LOCAL(mp32addsqrtrc_loop):
|
|||
movgu %xcc,1,%o0
|
||||
stx %g3,[%o1]
|
||||
sub %o1,8,%o1
|
||||
brnz,pt %g1,LOCAL(mp32addsqrtrc_loop)
|
||||
brnz,pt %g1,LOCAL(mpaddsqrtrc_loop)
|
||||
dec 4,%g1
|
||||
retl
|
||||
nop
|
||||
C_FUNCTION_END(mp32addsqrtrc, LOCAL(mp32addsqrtrc_size))
|
||||
C_FUNCTION_END(mpaddsqrtrc, LOCAL(mpaddsqrtrc_size))
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HMAC_m
|
||||
* \file hmac.c
|
||||
*
|
||||
* HMAC message authentication code, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999, 2000, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,16 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file hmac.c
|
||||
* HMAC algorithm.
|
||||
*
|
||||
* \see RFC2104 HMAC: Keyed-Hashing for Message Authentication.
|
||||
* H. Krawczyk, M. Bellare, R. Canetti.
|
||||
*
|
||||
* \author Bob Deblier <bob.deblier@pandore.be>
|
||||
* \ingroup HMAC_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "hmac.h"
|
||||
#include "mp.h"
|
||||
|
@ -35,105 +37,77 @@
|
|||
#define HMAC_OPAD 0x5c
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int hmacSetup(hmacParam* hp, const hashFunction* hash, hashFunctionParam* param, const uint32* key, int keybits)
|
||||
int hmacSetup(byte* kxi, byte* kxo, const hashFunction* hash, hashFunctionParam* param, const byte* key, size_t keybits)
|
||||
{
|
||||
register int i, rc;
|
||||
int keywords = (((uint32)keybits + 31) >> 5); /* rounded up */
|
||||
int keybytes = (((uint32)keybits ) >> 3);
|
||||
|
||||
size_t keybytes = (((uint32)keybits ) >> 3);
|
||||
|
||||
/* if the key is too large, hash it first */
|
||||
if (keybytes > 64)
|
||||
if (keybytes > hash->blocksize)
|
||||
{
|
||||
uint32 keydigest[16];
|
||||
byte* tmp;
|
||||
|
||||
/* if the hash digest is too large, this doesn't help */
|
||||
if (hash->digestsize > 64)
|
||||
/* if the hash digest is too large, this doesn't help; this is really a sanity check */
|
||||
if (hash->digestsize > hash->blocksize)
|
||||
return -1;
|
||||
|
||||
if (hash->reset(param))
|
||||
return -1;
|
||||
|
||||
tmp = (byte*) malloc(keybytes);
|
||||
|
||||
if (tmp == (byte*) 0)
|
||||
if (hash->update(param, key, keybytes))
|
||||
return -1;
|
||||
|
||||
/* before we can hash the key, we need to encode it! */
|
||||
(void) encodeIntsPartial(key, tmp, keybytes);
|
||||
|
||||
rc = hash->update(param, tmp, keybytes);
|
||||
free(tmp);
|
||||
|
||||
if (rc)
|
||||
if (hash->digest(param, kxi))
|
||||
return -1;
|
||||
|
||||
memset(keydigest, 0, sizeof(keydigest));
|
||||
if (hash->digest(param, keydigest))
|
||||
return -1;
|
||||
|
||||
keywords = hash->digestsize >> 2;
|
||||
keybytes = hash->digestsize;
|
||||
|
||||
(void) encodeInts(keydigest, hp->kxi, keybytes);
|
||||
(void) encodeInts(keydigest, hp->kxo, keybytes);
|
||||
memcpy(kxo, kxi, keybytes = hash->digestsize);
|
||||
}
|
||||
else if (keybytes > 0)
|
||||
{
|
||||
(void) encodeIntsPartial(key, hp->kxi, keybytes);
|
||||
(void) encodeIntsPartial(key, hp->kxo, keybytes);
|
||||
memcpy(kxi, key, keybytes);
|
||||
memcpy(kxo, key, keybytes);
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
for (i = 0; i < keybytes; i++)
|
||||
{
|
||||
hp->kxi[i] ^= HMAC_IPAD;
|
||||
hp->kxo[i] ^= HMAC_OPAD;
|
||||
kxi[i] ^= HMAC_IPAD;
|
||||
kxo[i] ^= HMAC_OPAD;
|
||||
}
|
||||
|
||||
for (i = keybytes; i < 64; i++)
|
||||
for (i = keybytes; i < hash->blocksize; i++)
|
||||
{
|
||||
hp->kxi[i] = HMAC_IPAD;
|
||||
hp->kxo[i] = HMAC_OPAD;
|
||||
kxi[i] = HMAC_IPAD;
|
||||
kxo[i] = HMAC_OPAD;
|
||||
}
|
||||
|
||||
return hmacReset(hp, hash, param);
|
||||
return hmacReset(kxi, hash, param);
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
int hmacReset(hmacParam* hp, const hashFunction* hash, hashFunctionParam* param)
|
||||
int hmacReset(const byte* kxi, const hashFunction* hash, hashFunctionParam* param)
|
||||
{
|
||||
if (hash->reset(param))
|
||||
return -1;
|
||||
|
||||
if (hash->update(param, hp->kxi, 64))
|
||||
if (hash->update(param, kxi, hash->blocksize))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hmacUpdate(/*@unused@*/ hmacParam* hp, const hashFunction* hash, hashFunctionParam* param, const byte* data, int size)
|
||||
int hmacUpdate(const hashFunction* hash, hashFunctionParam* param, const byte* data, size_t size)
|
||||
{
|
||||
return hash->update(param, data, size);
|
||||
}
|
||||
|
||||
int hmacDigest(hmacParam* hp, const hashFunction* hash, hashFunctionParam* param, uint32* data)
|
||||
int hmacDigest(const byte* kxo, const hashFunction* hash, hashFunctionParam* param, byte* data)
|
||||
{
|
||||
if (hash->digest(param, data))
|
||||
return -1;
|
||||
|
||||
if (hash->update(param, hp->kxo, 64))
|
||||
if (hash->update(param, kxo, hash->blocksize))
|
||||
return -1;
|
||||
|
||||
/* digestsize is in bytes; divide by 4 to get the number of words */
|
||||
/*@-compdef@*/ /* FIX: *data undef ??? Code looks bogus ... */
|
||||
(void) encodeInts((const javaint*) data, (byte*) data, hash->digestsize >> 2);
|
||||
/*@=compdef@*/
|
||||
|
||||
if (hash->update(param, (const byte*) data, hash->digestsize))
|
||||
if (hash->update(param, data, hash->digestsize))
|
||||
return -1;
|
||||
|
||||
if (hash->digest(param, data))
|
||||
return -1;
|
||||
|
||||
|
|
|
@ -30,14 +30,6 @@
|
|||
|
||||
#include "beecrypt.h"
|
||||
|
||||
/**
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
byte kxi[64];
|
||||
byte kxo[64];
|
||||
} hmacParam;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
@ -45,26 +37,26 @@ extern "C" {
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hmacSetup (hmacParam* hp, const hashFunction* hash, hashFunctionParam* param, const uint32* key, int keybits)
|
||||
int hmacSetup (byte* kxi, byte* kxo, const hashFunction* hash, hashFunctionParam* param, const byte* key, size_t keybits)
|
||||
/*@modifies kxi, kxo */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hmacReset (const byte* kxi, const hashFunction* hash, hashFunctionParam* param)
|
||||
/*@modifies hp */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hmacReset (hmacParam* hp, const hashFunction* hash, hashFunctionParam* param)
|
||||
int hmacUpdate(const hashFunction* hash, hashFunctionParam* param, const byte* data, size_t size)
|
||||
/*@modifies hp */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hmacUpdate(hmacParam* hp, const hashFunction* hash, hashFunctionParam* param, const byte* data, int size)
|
||||
/*@modifies hp */;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int hmacDigest(hmacParam* hp, const hashFunction* hash, hashFunctionParam* param, /*@out@*/ uint32* data)
|
||||
/*@modifies hp, data */;
|
||||
int hmacDigest(const byte* kxo, const hashFunction* hash, hashFunctionParam* param, /*@out@*/ byte* data)
|
||||
/*@modifies data */;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HMAC_md5_m HMAC_m
|
||||
* \file hmacmd5.c
|
||||
*
|
||||
* HMAC-MD5 message authentication code, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,16 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file hmacmd5.c
|
||||
* HMAC-MD5 message authentication code.
|
||||
*
|
||||
* \see RFC2202 - Test Cases for HMAC-MD5 and HMAC-SHA-1.
|
||||
* P. Cheng, R. Glenn.
|
||||
*
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HMAC_m HMAC_md5_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "hmacmd5.h"
|
||||
#include "debug.h"
|
||||
|
@ -34,23 +36,23 @@ const keyedHashFunction hmacmd5 = { "HMAC-MD5", sizeof(hmacmd5Param), 64, 4 * si
|
|||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-type@*/
|
||||
int hmacmd5Setup (hmacmd5Param* sp, const uint32* key, int keybits)
|
||||
int hmacmd5Setup (hmacmd5Param* sp, const byte* key, size_t keybits)
|
||||
{
|
||||
return hmacSetup(&sp->hparam, &md5, &sp->mparam, key, keybits);
|
||||
return hmacSetup(sp->kxi, &sp->kxo, &md5, &sp->mparam, key, keybits);
|
||||
}
|
||||
|
||||
int hmacmd5Reset (hmacmd5Param* sp)
|
||||
{
|
||||
return hmacReset(&sp->hparam, &md5, &sp->mparam);
|
||||
return hmacReset(sp->kxi, &md5, &sp->mparam);
|
||||
}
|
||||
|
||||
int hmacmd5Update(hmacmd5Param* sp, const byte* data, int size)
|
||||
int hmacmd5Update(hmacmd5Param* sp, const byte* data, size_t size)
|
||||
{
|
||||
return hmacUpdate(&sp->hparam, &md5, &sp->mparam, data, size);
|
||||
return hmacUpdate(&md5, &sp->mparam, data, size);
|
||||
}
|
||||
|
||||
int hmacmd5Digest(hmacmd5Param* sp, uint32* data)
|
||||
int hmacmd5Digest(hmacmd5Param* sp, byte* data)
|
||||
{
|
||||
return hmacDigest(&sp->hparam, &md5, &sp->mparam, data);
|
||||
return hmacDigest(sp->kxo, &md5, &sp->mparam, data);
|
||||
}
|
||||
/*@=type@*/
|
||||
|
|
|
@ -35,8 +35,9 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
hmacParam hparam;
|
||||
md5Param mparam;
|
||||
byte kxi[64];
|
||||
byte kxo[64];
|
||||
} hmacmd5Param;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -52,7 +53,7 @@ extern BEECRYPTAPI const keyedHashFunction hmacmd5;
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacmd5Setup (hmacmd5Param* sp, const uint32* key, int keybits)
|
||||
int hmacmd5Setup (hmacmd5Param* sp, const byte* key, size_t keybits)
|
||||
/*@globals md5 @*/
|
||||
/*@modifies sp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
@ -70,7 +71,7 @@ int hmacmd5Reset (hmacmd5Param* sp)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacmd5Update(hmacmd5Param* sp, const byte* data, int size)
|
||||
int hmacmd5Update(hmacmd5Param* sp, const byte* data, size_t size)
|
||||
/*@globals md5 @*/
|
||||
/*@modifies sp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
@ -79,7 +80,7 @@ int hmacmd5Update(hmacmd5Param* sp, const byte* data, int size)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacmd5Digest(hmacmd5Param* sp, uint32* data)
|
||||
int hmacmd5Digest(hmacmd5Param* sp, byte* data)
|
||||
/*@globals md5 @*/
|
||||
/*@modifies sp, data @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HMAC_sha1_m HMAC_m
|
||||
* \file hmacsha1.c
|
||||
*
|
||||
* HMAC-SHA-1 message authentication code, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999, 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,16 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file hmacsha1.c
|
||||
* HMAC-SHA-1 message authentication code.
|
||||
*
|
||||
* \see RFC2202 - Test Cases for HMAC-MD5 and HMAC-SHA-1.
|
||||
* P. Cheng, R. Glenn.
|
||||
*
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HMAC_m HMAC_sha1_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "hmacsha1.h"
|
||||
#include "debug.h"
|
||||
|
@ -34,23 +36,23 @@ const keyedHashFunction hmacsha1 = { "HMAC-SHA-1", sizeof(hmacsha1Param), 64, 5
|
|||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-type@*/
|
||||
int hmacsha1Setup (hmacsha1Param* sp, const uint32* key, int keybits)
|
||||
int hmacsha1Setup (hmacsha1Param* sp, const byte* key, size_t keybits)
|
||||
{
|
||||
return hmacSetup(&sp->hparam, &sha1, &sp->sparam, key, keybits);
|
||||
return hmacSetup(sp->kxi, sp->kxo, &sha1, &sp->sparam, key, keybits);
|
||||
}
|
||||
|
||||
int hmacsha1Reset (hmacsha1Param* sp)
|
||||
{
|
||||
return hmacReset(&sp->hparam, &sha1, &sp->sparam);
|
||||
return hmacReset(sp->kxi, &sha1, &sp->sparam);
|
||||
}
|
||||
|
||||
int hmacsha1Update(hmacsha1Param* sp, const byte* data, int size)
|
||||
int hmacsha1Update(hmacsha1Param* sp, const byte* data, size_t size)
|
||||
{
|
||||
return hmacUpdate(&sp->hparam, &sha1, &sp->sparam, data, size);
|
||||
return hmacUpdate(&sha1, &sp->sparam, data, size);
|
||||
}
|
||||
|
||||
int hmacsha1Digest(hmacsha1Param* sp, uint32* data)
|
||||
int hmacsha1Digest(hmacsha1Param* sp, byte* data)
|
||||
{
|
||||
return hmacDigest(&sp->hparam, &sha1, &sp->sparam, data);
|
||||
return hmacDigest(sp->kxo, &sha1, &sp->sparam, data);
|
||||
}
|
||||
/*@=type@*/
|
||||
|
|
|
@ -35,8 +35,9 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
hmacParam hparam;
|
||||
sha1Param sparam;
|
||||
byte kxi[64];
|
||||
byte kxo[64];
|
||||
} hmacsha1Param;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -52,7 +53,7 @@ extern BEECRYPTAPI const keyedHashFunction hmacsha1;
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacsha1Setup (hmacsha1Param* sp, const uint32* key, int keybits)
|
||||
int hmacsha1Setup (hmacsha1Param* sp, const byte* key, size_t keybits)
|
||||
/*@globals sha1 @*/
|
||||
/*@modifies sp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
@ -70,7 +71,7 @@ int hmacsha1Reset (hmacsha1Param* sp)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacsha1Update(hmacsha1Param* sp, const byte* data, int size)
|
||||
int hmacsha1Update(hmacsha1Param* sp, const byte* data, size_t size)
|
||||
/*@globals sha1 @*/
|
||||
/*@modifies sp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
@ -79,7 +80,7 @@ int hmacsha1Update(hmacsha1Param* sp, const byte* data, int size)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacsha1Digest(hmacsha1Param* sp, uint32* data)
|
||||
int hmacsha1Digest(hmacsha1Param* sp, byte* data)
|
||||
/*@globals sha1 @*/
|
||||
/*@modifies sp, data @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HMAC_sha256_m HMAC_m
|
||||
* \file hmacsha256.c
|
||||
*
|
||||
* HMAC-SHA-256 message authentication code, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file hmacsha256.c
|
||||
* \brief HMAC-SHA-256 message digest algorithm.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HMAC_m HMAC_sha256_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "hmacsha256.h"
|
||||
#include "debug.h"
|
||||
|
@ -34,23 +32,23 @@ const keyedHashFunction hmacsha256 = { "HMAC-SHA-256", sizeof(hmacsha256Param),
|
|||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-type@*/
|
||||
int hmacsha256Setup (hmacsha256Param* sp, const uint32* key, int keybits)
|
||||
int hmacsha256Setup (hmacsha256Param* sp, const byte* key, size_t keybits)
|
||||
{
|
||||
return hmacSetup(&sp->hparam, &sha256, &sp->sparam, key, keybits);
|
||||
return hmacSetup(sp->kxi, sp->kxo, &sha256, &sp->sparam, key, keybits);
|
||||
}
|
||||
|
||||
int hmacsha256Reset (hmacsha256Param* sp)
|
||||
{
|
||||
return hmacReset(&sp->hparam, &sha256, &sp->sparam);
|
||||
return hmacReset(sp->kxi, &sha256, &sp->sparam);
|
||||
}
|
||||
|
||||
int hmacsha256Update(hmacsha256Param* sp, const byte* data, int size)
|
||||
int hmacsha256Update(hmacsha256Param* sp, const byte* data, size_t size)
|
||||
{
|
||||
return hmacUpdate(&sp->hparam, &sha256, &sp->sparam, data, size);
|
||||
return hmacUpdate(&sha256, &sp->sparam, data, size);
|
||||
}
|
||||
|
||||
int hmacsha256Digest(hmacsha256Param* sp, uint32* data)
|
||||
int hmacsha256Digest(hmacsha256Param* sp, byte* data)
|
||||
{
|
||||
return hmacDigest(&sp->hparam, &sha256, &sp->sparam, data);
|
||||
return hmacDigest(sp->kxo, &sha256, &sp->sparam, data);
|
||||
}
|
||||
/*@=type@*/
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HMAC_sha256_m HMAC_m
|
||||
* \file hmacsha256.h
|
||||
*
|
||||
* HMAC-SHA-256 message authentication code, header.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file hmacsha256.h
|
||||
* \brief HMAC-SHA-256 message authentication code, headers.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HMAC_m HMAC_sha256_m
|
||||
*/
|
||||
|
||||
#ifndef _HMACSHA256_H
|
||||
#define _HMACSHA256_H
|
||||
|
||||
|
@ -35,8 +33,9 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
hmacParam hparam;
|
||||
sha256Param sparam;
|
||||
byte kxi[64];
|
||||
byte kxo[64];
|
||||
} hmacsha256Param;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -52,7 +51,7 @@ extern BEECRYPTAPI const keyedHashFunction hmacsha256;
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacsha256Setup (hmacsha256Param* sp, const uint32* key, int keybits)
|
||||
int hmacsha256Setup (hmacsha256Param* sp, const byte* key, size_t keybits)
|
||||
/*@globals sha256 @*/
|
||||
/*@modifies sp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
@ -70,7 +69,7 @@ int hmacsha256Reset (hmacsha256Param* sp)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacsha256Update(hmacsha256Param* sp, const byte* data, int size)
|
||||
int hmacsha256Update(hmacsha256Param* sp, const byte* data, size_t size)
|
||||
/*@globals sha256 @*/
|
||||
/*@modifies sp @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
@ -79,7 +78,7 @@ int hmacsha256Update(hmacsha256Param* sp, const byte* data, int size)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int hmacsha256Digest(hmacsha256Param* sp, uint32* data)
|
||||
int hmacsha256Digest(hmacsha256Param* sp, byte* data)
|
||||
/*@globals sha256 @*/
|
||||
/*@modifies sp, data @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#
|
||||
# Copyright (c) 2001, 2002 Virtual Unlimited B.V.
|
||||
#
|
||||
# Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
# Author: Bob Deblier <bob.deblier@pandora.be>
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -22,4 +22,4 @@
|
|||
|
||||
AUTOMAKE_OPTIONS = gnu no-dependencies
|
||||
|
||||
EXTRA_DIST = aesopt.i586.asm blowfishopt.i586.asm mp32opt.i386.asm sha1opt.i586.asm
|
||||
EXTRA_DIST = aesopt.i586.asm blowfishopt.i586.asm mpopt.x86.asm sha1opt.i586.asm
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
;
|
||||
; Compile target is Microsoft Macro Assembler
|
||||
;
|
||||
; Copyright (c) 2002 Bob Deblier <bob@virtualunlimited.com>
|
||||
; Copyright (c) 2002 Bob Deblier <bob.deblier@pandora.be>
|
||||
;
|
||||
; This library is free software; you can redistribute it and/or
|
||||
; modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -574,7 +574,7 @@ dblock macro label
|
|||
dlr
|
||||
endm
|
||||
|
||||
aesEncrypt proc
|
||||
aesEncrypt proc c export
|
||||
push edi
|
||||
push esi
|
||||
push ebp
|
||||
|
@ -616,7 +616,7 @@ aesEncrypt proc
|
|||
ret
|
||||
aesEncrypt endp
|
||||
|
||||
aesDecrypt proc
|
||||
aesDecrypt proc c export
|
||||
push edi
|
||||
push esi
|
||||
push ebp
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
;
|
||||
; Copyright (c) 2000 Virtual Unlimited B.V.
|
||||
;
|
||||
; Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
; Author: Bob Deblier <bob.deblier@pandora.be>
|
||||
;
|
||||
; This library is free software; you can redistribute it and/or
|
||||
; modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -86,7 +86,7 @@ dtworounds macro offset
|
|||
|
||||
align 8
|
||||
|
||||
blowfishEncrypt proc
|
||||
blowfishEncrypt proc c export
|
||||
push edi
|
||||
push esi
|
||||
push ebx
|
||||
|
@ -133,7 +133,7 @@ blowfishEncrypt endp
|
|||
|
||||
align 8
|
||||
|
||||
blowfishDecrypt proc
|
||||
blowfishDecrypt proc c export
|
||||
push edi
|
||||
push esi
|
||||
push ebx
|
||||
|
|
|
@ -0,0 +1,447 @@
|
|||
;
|
||||
; mpopt.x86.asm
|
||||
;
|
||||
; Assembler optimized multiprecision integer routines for Intel x86 processors
|
||||
;
|
||||
; Copyright (c) 2003 Bob Deblier
|
||||
;
|
||||
; Author: Bob Deblier <bob.deblier@pandora.be>
|
||||
;
|
||||
; 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
|
||||
;
|
||||
|
||||
.586
|
||||
.model flat,C
|
||||
.xmm
|
||||
.code
|
||||
|
||||
align 8
|
||||
mpzero proc c export
|
||||
push edi
|
||||
|
||||
mov ecx,dword ptr [esp+8]
|
||||
mov edi,dword ptr [esp+12]
|
||||
|
||||
xor eax,eax
|
||||
rep stosd
|
||||
|
||||
pop edi
|
||||
ret
|
||||
mpzero endp
|
||||
|
||||
|
||||
align 8
|
||||
mpfill proc c export
|
||||
push edi
|
||||
|
||||
mov ecx,dword ptr [esp+8]
|
||||
mov edi,dword ptr [esp+12]
|
||||
mov eax,dword ptr [esp+16]
|
||||
|
||||
rep stosd
|
||||
|
||||
pop edi
|
||||
ret
|
||||
mpfill endp
|
||||
|
||||
|
||||
align 8
|
||||
mpodd proc c export
|
||||
mov ecx,dword ptr [esp+4]
|
||||
mov eax,dword ptr [esp+8]
|
||||
mov eax,dword ptr [eax+ecx*4-4]
|
||||
and eax,1
|
||||
ret
|
||||
mpodd endp
|
||||
|
||||
|
||||
align 8
|
||||
mpeven proc c export
|
||||
mov ecx,dword ptr [esp+4]
|
||||
mov eax,dword ptr [esp+8]
|
||||
mov eax,dword ptr [eax+ecx*4-4]
|
||||
not eax
|
||||
and eax,1
|
||||
ret
|
||||
mpeven endp
|
||||
|
||||
|
||||
align 8
|
||||
mpaddw proc c export
|
||||
push edi
|
||||
|
||||
mov ecx,dword ptr [esp+8]
|
||||
mov edi,dword ptr [esp+12]
|
||||
mov eax,dword ptr [esp+16]
|
||||
|
||||
lea edi,dword ptr [edi+ecx*4-4]
|
||||
add dword ptr [edi],eax
|
||||
dec ecx
|
||||
jz @mpaddw_end
|
||||
sub edi,4
|
||||
xor edx,edx
|
||||
|
||||
align 4
|
||||
@mpaddw_loop:
|
||||
adc dword ptr [edi],edx
|
||||
sub edi,4
|
||||
dec ecx
|
||||
jnz @mpaddw_loop
|
||||
@mpaddw_end:
|
||||
sbb eax,eax
|
||||
neg eax
|
||||
|
||||
pop edi
|
||||
ret
|
||||
mpaddw endp
|
||||
|
||||
|
||||
align 8
|
||||
mpsubw proc c export
|
||||
push edi
|
||||
|
||||
mov ecx,dword ptr [esp+8]
|
||||
mov edi,dword ptr [esp+12]
|
||||
mov eax,dword ptr [esp+16]
|
||||
|
||||
lea edi,dword ptr [edi+ecx*4-4]
|
||||
sub dword ptr [edi],eax
|
||||
dec ecx
|
||||
jz @mpsubw_end
|
||||
sub edi,4
|
||||
xor edx,edx
|
||||
|
||||
align 4
|
||||
@mpsubw_loop:
|
||||
sbb dword ptr [edi],edx
|
||||
sub edi,4
|
||||
dec ecx
|
||||
jnz @mpsubw_loop
|
||||
@mpsubw_end:
|
||||
sbb eax,eax
|
||||
neg eax
|
||||
|
||||
pop edi
|
||||
ret
|
||||
mpsubw endp
|
||||
|
||||
|
||||
align 8
|
||||
mpadd proc c export
|
||||
push edi
|
||||
push esi
|
||||
|
||||
mov ecx,dword ptr [esp+12]
|
||||
mov edi,dword ptr [esp+16]
|
||||
mov esi,dword ptr [esp+20]
|
||||
|
||||
xor edx,edx
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpadd_loop:
|
||||
mov eax,dword ptr [esi+ecx*4]
|
||||
mov edx,dword ptr [edi+ecx*4]
|
||||
adc edx,eax
|
||||
mov dword ptr [edi+ecx*4],edx
|
||||
dec ecx
|
||||
jns @mpadd_loop
|
||||
|
||||
sbb eax,eax
|
||||
neg eax
|
||||
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
mpadd endp
|
||||
|
||||
|
||||
align 8
|
||||
mpsub proc c export
|
||||
push edi
|
||||
push esi
|
||||
|
||||
mov ecx,dword ptr [esp+12]
|
||||
mov edi,dword ptr [esp+16]
|
||||
mov esi,dword ptr [esp+20]
|
||||
|
||||
xor edx,edx
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpsub_loop:
|
||||
mov eax,dword ptr [esi+ecx*4]
|
||||
mov edx,dword ptr [edi+ecx*4]
|
||||
sbb edx,eax
|
||||
mov dword ptr [edi+ecx*4],edx
|
||||
dec ecx
|
||||
jns @mpsub_loop
|
||||
|
||||
sbb eax,eax
|
||||
neg eax
|
||||
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
mpsub endp
|
||||
|
||||
|
||||
align 8
|
||||
mpdivtwo proc c export
|
||||
push edi
|
||||
|
||||
mov ecx,dword ptr [esp+8]
|
||||
mov edi,dword ptr [esp+12]
|
||||
|
||||
lea edi,dword ptr [edi+ecx*4]
|
||||
neg ecx
|
||||
clc
|
||||
|
||||
@mpdivtwo_loop:
|
||||
rcr dword ptr [edi+ecx*4],1
|
||||
inc ecx
|
||||
jnz @mpdivtwo_loop
|
||||
|
||||
pop edi
|
||||
ret
|
||||
mpdivtwo endp
|
||||
|
||||
|
||||
align 8
|
||||
mpmultwo proc c export
|
||||
push edi
|
||||
|
||||
mov ecx,dword ptr [esp+8]
|
||||
mov edi,dword ptr [esp+12]
|
||||
|
||||
dec ecx
|
||||
clc
|
||||
|
||||
align 4
|
||||
@mpmultwo_loop:
|
||||
mov eax,dword ptr [edi+ecx*4]
|
||||
adc eax,eax
|
||||
mov dword ptr [edi+ecx*4],eax
|
||||
dec ecx
|
||||
jns @mpmultwo_loop
|
||||
|
||||
sbb eax,eax
|
||||
neg eax
|
||||
|
||||
pop edi
|
||||
ret
|
||||
mpmultwo endp
|
||||
|
||||
|
||||
align 8
|
||||
mpsetmul proc c export
|
||||
push edi
|
||||
push esi
|
||||
|
||||
ifdef USE_SSE2
|
||||
|
||||
mov ecx,dword ptr [esp+12]
|
||||
mov edi,dword ptr [esp+16]
|
||||
mov esi,dword ptr [esp+20]
|
||||
movd mm1,dword ptr [esp+24]
|
||||
|
||||
pxor mm0,mm0
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpsetmul_loop:
|
||||
movd mm2,dword ptr [esi+ecx*4]
|
||||
pmuludq mm2,mm1
|
||||
paddq mm0,mm2
|
||||
movd dword ptr [edi+ecx*4],mm0
|
||||
dec ecx
|
||||
psrlq mm0,32
|
||||
jns @mpsetmul_loop
|
||||
|
||||
movd eax,mm0
|
||||
emms
|
||||
|
||||
else
|
||||
|
||||
push ebx
|
||||
push ebp
|
||||
|
||||
mov ecx,dword ptr [esp+20]
|
||||
mov edi,dword ptr [esp+24]
|
||||
mov esi,dword ptr [esp+28]
|
||||
mov ebp,dword ptr [esp+32]
|
||||
|
||||
xor edx,edx
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpsetmul_loop:
|
||||
mov ebx,edx
|
||||
mov eax,dword ptr [esi+ecx*4]
|
||||
mul ebp
|
||||
add eax,ebx
|
||||
adc edx,0
|
||||
mov dword ptr [edi+ecx*4],eax
|
||||
dec ecx
|
||||
jns @mpsetmul_loop
|
||||
|
||||
mov eax,edx
|
||||
|
||||
pop ebp
|
||||
pop ebx
|
||||
|
||||
endif
|
||||
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
mpsetmul endp
|
||||
|
||||
|
||||
align 8
|
||||
mpaddmul proc c export
|
||||
push edi
|
||||
push esi
|
||||
|
||||
ifdef USE_SSE2
|
||||
|
||||
mov ecx,dword ptr [esp+12]
|
||||
mov edi,dword ptr [esp+16]
|
||||
mov esi,dword ptr [esp+20]
|
||||
movd mm1,dword ptr [esp+24]
|
||||
|
||||
pxor mm0,mm0
|
||||
dec ecx
|
||||
|
||||
@mpaddmul_loop:
|
||||
movd mm2,dword ptr [esi+ecx*4]
|
||||
pmuludq mm2,mm1
|
||||
movd mm3,dword ptr [edi+ecx*4]
|
||||
paddq mm3,mm2
|
||||
paddq mm0,mm3
|
||||
movd dword ptr [edi+ecx*4],mm0
|
||||
dec ecx
|
||||
psrlq mm0,32
|
||||
jns @mpaddmul_loop
|
||||
|
||||
movd eax,mm0
|
||||
emms
|
||||
|
||||
else
|
||||
|
||||
push ebx
|
||||
push ebp
|
||||
|
||||
mov ecx,dword ptr [esp+20]
|
||||
mov edi,dword ptr [esp+24]
|
||||
mov esi,dword ptr [esp+28]
|
||||
mov ebp,dword ptr [esp+32]
|
||||
|
||||
xor edx,edx
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpaddmul_loop:
|
||||
mov ebx,edx
|
||||
mov eax,dword ptr [esi+ecx*4]
|
||||
mul ebp
|
||||
add eax,ebx
|
||||
adc edx,0
|
||||
add dword ptr [edi+ecx*4],eax
|
||||
adc edx,0
|
||||
dec ecx
|
||||
jns @mpaddmul_loop
|
||||
|
||||
mov eax,edx
|
||||
|
||||
pop ebp
|
||||
pop ebx
|
||||
|
||||
endif
|
||||
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
mpaddmul endp
|
||||
|
||||
|
||||
align 8
|
||||
mpaddsqrtrc proc c export
|
||||
push edi
|
||||
push esi
|
||||
|
||||
ifdef USE_SSE2
|
||||
mov ecx,dword ptr [esp+12]
|
||||
mov edi,dword ptr [esp+16]
|
||||
mov esi,dword ptr [esp+20]
|
||||
|
||||
pxor mm0,mm0
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpaddsqrtrc_loop:
|
||||
movd mm2,dword ptr [esi+ecx*4]
|
||||
pmuludq mm2,mm2
|
||||
movd mm3,dword ptr [edi+ecx*8+4]
|
||||
paddq mm3,mm2
|
||||
movd mm4,dword ptr [edi+ecx*8+0]
|
||||
paddq mm0,mm3
|
||||
movd dword ptr [edi+ecx*8+4],mm0
|
||||
psrlq mm0,32
|
||||
paddq mm0,mm4
|
||||
movd dword ptr [edi+ecx*8+0],mm0
|
||||
psrlq mm0,32
|
||||
dec ecx
|
||||
jns @mpaddsqrtrc_loop
|
||||
|
||||
movd eax,mm0
|
||||
emms
|
||||
|
||||
else
|
||||
|
||||
push ebx
|
||||
|
||||
mov ecx,dword ptr [esp+16]
|
||||
mov edi,dword ptr [esp+20]
|
||||
mov esi,dword ptr [esp+24]
|
||||
|
||||
xor ebx,ebx
|
||||
dec ecx
|
||||
|
||||
align 4
|
||||
@mpaddsqrtrc_loop:
|
||||
mov eax,dword ptr [esi+ecx*4]
|
||||
mul eax
|
||||
add eax,ebx
|
||||
adc edx,0
|
||||
add dword ptr [edi+ecx*8+4],eax
|
||||
adc dword ptr [edi+ecx*8+0],edx
|
||||
sbb ebx,ebx
|
||||
neg ebx
|
||||
dec ecx
|
||||
jns @mpaddsqrtrc_loop
|
||||
|
||||
mov eax,ebx
|
||||
|
||||
pop ebx
|
||||
|
||||
endif
|
||||
|
||||
pop esi
|
||||
pop edi
|
||||
ret
|
||||
mpaddsqrtrc endp
|
||||
|
||||
end
|
|
@ -7,7 +7,7 @@
|
|||
;
|
||||
; Copyright (c) 2000 Virtual Unlimited B.V.
|
||||
;
|
||||
; Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
; Author: Bob Deblier <bob.deblier@pandora.be>
|
||||
;
|
||||
; This library is free software; you can redistribute it and/or
|
||||
; modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -34,7 +34,6 @@ K60 equ 0ca62c1d6h
|
|||
|
||||
PARAM_H equ 0
|
||||
PARAM_DATA equ 20
|
||||
PARAM_OFFSET equ 352
|
||||
|
||||
.code
|
||||
|
||||
|
@ -136,7 +135,6 @@ sha1Process proc
|
|||
jns @swaps
|
||||
|
||||
lea edi,dword ptr [esi+PARAM_DATA]
|
||||
mov byte ptr [esi+PARAM_OFFSET],al
|
||||
mov ecx,16
|
||||
|
||||
align 4
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HASH_md5_m HASH_m
|
||||
* \file md5.c
|
||||
*
|
||||
* MD5 hash function, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file md5.c
|
||||
* \brief MD5 hash function
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HASH_m HASH_md5_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "md5.h"
|
||||
#include "mp.h"
|
||||
|
@ -52,7 +50,6 @@ int md5Reset(register md5Param* p)
|
|||
#else
|
||||
# error
|
||||
#endif
|
||||
p->length = 0;
|
||||
p->offset = 0;
|
||||
return 0;
|
||||
}
|
||||
|
@ -257,7 +254,7 @@ static void md5Finish(md5Param* p)
|
|||
ptr[7] = (byte)(p->length[0] >> 24);
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
#endif
|
||||
|
||||
md5Process(p);
|
||||
|
||||
|
|
749
beecrypt/mp.c
749
beecrypt/mp.c
File diff suppressed because it is too large
Load Diff
170
beecrypt/mp.h
170
beecrypt/mp.h
|
@ -37,6 +37,32 @@
|
|||
|
||||
#include "mpopt.h"
|
||||
|
||||
#define MP_HWBITS (MP_WBITS >> 1)
|
||||
#define MP_WBYTES (MP_WBITS >> 3)
|
||||
#define MP_WNIBBLES (MP_WBITS >> 2)
|
||||
|
||||
#if (MP_WBITS == 64)
|
||||
# define MP_WORDS_TO_BITS(x) ((x) << 6)
|
||||
# define MP_WORDS_TO_NIBBLES(x) ((x) << 4)
|
||||
# define MP_WORDS_TO_BYTES(x) ((x) << 3)
|
||||
# define MP_BITS_TO_WORDS(x) ((x) >> 6)
|
||||
# define MP_NIBBLES_TO_WORDS(x) ((x) >> 4)
|
||||
# define MP_BYTES_TO_WORDS(x) ((x) >> 3)
|
||||
#elif (MP_WBITS == 32)
|
||||
# define MP_WORDS_TO_BITS(x) ((x) << 5)
|
||||
# define MP_WORDS_TO_NIBBLES(x) ((x) << 3)
|
||||
# define MP_WORDS_TO_BYTES(x) ((x) << 2)
|
||||
# define MP_BITS_TO_WORDS(x) ((x) >> 5)
|
||||
# define MP_NIBBLES_TO_WORDS(x) ((x) >> 3)
|
||||
# define MP_BYTES_TO_WORDS(x) ((x) >> 2)
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
|
||||
#define MP_MSBMASK (((mpw) 0x1) << (MP_WBITS-1))
|
||||
#define MP_LSBMASK ((mpw) 0x1)
|
||||
#define MP_ALLMASK ~((mpw) 0x0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
@ -46,12 +72,8 @@ extern "C" {
|
|||
BEECRYPTAPI /*@unused@*/
|
||||
void mpcopy(size_t size, /*@out@*/ mpw* dst, const mpw* src)
|
||||
/*@modifies dst @*/;
|
||||
#ifndef ASM_MP32COPY
|
||||
#ifdef __LCLINT__
|
||||
# define mpcopy(size, dst, src) memmove(dst, src, ((unsigned)(size)) << 2)
|
||||
#else
|
||||
# define mpcopy(size, dst, src) memcpy(dst, src, (size) << 2)
|
||||
#endif
|
||||
#ifndef ASM_MPCOPY
|
||||
# define mpcopy(size, dst, src) memcpy(dst, src, MP_WORDS_TO_BYTES(size))
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@ -59,49 +81,45 @@ void mpcopy(size_t size, /*@out@*/ mpw* dst, const mpw* src)
|
|||
BEECRYPTAPI /*@unused@*/
|
||||
void mpmove(size_t size, /*@out@*/ mpw* dst, const mpw* src)
|
||||
/*@modifies dst @*/;
|
||||
#ifndef ASM_MP32MOVE
|
||||
#ifdef __LCLINT__
|
||||
# define mpmove(size, dst, src) memmove(dst, src, ((unsigned)(size)) << 2)
|
||||
#else
|
||||
# define mpmove(size, dst, src) memmove(dst, src, (size) << 2)
|
||||
#endif
|
||||
#ifndef ASM_MPMOVE
|
||||
# define mpmove(size, dst, src) memmove(dst, src, MP_WORDS_TO_BYTES(size))
|
||||
#endif
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mpzero(size_t xsize, /*@out@*/ mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpzero(size_t size, /*@out@*/ mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
void mpfill(size_t xsize, /*@out@*/ mpw* xdata, uint32 val)
|
||||
/*@modifies xdata @*/;
|
||||
void mpfill(size_t size, /*@out@*/ mpw* data, mpw fill)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpodd (size_t xsize, const mpw* xdata)
|
||||
int mpodd (size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpeven(size_t xsize, const mpw* xdata)
|
||||
int mpeven(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpz (size_t xsize, const mpw* xdata)
|
||||
int mpz (size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
int mpnz (size_t xsize, const mpw* xdata)
|
||||
int mpnz (size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
|
@ -186,19 +204,19 @@ int mplex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpisone(size_t xsize, const mpw* xdata)
|
||||
int mpisone(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpistwo(size_t xsize, const mpw* xdata)
|
||||
int mpistwo(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpleone(size_t xsize, const mpw* xdata)
|
||||
int mpleone(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
|
@ -210,38 +228,38 @@ int mpeqmone(size_t size, const mpw* xdata, const mpw* ydata)
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpmsbset(size_t xsize, const mpw* xdata)
|
||||
int mpmsbset(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int mplsbset(size_t xsize, const mpw* xdata)
|
||||
int mplsbset(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
void mpsetmsb(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpsetmsb(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mpsetlsb(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpsetlsb(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
void mpclrmsb(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpclrmsb(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
void mpclrlsb(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpclrlsb(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
|
@ -265,8 +283,8 @@ void mpor(size_t size, mpw* xdata, const mpw* ydata)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
void mpnot(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpnot(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/**
|
||||
|
@ -290,7 +308,7 @@ int mpaddw(size_t xsize, mpw* xdata, uint32 y)
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
uint32 mpadd (size_t size, mpw* xdata, const mpw* ydata)
|
||||
int mpadd (size_t size, mpw* xdata, const mpw* ydata)
|
||||
/*@modifies xdata @*/;
|
||||
|
||||
/**
|
||||
|
@ -320,31 +338,37 @@ int mpsubx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata)
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mpmultwo(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
int mpmultwo(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mpneg(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpneg(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
size_t mpsize(size_t xsize, const mpw* xdata)
|
||||
size_t mpsize(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
size_t mpbits(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
size_t mpmszcnt(size_t xsize, const mpw* xdata)
|
||||
size_t mpmszcnt(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
size_t mpbitcnt(size_t xsize, const mpw* xdata)
|
||||
size_t mpbitcnt(size_t size, const mpw* data)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
|
@ -359,56 +383,50 @@ size_t mplszcnt(size_t xsize, const mpw* xdata)
|
|||
*/
|
||||
/*@-exportlocal@*/
|
||||
BEECRYPTAPI
|
||||
void mplshift(size_t xsize, mpw* xdata, uint32 count)
|
||||
/*@modifies xdata @*/;
|
||||
void mplshift(size_t size, mpw* data, size_t count)
|
||||
/*@modifies data @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mprshift(size_t xsize, mpw* xdata, uint32 count)
|
||||
/*@modifies xdata @*/;
|
||||
void mprshift(size_t size, mpw* data, size_t count)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
size_t mprshiftlsz(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
size_t mprshiftlsz(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
size_t mpnorm(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
uint32 mpdivpowtwo(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
size_t mpnorm(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mpdivtwo (size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpdivtwo (size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mpsdivtwo(size_t xsize, mpw* xdata)
|
||||
/*@modifies xdata @*/;
|
||||
void mpsdivtwo(size_t size, mpw* data)
|
||||
/*@modifies data @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
uint32 mpsetmul (size_t size, /*@out@*/ mpw* result, const mpw* xdata, uint32 y)
|
||||
mpw mpsetmul (size_t size, /*@out@*/ mpw* result, const mpw* xdata, mpw y)
|
||||
/*@modifies result @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
uint32 mpaddmul (size_t size, /*@out@*/ mpw* result, const mpw* xdata, uint32 y)
|
||||
mpw mpaddmul (size_t size, /*@out@*/ mpw* result, const mpw* xdata, mpw y)
|
||||
/*@modifies result @*/;
|
||||
|
||||
/**
|
||||
|
@ -440,7 +458,13 @@ void mpgcd_w(size_t size, const mpw* xdata, const mpw* ydata, /*@out@*/ mpw* res
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
uint32 mpnmodw(/*@out@*/ mpw* result, size_t xsize, const mpw* xdata, uint32 y, /*@out@*/ mpw* wksp)
|
||||
mpw mppndiv(mpw xhi, mpw xlo, mpw y)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
mpw mpnmodw(/*@out@*/ mpw* result, size_t xsize, const mpw* xdata, mpw y, /*@out@*/ mpw* wksp)
|
||||
/*@modifies result, wksp @*/;
|
||||
|
||||
/**
|
||||
|
@ -469,6 +493,24 @@ void mpprintln(/*@null@*/ FILE * fp, size_t xsize, /*@null@*/ const mpw* xdata)
|
|||
/*@globals fileSystem @*/
|
||||
/*@modifies *fp, fileSystem @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int i2osp(/*@out@*/ byte *osdata, size_t ossize, const mpw* idata, size_t isize)
|
||||
/*@modifies osdata @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int os2ip(/*@out@*/ mpw *idata, size_t isize, const byte* osdata, size_t ossize)
|
||||
/*@modifies idata @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int hs2ip(/*@out@*/ mpw* idata, size_t isize, const char* hsdata, size_t hssize)
|
||||
/*@modifies idata @*/;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,20 +1,7 @@
|
|||
/*@-sizeoftype -type@*/
|
||||
/** \ingroup MP_m
|
||||
* \file mpbarrett.c
|
||||
*
|
||||
* Barrett modular reduction, code.
|
||||
*
|
||||
* For more information on this algorithm, see:
|
||||
* "Handbook of Applied Cryptography", Chapter 14.3.3
|
||||
* Menezes, van Oorschot, Vanstone
|
||||
* CRC Press
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -31,6 +18,16 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file mpbarrett.c
|
||||
* \brief Multi-precision integer routines using Barrett modular reduction.
|
||||
* For more information on this algorithm, see:
|
||||
* "Handbook of Applied Cryptography", Chapter 14.3.3
|
||||
* Menezes, van Oorschot, Vanstone
|
||||
* CRC Press
|
||||
* \author Bob Deblier <bob@virtualunlimited.com>
|
||||
* \ingroup MP__m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "mp.h"
|
||||
#include "mpprime.h"
|
||||
|
@ -167,7 +164,6 @@ void mpbsethex(mpbarrett* b, const char* hex)
|
|||
{
|
||||
size_t length = strlen(hex);
|
||||
size_t size = (length+7) >> 3;
|
||||
uint8 rem = (uint8)(length & 0x7);
|
||||
|
||||
if (b->modl)
|
||||
{
|
||||
|
@ -179,35 +175,12 @@ void mpbsethex(mpbarrett* b, const char* hex)
|
|||
|
||||
if (b->modl != (mpw*) 0)
|
||||
{
|
||||
register size_t val = 0;
|
||||
register mpw* dst = b->modl;
|
||||
register mpw* temp = (mpw*) malloc((6*size+4) * sizeof(*temp));
|
||||
register char ch;
|
||||
|
||||
b->size = size;
|
||||
b->mu = b->modl+size;
|
||||
|
||||
while (length-- > 0)
|
||||
{
|
||||
ch = *(hex++);
|
||||
val <<= 4;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
val += (ch - '0');
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
val += (ch - 'A') + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
val += (ch - 'a') + 10;
|
||||
else
|
||||
{};
|
||||
|
||||
if ((length & 0x7) == 0)
|
||||
{
|
||||
*(dst++) = val;
|
||||
val = 0;
|
||||
}
|
||||
}
|
||||
if (rem != 0)
|
||||
*dst = val;
|
||||
hs2ip(b->modl, size, hex, length);
|
||||
|
||||
/*@-nullpass@*/ /* temp may be NULL */
|
||||
mpbmu_w(b, temp);
|
||||
|
@ -286,7 +259,7 @@ void mpbrnd_w(const mpbarrett* b, randomGeneratorContext* rc, mpw* result, mpw*
|
|||
/*@-boundswrite@*/
|
||||
void mpbrndodd_w(const mpbarrett* b, randomGeneratorContext* rc, mpw* result, mpw* wksp)
|
||||
{
|
||||
uint32 msz = mpmszcnt(b->size, b->modl);
|
||||
size_t msz = mpmszcnt(b->size, b->modl);
|
||||
|
||||
mpcopy(b->size, wksp, b->modl);
|
||||
(void) mpsubw(b->size, wksp, 1);
|
||||
|
@ -406,7 +379,7 @@ void mpbsubone(const mpbarrett* b, mpw* result)
|
|||
/*@-boundswrite@*/
|
||||
void mpbneg(const mpbarrett* b, const mpw* data, mpw* result)
|
||||
{
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
|
||||
mpcopy(size, result, data);
|
||||
mpneg(size, result);
|
||||
|
@ -421,10 +394,10 @@ void mpbneg(const mpbarrett* b, const mpw* data, mpw* result)
|
|||
void mpbaddmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata, mpw* result, mpw* wksp)
|
||||
{
|
||||
/* xsize and ysize must be less than or equal to b->size */
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = wksp + size*2+2;
|
||||
|
||||
mpsetx(2*size, temp, xsize, data);
|
||||
mpsetx(2*size, temp, xsize, xdata);
|
||||
(void) mpaddx(2*size, temp, ysize, ydata);
|
||||
|
||||
mpbmod_w(b, temp, result, wksp);
|
||||
|
@ -437,7 +410,7 @@ void mpbaddmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, size_t ysiz
|
|||
void mpbsubmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata, mpw* result, mpw* wksp)
|
||||
{
|
||||
/* xsize and ysize must be less than or equal to b->size */
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = wksp + size*2+2;
|
||||
|
||||
mpsetx(2*size, temp, xsize, xdata);
|
||||
|
@ -454,7 +427,7 @@ void mpbsubmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, size_t ysiz
|
|||
void mpbmulmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata, mpw* result, mpw* wksp)
|
||||
{
|
||||
/* xsize and ysize must be <= b->size */
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = wksp + size*2+2;
|
||||
register mpw fill = size*2-xsize-ysize;
|
||||
|
||||
|
@ -474,7 +447,7 @@ void mpbmulmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, size_t ysiz
|
|||
void mpbsqrmod_w(const mpbarrett* b, size_t xsize, const mpw* xdata, mpw* result, mpw* wksp)
|
||||
{
|
||||
/* xsize must be <= b->size */
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = wksp + size*2+2;
|
||||
register mpw fill = 2*(size-xsize);
|
||||
|
||||
|
@ -615,7 +588,6 @@ void mpbpowmodsld_w(const mpbarrett* b, const mpw* slide, size_t psize, const mp
|
|||
psize--;
|
||||
}
|
||||
|
||||
/*@+charindex@*/
|
||||
/* if temp is still zero, then we're trying to raise x to power zero, and result stays one */
|
||||
if (temp)
|
||||
{
|
||||
|
@ -651,14 +623,14 @@ void mpbpowmodsld_w(const mpbarrett* b, const mpw* slide, size_t psize, const mp
|
|||
if (l == 4)
|
||||
{
|
||||
byte s = mpbslide_presq[n];
|
||||
|
||||
|
||||
while (s--)
|
||||
mpbsqrmod_w(b, size, result, result, wksp);
|
||||
|
||||
|
||||
mpbmulmod_w(b, size, result, size, slide+mpbslide_mulg[n]*size, result, wksp);
|
||||
|
||||
s = mpbslide_postsq[n];
|
||||
|
||||
|
||||
while (s--)
|
||||
mpbsqrmod_w(b, size, result, result, wksp);
|
||||
|
||||
|
@ -683,16 +655,15 @@ void mpbpowmodsld_w(const mpbarrett* b, const mpw* slide, size_t psize, const mp
|
|||
byte s = mpbslide_presq[n];
|
||||
while (s--)
|
||||
mpbsqrmod_w(b, size, result, result, wksp);
|
||||
|
||||
|
||||
mpbmulmod_w(b, size, result, size, slide+mpbslide_mulg[n]*size, result, wksp);
|
||||
|
||||
|
||||
s = mpbslide_postsq[n];
|
||||
|
||||
|
||||
while (s--)
|
||||
mpbsqrmod_w(b, size, result, result, wksp);
|
||||
}
|
||||
}
|
||||
/*@=charindex@*/
|
||||
}
|
||||
/*@=boundsread@*/
|
||||
|
||||
|
@ -1141,7 +1112,7 @@ fprintf(stderr, " t3: "), mpprintln(stderr, ysize, t3);
|
|||
* needs workspace of (7*size+2) words
|
||||
*/
|
||||
/*@-boundsread@*/
|
||||
int mpbpprime_w(const mpbarrett* b, randomGeneratorContext* rc, int t, mpw* wksp)
|
||||
int mpbpprime_w(const mpbarrett* b, randomGeneratorContext* r, int t, mpw* wksp)
|
||||
{
|
||||
/*
|
||||
* This test works for candidate probable primes >= 3, which are also not small primes.
|
||||
|
@ -1184,7 +1155,7 @@ int mpbpprime_w(const mpbarrett* b, randomGeneratorContext* rc, int t, mpw* wksp
|
|||
|
||||
if (mpisone(size, wksp))
|
||||
{
|
||||
return mppmilrab_w(b, rc, t, wksp);
|
||||
return mppmilrab_w(b, r, t, wksp);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1194,7 +1165,7 @@ int mpbpprime_w(const mpbarrett* b, randomGeneratorContext* rc, int t, mpw* wksp
|
|||
|
||||
void mpbnrnd(const mpbarrett* b, randomGeneratorContext* rc, mpnumber* result)
|
||||
{
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = (mpw*) malloc(size * sizeof(*temp));
|
||||
|
||||
mpnfree(result);
|
||||
|
@ -1210,7 +1181,7 @@ void mpbnrnd(const mpbarrett* b, randomGeneratorContext* rc, mpnumber* result)
|
|||
|
||||
void mpbnmulmod(const mpbarrett* b, const mpnumber* x, const mpnumber* y, mpnumber* result)
|
||||
{
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = (mpw*) malloc((4*size+2) * sizeof(*temp));
|
||||
|
||||
/* xsize and ysize must be <= b->size */
|
||||
|
@ -1237,7 +1208,7 @@ void mpbnmulmod(const mpbarrett* b, const mpnumber* x, const mpnumber* y, mpnumb
|
|||
|
||||
void mpbnsqrmod(const mpbarrett* b, const mpnumber* x, mpnumber* result)
|
||||
{
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = (mpw*) malloc(size * sizeof(*temp));
|
||||
|
||||
/* xsize must be <= b->size */
|
||||
|
@ -1246,13 +1217,11 @@ void mpbnsqrmod(const mpbarrett* b, const mpnumber* x, mpnumber* result)
|
|||
register mpw* opnd = temp + size*2+2;
|
||||
/*@=nullptrarith@*/
|
||||
|
||||
mpnfree(result);
|
||||
mpnsize(result, size);
|
||||
|
||||
if (fill)
|
||||
mpzero(fill, opnd);
|
||||
|
||||
mpsqr(opnd+fill, x->size, x->data);
|
||||
mpnsize(result, size);
|
||||
/*@-nullpass@*/ /* temp may be NULL */
|
||||
/*@-usedef -compdef @*/ /* result->data unallocated? */
|
||||
mpbmod_w(b, opnd, result->data, temp);
|
||||
|
@ -1264,7 +1233,7 @@ void mpbnsqrmod(const mpbarrett* b, const mpnumber* x, mpnumber* result)
|
|||
|
||||
void mpbnpowmod(const mpbarrett* b, const mpnumber* x, const mpnumber* pow, mpnumber* y)
|
||||
{
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = (mpw*) malloc((4*size+2) * sizeof(*temp));
|
||||
|
||||
mpnfree(y);
|
||||
|
@ -1279,7 +1248,7 @@ void mpbnpowmod(const mpbarrett* b, const mpnumber* x, const mpnumber* pow, mpnu
|
|||
|
||||
void mpbnpowmodsld(const mpbarrett* b, const mpw* slide, const mpnumber* pow, mpnumber* y)
|
||||
{
|
||||
register size_t size = b->size;
|
||||
register size_t size = b->size;
|
||||
register mpw* temp = (mpw*) malloc((4*size+2) * sizeof(*temp));
|
||||
|
||||
mpnfree(y);
|
||||
|
|
|
@ -1,15 +1,7 @@
|
|||
/** \ingroup MP_m
|
||||
* \file mpnumber.c
|
||||
*
|
||||
* Multiple precision numbers, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2003 Bob Deblier
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -26,6 +18,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file mpnumber.c
|
||||
* \brief Multi-precision numbers.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup MP_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "mpnumber.h"
|
||||
#include "mp.h"
|
||||
|
@ -70,7 +68,6 @@ void mpnsize(mpnumber* n, size_t size)
|
|||
}
|
||||
/*@=compdef @*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
void mpninit(mpnumber* n, size_t size, const mpw* data)
|
||||
{
|
||||
n->size = size;
|
||||
|
@ -82,9 +79,8 @@ void mpninit(mpnumber* n, size_t size, const mpw* data)
|
|||
n->data = (mpw*) malloc(size * sizeof(*n->data));
|
||||
|
||||
if (n->data && data)
|
||||
mp32copy(size, n->data, data);
|
||||
mpcopy(size, n->data, data);
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
void mpnfree(mpnumber* n)
|
||||
{
|
||||
|
@ -104,10 +100,9 @@ void mpncopy(mpnumber* n, const mpnumber* copy)
|
|||
void mpnwipe(mpnumber* n)
|
||||
{
|
||||
if (n->data)
|
||||
mp32zero(n->size, n->data);
|
||||
mpzero(n->size, n->data);
|
||||
}
|
||||
|
||||
/*@-boundswrite@*/
|
||||
void mpnset(mpnumber* n, size_t size, const mpw* data)
|
||||
{
|
||||
if (size)
|
||||
|
@ -122,7 +117,7 @@ void mpnset(mpnumber* n, size_t size, const mpw* data)
|
|||
|
||||
if (n->data && data)
|
||||
/*@-nullpass@*/ /* data is notnull */
|
||||
mp32copy(n->size = size, n->data, data);
|
||||
mpcopy(n->size = size, n->data, data);
|
||||
/*@=nullpass@*/
|
||||
else
|
||||
{
|
||||
|
@ -139,9 +134,7 @@ void mpnset(mpnumber* n, size_t size, const mpw* data)
|
|||
else
|
||||
{};
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
void mpnsetw(mpnumber* n, mpw val)
|
||||
{
|
||||
if (n->data)
|
||||
|
@ -163,15 +156,12 @@ void mpnsetw(mpnumber* n, mpw val)
|
|||
n->data = (mpw*) 0;
|
||||
}
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
/*@-usedef @*/ /* n->data may be NULL */
|
||||
void mpnsethex(mpnumber* n, const char* hex)
|
||||
{
|
||||
register size_t len = strlen(hex);
|
||||
register size_t size = (len+7) >> 3;
|
||||
uint8 rem = (uint8)(len & 0x7);
|
||||
register size_t size = MP_NIBBLES_TO_WORDS(len + MP_WNIBBLES - 1);
|
||||
|
||||
if (n->data)
|
||||
{
|
||||
|
@ -183,41 +173,13 @@ void mpnsethex(mpnumber* n, const char* hex)
|
|||
|
||||
if (n->data)
|
||||
{
|
||||
register size_t val = 0;
|
||||
register mpw* dst = n->data;
|
||||
register char ch;
|
||||
|
||||
n->size = size;
|
||||
|
||||
while (len-- > 0)
|
||||
{
|
||||
ch = *(hex++);
|
||||
val <<= 4;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
val += (ch - '0');
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
val += (ch - 'A') + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
val += (ch - 'a') + 10;
|
||||
else
|
||||
{};
|
||||
|
||||
if ((len & 0x7) == 0)
|
||||
{
|
||||
*(dst++) = val;
|
||||
val = 0;
|
||||
rem = 0;
|
||||
} else
|
||||
rem = 1;
|
||||
}
|
||||
if (rem != 0) {
|
||||
*dst = val;
|
||||
}
|
||||
hs2ip(n->data, size, hex, len);
|
||||
}
|
||||
else {
|
||||
n->size = 0;
|
||||
n->data = (uint32*)0;
|
||||
n->data = (mpw*)0;
|
||||
}
|
||||
}
|
||||
/*@=usedef @*/
|
||||
/*@=boundswrite@*/
|
||||
|
|
158
beecrypt/mpopt.h
158
beecrypt/mpopt.h
|
@ -34,108 +34,108 @@ extern "C" {
|
|||
|
||||
#if WIN32
|
||||
# if __MWERKS__ && __INTEL__
|
||||
# define ASM_MP32ZERO
|
||||
# define ASM_MP32FILL
|
||||
# define ASM_MP32EVEN
|
||||
# define ASM_MP32ODD
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPZERO
|
||||
# define ASM_MPFILL
|
||||
# define ASM_MPEVEN
|
||||
# define ASM_MPODD
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# elif defined(_MSC_VER) && defined(_M_IX86)
|
||||
# define ASM_MP32ZERO
|
||||
# define ASM_MP32FILL
|
||||
# define ASM_MP32EVEN
|
||||
# define ASM_MP32ODD
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32DIVTWO
|
||||
# define ASM_MP32MULTWO
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPZERO
|
||||
# define ASM_MPFILL
|
||||
# define ASM_MPEVEN
|
||||
# define ASM_MPODD
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPDIVTWO
|
||||
# define ASM_MPMULTWO
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__)
|
||||
# if defined(OPTIMIZE_ARM)
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# endif
|
||||
# if defined(OPTIMIZE_I386) || defined(OPTIMIZE_I486) || defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686)
|
||||
# define ASM_MP32ZERO
|
||||
# define ASM_MP32FILL
|
||||
# define ASM_MP32EVEN
|
||||
# define ASM_MP32ODD
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32DIVTWO
|
||||
# define ASM_MP32MULTWO
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPZERO
|
||||
# define ASM_MPFILL
|
||||
# define ASM_MPEVEN
|
||||
# define ASM_MPODD
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPDIVTWO
|
||||
# define ASM_MPMULTWO
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
# if defined(OPTIMIZE_IA64)
|
||||
# define ASM_MP32ZERO
|
||||
# define ASM_MP32COPY
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUB
|
||||
# undef ASM_MP32SETMUL
|
||||
# undef ASM_MP32ADDMUL
|
||||
# define ASM_MPZERO
|
||||
# define ASM_MPCOPY
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUB
|
||||
# undef ASM_MPSETMUL
|
||||
# undef ASM_MPADDMUL
|
||||
# endif
|
||||
# if defined(OPTIMIZE_POWERPC)
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32MULTWO
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPMULTWO
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
# if defined(OPTIMIZE_SPARCV8)
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
# if defined(OPTIMIZE_SPARCV8PLUS) || defined(OPTIMIZE_SPARCV9)
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32MULTWO
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPMULTWO
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
||||
# if defined(OPTIMIZE_SPARCV8PLUS) /* || defined(OPTIMIZE_SPARCV9) */
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
# if defined(OPTIMIZE_I386) || defined(OPTIMIZE_I486) || defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686)
|
||||
# define ASM_MP32ADDW
|
||||
# define ASM_MP32ADD
|
||||
# define ASM_MP32SUBW
|
||||
# define ASM_MP32SUB
|
||||
# define ASM_MP32MULTWO
|
||||
# define ASM_MP32SETMUL
|
||||
# define ASM_MP32ADDMUL
|
||||
# define ASM_MP32ADDSQRTRC
|
||||
# define ASM_MPADDW
|
||||
# define ASM_MPADD
|
||||
# define ASM_MPSUBW
|
||||
# define ASM_MPSUB
|
||||
# define ASM_MPMULTWO
|
||||
# define ASM_MPSETMUL
|
||||
# define ASM_MPADDMUL
|
||||
# define ASM_MPADDSQRTRC
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -30,12 +30,12 @@
|
|||
|
||||
#include "mpbarrett.h"
|
||||
|
||||
#define SMALL_PRIMES_PRODUCT_MAX 64
|
||||
#define SMALL_PRIMES_PRODUCT_MAX 32
|
||||
|
||||
/**
|
||||
*/
|
||||
/*@-exportlocal@*/
|
||||
extern uint32* mp32spprod[SMALL_PRIMES_PRODUCT_MAX];
|
||||
extern mpw* mpspprod[SMALL_PRIMES_PRODUCT_MAX];
|
||||
/*@=exportlocal@*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -45,42 +45,42 @@ extern "C" {
|
|||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mp32ptrials (uint32 bits)
|
||||
int mpptrials (size_t bits)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int mp32pmilrab_w (const mpbarrett* p, randomGeneratorContext* rc, int t, /*@out@*/ uint32* wksp)
|
||||
int mppmilrab_w (const mpbarrett* p, randomGeneratorContext* rc, int t, /*@out@*/ mpw* wksp)
|
||||
/*@modifies wksp @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mp32prnd_w (mpbarrett* p, randomGeneratorContext* rc, uint32 size, int t, /*@null@*/ const mpnumber* f, /*@out@*/ uint32* wksp)
|
||||
/*@globals mp32spprod @*/
|
||||
void mpprnd_w (mpbarrett* p, randomGeneratorContext* rc, size_t size, int t, /*@null@*/ const mpnumber* f, /*@out@*/ mpw* wksp)
|
||||
/*@globals mpspprod @*/
|
||||
/*@modifies p, rc, wksp @*/;
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mp32prndsafe_w (mpbarrett* p, randomGeneratorContext* rc, uint32 size, int t, /*@out@*/ uint32* wksp)
|
||||
/*@globals mp32spprod @*/
|
||||
void mpprndsafe_w (mpbarrett* p, randomGeneratorContext* rc, size_t size, int t, /*@out@*/ mpw* wksp)
|
||||
/*@globals mpspprod @*/
|
||||
/*@modifies p, rc, wksp @*/;
|
||||
|
||||
#ifdef NOTYET
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
void mp32prndcon_w (mpbarrett* p, randomGeneratorContext* rc, uint32, int, const mpnumber*, const mpnumber*, const mpnumber*, mpnumber*, /*@out@*/ uint32* wksp)
|
||||
void mpprndcon_w (mpbarrett* p, randomGeneratorContext* rc, size_t, int, const mpnumber*, const mpnumber*, const mpnumber*, mpnumber*, /*@out@*/ mpw* wksp)
|
||||
/*@modifies wksp @*/;
|
||||
#endif
|
||||
|
||||
/**
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
void mp32prndconone_w(mpbarrett* p, randomGeneratorContext* rc, uint32 size, int t, const mpbarrett* q, /*@null@*/ const mpnumber* f, mpnumber* r, int cofactor, /*@out@*/ uint32* wksp)
|
||||
/*@globals mp32spprod @*/
|
||||
void mpprndconone_w(mpbarrett* p, randomGeneratorContext* rc, size_t size, int t, const mpbarrett* q, /*@null@*/ const mpnumber* f, mpnumber* r, int cofactor, /*@out@*/ mpw* wksp)
|
||||
/*@globals mpspprod @*/
|
||||
/*@modifies p, rc, r, wksp @*/;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -131,11 +131,11 @@ int mtprngSeed(mtprngParam* mp, const uint32* data, int size)
|
|||
#endif
|
||||
while (size < needed)
|
||||
{
|
||||
mp32copy(size, dest, data);
|
||||
mpcopy(size, dest, data);
|
||||
dest += size;
|
||||
needed -= size;
|
||||
}
|
||||
mp32copy(needed, dest, data);
|
||||
mpcopy(needed, dest, data);
|
||||
#ifdef _REENTRANT
|
||||
# if WIN32
|
||||
if (!ReleaseMutex(mp->lock))
|
||||
|
|
|
@ -66,7 +66,8 @@ typedef struct
|
|||
#endif
|
||||
uint32 state[N+1];
|
||||
uint32 left;
|
||||
/*@kept@*/ uint32* nextw;
|
||||
/*@kept@*/
|
||||
uint32* nextw;
|
||||
} mtprngParam;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
118
beecrypt/rsa.c
118
beecrypt/rsa.c
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup RSA_m
|
||||
* \file rsa.c
|
||||
*
|
||||
* RSA Encryption & signature scheme, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,20 +17,53 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file rsa.c
|
||||
* \brief RSA algorithm.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup IF_m IF_rsa_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "rsa.h"
|
||||
#include "mp.h"
|
||||
#include "debug.h"
|
||||
|
||||
int rsapri(const rsakp* kp, const mpnumber* m, mpnumber* c)
|
||||
int rsapub(const rsapk* pk, const mpnumber* m, mpnumber* c)
|
||||
{
|
||||
register uint32 size = kp->n.size;
|
||||
register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(*temp));
|
||||
register size_t size = pk->n.size;
|
||||
register mpw* temp;
|
||||
|
||||
if (mpgex(m->size, m->data, pk->n.size, pk->n.modl))
|
||||
return -1;
|
||||
|
||||
temp = (mpw*) malloc((4*size+2)*sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
mpnsize(c, size);
|
||||
mpbpowmod_w(&kp->n, m->size, m->data, kp->d.size, kp->d.data, c->data, temp);
|
||||
|
||||
mpbpowmod_w(&pk->n, m->size, m->data, pk->e.size, pk->e.data, c->data, temp);
|
||||
free(temp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rsapri(const rsakp* kp, const mpnumber* c, mpnumber* m)
|
||||
{
|
||||
register size_t size = kp->n.size;
|
||||
register mpw* temp;
|
||||
|
||||
if (mpgex(c->size, c->data, kp->n.size, kp->n.modl))
|
||||
return -1;
|
||||
|
||||
temp = (mpw*) malloc((4*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
mpnsize(m, size);
|
||||
mpbpowmod_w(&kp->n, c->size, c->data, kp->d.size, kp->d.data, m->data, temp);
|
||||
|
||||
free(temp);
|
||||
|
||||
|
@ -47,43 +72,47 @@ int rsapri(const rsakp* kp, const mpnumber* m, mpnumber* c)
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int rsapricrt(const rsakp* kp, const mpnumber* m, mpnumber* c)
|
||||
{
|
||||
register uint32 nsize = kp->n.size;
|
||||
register uint32 psize = kp->p.size;
|
||||
register uint32 qsize = kp->q.size;
|
||||
register size_t nsize = kp->n.size;
|
||||
register size_t psize = kp->p.size;
|
||||
register size_t qsize = kp->q.size;
|
||||
|
||||
register uint32* ptemp;
|
||||
register uint32* qtemp;
|
||||
register mpw* ptemp;
|
||||
register mpw* qtemp;
|
||||
|
||||
/* m must be small enough to be exponentiated modulo p and q */
|
||||
if (m->size > psize || m->size > qsize)
|
||||
if (mpgex(c->size, c->data, kp->n.size, kp->n.modl))
|
||||
return -1;
|
||||
|
||||
ptemp = (uint32*) malloc((6*psize+2)*sizeof(*ptemp));
|
||||
if (ptemp == NULL)
|
||||
ptemp = (mpw*) malloc((6*psize+2)*sizeof(*ptemp));
|
||||
if (ptemp == (mpw*) 0)
|
||||
return -1;
|
||||
|
||||
qtemp = (uint32*) malloc((6*qsize+2)*sizeof(*qtemp));
|
||||
if (qtemp == NULL)
|
||||
qtemp = (mpw*) malloc((6*qsize+2)*sizeof(*qtemp));
|
||||
if (qtemp == (mpw*) 0)
|
||||
{
|
||||
free(ptemp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* resize m for powmod p */
|
||||
mp32setx(psize, ptemp+psize, m->size, m->data);
|
||||
/* resize c for powmod p */
|
||||
mpsetx(psize, ptemp+psize, c->size, c->data);
|
||||
|
||||
/* compute j1 = m^d1 mod p, store @ ptemp */
|
||||
/* reduce modulo p before we powmod */
|
||||
mpbmod_w(&kp->p, ptemp, ptemp+psize, ptemp+2*psize);
|
||||
|
||||
/* compute j1 = c^d1 mod p, store @ ptemp */
|
||||
/*@-compdef@*/
|
||||
mpbpowmod_w(&kp->p, psize, ptemp+psize, kp->d1.size, kp->d1.data, ptemp, ptemp+2*psize);
|
||||
|
||||
/* resize m for powmod p */
|
||||
mp32setx(qsize, qtemp+psize, m->size, m->data);
|
||||
/* resize c for powmod p */
|
||||
mpsetx(qsize, qtemp+psize, c->size, c->data);
|
||||
|
||||
/* compute j2 = m^d2 mod q, store @ qtemp */
|
||||
mpbpowmod_w(&kp->q, qsize, qtemp+psize, kp->d2.size, kp->d2.data, qtemp, qtemp+2*qsize);
|
||||
/* reduce modulo q before we powmod */
|
||||
mpbmod_w(&kp->q, qtemp, qtemp+qsize, qtemp+2*qsize);
|
||||
|
||||
/* compute j2 = c^d2 mod q, store @ qtemp */
|
||||
mpbpowmod_w(&kp->q, qsize, qtemp+qsize, kp->d2.size, kp->d2.data, qtemp, qtemp+2*qsize);
|
||||
/*@=compdef@*/
|
||||
|
||||
/* compute j1-j2 mod p, store @ ptemp */
|
||||
|
@ -92,12 +121,12 @@ int rsapricrt(const rsakp* kp, const mpnumber* m, mpnumber* c)
|
|||
/* compute h = c*(j1-j2) mod p, store @ ptemp */
|
||||
mpbmulmod_w(&kp->p, psize, ptemp, psize, kp->c.data, ptemp, ptemp+2*psize);
|
||||
|
||||
/* make sure the signature gets the proper size */
|
||||
mpnsize(c, nsize);
|
||||
/* make sure the message gets the proper size */
|
||||
mpnsize(m, nsize);
|
||||
|
||||
/* compute s = h*q + j2 */
|
||||
mp32mul(c->data, psize, ptemp, qsize, kp->q.modl);
|
||||
(void) mp32addx(nsize, c->data, qsize, qtemp);
|
||||
/* compute m = h*q + j2 */
|
||||
mpmul(m->data, psize, ptemp, qsize, kp->q.modl);
|
||||
(void) mpaddx(nsize, m->data, qsize, qtemp);
|
||||
|
||||
free(ptemp);
|
||||
free(qtemp);
|
||||
|
@ -105,20 +134,25 @@ int rsapricrt(const rsakp* kp, const mpnumber* m, mpnumber* c)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 1 if signature verifies, 0 otherwise (can also indicate errors)
|
||||
*/
|
||||
int rsavrfy(const rsapk* pk, const mpnumber* m, const mpnumber* c)
|
||||
{
|
||||
int rc;
|
||||
register uint32 size = pk->n.size;
|
||||
register uint32* temp = (uint32*) malloc((5*size+2) * sizeof(*temp));
|
||||
register size_t size = pk->n.size;
|
||||
register mpw* temp;
|
||||
|
||||
if (mpgex(m->size, m->data, pk->n.size, pk->n.modl))
|
||||
return -1;
|
||||
|
||||
if (mpgex(c->size, c->data, pk->n.size, pk->n.modl))
|
||||
return 0;
|
||||
|
||||
temp = (uint32*) malloc((5*size+2) * sizeof(*temp));
|
||||
|
||||
if (temp)
|
||||
{
|
||||
mpbpowmod_w(&pk->n, c->size, c->data, pk->e.size, pk->e.data, temp, temp+size);
|
||||
|
||||
rc = mp32eqx(size, temp, m->size, m->data);
|
||||
rc = mpeqx(size, temp, m->size, m->data);
|
||||
|
||||
free(temp);
|
||||
|
||||
|
|
|
@ -35,18 +35,67 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
/**
|
||||
* The raw RSA public key operation.
|
||||
*
|
||||
* This function can be used for encryption and verifying.
|
||||
*
|
||||
* It performs the following operation:
|
||||
* \li \f$c=m^{e}\ \textrm{mod}\ n\f$
|
||||
*
|
||||
* @param pk RSA public key
|
||||
* @param m message
|
||||
* @param c ciphertext
|
||||
* @retval 0 on success, -1 on failure
|
||||
*/
|
||||
int rsapub(const rsapk* pk, const mpnumber* m, mpnumber* c)
|
||||
/*@*/;
|
||||
|
||||
/**
|
||||
* The raw RSA private key operation.
|
||||
*
|
||||
* This function can be used for decryption and signing.
|
||||
*
|
||||
* It performs the operation:
|
||||
* \li \f$m=c^{d}\ \textrm{mod}\ n\f$
|
||||
*
|
||||
* @param kp RSA keypair
|
||||
* @param c ciphertext
|
||||
* @param m message
|
||||
* @retval 0 on success, -1 on failure
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int rsapri (const rsakp* kp, const mpnumber* m, mpnumber* c)
|
||||
int rsapri (const rsakp* kp, const mpnumber* c, mpnumber* m)
|
||||
/*@modifies c */;
|
||||
|
||||
/**
|
||||
* The raw RSA private key operation, with Chinese Remainder Theorem.
|
||||
*
|
||||
* It performs the operation:
|
||||
* \li \f$j_1=c^{d_1}\ \textrm{mod}\ p\f$
|
||||
* \li \f$j_2=c^{d_2}\ \textrm{mod}\ q\f$
|
||||
* \li \f$h=c \cdot (j_1-j_2)\ \textrm{mod}\ p\f$
|
||||
* \li \f$m=j_2+hq\f$
|
||||
*
|
||||
* @param kp RSA keypair
|
||||
* @param c ciphertext
|
||||
* @param m message
|
||||
* @retval 0 on success, -1 on failure.
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int rsapricrt(const rsakp* kp, const mpnumber* m, mpnumber* c)
|
||||
int rsapricrt(const rsakp* kp, const mpnumber* c, mpnumber* m)
|
||||
/*@modifies c */;
|
||||
|
||||
/**
|
||||
* Verify if ciphertext \e c was encrypted from cleartext \e m
|
||||
* with the private key matching the given public key \e pk.
|
||||
*
|
||||
* @warning The return type of this function should be a boolean, but since
|
||||
* that type isn't as portable, an int is used.
|
||||
*
|
||||
* @param pk RSA public key
|
||||
* @param m cleartext message
|
||||
* @param c ciphertext message
|
||||
* @retval 1 on success, 0 on failure
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int rsavrfy (const rsapk* pk, const mpnumber* m, const mpnumber* c)
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/** \ingroup RSA_m
|
||||
* \file rsakp.c
|
||||
*
|
||||
* RSA Keypair, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* <conformance statement for IEEE P1363 needed here>
|
||||
*
|
||||
* Copyright (c) 2000, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -27,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file rsakp.c
|
||||
* \brief RSA keypair.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup IF_m IF_rsa_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "rsakp.h"
|
||||
#include "mpprime.h"
|
||||
|
@ -40,9 +36,9 @@ int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
|
|||
* Generates an RSA Keypair for use with the Chinese Remainder Theorem
|
||||
*/
|
||||
|
||||
register uint32 pqsize = ((uint32)(nsize+1)) >> 1;
|
||||
register uint32* temp = (uint32*) malloc((16*pqsize+6) * sizeof(*temp));
|
||||
register uint32 newn = 1;
|
||||
register size_t pqsize = ((uint32)(nsize+1)) >> 1;
|
||||
register mpw* temp = (mpw*) malloc((16*pqsize+6) * sizeof(*temp));
|
||||
register int newn = 1;
|
||||
|
||||
if (temp)
|
||||
{
|
||||
|
@ -55,12 +51,12 @@ int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
|
|||
|
||||
/* generate a random prime p and q */
|
||||
/*@-globs@*/
|
||||
mp32prnd_w(&kp->p, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
|
||||
mp32prnd_w(&kp->q, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
|
||||
mpprnd_w(&kp->p, rgc, pqsize, mpptrials(MP_WORDS_TO_BITS(pqsize)), &kp->e, temp);
|
||||
mpprnd_w(&kp->q, rgc, pqsize, mpptrials(MP_WORDS_TO_BITS(pqsize)), &kp->e, temp);
|
||||
/*@-globs@*/
|
||||
|
||||
/* if p <= q, perform a swap to make p larger than q */
|
||||
if (mp32le(pqsize, kp->p.modl, kp->q.modl))
|
||||
if (mple(pqsize, kp->p.modl, kp->q.modl))
|
||||
{
|
||||
/*@-sizeoftype@*/
|
||||
memcpy(&r, &kp->q, sizeof(r));
|
||||
|
@ -76,19 +72,19 @@ int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
|
|||
|
||||
while (1)
|
||||
{
|
||||
mp32mul(temp, pqsize, kp->p.modl, pqsize, kp->q.modl);
|
||||
mpmul(temp, pqsize, kp->p.modl, pqsize, kp->q.modl);
|
||||
|
||||
if (newn && mp32msbset(nsize, temp))
|
||||
if (newn && mpmsbset(nsize, temp))
|
||||
break;
|
||||
|
||||
/* product of p and q doesn't have the required size (one bit short) */
|
||||
|
||||
/*@-globs@*/
|
||||
mp32prnd_w(&r, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
|
||||
mpprnd_w(&r, rgc, pqsize, mpptrials(MP_WORDS_TO_BITS(pqsize)), &kp->e, temp);
|
||||
/*@=globs@*/
|
||||
|
||||
/*@-usedef -branchstate @*/ /* r is set */
|
||||
if (mp32le(pqsize, kp->p.modl, r.modl))
|
||||
if (mple(pqsize, kp->p.modl, r.modl))
|
||||
{
|
||||
mpbfree(&kp->q);
|
||||
/*@-sizeoftype@*/
|
||||
|
@ -98,7 +94,7 @@ int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
|
|||
mpbzero(&r);
|
||||
newn = 1;
|
||||
}
|
||||
else if (mp32le(pqsize, kp->q.modl, r.modl))
|
||||
else if (mple(pqsize, kp->q.modl, r.modl))
|
||||
{
|
||||
mpbfree(&kp->q);
|
||||
/*@-sizeoftype@*/
|
||||
|
@ -127,7 +123,7 @@ int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
|
|||
|
||||
/*@-usedef@*/ /* psubone/qsubone are set */
|
||||
/* compute phi = (p-1)*(q-1) */
|
||||
mp32mul(temp, pqsize, psubone.modl, pqsize, qsubone.modl);
|
||||
mpmul(temp, pqsize, psubone.modl, pqsize, qsubone.modl);
|
||||
mpbset(&phi, nsize, temp);
|
||||
|
||||
/* compute d = inv(e) mod phi */
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/** \ingroup RSA_m
|
||||
* \file rsapk.c
|
||||
*
|
||||
* RSA Public Key, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* <conformance statement for IEEE P1363 needed here>
|
||||
*
|
||||
* Copyright (c) 2000, 2001, 2002 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -27,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file rsapk.c
|
||||
* \brief RSA public key.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup IF_m IF_rsa_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "rsapk.h"
|
||||
#include "debug.h"
|
||||
|
|
126
beecrypt/sha1.c
126
beecrypt/sha1.c
|
@ -1,17 +1,6 @@
|
|||
/** \ingroup HASH_sha1_m HASH_m
|
||||
* \file sha1.c
|
||||
*
|
||||
* SHA-1 hash function, code.
|
||||
*
|
||||
* For more information on this algorithm, see:
|
||||
* NIST FIPS PUB 180-1
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -28,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file sha1.c
|
||||
* \brief SHA-1 hash function, as specified by NIST FIPS 180-1.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HASH_m HASH_sha1_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "beecrypt.h"
|
||||
#include "sha1opt.h"
|
||||
|
@ -39,23 +34,29 @@
|
|||
/** \ingroup HASH_sha1_m
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const uint32 k[4] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
|
||||
static const uint32_t k[4] = { 0x5a827999U, 0x6ed9eba1U, 0x8f1bbcdcU, 0xca62c1d6U };
|
||||
|
||||
/** \ingroup HASH_sha1_m
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const uint32 hinit[5] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
|
||||
static const uint32_t hinit[5] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
|
||||
|
||||
/*@-sizeoftype@*/
|
||||
const hashFunction sha1 = { "SHA-1", sizeof(sha1Param), 64, 5 * sizeof(uint32), (hashFunctionReset) sha1Reset, (hashFunctionUpdate) sha1Update, (hashFunctionDigest) sha1Digest };
|
||||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int sha1Reset(register sha1Param *p)
|
||||
int sha1Reset(register sha1Param* p)
|
||||
{
|
||||
mp32copy(5, p->h, hinit);
|
||||
mp32zero(80, p->data);
|
||||
p->length = 0;
|
||||
memcpy(p->h, hinit, sizeof(p->h));
|
||||
memset(p->data, 0, sizeof(p->data));
|
||||
#if (MP_WBITS == 64)
|
||||
mpzero(1, p->length);
|
||||
#elif (MP_WBITS == 32)
|
||||
mpzero(2, p->length);
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
p->offset = 0;
|
||||
return 0;
|
||||
}
|
||||
|
@ -76,12 +77,12 @@ int sha1Reset(register sha1Param *p)
|
|||
|
||||
#ifndef ASM_SHA1PROCESS
|
||||
/*@-boundsread@*/
|
||||
void sha1Process(register sha1Param *p)
|
||||
void sha1Process(register sha1Param* p)
|
||||
{
|
||||
register uint32 a, b, c, d, e;
|
||||
register uint32 *w;
|
||||
register uint32_t a, b, c, d, e;
|
||||
register uint32_t *w;
|
||||
register byte t;
|
||||
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
w = p->data + 16;
|
||||
#else
|
||||
|
@ -89,7 +90,7 @@ void sha1Process(register sha1Param *p)
|
|||
t = 16;
|
||||
while (t--)
|
||||
{
|
||||
register uint32 temp = swapu32(*w);
|
||||
register uint32_t temp = swapu32(*w);
|
||||
*(w++) = temp;
|
||||
}
|
||||
#endif
|
||||
|
@ -97,7 +98,7 @@ void sha1Process(register sha1Param *p)
|
|||
t = 64;
|
||||
while (t--)
|
||||
{
|
||||
register uint32 temp = w[-3] ^ w[-8] ^ w[-14] ^ w[-16];
|
||||
register uint32_t temp = w[-3] ^ w[-8] ^ w[-14] ^ w[-16];
|
||||
*(w++) = ROTL32(temp, 1);
|
||||
}
|
||||
|
||||
|
@ -199,11 +200,24 @@ void sha1Process(register sha1Param *p)
|
|||
#endif
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int sha1Update(register sha1Param *p, const byte *data, int size)
|
||||
int sha1Update(register sha1Param *p, const byte* data, size_t size)
|
||||
{
|
||||
register int proclength;
|
||||
|
||||
p->length += size;
|
||||
#if (MP_WBITS == 64)
|
||||
mpw add[1];
|
||||
mpsetw(1, add, size);
|
||||
mplshift(1, add, 3);
|
||||
mpadd(1, p->length, add);
|
||||
#elif (MP_WBITS == 32)
|
||||
mpw add[2];
|
||||
mpsetw(2, add, size);
|
||||
mplshift(2, add, 3);
|
||||
mpadd(2, p->length, add);
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
|
||||
while (size > 0)
|
||||
{
|
||||
proclength = ((p->offset + size) > 64) ? (64 - p->offset) : size;
|
||||
|
@ -225,7 +239,7 @@ int sha1Update(register sha1Param *p, const byte *data, int size)
|
|||
/** \ingroup HASH_sha1_m
|
||||
*/
|
||||
/*@-boundswrite@*/
|
||||
static void sha1Finish(register sha1Param *p)
|
||||
static void sha1Finish(register sha1Param* p)
|
||||
/*@modifies p @*/
|
||||
{
|
||||
register byte *ptr = ((byte *) p->data) + p->offset++;
|
||||
|
@ -241,29 +255,75 @@ static void sha1Finish(register sha1Param *p)
|
|||
p->offset = 0;
|
||||
}
|
||||
|
||||
ptr = ((byte *) p->data) + p->offset;
|
||||
ptr = ((byte*) p->data) + p->offset;
|
||||
while (p->offset++ < 56)
|
||||
*(ptr++) = 0;
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
p->data[14] = ((uint32)(p->length >> 29));
|
||||
p->data[15] = ((uint32)((p->length << 3) & 0xffffffff));
|
||||
memcpy(ptr, p->length, sizeof(p->length));
|
||||
#else
|
||||
p->data[14] = swapu32((uint32)(p->length >> 29));
|
||||
p->data[15] = swapu32((uint32)((p->length << 3) & 0xffffffff));
|
||||
# if (MP_WBITS == 64)
|
||||
ptr[0] = (byte)(p->length[0] >> 56);
|
||||
ptr[1] = (byte)(p->length[0] >> 48);
|
||||
ptr[2] = (byte)(p->length[0] >> 40);
|
||||
ptr[3] = (byte)(p->length[0] >> 32);
|
||||
ptr[4] = (byte)(p->length[0] >> 24);
|
||||
ptr[5] = (byte)(p->length[0] >> 16);
|
||||
ptr[6] = (byte)(p->length[0] >> 8);
|
||||
ptr[7] = (byte)(p->length[0] );
|
||||
#elif (MP_WBITS == 32)
|
||||
ptr[0] = (byte)(p->length[0] >> 24);
|
||||
ptr[1] = (byte)(p->length[0] >> 16);
|
||||
ptr[2] = (byte)(p->length[0] >> 8);
|
||||
ptr[3] = (byte)(p->length[0] );
|
||||
ptr[4] = (byte)(p->length[1] >> 24);
|
||||
ptr[5] = (byte)(p->length[1] >> 16);
|
||||
ptr[6] = (byte)(p->length[1] >> 8);
|
||||
ptr[7] = (byte)(p->length[1] );
|
||||
# else
|
||||
# error
|
||||
# endif
|
||||
#endif
|
||||
|
||||
sha1Process(p);
|
||||
|
||||
p->offset = 0;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int sha1Digest(register sha1Param *p, uint32 *data)
|
||||
int sha1Digest(register sha1Param* p, byte* data)
|
||||
{
|
||||
sha1Finish(p);
|
||||
mp32copy(5, data, p->h);
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
memcpy(data, p->h, sizeof(p->h));
|
||||
#else
|
||||
/* encode 5 integers big-endian style */
|
||||
data[ 0] = (byte)(p->h[0] >> 24);
|
||||
data[ 1] = (byte)(p->h[0] >> 16);
|
||||
data[ 2] = (byte)(p->h[0] >> 8);
|
||||
data[ 3] = (byte)(p->h[0] >> 0);
|
||||
data[ 4] = (byte)(p->h[1] >> 24);
|
||||
data[ 5] = (byte)(p->h[1] >> 16);
|
||||
data[ 6] = (byte)(p->h[1] >> 8);
|
||||
data[ 7] = (byte)(p->h[1] >> 0);
|
||||
data[ 8] = (byte)(p->h[2] >> 24);
|
||||
data[ 9] = (byte)(p->h[2] >> 16);
|
||||
data[10] = (byte)(p->h[2] >> 8);
|
||||
data[11] = (byte)(p->h[2] >> 0);
|
||||
data[12] = (byte)(p->h[3] >> 24);
|
||||
data[13] = (byte)(p->h[3] >> 16);
|
||||
data[14] = (byte)(p->h[3] >> 8);
|
||||
data[15] = (byte)(p->h[3] >> 0);
|
||||
data[16] = (byte)(p->h[4] >> 24);
|
||||
data[17] = (byte)(p->h[4] >> 16);
|
||||
data[18] = (byte)(p->h[4] >> 8);
|
||||
data[19] = (byte)(p->h[4] >> 0);
|
||||
#endif
|
||||
|
||||
(void) sha1Reset(p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*@=boundswrite@*/
|
||||
|
|
|
@ -34,10 +34,16 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32 h[5];
|
||||
uint32 data[80];
|
||||
uint64 length;
|
||||
uint8 offset;
|
||||
uint32_t h[5];
|
||||
uint32_t data[80];
|
||||
#if (MP_WBITS == 64)
|
||||
mpw length[1];
|
||||
#elif (MP_WBITS == 32)
|
||||
mpw length[2];
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
short offset;
|
||||
} sha1Param;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -64,13 +70,13 @@ int sha1Reset (sha1Param* p)
|
|||
/** \ingroup HASH_sha1_m
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int sha1Update (sha1Param* p, const byte* data, int size)
|
||||
int sha1Update (sha1Param* p, const byte* data, size_t size)
|
||||
/*@modifies p */;
|
||||
|
||||
/** \ingroup HASH_sha1_m
|
||||
*/
|
||||
BEECRYPTAPI /*@unused@*/
|
||||
int sha1Digest (sha1Param* p, /*@out@*/ uint32* data)
|
||||
int sha1Digest (sha1Param* p, /*@out@*/ byte* data)
|
||||
/*@modifies p, data */;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
/** \ingroup HASH_sha256_m HASH_m
|
||||
* \file sha256.c
|
||||
*
|
||||
* SHA-256 hash function, code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000, 2001 Virtual Unlimited B.V.
|
||||
*
|
||||
* Author: Bob Deblier <bob@virtualunlimited.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -25,6 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/*!\file sha256.c
|
||||
* \brief SHA-256 hash function, as specified by NIST DFIPS 180-2.
|
||||
* \author Bob Deblier <bob.deblier@pandora.be>
|
||||
* \ingroup HASH_m HASH_sha256_m
|
||||
*/
|
||||
|
||||
#include "system.h"
|
||||
#include "sha256.h"
|
||||
#include "mp.h"
|
||||
|
@ -34,22 +32,22 @@
|
|||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const uint32 k[64] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
static const uint32_t k[64] = {
|
||||
0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U,
|
||||
0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U,
|
||||
0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU,
|
||||
0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U,
|
||||
0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U,
|
||||
0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U,
|
||||
0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U,
|
||||
0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U
|
||||
};
|
||||
|
||||
/**
|
||||
*/
|
||||
/*@observer@*/ /*@unchecked@*/
|
||||
static const uint32 hinit[8] = {
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
||||
static const uint32_t hinit[8] = {
|
||||
0x6a09e667U, 0xbb67ae85U, 0x3c6ef372U, 0xa54ff53aU, 0x510e527fU, 0x9b05688cU, 0x1f83d9abU, 0x5be0cd19U
|
||||
};
|
||||
|
||||
/*@-sizeoftype@*/
|
||||
|
@ -57,11 +55,11 @@ const hashFunction sha256 = { "SHA-256", sizeof(sha256Param), 64, 8 * sizeof(uin
|
|||
/*@=sizeoftype@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int sha256Reset(register sha256Param *p)
|
||||
int sha256Reset(register sha256Param* p)
|
||||
{
|
||||
mp32copy(8, p->h, hinit);
|
||||
mp32zero(64, p->data);
|
||||
p->length = 0;
|
||||
memcpy(p->h, hinit, sizeof(p->h));
|
||||
memset(p->data, 0, sizeof(p->data));
|
||||
memset(&p->length, 0, sizeof(p->length));
|
||||
p->offset = 0;
|
||||
return 0;
|
||||
}
|
||||
|
@ -84,10 +82,10 @@ int sha256Reset(register sha256Param *p)
|
|||
|
||||
#ifndef ASM_SHA256PROCESS
|
||||
/*@-boundsread@*/
|
||||
void sha256Process(register sha256Param *p)
|
||||
void sha256Process(register sha256Param* p)
|
||||
{
|
||||
register uint32 a, b, c, d, e, f, g, h, temp;
|
||||
register uint32 *w;
|
||||
register uint32_t a, b, c, d, e, f, g, h, temp;
|
||||
register uint32_t *w;
|
||||
register byte t;
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
|
@ -96,18 +94,12 @@ void sha256Process(register sha256Param *p)
|
|||
w = p->data;
|
||||
t = 16;
|
||||
while (t--)
|
||||
{
|
||||
register uint32 ttemp = swapu32(*w);
|
||||
*(w++) = ttemp;
|
||||
}
|
||||
*(w++) = swapu32(*w);
|
||||
#endif
|
||||
|
||||
t = 48;
|
||||
while (t--)
|
||||
{
|
||||
register uint32 ttemp = sig1(w[-2]) + w[-7] + sig0(w[-15]) + w[-16];
|
||||
*(w++) = ttemp;
|
||||
}
|
||||
*(w++) = sig1(w[-2]) + w[-7] + sig0(w[-15]) + w[-16];
|
||||
|
||||
w = p->data;
|
||||
|
||||
|
@ -192,11 +184,24 @@ void sha256Process(register sha256Param *p)
|
|||
#endif
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int sha256Update(register sha256Param *p, const byte *data, int size)
|
||||
int sha256Update(register sha256Param* p, const byte* data, size_t size)
|
||||
{
|
||||
register int proclength;
|
||||
|
||||
p->length += size;
|
||||
#if (MP_WBITS == 64)
|
||||
mpw add[1];
|
||||
mpsetw(1, add, size);
|
||||
mplshift(1, add, 3);
|
||||
mpadd(1, p->length, add);
|
||||
#elif (MP_WBITS == 32)
|
||||
mpw add[2];
|
||||
mpsetw(2, add, size);
|
||||
mplshift(2, add, 3);
|
||||
mpadd(2, p->length, add);
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
|
||||
while (size > 0)
|
||||
{
|
||||
proclength = ((p->offset + size) > 64) ? (64 - p->offset) : size;
|
||||
|
@ -218,7 +223,7 @@ int sha256Update(register sha256Param *p, const byte *data, int size)
|
|||
/**
|
||||
*/
|
||||
/*@-boundswrite@*/
|
||||
static void sha256Finish(register sha256Param *p)
|
||||
static void sha256Finish(register sha256Param* p)
|
||||
/*@globals internalState @*/
|
||||
/*@modifies p, internalState @*/
|
||||
{
|
||||
|
@ -239,12 +244,26 @@ static void sha256Finish(register sha256Param *p)
|
|||
while (p->offset++ < 56)
|
||||
*(ptr++) = 0;
|
||||
|
||||
#if WORDS_BIGENDIAN
|
||||
p->data[14] = ((uint32)(p->length >> 29));
|
||||
p->data[15] = ((uint32)((p->length << 3) & 0xffffffff));
|
||||
#if (MP_WBITS == 64)
|
||||
ptr[0] = (byte)(p->length[0] >> 56);
|
||||
ptr[1] = (byte)(p->length[0] >> 48);
|
||||
ptr[2] = (byte)(p->length[0] >> 40);
|
||||
ptr[3] = (byte)(p->length[0] >> 32);
|
||||
ptr[4] = (byte)(p->length[0] >> 24);
|
||||
ptr[5] = (byte)(p->length[0] >> 16);
|
||||
ptr[6] = (byte)(p->length[0] >> 8);
|
||||
ptr[7] = (byte)(p->length[0] );
|
||||
#elif (MP_WBITS == 32)
|
||||
ptr[0] = (byte)(p->length[0] >> 24);
|
||||
ptr[1] = (byte)(p->length[0] >> 16);
|
||||
ptr[2] = (byte)(p->length[0] >> 8);
|
||||
ptr[3] = (byte)(p->length[0] );
|
||||
ptr[4] = (byte)(p->length[1] >> 24);
|
||||
ptr[5] = (byte)(p->length[1] >> 16);
|
||||
ptr[6] = (byte)(p->length[1] >> 8);
|
||||
ptr[7] = (byte)(p->length[1] );
|
||||
#else
|
||||
p->data[14] = swapu32((uint32)(p->length >> 29));
|
||||
p->data[15] = swapu32((uint32)((p->length << 3) & 0xffffffff));
|
||||
# error
|
||||
#endif
|
||||
|
||||
sha256Process(p);
|
||||
|
@ -253,10 +272,44 @@ static void sha256Finish(register sha256Param *p)
|
|||
/*@=boundswrite@*/
|
||||
|
||||
/*@-boundswrite@*/
|
||||
int sha256Digest(register sha256Param *p, uint32 *data)
|
||||
int sha256Digest(register sha256Param* p, byte* data)
|
||||
{
|
||||
sha256Finish(p);
|
||||
mp32copy(8, data, p->h);
|
||||
|
||||
/* encode 8 integers big-endian style */
|
||||
data[ 0] = (byte)(p->h[0] >> 24);
|
||||
data[ 1] = (byte)(p->h[0] >> 16);
|
||||
data[ 2] = (byte)(p->h[0] >> 8);
|
||||
data[ 3] = (byte)(p->h[0] >> 0);
|
||||
data[ 4] = (byte)(p->h[1] >> 24);
|
||||
data[ 5] = (byte)(p->h[1] >> 16);
|
||||
data[ 6] = (byte)(p->h[1] >> 8);
|
||||
data[ 7] = (byte)(p->h[1] >> 0);
|
||||
data[ 8] = (byte)(p->h[2] >> 24);
|
||||
data[ 9] = (byte)(p->h[2] >> 16);
|
||||
data[10] = (byte)(p->h[2] >> 8);
|
||||
data[11] = (byte)(p->h[2] >> 0);
|
||||
data[12] = (byte)(p->h[3] >> 24);
|
||||
data[13] = (byte)(p->h[3] >> 16);
|
||||
data[14] = (byte)(p->h[3] >> 8);
|
||||
data[15] = (byte)(p->h[3] >> 0);
|
||||
data[16] = (byte)(p->h[4] >> 24);
|
||||
data[17] = (byte)(p->h[4] >> 16);
|
||||
data[18] = (byte)(p->h[4] >> 8);
|
||||
data[19] = (byte)(p->h[4] >> 0);
|
||||
data[20] = (byte)(p->h[5] >> 24);
|
||||
data[21] = (byte)(p->h[5] >> 16);
|
||||
data[22] = (byte)(p->h[5] >> 8);
|
||||
data[23] = (byte)(p->h[5] >> 0);
|
||||
data[24] = (byte)(p->h[6] >> 24);
|
||||
data[25] = (byte)(p->h[6] >> 16);
|
||||
data[26] = (byte)(p->h[6] >> 8);
|
||||
data[27] = (byte)(p->h[6] >> 0);
|
||||
data[28] = (byte)(p->h[7] >> 24);
|
||||
data[29] = (byte)(p->h[7] >> 16);
|
||||
data[30] = (byte)(p->h[7] >> 8);
|
||||
data[31] = (byte)(p->h[7] >> 0);
|
||||
|
||||
(void) sha256Reset(p);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -34,10 +34,16 @@
|
|||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32 h[8];
|
||||
uint32 data[64];
|
||||
uint64 length;
|
||||
uint8 offset;
|
||||
uint32_t h[8];
|
||||
uint32_t data[64];
|
||||
#if (MP_WBITS == 64)
|
||||
mpw length[1];
|
||||
#elif (MP_WBITS == 32)
|
||||
mpw length[2];
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
short offset;
|
||||
} sha256Param;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -66,14 +72,14 @@ int sha256Reset (sha256Param* p)
|
|||
/** \ingroup HASH_sha256_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int sha256Update (sha256Param* p, const byte* data, int size)
|
||||
int sha256Update (sha256Param* p, const byte* data, size_t size)
|
||||
/*@globals internalState @*/
|
||||
/*@modifies p, internalState @*/;
|
||||
|
||||
/** \ingroup HASH_sha256_m
|
||||
*/
|
||||
BEECRYPTAPI
|
||||
int sha256Digest (sha256Param* p, /*@out@*/ uint32* data)
|
||||
int sha256Digest (sha256Param* p, /*@out@*/ byte* data)
|
||||
/*@globals internalState @*/
|
||||
/*@modifies p, data, internalState @*/;
|
||||
/*@=exportlocal@*/
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
#include "rsa.h"
|
||||
#include "sha1.h"
|
||||
#include "sha256.h"
|
||||
#include "mp32.h"
|
||||
#include "mp.h"
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
|
@ -79,7 +79,7 @@ static int testVectorInvMod(const dlkp_p* keypair)
|
|||
|
||||
mpbmulmod_w(&keypair->param.n, size, temp, size, temp+size, temp, temp+2*size);
|
||||
|
||||
rc = mp32isone(size, temp);
|
||||
rc = mpisone(size, temp);
|
||||
|
||||
free(temp);
|
||||
/*@=nullpass =nullptrarith @*/
|
||||
|
@ -104,7 +104,7 @@ static int testVectorExpMod(const dlkp_p* keypair)
|
|||
|
||||
mpbnpowmod(&keypair->param.p, &keypair->param.g, &keypair->x, &y);
|
||||
|
||||
rc = mp32eqx(y.size, y.data, keypair->y.size, keypair->y.data);
|
||||
rc = mpeqx(y.size, y.data, keypair->y.size, keypair->y.data);
|
||||
|
||||
mpnfree(&y);
|
||||
|
||||
|
@ -364,7 +364,7 @@ static int testVectorDLDP(void)
|
|||
|
||||
/* we have the parameters, now see if g^q == 1 */
|
||||
mpbnpowmod(&dp.p, &dp.g, (mpnumber*) &dp.q, &gq);
|
||||
result = mp32isone(gq.size, gq.data);
|
||||
result = mpisone(gq.size, gq.data);
|
||||
|
||||
mpnfree(&gq);
|
||||
(void) dldp_pFree(&dp);
|
||||
|
@ -392,7 +392,7 @@ static int testVectorMD5(void)
|
|||
(void) md5Update(¶m, (const unsigned char*) "abc", 3);
|
||||
(void) md5Digest(¶m, digest);
|
||||
|
||||
return mp32eq(4, expect, digest);
|
||||
return mpeq(4, expect, digest);
|
||||
}
|
||||
|
||||
/*@unused@*/
|
||||
|
@ -409,7 +409,7 @@ static int testVectorSHA1(void)
|
|||
(void) sha1Update(¶m, (const unsigned char*) "abc", 3);
|
||||
(void) sha1Digest(¶m, digest);
|
||||
|
||||
return mp32eq(5, expect, digest);
|
||||
return mpeq(5, expect, digest);
|
||||
}
|
||||
|
||||
/*@unused@*/ static int testVectorSHA256(void)
|
||||
|
@ -427,7 +427,7 @@ static int testVectorSHA1(void)
|
|||
(void) sha256Digest(¶m, digest);
|
||||
/*@=internalglobs =mods @*/
|
||||
|
||||
return mp32eq(8, expect, digest);
|
||||
return mpeq(8, expect, digest);
|
||||
}
|
||||
|
||||
/*@unchecked@*/
|
||||
|
|
|
@ -48,7 +48,7 @@ int main()
|
|||
|
||||
/* we have the parameters, now see if g^q == 1 */
|
||||
mpbnpowmod(¶ms.p, ¶ms.g, (mpnumber*) ¶ms.q, &gq);
|
||||
if (mp32isone(gq.size, gq.data))
|
||||
if (mpisone(gq.size, gq.data))
|
||||
printf("ok\n");
|
||||
else
|
||||
failures++;
|
||||
|
|
|
@ -59,7 +59,7 @@ int fake_next(randomGeneratorParam* p, uint32* data, int size)
|
|||
mpnzero(&tmp);
|
||||
mpnsethex(&tmp, dsa_k);
|
||||
|
||||
mp32setx(size, data, tmp.size, tmp.data);
|
||||
mpsetx(size, data, tmp.size, tmp.data);
|
||||
|
||||
mpnfree(&tmp);
|
||||
|
||||
|
@ -104,7 +104,7 @@ int main()
|
|||
if (dsasign(&keypair.param.p, &keypair.param.q, &keypair.param.g, &rngc, &hm, &keypair.x, &r, &s))
|
||||
return -1;
|
||||
|
||||
if (mp32eqx(5, expect_r, r.size, r.data) && mp32eqx(5, expect_s, s.size, s.data))
|
||||
if (mpeqx(5, expect_r, r.size, r.data) && mpeqx(5, expect_s, s.size, s.data))
|
||||
printf("ok\n");
|
||||
else
|
||||
failures++;
|
||||
|
|
|
@ -70,7 +70,7 @@ int main()
|
|||
if (hmacmd5Digest(¶m, digest))
|
||||
return -1;
|
||||
|
||||
if (mp32ne(4, digest, table[i].expect))
|
||||
if (mpne(4, digest, table[i].expect))
|
||||
{
|
||||
printf("failed\n");
|
||||
failures++;
|
||||
|
|
|
@ -69,11 +69,11 @@ int main()
|
|||
if (hmacsha1Digest(¶m, digest))
|
||||
return -1;
|
||||
|
||||
if (mp32ne(5, digest, table[i].expect))
|
||||
if (mpne(5, digest, table[i].expect))
|
||||
{
|
||||
printf("failed\n");
|
||||
mp32println(5, table[i].expect);
|
||||
mp32println(5, digest);
|
||||
mpprintln(stdout, 5, table[i].expect);
|
||||
mpprintln(stdout, 5, digest);
|
||||
failures++;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -64,7 +64,7 @@ int main()
|
|||
if (md5Digest(¶m, digest))
|
||||
return -1;
|
||||
|
||||
if (mp32ne(4, digest, table[i].expect))
|
||||
if (mpne(4, digest, table[i].expect))
|
||||
{
|
||||
printf("failed\n");
|
||||
failures++;
|
||||
|
|
|
@ -54,7 +54,7 @@ int main()
|
|||
if (sha1Digest(¶m, digest))
|
||||
return -1;
|
||||
|
||||
if (mp32ne(5, digest, table[i].expect))
|
||||
if (mpne(5, digest, table[i].expect))
|
||||
{
|
||||
printf("failed\n");
|
||||
failures++;
|
||||
|
|
|
@ -54,7 +54,7 @@ int main()
|
|||
if (sha256Digest(¶m, digest))
|
||||
return -1;
|
||||
|
||||
if (mp32ne(8, digest, table[i].expect))
|
||||
if (mpne(8, digest, table[i].expect))
|
||||
{
|
||||
printf("failed\n");
|
||||
failures++;
|
||||
|
|
|
@ -68,4 +68,28 @@ typedef @uint16_type@ javachar;
|
|||
typedef @float4_type@ javafloat;
|
||||
typedef @double8_type@ javadouble;
|
||||
|
||||
#if HAVE_INTTYPES_H
|
||||
# include <inttypes.h>
|
||||
#else
|
||||
# if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
# endif
|
||||
#endif
|
||||
#define MP_WBITS 32
|
||||
|
||||
#if (MP_WBITS == 64)
|
||||
typedef uint64_t mpw;
|
||||
typedef uint32_t mphw;
|
||||
#elif (MP_WBITS == 32)
|
||||
# if HAVE_UINT64_T
|
||||
# define HAVE_MPDW 1
|
||||
typedef uint64_t mpdw;
|
||||
# endif
|
||||
typedef uint32_t mpw;
|
||||
typedef uint16_t mphw;
|
||||
#else
|
||||
# error
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* _BEECRYPT_TYPES_H */
|
||||
|
|
Loading…
Reference in New Issue