added libtomcrypt-0.91

This commit is contained in:
Tom St Denis 2003-09-26 01:16:18 +00:00 committed by Steffen Jaeckel
parent 16100c38eb
commit 55d745af4f
14 changed files with 386 additions and 354 deletions

View File

@ -1,3 +1,12 @@
Sept 25th, 2003
v0.91 -- HMAC fix of 0.90 was incorrect for keys larger than the block size of the hash.
-- Added error CRYPT_FILE_NOTFOUND for the file [hmac/hash] routines.
-- Added RIPEMD hashes to the hashsum demo.
-- Added hashsum demo to MSVC makefile.
-- Added RMD160 to the x86_prof demo [oops]
-- Merged in LibTomMath-0.27 with a patch to mp_shrink() that will be in LibTomMath-0.28
Fixes another potential memory leak.
Sept 7th, 2003
v0.90 -- new ROL/ROR for x86 GCC
-- Jochen Katz submitted a patch to the makefile to prevent "make" from making the .a library

BIN
crypt.pdf

Binary file not shown.

View File

@ -47,7 +47,7 @@
\def\gap{\vspace{0.5ex}}
\makeindex
\begin{document}
\title{A Tiny Crypto Library, \\ LibTomCrypt \\ Version 0.90}
\title{A Tiny Crypto Library, \\ LibTomCrypt \\ Version 0.91}
\author{Tom St Denis \\
Algonquin College \\
\\

View File

@ -74,4 +74,6 @@ void register_algs(void)
register_hash(&md4_desc);
register_hash(&tiger_desc);
register_hash(&md2_desc);
register_hash(&rmd128_desc);
register_hash(&rmd160_desc);
}

View File

@ -1700,6 +1700,7 @@ test_errs (void)
ERR (CRYPT_PK_NOT_PRIVATE);
ERR (CRYPT_INVALID_ARG);
ERR (CRYPT_FILE_NOTFOUND);
ERR (CRYPT_PK_INVALID_TYPE);
ERR (CRYPT_PK_INVALID_SYSTEM);

View File

@ -135,6 +135,9 @@ void reg_algs(void)
#ifdef RIPEMD128
register_hash (&rmd128_desc);
#endif
#ifdef RIPEMD160
register_hash (&rmd160_desc);
#endif
}

8
hash.c
View File

@ -78,16 +78,12 @@ int hash_file(int hash, const char *fname, unsigned char *dst, unsigned long *ou
in = fopen(fname, "rb");
if (in == NULL) {
return CRYPT_INVALID_ARG;
return CRYPT_FILE_NOTFOUND;
}
if ((err = hash_filehandle(hash, in, dst, outlen)) != CRYPT_OK) {
err = hash_filehandle(hash, in, dst, outlen);
(void)fclose(in);
return err;
}
(void)fclose(in);
return CRYPT_OK;
#endif
}

13
hmac.c
View File

