Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amalgamation #596

Draft
wants to merge 30 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1f706b3
add OpenSSH Private Key decryption demo
sjaeckel Sep 26, 2019
ee06437
add rsa-support
sjaeckel Oct 1, 2019
36a8d97
add ecdsa key support
sjaeckel Oct 2, 2019
7215dc0
add ssh private testkeys
sjaeckel Oct 2, 2019
dc86db5
refactor & clean-up
sjaeckel Oct 2, 2019
2706afa
use updated API
sjaeckel Jan 21, 2020
041e5cc
re-factor openssh-privkey demo into library functions
sjaeckel Dec 26, 2021
f18f530
rename file
sjaeckel Jan 6, 2022
330b923
add `pk_get_oid_from_asn1()`
sjaeckel Jan 6, 2022
62f704d
add `der_flexi_sequence_cmp()`
sjaeckel Jan 6, 2022
d0128fc
add `LTC_OID_MAX_STRLEN`
sjaeckel Jan 6, 2022
02097d3
add `pkcs8_get_children()`
sjaeckel Jan 6, 2022
2b14cb0
re-factor PKCS#8 API a bit
sjaeckel Jan 11, 2022
73cdf41
add support for regular PEM files
sjaeckel Jan 12, 2022
d882800
add PEM tests
sjaeckel Jan 12, 2022
a6cda8a
add `dsa_import_pkcs8()`
sjaeckel Jan 14, 2022
375a7a1
add DSA support to PEM decoder
sjaeckel Jan 14, 2022
3988928
Verify that the imported keys match
sjaeckel Jan 16, 2022
6309541
add file-iterator to `test_process_dir()`
sjaeckel Jan 16, 2022
ac33c3a
also test FILE-based PEM API's
sjaeckel Jan 16, 2022
94d02c5
split-up into multiple C files
sjaeckel Jan 22, 2022
c4e6c6a
add support for DH keys
sjaeckel Feb 11, 2022
a821274
disable PEM support on MSVC
sjaeckel Mar 17, 2022
b476c18
clean-up a bit
sjaeckel Aug 4, 2022
0ebd1df
introduce `pka_key_free()`
sjaeckel Aug 4, 2022
066769c
Update docs
sjaeckel Aug 4, 2022
bcb5890
Update makefiles
sjaeckel Aug 4, 2022
d77f632
`undef` Macros after usage & rename duplicate symbols
sjaeckel Aug 5, 2022
55c42f9
prefix the MPI related macros with `ltc_`
sjaeckel Aug 5, 2022
271fbef
minor changes for amalgamation
sjaeckel Aug 5, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# release files
/libtomcrypt-*
/crypt-*
pre_gen/

# suppress output of build process
gcc_[12].txt
Expand All @@ -34,6 +35,8 @@ multi
multi.exe
openssl-enc
openssl-enc.exe
openssh-privkey
openssh-privkey.exe
sizes
sizes.exe
small
Expand Down
72 changes: 72 additions & 0 deletions demos/openssh-privkey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/**
@file openssh-privkey.c
OpenSSH Private Key decryption demo, Steffen Jaeckel
*/

#include <tomcrypt.h>
#include <stdarg.h>

static int verbose = 0;

static void print_err(const char *fmt, ...)
{
va_list args;

if (!verbose) return;

va_start(args, fmt);
vfprintf(stderr, fmt, args);
}

static void die_(int err, int line)
{
verbose = 1;
print_err("%3d: LTC sez %s\n", line, error_to_string(err));
exit(EXIT_FAILURE);
}

#define die(i) do { die_(i, __LINE__); } while(0)
#define DIE(s, ...) do { verbose = 1; print_err("%3d: " s "\n", __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0)

static int password_get(void **p, unsigned long *l, void *u)
{
(void)u;
*p = strdup("abc123");
*l = strlen(*p);
return 0;
}

int main(int argc, char **argv)
{
int err;

FILE *f = NULL;
ltc_pka_key k;
password_ctx pw_ctx = { .callback = password_get };

if ((err = register_all_ciphers()) != CRYPT_OK) {
die(err);
}
if ((err = register_all_hashes()) != CRYPT_OK) {
die(err);
}
if ((err = crypt_mp_init("ltm")) != CRYPT_OK) {
die(err);
}

if (argc > 1) f = fopen(argv[1], "r");
else f = stdin;
if (f == NULL) DIE("fopen sez no");

if ((err = pem_decode_openssh_filehandle(f, &k, &pw_ctx))) {
die(err);
}
return EXIT_SUCCESS;
}

/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */
2 changes: 1 addition & 1 deletion demos/small.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

int main(void)
{
register_cipher(&rijndael_enc_desc);
register_cipher(&rijndael_desc);
register_prng(&yarrow_desc);
register_hash(&sha256_desc);
return 0;
Expand Down
26 changes: 13 additions & 13 deletions demos/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,12 @@ static void time_mult(void)
if (ltc_mp.name == NULL) return;

fprintf(stderr, "Timing Multiplying:\n");
mp_init_multi(&a,&b,&c,NULL);
for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
mp_rand(a, x);
mp_rand(b, x);
ltc_mp_init_multi(&a,&b,&c,NULL);
for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) {
ltc_mp_rand(a, x);
ltc_mp_rand(b, x);

#define DO1 mp_mul(a, b, c);
#define DO1 ltc_mp_mul(a, b, c);
#define DO2 DO1; DO1;

t2 = -1;
Expand All @@ -544,9 +544,9 @@ static void time_mult(void)
t1 = (t_read() - t1)>>1;
if (t1 < t2) t2 = t1;
}
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*LTC_MP_DIGIT_BIT, t2);
}
mp_clear_multi(a,b,c,NULL);
ltc_mp_deinit_multi(a,b,c,NULL);

#undef DO1
#undef DO2
Expand All @@ -561,11 +561,11 @@ static void time_sqr(void)
if (ltc_mp.name == NULL) return;

fprintf(stderr, "Timing Squaring:\n");
mp_init_multi(&a,&b,NULL);
for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
mp_rand(a, x);
ltc_mp_init_multi(&a,&b,NULL);
for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) {
ltc_mp_rand(a, x);

#define DO1 mp_sqr(a, b);
#define DO1 ltc_mp_sqr(a, b);
#define DO2 DO1; DO1;

t2 = -1;
Expand All @@ -576,9 +576,9 @@ static void time_sqr(void)
t1 = (t_read() - t1)>>1;
if (t1 < t2) t2 = t1;
}
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*LTC_MP_DIGIT_BIT, t2);
}
mp_clear_multi(a,b,NULL);
ltc_mp_deinit_multi(a,b,NULL);

#undef DO1
#undef DO2
Expand Down
34 changes: 17 additions & 17 deletions demos/tv_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,31 +669,31 @@ static void ecc_gen(void)
fprintf(out, "ecc vectors. These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
G = ltc_ecc_new_point();
R = ltc_ecc_new_point();
mp_init(&k);
mp_init(&order);
mp_init(&modulus);
mp_init(&a);
ltc_mp_init(&k);
ltc_mp_init(&order);
ltc_mp_init(&modulus);
ltc_mp_init(&a);

for (x = 0; ltc_ecc_curves[x].prime != NULL; x++) {
fprintf(out, "%s\n", ltc_ecc_curves[x].OID);
mp_set(k, 1);
ltc_mp_set(k, 1);

mp_read_radix(order, (char *)ltc_ecc_curves[x].order, 16);
mp_read_radix(modulus, (char *)ltc_ecc_curves[x].prime, 16);
mp_read_radix(a, (char *)ltc_ecc_curves[x].A, 16);
mp_read_radix(G->x, (char *)ltc_ecc_curves[x].Gx, 16);
mp_read_radix(G->y, (char *)ltc_ecc_curves[x].Gy, 16);
mp_set(G->z, 1);
ltc_mp_read_radix(order, (char *)ltc_ecc_curves[x].order, 16);
ltc_mp_read_radix(modulus, (char *)ltc_ecc_curves[x].prime, 16);
ltc_mp_read_radix(a, (char *)ltc_ecc_curves[x].A, 16);
ltc_mp_read_radix(G->x, (char *)ltc_ecc_curves[x].Gx, 16);
ltc_mp_read_radix(G->y, (char *)ltc_ecc_curves[x].Gy, 16);
ltc_mp_set(G->z, 1);

while (mp_cmp(k, order) == LTC_MP_LT) {
while (ltc_mp_cmp(k, order) == LTC_MP_LT) {
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
mp_mul_d(k, 3, k);
ltc_mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
ltc_mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
ltc_mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
ltc_mp_mul_d(k, 3, k);
}
}
mp_clear_multi(k, order, modulus, a, NULL);
ltc_mp_deinit_multi(k, order, modulus, a, NULL);
ltc_ecc_del_point(G);
ltc_ecc_del_point(R);
fclose(out);
Expand Down
Loading