340fd281628f1dc2498037e636f1772ec662bbd4
Marco Ricci Change the author e-mail ad...

Marco Ricci authored 3 months ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <software@the13thletter.info>
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

2) #
3) # SPDX-License-Identifier: MIT
4) 
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

5) """Python port of the vault(1) password generation scheme."""
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

6) 
7) from __future__ import annotations
8) 
9) import base64
10) import collections
11) import hashlib
12) import math
Marco Ricci Fix miscellaneous small doc...

Marco Ricci authored 3 months ago

13) import types
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

14) from collections.abc import Callable
15) from typing import TypeAlias
16) 
17) from typing_extensions import assert_type
18) 
19) from derivepassphrase import sequin, ssh_agent
20) 
Marco Ricci Change the author e-mail ad...

Marco Ricci authored 3 months ago

21) __author__ = 'Marco Ricci <software@the13thletter.info>'
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

22) 
23) 
24) class Vault:
25)     """A work-alike of James Coglan's vault.
26) 
27)     Store settings for generating (actually: deriving) passphrases for
28)     named services, with various constraints, given only a master
29)     passphrase.  Also, actually generate the passphrase.  The derivation
30)     is deterministic and non-secret; only the master passphrase need be
31)     kept secret.  The implementation is compatible with [vault][].
32) 
33)     [James Coglan explains the passphrase derivation algorithm in great
34)     detail][ALGORITHM] in his blog post on said topic: A principally
35)     infinite bit stream is obtained by running a key-derivation function
36)     on the master passphrase and the service name, then this bit stream
Marco Ricci Update documentation to use...

Marco Ricci authored 4 months ago

37)     is fed into a [Sequin][derivepassphrase.sequin.Sequin] to generate
38)     random numbers in the correct range, and finally these random
39)     numbers select passphrase characters until the desired length is
40)     reached.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

41) 
42)     [vault]: https://getvau.lt
43)     [ALGORITHM]: https://blog.jcoglan.com/2012/07/16/designing-vaults-generator-algorithm/
44) 
45)     """
46) 
47)     _UUID = b'e87eb0f4-34cb-46b9-93ad-766c5ab063e7'
48)     """A tag used by vault in the bit stream generation."""
Marco Ricci Fix miscellaneous small doc...

Marco Ricci authored 3 months ago

49)     _CHARSETS = types.MappingProxyType(
50)         collections.OrderedDict([
51)             ('lower', b'abcdefghijklmnopqrstuvwxyz'),
52)             ('upper', b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
53)             (
54)                 'alpha',
55)                 (
56)                     # _CHARSETS['lower']
57)                     b'abcdefghijklmnopqrstuvwxyz'
58)                     # _CHARSETS['upper']
59)                     b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
60)                 ),
61)             ),
62)             ('number', b'0123456789'),
63)             (
64)                 'alphanum',
65)                 (
66)                     # _CHARSETS['lower']
67)                     b'abcdefghijklmnopqrstuvwxyz'
68)                     # _CHARSETS['upper']
69)                     b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
70)                     # _CHARSETS['number']
71)                     b'0123456789'
72)                 ),
73)             ),
74)             ('space', b' '),
75)             ('dash', b'-_'),
76)             ('symbol', b'!"#$%&\'()*+,./:;<=>?@[\\]^{|}~-_'),
77)             (
78)                 'all',
79)                 (
80)                     # _CHARSETS['lower']
81)                     b'abcdefghijklmnopqrstuvwxyz'
82)                     # _CHARSETS['upper']
83)                     b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
84)                     # _CHARSETS['number']
85)                     b'0123456789'
86)                     # _CHARSETS['space']
87)                     b' '
88)                     # _CHARSETS['symbol']
89)                     b'!"#$%&\'()*+,./:;<=>?@[\\]^{|}~-_'
90)                 ),
91)             ),
92)         ])
93)     )
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

94)     """
95)         Known character sets from which to draw passphrase characters.
96)         Relies on a certain, fixed order for their definition and their
97)         contents.
98) 
99)     """
100) 
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

