1 /* Copyright (c) 2012-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #ifndef TOR_CRYPTO_ED25519_H
5 #define TOR_CRYPTO_ED25519_H
7 #include "testsupport.h"
9 #include "crypto_curve25519.h"
11 #define ED25519_PUBKEY_LEN 32
12 #define ED25519_SECKEY_LEN 64
13 #define ED25519_SECKEY_SEED_LEN 32
14 #define ED25519_SIG_LEN 64
16 /** An Ed25519 signature. */
18 uint8_t sig
[ED25519_SIG_LEN
];
19 } ed25519_signature_t
;
21 /** An Ed25519 public key */
23 uint8_t pubkey
[ED25519_PUBKEY_LEN
];
24 } ed25519_public_key_t
;
26 /** An Ed25519 secret key */
28 /** Note that we store secret keys in an expanded format that doesn't match
29 * the format from standard ed25519. Ed25519 stores a 32-byte value k and
30 * expands it into a 64-byte H(k), using the first 32 bytes for a multiplier
31 * of the base point, and second 32 bytes as an input to a hash function
32 * for deriving r. But because we implement key blinding, we need to store
33 * keys in the 64-byte expanded form. */
34 uint8_t seckey
[ED25519_SECKEY_LEN
];
35 } ed25519_secret_key_t
;
37 /** An Ed25519 keypair. */
39 ed25519_public_key_t pubkey
;
40 ed25519_secret_key_t seckey
;
43 int ed25519_secret_key_generate(ed25519_secret_key_t
*seckey_out
,
45 int ed25519_secret_key_from_seed(ed25519_secret_key_t
*seckey_out
,
48 int ed25519_public_key_generate(ed25519_public_key_t
*pubkey_out
,
49 const ed25519_secret_key_t
*seckey
);
50 int ed25519_keypair_generate(ed25519_keypair_t
*keypair_out
, int extra_strong
);
51 int ed25519_sign(ed25519_signature_t
*signature_out
,
52 const uint8_t *msg
, size_t len
,
53 const ed25519_keypair_t
*key
);
54 MOCK_DECL(int,ed25519_checksig
,(const ed25519_signature_t
*signature
,
55 const uint8_t *msg
, size_t len
,
56 const ed25519_public_key_t
*pubkey
));
59 ed25519_sign_prefixed
,(ed25519_signature_t
*signature_out
,
60 const uint8_t *msg
, size_t len
,
61 const char *prefix_str
,
62 const ed25519_keypair_t
*keypair
));
65 ed25519_checksig_prefixed(const ed25519_signature_t
*signature
,
66 const uint8_t *msg
, size_t len
,
67 const char *prefix_str
,
68 const ed25519_public_key_t
*pubkey
);
70 int ed25519_public_key_is_zero(const ed25519_public_key_t
*pubkey
);
73 * A collection of information necessary to check an Ed25519 signature. Used
74 * for batch verification.
77 /** The public key that supposedly generated the signature. */
78 const ed25519_public_key_t
*pubkey
;
79 /** The signature to check. */
80 ed25519_signature_t signature
;
81 /** The message that the signature is supposed to have been applied to. */
83 /** The length of the message. */
85 } ed25519_checkable_t
;
87 MOCK_DECL(int, ed25519_checksig_batch
,(int *okay_out
,
88 const ed25519_checkable_t
*checkable
,
91 int ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t
*out
,
93 const curve25519_keypair_t
*inp
);
95 int ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t
*pubkey
,
96 const curve25519_public_key_t
*pubkey_in
,
98 int ed25519_keypair_blind(ed25519_keypair_t
*out
,
99 const ed25519_keypair_t
*inp
,
100 const uint8_t *param
);
101 int ed25519_public_blind(ed25519_public_key_t
*out
,
102 const ed25519_public_key_t
*inp
,
103 const uint8_t *param
);
105 /* XXXX read encrypted, write encrypted. */
107 int ed25519_seckey_write_to_file(const ed25519_secret_key_t
*seckey
,
108 const char *filename
,
110 int ed25519_seckey_read_from_file(ed25519_secret_key_t
*seckey_out
,
112 const char *filename
);
113 int ed25519_pubkey_write_to_file(const ed25519_public_key_t
*pubkey
,
114 const char *filename
,
116 int ed25519_pubkey_read_from_file(ed25519_public_key_t
*pubkey_out
,
118 const char *filename
);
120 void ed25519_keypair_free(ed25519_keypair_t
*kp
);
122 int ed25519_pubkey_eq(const ed25519_public_key_t
*key1
,
123 const ed25519_public_key_t
*key2
);
124 void ed25519_pubkey_copy(ed25519_public_key_t
*dest
,
125 const ed25519_public_key_t
*src
);
127 void ed25519_set_impl_params(int use_donna
);
128 void ed25519_init(void);
130 #ifdef TOR_UNIT_TESTS
131 void crypto_ed25519_testing_force_impl(const char *name
);
132 void crypto_ed25519_testing_restore_impl(void);
135 #ifdef CRYPTO_ED25519_PRIVATE
136 MOCK_DECL(STATIC
int, ed25519_impl_spot_check
, (void));