Skip to content

Instantly share code, notes, and snippets.

@mcspr
Last active December 22, 2022 11:52
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <array>
namespace {
struct KeygenResult {
using Buffer = std::array<uint8_t, BR_EC_KBUF_PRIV_MAX_SIZE>;
alignas(4) Buffer buf;
br_ec_private_key key;
bool ok { false };
};
auto keygen_ec(int curve) {
KeygenResult out;
auto seeder = br_prng_seeder_system(nullptr);
if (seeder == nullptr) {
return out;
}
br_hmac_drbg_context rng;
br_hmac_drbg_init(&rng, &br_sha256_vtable, nullptr, 0);
if (!seeder(&rng.vtable)) {
return out;
}
const auto* impl = br_ec_get_default();
out.ok = 0 != br_ec_keygen(
&rng.vtable, impl,
&out.key, out.buf.data(), curve);
return out;
}
} // namespace
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
delay(1000);
const auto result = keygen_ec(BR_EC_secp256r1);
if (result.ok) {
hexdump(result.key.x, result.key.xlen);
} else {
puts("-ERROR!");
}
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment