Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion demos/latex-tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,94 @@ static int s_to_lower(const char *in, char *out, unsigned long *outlen)
return CRYPT_OK;
}

static int s_to_upper(const char *in, char *out, unsigned long *outlen)
{
unsigned long n;
for (n = 0; n < *outlen && in[n]; ++n) {
out[n] = toupper(in[n]);
}
if (n == *outlen)
return CRYPT_BUFFER_OVERFLOW;
out[n] = '\0';
*outlen = n;
return CRYPT_OK;
}

static int s_to_desc(const char *in, char *out, unsigned long outlen)
{
unsigned long n, m;
if (outlen < 6) goto err_exit;
XMEMCPY(out, "\\code{", 6);
m = 6;
for (n = 0; m < outlen - 1 && in[n]; ++n, ++m) {
if (in[n] == '-' || in[n] == '_') {
out[m++] = '\\';
out[m] = '_';
} else
out[m] = tolower(in[n]);
}
if (outlen <= m) goto err_exit;
XMEMCPY(&out[m], "\\_desc}", 8);
m += 8;
out[m] = '\0';
return CRYPT_OK;
err_exit:
fprintf(stderr, "Error: Can't print descriptor %s\n", in);
exit(1);
}

struct desc {
void *orig;
char desc[64];
};

static int hash_sorter(const void *a, const void *b)
{
const struct ltc_hash_descriptor *A, *B;
A = ((const struct desc*)a)->orig;
B = ((const struct desc*)b)->orig;
if (A->hashsize < B->hashsize) return 1;
if (A->hashsize > B->hashsize) return -1;
if (A->ID < B->ID) return -1;
if (A->ID > B->ID) return 1;
return 0;
}

