upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/49.md
diff options
context:
space:
mode:
Diffstat (limited to '49.md')
-rw-r--r--49.md126
1 files changed, 126 insertions, 0 deletions
diff --git a/49.md b/49.md
new file mode 100644
index 0000000..731f132
--- /dev/null
+++ b/49.md
@@ -0,0 +1,126 @@
1
2NIP-49
3======
4
5Private Key Encryption
6----------------------
7
8`draft` `optional`
9
10This NIP defines a method by which clients can encrypt (and decrypt) a user's private key with a password.
11
12Symmetric Encryption Key derivation
13-----------------------------------
14
15PASSWORD = Read from the user. The password should be unicode normalized to NFKC format to ensure that the password can be entered identically on other computers/clients.
16
17LOG\_N = Let the user or implementer choose one byte representing a power of 2 (e.g. 18 represents 262,144) which is used as the number of rounds for scrypt. Larger numbers take more time and more memory, and offer better protection:
18
19 | LOG_N | MEMORY REQUIRED | APPROX TIME ON FAST COMPUTER |
20 |-------|-----------------|----------------------------- |
21 | 16 | 64 MiB | 100 ms |
22 | 18 | 256 MiB | |
23 | 20 | 1 GiB | 2 seconds |
24 | 21 | 2 GiB | |
25 | 22 | 4 GiB | |
26
27SALT = 16 random bytes
28
29SYMMETRIC_KEY = scrypt(password=PASSWORD, salt=SALT, log\_n=LOG\_N, r=8, p=1)
30
31The symmetric key should be 32 bytes long.
32
33This symmetric encryption key is temporary and should be zeroed and discarded after use and not stored or reused for any other purpose.
34
35
36Encrypting a private key
37------------------------
38
39The private key encryption process is as follows:
40
41PRIVATE\_KEY = User's private (secret) secp256k1 key as 32 raw bytes (not hex or bech32 encoded!)
42
43KEY\_SECURITY\_BYTE = one of:
44
45* 0x00 - if the key has been known to have been handled insecurely (stored unencrypted, cut and paste unencrypted, etc)
46* 0x01 - if the key has NOT been known to have been handled insecurely (stored unencrypted, cut and paste unencrypted, etc)
47 * 0x02 - if the client does not track this data
48
49ASSOCIATED\_DATA = KEY\_SECURITY\_BYTE
50
51NONCE = 24 byte random nonce
52
53CIPHERTEXT = XChaCha20-Poly1305(
54 plaintext=PRIVATE\_KEY,
55 associated_data=ASSOCIATED\_DATA,
56 nonce=NONCE,
57 key=SYMMETRIC\_KEY
58)
59
60VERSION\_NUMBER = 0x02
61
62CIPHERTEXT_CONCATENATION = concat(
63 VERSION\_NUMBER,
64 LOG\_N,
65 SALT,
66 NONCE,
67 ASSOCIATED\_DATA,
68 CIPHERTEXT
69)
70
71ENCRYPTED\_PRIVATE\_KEY = bech32_encode('ncryptsec', CIPHERTEXT\_CONCATENATION)
72
73The output prior to bech32 encoding should be 91 bytes long.
74
75The decryption process operates in the reverse.
76
77
78Test Data
79---------
80
81## Password Unicode Normalization
82
83The following password input: "ÅΩẛ̣"
84- Unicode Codepoints: U+212B U+2126 U+1E9B U+0323
85- UTF-8 bytes: [0xE2, 0x84, 0xAB, 0xE2, 0x84, 0xA6, 0xE1, 0xBA, 0x9B, 0xCC, 0xA3]
86
87Should be converted into the unicode normalized NFKC format prior to use in scrypt: "ÅΩẛ̣"
88- Unicode Codepoints: U+00C5 U+03A9 U+1E69
89- UTF-8 bytes: [0xC3, 0x85, 0xCE, 0xA9, 0xE1, 0xB9, 0xA9]
90
91## Encryption
92
93The encryption process is non-deterministic due to the random nonce.
94
95## Decryption
96
97The following encrypted private key:
98
99`ncryptsec1qgg9947rlpvqu76pj5ecreduf9jxhselq2nae2kghhvd5g7dgjtcxfqtd67p9m0w57lspw8gsq6yphnm8623nsl8xn9j4jdzz84zm3frztj3z7s35vpzmqf6ksu8r89qk5z2zxfmu5gv8th8wclt0h4p`
100
101When decrypted with password='nostr' and log_n=16 yields the following hex-encoded private key:
102
103`3501454135014541350145413501453fefb02227e449e57cf4d3a3ce05378683`
104
105Discussion
106----------
107
108### On Key Derivation
109
110Passwords make poor cryptographic keys. Prior to use as a cryptographic key, two things need to happen:
111
1121. An encryption key needs to be deterministically created from the password such that is has a uniform functionally random distribution of bits, such that the symmetric encryption algorithm's assumptions are valid, and
1132. A slow irreversible algorithm should be injected into the process, so that brute-force attempts to decrypt by trying many passwords are severely hampered.
114
115These are achieved using a password-based key derivation function. We use scrypt, which has been proven to be maximally memory hard and which several cryptographers have indicated to the author is better than argon2 even though argon2 won a competition in 2015.
116
117### On the symmetric encryption algorithm
118
119XChaCha20-Poly1305 is typically favored by cryptographers over AES and is less associated with the U.S. government. It (or it's earlier variant without the 'X') is gaining wide usage, is used in TLS and OpenSSH, and is available in most modern crypto libraries.
120
121Recommendations
122---------
123
124It is not recommended that users publish these encrypted private keys to nostr, as cracking a key may become easier when an attacker can amass many encrypted private keys.
125
126It is recommended that clients zero out the memory of passwords and private keys before freeing that memory.