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 Sept 7th, 2003
v0.90 -- new ROL/ROR for x86 GCC v0.90 -- new ROL/ROR for x86 GCC
-- Jochen Katz submitted a patch to the makefile to prevent "make" from making the .a library -- 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}} \def\gap{\vspace{0.5ex}}
\makeindex \makeindex
\begin{document} \begin{document}
\title{A Tiny Crypto Library, \\ LibTomCrypt \\ Version 0.90} \title{A Tiny Crypto Library, \\ LibTomCrypt \\ Version 0.91}
\author{Tom St Denis \\ \author{Tom St Denis \\
Algonquin College \\ Algonquin College \\
\\ \\

View File

@ -74,4 +74,6 @@ void register_algs(void)
register_hash(&md4_desc); register_hash(&md4_desc);
register_hash(&tiger_desc); register_hash(&tiger_desc);
register_hash(&md2_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_PK_NOT_PRIVATE);
ERR (CRYPT_INVALID_ARG); ERR (CRYPT_INVALID_ARG);
ERR (CRYPT_FILE_NOTFOUND);
ERR (CRYPT_PK_INVALID_TYPE); ERR (CRYPT_PK_INVALID_TYPE);
ERR (CRYPT_PK_INVALID_SYSTEM); ERR (CRYPT_PK_INVALID_SYSTEM);

View File

@ -135,6 +135,9 @@ void reg_algs(void)
#ifdef RIPEMD128 #ifdef RIPEMD128
register_hash (&rmd128_desc); register_hash (&rmd128_desc);
#endif #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"); in = fopen(fname, "rb");
if (in == NULL) { 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); (void)fclose(in);
return err; return err;
}
(void)fclose(in);
return CRYPT_OK;
#endif #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? */ /* valid key length? */
if (keylen == 0 || keylen > MAXBLOCKSIZE) { if (keylen == 0) {
return CRYPT_INVALID_KEYSIZE; 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) { if(hashsize < HMAC_BLOCKSIZE) {
zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize)); zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize));
} }
keylen = hashsize;
} else { } else {
memcpy(hmac->key, key, (size_t)keylen); memcpy(hmac->key, key, (size_t)keylen);
if(keylen < HMAC_BLOCKSIZE) { 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) // 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; buf[i] = hmac->key[i] ^ 0x36;
} }
for( ; i < HMAC_BLOCKSIZE; i++) {
buf[i] = 0x36;
}
// Pre-pend that to the hash data // Pre-pend that to the hash data
hash_descriptor[hash].init(&hmac->md); hash_descriptor[hash].init(&hmac->md);
hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE); 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); hash_descriptor[hash].done(&hmac->md, hashOut);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(isha, sizeof(buf));
zeromem(buf, sizeof(isha));
zeromem(hmac->key, sizeof(hmac->key)); zeromem(hmac->key, sizeof(hmac->key));
#endif #endif
return CRYPT_OK; return CRYPT_OK;
@ -188,7 +187,7 @@ int hmac_file(int hash, const char *fname, const unsigned char *key,
in = fopen(fname, "rb"); in = fopen(fname, "rb");
if (in == NULL) { if (in == NULL) {
return CRYPT_INVALID_ARG; return CRYPT_FILE_NOTFOUND;
} }
/* process the file contents */ /* process the file contents */

View File

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

View File

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

69
mpi.c
View File

@ -14,7 +14,7 @@
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include "mycrypt.h" #include "mycrypt.h"
#include <tommath.h> #include "tommath.h"
static const struct { static const struct {
int code; int code;
@ -943,9 +943,6 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
/* if a is positive */ /* if a is positive */
if (a->sign == MP_ZPOS) { if (a->sign == MP_ZPOS) {
/* setup size */
c->used = a->used + 1;
/* add digit, after this we're propagating /* add digit, after this we're propagating
* the carry. * the carry.
*/ */
@ -962,6 +959,9 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
/* set final carry */ /* set final carry */
ix++; ix++;
*tmpc++ = mu; *tmpc++ = mu;
/* setup size */
c->used = a->used + 1;
} else { } else {
/* a was negative and |a| < b */ /* a was negative and |a| < b */
c->used = 1; 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 * 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 int
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k) 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> #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. * 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. * The value of k changes based on the size of the exponent.
@ -2927,17 +2927,29 @@ int
mp_grow (mp_int * a, int size) mp_grow (mp_int * a, int size)
{ {
int i; int i;
mp_digit *tmp;
/* if the alloc size is smaller alloc more ram */ /* if the alloc size is smaller alloc more ram */
if (a->alloc < size) { if (a->alloc < size) {
/* ensure there are always at least MP_PREC digits extra on top */ /* ensure there are always at least MP_PREC digits extra on top */
size += (MP_PREC * 2) - (size % MP_PREC); size += (MP_PREC * 2) - (size % MP_PREC);
a->dp = OPT_CAST XREALLOC (a->dp, sizeof (mp_digit) * size); /* reallocate the array a->dp
if (a->dp == NULL) { *
* 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; return MP_MEM;
} }
/* reallocation succeeded so set a->dp */
a->dp = tmp;
/* zero excess digits */ /* zero excess digits */
i = a->alloc; i = a->alloc;
a->alloc = size; a->alloc = size;
@ -3875,7 +3887,7 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
*/ */
#include <tommath.h> #include <tommath.h>
/* calc a value mod 2^b */ /* calc a value mod 2**b */
int int
mp_mod_2d (mp_int * a, int b, mp_int * c) 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 int
mp_mul_d (mp_int * a, mp_digit b, mp_int * c) 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 */ /* make sure c is big enough to hold a*b */
pa = a->used; if (c->alloc < a->used + 1) {
if (c->alloc < pa + 1) { if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
if ((res = mp_grow (c, pa + 1)) != MP_OKAY) {
return res; return res;
} }
} }
@ -4419,15 +4432,9 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
/* get the original destinations used count */ /* get the original destinations used count */
olduse = c->used; olduse = c->used;
/* set the new temporary used count */ /* set the sign */
c->used = pa + 1;
c->sign = a->sign; c->sign = a->sign;
{
register mp_digit u, *tmpa, *tmpc;
register mp_word r;
register int ix;
/* alias for a->dp [source] */ /* alias for a->dp [source] */
tmpa = a->dp; tmpa = a->dp;
@ -4436,7 +4443,9 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
/* zero carry */ /* zero carry */
u = 0; 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 */ /* compute product and carry sum for this term */
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b); 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 */ /* send carry into next iteration */
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
} }
/* store final carry [if any] */ /* store final carry [if any] */
*tmpc++ = u; *tmpc++ = u;
/* now zero digits above the top */ /* now zero digits above the top */
for (; pa < olduse; pa++) { while (ix++ < olduse) {
*tmpc++ = 0; *tmpc++ = 0;
} }
}
mp_clamp (c); /* set used count */
c->used = a->used + 1;
mp_clamp(c);
return MP_OKAY; return MP_OKAY;
} }
@ -5920,10 +5932,12 @@ mp_set_int (mp_int * a, unsigned int b)
int int
mp_shrink (mp_int * a) mp_shrink (mp_int * a)
{ {
mp_digit *tmp;
if (a->alloc != a->used) { 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; return MP_MEM;
} }
a->dp = tmp;
a->alloc = a->used; a->alloc = a->used;
} }
return MP_OKAY; 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; *tmpc++ = 0;
} }
mp_clamp(c); mp_clamp(c);
@ -6611,7 +6626,7 @@ mp_toom_sqr(mp_int *a, mp_int *b)
/* B */ /* B */
B = a->used / 3; 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) { if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
goto ERR; goto ERR;
} }

View File

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

View File

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

View File

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