add register_all_{ciphers,hashes,prngs}()

This commit is contained in:
Steffen Jaeckel 2017-06-08 11:49:13 +02:00
parent 7f91e5ae65
commit 4ceb82bf54
7 changed files with 258 additions and 10 deletions

View File

@ -81,27 +81,33 @@ sub check_defines {
return $fails;
}
sub check_descriptors {
sub check_descriptor {
my $which = shift;
my $what = shift;
my @src;
my @descriptors;
find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, './src/hashes/');
find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, "./src/${which}/");
for my $f (@src) {
my @n = map { my $x = $_; $x =~ s/^.*?ltc_hash_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_hash_descriptor/ } split /\n/, read_file($f);
push @descriptors, @n if @n;
}
find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, './src/ciphers/');
for my $f (@src) {
my @n = map { my $x = $_; $x =~ s/^.*?ltc_cipher_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_cipher_descriptor/ } split /\n/, read_file($f);
my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file($f);
push @descriptors, @n if @n;
}
my $fails = 0;
for my $d (@descriptors) {
for my $f (qw{ tests/common.c }) {
for my $f ("./src/misc/crypt/crypt_register_all_${which}.c") {
my $txt = read_file($f);
warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
}
}
warn( $fails > 0 ? "check-hashes: FAIL $fails\n" : "check-hashes: PASS\n" );
my $name = sprintf("%-17s", "check-${which}:");
warn( $fails > 0 ? "${name}FAIL $fails\n" : "${name}PASS\n" );
return $fails;
}
sub check_descriptors {
my $fails = 0;
$fails = $fails + check_descriptor("ciphers", "cipher");
$fails = $fails + check_descriptor("hashes", "hash");
$fails = $fails + check_descriptor("prngs", "prng");
return $fails;
}

View File

@ -933,6 +933,7 @@ int find_cipher_any(const char *name, int blocklen, int keylen);
int find_cipher_id(unsigned char ID);
int register_cipher(const struct ltc_cipher_descriptor *cipher);
int unregister_cipher(const struct ltc_cipher_descriptor *cipher);
int register_all_ciphers(void);
int cipher_is_valid(int idx);
LTC_MUTEX_PROTO(ltc_cipher_mutex)

View File

@ -461,6 +461,7 @@ int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
int find_hash_any(const char *name, int digestlen);
int register_hash(const struct ltc_hash_descriptor *hash);
int unregister_hash(const struct ltc_hash_descriptor *hash);
int register_all_hashes(void);
int hash_is_valid(int idx);
LTC_MUTEX_PROTO(ltc_hash_mutex)

View File

@ -199,6 +199,7 @@ extern const struct ltc_prng_descriptor sober128_desc;
int find_prng(const char *name);
int register_prng(const struct ltc_prng_descriptor *prng);
int unregister_prng(const struct ltc_prng_descriptor *prng);
int register_all_prngs(void);
int prng_is_valid(int idx);
LTC_MUTEX_PROTO(ltc_prng_mutex)

View File

@ -0,0 +1,98 @@
/* 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.
*/
#include "tomcrypt.h"
/**
@file crypt_register_all_ciphers.c
Steffen Jaeckel
*/
#define REGISTER_CIPHER(h) do {\
LTC_ARGCHK((err = register_cipher(h) == -1 ? CRYPT_ERROR : CRYPT_OK) == CRYPT_OK); \
} while(0)
int register_all_ciphers(void)
{
int err = CRYPT_NOP;
#ifdef LTC_RIJNDAEL
#ifdef ENCRYPT_ONLY
/* alternative would be
* register_cipher(&rijndael_enc_desc);
*/
REGISTER_CIPHER(&aes_enc_desc);
#else
/* alternative would be
* register_cipher(&rijndael_desc);
*/
REGISTER_CIPHER(&aes_desc);
#endif
#endif
#ifdef LTC_BLOWFISH
REGISTER_CIPHER(&blowfish_desc);
#endif
#ifdef LTC_XTEA
REGISTER_CIPHER(&xtea_desc);
#endif
#ifdef LTC_RC5
REGISTER_CIPHER(&rc5_desc);
#endif
#ifdef LTC_RC6
REGISTER_CIPHER(&rc6_desc);
#endif
#ifdef LTC_SAFERP
REGISTER_CIPHER(&saferp_desc);
#endif
#ifdef LTC_TWOFISH
REGISTER_CIPHER(&twofish_desc);
#endif
#ifdef LTC_SAFER
REGISTER_CIPHER(&safer_k64_desc);
REGISTER_CIPHER(&safer_sk64_desc);
REGISTER_CIPHER(&safer_k128_desc);
REGISTER_CIPHER(&safer_sk128_desc);
#endif
#ifdef LTC_RC2
REGISTER_CIPHER(&rc2_desc);
#endif
#ifdef LTC_DES
REGISTER_CIPHER(&des_desc);
REGISTER_CIPHER(&des3_desc);
#endif
#ifdef LTC_CAST5
REGISTER_CIPHER(&cast5_desc);
#endif
#ifdef LTC_NOEKEON
REGISTER_CIPHER(&noekeon_desc);
#endif
#ifdef LTC_SKIPJACK
REGISTER_CIPHER(&skipjack_desc);
#endif
#ifdef LTC_ANUBIS
REGISTER_CIPHER(&anubis_desc);
#endif
#ifdef LTC_KHAZAD
REGISTER_CIPHER(&khazad_desc);
#endif
#ifdef LTC_KSEED
REGISTER_CIPHER(&kseed_desc);
#endif
#ifdef LTC_KASUMI
REGISTER_CIPHER(&kasumi_desc);
#endif
#ifdef LTC_MULTI2
REGISTER_CIPHER(&multi2_desc);
#endif
#ifdef LTC_CAMELLIA
REGISTER_CIPHER(&camellia_desc);
#endif
return err;
}

