Skip to content

Commit b357285

Browse files
committed
Rename 'salt' import alias to 'sodium'
1 parent 985a7be commit b357285

File tree

7 files changed

+75
-65
lines changed

7 files changed

+75
-65
lines changed

contrib/auth-example.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example script for demonstrating X-SOGS-* authentication calculation.
22

3-
import nacl.bindings as salt
3+
import nacl.bindings as sodium
44
from nacl.signing import SigningKey
55
from hashlib import blake2b, sha512
66
from base64 import b64encode
@@ -29,10 +29,12 @@ def blinded_ed25519_signature(message_parts, s: SigningKey, ka: bytes, kA: bytes
2929
domain separation for different blinded pubkeys. (This doesn't affect verification at all).
3030
"""
3131
H_rh = sha512(s.encode()).digest()[32:]
32-
r = salt.crypto_core_ed25519_scalar_reduce(sha512_multipart(H_rh, kA, message_parts))
33-
sig_R = salt.crypto_scalarmult_ed25519_base_noclamp(r)
34-
HRAM = salt.crypto_core_ed25519_scalar_reduce(sha512_multipart(sig_R, kA, message_parts))
35-
sig_s = salt.crypto_core_ed25519_scalar_add(r, salt.crypto_core_ed25519_scalar_mul(HRAM, ka))
32+
r = sodium.crypto_core_ed25519_scalar_reduce(sha512_multipart(H_rh, kA, message_parts))
33+
sig_R = sodium.crypto_scalarmult_ed25519_base_noclamp(r)
34+
HRAM = sodium.crypto_core_ed25519_scalar_reduce(sha512_multipart(sig_R, kA, message_parts))
35+
sig_s = sodium.crypto_core_ed25519_scalar_add(
36+
r, sodium.crypto_core_ed25519_scalar_mul(HRAM, ka)
37+
)
3638
return sig_R + sig_s
3739

3840

@@ -52,7 +54,7 @@ def get_signing_headers(
5254

5355
if blinded:
5456
# 64-byte blake2b hash then reduce to get the blinding factor:
55-
k = salt.crypto_core_ed25519_scalar_reduce(blake2b(server_pk, digest_size=64).digest())
57+
k = sodium.crypto_core_ed25519_scalar_reduce(blake2b(server_pk, digest_size=64).digest())
5658

5759
# Calculate k*a. To get 'a' (the Ed25519 private key scalar) we call the sodium function to
5860
# convert to an *x* secret key, which seems wrong--but isn't because converted keys use the
@@ -61,8 +63,8 @@ def get_signing_headers(
6163
a = s.to_curve25519_private_key().encode()
6264

6365
# Our blinded keypair:
64-
ka = salt.crypto_core_ed25519_scalar_mul(k, a)
65-
kA = salt.crypto_scalarmult_ed25519_base_noclamp(ka)
66+
ka = sodium.crypto_core_ed25519_scalar_mul(k, a)
67+
kA = sodium.crypto_scalarmult_ed25519_base_noclamp(ka)
6668

6769
# Blinded session id:
6870
pubkey = '15' + kA.hex()

contrib/blinding.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from nacl.signing import SigningKey, VerifyKey
2-
import nacl.bindings as salt
2+
import nacl.bindings as sodium
33
from nacl.utils import random
44
import nacl.hash
55
from hashlib import blake2b
@@ -16,24 +16,26 @@
1616

1717
# This seems a bit weird, but: sodium's sk-to-curve uses the same private key scalar (a) for
1818
# both curves, so getting `a` for the curve25519 implicitly gives us the ed25519 `a` as well.
19-
a = salt.crypto_sign_ed25519_sk_to_curve25519(s.encode() + A)
19+
a = sodium.crypto_sign_ed25519_sk_to_curve25519(s.encode() + A)
2020

2121
A_xpk = s.to_curve25519_private_key().public_key.encode() # session id is 05 + this
2222

23-
assert salt.crypto_scalarmult_ed25519_base_noclamp(a) == A
24-
assert salt.crypto_scalarmult_base(a) == A_xpk
25-
assert salt.crypto_core_ed25519_is_valid_point(A)
23+
assert sodium.crypto_scalarmult_ed25519_base_noclamp(a) == A
24+
assert sodium.crypto_scalarmult_base(a) == A_xpk
25+
assert sodium.crypto_core_ed25519_is_valid_point(A)
2626

2727
server_pubkey = random(32)
28-
k = salt.crypto_core_ed25519_scalar_reduce(nacl.hash.generichash(server_pubkey, digest_size=64))
28+
k = sodium.crypto_core_ed25519_scalar_reduce(
29+
nacl.hash.generichash(server_pubkey, digest_size=64)
30+
)
2931

30-
ka = salt.crypto_core_ed25519_scalar_mul(k, a)
32+
ka = sodium.crypto_core_ed25519_scalar_mul(k, a)
3133
# kA will be my blinded pubkey visible from my posts, with '15' prepended, and is an *Ed*
3234
# pubkey, not an X pubkey.
33-
kA = salt.crypto_scalarmult_ed25519_noclamp(k, A)
35+
kA = sodium.crypto_scalarmult_ed25519_noclamp(k, A)
3436

35-
assert salt.crypto_scalarmult_ed25519_base_noclamp(ka) == kA
36-
assert salt.crypto_core_ed25519_is_valid_point(kA)
37+
assert sodium.crypto_scalarmult_ed25519_base_noclamp(ka) == kA
38+
assert sodium.crypto_core_ed25519_is_valid_point(kA)
3739

3840
#############################
3941
# Signing (e.g. for X-SOGS-*) with a blinded keypair ka/kA
@@ -45,13 +47,17 @@
4547
# which gives us a bog standard Ed25519 that can be verified using the kA pubkey with standard
4648
# verification code.
4749
message_to_sign = b'omg happy days'
48-
H_rh = salt.crypto_hash_sha512(s.encode())[32:]
49-
r = salt.crypto_core_ed25519_scalar_reduce(salt.crypto_hash_sha512(H_rh + kA + message_to_sign))
50-
sig_R = salt.crypto_scalarmult_ed25519_base_noclamp(r)
51-
HRAM = salt.crypto_core_ed25519_scalar_reduce(
52-
salt.crypto_hash_sha512(sig_R + kA + message_to_sign)
50+
H_rh = sodium.crypto_hash_sha512(s.encode())[32:]
51+
r = sodium.crypto_core_ed25519_scalar_reduce(
52+
sodium.crypto_hash_sha512(H_rh + kA + message_to_sign)
53+
)
54+
sig_R = sodium.crypto_scalarmult_ed25519_base_noclamp(r)
55+
HRAM = sodium.crypto_core_ed25519_scalar_reduce(
56+
sodium.crypto_hash_sha512(sig_R + kA + message_to_sign)
57+
)
58+
sig_s = sodium.crypto_core_ed25519_scalar_add(
59+
r, sodium.crypto_core_ed25519_scalar_mul(HRAM, ka)
5360
)
54-
sig_s = salt.crypto_core_ed25519_scalar_add(r, salt.crypto_core_ed25519_scalar_mul(HRAM, ka))
5561
full_sig = sig_R + sig_s
5662

5763
assert VerifyKey(kA).verify(message_to_sign, full_sig)
@@ -62,29 +68,29 @@
6268
# Our user A above wants to send a SOGS DM to another user B:
6369
s2 = SigningKey.generate()
6470
B = s2.verify_key.encode()
65-
b = salt.crypto_sign_ed25519_sk_to_curve25519(s2.encode() + B)
71+
b = sodium.crypto_sign_ed25519_sk_to_curve25519(s2.encode() + B)
6672

6773
# with blinded keys:
68-
kb = salt.crypto_core_ed25519_scalar_mul(k, b)
69-
kB = salt.crypto_scalarmult_ed25519_noclamp(k, B)
74+
kb = sodium.crypto_core_ed25519_scalar_mul(k, b)
75+
kB = sodium.crypto_scalarmult_ed25519_noclamp(k, B)
7076

7177
B_xpk = s2.to_curve25519_private_key().public_key.encode()
7278

73-
assert salt.crypto_scalarmult_ed25519_base_noclamp(kb) == kB
74-
assert salt.crypto_core_ed25519_is_valid_point(kB)
79+
assert sodium.crypto_scalarmult_ed25519_base_noclamp(kb) == kB
80+
assert sodium.crypto_core_ed25519_is_valid_point(kB)
7581

7682
#############################
7783
# Finding friends:
7884

7985
# For example (in reality this would come directly from the known session id):
80-
friend_xpk = salt.crypto_sign_ed25519_pk_to_curve25519(A)
86+
friend_xpk = sodium.crypto_sign_ed25519_pk_to_curve25519(A)
8187

8288
# From the session id (ignoring 05 prefix) we have two possible ed25519 pubkeys; the first is
8389
# the positive (which is what Signal's XEd25519 conversion always uses):
8490
pk1 = xed25519.pubkey(friend_xpk)
8591

8692
# Blind it:
87-
pk1 = salt.crypto_scalarmult_ed25519_noclamp(k, pk1)
93+
pk1 = sodium.crypto_scalarmult_ed25519_noclamp(k, pk1)
8894

8995
# For the negative, what we're going to get out of the above is simply the negative of pk1, so
9096
# flip the sign bit to get pk2:
@@ -102,7 +108,7 @@
102108
# around gives the same result as the shortcut:
103109
pk2_alt = xed25519.pubkey(friend_xpk)
104110
pk2_alt = pk2_alt[0:31] + bytes([pk2_alt[31] | 0b1000_0000])
105-
pk2_alt = salt.crypto_scalarmult_ed25519_noclamp(k, pk2_alt)
111+
pk2_alt = sodium.crypto_scalarmult_ed25519_noclamp(k, pk2_alt)
106112
assert pk2 == pk2_alt
107113

108114
# Now, if this is really my friend, his blinded key will equal one of these blinded keys:
@@ -128,15 +134,15 @@
128134
# BLAKE2b(b kA || kA || kB)
129135
#
130136
enc_key = blake2b(
131-
salt.crypto_scalarmult_ed25519_noclamp(a, kB) + kA + kB, digest_size=32
137+
sodium.crypto_scalarmult_ed25519_noclamp(a, kB) + kA + kB, digest_size=32
132138
).digest()
133139

134140
# Inner data: msg || A (i.e. the sender's ed25519 master pubkey, *not* kA blinded pubkey)
135141
plaintext = msg.encode() + A
136142

137143
# Encrypt using xchacha20-poly1305
138144
nonce = random(24)
139-
ciphertext = salt.crypto_aead_xchacha20poly1305_ietf_encrypt(
145+
ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
140146
plaintext, aad=None, nonce=nonce, key=enc_key
141147
)
142148

@@ -151,7 +157,7 @@
151157

152158
# Calculate the shared encryption key (see above)
153159
dec_key = blake2b(
154-
salt.crypto_scalarmult_ed25519_noclamp(b, kA) + kA + kB, digest_size=32
160+
sodium.crypto_scalarmult_ed25519_noclamp(b, kA) + kA + kB, digest_size=32
155161
).digest()
156162

157163
assert enc_key == dec_key
@@ -162,19 +168,21 @@
162168
assert v == 0x00 # Make sure our encryption version is okay
163169

164170
# Decrypt
165-
plaintext = salt.crypto_aead_xchacha20poly1305_ietf_decrypt(ct, aad=None, nonce=nc, key=dec_key)
171+
plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
172+
ct, aad=None, nonce=nc, key=dec_key
173+
)
166174

167175
assert len(plaintext) > 32
168176

169177
# Split up: the last 32 bytes are the sender's *unblinded* ed25519 key
170178
message, sender_edpk = plaintext[:-32], plaintext[-32:]
171179

172180
# Verify that the inner sender_edpk (A) yields the same outer kA we got with the message
173-
assert kA == salt.crypto_scalarmult_ed25519_noclamp(k, sender_edpk)
181+
assert kA == sodium.crypto_scalarmult_ed25519_noclamp(k, sender_edpk)
174182

175183
message = message.decode() # utf-8 bytes back to str
176184

177-
sender_session_id = '05' + salt.crypto_sign_ed25519_pk_to_curve25519(sender_edpk).hex()
185+
sender_session_id = '05' + sodium.crypto_sign_ed25519_pk_to_curve25519(sender_edpk).hex()
178186

179187
assert message == msg
180188
assert sender_edpk == A

sogs/routes/auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import nacl
1010
from nacl.signing import VerifyKey
1111
import nacl.exceptions
12-
import nacl.bindings as salt
12+
import nacl.bindings as sodium
1313
import sqlalchemy.exc
1414
from functools import wraps
1515

@@ -238,7 +238,7 @@ def handle_http_auth():
238238
blinded_pk = pk[0] == 0x15
239239
pk = pk[1:]
240240

241-
if not salt.crypto_core_ed25519_is_valid_point(pk):
241+
if not sodium.crypto_core_ed25519_is_valid_point(pk):
242242
abort_with_reason(
243243
http.BAD_REQUEST,
244244
"Invalid authentication: given X-SOGS-Pubkey is not a valid Ed25519 pubkey",

tests/auth.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44
import time
55
from sogs.hashing import blake2b, sha512
6-
import nacl.bindings as salt
6+
import nacl.bindings as sodium
77
from nacl.utils import random
88

99
import sogs.utils
@@ -37,11 +37,11 @@ def x_sogs_raw(
3737

3838
if blinded:
3939
a = s.to_curve25519_private_key().encode()
40-
k = salt.crypto_core_ed25519_scalar_reduce(
40+
k = sodium.crypto_core_ed25519_scalar_reduce(
4141
blake2b(sogs.crypto.server_pubkey_bytes, digest_size=64)
4242
)
43-
ka = salt.crypto_core_ed25519_scalar_mul(k, a)
44-
kA = salt.crypto_scalarmult_ed25519_base_noclamp(ka)
43+
ka = sodium.crypto_core_ed25519_scalar_mul(k, a)
44+
kA = sodium.crypto_scalarmult_ed25519_base_noclamp(ka)
4545
pubkey = '15' + kA.hex()
4646
else:
4747
pubkey = '00' + s.verify_key.encode().hex()
@@ -52,11 +52,11 @@ def x_sogs_raw(
5252

5353
if blinded:
5454
H_rh = sha512(s.encode())[32:]
55-
r = salt.crypto_core_ed25519_scalar_reduce(sha512([H_rh, kA, *to_sign]))
56-
sig_R = salt.crypto_scalarmult_ed25519_base_noclamp(r)
57-
HRAM = salt.crypto_core_ed25519_scalar_reduce(sha512([sig_R, kA, *to_sign]))
58-
sig_s = salt.crypto_core_ed25519_scalar_add(
59-
r, salt.crypto_core_ed25519_scalar_mul(HRAM, ka)
55+
r = sodium.crypto_core_ed25519_scalar_reduce(sha512([H_rh, kA, *to_sign]))
56+
sig_R = sodium.crypto_scalarmult_ed25519_base_noclamp(r)
57+
HRAM = sodium.crypto_core_ed25519_scalar_reduce(sha512([sig_R, kA, *to_sign]))
58+
sig_s = sodium.crypto_core_ed25519_scalar_add(
59+
r, sodium.crypto_core_ed25519_scalar_mul(HRAM, ka)
6060
)
6161
sig = sig_R + sig_s
6262

tests/test_auth.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import json
88
from nacl.signing import SigningKey
9-
import nacl.bindings as salt
9+
import nacl.bindings as sodium
1010

1111

1212
@app.get("/auth_test/whoami")
@@ -507,15 +507,15 @@ def test_small_subgroups(client, db):
507507

508508
assert A == a.verify_key.encode()
509509

510-
if hasattr(salt, 'crypto_core_ed25519_is_valid_point'):
511-
assert salt.crypto_core_ed25519_is_valid_point(A)
510+
if hasattr(sodium, 'crypto_core_ed25519_is_valid_point'):
511+
assert sodium.crypto_core_ed25519_is_valid_point(A)
512512

513-
Abad = salt.crypto_core_ed25519_add(
513+
Abad = sodium.crypto_core_ed25519_add(
514514
A, bytes.fromhex('0000000000000000000000000000000000000000000000000000000000000000')
515515
)
516516

517-
if hasattr(salt, 'crypto_core_ed25519_is_valid_point'):
518-
assert not salt.crypto_core_ed25519_is_valid_point(Abad)
517+
if hasattr(sodium, 'crypto_core_ed25519_is_valid_point'):
518+
assert not sodium.crypto_core_ed25519_is_valid_point(Abad)
519519

520520
headers['X-SOGS-Pubkey'] = '00' + Abad.hex()
521521

@@ -528,12 +528,12 @@ def test_small_subgroups(client, db):
528528
assert headers['X-SOGS-Pubkey'].startswith('15')
529529
A = bytes.fromhex(headers['X-SOGS-Pubkey'][2:])
530530

531-
Abad = salt.crypto_core_ed25519_add(
531+
Abad = sodium.crypto_core_ed25519_add(
532532
A, bytes.fromhex('c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a')
533533
)
534534

535-
if hasattr(salt, 'crypto_core_ed25519_is_valid_point'):
536-
assert not salt.crypto_core_ed25519_is_valid_point(Abad)
535+
if hasattr(sodium, 'crypto_core_ed25519_is_valid_point'):
536+
assert not sodium.crypto_core_ed25519_is_valid_point(Abad)
537537

538538
headers['X-SOGS-Pubkey'] = '15' + Abad.hex()
539539
r = client.get("/auth_test/whoami", headers=headers)

tests/test_dm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sogs.hashing import blake2b
44
from sogs.utils import encode_base64
55
from sogs.model.user import SystemUser
6-
import nacl.bindings as salt
6+
import nacl.bindings as sodium
77
from nacl.utils import random
88
from util import from_now
99

@@ -25,12 +25,12 @@ def make_post(message, sender, to):
2525
a = sender.ed_key.to_curve25519_private_key().encode()
2626
kA = bytes.fromhex(sender.session_id[2:])
2727
kB = bytes.fromhex(to.session_id[2:])
28-
key = blake2b(salt.crypto_scalarmult_ed25519_noclamp(a, kB) + kA + kB, digest_size=32)
28+
key = blake2b(sodium.crypto_scalarmult_ed25519_noclamp(a, kB) + kA + kB, digest_size=32)
2929

3030
# MESSAGE || UNBLINDED_ED_PUBKEY
3131
plaintext = message + sender.ed_key.verify_key.encode()
3232
nonce = random(24)
33-
ciphertext = salt.crypto_aead_xchacha20poly1305_ietf_encrypt(
33+
ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
3434
plaintext, aad=None, nonce=nonce, key=key
3535
)
3636
data = b'\x00' + ciphertext + nonce

tests/user.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sogs.model.user
22
from nacl.signing import SigningKey
3-
import nacl.bindings as salt
3+
import nacl.bindings as sodium
44
from sogs.hashing import blake2b
55
import sogs.crypto
66

@@ -11,11 +11,11 @@ def __init__(self, blinded=False):
1111

1212
if blinded:
1313
a = self.ed_key.to_curve25519_private_key().encode()
14-
k = salt.crypto_core_ed25519_scalar_reduce(
14+
k = sodium.crypto_core_ed25519_scalar_reduce(
1515
blake2b(sogs.crypto.server_pubkey_bytes, digest_size=64)
1616
)
17-
ka = salt.crypto_core_ed25519_scalar_mul(k, a)
18-
kA = salt.crypto_scalarmult_ed25519_base_noclamp(ka)
17+
ka = sodium.crypto_core_ed25519_scalar_mul(k, a)
18+
kA = sodium.crypto_scalarmult_ed25519_base_noclamp(ka)
1919
session_id = '15' + kA.hex()
2020
else:
2121
session_id = '05' + self.ed_key.to_curve25519_private_key().public_key.encode().hex()

0 commit comments

Comments
 (0)