b685fdd3a683a781d48c35832818f9c4e0f6790a
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
13) from collections.abc import Callable
14) from typing import TypeAlias
15) 
16) from typing_extensions import assert_type
17) 
18) from derivepassphrase import sequin, ssh_agent
19) 
Marco Ricci Change the author e-mail ad...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

21) 
22) 
23) _CHARSETS = collections.OrderedDict([
24)     ('lower', b'abcdefghijklmnopqrstuvwxyz'),
25)     ('upper', b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
26)     ('alpha', b''),  # Placeholder.
27)     ('number', b'0123456789'),
28)     ('alphanum', b''),  # Placeholder.
29)     ('space', b' '),
30)     ('dash', b'-_'),
31)     ('symbol', b'!"#$%&\'()*+,./:;<=>?@[\\]^{|}~-_'),
32)     ('all', b''),  # Placeholder.
33) ])
34) _CHARSETS['alpha'] = _CHARSETS['lower'] + _CHARSETS['upper']
35) _CHARSETS['alphanum'] = _CHARSETS['alpha'] + _CHARSETS['number']
36) _CHARSETS['all'] = (
37)     _CHARSETS['alphanum'] + _CHARSETS['space'] + _CHARSETS['symbol']
38) )
39) 
40) 
41) class Vault:
42)     """A work-alike of James Coglan's vault.
43) 
44)     Store settings for generating (actually: deriving) passphrases for
45)     named services, with various constraints, given only a master
46)     passphrase.  Also, actually generate the passphrase.  The derivation
47)     is deterministic and non-secret; only the master passphrase need be
48)     kept secret.  The implementation is compatible with [vault][].
49) 
50)     [James Coglan explains the passphrase derivation algorithm in great
51)     detail][ALGORITHM] in his blog post on said topic: A principally
52)     infinite bit stream is obtained by running a key-derivation function
53)     on the master passphrase and the service name, then this bit stream
Marco Ricci Update documentation to use...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

58) 
59)     [vault]: https://getvau.lt
60)     [ALGORITHM]: https://blog.jcoglan.com/2012/07/16/designing-vaults-generator-algorithm/
61) 
62)     """
63) 
64)     _UUID = b'e87eb0f4-34cb-46b9-93ad-766c5ab063e7'
65)     """A tag used by vault in the bit stream generation."""
66)     _CHARSETS = _CHARSETS
67)     """
68)         Known character sets from which to draw passphrase characters.
69)         Relies on a certain, fixed order for their definition and their
70)         contents.
71) 
72)     """
73) 
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

75)         self,
76)         *,
77)         phrase: bytes | bytearray | str = b'',
78)         length: int = 20,
79)         repeat: int = 0,
80)         lower: int | None = None,
81)         upper: int | None = None,
82)         number: int | None = None,
83)         space: int | None = None,
84)         dash: int | None = None,
85)         symbol: int | None = None,
86)     ) -> None:
87)         """Initialize the Vault object.
88) 
89)         Args:
90)             phrase:
91)                 The master passphrase from which to derive the service
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

94)             length:
95)                 Desired passphrase length.
96)             repeat:
97)                 The maximum number of immediate character repetitions
98)                 allowed in the passphrase.  Disabled if set to 0.
99)             lower:
100)                 Optional constraint on ASCII lowercase characters.  If
101)                 positive, include this many lowercase characters
102)                 somewhere in the passphrase.  If 0, avoid lowercase
103)                 characters altogether.
104)             upper:
105)                 Same as `lower`, but for ASCII uppercase characters.
106)             number:
107)                 Same as `lower`, but for ASCII digits.
108)             space:
109)                 Same as `lower`, but for the space character.
110)             dash:
111)                 Same as `lower`, but for the hyphen-minus and underscore
112)                 characters.
113)             symbol:
114)                 Same as `lower`, but for all other hitherto unlisted
115)                 ASCII printable characters (except backquote).
116) 
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 3 months ago

117)         Raises:
118)             ValueError:
119)                 Conflicting passphrase constraints.  Permit more
120)                 characters, or increase the desired passphrase length.
121) 
Marco Ricci Add hypothesis-based tests...

Marco Ricci authored 2 months ago

122)         Warning:
123)             Because of repetition constraints, it is not always possible
124)             to detect conflicting passphrase constraints at construction
125)             time.
126) 
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

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

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

222)         # Ensure the bound is strictly positive.
223)         entropy_bound = max(1, self._entropy())
224)         return int(math.ceil(safety_factor * entropy_bound / 8))
225) 
226)     @staticmethod
227)     def _get_binary_string(s: bytes | bytearray | str, /) -> bytes:
228)         """Convert the input string to a read-only, binary string.
229) 
Marco Ricci Allow all textual strings,...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

232) 
233)         Args:
234)             s: The string to (check and) convert.
235) 
236)         Returns:
237)             A read-only, binary copy of the string.
238) 
239)         """
240)         if isinstance(s, str):
241)             return s.encode('UTF-8')
242)         return bytes(s)
243) 
244)     @classmethod
245)     def create_hash(
246)         cls,
247)         phrase: bytes | bytearray | str,
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

249)         *,
250)         length: int = 32,
251)     ) -> bytes:
252)         r"""Create a pseudorandom byte stream from phrase and service.
253) 
254)         Create a pseudorandom byte stream from `phrase` and `service` by
255)         feeding them into the key-derivation function PBKDF2
256)         (8 iterations, using SHA-1).
257) 
258)         Args:
259)             phrase:
260)                 A master passphrase, or sometimes an SSH signature.
261)                 Used as the key for PBKDF2, the underlying cryptographic
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

