-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use random aes key to simplify implementation
- Loading branch information
Showing
5 changed files
with
112 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
packages/neuron-wallet/tests/services/log-encryption.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import LogEncryption, { LogDecryption } from '../../src/services/log-encryption' | ||
import { generateKeyPairSync } from 'node:crypto' | ||
|
||
describe('Test LogEncryption', () => { | ||
it('encrypted message should be able to decrypt', () => { | ||
const { publicKey: adminPublicKey, privateKey: adminPrivateKey } = generateKeyPairSync('rsa', { | ||
modulusLength: 2048, | ||
}) | ||
|
||
const encryption = new LogEncryption(adminPublicKey.export({ format: 'pem', type: 'pkcs1' }).toString()) | ||
const decryption = new LogDecryption(adminPrivateKey.export({ format: 'pem', type: 'pkcs1' }).toString()) | ||
|
||
const message = 'hello' | ||
const encryptedMessage = encryption.encrypt(message) | ||
const decryptedMessage = decryption.decrypt(encryptedMessage) | ||
|
||
expect(decryptedMessage).toBe(message) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
## Log Decryption | ||
## Log Encryption & Decryption | ||
|
||
## Encryption | ||
|
||
An environment variable `LOG_ENCRYPTION_PUBLIC_KEY` must be set to enable log encryption when releasing Neuron. If the variable is not set, a placeholder message will be left in the log file. | ||
|
||
## Decryption | ||
|
||
An encrypted log looks the following block | ||
|
||
``` | ||
[2024-07-31T11:15:06.811Z] [info] LogEncryption key LFWL6pdZrEgMwhuyL6ViGYKy/ZSilpeksZW3gpvGEqTg+4tzKk0Sjep8/Emzy1t5tyGEI6fs0BJVVkgmiAVCozotDQJVmUUtAZkdNok7Y9rnZxIaTsyLciXUyWXyqckW7WJriNKmpzxUSj9PBH+U69irdeqmwNTysJ3Qv4y7wSdSG4mZ9/WOOH3S4S27NmJ9ZeO1PNaXZWMz2i7baA0erYAkl9zyPtgg3QSlYrSqk91mkOGgCrqJebC6d63+516wIskNk/NWPt0GA+KXIlDNketIFgu6SOBopLorhXi69mX/7q5XU/Cmv8+4nYrdnhqd+hReJg3MIK8tJuZvxNXy6w== | ||
[2024-07-31T11:15:06.811Z] [info] [iv:OJ9oGf7yL3K1jWYx7ABWHg==] /b4lCkOpL/kt7DHoyaDlOg== | ||
[2024-07-31T11:15:06.811Z] [info] [key:sWFKSuG+GzC52QlqDUcLhCvWFevSR8JjcvlIwCmB6U750UbO59zQZlQFyIUCBMH2Vamdr/ScZaF00wObzyi2BERMkKCQ9XY1ELcQSvCaAjUy4251B4MIyrnYPu4Bf+bca5U/906ko37G6dZMDNCcm2J5pm3+0TvqwXFA+BDXsAeZ7YWXpNha+WTMbQJiGj+ltbjIlodXhtqGWBhkLHgeZtfpM/OQDclOUfSP4SDva1LUvjdkQjnmUB+5dLumEAQpm7u7mroXl5eMTpVhyVtULm+QkQ4aA/D9Q/Y1dGUxl8jU2zcgL1h8Uhrb9FMpCaLyu13gGZr42HlFVU4j/VzD/g==] [iv:/jDhuN6b/qEetyHnU2WPDw==] 0+B+gimzrZgbxfxBTtznyA== | ||
``` | ||
|
||
To decrypt the message | ||
|
||
1. Create an `.env` at this folder(`/decrypt-log`) | ||
2. Config `CLIENT_ENCRYPTED_KEY`: Search the keyword `LogEncryption key` in the log file, copy the value to `.env`. This is an encrypted AES key used for decrypting the encrypted log message | ||
3. Config `ADMIN_PRIVATE_KEY`: copy the PEM format RSA private key to the `.env` file | ||
4. Config `LOG_MESSAGE`: copy the encrypted, base64 encoded log message to the `.env` file | ||
5. Run `node --experimental-strip-types --env-file .env run.ts` or `bun run.ts` | ||
```sh | ||
export LOG_MESSAGE="<log message similar the above mentioned>" | ||
export ADMIN_PRIVATE_KEY="<pem formatted rsa private key>" | ||
bun run.ts | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,10 @@ | ||
import { createDecipheriv, privateDecrypt } from "node:crypto"; | ||
import { LogDecryption } from "../../../packages/neuron-wallet/src/services/log-encryption"; | ||
|
||
const CLIENT_ENCRYPTED_KEY = process.env.CLIENT_ENCRYPTED_KEY!; | ||
const ADMIN_PRIVATE_KEY = process.env.ADMIN_PRIVATE_KEY!; | ||
const ADMIN_PRIVATE_KEY = process.env | ||
.ADMIN_PRIVATE_KEY!.split(/\r?\n/) | ||
.map((line) => line.trim()) | ||
.join("\n"); | ||
const LOG_MESSAGE = process.env.LOG_MESSAGE!; | ||
|
||
const ALGORITHM = "aes-256-cbc"; | ||
|
||
let [_original, _date, _level, iv, message] = LOG_MESSAGE.match(/(\[.+])\s*(\[.+])\s*(\[iv:.+])\s*(.+)/)!; | ||
|
||
// recovery the client log key | ||
const encryptedClientKey = Buffer.from(CLIENT_ENCRYPTED_KEY, "base64"); | ||
const clientKey = privateDecrypt(ADMIN_PRIVATE_KEY, encryptedClientKey); | ||
|
||
const decodedIV = Buffer.from(iv.substring("[iv:".length, iv.length - 1), "base64"); | ||
const decipher = createDecipheriv(ALGORITHM, clientKey, decodedIV); | ||
|
||
const decryptedLog = Buffer.concat([decipher.update(message, "base64"), decipher.final()]); | ||
|
||
console.log(decryptedLog.toString("utf-8")); | ||
const decryption = new LogDecryption(ADMIN_PRIVATE_KEY); | ||
console.log(decryption.decrypt(LOG_MESSAGE)); |