|
| 1 | +/************************************************************************* |
| 2 | + * To the extent possible under law, the author(s) have dedicated all * |
| 3 | + * copyright and related and neighboring rights to the software in this * |
| 4 | + * file to the public domain worldwide. This software is distributed * |
| 5 | + * without any warranty. For the CC0 Public Domain Dedication, see * |
| 6 | + * EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 * |
| 7 | + *************************************************************************/ |
| 8 | + |
| 9 | +/** This file demonstrates how to use the recovery module to create a |
| 10 | + * recoverable ECDSA signature and extract the corresponding |
| 11 | + * public key from it. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <assert.h> |
| 17 | +#include <string.h> |
| 18 | + |
| 19 | +#include <secp256k1.h> |
| 20 | +#include <secp256k1_recovery.h> |
| 21 | + |
| 22 | +#include "examples_util.h" |
| 23 | + |
| 24 | +int main(void) { |
| 25 | + unsigned char msg[32] = "this_could_be_the_hash_of_a_msg"; |
| 26 | + unsigned char seckey[32]; |
| 27 | + unsigned char randomize[32]; |
| 28 | + unsigned char recoverable_sig_ser[64]; |
| 29 | + unsigned char serialized_pubkey[33]; |
| 30 | + unsigned char serialized_recovered_pubkey[33]; |
| 31 | + size_t len; |
| 32 | + int return_val, recovery_id; |
| 33 | + secp256k1_pubkey pubkey, recovered_pubkey; |
| 34 | + secp256k1_ecdsa_recoverable_signature recoverable_sig; |
| 35 | + secp256k1_ecdsa_signature normal_sig; |
| 36 | + |
| 37 | + /* Before we can call actual API functions, we need to create a "context". */ |
| 38 | + secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE); |
| 39 | + if (!fill_random(randomize, sizeof(randomize))) { |
| 40 | + printf("Failed to generate randomness\n"); |
| 41 | + return EXIT_FAILURE; |
| 42 | + } |
| 43 | + /* Randomizing the context is recommended to protect against side-channel |
| 44 | + * leakage. See `secp256k1_context_randomize` in secp256k1.h for more |
| 45 | + * information about it. This should never fail. */ |
| 46 | + return_val = secp256k1_context_randomize(ctx, randomize); |
| 47 | + assert(return_val); |
| 48 | + |
| 49 | + /*** Key Generation ***/ |
| 50 | + if (!fill_random(seckey, sizeof(seckey))) { |
| 51 | + printf("Failed to generate randomness\n"); |
| 52 | + return EXIT_FAILURE; |
| 53 | + } |
| 54 | + /* Try to create a public key with a valid context. This only fails if the |
| 55 | + * secret key is zero or out of range (greater than secp256k1's order). Note |
| 56 | + * that the probability of this occurring is negligible with a properly |
| 57 | + * functioning random number generator. */ |
| 58 | + if (!secp256k1_ec_pubkey_create(ctx, &pubkey, seckey)) { |
| 59 | + printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n"); |
| 60 | + return EXIT_FAILURE; |
| 61 | + } |
| 62 | + |
| 63 | + /* Serialize the public key. Should always return 1 for a valid public key. */ |
| 64 | + len = sizeof(serialized_pubkey); |
| 65 | + return_val = secp256k1_ec_pubkey_serialize(ctx, serialized_pubkey, &len, &pubkey, SECP256K1_EC_COMPRESSED); |
| 66 | + assert(return_val); |
| 67 | + |
| 68 | + /*** Signing ***/ |
| 69 | + |
| 70 | + /* Signing with a valid context, verified secret key |
| 71 | + * and the default nonce function should never fail. */ |
| 72 | + return_val = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_sig, msg, seckey, NULL, NULL); |
| 73 | + assert(return_val); |
| 74 | + |
| 75 | + /* Serialize in compact format (64 bytes + recovery id integer) */ |
| 76 | + return_val = secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, |
| 77 | + recoverable_sig_ser, &recovery_id, &recoverable_sig); |
| 78 | + assert(return_val); |
| 79 | + |
| 80 | + /*** Public key recovery / verification ***/ |
| 81 | + |
| 82 | + /* Deserialize the recoverable signature. This will return 0 if the signature can't be parsed correctly. */ |
| 83 | + if (!secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, &recoverable_sig, recoverable_sig_ser, recovery_id)) { |
| 84 | + printf("Failed parsing the recoverable signature\n"); |
| 85 | + return EXIT_FAILURE; |
| 86 | + } |
| 87 | + |
| 88 | + /* Recover the public key */ |
| 89 | + if (!secp256k1_ecdsa_recover(ctx, &recovered_pubkey, &recoverable_sig, msg)) { |
| 90 | + printf("Public key recovery failed\n"); |
| 91 | + return EXIT_FAILURE; |
| 92 | + } |
| 93 | + len = sizeof(serialized_recovered_pubkey); |
| 94 | + return_val = secp256k1_ec_pubkey_serialize(ctx, serialized_recovered_pubkey, |
| 95 | + &len, &recovered_pubkey, SECP256K1_EC_COMPRESSED); |
| 96 | + assert(return_val); |
| 97 | + |
| 98 | + /* Successful recovery guarantees a correct signature, but we also do an explicit verification |
| 99 | + do demonstrate how to convert a recoverable to a normal ECDSA signature */ |
| 100 | + return_val = secp256k1_ecdsa_recoverable_signature_convert(ctx, &normal_sig, &recoverable_sig); |
| 101 | + assert(return_val); |
| 102 | + if (!secp256k1_ecdsa_verify(ctx, &normal_sig, msg, &recovered_pubkey)) { |
| 103 | + printf("Signature verification with converted recoverable signature failed\n"); |
| 104 | + return EXIT_FAILURE; |
| 105 | + } |
| 106 | + |
| 107 | + /* Actual public key and recovered public key should match */ |
| 108 | + return_val = memcmp(serialized_pubkey, serialized_recovered_pubkey, sizeof(serialized_pubkey)); |
| 109 | + assert(return_val == 0); |
| 110 | + |
| 111 | + printf(" Secret Key: "); |
| 112 | + print_hex(seckey, sizeof(seckey)); |
| 113 | + printf(" Public Key: "); |
| 114 | + print_hex(serialized_pubkey, sizeof(serialized_pubkey)); |
| 115 | + printf(" Rec. signature: "); |
| 116 | + print_hex(recoverable_sig_ser, sizeof(recoverable_sig_ser)); |
| 117 | + printf(" Recovery id: %d\n", recovery_id); |
| 118 | + printf("Rec. public key: "); |
| 119 | + print_hex(serialized_recovered_pubkey, sizeof(serialized_recovered_pubkey)); |
| 120 | + |
| 121 | + /* This will clear everything from the context and free the memory */ |
| 122 | + secp256k1_context_destroy(ctx); |
| 123 | + |
| 124 | + /* It's best practice to try to clear secrets from memory after using them. |
| 125 | + * This is done because some bugs can allow an attacker to leak memory, for |
| 126 | + * example through "out of bounds" array access (see Heartbleed), or the OS |
| 127 | + * swapping them to disk. Hence, we overwrite the secret key buffer with zeros. |
| 128 | + * |
| 129 | + * Here we are preventing these writes from being optimized out, as any good compiler |
| 130 | + * will remove any writes that aren't used. */ |
| 131 | + secure_erase(seckey, sizeof(seckey)); |
| 132 | + |
| 133 | + return EXIT_SUCCESS; |
| 134 | +} |
0 commit comments