From 8a87f4509c2349b26e6c8a07e24af8f659474b15 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 9 Jan 2020 07:21:30 +0000 Subject: [PATCH] Expose key generation functions in testcrypt. They're not much use for 'real' key generation, since like all the other randomness-using testcrypt functions, they need you to have explicitly queued up some random data. But for generating keys for test purposes, they have the great virtue that they deliver the key in the internal format, where we can generate all the various public and private blobs from it as well as the on-disk formats. A minor change to one of the keygen functions itself: rsa_generate now fills in the 'bits' and 'bytes' fields of the returned RSAKey, without which it didn't actually work to try to generate a public blob from it. (We'd never noticed before, because no previous client of rsa_generate even tried that.) --- Recipe | 15 +++++++++------ sshrsag.c | 3 +++ testcrypt.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ testcrypt.h | 11 ++++++++++- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/Recipe b/Recipe index 320476fc..d6171b5d 100644 --- a/Recipe +++ b/Recipe @@ -322,6 +322,9 @@ W_BE_NOSSH = be_nos_s winser nocproxy U_BE_ALL = be_all_s uxser cproxy U_BE_NOSSH = be_nos_s uxser nocproxy +# Auxiliary crypto modules used by key generators. +KEYGEN = sshrsag sshdssg sshecdsag + # ------------------------------------------------------------ # Definitions of actual programs. The program name, followed by a # colon, followed by a list of objects. Also in the list may be the @@ -343,11 +346,11 @@ pageant : [G] winpgnt pageant sshrsa sshpubk sshdes ARITH sshmd5 version + sshauxcrypt sshhmac wincapi winnps winnpc winhsock errsock winnet + winhandl callback be_misc winselgui winhandl LIBS -puttygen : [G] winpgen sshrsag sshdssg sshprime sshdes ARITH sshmd5 version +puttygen : [G] winpgen KEYGEN sshprime sshdes ARITH sshmd5 version + sshrand winnoise sshsha winstore MISC winctrls sshrsa sshdss winmisc + sshpubk sshaes sshsh256 sshsh512 IMPORT winutils puttygen.res + tree234 notiming winhelp winnojmp CONF LIBS wintime sshecc sshprng - + sshecdsag sshauxcrypt sshhmac winsecur winmiscs + + sshauxcrypt sshhmac winsecur winmiscs pterm : [X] GTKTERM uxmisc misc ldisc settings uxpty uxsel BE_NONE uxstore + uxsignal CHARSET cmdline uxpterm version time xpmpterm xpmptcfg @@ -362,10 +365,10 @@ puttytel : [X] GTKTERM uxmisc misc ldisc settings uxsel U_BE_NOSSH plink : [U] uxplink uxcons NONSSH UXSSH U_BE_ALL logging UXMISC uxsignal + ux_x11 noterm uxnogtk sessprep cmdline -PUTTYGEN_UNIX = sshrsag sshdssg sshprime sshdes ARITH sshmd5 version sshprng +PUTTYGEN_UNIX = KEYGEN sshprime sshdes ARITH sshmd5 version sshprng + sshrand uxnoise sshsha MISC sshrsa sshdss uxcons uxstore uxmisc + sshpubk sshaes sshsh256 sshsh512 IMPORT puttygen.res time tree234 - + uxgen notiming CONF sshecc sshecdsag uxnogtk sshauxcrypt sshhmac + + uxgen notiming CONF sshecc uxnogtk sshauxcrypt sshhmac + uxpoll uxutils puttygen : [U] cmdgen PUTTYGEN_UNIX cgtest : [UT] cgtest PUTTYGEN_UNIX @@ -390,9 +393,9 @@ osxlaunch : [UT] osxlaunch fuzzterm : [UT] UXTERM CHARSET MISC version uxmisc uxucs fuzzterm time settings + uxstore be_none uxnogtk memory testcrypt : [UT] testcrypt SSHCRYPTO sshprng sshprime marshal utils - + memory tree234 uxutils + + memory tree234 uxutils KEYGEN testcrypt : [C] testcrypt SSHCRYPTO sshprng sshprime marshal utils - + memory tree234 winmiscs + + memory tree234 winmiscs KEYGEN testsc : [UT] testsc SSHCRYPTO marshal utils memory tree234 wildcard + sshmac uxutils testzlib : [UT] testzlib sshzlib utils marshal memory diff --git a/sshrsag.c b/sshrsag.c index fe96c994..c3e39f63 100644 --- a/sshrsag.c +++ b/sshrsag.c @@ -124,5 +124,8 @@ int rsa_generate(RSAKey *key, int bits, progfn_t pfn, key->q = q; key->iqmp = iqmp; + key->bits = mp_get_nbits(modulus); + key->bytes = (key->bits + 7) / 8; + return 1; } diff --git a/testcrypt.c b/testcrypt.c index 37eb6869..49fa72e7 100644 --- a/testcrypt.c +++ b/testcrypt.c @@ -920,6 +920,50 @@ mp_int *primegen_wrapper( } #define primegen primegen_wrapper +RSAKey *rsa1_generate(int bits) +{ + RSAKey *rsakey = snew(RSAKey); + rsa_generate(rsakey, bits, no_progress, NULL); + rsakey->comment = NULL; + return rsakey; +} + +ssh_key *rsa_generate_wrapper(int bits) +{ + return &rsa1_generate(bits)->sshk; +} +#define rsa_generate rsa_generate_wrapper + +ssh_key *dsa_generate_wrapper(int bits) +{ + struct dss_key *dsskey = snew(struct dss_key); + dsa_generate(dsskey, bits, no_progress, NULL); + return &dsskey->sshk; +} +#define dsa_generate dsa_generate_wrapper + +ssh_key *ecdsa_generate_wrapper(int bits) +{ + struct ecdsa_key *ek = snew(struct ecdsa_key); + if (!ecdsa_generate(ek, bits, no_progress, NULL)) { + sfree(ek); + return NULL; + } + return &ek->sshk; +} +#define ecdsa_generate ecdsa_generate_wrapper + +ssh_key *eddsa_generate_wrapper(int bits) +{ + struct eddsa_key *ek = snew(struct eddsa_key); + if (!eddsa_generate(ek, bits, no_progress, NULL)) { + sfree(ek); + return NULL; + } + return &ek->sshk; +} +#define eddsa_generate eddsa_generate_wrapper + #define VALTYPE_TYPEDEF(n,t,f) \ typedef t TD_val_##n; \ typedef t *TD_out_val_##n; diff --git a/testcrypt.h b/testcrypt.h index a04672c3..e4edcc37 100644 --- a/testcrypt.h +++ b/testcrypt.h @@ -228,6 +228,16 @@ FUNC1(void, prng_seed_finish, val_prng) FUNC2(val_string, prng_read, val_prng, uint) FUNC3(void, prng_add_entropy, val_prng, uint, val_string_ptrlen) +/* + * Key generation functions. + */ +FUNC1(val_key, rsa_generate, uint) +FUNC1(val_key, dsa_generate, uint) +FUNC1(opt_val_key, ecdsa_generate, uint) +FUNC1(opt_val_key, eddsa_generate, uint) +FUNC1(val_rsa, rsa1_generate, uint) +FUNC5(val_mpint, primegen, uint, uint, uint, val_mpint, uint) + /* * Miscellaneous. */ @@ -245,7 +255,6 @@ FUNC1(uint, crc32_rfc1662, val_string_ptrlen) FUNC1(uint, crc32_ssh1, val_string_ptrlen) FUNC2(uint, crc32_update, uint, val_string_ptrlen) FUNC2(boolean, crcda_detect, val_string_ptrlen, val_string_ptrlen) -FUNC5(val_mpint, primegen, uint, uint, uint, val_mpint, uint) /* * These functions aren't part of PuTTY's own API, but are additions