This writes encrypted static backup in the recovery file */
| 57 | |
| 58 | /* This writes encrypted static backup in the recovery file */ |
| 59 | static void write_scb(struct plugin *p, |
| 60 | int fd, |
| 61 | struct scb_chan **scb_chan_arr) |
| 62 | { |
| 63 | u32 timestamp = time_now().ts.tv_sec; |
| 64 | |
| 65 | u8 *decrypted_scb = towire_static_chan_backup(tmpctx, |
| 66 | VERSION, |
| 67 | timestamp, |
| 68 | cast_const2(const struct scb_chan **, |
| 69 | scb_chan_arr)); |
| 70 | |
| 71 | u8 *encrypted_scb = tal_arr(tmpctx, |
| 72 | u8, |
| 73 | tal_bytelen(decrypted_scb) + |
| 74 | ABYTES + |
| 75 | HEADER_LEN); |
| 76 | |
| 77 | crypto_secretstream_xchacha20poly1305_state crypto_state; |
| 78 | |
| 79 | if (crypto_secretstream_xchacha20poly1305_init_push(&crypto_state, |
| 80 | encrypted_scb, |
| 81 | (&secret)->data) != 0) |
| 82 | { |
| 83 | plugin_err(p, "Can't encrypt the data!"); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (crypto_secretstream_xchacha20poly1305_push(&crypto_state, |
| 88 | encrypted_scb + |
| 89 | HEADER_LEN, |
| 90 | NULL, decrypted_scb, |
| 91 | tal_bytelen(decrypted_scb), |
| 92 | /* Additional data and tag */ |
| 93 | NULL, 0, 0)) { |
| 94 | plugin_err(p, "Can't encrypt the data!"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (!write_all(fd, encrypted_scb, tal_bytelen(encrypted_scb))) { |
| 99 | unlink_noerr("scb.tmp"); |
| 100 | plugin_err(p, "Writing encrypted SCB: %s", |
| 101 | strerror(errno)); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /* checks if the SCB file exists, creates a new one in case it doesn't. */ |
| 107 | static void maybe_create_new_scb(struct plugin *p, |
no test coverage detected