Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/examples/ecdh.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 <secp256k1_ecdh.h> | #include <secp256k1_ecdh.h> | ||||
| #include "random.h" | #include "examples_util.h" | ||||
| int main(void) { | int main(void) { | ||||
| unsigned char seckey1[32]; | unsigned char seckey1[32]; | ||||
| unsigned char seckey2[32]; | unsigned char seckey2[32]; | ||||
| unsigned char compressed_pubkey1[33]; | unsigned char compressed_pubkey1[33]; | ||||
| unsigned char compressed_pubkey2[33]; | unsigned char compressed_pubkey2[33]; | ||||
| unsigned char shared_secret1[32]; | unsigned char shared_secret1[32]; | ||||
| unsigned char shared_secret2[32]; | unsigned char shared_secret2[32]; | ||||
| ▲ Show 20 Lines • Show All 80 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(seckey1, 0, sizeof(seckey1)); | secure_erase(seckey1, sizeof(seckey1)); | ||||
| memset(seckey2, 0, sizeof(seckey2)); | secure_erase(seckey2, sizeof(seckey2)); | ||||
| memset(shared_secret1, 0, sizeof(shared_secret1)); | secure_erase(shared_secret1, sizeof(shared_secret1)); | ||||
| memset(shared_secret2, 0, sizeof(shared_secret2)); | secure_erase(shared_secret2, sizeof(shared_secret2)); | ||||
| return 0; | return 0; | ||||
| } | } | ||||