int main(int argc, char **argv)
{
struct desc descs[TAB_SIZE + 1] = {0};
int ids[TAB_SIZE + 1] = {0};
unsigned long n;
if (argc > 1 && strstr(argv[1], "-h"))
die(0);
printf("PEM ciphers:\n\n");
printf("libtomcrypt latex tables\n");

printf("\nhash descriptors:\n\n");
register_all_hashes();

for (n = 0; hash_descriptor[n].name != NULL && n < TAB_SIZE; ++n) {
if (hash_descriptor[n].ID > TAB_SIZE) {
printf("Hash descriptor '%s' has invalid ID %d\n", hash_descriptor[n].name, hash_descriptor[n].ID);
return EXIT_FAILURE;
}
if (ids[hash_descriptor[n].ID] != 0) {
printf("Hash descriptor '%s' has duplicate ID %d\n", hash_descriptor[n].name, hash_descriptor[n].ID);
return EXIT_FAILURE;
}
ids[hash_descriptor[n].ID] = 1;
descs[n].orig = &hash_descriptor[n];
s_to_desc(hash_descriptor[n].name, descs[n].desc, sizeof(descs[n].desc));
}
qsort(descs, n, sizeof(struct desc), &hash_sorter);
for (n = 0; hash_descriptor[n].name != NULL && n < TAB_SIZE; ++n) {
char nbuf[32] = {0};
struct ltc_hash_descriptor *p = descs[n].orig;
unsigned long nlen = sizeof(nbuf);
s_to_upper(p->name, nbuf, &nlen);
printf("\\hline %-17s & %-28s & %lu & %d \\\\\n", nbuf, descs[n].desc, p->hashsize, p->ID);
}

printf("\nPEM ciphers:\n\n");
for (n = 0; n < pem_dek_infos_num; ++n) {
char nbuf[32] = {0};
size_t nlen = strlen(pem_dek_infos[n].name);
Expand Down
70 changes: 35 additions & 35 deletions doc/crypt.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2931,41 +2931,41 @@ \subsection{Hash Registration}
\begin{figure}[H]
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline \textbf{Name} & \textbf{Descriptor Name} & \textbf{Size of Message Digest (bytes)} & \textbf{Id} \\
\hline WHIRLPOOL & whirlpool\_desc & 64 & 11 \\
\hline Keccak512 & keccak\_512\_desc & 64 & 32 \\
\hline SHA3-512 & sha3\_512\_desc & 64 & 20 \\
\hline SHA-512 & sha512\_desc & 64 & 5 \\
\hline BLAKE2B-512 & blake2b\_512\_desc & 64 & 28 \\
\hline Keccak384 & keccak\_384\_desc & 48 & 31 \\
\hline SHA3-384 & sha3\_384\_desc & 48 & 19 \\
\hline SHA-384 & sha384\_desc & 48 & 4 \\
\hline BLAKE2B-384 & blake2b\_384\_desc & 48 & 27 \\
\hline RIPEMD-320 & rmd160\_desc & 40 & 14 \\
\hline SHA-512/256 & sha512\_256\_desc & 32 & 16 \\
\hline Keccak256 & keccak\_256\_desc & 32 & 30 \\
\hline SHA3-256 & sha3\_256\_desc & 32 & 18 \\
\hline SHA-256 & sha256\_desc & 32 & 0 \\
\hline RIPEMD-256 & rmd160\_desc & 32 & 13 \\
\hline BLAKE2S-256 & blake2s\_256\_desc & 32 & 24 \\
\hline BLAKE2B-256 & blake2b\_256\_desc & 32 & 26 \\
\hline SHA-512/224 & sha512\_224\_desc & 28 & 15 \\
\hline Keccak224 & keccak\_224\_desc & 28 & 29 \\
\hline SHA3-224 & sha3\_224\_desc & 28 & 17 \\
\hline SHA-224 & sha224\_desc & 28 & 10 \\
\hline BLAKE2S-224 & blake2s\_224\_desc & 28 & 23 \\
\hline TIGER-192 & tiger\_desc & 24 & 1 \\
\hline TIGER2-192 & tiger2\_desc & 24 & 33 \\
\hline SHA-1 & sha1\_desc & 20 & 2 \\
\hline RIPEMD-160 & rmd160\_desc & 20 & 9 \\
\hline BLAKE2S-160 & blake2s\_160\_desc & 20 & 22 \\
\hline BLAKE2B-160 & blake2b\_160\_desc & 20 & 25 \\
\hline RIPEMD-128 & rmd128\_desc & 16 & 8 \\
\hline MD5 & md5\_desc & 16 & 3 \\
\hline MD4 & md4\_desc & 16 & 6 \\
\hline MD2 & md2\_desc & 16 & 7 \\
\hline BLAKE2S-128 & blake2s\_128\_desc & 16 & 21 \\
\hline
\hline \textbf{Name} & \textbf{Descriptor Name} & \textbf{Size of Message Digest (bytes)} & \textbf{Id} \\
\hline SHA512 & \code{sha512\_desc} & 64 & 5 \\
\hline WHIRLPOOL & \code{whirlpool\_desc} & 64 & 11 \\
\hline SHA3-512 & \code{sha3\_512\_desc} & 64 & 20 \\
\hline BLAKE2B-512 & \code{blake2b\_512\_desc} & 64 & 28 \\
\hline KECCAK512 & \code{keccak512\_desc} & 64 & 32 \\
\hline SHA384 & \code{sha384\_desc} & 48 & 4 \\
\hline SHA3-384 & \code{sha3\_384\_desc} & 48 & 19 \\
\hline BLAKE2B-384 & \code{blake2b\_384\_desc} & 48 & 27 \\
\hline KECCAK384 & \code{keccak384\_desc} & 48 & 31 \\
\hline RMD320 & \code{rmd320\_desc} & 40 & 14 \\
\hline SHA256 & \code{sha256\_desc} & 32 & 0 \\
\hline RMD256 & \code{rmd256\_desc} & 32 & 13 \\
\hline SHA512-256 & \code{sha512\_256\_desc} & 32 & 16 \\
\hline SHA3-256 & \code{sha3\_256\_desc} & 32 & 18 \\
\hline BLAKE2S-256 & \code{blake2s\_256\_desc} & 32 & 24 \\
\hline BLAKE2B-256 & \code{blake2b\_256\_desc} & 32 & 26 \\
\hline KECCAK256 & \code{keccak256\_desc} & 32 & 30 \\
\hline SHA224 & \code{sha224\_desc} & 28 & 10 \\
\hline SHA512-224 & \code{sha512\_224\_desc} & 28 & 15 \\
\hline SHA3-224 & \code{sha3\_224\_desc} & 28 & 17 \\
\hline BLAKE2S-224 & \code{blake2s\_224\_desc} & 28 & 23 \\
\hline KECCAK224 & \code{keccak224\_desc} & 28 & 29 \\
\hline TIGER & \code{tiger\_desc} & 24 & 1 \\
\hline TIGER2 & \code{tiger2\_desc} & 24 & 33 \\
\hline SHA1 & \code{sha1\_desc} & 20 & 2 \\
\hline RMD160 & \code{rmd160\_desc} & 20 & 9 \\
\hline BLAKE2S-160 & \code{blake2s\_160\_desc} & 20 & 22 \\
\hline BLAKE2B-160 & \code{blake2b\_160\_desc} & 20 & 25 \\
\hline MD5 & \code{md5\_desc} & 16 & 3 \\
\hline MD4 & \code{md4\_desc} & 16 & 6 \\
\hline MD2 & \code{md2\_desc} & 16 & 7 \\
\hline RMD128 & \code{rmd128\_desc} & 16 & 8 \\
\hline BLAKE2S-128 & \code{blake2s\_128\_desc} & 16 & 21 \\
\hline
\end{tabular}
\end{center}
\caption{Built--In Software Hashes}
Expand Down
15 changes: 2 additions & 13 deletions src/hashes/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const struct ltc_hash_descriptor sha1_portable_desc =
{ 1, 3, 14, 3, 2, 26, },
6,

&sha1_c_init,
&sha1_init,
&sha1_c_process,
&sha1_c_done,
&sha1_c_test,
Expand Down Expand Up @@ -186,18 +186,7 @@ static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
*/
int sha1_c_init(hash_state * md)
{
LTC_ARGCHK(md != NULL);

md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);

md->sha1.state[0] = 0x67452301UL;
md->sha1.state[1] = 0xefcdab89UL;
md->sha1.state[2] = 0x98badcfeUL;
md->sha1.state[3] = 0x10325476UL;
md->sha1.state[4] = 0xc3d2e1f0UL;
md->sha1.curlen = 0;
md->sha1.length = 0;
return CRYPT_OK;
return sha1_init(md);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/hashes/sha1_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ static LTC_INLINE int s_sha1_x86_is_supported(void)
*/
int sha1_init(hash_state * md)
{
#if defined LTC_SHA1_X86
if(s_sha1_x86_is_supported()) {
return sha1_x86_init(md);
}
#endif
return sha1_c_init(md);
LTC_ARGCHK(md != NULL);

md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);

md->sha1.state[0] = 0x67452301UL;
md->sha1.state[1] = 0xefcdab89UL;
md->sha1.state[2] = 0x98badcfeUL;
md->sha1.state[3] = 0x10325476UL;
md->sha1.state[4] = 0xc3d2e1f0UL;
md->sha1.curlen = 0;
md->sha1.length = 0;
return CRYPT_OK;
}

/**
Expand Down
15 changes: 2 additions & 13 deletions src/hashes/sha1_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const struct ltc_hash_descriptor sha1_x86_desc =
{ 1, 3, 14, 3, 2, 26, },
6,

&sha1_x86_init,
&sha1_init,
&sha1_x86_process,
&sha1_x86_done,
&sha1_x86_test,
Expand Down Expand Up @@ -198,18 +198,7 @@ static int s_sha1_x86_compress(hash_state *md, const unsigned char *buf)
*/
int sha1_x86_init(hash_state * md)
{
LTC_ARGCHK(md != NULL);

md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);

md->sha1.state[0] = 0x67452301UL;
md->sha1.state[1] = 0xefcdab89UL;
md->sha1.state[2] = 0x98badcfeUL;
md->sha1.state[3] = 0x10325476UL;
md->sha1.state[4] = 0xc3d2e1f0UL;
md->sha1.curlen = 0;
md->sha1.length = 0;
return CRYPT_OK;
return sha1_init(md);
}

