]>
Commit | Line | Data |
---|---|---|
a6d68949 | 1 | #ifndef _SECP256K1_ |
8563713a | 2 | # define _SECP256K1_ |
a6d68949 | 3 | |
8563713a | 4 | # ifdef __cplusplus |
254327e4 | 5 | extern "C" { |
8563713a GM |
6 | # endif |
7 | ||
788038d3 LD |
8 | #include <stddef.h> |
9 | ||
dc0ce9fc PW |
10 | /* These rules specify the order of arguments in API calls: |
11 | * | |
12 | * 1. Context pointers go first, followed by output arguments, combined | |
13 | * output/input arguments, and finally input-only arguments. | |
14 | * 2. Array lengths always immediately the follow the argument whose length | |
15 | * they describe, even if this violates rule 1. | |
16 | * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated | |
17 | * later go first. This means: signatures, public nonces, private nonces, | |
18 | * messages, public keys, secret keys, tweaks. | |
19 | * 4. Arguments that are not data pointers go last, from more complex to less | |
20 | * complex: function pointers, algorithm names, messages, void pointers, | |
21 | * counts, flags, booleans. | |
22 | * 5. Opaque data pointers follow the function pointer they are to be passed to. | |
23 | */ | |
24 | ||
f66907f2 PW |
25 | /** Opaque data structure that holds context information (precomputed tables etc.). |
26 | * | |
27 | * The purpose of context structures is to cache large precomputed data tables | |
28 | * that are expensive to construct, and also to maintain the randomization data | |
29 | * for blinding. | |
30 | * | |
31 | * Do not create a new context object for each operation, as construction is | |
32 | * far slower than all other API calls (~100 times slower than an ECDSA | |
33 | * verification). | |
34 | * | |
35 | * A constructed context can safely be used from multiple threads | |
36 | * simultaneously, but API call that take a non-const pointer to a context | |
37 | * need exclusive access to it. In particular this is the case for | |
38 | * secp256k1_context_destroy and secp256k1_context_randomize. | |
39 | * | |
40 | * Regarding randomization, either do it once at creation time (in which case | |
41 | * you do not need any locking for the other calls), or use a read-write lock. | |
42 | */ | |
dd891e0e | 43 | typedef struct secp256k1_context_struct secp256k1_context; |
f66907f2 PW |
44 | |
45 | /** Opaque data structure that holds a parsed and valid public key. | |
46 | * | |
47 | * The exact representation of data inside is implementation defined and not | |
48 | * guaranteed to be portable between different platforms or versions. It is | |
49 | * however guaranteed to be 64 bytes in size, and can be safely copied/moved. | |
91219a1c AP |
50 | * If you need to convert to a format suitable for storage, transmission, or |
51 | * comparison, use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse. | |
f66907f2 PW |
52 | */ |
53 | typedef struct { | |
54 | unsigned char data[64]; | |
dd891e0e | 55 | } secp256k1_pubkey; |
f66907f2 | 56 | |
439d34ad PW |
57 | /** Opaque data structured that holds a parsed ECDSA signature. |
58 | * | |
59 | * The exact representation of data inside is implementation defined and not | |
60 | * guaranteed to be portable between different platforms or versions. It is | |
61 | * however guaranteed to be 64 bytes in size, and can be safely copied/moved. | |
91219a1c AP |
62 | * If you need to convert to a format suitable for storage, transmission, or |
63 | * comparison, use the secp256k1_ecdsa_signature_serialize_* and | |
439d34ad | 64 | * secp256k1_ecdsa_signature_serialize_* functions. |
439d34ad PW |
65 | */ |
66 | typedef struct { | |
67 | unsigned char data[64]; | |
dd891e0e | 68 | } secp256k1_ecdsa_signature; |
439d34ad | 69 | |
f66907f2 PW |
70 | /** A pointer to a function to deterministically generate a nonce. |
71 | * | |
72 | * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail. | |
dc0ce9fc | 73 | * Out: nonce32: pointer to a 32-byte array to be filled by the function. |
f66907f2 PW |
74 | * In: msg32: the 32-byte message hash being verified (will not be NULL) |
75 | * key32: pointer to a 32-byte secret key (will not be NULL) | |
76 | * algo16: pointer to a 16-byte array describing the signature | |
77 | * algorithm (will be NULL for ECDSA for compatibility). | |
dc0ce9fc | 78 | * data: Arbitrary data pointer that is passed through. |
f66907f2 PW |
79 | * attempt: how many iterations we have tried to find a nonce. |
80 | * This will almost always be 0, but different attempt values | |
81 | * are required to result in a different nonce. | |
f66907f2 PW |
82 | * |
83 | * Except for test cases, this function should compute some cryptographic hash of | |
84 | * the message, the algorithm, the key and the attempt. | |
85 | */ | |
dd891e0e | 86 | typedef int (*secp256k1_nonce_function)( |
f66907f2 PW |
87 | unsigned char *nonce32, |
88 | const unsigned char *msg32, | |
89 | const unsigned char *key32, | |
90 | const unsigned char *algo16, | |
05732c5a | 91 | void *data, |
dc0ce9fc | 92 | unsigned int attempt |
f66907f2 PW |
93 | ); |
94 | ||
8563713a GM |
95 | # if !defined(SECP256K1_GNUC_PREREQ) |
96 | # if defined(__GNUC__)&&defined(__GNUC_MINOR__) | |
97 | # define SECP256K1_GNUC_PREREQ(_maj,_min) \ | |
98 | ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min)) | |
99 | # else | |
100 | # define SECP256K1_GNUC_PREREQ(_maj,_min) 0 | |
101 | # endif | |
102 | # endif | |
103 | ||
8563713a GM |
104 | # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) ) |
105 | # if SECP256K1_GNUC_PREREQ(2,7) | |
106 | # define SECP256K1_INLINE __inline__ | |
107 | # elif (defined(_MSC_VER)) | |
108 | # define SECP256K1_INLINE __inline | |
109 | # else | |
110 | # define SECP256K1_INLINE | |
111 | # endif | |
112 | # else | |
113 | # define SECP256K1_INLINE inline | |
114 | # endif | |
115 | ||
118cd821 GM |
116 | #ifndef SECP256K1_API |
117 | # if defined(_WIN32) | |
118 | # ifdef SECP256K1_BUILD | |
119 | # define SECP256K1_API __declspec(dllexport) | |
120 | # else | |
121 | # define SECP256K1_API | |
122 | # endif | |
123 | # elif defined(__GNUC__) && defined(SECP256K1_BUILD) | |
124 | # define SECP256K1_API __attribute__ ((visibility ("default"))) | |
125 | # else | |
126 | # define SECP256K1_API | |
127 | # endif | |
128 | #endif | |
129 | ||
8563713a GM |
130 | /**Warning attributes |
131 | * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out | |
132 | * some paranoid null checks. */ | |
133 | # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4) | |
134 | # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__)) | |
135 | # else | |
136 | # define SECP256K1_WARN_UNUSED_RESULT | |
137 | # endif | |
138 | # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4) | |
139 | # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x))) | |
140 | # else | |
141 | # define SECP256K1_ARG_NONNULL(_x) | |
142 | # endif | |
143 | ||
9234391e PW |
144 | /** All flags' lower 8 bits indicate what they're for. Do not use directly. */ |
145 | #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1) | |
146 | #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0) | |
147 | #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1) | |
148 | /** The higher bits contain the actual data. Do not use directly. */ | |
149 | #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8) | |
150 | #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9) | |
151 | #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8) | |
152 | ||
a9b6595e | 153 | /** Flags to pass to secp256k1_context_create. */ |
9234391e PW |
154 | #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) |
155 | #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN) | |
156 | #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT) | |
157 | ||
486b9bb8 | 158 | /** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */ |
9234391e PW |
159 | #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION) |
160 | #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION) | |
486b9bb8 | 161 | |
a9b6595e | 162 | /** Create a secp256k1 context object. |
f66907f2 | 163 | * |
a9b6595e PW |
164 | * Returns: a newly created context object. |
165 | * In: flags: which parts of the context to initialize. | |
70ff29b6 RR |
166 | * |
167 | * See also secp256k1_context_randomize. | |
b2966ce8 | 168 | */ |
118cd821 | 169 | SECP256K1_API secp256k1_context* secp256k1_context_create( |
64b730bc | 170 | unsigned int flags |
a9b6595e | 171 | ) SECP256K1_WARN_UNUSED_RESULT; |
b2966ce8 | 172 | |
d899b5b6 | 173 | /** Copies a secp256k1 context object. |
f66907f2 | 174 | * |
d899b5b6 | 175 | * Returns: a newly created context object. |
dc0ce9fc | 176 | * Args: ctx: an existing context to copy (cannot be NULL) |
d899b5b6 | 177 | */ |
118cd821 | 178 | SECP256K1_API secp256k1_context* secp256k1_context_clone( |
dd891e0e | 179 | const secp256k1_context* ctx |
f66907f2 | 180 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT; |
d899b5b6 | 181 | |
a9b6595e | 182 | /** Destroy a secp256k1 context object. |
f66907f2 | 183 | * |
a9b6595e | 184 | * The context pointer may not be used afterwards. |
dc0ce9fc | 185 | * Args: ctx: an existing context to destroy (cannot be NULL) |
b2966ce8 | 186 | */ |
118cd821 | 187 | SECP256K1_API void secp256k1_context_destroy( |
dd891e0e | 188 | secp256k1_context* ctx |
9aac0080 | 189 | ); |
b2966ce8 | 190 | |
995c5487 | 191 | /** Set a callback function to be called when an illegal argument is passed to |
f66907f2 PW |
192 | * an API call. It will only trigger for violations that are mentioned |
193 | * explicitly in the header. | |
194 | * | |
195 | * The philosophy is that these shouldn't be dealt with through a | |
995c5487 PW |
196 | * specific return value, as calling code should not have branches to deal with |
197 | * the case that this code itself is broken. | |
f66907f2 | 198 | * |
995c5487 PW |
199 | * On the other hand, during debug stage, one would want to be informed about |
200 | * such mistakes, and the default (crashing) may be inadvisable. | |
201 | * When this callback is triggered, the API function called is guaranteed not | |
202 | * to cause a crash, though its return value and output arguments are | |
203 | * undefined. | |
f66907f2 | 204 | * |
dc0ce9fc PW |
205 | * Args: ctx: an existing context object (cannot be NULL) |
206 | * In: fun: a pointer to a function to call when an illegal argument is | |
207 | * passed to the API, taking a message and an opaque pointer | |
c9d7c2a4 | 208 | * (NULL restores a default handler that calls abort). |
f66907f2 | 209 | * data: the opaque pointer to pass to fun above. |
995c5487 | 210 | */ |
118cd821 | 211 | SECP256K1_API void secp256k1_context_set_illegal_callback( |
dd891e0e | 212 | secp256k1_context* ctx, |
f66907f2 | 213 | void (*fun)(const char* message, void* data), |
05732c5a | 214 | const void* data |
c9d7c2a4 | 215 | ) SECP256K1_ARG_NONNULL(1); |
995c5487 PW |
216 | |
217 | /** Set a callback function to be called when an internal consistency check | |
218 | * fails. The default is crashing. | |
f66907f2 | 219 | * |
995c5487 PW |
220 | * This can only trigger in case of a hardware failure, miscompilation, |
221 | * memory corruption, serious bug in the library, or other error would can | |
222 | * otherwise result in undefined behaviour. It will not trigger due to mere | |
223 | * incorrect usage of the API (see secp256k1_context_set_illegal_callback | |
224 | * for that). After this callback returns, anything may happen, including | |
225 | * crashing. | |
f66907f2 | 226 | * |
dc0ce9fc | 227 | * Args: ctx: an existing context object (cannot be NULL) |
269d4227 | 228 | * In: fun: a pointer to a function to call when an internal error occurs, |
c9d7c2a4 LD |
229 | * taking a message and an opaque pointer (NULL restores a default |
230 | * handler that calls abort). | |
f66907f2 | 231 | * data: the opaque pointer to pass to fun above. |
995c5487 | 232 | */ |
118cd821 | 233 | SECP256K1_API void secp256k1_context_set_error_callback( |
dd891e0e | 234 | secp256k1_context* ctx, |
f66907f2 | 235 | void (*fun)(const char* message, void* data), |
05732c5a | 236 | const void* data |
c9d7c2a4 | 237 | ) SECP256K1_ARG_NONNULL(1); |
995c5487 | 238 | |
23cfa914 | 239 | /** Parse a variable-length public key into the pubkey object. |
f66907f2 | 240 | * |
23cfa914 PW |
241 | * Returns: 1 if the public key was fully valid. |
242 | * 0 if the public key could not be parsed or is invalid. | |
dc0ce9fc PW |
243 | * Args: ctx: a secp256k1 context object. |
244 | * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a | |
245 | * parsed version of input. If not, its value is undefined. | |
246 | * In: input: pointer to a serialized public key | |
247 | * inputlen: length of the array pointed to by input | |
f66907f2 | 248 | * |
23cfa914 PW |
249 | * This function supports parsing compressed (33 bytes, header byte 0x02 or |
250 | * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header | |
251 | * byte 0x06 or 0x07) format public keys. | |
252 | */ | |
118cd821 | 253 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse( |
dd891e0e PW |
254 | const secp256k1_context* ctx, |
255 | secp256k1_pubkey* pubkey, | |
f66907f2 | 256 | const unsigned char *input, |
788038d3 | 257 | size_t inputlen |
23cfa914 PW |
258 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
259 | ||
260 | /** Serialize a pubkey object into a serialized byte sequence. | |
f66907f2 | 261 | * |
23cfa914 | 262 | * Returns: 1 always. |
06aeea55 PW |
263 | * Args: ctx: a secp256k1 context object. |
264 | * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if | |
265 | * compressed==1) byte array to place the serialized key | |
266 | * in. | |
267 | * In/Out: outputlen: a pointer to an integer which is initially set to the | |
268 | * size of output, and is overwritten with the written | |
269 | * size. | |
270 | * In: pubkey: a pointer to a secp256k1_pubkey containing an | |
271 | * initialized public key. | |
272 | * flags: SECP256K1_EC_COMPRESSED if serialization should be in | |
273 | * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED. | |
23cfa914 | 274 | */ |
118cd821 | 275 | SECP256K1_API int secp256k1_ec_pubkey_serialize( |
dd891e0e | 276 | const secp256k1_context* ctx, |
f66907f2 | 277 | unsigned char *output, |
788038d3 | 278 | size_t *outputlen, |
dd891e0e | 279 | const secp256k1_pubkey* pubkey, |
486b9bb8 | 280 | unsigned int flags |
23cfa914 PW |
281 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
282 | ||
3bb9c447 PW |
283 | /** Parse an ECDSA signature in compact (64 bytes) format. |
284 | * | |
285 | * Returns: 1 when the signature could be parsed, 0 otherwise. | |
286 | * Args: ctx: a secp256k1 context object | |
287 | * Out: sig: a pointer to a signature object | |
288 | * In: input64: a pointer to the 64-byte array to parse | |
289 | * | |
290 | * The signature must consist of a 32-byte big endian R value, followed by a | |
291 | * 32-byte big endian S value. If R or S fall outside of [0..order-1], the | |
292 | * encoding is invalid. R and S with value 0 are allowed in the encoding. | |
293 | * | |
294 | * After the call, sig will always be initialized. If parsing failed or R or | |
295 | * S are zero, the resulting sig value is guaranteed to fail validation for any | |
296 | * message and public key. | |
297 | */ | |
298 | SECP256K1_API int secp256k1_ecdsa_signature_parse_compact( | |
299 | const secp256k1_context* ctx, | |
300 | secp256k1_ecdsa_signature* sig, | |
301 | const unsigned char *input64 | |
302 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); | |
303 | ||
74a2acdb | 304 | /** Parse a DER ECDSA signature. |
f66907f2 | 305 | * |
74a2acdb | 306 | * Returns: 1 when the signature could be parsed, 0 otherwise. |
dc0ce9fc PW |
307 | * Args: ctx: a secp256k1 context object |
308 | * Out: sig: a pointer to a signature object | |
309 | * In: input: a pointer to the signature to be parsed | |
310 | * inputlen: the length of the array pointed to be input | |
74a2acdb | 311 | * |
3bb9c447 PW |
312 | * This function will accept any valid DER encoded signature, even if the |
313 | * encoded numbers are out of range. | |
314 | * | |
315 | * After the call, sig will always be initialized. If parsing failed or the | |
316 | * encoded numbers are out of range, signature validation with it is | |
317 | * guaranteed to fail for every message and public key. | |
74a2acdb | 318 | */ |
118cd821 | 319 | SECP256K1_API int secp256k1_ecdsa_signature_parse_der( |
dd891e0e PW |
320 | const secp256k1_context* ctx, |
321 | secp256k1_ecdsa_signature* sig, | |
f66907f2 | 322 | const unsigned char *input, |
788038d3 | 323 | size_t inputlen |
74a2acdb PW |
324 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
325 | ||
74a2acdb | 326 | /** Serialize an ECDSA signature in DER format. |
f66907f2 | 327 | * |
74a2acdb | 328 | * Returns: 1 if enough space was available to serialize, 0 otherwise |
dc0ce9fc | 329 | * Args: ctx: a secp256k1 context object |
74a2acdb PW |
330 | * Out: output: a pointer to an array to store the DER serialization |
331 | * In/Out: outputlen: a pointer to a length integer. Initially, this integer | |
332 | * should be set to the length of output. After the call | |
333 | * it will be set to the length of the serialization (even | |
334 | * if 0 was returned). | |
dc0ce9fc | 335 | * In: sig: a pointer to an initialized signature object |
74a2acdb | 336 | */ |
118cd821 | 337 | SECP256K1_API int secp256k1_ecdsa_signature_serialize_der( |
dd891e0e | 338 | const secp256k1_context* ctx, |
f66907f2 | 339 | unsigned char *output, |
788038d3 | 340 | size_t *outputlen, |
dd891e0e | 341 | const secp256k1_ecdsa_signature* sig |
74a2acdb PW |
342 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
343 | ||
3bb9c447 PW |
344 | /** Serialize an ECDSA signature in compact (64 byte) format. |
345 | * | |
346 | * Returns: 1 | |
347 | * Args: ctx: a secp256k1 context object | |
348 | * Out: output64: a pointer to a 64-byte array to store the compact serialization | |
349 | * In: sig: a pointer to an initialized signature object | |
350 | * | |
351 | * See secp256k1_ecdsa_signature_parse_compact for details about the encoding. | |
352 | */ | |
353 | SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact( | |
354 | const secp256k1_context* ctx, | |
355 | unsigned char *output64, | |
356 | const secp256k1_ecdsa_signature* sig | |
357 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); | |
358 | ||
b2966ce8 | 359 | /** Verify an ECDSA signature. |
f66907f2 | 360 | * |
b2966ce8 | 361 | * Returns: 1: correct signature |
23cfa914 | 362 | * 0: incorrect or unparseable signature |
dc0ce9fc PW |
363 | * Args: ctx: a secp256k1 context object, initialized for verification. |
364 | * In: sig: the signature being verified (cannot be NULL) | |
a9b6595e | 365 | * msg32: the 32-byte message hash being verified (cannot be NULL) |
23cfa914 | 366 | * pubkey: pointer to an initialized public key to verify with (cannot be NULL) |
0c6ab2ff PW |
367 | * |
368 | * To avoid accepting malleable signatures, only ECDSA signatures in lower-S | |
369 | * form are accepted. | |
370 | * | |
371 | * If you need to accept ECDSA signatures from sources that do not obey this | |
372 | * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to | |
373 | * validation, but be aware that doing so results in malleable signatures. | |
374 | * | |
375 | * For details, see the comments for that function. | |
b2966ce8 | 376 | */ |
118cd821 | 377 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify( |
dd891e0e PW |
378 | const secp256k1_context* ctx, |
379 | const secp256k1_ecdsa_signature *sig, | |
dc0ce9fc | 380 | const unsigned char *msg32, |
dd891e0e | 381 | const secp256k1_pubkey *pubkey |
74a2acdb | 382 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
b2966ce8 | 383 | |
0c6ab2ff PW |
384 | /** Convert a signature to a normalized lower-S form. |
385 | * | |
386 | * Returns: 1 if sigin was not normalized, 0 if it already was. | |
387 | * Args: ctx: a secp256k1 context object | |
388 | * Out: sigout: a pointer to a signature to fill with the normalized form, | |
389 | * or copy if the input was already normalized. (can be NULL if | |
390 | * you're only interested in whether the input was already | |
391 | * normalized). | |
392 | * In: sigin: a pointer to a signature to check/normalize (cannot be NULL, | |
393 | * can be identical to sigout) | |
394 | * | |
395 | * With ECDSA a third-party can forge a second distinct signature of the same | |
396 | * message, given a single initial signature, but without knowing the key. This | |
397 | * is done by negating the S value modulo the order of the curve, 'flipping' | |
398 | * the sign of the random point R which is not included in the signature. | |
399 | * | |
400 | * Forgery of the same message isn't universally problematic, but in systems | |
401 | * where message malleability or uniqueness of signatures is important this can | |
402 | * cause issues. This forgery can be blocked by all verifiers forcing signers | |
403 | * to use a normalized form. | |
404 | * | |
405 | * The lower-S form reduces the size of signatures slightly on average when | |
406 | * variable length encodings (such as DER) are used and is cheap to verify, | |
407 | * making it a good choice. Security of always using lower-S is assured because | |
408 | * anyone can trivially modify a signature after the fact to enforce this | |
409 | * property anyway. | |
410 | * | |
411 | * The lower S value is always between 0x1 and | |
412 | * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, | |
413 | * inclusive. | |
414 | * | |
415 | * No other forms of ECDSA malleability are known and none seem likely, but | |
416 | * there is no formal proof that ECDSA, even with this additional restriction, | |
417 | * is free of other malleability. Commonly used serialization schemes will also | |
418 | * accept various non-unique encodings, so care should be taken when this | |
419 | * property is required for an application. | |
420 | * | |
421 | * The secp256k1_ecdsa_sign function will by default create signatures in the | |
422 | * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case | |
423 | * signatures come from a system that cannot enforce this property, | |
424 | * secp256k1_ecdsa_signature_normalize must be called before verification. | |
425 | */ | |
426 | SECP256K1_API int secp256k1_ecdsa_signature_normalize( | |
427 | const secp256k1_context* ctx, | |
428 | secp256k1_ecdsa_signature *sigout, | |
429 | const secp256k1_ecdsa_signature *sigin | |
430 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3); | |
431 | ||
1573a102 PW |
432 | /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function. |
433 | * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of | |
434 | * extra entropy. | |
435 | */ | |
338fc8bd | 436 | SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979; |
bbd5ba7c PW |
437 | |
438 | /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */ | |
338fc8bd | 439 | SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default; |
bbd5ba7c | 440 | |
42cccdaf | 441 | /** Create an ECDSA signature. |
f66907f2 | 442 | * |
42cccdaf | 443 | * Returns: 1: signature created |
74a2acdb | 444 | * 0: the nonce generation function failed, or the private key was invalid. |
dc0ce9fc PW |
445 | * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL) |
446 | * Out: sig: pointer to an array where the signature will be placed (cannot be NULL) | |
447 | * In: msg32: the 32-byte message hash being signed (cannot be NULL) | |
8030d7c0 | 448 | * seckey: pointer to a 32-byte secret key (cannot be NULL) |
bbd5ba7c | 449 | * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used |
c6e7f4e8 | 450 | * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL) |
74a2acdb | 451 | * |
0c6ab2ff PW |
452 | * The created signature is always in lower-S form. See |
453 | * secp256k1_ecdsa_signature_normalize for more details. | |
42cccdaf | 454 | */ |
118cd821 | 455 | SECP256K1_API int secp256k1_ecdsa_sign( |
dd891e0e PW |
456 | const secp256k1_context* ctx, |
457 | secp256k1_ecdsa_signature *sig, | |
dc0ce9fc | 458 | const unsigned char *msg32, |
f66907f2 | 459 | const unsigned char *seckey, |
dd891e0e | 460 | secp256k1_nonce_function noncefp, |
f66907f2 | 461 | const void *ndata |
a9b6595e | 462 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
50eb498e | 463 | |
42cccdaf | 464 | /** Verify an ECDSA secret key. |
f66907f2 | 465 | * |
42cccdaf PW |
466 | * Returns: 1: secret key is valid |
467 | * 0: secret key is invalid | |
dc0ce9fc PW |
468 | * Args: ctx: pointer to a context object (cannot be NULL) |
469 | * In: seckey: pointer to a 32-byte secret key (cannot be NULL) | |
42cccdaf | 470 | */ |
118cd821 | 471 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify( |
dd891e0e | 472 | const secp256k1_context* ctx, |
f66907f2 | 473 | const unsigned char *seckey |
a9b6595e | 474 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); |
78239167 | 475 | |
b2966ce8 | 476 | /** Compute the public key for a secret key. |
f66907f2 | 477 | * |
b2966ce8 | 478 | * Returns: 1: secret was valid, public key stores |
5098f625 | 479 | * 0: secret was invalid, try again |
dc0ce9fc PW |
480 | * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL) |
481 | * Out: pubkey: pointer to the created public key (cannot be NULL) | |
482 | * In: seckey: pointer to a 32-byte private key (cannot be NULL) | |
b2966ce8 | 483 | */ |
118cd821 | 484 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create( |
dd891e0e PW |
485 | const secp256k1_context* ctx, |
486 | secp256k1_pubkey *pubkey, | |
f66907f2 | 487 | const unsigned char *seckey |
23cfa914 | 488 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
ae6bc76e | 489 | |
8e48aa60 AP |
490 | /** Negates a private key in place. |
491 | * | |
492 | * Returns: 1 always | |
493 | * Args: ctx: pointer to a context object | |
494 | * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL) | |
495 | */ | |
496 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate( | |
497 | const secp256k1_context* ctx, | |
498 | unsigned char *seckey | |
499 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); | |
500 | ||
501 | /** Negates a public key in place. | |
502 | * | |
503 | * Returns: 1 always | |
504 | * Args: ctx: pointer to a context object | |
505 | * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL) | |
506 | */ | |
507 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate( | |
508 | const secp256k1_context* ctx, | |
509 | secp256k1_pubkey *pubkey | |
510 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); | |
ae6bc76e | 511 | |
f66907f2 | 512 | /** Tweak a private key by adding tweak to it. |
f66907f2 PW |
513 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
514 | * uniformly random 32-byte arrays, or if the resulting private key | |
515 | * would be invalid (only when the tweak is the complement of the | |
516 | * private key). 1 otherwise. | |
dc0ce9fc PW |
517 | * Args: ctx: pointer to a context object (cannot be NULL). |
518 | * In/Out: seckey: pointer to a 32-byte private key. | |
519 | * In: tweak: pointer to a 32-byte tweak. | |
f66907f2 | 520 | */ |
118cd821 | 521 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add( |
dd891e0e | 522 | const secp256k1_context* ctx, |
f66907f2 PW |
523 | unsigned char *seckey, |
524 | const unsigned char *tweak | |
a9b6595e | 525 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
da3038c7 | 526 | |
ae6bc76e | 527 | /** Tweak a public key by adding tweak times the generator to it. |
f66907f2 PW |
528 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
529 | * uniformly random 32-byte arrays, or if the resulting public key | |
530 | * would be invalid (only when the tweak is the complement of the | |
531 | * corresponding private key). 1 otherwise. | |
dc0ce9fc PW |
532 | * Args: ctx: pointer to a context object initialized for validation |
533 | * (cannot be NULL). | |
534 | * In/Out: pubkey: pointer to a public key object. | |
535 | * In: tweak: pointer to a 32-byte tweak. | |
ae6bc76e | 536 | */ |
118cd821 | 537 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add( |
dd891e0e PW |
538 | const secp256k1_context* ctx, |
539 | secp256k1_pubkey *pubkey, | |
f66907f2 | 540 | const unsigned char *tweak |
23cfa914 | 541 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
da3038c7 | 542 | |
f66907f2 | 543 | /** Tweak a private key by multiplying it by a tweak. |
f66907f2 PW |
544 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
545 | * uniformly random 32-byte arrays, or equal to zero. 1 otherwise. | |
dc0ce9fc PW |
546 | * Args: ctx: pointer to a context object (cannot be NULL). |
547 | * In/Out: seckey: pointer to a 32-byte private key. | |
548 | * In: tweak: pointer to a 32-byte tweak. | |
f66907f2 | 549 | */ |
118cd821 | 550 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul( |
dd891e0e | 551 | const secp256k1_context* ctx, |
f66907f2 PW |
552 | unsigned char *seckey, |
553 | const unsigned char *tweak | |
a9b6595e | 554 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
ae6bc76e | 555 | |
f66907f2 | 556 | /** Tweak a public key by multiplying it by a tweak value. |
f66907f2 PW |
557 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
558 | * uniformly random 32-byte arrays, or equal to zero. 1 otherwise. | |
dc0ce9fc PW |
559 | * Args: ctx: pointer to a context object initialized for validation |
560 | * (cannot be NULL). | |
561 | * In/Out: pubkey: pointer to a public key obkect. | |
562 | * In: tweak: pointer to a 32-byte tweak. | |
ae6bc76e | 563 | */ |
118cd821 | 564 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul( |
dd891e0e PW |
565 | const secp256k1_context* ctx, |
566 | secp256k1_pubkey *pubkey, | |
f66907f2 | 567 | const unsigned char *tweak |
23cfa914 | 568 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
194eea06 | 569 | |
70ff29b6 | 570 | /** Updates the context randomization to protect against side-channel leakage. |
d2275795 GM |
571 | * Returns: 1: randomization successfully updated |
572 | * 0: error | |
dc0ce9fc PW |
573 | * Args: ctx: pointer to a context object (cannot be NULL) |
574 | * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state) | |
70ff29b6 RR |
575 | * |
576 | * While secp256k1 code is written to be constant-time no matter what secret | |
577 | * values are, it's possible that a future compiler may output code which isn't, | |
578 | * and also that the CPU may not emit the same radio frequencies or draw the same | |
579 | * amount power for all values. | |
580 | * | |
581 | * This function provides a seed which is combined into the blinding value: that | |
72d952c9 | 582 | * blinding value is added before each multiplication (and removed afterwards) so |
70ff29b6 RR |
583 | * that it does not affect function results, but shields against attacks which |
584 | * rely on any input-dependent behaviour. | |
585 | * | |
586 | * You should call this after secp256k1_context_create or | |
587 | * secp256k1_context_clone, and may call this repeatedly afterwards. | |
d2275795 | 588 | */ |
118cd821 | 589 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize( |
dd891e0e | 590 | secp256k1_context* ctx, |
f66907f2 | 591 | const unsigned char *seed32 |
d2275795 GM |
592 | ) SECP256K1_ARG_NONNULL(1); |
593 | ||
a5a66c70 PW |
594 | /** Add a number of public keys together. |
595 | * Returns: 1: the sum of the public keys is valid. | |
596 | * 0: the sum of the public keys is not valid. | |
dc0ce9fc | 597 | * Args: ctx: pointer to a context object |
c69dea02 | 598 | * Out: out: pointer to a public key object for placing the resulting public key |
a5a66c70 | 599 | * (cannot be NULL) |
dc0ce9fc | 600 | * In: ins: pointer to array of pointers to public keys (cannot be NULL) |
a5a66c70 | 601 | * n: the number of public keys to add together (must be at least 1) |
a5a66c70 | 602 | */ |
118cd821 | 603 | SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine( |
dd891e0e PW |
604 | const secp256k1_context* ctx, |
605 | secp256k1_pubkey *out, | |
606 | const secp256k1_pubkey * const * ins, | |
8e48787d | 607 | size_t n |
dc0ce9fc | 608 | ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
d2275795 | 609 | |
8563713a | 610 | # ifdef __cplusplus |
a6d68949 | 611 | } |
8563713a | 612 | # endif |
a6d68949 PW |
613 | |
614 | #endif |