2023-10-31 06:31:59 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
#
|
2018-02-04 14:41:30 +00:00
|
|
|
# Copyright (c) 2017 PySecretHandshake contributors (see AUTHORS for more details)
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
2023-10-29 08:55:39 +00:00
|
|
|
"""Utility functions"""
|
2018-02-04 14:41:30 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
import struct
|
2023-10-30 04:47:28 +00:00
|
|
|
from typing import Generator, Sequence, TypeVar
|
2018-02-04 14:41:30 +00:00
|
|
|
|
2017-06-05 09:35:55 +00:00
|
|
|
NONCE_SIZE = 24
|
2023-10-29 08:47:57 +00:00
|
|
|
MAX_NONCE = 8 * NONCE_SIZE
|
2023-10-30 04:47:28 +00:00
|
|
|
T = TypeVar("T")
|
2017-06-05 09:35:55 +00:00
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
def inc_nonce(nonce: bytes) -> bytes:
|
2023-10-29 08:55:39 +00:00
|
|
|
"""Increment nonce"""
|
|
|
|
|
2017-06-05 09:35:55 +00:00
|
|
|
num = bytes_to_long(nonce) + 1
|
2023-10-29 08:55:39 +00:00
|
|
|
|
2023-10-29 08:47:57 +00:00
|
|
|
if num > 2**MAX_NONCE:
|
2017-06-05 09:35:55 +00:00
|
|
|
num = 0
|
2023-10-29 08:55:39 +00:00
|
|
|
|
2017-06-05 09:35:55 +00:00
|
|
|
bnum = long_to_bytes(num)
|
2023-10-29 08:47:57 +00:00
|
|
|
bnum = b"\x00" * (NONCE_SIZE - len(bnum)) + bnum
|
2023-10-29 08:55:39 +00:00
|
|
|
|
2017-06-05 09:35:55 +00:00
|
|
|
return bnum
|
2017-06-04 19:50:49 +00:00
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
def split_chunks(seq: Sequence[T], n: int) -> Generator[Sequence[T], None, None]:
|
2017-06-05 09:35:55 +00:00
|
|
|
"""Split sequence in equal-sized chunks.
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-05 09:35:55 +00:00
|
|
|
The last chunk is not padded."""
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-05 09:35:55 +00:00
|
|
|
while seq:
|
|
|
|
yield seq[:n]
|
|
|
|
seq = seq[n:]
|
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
def long_to_bytes(n: int, blocksize: int = 0) -> bytes:
|
|
|
|
"""Convert a long integer to a byte string.
|
|
|
|
|
|
|
|
If optional ``blocksize`` is given and greater than zero, pad the front of the byte string with binary zeros so
|
|
|
|
that the length is a multiple of blocksize.
|
2017-06-04 19:50:49 +00:00
|
|
|
"""
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
# after much testing, this algorithm was deemed to be the fastest
|
2023-11-03 06:44:28 +00:00
|
|
|
s = b""
|
2017-06-04 19:50:49 +00:00
|
|
|
pack = struct.pack
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
while n > 0:
|
2023-10-29 08:47:57 +00:00
|
|
|
s = pack(">I", n & 0xFFFFFFFF) + s
|
2017-06-04 19:50:49 +00:00
|
|
|
n = n >> 32
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
# strip off leading zeros
|
2023-10-29 08:55:39 +00:00
|
|
|
for i, c in enumerate(s):
|
2023-11-03 06:44:28 +00:00
|
|
|
if c != 0:
|
2017-06-04 19:50:49 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
# only happens when n == 0
|
2023-11-03 06:44:28 +00:00
|
|
|
s = b"\x00"
|
2017-06-04 19:50:49 +00:00
|
|
|
i = 0
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
s = s[i:]
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
# add back some pad bytes. this could be done more efficiently w.r.t. the
|
|
|
|
# de-padding being done above, but sigh...
|
|
|
|
if blocksize > 0 and len(s) % blocksize:
|
2023-11-03 06:44:28 +00:00
|
|
|
s = (blocksize - len(s) % blocksize) * b"\x00" + s
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
def bytes_to_long(s: bytes) -> int:
|
|
|
|
"""Convert a byte string to a long integer.
|
|
|
|
|
|
|
|
This is (essentially) the inverse of ``long_to_bytes()``.
|
2017-06-04 19:50:49 +00:00
|
|
|
"""
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
acc = 0
|
|
|
|
unpack = struct.unpack
|
|
|
|
length = len(s)
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
if length % 4:
|
2023-10-29 08:47:57 +00:00
|
|
|
extra = 4 - length % 4
|
2023-11-03 06:44:28 +00:00
|
|
|
s = b"\x00" * extra + s
|
2017-06-04 19:50:49 +00:00
|
|
|
length = length + extra
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
for i in range(0, length, 4):
|
2023-10-29 08:47:57 +00:00
|
|
|
acc = (acc << 32) + unpack(">I", s[i : i + 4])[0]
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2017-06-04 19:50:49 +00:00
|
|
|
return acc
|