sinha 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sinha/__init__.py +96 -0
- sinha/__main__.py +8 -0
- sinha/cipher/__init__.py +13 -0
- sinha/cipher/aead.py +91 -0
- sinha/cipher/api.py +89 -0
- sinha/cli.py +505 -0
- sinha/core/__init__.py +27 -0
- sinha/core/constants.py +6 -0
- sinha/core/gf.py +21 -0
- sinha/core/permutation.py +82 -0
- sinha/core/sponge.py +32 -0
- sinha/encoding/__init__.py +13 -0
- sinha/encoding/checks.py +75 -0
- sinha/encoding/codec.py +63 -0
- sinha/hashing/__init__.py +28 -0
- sinha/hashing/rolling.py +63 -0
- sinha/hashing/sponge_hash.py +90 -0
- sinha/kdf/__init__.py +16 -0
- sinha/kdf/passwords.py +100 -0
- sinha/kdf/romix.py +44 -0
- sinha-1.0.0.dist-info/METADATA +285 -0
- sinha-1.0.0.dist-info/RECORD +25 -0
- sinha-1.0.0.dist-info/WHEEL +5 -0
- sinha-1.0.0.dist-info/entry_points.txt +2 -0
- sinha-1.0.0.dist-info/top_level.txt +1 -0
sinha/__init__.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Sinha v1.0.0 experimental cryptographic toolkit.
|
|
2
|
+
|
|
3
|
+
This package is a research prototype and has not received independent
|
|
4
|
+
cryptanalysis. Use a standard, reviewed cryptographic library for real secrets.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .cipher import (
|
|
8
|
+
LABEL,
|
|
9
|
+
MIN_BITS,
|
|
10
|
+
MIN_PAYLOAD,
|
|
11
|
+
SinhaCipher,
|
|
12
|
+
_ad,
|
|
13
|
+
_check_cost,
|
|
14
|
+
_padme,
|
|
15
|
+
_payload_len,
|
|
16
|
+
_secret,
|
|
17
|
+
_wrap,
|
|
18
|
+
decrypt,
|
|
19
|
+
encrypt,
|
|
20
|
+
)
|
|
21
|
+
from .core import (
|
|
22
|
+
M64,
|
|
23
|
+
MDS,
|
|
24
|
+
RATE,
|
|
25
|
+
ROUNDS,
|
|
26
|
+
SBOX,
|
|
27
|
+
T0,
|
|
28
|
+
T1,
|
|
29
|
+
T2,
|
|
30
|
+
T3,
|
|
31
|
+
T4,
|
|
32
|
+
T5,
|
|
33
|
+
T6,
|
|
34
|
+
T7,
|
|
35
|
+
TAG_LEN,
|
|
36
|
+
_RC,
|
|
37
|
+
_bytes,
|
|
38
|
+
_p,
|
|
39
|
+
_xor_into,
|
|
40
|
+
ginv,
|
|
41
|
+
gmul,
|
|
42
|
+
perm,
|
|
43
|
+
xof,
|
|
44
|
+
)
|
|
45
|
+
from .encoding import (
|
|
46
|
+
ALPHABET,
|
|
47
|
+
_ct_eq,
|
|
48
|
+
_from_letters,
|
|
49
|
+
_group,
|
|
50
|
+
_parse_key,
|
|
51
|
+
_to_letters,
|
|
52
|
+
check_letters,
|
|
53
|
+
correct_check,
|
|
54
|
+
generate_key,
|
|
55
|
+
verify_check,
|
|
56
|
+
with_check,
|
|
57
|
+
)
|
|
58
|
+
from .hashing import (
|
|
59
|
+
DOM_HASH,
|
|
60
|
+
DOM_MAC,
|
|
61
|
+
DOM_ROLL,
|
|
62
|
+
HASH_LETTERS,
|
|
63
|
+
P61,
|
|
64
|
+
SinhaHash,
|
|
65
|
+
SinhaRoll,
|
|
66
|
+
_letters,
|
|
67
|
+
fingerprint,
|
|
68
|
+
mac_verify,
|
|
69
|
+
sinha_hash,
|
|
70
|
+
sinha_mac,
|
|
71
|
+
)
|
|
72
|
+
from .kdf import (
|
|
73
|
+
MAX_LOGN,
|
|
74
|
+
MAX_PASSES,
|
|
75
|
+
MIN_LOGN,
|
|
76
|
+
WeakPasswordError,
|
|
77
|
+
calibrate,
|
|
78
|
+
crack_time,
|
|
79
|
+
derive_key,
|
|
80
|
+
estimate_entropy,
|
|
81
|
+
generate_passphrase,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
__version__ = "1.0.0"
|
|
85
|
+
|
|
86
|
+
__all__ = [
|
|
87
|
+
"ALPHABET", "DOM_HASH", "DOM_MAC", "DOM_ROLL", "HASH_LETTERS", "LABEL",
|
|
88
|
+
"M64", "MAX_LOGN", "MAX_PASSES", "MDS", "MIN_BITS", "MIN_LOGN",
|
|
89
|
+
"MIN_PAYLOAD", "P61", "RATE", "ROUNDS", "SBOX", "SinhaCipher", "SinhaHash",
|
|
90
|
+
"SinhaRoll", "TAG_LEN", "T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7",
|
|
91
|
+
"WeakPasswordError", "calibrate", "check_letters", "correct_check",
|
|
92
|
+
"crack_time", "decrypt", "derive_key", "encrypt", "estimate_entropy",
|
|
93
|
+
"fingerprint", "generate_key", "generate_passphrase", "ginv", "gmul",
|
|
94
|
+
"mac_verify", "perm", "sinha_hash", "sinha_mac", "verify_check",
|
|
95
|
+
"with_check", "xof",
|
|
96
|
+
]
|
sinha/__main__.py
ADDED
sinha/cipher/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Authenticated encryption API, wire-format helpers, and SinhaCipher facade."""
|
|
2
|
+
|
|
3
|
+
from .aead import LABEL, MIN_BITS, MIN_PAYLOAD, _ad, _check_cost, _padme, _payload_len, _secret, _wrap
|
|
4
|
+
from .api import SinhaCipher, decrypt, encrypt
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"LABEL",
|
|
8
|
+
"MIN_BITS",
|
|
9
|
+
"MIN_PAYLOAD",
|
|
10
|
+
"SinhaCipher",
|
|
11
|
+
"decrypt",
|
|
12
|
+
"encrypt",
|
|
13
|
+
]
|
sinha/cipher/aead.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Duplex AEAD wrap, Padme length bucketing, and key/password secret helpers."""
|
|
2
|
+
|
|
3
|
+
import struct
|
|
4
|
+
|
|
5
|
+
from ..core import RATE, _p, _xor_into, xof
|
|
6
|
+
from ..encoding import _parse_key
|
|
7
|
+
from ..kdf import MAX_LOGN, MAX_PASSES, MIN_LOGN, WeakPasswordError, derive_key, estimate_entropy
|
|
8
|
+
|
|
9
|
+
LABEL = b"SINHA3.1"
|
|
10
|
+
MIN_BITS = 64
|
|
11
|
+
MIN_PAYLOAD = 64
|
|
12
|
+
_IV = b"SINHA-WRAP-v2\x00\x00\x00"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _wrap(key, nonce, ad, data, decrypt):
|
|
16
|
+
state = bytearray(nonce + key + _IV)
|
|
17
|
+
_p(state)
|
|
18
|
+
_xor_into(state, 32, key)
|
|
19
|
+
ad = ad + b"\x80"
|
|
20
|
+
ad += b"\x00" * (-len(ad) % RATE)
|
|
21
|
+
for i in range(0, len(ad), RATE):
|
|
22
|
+
_xor_into(state, 0, ad[i:i + RATE])
|
|
23
|
+
_p(state)
|
|
24
|
+
state[63] ^= 1
|
|
25
|
+
out = bytearray()
|
|
26
|
+
full = len(data) - len(data) % RATE
|
|
27
|
+
for i in range(0, full, RATE):
|
|
28
|
+
block = data[i:i + RATE]
|
|
29
|
+
keystream = state[:RATE]
|
|
30
|
+
encrypted = bytes(a ^ b for a, b in zip(block, keystream))
|
|
31
|
+
out += encrypted
|
|
32
|
+
state[:RATE] = block if decrypt else encrypted
|
|
33
|
+
_p(state)
|
|
34
|
+
tail = data[full:]
|
|
35
|
+
size = len(tail)
|
|
36
|
+
encrypted = bytes(a ^ b for a, b in zip(tail, state[:size]))
|
|
37
|
+
out += encrypted
|
|
38
|
+
state[:size] = tail if decrypt else encrypted
|
|
39
|
+
state[size] ^= 0x80
|
|
40
|
+
_xor_into(state, 16, key)
|
|
41
|
+
_p(state)
|
|
42
|
+
tag = bytes(a ^ b for a, b in zip(state[48:64], key[16:32]))
|
|
43
|
+
return bytes(out), tag
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _padme(length):
|
|
47
|
+
if length < 2:
|
|
48
|
+
return length
|
|
49
|
+
exponent = length.bit_length() - 1
|
|
50
|
+
significant = exponent.bit_length()
|
|
51
|
+
mask = (1 << (exponent - significant)) - 1
|
|
52
|
+
return (length + mask) & ~mask
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _payload_len(plaintext_length):
|
|
56
|
+
payload = max(MIN_PAYLOAD, _padme(4 + plaintext_length))
|
|
57
|
+
return payload + (-payload % 3)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _check_cost(password, logn, passes, unsafe_low_cost):
|
|
61
|
+
if password is None:
|
|
62
|
+
return
|
|
63
|
+
minimum = 8 if unsafe_low_cost else MIN_LOGN
|
|
64
|
+
if not (minimum <= logn <= MAX_LOGN and 1 <= passes <= MAX_PASSES):
|
|
65
|
+
raise ValueError(
|
|
66
|
+
f"KDF cost පරාසයෙන් පිටත: logn {minimum}..{MAX_LOGN}, passes 1..{MAX_PASSES} "
|
|
67
|
+
"(floor සඳහා unsafe_low_cost=True tests වලට පමණි)"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _secret(password, key, salt, logn, passes, enforce, allow_weak):
|
|
72
|
+
if password is None and key is None:
|
|
73
|
+
raise ValueError("password හෝ key (හෝ දෙකම) දෙන්න")
|
|
74
|
+
keyfile = _parse_key(key) if key is not None else None
|
|
75
|
+
if password is None:
|
|
76
|
+
return keyfile
|
|
77
|
+
if keyfile is None and enforce and not allow_weak:
|
|
78
|
+
bits = estimate_entropy(password)
|
|
79
|
+
if bits < MIN_BITS:
|
|
80
|
+
raise WeakPasswordError(
|
|
81
|
+
f"Password එක දුර්වලයි (≈{bits:.0f} bits < {MIN_BITS}). "
|
|
82
|
+
"generate_passphrase() හෝ key mode / dual mode (key= ද දෙන්න)."
|
|
83
|
+
)
|
|
84
|
+
derived = derive_key(password.encode("utf-8"), salt, logn, passes)
|
|
85
|
+
return derived if keyfile is None else xof(derived + keyfile, 32, 7)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _ad(password, key, logn, passes, salt, nonce):
|
|
89
|
+
mode = (password is not None) + 2 * (key is not None)
|
|
90
|
+
cost_log, cost_passes = (logn, passes) if password is not None else (0, 0)
|
|
91
|
+
return LABEL + bytes([mode, cost_log, cost_passes]) + salt + nonce
|
sinha/cipher/api.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Public authenticated encryption functions and SinhaCipher class."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import struct
|
|
5
|
+
|
|
6
|
+
from ..core import TAG_LEN
|
|
7
|
+
from ..encoding import _ct_eq, _from_letters, _group, _to_letters
|
|
8
|
+
from ..kdf import MIN_LOGN
|
|
9
|
+
from .aead import MIN_PAYLOAD, _ad, _check_cost, _payload_len, _secret, _wrap
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def encrypt(plaintext, password=None, *, key=None, logn=MIN_LOGN, passes=1,
|
|
13
|
+
group=8, allow_weak=False, unsafe_low_cost=False):
|
|
14
|
+
"""Encrypt text or bytes, returning a grouped Sinhala-letter ciphertext."""
|
|
15
|
+
_check_cost(password, logn, passes, unsafe_low_cost)
|
|
16
|
+
data = plaintext.encode("utf-8") if isinstance(plaintext, str) else bytes(plaintext)
|
|
17
|
+
salt, nonce = os.urandom(16), os.urandom(16)
|
|
18
|
+
secret = _secret(password, key, salt, logn, passes, True, allow_weak)
|
|
19
|
+
payload = struct.pack(">I", len(data)) + data
|
|
20
|
+
payload += b"\x00" * (_payload_len(len(data)) - len(payload))
|
|
21
|
+
ciphertext, tag = _wrap(
|
|
22
|
+
secret, nonce, _ad(password, key, logn, passes, salt, nonce), payload, False
|
|
23
|
+
)
|
|
24
|
+
return _group(_to_letters(salt + nonce + ciphertext + tag), group)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def decrypt(text, password=None, *, key=None, logn=MIN_LOGN, passes=1,
|
|
28
|
+
as_str=True, unsafe_low_cost=False):
|
|
29
|
+
"""Authenticate and decrypt a Sinhala-letter ciphertext."""
|
|
30
|
+
_check_cost(password, logn, passes, unsafe_low_cost)
|
|
31
|
+
raw = _from_letters(text)
|
|
32
|
+
if len(raw) < 48 + MIN_PAYLOAD:
|
|
33
|
+
raise ValueError("Sinha ciphertext එකක් නොවේ (දිග)")
|
|
34
|
+
salt, nonce, ciphertext, tag = raw[:16], raw[16:32], raw[32:-TAG_LEN], raw[-TAG_LEN:]
|
|
35
|
+
secret = _secret(password, key, salt, logn, passes, False, True)
|
|
36
|
+
payload, expected_tag = _wrap(
|
|
37
|
+
secret, nonce, _ad(password, key, logn, passes, salt, nonce), ciphertext, True
|
|
38
|
+
)
|
|
39
|
+
if not _ct_eq(tag, expected_tag):
|
|
40
|
+
raise ValueError(
|
|
41
|
+
"Decryption fail - password/key/cost වැරදියි හෝ data වෙනස් කර ඇත"
|
|
42
|
+
)
|
|
43
|
+
(size,) = struct.unpack(">I", payload[:4])
|
|
44
|
+
if (
|
|
45
|
+
4 + size > len(payload)
|
|
46
|
+
or any(payload[4 + size:])
|
|
47
|
+
or len(raw) != 48 + _payload_len(size)
|
|
48
|
+
):
|
|
49
|
+
raise ValueError("Decryption fail - payload format වැරදියි")
|
|
50
|
+
plaintext = payload[4:4 + size]
|
|
51
|
+
return plaintext.decode("utf-8") if as_str else plaintext
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class SinhaCipher:
|
|
55
|
+
"""Convenience object that reuses the same password/key configuration."""
|
|
56
|
+
|
|
57
|
+
__slots__ = ("password", "key", "logn", "passes", "allow_weak", "unsafe_low_cost")
|
|
58
|
+
|
|
59
|
+
def __init__(self, password=None, *, key=None, logn=MIN_LOGN, passes=1,
|
|
60
|
+
allow_weak=False, unsafe_low_cost=False):
|
|
61
|
+
self.password = password
|
|
62
|
+
self.key = key
|
|
63
|
+
self.logn = logn
|
|
64
|
+
self.passes = passes
|
|
65
|
+
self.allow_weak = allow_weak
|
|
66
|
+
self.unsafe_low_cost = unsafe_low_cost
|
|
67
|
+
|
|
68
|
+
def encrypt(self, plaintext, *, group=8):
|
|
69
|
+
return encrypt(
|
|
70
|
+
plaintext,
|
|
71
|
+
self.password,
|
|
72
|
+
key=self.key,
|
|
73
|
+
logn=self.logn,
|
|
74
|
+
passes=self.passes,
|
|
75
|
+
group=group,
|
|
76
|
+
allow_weak=self.allow_weak,
|
|
77
|
+
unsafe_low_cost=self.unsafe_low_cost,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
def decrypt(self, ciphertext, *, as_str=True):
|
|
81
|
+
return decrypt(
|
|
82
|
+
ciphertext,
|
|
83
|
+
self.password,
|
|
84
|
+
key=self.key,
|
|
85
|
+
logn=self.logn,
|
|
86
|
+
passes=self.passes,
|
|
87
|
+
as_str=as_str,
|
|
88
|
+
unsafe_low_cost=self.unsafe_low_cost,
|
|
89
|
+
)
|