269)             length:
270)                 The length of the byte stream to generate.
271) 
272)         Returns:
273)             A pseudorandom byte string of length `length`.
274) 
275)         Note:
276)             Shorter values returned from this method (with the same key
277)             and message) are prefixes of longer values returned from
278)             this method.  (This property is inherited from the
279)             underlying PBKDF2 function.)  It is thus safe (if slow) to
280)             call this method with the same input with ever-increasing
281)             target lengths.
282) 
283)         Examples:
284)             >>> # See also Vault.phrase_from_key examples.
285)             >>> phrase = bytes.fromhex('''
286)             ... 00 00 00 0b 73 73 68 2d 65 64 32 35 35 31 39
287)             ... 00 00 00 40
288)             ... f0 98 19 80 6c 1a 97 d5 26 03 6e cc e3 65 8f 86
289)             ... 66 07 13 19 13 09 21 33 33 f9 e4 36 53 1d af fd
290)             ... 0d 08 1f ec f8 73 9b 8c 5f 55 39 16 7c 53 54 2c
291)             ... 1e 52 bb 30 ed 7f 89 e2 2f 69 51 55 d8 9e a6 02
292)             ... ''')
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

294)             b'M\xb1<S'
295)             >>> Vault.create_hash(phrase, b'some_service', length=16)
296)             b'M\xb1<S\x827E\xd1M\xaf\xf8~\xc8n\x10\xcc'
297)             >>> Vault.create_hash(phrase, b'NOSUCHSERVICE', length=16)
298)             b'\x1c\xc3\x9c\xd9\xb6\x1a\x99CS\x07\xc41\xf4\x85#s'
299) 
300)         """
301)         phrase = cls._get_binary_string(phrase)
302)         assert not isinstance(phrase, str)
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

304)         return hashlib.pbkdf2_hmac(
305)             hash_name='sha1',
306)             password=phrase,
307)             salt=salt,
308)             iterations=8,
309)             dklen=length,
310)         )
311) 
312)     def generate(
313)         self,
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

315)         /,
316)         *,
317)         phrase: bytes | bytearray | str = b'',
318)     ) -> bytes:
319)         r"""Generate a service passphrase.
320) 
321)         Args:
322)             service_name:
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

329) 
330)         Returns:
331)             The service passphrase.
332) 
Marco Ricci Add hypothesis-based tests...

Marco Ricci authored 2 months ago

333)         Raises:
334)             ValueError:
335)                 Conflicting passphrase constraints.  Permit more
336)                 characters, or increase the desired passphrase length.
337) 
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

338)         Examples:
339)             >>> phrase = b'She cells C shells bye the sea shoars'
340)             >>> # Using default options in constructor.
341)             >>> Vault(phrase=phrase).generate(b'google')
342)             b': 4TVH#5:aZl8LueOT\\{'
343)             >>> # Also possible:
344)             >>> Vault().generate(b'google', phrase=phrase)
345)             b': 4TVH#5:aZl8LueOT\\{'
346) 
Marco Ricci Add hypothesis-based tests...

Marco Ricci authored 2 months ago

347)             Conflicting constraints are sometimes only found during
348)             generation.
349) 
350)             >>> # Note: no error here...
351)             >>> v = Vault(
352)             ...     lower=0,
353)             ...     upper=0,
354)             ...     number=0,
355)             ...     space=2,
356)             ...     dash=0,
357)             ...     symbol=1,
358)             ...     repeat=2,
359)             ...     length=3,
360)             ... )
361)             >>> # ... but here.
362)             >>> v.generate(
363)             ...     '0', phrase=b'\x00'
364)             ... )  # doctest: +IGNORE_EXCEPTION_DETAIL
365)             Traceback (most recent call last):
366)                 ...
367)             ValueError: no allowed characters left
368) 
369) 
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

370)         """
371)         hash_length = self._estimate_sufficient_hash_length()
372)         assert hash_length >= 1
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

376)         assert_type(service_name, bytes)
377)         if not phrase:
378)             phrase = self._phrase
379)         phrase = self._get_binary_string(phrase)
Marco Ricci Support text string service...

Marco Ricci authored 3 months ago

380)         assert_type(phrase, bytes)
Marco Ricci Move `sequin` and `ssh_agen...

Marco Ricci authored 4 months ago

381)         # Repeat the passphrase generation with ever-increasing hash
382)         # lengths, until the passphrase can be formed without exhausting
383)         # the sequin.  See the guarantee in the create_hash method for
384)         # why this works.
385)         while True:
386)             try:
387)                 required = self._required[:]
388)                 seq = sequin.Sequin(
389)                     self.create_hash(
390)                         phrase=phrase, service=service_name, length=hash_length
391)                     )
392)                 )
393)                 result = bytearray()
394)                 while len(result) < self._length:
395)                     pos = seq.generate(len(required))
396)                     charset = required.pop(pos)
397)                     # Determine if an unlucky choice right now might
398)                     # violate the restriction on repeated characters.
399)                     # That is, check if the current partial passphrase
400)                     # ends with r - 1 copies of the same character
401)                     # (where r is the repeat limit that must not be
402)                     # reached), and if so, remove this same character
403)                     # from the current character's allowed set.
404)                     if self._repeat and result:
405)                         bad_suffix = bytes(result[-1:]) * (self._repeat - 1)
406)                         if result.endswith(bad_suffix):
407)                             charset = self._subtract(
408)                                 bytes(result[-1:]), charset
409)                             )
410)                     pos = seq.generate(len(charset))
411)                     result.extend(charset[pos : pos + 1])
Marco Ricci Add hypothesis-based tests...

Marco Ricci authored 2 months ago

412)             except ValueError as exc:
413)                 msg = 'no allowed characters left'
414)                 raise ValueError(msg) from exc