View File

@ -0,0 +1,96 @@
/* 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.
*/
#include "tomcrypt.h"
/**
@file crypt_register_all_hashes.c
Steffen Jaeckel
*/
#define REGISTER_HASH(h) do {\
LTC_ARGCHK((err = register_hash(h) == -1 ? CRYPT_ERROR : CRYPT_OK) == CRYPT_OK); \
} while(0)
int register_all_hashes(void)
{
int err = CRYPT_NOP;
#ifdef LTC_TIGER
REGISTER_HASH(&tiger_desc);
#endif
#ifdef LTC_MD2
REGISTER_HASH(&md2_desc);
#endif
#ifdef LTC_MD4
REGISTER_HASH(&md4_desc);
#endif
#ifdef LTC_MD5
REGISTER_HASH(&md5_desc);
#endif
#ifdef LTC_SHA1
REGISTER_HASH(&sha1_desc);
#endif
#ifdef LTC_SHA224
REGISTER_HASH(&sha224_desc);
#endif
#ifdef LTC_SHA256
REGISTER_HASH(&sha256_desc);
#endif
#ifdef LTC_SHA384
REGISTER_HASH(&sha384_desc);
#endif
#ifdef LTC_SHA512
REGISTER_HASH(&sha512_desc);
#endif
#ifdef LTC_SHA512_224
REGISTER_HASH(&sha512_224_desc);
#endif
#ifdef LTC_SHA512_256
REGISTER_HASH(&sha512_256_desc);
#endif
#ifdef LTC_SHA3
REGISTER_HASH(&sha3_224_desc);
REGISTER_HASH(&sha3_256_desc);
REGISTER_HASH(&sha3_384_desc);
REGISTER_HASH(&sha3_512_desc);
#endif
#ifdef LTC_RIPEMD128
REGISTER_HASH(&rmd128_desc);
#endif
#ifdef LTC_RIPEMD160
REGISTER_HASH(&rmd160_desc);
#endif
#ifdef LTC_RIPEMD256
REGISTER_HASH(&rmd256_desc);
#endif
#ifdef LTC_RIPEMD320
REGISTER_HASH(&rmd320_desc);
#endif
#ifdef LTC_WHIRLPOOL
REGISTER_HASH(&whirlpool_desc);
#endif
#ifdef LTC_BLAKE2S
REGISTER_HASH(&blake2s_128_desc);
REGISTER_HASH(&blake2s_160_desc);
REGISTER_HASH(&blake2s_224_desc);
REGISTER_HASH(&blake2s_256_desc);
#endif
#ifdef LTC_BLAKE2S
REGISTER_HASH(&blake2b_160_desc);
REGISTER_HASH(&blake2b_256_desc);
REGISTER_HASH(&blake2b_384_desc);
REGISTER_HASH(&blake2b_512_desc);
#endif
#ifdef LTC_CHC_HASH
REGISTER_HASH(&chc_desc);
LTC_ARGCHK((err = chc_register(find_cipher_any("aes", 8, 16))) == CRYPT_OK);
#endif
return err;
}

View File

@ -0,0 +1,45 @@
/* 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.
*/
#include "tomcrypt.h"
/**
@file crypt_register_all_prngs.c
Steffen Jaeckel
*/
#define REGISTER_PRNG(h) do {\
LTC_ARGCHK((err = register_prng(h) == -1 ? CRYPT_ERROR : CRYPT_OK) == CRYPT_OK); \
} while(0)
int register_all_prngs(void)
{
int err = CRYPT_NOP;
#ifdef LTC_YARROW
REGISTER_PRNG(&yarrow_desc);
#endif
#ifdef LTC_FORTUNA
REGISTER_PRNG(&fortuna_desc);
#endif
#ifdef LTC_RC4
REGISTER_PRNG(&rc4_desc);
#endif
#ifdef LTC_CHACHA20_PRNG
REGISTER_PRNG(&chacha20_prng_desc);
#endif
#ifdef LTC_SOBER128
REGISTER_PRNG(&sober128_desc);
#endif
#ifdef LTC_SPRNG
REGISTER_PRNG(&sprng_desc);
#endif
return err;
}