~ This returns the secret and/or public key for this node. */
| 245 | |
| 246 | /*~ This returns the secret and/or public key for this node. */ |
| 247 | static void node_key(struct privkey *node_privkey, struct pubkey *node_id) |
| 248 | { |
| 249 | u32 salt = 0; |
| 250 | struct privkey unused_s; |
| 251 | struct pubkey unused_k; |
| 252 | |
| 253 | /* If caller specifies NULL, they don't want the results. */ |
| 254 | if (node_privkey == NULL) |
| 255 | node_privkey = &unused_s; |
| 256 | if (node_id == NULL) |
| 257 | node_id = &unused_k; |
| 258 | |
| 259 | /*~ So, there is apparently a 1 in 2^127 chance that a random value is |
| 260 | * not a valid private key, so this never actually loops. */ |
| 261 | do { |
| 262 | /*~ ccan/crypto/hkdf_sha256 implements RFC5869 "Hardened Key |
| 263 | * Derivation Functions". That means that if a derived key |
| 264 | * leaks somehow, the other keys are not compromised. */ |
| 265 | hkdf_sha256(node_privkey, sizeof(*node_privkey), |
| 266 | &salt, sizeof(salt), |
| 267 | secretstuff.bip32_seed, |
| 268 | 32, /* Use first 32 bytes for node key derivation */ |
| 269 | "nodeid", 6); |
| 270 | salt++; |
| 271 | } while (!secp256k1_ec_pubkey_create(secp256k1_ctx, &node_id->pubkey, |
| 272 | node_privkey->secret.data)); |
| 273 | |
| 274 | /* In --developer mode, we can override with --dev-force-privkey */ |
| 275 | if (dev_force_privkey) { |
| 276 | *node_privkey = *dev_force_privkey; |
| 277 | if (!secp256k1_ec_pubkey_create(secp256k1_ctx, &node_id->pubkey, |
| 278 | node_privkey->secret.data)) |
| 279 | hsmd_status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 280 | "Failed to derive pubkey for dev_force_privkey"); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /*~ This returns the secret key for this node. */ |
| 285 | static void node_schnorrkey(secp256k1_keypair *node_keypair) |
no test coverage detected