diff options
author | jbj <devnull@localhost> | 2001-09-19 18:05:47 +0000 |
---|---|---|
committer | jbj <devnull@localhost> | 2001-09-19 18:05:47 +0000 |
commit | 3f2193ff8fd567ec5bbfcf6d430d371ee9637e5e (patch) | |
tree | 8feb50291ee198262985f533dbac05af502648f0 /beecrypt/beecrypt.c | |
parent | 02c1189d5951c70f632482fc0a674af35e9ea15d (diff) | |
download | rpm-3f2193ff8fd567ec5bbfcf6d430d371ee9637e5e.tar.gz |
lclint fiddles.
CVS patchset: 5063
CVS date: 2001/09/19 18:05:47
Diffstat (limited to 'beecrypt/beecrypt.c')
-rw-r--r-- | beecrypt/beecrypt.c | 533 |
1 files changed, 490 insertions, 43 deletions
diff --git a/beecrypt/beecrypt.c b/beecrypt/beecrypt.c index 524edee31..fd6a6c968 100644 --- a/beecrypt/beecrypt.c +++ b/beecrypt/beecrypt.c @@ -3,7 +3,7 @@ * * BeeCrypt library hooks & stubs, code * - * Copyright (c) 1999-2000 Virtual Unlimited B.V. + * Copyright (c) 1999, 2000, 2001 Virtual Unlimited B.V. * * Author: Bob Deblier <bob@virtualunlimited.com> * @@ -28,25 +28,39 @@ #include "beecrypt.h" #if HAVE_STDLIB_H -#include <stdlib.h> +# include <stdlib.h> +#endif +#if HAVE_MALLOC_H +# include <malloc.h> #endif #if HAVE_STRING_H -#include <string.h> +# include <string.h> +#endif +#if WIN32 +# include <windows.h> #endif +#include "endianness.h" #include "entropy.h" #include "fips180.h" #include "fips186.h" +#include "hmacmd5.h" +#include "hmacsha1.h" +#include "hmacsha256.h" +#include "md5.h" +#include "mp32.h" #include "mtprng.h" -#include "sha1hmac.h" +#include "sha256.h" #include "blowfish.h" #include "blockmode.h" -static entropySource entropySourceList[] = +/*@observer@*/ static entropySource entropySourceList[] = { #if WIN32 { "wavein", entropy_wavein }, + { "console", entropy_console }, + { "wincrypt", entropy_wincrypt }, #else # if HAVE_DEV_AUDIO { "audio", entropy_dev_audio }, @@ -57,6 +71,9 @@ static entropySource entropySourceList[] = # if HAVE_DEV_RANDOM { "random", entropy_dev_random }, # endif +# if HAVE_DEV_URANDOM + { "urandom", entropy_dev_urandom }, +# endif # if HAVE_DEV_TTY { "tty", entropy_dev_tty }, # endif @@ -92,10 +109,11 @@ const entropySource* entropySourceFind(const char* name) const entropySource* entropySourceDefault() { - char* tmp = getenv("BEECRYPT_ENTROPY"); - if (tmp) + const char* selection = getenv("BEECRYPT_ENTROPY"); + + if (selection) { - return entropySourceFind(tmp); + return entropySourceFind(selection); } else if (ENTROPYSOURCES) { @@ -104,7 +122,31 @@ const entropySource* entropySourceDefault() return (const entropySource*) 0; } -static const randomGenerator* randomGeneratorList[] = +int entropyGatherNext(uint32* data, int size) +{ + const char* selection = getenv("BEECRYPT_ENTROPY"); + + if (selection) + { + const entropySource* ptr = entropySourceFind(selection); + + if (ptr) + return ptr->next(data, size); + } + else + { + register int index; + + for (index = 0; index < ENTROPYSOURCES; index++) + { + if (entropySourceList[index].next(data, size) == 0) + return 0; + } + } + return -1; +} + +/*@observer@*/ static const randomGenerator* randomGeneratorList[] = { &fips186prng, &mtprng @@ -139,28 +181,58 @@ const randomGenerator* randomGeneratorFind(const char* name) const randomGenerator* randomGeneratorDefault() { - char* tmp = getenv("BEECRYPT_RANDOM"); + char* selection = getenv("BEECRYPT_RANDOM"); - if (tmp) - return randomGeneratorFind(tmp); + if (selection) + return randomGeneratorFind(selection); else return &fips186prng; } -void randomGeneratorContextInit(randomGeneratorContext* ctxt, const randomGenerator* rng) +int randomGeneratorContextInit(randomGeneratorContext* ctxt, const randomGenerator* rng) { + if (ctxt == (randomGeneratorContext*) 0) + return -1; + + if (rng == (randomGenerator*) 0) + return -1; + ctxt->rng = rng; - ctxt->param = malloc(rng->paramsize); + ctxt->param = (randomGeneratorParam*) calloc(rng->paramsize, 1); + + if (ctxt->param == (randomGeneratorParam*) 0) + return -1; + + return ctxt->rng->setup(ctxt->param); } -void randomGeneratorContextFree(randomGeneratorContext* ctxt) +int randomGeneratorContextFree(randomGeneratorContext* ctxt) { + register int rc; + + if (ctxt == (randomGeneratorContext*) 0) + return -1; + + if (ctxt->rng == (randomGenerator*) 0) + return -1; + + if (ctxt->param == (randomGeneratorParam*) 0) + return -1; + + rc = ctxt->rng->cleanup(ctxt->param); + free(ctxt->param); + + ctxt->param = (randomGeneratorParam*) 0; + + return rc; } -static const hashFunction* hashFunctionList[] = +/*@observer@*/ static const hashFunction* hashFunctionList[] = { - &sha1 + &md5, + &sha1, + &sha256 }; #define HASHFUNCTIONS (sizeof(hashFunctionList) / sizeof(hashFunction*)) @@ -172,9 +244,10 @@ int hashFunctionCount() const hashFunction* hashFunctionDefault() { - char* tmp = getenv("BEECRYPT_HASH"); - if (tmp) - return hashFunctionFind(tmp); + char* selection = getenv("BEECRYPT_HASH"); + + if (selection) + return hashFunctionFind(selection); else return &sha1; } @@ -199,21 +272,162 @@ const hashFunction* hashFunctionFind(const char* name) return (const hashFunction*) 0; } -void hashFunctionContextInit(hashFunctionContext* ctxt, const hashFunction* hash) +int hashFunctionContextInit(hashFunctionContext* ctxt, const hashFunction* hash) { - ctxt->hash = hash; - ctxt->param = malloc(hash->paramsize); + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (hash == (hashFunction*) 0) + return -1; + + ctxt->algo = hash; + ctxt->param = (hashFunctionParam*) calloc(hash->paramsize, 1); + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; + + return ctxt->algo->reset(ctxt->param); } -void hashFunctionContextFree(hashFunctionContext* ctxt) +int hashFunctionContextFree(hashFunctionContext* ctxt) { + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; + free(ctxt->param); + + ctxt->param = (hashFunctionParam*) 0; + + return 0; +} + +int hashFunctionContextReset(hashFunctionContext* ctxt) +{ + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (hashFunction*) 0) + return -1; + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; + + return ctxt->algo->reset(ctxt->param); +} + +int hashFunctionContextUpdate(hashFunctionContext* ctxt, const byte* data, int size) +{ + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (hashFunction*) 0) + return -1; + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; + + if (data == (const byte*) 0) + return -1; + + return ctxt->algo->update(ctxt->param, data, size); +} + +int hashFunctionContextUpdateMC(hashFunctionContext* ctxt, const memchunk* m) +{ + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (hashFunction*) 0) + return -1; + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; + + if (m == (memchunk*) 0) + return -1; + + return ctxt->algo->update(ctxt->param, m->data, m->size); } +int hashFunctionContextUpdateMP32(hashFunctionContext* ctxt, const mp32number* n) +{ + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (hashFunction*) 0) + return -1; + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; -static const keyedHashFunction* keyedHashFunctionList[] = + if (n != (mp32number*) 0) + { + register int rc = -1; + register byte* temp = (byte*) malloc((n->size << 2) + 1); + + if (mp32msbset(n->size, n->data)) + { + temp[0] = 0; + encodeInts((javaint*) n->data, temp+1, n->size); + rc = ctxt->algo->update(ctxt->param, temp, (n->size << 2) + 1); + } + else + { + encodeInts((javaint*) n->data, temp, n->size); + rc = ctxt->algo->update(ctxt->param, temp, n->size << 2); + } + free(temp); + + return rc; + } + return -1; +} + +int hashFunctionContextDigest(hashFunctionContext* ctxt, mp32number* dig) { - &sha1hmac + if (ctxt == (hashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (hashFunction*) 0) + return -1; + + if (ctxt->param == (hashFunctionParam*) 0) + return -1; + + if (dig != (mp32number*) 0) + { + mp32nsize(dig, (ctxt->algo->digestsize + 3) >> 2); + + return ctxt->algo->digest(ctxt->param, dig->data); + } + return -1; +} + +int hashFunctionContextDigestMatch(hashFunctionContext* ctxt, const mp32number* match) +{ + register int rc = 0; + + mp32number dig; + + mp32nzero(&dig); + + if (hashFunctionContextDigest(ctxt, &dig) == 0) + if (dig.size == match->size) + rc = mp32eq(dig.size, dig.data, match->data); + + mp32nfree(&dig); + + return rc; +} + +/*@observer@*/ static const keyedHashFunction* keyedHashFunctionList[] = +{ + &hmacmd5, + &hmacsha1, + &hmacsha256 }; #define KEYEDHASHFUNCTIONS (sizeof(keyedHashFunctionList) / sizeof(keyedHashFunction*)) @@ -225,11 +439,12 @@ int keyedHashFunctionCount() const keyedHashFunction* keyedHashFunctionDefault() { - char* tmp = getenv("BEECRYPT_KEYEDHASH"); - if (tmp) - return keyedHashFunctionFind(tmp); + char* selection = getenv("BEECRYPT_KEYEDHASH"); + + if (selection) + return keyedHashFunctionFind(selection); else - return (const keyedHashFunction*) 0; + return &hmacsha1; } const keyedHashFunction* keyedHashFunctionGet(int index) @@ -252,21 +467,182 @@ const keyedHashFunction* keyedHashFunctionFind(const char* name) return (const keyedHashFunction*) 0; } -void keyedHashFunctionContextInit(keyedHashFunctionContext* ctxt, const keyedHashFunction* hash) +int keyedHashFunctionContextInit(keyedHashFunctionContext* ctxt, const keyedHashFunction* mac) { - ctxt->hash = hash; - ctxt->param = malloc(hash->paramsize); + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (mac == (keyedHashFunction*) 0) + return -1; + + ctxt->algo = mac; + ctxt->param = (keyedHashFunctionParam*) calloc(mac->paramsize, 1); + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + return ctxt->algo->reset(ctxt->param); } -void keyedHashFunctionContextFree(keyedHashFunctionContext* ctxt) +int keyedHashFunctionContextFree(keyedHashFunctionContext* ctxt) { + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + free(ctxt->param); + + ctxt->param = (keyedHashFunctionParam*) 0; + + return 0; +} + +int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const uint32* key, int keybits) +{ + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + if (key == (uint32*) 0) + return -1; + + return ctxt->algo->setup(ctxt->param, key, keybits); } +int keyedHashFunctionContextReset(keyedHashFunctionContext* ctxt) +{ + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + return ctxt->algo->reset(ctxt->param); +} -static const blockCipher* blockCipherList[] = +int keyedHashFunctionContextUpdate(keyedHashFunctionContext* ctxt, const byte* data, int size) { - &blowfish, + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + if (data == (byte*) 0) + return -1; + + return ctxt->algo->update(ctxt->param, data, size); +} + +int keyedHashFunctionContextUpdateMC(keyedHashFunctionContext* ctxt, const memchunk* m) +{ + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + if (m == (memchunk*) 0) + return -1; + + return ctxt->algo->update(ctxt->param, m->data, m->size); +} + +int keyedHashFunctionContextUpdateMP32(keyedHashFunctionContext* ctxt, const mp32number* n) +{ + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + if (n != (mp32number*) 0) + { + register int rc; + register byte* temp = (byte*) malloc((n->size << 2) + 1); + + if (mp32msbset(n->size, n->data)) + { + temp[0] = 0; + encodeInts((javaint*) n->data, temp+1, n->size); + rc = ctxt->algo->update(ctxt->param, temp, (n->size << 2) + 1); + } + else + { + encodeInts((javaint*) n->data, temp, n->size); + rc = ctxt->algo->update(ctxt->param, temp, n->size << 2); + } + free(temp); + + return rc; + } + return -1; +} + +int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, mp32number* dig) +{ + if (ctxt == (keyedHashFunctionContext*) 0) + return -1; + + if (ctxt->algo == (keyedHashFunction*) 0) + return -1; + + if (ctxt->param == (keyedHashFunctionParam*) 0) + return -1; + + if (dig != (mp32number*) 0) + { + mp32nsize(dig, (ctxt->algo->digestsize + 3) >> 2); + + return ctxt->algo->digest(ctxt->param, dig->data); + } + return -1; +} + +int keyedHashFunctionContextDigestMatch(keyedHashFunctionContext* ctxt, const mp32number* match) +{ + register int rc = 0; + + mp32number dig; + + mp32nzero(&dig); + + if (keyedHashFunctionContextDigest(ctxt, &dig) == 0) + if (dig.size == match->size) + + rc = mp32eq(dig.size, dig.data, match->data); + + mp32nfree(&dig); + + return rc; +} + + +/*@observer@*/ static const blockCipher* blockCipherList[] = +{ + &blowfish }; #define BLOCKCIPHERS (sizeof(blockCipherList) / sizeof(blockCipher*)) @@ -278,10 +654,10 @@ int blockCipherCount() const blockCipher* blockCipherDefault() { - char* tmp = getenv("BEECRYPT_CIPHER"); + char* selection = getenv("BEECRYPT_CIPHER"); - if (tmp) - return blockCipherFind(tmp); + if (selection) + return blockCipherFind(selection); else return &blowfish; } @@ -307,13 +683,84 @@ const blockCipher* blockCipherFind(const char* name) return (const blockCipher*) 0; } -void blockCipherContextInit(blockCipherContext* ctxt, const blockCipher* ciph) +int blockCipherContextInit(blockCipherContext* ctxt, const blockCipher* ciph) +{ + if (ctxt == (blockCipherContext*) 0) + return -1; + + if (ciph == (blockCipher*) 0) + return -1; + + ctxt->algo = ciph; + ctxt->param = (blockCipherParam*) calloc(ciph->paramsize, 1); + + if (ctxt->param == (blockCipherParam*) 0) + return -1; + + return 0; +} + +int blockCipherContextSetup(blockCipherContext* ctxt, const uint32* key, int keybits, cipherOperation op) { - ctxt->ciph = ciph; - ctxt->param = malloc(ciph->paramsize); + if (ctxt == (blockCipherContext*) 0) + return -1; + + if (ctxt->algo == (blockCipher*) 0) + return -1; + + if (ctxt->param == (blockCipherParam*) 0) + return -1; + + if (key == (uint32*) 0) + return -1; + + return ctxt->algo->setup(ctxt->param, key, keybits, op); +} + +int blockCipherContextSetIV(blockCipherContext* ctxt, const uint32* iv) +{ + if (ctxt == (blockCipherContext*) 0) + return -1; + + if (ctxt->algo == (blockCipher*) 0) + return -1; + + if (ctxt->param == (blockCipherParam*) 0) + return -1; + + /* null is an allowed value for iv, so don't test it */ + + return ctxt->algo->setiv(ctxt->param, iv); } -void blockCipherContextFree(blockCipherContext* ctxt) +int blockCipherContextFree(blockCipherContext* ctxt) { + if (ctxt == (blockCipherContext*) 0) + return -1; + + if (ctxt->param == (blockCipherParam*) 0) + return -1; + free(ctxt->param); + + ctxt->param = (blockCipherParam*) 0; + + return 0; +} + +#if WIN32 +__declspec(dllexport) +BOOL WINAPI DllMain(HINSTANCE hInst, DWORD wDataSeg, LPVOID lpReserved) +{ + switch (wDataSeg) + { + case DLL_PROCESS_ATTACH: + entropy_provider_setup(hInst); + break; + case DLL_PROCESS_DETACH: + entropy_provider_cleanup(); + break; + } + return TRUE; } +#endif |