/**
Expand Down
22 changes: 4 additions & 18 deletions src/hashes/sha2/sha224.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const struct ltc_hash_descriptor sha224_portable_desc =
{ 2, 16, 840, 1, 101, 3, 4, 2, 4, },
9,

&sha224_c_init,
&sha224_init,
&sha256_c_process,
&sha224_c_done,
&sha224_test,
&sha224_c_test,
NULL
};

Expand All @@ -35,21 +35,7 @@ const struct ltc_hash_descriptor sha224_portable_desc =
*/
int sha224_c_init(hash_state * md)
{
LTC_ARGCHK(md != NULL);

md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);

md->sha256.curlen = 0;
md->sha256.length = 0;
md->sha256.state[0] = 0xc1059ed8UL;
md->sha256.state[1] = 0x367cd507UL;
md->sha256.state[2] = 0x3070dd17UL;
md->sha256.state[3] = 0xf70e5939UL;
md->sha256.state[4] = 0xffc00b31UL;
md->sha256.state[5] = 0x68581511UL;
md->sha256.state[6] = 0x64f98fa7UL;
md->sha256.state[7] = 0xbefa4fa4UL;
return CRYPT_OK;
return sha224_init(md);
}

/**
Expand All @@ -66,7 +52,7 @@ int sha224_c_done(hash_state * md, unsigned char *out)
LTC_ARGCHK(md != NULL);
LTC_ARGCHK(out != NULL);

err = sha256_done(md, buf);
err = sha256_c_done(md, buf);
XMEMCPY(out, buf, 28);
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
Expand Down
21 changes: 15 additions & 6 deletions src/hashes/sha2/sha224_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@ static LTC_INLINE int s_sha224_x86_is_supported(void)
*/
int sha224_init(hash_state * md)
{
#if defined LTC_SHA224_X86
if(s_sha224_x86_is_supported()) {
return sha224_x86_init(md);
}
#endif
return sha224_c_init(md);
LTC_ARGCHK(md != NULL);

md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);

