ed448.h 7.9 KB
Newer Older
1 2 3
/*
 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright 2015-2016 Cryptography Research, Inc.
M
Matt Caswell 已提交
4
 *
5 6 7 8
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
M
Matt Caswell 已提交
9
 *
10
 * Originally written by Mike Hamburg
M
Matt Caswell 已提交
11 12
 */

M
Matt Caswell 已提交
13 14
#ifndef __C448_ED448_H__
# define __C448_ED448_H__ 1
M
Matt Caswell 已提交
15

16
# include "point_448.h"
M
Matt Caswell 已提交
17 18 19 20 21

#ifdef __cplusplus
extern "C" {
#endif

22
/* Number of bytes in an EdDSA public key. */
23
# define EDDSA_448_PUBLIC_BYTES 57
M
Matt Caswell 已提交
24

25
/* Number of bytes in an EdDSA private key. */
26
# define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES
M
Matt Caswell 已提交
27

28
/* Number of bytes in an EdDSA private key. */
29 30
# define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \
                                    EDDSA_448_PRIVATE_BYTES)
M
Matt Caswell 已提交
31

32
/* EdDSA encoding ratio. */
33
# define C448_EDDSA_ENCODE_RATIO 4
M
Matt Caswell 已提交
34

35
/* EdDSA decoding ratio. */
36
# define C448_EDDSA_DECODE_RATIO (4 / 4)
M
Matt Caswell 已提交
37

38 39
/*
 * EdDSA key generation.  This function uses a different (non-Decaf) encoding.
M
Matt Caswell 已提交
40
 *
41 42
 * pubkey (out): The public key.
 * privkey (in): The private key.
43
 */
M
Matt Caswell 已提交
44
c448_error_t c448_ed448_derive_public_key(
45 46
                        uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
                        const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/*
 * EdDSA signing.
 *
 * signature (out): The signature.
 * privkey (in): The private key.
 * pubkey (in):  The public key.
 * message (in):  The message to sign.
 * message_len (in):  The length of the message.
 * prehashed (in):  Nonzero if the message is actually the hash of something
 *                  you want to sign.
 * context (in):  A "context" for this signature of up to 255 bytes.
 * context_len (in):  Length of the context.
 *
 * For Ed25519, it is unsafe to use the same key for both prehashed and
 * non-prehashed messages, at least without some very careful protocol-level
 * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
 * it harder to screw this up, but this C code gives you no seat belt.
65
 */
M
Matt Caswell 已提交
66
c448_error_t c448_ed448_sign(
67 68 69
                        uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
                        const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
                        const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
70 71
                        const uint8_t *message, size_t message_len,
                        uint8_t prehashed, const uint8_t *context,
72
                        size_t context_len);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

/*
 * EdDSA signing with prehash.
 *
 * signature (out): The signature.
 * privkey (in): The private key.
 * pubkey (in): The public key.
 * hash (in): The hash of the message.  This object will not be modified by the
 *            call.
 * context (in): A "context" for this signature of up to 255 bytes.  Must be the
 *               same as what was used for the prehash.
 * context_len (in): Length of the context.
 *
 * For Ed25519, it is unsafe to use the same key for both prehashed and
 * non-prehashed messages, at least without some very careful protocol-level
 * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
 * it harder to screw this up, but this C code gives you no seat belt.
90
 */
M
Matt Caswell 已提交
91
c448_error_t c448_ed448_sign_prehash(
92 93 94
                        uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
                        const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
                        const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
95 96
                        const uint8_t hash[64],
                        const uint8_t *context,
97
                        size_t context_len);
98 99 100

/*
 * EdDSA signature verification.
M
Matt Caswell 已提交
101 102 103
 *
 * Uses the standard (i.e. less-strict) verification formula.
 *
104 105 106 107 108 109 110 111 112 113 114 115 116
 * signature (in): The signature.
 * pubkey (in): The public key.
 * message (in): The message to verify.
 * message_len (in): The length of the message.
 * prehashed (in): Nonzero if the message is actually the hash of something you
 *                 want to verify.
 * context (in): A "context" for this signature of up to 255 bytes.
 * context_len (in): Length of the context.
 *
 * For Ed25519, it is unsafe to use the same key for both prehashed and
 * non-prehashed messages, at least without some very careful protocol-level
 * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
 * it harder to screw this up, but this C code gives you no seat belt.
M
Matt Caswell 已提交
117
 */
M
Matt Caswell 已提交
118
c448_error_t c448_ed448_verify(const uint8_t
119
                                 signature[EDDSA_448_SIGNATURE_BYTES],
120
                                 const uint8_t
121
                                 pubkey[EDDSA_448_PUBLIC_BYTES],
122 123
                                 const uint8_t *message, size_t message_len,
                                 uint8_t prehashed, const uint8_t *context,
124
                                 uint8_t context_len);
M
Matt Caswell 已提交
125

126 127
/*
 * EdDSA signature verification.
M
Matt Caswell 已提交
128 129 130
 *
 * Uses the standard (i.e. less-strict) verification formula.
 *
131 132 133 134 135 136 137 138 139 140 141 142
 * signature (in): The signature.
 * pubkey (in): The public key.
 * hash (in): The hash of the message.  This object will not be modified by the
 *            call.
 * context (in): A "context" for this signature of up to 255 bytes.  Must be the
 *               same as what was used for the prehash.
 * context_len (in): Length of the context.
 *
 * For Ed25519, it is unsafe to use the same key for both prehashed and
 * non-prehashed messages, at least without some very careful protocol-level
 * disambiguation.  For Ed448 it is safe.  The C++ wrapper is designed to make
 * it harder to screw this up, but this C code gives you no seat belt.
M
Matt Caswell 已提交
143
 */
M
Matt Caswell 已提交
144
c448_error_t c448_ed448_verify_prehash(
145 146
                    const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
                    const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
147 148
                    const uint8_t hash[64],
                    const uint8_t *context,
149
                    uint8_t context_len);
150 151 152

/*
 * EdDSA point encoding.  Used internally, exposed externally.
153
 * Multiplies by C448_EDDSA_ENCODE_RATIO first.
M
Matt Caswell 已提交
154 155 156 157 158 159 160
 *
 * The multiplication is required because the EdDSA encoding represents
 * the cofactor information, but the Decaf encoding ignores it (which
 * is the whole point).  So if you decode from EdDSA and re-encode to
 * EdDSA, the cofactor info must get cleared, because the intermediate
 * representation doesn't track it.
 *
161 162
 * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
 * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding.  The product of
M
Matt Caswell 已提交
163 164 165
 * these ratios is always exactly the cofactor 4, so the cofactor ends up
 * cleared one way or another.  But exactly how that shakes out depends on the
 * base points specified in RFC 8032.
M
Matt Caswell 已提交
166 167
 *
 * The upshot is that if you pass the Decaf/Ristretto base point to
168
 * this function, you will get C448_EDDSA_ENCODE_RATIO times the
M
Matt Caswell 已提交
169 170
 * EdDSA base point.
 *
171 172
 * enc (out): The encoded point.
 * p (in): The point.
173
 */
174
void curve448_point_mul_by_ratio_and_encode_like_eddsa(
175
                                    uint8_t enc [EDDSA_448_PUBLIC_BYTES],
176
                                    const curve448_point_t p);
M
Matt Caswell 已提交
177

178
/*
179
 * EdDSA point decoding.  Multiplies by C448_EDDSA_DECODE_RATIO, and
180
 * ignores cofactor information.
M
Matt Caswell 已提交
181
 *
M
Matt Caswell 已提交
182
 * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
M
Matt Caswell 已提交
183
 *
184 185
 * enc (out): The encoded point.
 * p (in): The point.
186
 */
M
Matt Caswell 已提交
187
c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
188
                            curve448_point_t p,
189
                            const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
190 191 192

/*
 * EdDSA to ECDH private key conversion
M
Matt Caswell 已提交
193 194 195
 * Using the appropriate hash function, hash the EdDSA private key
 * and keep only the lower bytes to get the ECDH private key
 *
196 197
 * x (out): The ECDH private key as in RFC7748
 * ed (in): The EdDSA private key
M
Matt Caswell 已提交
198
 */
M
Matt Caswell 已提交
199
c448_error_t c448_ed448_convert_private_key_to_x448(
200 201
                            uint8_t x[X448_PRIVATE_BYTES],
                            const uint8_t ed[EDDSA_448_PRIVATE_BYTES]);
M
Matt Caswell 已提交
202 203 204 205 206

#ifdef __cplusplus
} /* extern "C" */
#endif

M
Matt Caswell 已提交
207
#endif                          /* __C448_ED448_H__ */