Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/examples/ecdsa.c
| /************************************************************************* | /************************************************************************* | ||||
| * Written in 2020-2022 by Elichai Turkel * | * Written in 2020-2022 by Elichai Turkel * | ||||
| * To the extent possible under law, the author(s) have dedicated all * | * To the extent possible under law, the author(s) have dedicated all * | ||||
| * copyright and related and neighboring rights to the software in this * | * copyright and related and neighboring rights to the software in this * | ||||
| * file to the public domain worldwide. This software is distributed * | * file to the public domain worldwide. This software is distributed * | ||||
| * without any warranty. For the CC0 Public Domain Dedication, see * | * without any warranty. For the CC0 Public Domain Dedication, see * | ||||
| * EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 * | * EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 * | ||||
| *************************************************************************/ | *************************************************************************/ | ||||
| #include <stdio.h> | #include <stdio.h> | ||||
| #include <assert.h> | #include <assert.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <secp256k1.h> | #include <secp256k1.h> | ||||
| #include "random.h" | #include "examples_util.h" | ||||
| int main(void) { | int main(void) { | ||||
| /* Instead of signing the message directly, we must sign a 32-byte hash. | /* Instead of signing the message directly, we must sign a 32-byte hash. | ||||
| * Here the message is "Hello, world!" and the hash function was SHA-256. | * Here the message is "Hello, world!" and the hash function was SHA-256. | ||||
| * An actual implementation should just call SHA-256, but this example | * An actual implementation should just call SHA-256, but this example | ||||
| * hardcodes the output to avoid depending on an additional library. | * hardcodes the output to avoid depending on an additional library. | ||||
| * See https://bitcoin.stackexchange.com/questions/81115/if-someone-wanted-to-pretend-to-be-satoshi-by-posting-a-fake-signature-to-defrau/81116#81116 */ | * See https://bitcoin.stackexchange.com/questions/81115/if-someone-wanted-to-pretend-to-be-satoshi-by-posting-a-fake-signature-to-defrau/81116#81116 */ | ||||
| unsigned char msg_hash[32] = { | unsigned char msg_hash[32] = { | ||||
| ▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | int main(void) { | ||||
| /* This will clear everything from the context and free the memory */ | /* This will clear everything from the context and free the memory */ | ||||
| secp256k1_context_destroy(ctx); | secp256k1_context_destroy(ctx); | ||||
| /* It's best practice to try to clear secrets from memory after using them. | /* It's best practice to try to clear secrets from memory after using them. | ||||
| * This is done because some bugs can allow an attacker to leak memory, for | * This is done because some bugs can allow an attacker to leak memory, for | ||||
| * example through "out of bounds" array access (see Heartbleed), Or the OS | * example through "out of bounds" array access (see Heartbleed), Or the OS | ||||
| * swapping them to disk. Hence, we overwrite the secret key buffer with zeros. | * swapping them to disk. Hence, we overwrite the secret key buffer with zeros. | ||||
| * | * | ||||
| * TODO: Prevent these writes from being optimized out, as any good compiler | * Here we are preventing these writes from being optimized out, as any good compiler | ||||
| * will remove any writes that aren't used. */ | * will remove any writes that aren't used. */ | ||||
| memset(seckey, 0, sizeof(seckey)); | secure_erase(seckey, sizeof(seckey)); | ||||
| return 0; | return 0; | ||||
| } | } | ||||