123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821 |
- #define CRYPTO_ED25519_PRIVATE
- #include "orconfig.h"
- #ifdef HAVE_SYS_STAT_H
- #include <sys/stat.h>
- #endif
- #include "lib/ctime/di_ops.h"
- #include "lib/crypt_ops/crypto_curve25519.h"
- #include "lib/crypt_ops/crypto_digest.h"
- #include "lib/crypt_ops/crypto_ed25519.h"
- #include "lib/crypt_ops/crypto_format.h"
- #include "lib/crypt_ops/crypto_rand.h"
- #include "lib/crypt_ops/crypto_util.h"
- #include "lib/log/log.h"
- #include "lib/log/util_bug.h"
- #include "lib/encoding/binascii.h"
- #include "lib/string/util_string.h"
- #include "ed25519/ref10/ed25519_ref10.h"
- #include "ed25519/donna/ed25519_donna_tor.h"
- #include <string.h>
- #include <errno.h>
- static void pick_ed25519_impl(void);
- typedef struct {
- int (*selftest)(void);
- int (*seckey)(unsigned char *);
- int (*seckey_expand)(unsigned char *, const unsigned char *);
- int (*pubkey)(unsigned char *, const unsigned char *);
- int (*keygen)(unsigned char *, unsigned char *);
- int (*open)(const unsigned char *, const unsigned char *, size_t, const
- unsigned char *);
- int (*sign)(unsigned char *, const unsigned char *, size_t,
- const unsigned char *, const unsigned char *);
- int (*open_batch)(const unsigned char **, size_t *, const unsigned char **,
- const unsigned char **, size_t, int *);
- int (*blind_secret_key)(unsigned char *, const unsigned char *,
- const unsigned char *);
- int (*blind_public_key)(unsigned char *, const unsigned char *,
- const unsigned char *);
- int (*pubkey_from_curve25519_pubkey)(unsigned char *, const unsigned char *,
- int);
- int (*ed25519_scalarmult_with_group_order)(unsigned char *,
- const unsigned char *);
- } ed25519_impl_t;
- static const ed25519_impl_t impl_ref10 = {
- NULL,
- ed25519_ref10_seckey,
- ed25519_ref10_seckey_expand,
- ed25519_ref10_pubkey,
- ed25519_ref10_keygen,
- ed25519_ref10_open,
- ed25519_ref10_sign,
- NULL,
- ed25519_ref10_blind_secret_key,
- ed25519_ref10_blind_public_key,
- ed25519_ref10_pubkey_from_curve25519_pubkey,
- ed25519_ref10_scalarmult_with_group_order,
- };
- static const ed25519_impl_t impl_donna = {
- ed25519_donna_selftest,
- ed25519_donna_seckey,
- ed25519_donna_seckey_expand,
- ed25519_donna_pubkey,
- ed25519_donna_keygen,
- ed25519_donna_open,
- ed25519_donna_sign,
- ed25519_sign_open_batch_donna,
- ed25519_donna_blind_secret_key,
- ed25519_donna_blind_public_key,
- ed25519_donna_pubkey_from_curve25519_pubkey,
- ed25519_donna_scalarmult_with_group_order,
- };
- static const ed25519_impl_t *ed25519_impl = NULL;
- static inline const ed25519_impl_t *
- get_ed_impl(void)
- {
- if (BUG(ed25519_impl == NULL)) {
- pick_ed25519_impl();
- }
- return ed25519_impl;
- }
- #ifdef TOR_UNIT_TESTS
- static const ed25519_impl_t *saved_ed25519_impl = NULL;
- void
- crypto_ed25519_testing_force_impl(const char *name)
- {
- tor_assert(saved_ed25519_impl == NULL);
- saved_ed25519_impl = ed25519_impl;
- if (! strcmp(name, "donna")) {
- ed25519_impl = &impl_donna;
- } else {
- tor_assert(!strcmp(name, "ref10"));
- ed25519_impl = &impl_ref10;
- }
- }
- void
- crypto_ed25519_testing_restore_impl(void)
- {
- ed25519_impl = saved_ed25519_impl;
- saved_ed25519_impl = NULL;
- }
- #endif
- int
- ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
- int extra_strong)
- {
- int r;
- uint8_t seed[32];
- if (extra_strong)
- crypto_strongest_rand(seed, sizeof(seed));
- else
- crypto_rand((char*)seed, sizeof(seed));
- r = get_ed_impl()->seckey_expand(seckey_out->seckey, seed);
- memwipe(seed, 0, sizeof(seed));
- return r < 0 ? -1 : 0;
- }
- int
- ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
- const uint8_t *seed)
- {
- if (get_ed_impl()->seckey_expand(seckey_out->seckey, seed) < 0)
- return -1;
- return 0;
- }
- int
- ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
- const ed25519_secret_key_t *seckey)
- {
- if (get_ed_impl()->pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
- return -1;
- return 0;
- }
- int
- ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
- {
- if (ed25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
- return -1;
- if (ed25519_public_key_generate(&keypair_out->pubkey,
- &keypair_out->seckey)<0)
- return -1;
- return 0;
- }
- int
- ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
- {
- return tor_mem_is_zero((char*)pubkey->pubkey, ED25519_PUBKEY_LEN);
- }
- static uint8_t *
- get_prefixed_msg(const uint8_t *msg, size_t msg_len,
- const char *prefix_str,
- size_t *final_msg_len_out)
- {
- size_t prefixed_msg_len, prefix_len;
- uint8_t *prefixed_msg;
- tor_assert(prefix_str);
- tor_assert(final_msg_len_out);
- prefix_len = strlen(prefix_str);
-
- if (msg_len > SIZE_T_CEILING - prefix_len) {
- return NULL;
- }
- prefixed_msg_len = msg_len + prefix_len;
- prefixed_msg = tor_malloc_zero(prefixed_msg_len);
- memcpy(prefixed_msg, prefix_str, prefix_len);
- memcpy(prefixed_msg + prefix_len, msg, msg_len);
- *final_msg_len_out = prefixed_msg_len;
- return prefixed_msg;
- }
- int
- ed25519_sign(ed25519_signature_t *signature_out,
- const uint8_t *msg, size_t len,
- const ed25519_keypair_t *keypair)
- {
- if (get_ed_impl()->sign(signature_out->sig, msg, len,
- keypair->seckey.seckey,
- keypair->pubkey.pubkey) < 0) {
- return -1;
- }
- return 0;
- }
- MOCK_IMPL(int,
- ed25519_sign_prefixed,(ed25519_signature_t *signature_out,
- const uint8_t *msg, size_t msg_len,
- const char *prefix_str,
- const ed25519_keypair_t *keypair))
- {
- int retval;
- size_t prefixed_msg_len;
- uint8_t *prefixed_msg;
- tor_assert(prefix_str);
- prefixed_msg = get_prefixed_msg(msg, msg_len, prefix_str,
- &prefixed_msg_len);
- if (BUG(!prefixed_msg)) {
-
- log_warn(LD_GENERAL, "Failed to get prefixed msg.");
- return -1;
-
- }
- retval = ed25519_sign(signature_out,
- prefixed_msg, prefixed_msg_len,
- keypair);
- tor_free(prefixed_msg);
- return retval;
- }
- MOCK_IMPL(int,
- ed25519_checksig,(const ed25519_signature_t *signature,
- const uint8_t *msg, size_t len,
- const ed25519_public_key_t *pubkey))
- {
- return
- get_ed_impl()->open(signature->sig, msg, len, pubkey->pubkey) < 0 ? -1 : 0;
- }
- int
- ed25519_checksig_prefixed(const ed25519_signature_t *signature,
- const uint8_t *msg, size_t msg_len,
- const char *prefix_str,
- const ed25519_public_key_t *pubkey)
- {
- int retval;
- size_t prefixed_msg_len;
- uint8_t *prefixed_msg;
- prefixed_msg = get_prefixed_msg(msg, msg_len, prefix_str,
- &prefixed_msg_len);
- if (BUG(!prefixed_msg)) {
-
- log_warn(LD_GENERAL, "Failed to get prefixed msg.");
- return -1;
-
- }
- retval = ed25519_checksig(signature,
- prefixed_msg, prefixed_msg_len,
- pubkey);
- tor_free(prefixed_msg);
- return retval;
- }
- MOCK_IMPL(int,
- ed25519_checksig_batch,(int *okay_out,
- const ed25519_checkable_t *checkable,
- int n_checkable))
- {
- int i, res;
- const ed25519_impl_t *impl = get_ed_impl();
- if (impl->open_batch == NULL) {
-
- res = 0;
- for (i = 0; i < n_checkable; ++i) {
- const ed25519_checkable_t *ch = &checkable[i];
- int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
- if (r < 0)
- --res;
- if (okay_out)
- okay_out[i] = (r == 0);
- }
- } else {
-
- const uint8_t **ms;
- size_t *lens;
- const uint8_t **pks;
- const uint8_t **sigs;
- int *oks;
- int all_ok;
- ms = tor_calloc(n_checkable, sizeof(uint8_t*));
- lens = tor_calloc(n_checkable, sizeof(size_t));
- pks = tor_calloc(n_checkable, sizeof(uint8_t*));
- sigs = tor_calloc(n_checkable, sizeof(uint8_t*));
- oks = okay_out ? okay_out : tor_calloc(n_checkable, sizeof(int));
- for (i = 0; i < n_checkable; ++i) {
- ms[i] = checkable[i].msg;
- lens[i] = checkable[i].len;
- pks[i] = checkable[i].pubkey->pubkey;
- sigs[i] = checkable[i].signature.sig;
- oks[i] = 0;
- }
- res = 0;
- all_ok = impl->open_batch(ms, lens, pks, sigs, n_checkable, oks);
- for (i = 0; i < n_checkable; ++i) {
- if (!oks[i])
- --res;
- }
-
- tor_assert(((res == 0) && !all_ok) || ((res < 0) && all_ok));
- tor_free(ms);
- tor_free(lens);
- tor_free(pks);
- tor_free(sigs);
- if (! okay_out)
- tor_free(oks);
- }
- return res;
- }
- int
- ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
- int *signbit_out,
- const curve25519_keypair_t *inp)
- {
- const char string[] = "Derive high part of ed25519 key from curve25519 key";
- ed25519_public_key_t pubkey_check;
- crypto_digest_t *ctx;
- uint8_t sha512_output[DIGEST512_LEN];
- memcpy(out->seckey.seckey, inp->seckey.secret_key, 32);
- ctx = crypto_digest512_new(DIGEST_SHA512);
- crypto_digest_add_bytes(ctx, (const char*)out->seckey.seckey, 32);
- crypto_digest_add_bytes(ctx, (const char*)string, sizeof(string));
- crypto_digest_get_digest(ctx, (char *)sha512_output, sizeof(sha512_output));
- crypto_digest_free(ctx);
- memcpy(out->seckey.seckey + 32, sha512_output, 32);
- ed25519_public_key_generate(&out->pubkey, &out->seckey);
- *signbit_out = out->pubkey.pubkey[31] >> 7;
- ed25519_public_key_from_curve25519_public_key(&pubkey_check, &inp->pubkey,
- *signbit_out);
- tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
- memwipe(&pubkey_check, 0, sizeof(pubkey_check));
- memwipe(sha512_output, 0, sizeof(sha512_output));
- return 0;
- }
- int
- ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey,
- const curve25519_public_key_t *pubkey_in,
- int signbit)
- {
- return get_ed_impl()->pubkey_from_curve25519_pubkey(pubkey->pubkey,
- pubkey_in->public_key,
- signbit);
- }
- int
- ed25519_keypair_blind(ed25519_keypair_t *out,
- const ed25519_keypair_t *inp,
- const uint8_t *param)
- {
- ed25519_public_key_t pubkey_check;
- get_ed_impl()->blind_secret_key(out->seckey.seckey,
- inp->seckey.seckey, param);
- if (ed25519_public_blind(&pubkey_check, &inp->pubkey, param) < 0) {
- return -1;
- }
- ed25519_public_key_generate(&out->pubkey, &out->seckey);
- tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
- memwipe(&pubkey_check, 0, sizeof(pubkey_check));
- return 0;
- }
- int
- ed25519_public_blind(ed25519_public_key_t *out,
- const ed25519_public_key_t *inp,
- const uint8_t *param)
- {
- return get_ed_impl()->blind_public_key(out->pubkey, inp->pubkey, param);
- }
- int
- ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey,
- const char *filename,
- const char *tag)
- {
- return crypto_write_tagged_contents_to_file(filename,
- "ed25519v1-secret",
- tag,
- seckey->seckey,
- sizeof(seckey->seckey));
- }
- int
- ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out,
- char **tag_out,
- const char *filename)
- {
- ssize_t len;
- len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-secret",
- tag_out, seckey_out->seckey,
- sizeof(seckey_out->seckey));
- if (len == sizeof(seckey_out->seckey)) {
- return 0;
- } else if (len >= 0) {
- errno = EINVAL;
- }
- tor_free(*tag_out);
- return -1;
- }
- int
- ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey,
- const char *filename,
- const char *tag)
- {
- return crypto_write_tagged_contents_to_file(filename,
- "ed25519v1-public",
- tag,
- pubkey->pubkey,
- sizeof(pubkey->pubkey));
- }
- int
- ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
- char **tag_out,
- const char *filename)
- {
- ssize_t len;
- len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-public",
- tag_out, pubkey_out->pubkey,
- sizeof(pubkey_out->pubkey));
- if (len == sizeof(pubkey_out->pubkey)) {
- return 0;
- } else if (len >= 0) {
- errno = EINVAL;
- }
- tor_free(*tag_out);
- return -1;
- }
- void
- ed25519_keypair_free_(ed25519_keypair_t *kp)
- {
- if (! kp)
- return;
- memwipe(kp, 0, sizeof(*kp));
- tor_free(kp);
- }
- int
- ed25519_pubkey_eq(const ed25519_public_key_t *key1,
- const ed25519_public_key_t *key2)
- {
- tor_assert(key1);
- tor_assert(key2);
- return tor_memeq(key1->pubkey, key2->pubkey, ED25519_PUBKEY_LEN);
- }
- void
- ed25519_pubkey_copy(ed25519_public_key_t *dest,
- const ed25519_public_key_t *src)
- {
- tor_assert(dest);
- tor_assert(src);
- memcpy(dest, src, sizeof(ed25519_public_key_t));
- }
- MOCK_IMPL(STATIC int,
- ed25519_impl_spot_check,(void))
- {
- static const uint8_t alicesk[32] = {
- 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
- 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
- 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
- 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
- };
- static const uint8_t alicepk[32] = {
- 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
- 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
- 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
- 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
- };
- static const uint8_t alicemsg[2] = { 0xaf, 0x82 };
- static const uint8_t alicesig[64] = {
- 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
- 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
- 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
- 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
- 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
- 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
- 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
- 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
- };
- const ed25519_impl_t *impl = get_ed_impl();
- uint8_t sk[ED25519_SECKEY_LEN];
- uint8_t pk[ED25519_PUBKEY_LEN];
- uint8_t sig[ED25519_SIG_LEN];
- int r = 0;
-
- if (impl->selftest && impl->selftest() != 0)
- goto fail;
-
-
- if (impl->seckey_expand(sk, alicesk) < 0)
- goto fail;
- if (impl->pubkey(pk, sk) < 0)
- goto fail;
- if (fast_memneq(pk, alicepk, ED25519_PUBKEY_LEN))
- goto fail;
-
- if (impl->sign(sig, alicemsg, sizeof(alicemsg), sk, pk) < 0)
- return -1;
- if (fast_memneq(sig, alicesig, ED25519_SIG_LEN))
- return -1;
- if (impl->open(sig, alicemsg, sizeof(alicemsg), pk) < 0)
- return -1;
-
- goto end;
-
-
- fail:
- r = -1;
-
- end:
- return r;
- }
- void
- ed25519_set_impl_params(int use_donna)
- {
- if (use_donna)
- ed25519_impl = &impl_donna;
- else
- ed25519_impl = &impl_ref10;
- }
- static void
- pick_ed25519_impl(void)
- {
- ed25519_impl = &impl_donna;
- if (ed25519_impl_spot_check() == 0)
- return;
-
- log_warn(LD_CRYPTO, "The Ed25519-donna implementation seems broken; using "
- "the ref10 implementation.");
- ed25519_impl = &impl_ref10;
-
- }
- void
- ed25519_init(void)
- {
- pick_ed25519_impl();
- }
- static int
- ed25519_point_is_identity_element(const uint8_t *point)
- {
-
- static const uint8_t ed25519_identity[32] = {
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- tor_assert(sizeof(ed25519_identity) == ED25519_PUBKEY_LEN);
- return tor_memeq(point, ed25519_identity, sizeof(ed25519_identity));
- }
- int
- ed25519_validate_pubkey(const ed25519_public_key_t *pubkey)
- {
- uint8_t result[32] = {9};
-
- if (ed25519_point_is_identity_element(pubkey->pubkey)) {
- log_warn(LD_CRYPTO, "ed25519 pubkey is the identity");
- return -1;
- }
-
- if (get_ed_impl()->ed25519_scalarmult_with_group_order(result,
- pubkey->pubkey) < 0) {
- log_warn(LD_CRYPTO, "ed25519 group order scalarmult failed");
- return -1;
- }
- if (!ed25519_point_is_identity_element(result)) {
- log_warn(LD_CRYPTO, "ed25519 validation failed");
- return -1;
- }
- return 0;
- }
|