@ -38,7 +38,7 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
}
/* valid key length? */
if (keylen == 0 || keylen > MAXBLOCKSIZE) {
if (keylen == 0) {
return CRYPT_INVALID_KEYSIZE;
}
@ -54,6 +54,7 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
if(hashsize < HMAC_BLOCKSIZE) {
zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize));
}
keylen = hashsize;
} else {
memcpy(hmac->key, key, (size_t)keylen);
if(keylen < HMAC_BLOCKSIZE) {
@ -62,14 +63,10 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
}
// Create the initial vector for step (3)
for(i=0; i < keylen; i++) {
for(i=0; i < HMAC_BLOCKSIZE; i++) {
buf[i] = hmac->key[i] ^ 0x36;
}
for( ; i < HMAC_BLOCKSIZE; i++) {
buf[i] = 0x36;
}
// Pre-pend that to the hash data
hash_descriptor[hash].init(&hmac->md);
hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE);
@ -126,6 +123,8 @@ int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
hash_descriptor[hash].done(&hmac->md, hashOut);
#ifdef CLEAN_STACK
zeromem(isha, sizeof(buf));
zeromem(buf, sizeof(isha));
zeromem(hmac->key, sizeof(hmac->key));
#endif
return CRYPT_OK;
@ -188,7 +187,7 @@ int hmac_file(int hash, const char *fname, const unsigned char *key,
in = fopen(fname, "rb");
if (in == NULL) {
return CRYPT_INVALID_ARG;
return CRYPT_FILE_NOTFOUND;
}
/* process the file contents */

View File

@ -9,7 +9,7 @@
# a build. This is easy to remedy though, for those that have problems.
# The version
VERSION=0.90
VERSION=0.91
#ch1-01-1
# Compiler and Linker Names

View File

@ -26,3 +26,6 @@ x86_prof: demos/x86_prof.c library
tv_gen: demos/tv_gen.c library
cl $(CFLAGS) demos/tv_gen.c tomcrypt.lib advapi32.lib
hashsum: demos/hashsum.c library
cl $(CFLAGS) demos/hashsum.c tomcrypt.lib advapi32.lib

67
mpi.c
View File

@ -14,7 +14,7 @@
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include "mycrypt.h"
#include <tommath.h>
#include "tommath.h"
static const struct {
int code;
@ -943,9 +943,6 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
/* if a is positive */
if (a->sign == MP_ZPOS) {
/* setup size */
c->used = a->used + 1;
/* add digit, after this we're propagating
* the carry.
*/
@ -962,6 +959,9 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
/* set final carry */
ix++;
*tmpc++ = mu;
/* setup size */
c->used = a->used + 1;
} else {
/* a was negative and |a| < b */
c->used = 1;
@ -2122,7 +2122,7 @@ int mp_dr_is_modulus(mp_int *a)
*
* Has been modified to use algorithm 7.10 from the LTM book instead
*
* Input x must be in the range 0 <= x <= (n-1)^2
* Input x must be in the range 0 <= x <= (n-1)**2
*/
int
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
@ -2403,7 +2403,7 @@ mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
*/
#include <tommath.h>
/* computes Y == G^X mod P, HAC pp.616, Algorithm 14.85
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
*
* Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
* The value of k changes based on the size of the exponent.
@ -2927,17 +2927,29 @@ int
mp_grow (mp_int * a, int size)
{
int i;
mp_digit *tmp;
/* if the alloc size is smaller alloc more ram */
if (a->alloc < size) {
/* ensure there are always at least MP_PREC digits extra on top */
size += (MP_PREC * 2) - (size % MP_PREC);
a->dp = OPT_CAST XREALLOC (a->dp, sizeof (mp_digit) * size);
if (a->dp == NULL) {
/* reallocate the array a->dp
*
* We store the return in a temporary variable
* in case the operation failed we don't want
* to overwrite the dp member of a.
*/
tmp = OPT_CAST XREALLOC (a->dp, sizeof (mp_digit) * size);
if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM;
}
/* reallocation succeeded so set a->dp */
a->dp = tmp;
/* zero excess digits */
i = a->alloc;
a->alloc = size;
@ -3875,7 +3887,7 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
*/
#include <tommath.h>
/* calc a value mod 2^b */
/* calc a value mod 2**b */
int
mp_mod_2d (mp_int * a, int b, mp_int * c)
{
@ -4406,12 +4418,13 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
int
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
{
int res, pa, olduse;
mp_digit u, *tmpa, *tmpc;
mp_word r;
int ix, res, olduse;
/* make sure c is big enough to hold a*b */
pa = a->used;
if (c->alloc < pa + 1) {
if ((res = mp_grow (c, pa + 1)) != MP_OKAY) {
if (c->alloc < a->used + 1) {
if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
return res;
}
}
@ -4419,15 +4432,9 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
/* get the original destinations used count */
olduse = c->used;
/* set the new temporary used count */
c->used = pa + 1;
/* set the sign */
c->sign = a->sign;
{
register mp_digit u, *tmpa, *tmpc;
register mp_word r;
register int ix;
/* alias for a->dp [source] */
tmpa = a->dp;
@ -4436,7 +4443,9 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
/* zero carry */
u = 0;
for (ix = 0; ix < pa; ix++) {
/* compute columns */
for (ix = 0; ix < a->used; ix++) {
/* compute product and carry sum for this term */
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
@ -4446,16 +4455,19 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
/* send carry into next iteration */
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
}
/* store final carry [if any] */
*tmpc++ = u;
/* now zero digits above the top */
for (; pa < olduse; pa++) {
while (ix++ < olduse) {
*tmpc++ = 0;
}
}
/* set used count */
c->used = a->used + 1;
mp_clamp(c);
return MP_OKAY;
}
@ -5920,10 +5932,12 @@ mp_set_int (mp_int * a, unsigned int b)
int
mp_shrink (mp_int * a)
{
mp_digit *tmp;
if (a->alloc != a->used) {
if ((a->dp = OPT_CAST XREALLOC (a->dp, sizeof (mp_digit) * a->used)) == NULL) {
if ((tmp = OPT_CAST XREALLOC (a->dp, sizeof (mp_digit) * a->used)) == NULL) {
return MP_MEM;
}
a->dp = tmp;
a->alloc = a->used;
}
return MP_OKAY;
@ -6173,7 +6187,8 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
}
}
for (; ix < oldused; ix++) {
/* zero excess digits */
while (ix++ < oldused) {
*tmpc++ = 0;
}
mp_clamp(c);
@ -6611,7 +6626,7 @@ mp_toom_sqr(mp_int *a, mp_int *b)
/* B */
B = a->used / 3;
/* a = a2 * B^2 + a1 * B + a0 */
/* a = a2 * B**2 + a1 * B + a0 */
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
goto ERR;
}

View File

@ -16,8 +16,8 @@ extern "C" {
#endif
/* version */
#define CRYPT 0x0090
#define SCRYPT "0.90"
#define CRYPT 0x0091
#define SCRYPT "0.91"
/* max size of either a cipher/hash block or symmetric key [largest of the two] */
#define MAXBLOCKSIZE 128
@ -49,6 +49,7 @@ enum {
CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
CRYPT_INVALID_ARG, /* Generic invalid argument */
CRYPT_FILE_NOTFOUND, /* File Not Found */
CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */

View File

@ -16,6 +16,7 @@
#define XCLOCK clock
#define XCLOCKS_PER_SEC CLOCKS_PER_SEC
#define SMALL_CODE
#define CLEAN_STACK
#define LTC_TEST
#define BLOWFISH
#define RC2

View File

@ -27,6 +27,7 @@ static const char *err_2_str[] =
"A private PK key is required.",
"Invalid argument provided.",
"File Not Found",
"Invalid PK type.",
"Invalid PK system.",
@ -34,7 +35,8 @@ static const char *err_2_str[] =
"Key not found in keyring.",
"Invalid sized parameter.",
"Invalid size for prime."
"Invalid size for prime.",
};
const char *error_to_string(int err)