101)     def __init__(  # noqa: PLR0913
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

102)         self,
103)         *,
104)         phrase: bytes | bytearray | str = b'',
105)         length: int = 20,
106)         repeat: int = 0,
107)         lower: int | None = None,
108)         upper: int | None = None,
109)         number: int | None = None,
110)         space: int | None = None,
111)         dash: int | None = None,
112)         symbol: int | None = None,
113)     ) -> None:
114)         """Initialize the Vault object.
115) 
116)         Args:
117)             phrase:
118)                 The master passphrase from which to derive the service
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

119)                 passphrases.  If a string, then the UTF-8 encoding of
120)                 the string is used.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

121)             length:
122)                 Desired passphrase length.
123)             repeat:
124)                 The maximum number of immediate character repetitions
125)                 allowed in the passphrase.  Disabled if set to 0.
126)             lower:
127)                 Optional constraint on ASCII lowercase characters.  If
128)                 positive, include this many lowercase characters
129)                 somewhere in the passphrase.  If 0, avoid lowercase
130)                 characters altogether.
131)             upper:
132)                 Same as `lower`, but for ASCII uppercase characters.
133)             number:
134)                 Same as `lower`, but for ASCII digits.
135)             space:
136)                 Same as `lower`, but for the space character.
137)             dash:
138)                 Same as `lower`, but for the hyphen-minus and underscore
139)                 characters.
140)             symbol:
141)                 Same as `lower`, but for all other hitherto unlisted
142)                 ASCII printable characters (except backquote).
143) 
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

144)         Raises:
145)             ValueError:
146)                 Conflicting passphrase constraints.  Permit more
147)                 characters, or increase the desired passphrase length.
148) 
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

149)         """
150)         self._phrase = self._get_binary_string(phrase)
151)         self._length = length
152)         self._repeat = repeat
153)         self._allowed = bytearray(self._CHARSETS['all'])
154)         self._required: list[bytes] = []
155) 
156)         def subtract_or_require(
157)             count: int | None, characters: bytes | bytearray
158)         ) -> None:
159)             if not isinstance(count, int):
160)                 return
161)             if count <= 0:
162)                 self._allowed = self._subtract(characters, self._allowed)
163)             else:
164)                 for _ in range(count):
165)                     self._required.append(characters)
166) 
167)         subtract_or_require(lower, self._CHARSETS['lower'])
168)         subtract_or_require(upper, self._CHARSETS['upper'])
169)         subtract_or_require(number, self._CHARSETS['number'])
170)         subtract_or_require(space, self._CHARSETS['space'])
171)         subtract_or_require(dash, self._CHARSETS['dash'])
172)         subtract_or_require(symbol, self._CHARSETS['symbol'])
173)         if len(self._required) > self._length:
174)             msg = 'requested passphrase length too short'
175)             raise ValueError(msg)
176)         if not self._allowed:
177)             msg = 'no allowed characters left'
178)             raise ValueError(msg)
179)         for _ in range(len(self._required), self._length):
180)             self._required.append(bytes(self._allowed))
181) 
182)     def _entropy(self) -> float:
183)         """Estimate the passphrase entropy, given the current settings.
184) 
185)         The entropy is the base 2 logarithm of the amount of
186)         possibilities.  We operate directly on the logarithms, and use
187)         sorting and [`math.fsum`][] to keep high accuracy.
188) 
189)         Note:
190)             We actually overestimate the entropy here because of poor
191)             handling of character repetitions.  In the extreme, assuming
192)             that only one character were allowed, then because there is
193)             only one possible string of each given length, the entropy
194)             of that string `s` is always be zero.  However, we calculate
195)             the entropy as `math.log2(math.factorial(len(s)))`, i.e. we
196)             assume the characters at the respective string position are
197)             distinguishable from each other.
198) 
199)         Returns:
200)             A valid (and somewhat close) upper bound to the entropy.
201) 
202)         """
203)         factors: list[int] = []
204)         if not self._required or any(not x for x in self._required):
205)             return float('-inf')
206)         for i, charset in enumerate(self._required):
207)             factors.extend([i + 1, len(charset)])
208)         factors.sort()
209)         return math.fsum(math.log2(f) for f in factors)
210) 
211)     def _estimate_sufficient_hash_length(
212)         self,
213)         safety_factor: float = 2.0,
214)     ) -> int:
215)         """Estimate the sufficient hash length, given the current settings.
216) 
217)         Using the entropy (via `_entropy`) and a safety factor, give an
218)         initial estimate of the length to use for `create_hash` such
219)         that using a `Sequin` with this hash will not exhaust it during
220)         passphrase generation.
221) 
222)         Args:
223)             safety_factor: The safety factor.  Must be at least 1.
224) 
225)         Returns:
226)             The estimated sufficient hash length.
227) 
228)         Warning:
229)             This is a heuristic, not an exact computation; it may
230)             underestimate the true necessary hash length.  It is
231)             intended as a starting point for searching for a sufficient
232)             hash length, usually by doubling the hash length each time
233)             it does not yet prove so.
234) 
235)         """
236)         try:
237)             safety_factor = float(safety_factor)
238)         except TypeError as e:
239)             msg = f'invalid safety factor: not a float: {safety_factor!r}'
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

240)             raise TypeError(msg) from e  # noqa: DOC501
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

241)         if not math.isfinite(safety_factor) or safety_factor < 1.0:
242)             msg = f'invalid safety factor {safety_factor!r}'
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

243)             raise ValueError(msg)  # noqa: DOC501
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

