diff options
Diffstat (limited to 'crypto/userspace/libtomcrypt/hashes')
4 files changed, 258 insertions, 0 deletions
diff --git a/crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c b/crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c new file mode 100644 index 00000000000..d01d4183f80 --- /dev/null +++ b/crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c @@ -0,0 +1,30 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file crypt_hash_is_valid.c + Determine if hash is valid, Tom St Denis +*/ + +/* + Test if a hash index is valid + @param idx The hash to search for + @return CRYPT_OK if valid +*/ +int hash_is_valid(const struct algo_properties_st *hash) +{ + return CRYPT_OK; +} + +/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c,v $ */ +/* $Revision: 1.6 $ */ +/* $Date: 2006/12/28 01:27:24 $ */ diff --git a/crypto/userspace/libtomcrypt/hashes/hash_get_oid.c b/crypto/userspace/libtomcrypt/hashes/hash_get_oid.c new file mode 100644 index 00000000000..39f43722884 --- /dev/null +++ b/crypto/userspace/libtomcrypt/hashes/hash_get_oid.c @@ -0,0 +1,78 @@ +/* LibTomCrypt, modular cryptographic library + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + */ +#include "tomcrypt.h" +#include <ncr-int.h> + +/* + Returns the OID of the hash. + @param idx The hash identifier of the hash to search for + @return CRYPT_OK if valid +*/ + +static const oid_st sha1_oid = { + .OIDlen = 6, + .OID = { 1, 3, 14, 3, 2, 26 }, +}; + +static const oid_st md5_oid = { + .OIDlen = 6, + .OID = { 1, 2, 840, 113549, 2, 5, }, +}; + +static const oid_st sha224_oid = { + .OIDlen = 9, + .OID = { 2, 16, 840, 1, 101, 3, 4, 2, 4, }, +}; + +static const oid_st sha256_oid = { + .OIDlen = 9, + .OID = { 2, 16, 840, 1, 101, 3, 4, 2, 1, }, +}; + +static const oid_st sha384_oid = { + .OIDlen = 9, + .OID = { 2, 16, 840, 1, 101, 3, 4, 2, 2, }, +}; + +static const oid_st sha512_oid = { + .OIDlen = 9, + .OID = { 2, 16, 840, 1, 101, 3, 4, 2, 3, }, +}; + +int hash_get_oid(const struct algo_properties_st *hash, oid_st *st) +{ + switch (hash->algo) { + case NCR_ALG_SHA1: + memcpy(st, &sha1_oid, sizeof(*st)); + break; + case NCR_ALG_MD5: + memcpy(st, &md5_oid, sizeof(*st)); + break; + case NCR_ALG_SHA2_224: + memcpy(st, &sha224_oid, sizeof(*st)); + break; + case NCR_ALG_SHA2_256: + memcpy(st, &sha256_oid, sizeof(*st)); + break; + case NCR_ALG_SHA2_384: + memcpy(st, &sha384_oid, sizeof(*st)); + break; + case NCR_ALG_SHA2_512: + memcpy(st, &sha512_oid, sizeof(*st)); + break; + default: + return CRYPT_INVALID_ARG; + } + return CRYPT_OK; +} + +/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c,v $ */ +/* $Revision: 1.6 $ */ +/* $Date: 2006/12/28 01:27:24 $ */ diff --git a/crypto/userspace/libtomcrypt/hashes/hash_memory.c b/crypto/userspace/libtomcrypt/hashes/hash_memory.c new file mode 100644 index 00000000000..c6f51881245 --- /dev/null +++ b/crypto/userspace/libtomcrypt/hashes/hash_memory.c @@ -0,0 +1,66 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" +#include <ncr-int.h> +#include <cryptodev_int.h> + +/** + @file hash_memory.c + Hash memory helper, Tom St Denis +*/ + +/** + Hash a block of memory and store the digest. + @param hash The hash you wish to use + @param in The data you wish to hash + @param inlen The length of the data to hash (octets) + @param out [out] Where to store the digest + @param outlen [in/out] Max size and resulting size of the digest + @return CRYPT_OK if successful +*/ +int hash_memory(const struct algo_properties_st *hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) +{ + int err; + struct hash_data hdata; + + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if ((err = hash_is_valid(hash)) != CRYPT_OK) { + return err; + } + + if (*outlen < hash->digest_size) { + *outlen = hash->digest_size; + return CRYPT_BUFFER_OVERFLOW; + } + + err = cryptodev_hash_init(&hdata, hash->kstr, NULL, 0); + if (err < 0) { + err = CRYPT_INVALID_HASH; + goto LBL_ERR; + } + + if ((err = _cryptodev_hash_update(&hdata, in, inlen)) < 0) { + err = CRYPT_ERROR; + goto LBL_ERR; + } + + err = cryptodev_hash_final(&hdata, out); + + *outlen = hash->digest_size; +LBL_ERR: + cryptodev_hash_deinit(&hdata); + + return err; +} + diff --git a/crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c b/crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c new file mode 100644 index 00000000000..74226767a72 --- /dev/null +++ b/crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c @@ -0,0 +1,84 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" +#include <stdarg.h> +#include <ncr-int.h> +#include <cryptodev_int.h> + +/** + @file hash_memory_multi.c + Hash (multiple buffers) memory helper, Tom St Denis +*/ + +/** + Hash multiple (non-adjacent) blocks of memory at once. + @param hash The hash you wish to use + @param out [out] Where to store the digest + @param outlen [in/out] Max size and resulting size of the digest + @param in The data you wish to hash + @param inlen The length of the data to hash (octets) + @param ... tuples of (data,len) pairs to hash, terminated with a (NULL,x) (x=don't care) + @return CRYPT_OK if successful +*/ +int hash_memory_multi(const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...) +{ + struct hash_data hdata; + int err; + va_list args; + const unsigned char *curptr; + unsigned long curlen; + + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if ((err = hash_is_valid(hash)) != CRYPT_OK) { + return err; + } + + if (*outlen < hash->digest_size) { + *outlen = hash->digest_size; + return CRYPT_BUFFER_OVERFLOW; + } + + err = cryptodev_hash_init(&hdata, hash->kstr, NULL, 0); + if (err < 0) { + err = CRYPT_INVALID_HASH; + goto LBL_ERR; + } + + va_start(args, inlen); + curptr = in; + curlen = inlen; + for (;;) { + /* process buf */ + if ((err = _cryptodev_hash_update(&hdata, curptr, curlen)) < 0) { + err = CRYPT_ERROR; + goto LBL_ERR; + } + /* step to next */ + curptr = va_arg(args, const unsigned char*); + if (curptr == NULL) { + break; + } + curlen = va_arg(args, unsigned long); + } + + err = cryptodev_hash_final(&hdata, out); + + *outlen = hash->digest_size; +LBL_ERR: + cryptodev_hash_deinit(&hdata); + va_end(args); + return err; +} + |