md->sha256.curlen = 0;
md->sha256.length = 0;
md->sha256.state[0] = 0xc1059ed8UL;
md->sha256.state[1] = 0x367cd507UL;
md->sha256.state[2] = 0x3070dd17UL;
md->sha256.state[3] = 0xf70e5939UL;
md->sha256.state[4] = 0xffc00b31UL;
md->sha256.state[5] = 0x68581511UL;
md->sha256.state[6] = 0x64f98fa7UL;
md->sha256.state[7] = 0xbefa4fa4UL;
return CRYPT_OK;
}

/**
Expand Down
18 changes: 2 additions & 16 deletions src/hashes/sha2/sha224_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const struct ltc_hash_descriptor sha224_x86_desc =
{ 2, 16, 840, 1, 101, 3, 4, 2, 4, },
9,

&sha224_x86_init,
&sha224_init,
&sha256_x86_process,
&sha224_x86_done,
&sha224_x86_test,
Expand All @@ -35,21 +35,7 @@ const struct ltc_hash_descriptor sha224_x86_desc =
*/
int sha224_x86_init(hash_state * md)
{
LTC_ARGCHK(md != NULL);

md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);

md->sha256.curlen = 0;
md->sha256.length = 0;
md->sha256.state[0] = 0xc1059ed8UL;
md->sha256.state[1] = 0x367cd507UL;
md->sha256.state[2] = 0x3070dd17UL;
md->sha256.state[3] = 0xf70e5939UL;
md->sha256.state[4] = 0xffc00b31UL;
md->sha256.state[5] = 0x68581511UL;
md->sha256.state[6] = 0x64f98fa7UL;
md->sha256.state[7] = 0xbefa4fa4UL;
return CRYPT_OK;
return sha224_init(md);
}

/**
Expand Down
18 changes: 2 additions & 16 deletions src/hashes/sha2/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const struct ltc_hash_descriptor sha256_portable_desc =
{ 2, 16, 840, 1, 101, 3, 4, 2, 1, },
9,

&sha256_c_init,
&sha256_init,
&sha256_c_process,
&sha256_c_done,
&sha256_c_test,
Expand Down Expand Up @@ -233,21 +233,7 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
*/
int sha256_c_init(hash_state * md)
{
LTC_ARGCHK(md != NULL);

md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);

md->sha256.curlen = 0;
md->sha256.length = 0;
md->sha256.state[0] = 0x6A09E667UL;
md->sha256.state[1] = 0xBB67AE85UL;
md->sha256.state[2] = 0x3C6EF372UL;
md->sha256.state[3] = 0xA54FF53AUL;
md->sha256.state[4] = 0x510E527FUL;
md->sha256.state[5] = 0x9B05688CUL;
md->sha256.state[6] = 0x1F83D9ABUL;
md->sha256.state[7] = 0x5BE0CD19UL;
return CRYPT_OK;
return sha256_init(md);
}

/**
Expand Down
Loading
Loading