244)         # Ensure the bound is strictly positive.
245)         entropy_bound = max(1, self._entropy())
246)         return int(math.ceil(safety_factor * entropy_bound / 8))
247) 
248)     @staticmethod
249)     def _get_binary_string(s: bytes | bytearray | str, /) -> bytes:
250)         """Convert the input string to a read-only, binary string.
251) 
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

252)         If it is a text string, return the string's UTF-8
253)         representation.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

254) 
255)         Args:
256)             s: The string to (check and) convert.
257) 
258)         Returns:
259)             A read-only, binary copy of the string.
260) 
261)         """
262)         if isinstance(s, str):
263)             return s.encode('UTF-8')
264)         return bytes(s)
265) 
266)     @classmethod
267)     def create_hash(
268)         cls,
269)         phrase: bytes | bytearray | str,
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

270)         service: bytes | bytearray | str,
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

271)         *,
272)         length: int = 32,
273)     ) -> bytes:
274)         r"""Create a pseudorandom byte stream from phrase and service.
275) 
276)         Create a pseudorandom byte stream from `phrase` and `service` by
277)         feeding them into the key-derivation function PBKDF2
278)         (8 iterations, using SHA-1).
279) 
280)         Args:
281)             phrase:
282)                 A master passphrase, or sometimes an SSH signature.
283)                 Used as the key for PBKDF2, the underlying cryptographic
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

284)                 primitive.  If a string, then the UTF-8 encoding of the
285)                 string is used.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

286)             service:
287)                 A vault service name.  Will be suffixed with
288)                 `Vault._UUID`, and then used as the salt value for
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

289)                 PBKDF2.  If a string, then the UTF-8 encoding of the
290)                 string is used.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

291)             length:
292)                 The length of the byte stream to generate.
293) 
294)         Returns:
295)             A pseudorandom byte string of length `length`.
296) 
297)         Note:
298)             Shorter values returned from this method (with the same key
299)             and message) are prefixes of longer values returned from
300)             this method.  (This property is inherited from the
301)             underlying PBKDF2 function.)  It is thus safe (if slow) to
302)             call this method with the same input with ever-increasing
303)             target lengths.
304) 
305)         Examples:
306)             >>> # See also Vault.phrase_from_key examples.
307)             >>> phrase = bytes.fromhex('''
308)             ... 00 00 00 0b 73 73 68 2d 65 64 32 35 35 31 39
309)             ... 00 00 00 40
310)             ... f0 98 19 80 6c 1a 97 d5 26 03 6e cc e3 65 8f 86
311)             ... 66 07 13 19 13 09 21 33 33 f9 e4 36 53 1d af fd
312)             ... 0d 08 1f ec f8 73 9b 8c 5f 55 39 16 7c 53 54 2c
313)             ... 1e 52 bb 30 ed 7f 89 e2 2f 69 51 55 d8 9e a6 02
314)             ... ''')
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

315)             >>> Vault.create_hash(phrase, 'some_service', length=4)
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

316)             b'M\xb1<S'
317)             >>> Vault.create_hash(phrase, b'some_service', length=16)
318)             b'M\xb1<S\x827E\xd1M\xaf\xf8~\xc8n\x10\xcc'
319)             >>> Vault.create_hash(phrase, b'NOSUCHSERVICE', length=16)
320)             b'\x1c\xc3\x9c\xd9\xb6\x1a\x99CS\x07\xc41\xf4\x85#s'
321) 
322)         """
323)         phrase = cls._get_binary_string(phrase)
324)         assert not isinstance(phrase, str)
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

325)         salt = cls._get_binary_string(service) + cls._UUID
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

326)         return hashlib.pbkdf2_hmac(
327)             hash_name='sha1',
328)             password=phrase,
329)             salt=salt,
330)             iterations=8,
331)             dklen=length,
332)         )
333) 
334)     def generate(
335)         self,
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

336)         service_name: bytes | bytearray | str,
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

337)         /,
338)         *,
339)         phrase: bytes | bytearray | str = b'',
340)     ) -> bytes:
341)         r"""Generate a service passphrase.
342) 
343)         Args:
344)             service_name:
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

345)                 The service name.  If a string, then the UTF-8 encoding
346)                 of the string is used.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

347)             phrase:
348)                 If given, override the passphrase given during
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

349)                 construction.  If a string, then the UTF-8 encoding of
350)                 the string is used.
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

351) 
352)         Returns:
353)             The service passphrase.
354) 
355)         Examples:
356)             >>> phrase = b'She cells C shells bye the sea shoars'
357)             >>> # Using default options in constructor.
358)             >>> Vault(phrase=phrase).generate(b'google')
359)             b': 4TVH#5:aZl8LueOT\\{'
360)             >>> # Also possible:
361)             >>> Vault().generate(b'google', phrase=phrase)
362)             b': 4TVH#5:aZl8LueOT\\{'
363) 
364)         """
365)         hash_length = self._estimate_sufficient_hash_length()
366)         assert hash_length >= 1
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

367)         # Ensure the phrase and the service name are bytes objects.
368)         # This is needed later for safe concatenation.
369)         service_name = self._get_binary_string(service_name)
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

370)         assert_type(service_name, bytes)
371)         if not phrase:
372)             phrase = self._phrase
373)         phrase = self._get_binary_string(phrase)
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

374)         assert_type(phrase, bytes)