/*- * Copyright 2022 John-Mark Gurney. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include #include #include #include /* roundup */ #include uint8_t keyvalid; static const uint8_t privkey[roundup(EC_PRIVATE_BYTES, sizeof(uint8_t)) / sizeof(uint8_t)] __attribute__ ((section (".savekeys"))) = { #if 0 0xae, 0xe7, 0xdd, 0x04, 0x84, 0xb3, 0xcd, 0x3c, 0xef, 0x25, 0x71, 0x83, 0xc4, 0x6c, 0x5d, 0x3c, 0xee, 0x98, 0xee, 0x79, 0xf2, 0x97, 0x6a, 0xe8, 0x39, 0xec, 0x7d, 0xe8, 0x23, 0xe7, 0x20, 0xdb, #endif }; #include void get_pubkey(uint8_t pubkey[EC_PUBLIC_BYTES]) { x25519_base(pubkey, privkey, 1); } struct strobepkikey get_key(void) { struct strobepkikey spk; uint8_t key[sizeof privkey] = {}; uint8_t keyf[sizeof privkey] = {}; int r; memset(keyf, 0xff, sizeof keyf); if (memcmp(key, privkey, sizeof privkey) == 0 || memcmp(keyf, privkey, sizeof privkey) == 0) { /* Generate new key */ do { r = strobe_randomize((uint8_t *)key, sizeof key); if (r < 0) continue; } while (r != 0); /* and write it. */ doflash(privkey, key, sizeof key); } spk = (struct strobepkikey){ .privkey.pkt = (void *)(uintptr_t)privkey, .privkey.pktlen = EC_PRIVATE_BYTES, .pubkey.pkt = (void *)(uintptr_t)pubkey, .pubkey.pktlen = EC_PUBLIC_BYTES, }; return spk; }