]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/curve448/ed448.h
More style fixes to Curve448 code based on review feedback
[thirdparty/openssl.git] / crypto / ec / curve448 / ed448.h
CommitLineData
1308e022 1/*
f53c7764 2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
1308e022 3 * Copyright 2015-2016 Cryptography Research, Inc.
7324473f 4 *
1308e022
MC
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
7324473f 9 *
1308e022 10 * Originally written by Mike Hamburg
7324473f
MC
11 */
12
68b20c00
MC
13#ifndef HEADER_ED448_H
14# define HEADER_ED448_H
7324473f 15
205fd638 16# include "point_448.h"
7324473f 17
8d55f844 18/* Number of bytes in an EdDSA public key. */
db90b274 19# define EDDSA_448_PUBLIC_BYTES 57
7324473f 20
8d55f844 21/* Number of bytes in an EdDSA private key. */
db90b274 22# define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES
7324473f 23
8d55f844 24/* Number of bytes in an EdDSA private key. */
db90b274
MC
25# define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \
26 EDDSA_448_PRIVATE_BYTES)
7324473f 27
8d55f844 28/* EdDSA encoding ratio. */
db90b274 29# define C448_EDDSA_ENCODE_RATIO 4
7324473f 30
8d55f844 31/* EdDSA decoding ratio. */
db90b274 32# define C448_EDDSA_DECODE_RATIO (4 / 4)
7324473f 33
8d55f844
MC
34/*
35 * EdDSA key generation. This function uses a different (non-Decaf) encoding.
7324473f 36 *
8d55f844
MC
37 * pubkey (out): The public key.
38 * privkey (in): The private key.
205fd638 39 */
aeeef83c 40c448_error_t c448_ed448_derive_public_key(
db90b274
MC
41 uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
42 const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]);
8d55f844
MC
43
44/*
45 * EdDSA signing.
46 *
47 * signature (out): The signature.
48 * privkey (in): The private key.
49 * pubkey (in): The public key.
50 * message (in): The message to sign.
51 * message_len (in): The length of the message.
52 * prehashed (in): Nonzero if the message is actually the hash of something
53 * you want to sign.
54 * context (in): A "context" for this signature of up to 255 bytes.
55 * context_len (in): Length of the context.
56 *
57 * For Ed25519, it is unsafe to use the same key for both prehashed and
58 * non-prehashed messages, at least without some very careful protocol-level
59 * disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
60 * it harder to screw this up, but this C code gives you no seat belt.
205fd638 61 */
aeeef83c 62c448_error_t c448_ed448_sign(
db90b274
MC
63 uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
64 const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
65 const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
8d55f844
MC
66 const uint8_t *message, size_t message_len,
67 uint8_t prehashed, const uint8_t *context,
575d5afc 68 size_t context_len);
8d55f844
MC
69
70/*
71 * EdDSA signing with prehash.
72 *
73 * signature (out): The signature.
74 * privkey (in): The private key.
75 * pubkey (in): The public key.
76 * hash (in): The hash of the message. This object will not be modified by the
77 * call.
78 * context (in): A "context" for this signature of up to 255 bytes. Must be the
79 * same as what was used for the prehash.
80 * context_len (in): Length of the context.
81 *
82 * For Ed25519, it is unsafe to use the same key for both prehashed and
83 * non-prehashed messages, at least without some very careful protocol-level
84 * disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
85 * it harder to screw this up, but this C code gives you no seat belt.
205fd638 86 */
aeeef83c 87c448_error_t c448_ed448_sign_prehash(
db90b274
MC
88 uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
89 const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
90 const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
8d55f844
MC
91 const uint8_t hash[64],
92 const uint8_t *context,
575d5afc 93 size_t context_len);
8d55f844
MC
94
95/*
96 * EdDSA signature verification.
7324473f
MC
97 *
98 * Uses the standard (i.e. less-strict) verification formula.
99 *
8d55f844
MC
100 * signature (in): The signature.
101 * pubkey (in): The public key.
102 * message (in): The message to verify.
103 * message_len (in): The length of the message.
104 * prehashed (in): Nonzero if the message is actually the hash of something you
105 * want to verify.
106 * context (in): A "context" for this signature of up to 255 bytes.
107 * context_len (in): Length of the context.
108 *
109 * For Ed25519, it is unsafe to use the same key for both prehashed and
110 * non-prehashed messages, at least without some very careful protocol-level
68b20c00 111 * disambiguation. For Ed448 it is safe.
7324473f 112 */
aeeef83c 113c448_error_t c448_ed448_verify(const uint8_t
db90b274 114 signature[EDDSA_448_SIGNATURE_BYTES],
205fd638 115 const uint8_t
db90b274 116 pubkey[EDDSA_448_PUBLIC_BYTES],
205fd638
MC
117 const uint8_t *message, size_t message_len,
118 uint8_t prehashed, const uint8_t *context,
575d5afc 119 uint8_t context_len);
7324473f 120
8d55f844
MC
121/*
122 * EdDSA signature verification.
7324473f
MC
123 *
124 * Uses the standard (i.e. less-strict) verification formula.
125 *
8d55f844
MC
126 * signature (in): The signature.
127 * pubkey (in): The public key.
128 * hash (in): The hash of the message. This object will not be modified by the
129 * call.
130 * context (in): A "context" for this signature of up to 255 bytes. Must be the
131 * same as what was used for the prehash.
132 * context_len (in): Length of the context.
133 *
134 * For Ed25519, it is unsafe to use the same key for both prehashed and
135 * non-prehashed messages, at least without some very careful protocol-level
136 * disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
137 * it harder to screw this up, but this C code gives you no seat belt.
7324473f 138 */
aeeef83c 139c448_error_t c448_ed448_verify_prehash(
db90b274
MC
140 const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
141 const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
8d55f844
MC
142 const uint8_t hash[64],
143 const uint8_t *context,
575d5afc 144 uint8_t context_len);
8d55f844
MC
145
146/*
147 * EdDSA point encoding. Used internally, exposed externally.
db90b274 148 * Multiplies by C448_EDDSA_ENCODE_RATIO first.
7324473f
MC
149 *
150 * The multiplication is required because the EdDSA encoding represents
151 * the cofactor information, but the Decaf encoding ignores it (which
152 * is the whole point). So if you decode from EdDSA and re-encode to
153 * EdDSA, the cofactor info must get cleared, because the intermediate
154 * representation doesn't track it.
155 *
db90b274
MC
156 * The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
157 * decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of
aeeef83c
MC
158 * these ratios is always exactly the cofactor 4, so the cofactor ends up
159 * cleared one way or another. But exactly how that shakes out depends on the
160 * base points specified in RFC 8032.
7324473f
MC
161 *
162 * The upshot is that if you pass the Decaf/Ristretto base point to
db90b274 163 * this function, you will get C448_EDDSA_ENCODE_RATIO times the
7324473f
MC
164 * EdDSA base point.
165 *
8d55f844
MC
166 * enc (out): The encoded point.
167 * p (in): The point.
205fd638 168 */
8d55f844 169void curve448_point_mul_by_ratio_and_encode_like_eddsa(
db90b274 170 uint8_t enc [EDDSA_448_PUBLIC_BYTES],
8d55f844 171 const curve448_point_t p);
7324473f 172
8d55f844 173/*
db90b274 174 * EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and
8d55f844 175 * ignores cofactor information.
7324473f 176 *
e7772577 177 * See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
7324473f 178 *
8d55f844
MC
179 * enc (out): The encoded point.
180 * p (in): The point.
205fd638 181 */
aeeef83c 182c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
8d55f844 183 curve448_point_t p,
db90b274 184 const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
8d55f844 185
8d55f844
MC
186/*
187 * EdDSA to ECDH private key conversion
7324473f
MC
188 * Using the appropriate hash function, hash the EdDSA private key
189 * and keep only the lower bytes to get the ECDH private key
190 *
8d55f844
MC
191 * x (out): The ECDH private key as in RFC7748
192 * ed (in): The EdDSA private key
7324473f 193 */
aeeef83c 194c448_error_t c448_ed448_convert_private_key_to_x448(
db90b274
MC
195 uint8_t x[X448_PRIVATE_BYTES],
196 const uint8_t ed[EDDSA_448_PRIVATE_BYTES]);
7324473f 197
68b20c00 198#endif /* HEADER_ED448_H */