Skip to content

Commit 00319a9

Browse files
authored
Use hashlib.pbkdf2_hmac for SCRAM password derivation (MagicStack#1359)
SCRAM's `Hi()` is PBKDF2-HMAC-SHA256. Replace the Python HMAC/XOR loop with `hashlib.pbkdf2_hmac` and add tests against known vectors.
1 parent bc3e246 commit 00319a9

3 files changed

Lines changed: 107 additions & 16 deletions

File tree

‎asyncpg/protocol/scram.pxd‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ cdef class SCRAMAuthentication:
2727
cdef _bytes_xor(self, bytes a, bytes b)
2828
cdef _generate_client_nonce(self, int num_bytes)
2929
cdef _generate_client_proof(self, str password)
30-
cdef _generate_salted_password(self, str password, bytes salt, int iterations)
30+
cpdef _generate_salted_password(self, str password, bytes salt, int iterations)
3131
cdef _normalize_password(self, str original_password)

‎asyncpg/protocol/scram.pyx‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,31 +227,21 @@ cdef class SCRAMAuthentication:
227227
# and the proof
228228
return self._bytes_xor(client_key.digest(), client_signature.digest())
229229

230-
cdef _generate_salted_password(self, str password, bytes salt, int iterations):
230+
cpdef _generate_salted_password(self, str password, bytes salt, int iterations):
231231
"""This follows the "Hi" algorithm specified in RFC5802"""
232232
cdef:
233233
bytes p
234234
bytes s
235-
bytes u
236235

237236
# convert the password to a binary string - UTF8 is safe for SASL
238237
# (though there are SASLPrep rules)
239238
p = password.encode("utf8")
240239
# the salt needs to be base64 decoded -- full binary must be used
241240
s = base64.b64decode(salt)
242-
# the initial signature is the salt with a terminator of a 32-bit string
243-
# ending in 1
244-
ui = hmac.new(p, s + b'\x00\x00\x00\x01', self.DIGEST)
245-
# grab the initial digest
246-
u = ui.digest()
247-
# for X number of iterations, recompute the HMAC signature against the
248-
# password and the latest iteration of the hash, and XOR it with the
249-
# previous version
250-
for x in range(iterations - 1):
251-
ui = hmac.new(p, ui.digest(), hashlib.sha256)
252-
# this is a fancy way of XORing two byte strings together
253-
u = self._bytes_xor(u, ui.digest())
254-
return u
241+
# hashlib.pbkdf2_hmac is PBKDF2-HMAC-SHA256: the same output as the
242+
# Python loop this replaces, computed in C. SHA-256 is hardcoded
243+
# because SCRAM-SHA-256 is the only method this class supports.
244+
return hashlib.pbkdf2_hmac("sha256", p, s, iterations)
255245

256246
cdef _normalize_password(self, str original_password):
257247
"""Normalize the password using the SASLprep from RFC4013"""

‎tests/test_scram.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (C) 2016-present the asyncpg authors and contributors
2+
# <see AUTHORS file>
3+
#
4+
# This module is part of asyncpg and is released under
5+
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
6+
7+
import base64
8+
import hashlib
9+
import hmac
10+
import unittest
11+
12+
from asyncpg.protocol.protocol import SCRAMAuthentication
13+
14+
15+
# PBKDF2-HMAC-SHA-256 vectors from RFC 7914, Section 11. SCRAM only
16+
# needs one block, so the expected values are the first halves of the
17+
# RFC's 64-byte outputs.
18+
RFC7914_VECTORS = [
19+
(b"passwd", b"salt", 1,
20+
"55ac046e56e3089fec1691c22544b605"
21+
"f94185216dde0465e68b9d57c20dacbc"),
22+
(b"Password", b"NaCl", 80000,
23+
"4ddcd8f60b98be21830cee5ef22701f9"
24+
"641a4418d04c0414aeff08876b34ab56"),
25+
]
26+
27+
# Salted password from the SCRAM-SHA-256 example in RFC 7677, Section 3
28+
# (password "pencil", with the salt and iteration count from the server).
29+
RFC7677_PASSWORD = "pencil"
30+
RFC7677_SALT = b"W22ZaJ0SNY7soEsUEjb6gQ=="
31+
RFC7677_ITERATIONS = 4096
32+
RFC7677_SALTED_PASSWORD = (
33+
"c4a49510323ab4f952cac1fa99441939"
34+
"e78ea74d6be81ddf7096e87513dc615d"
35+
)
36+
37+
38+
def reference_hifi(password, salt, iterations):
39+
"""The RFC 5802 Hi() loop that the salted password used to implement."""
40+
p = password.encode("utf8")
41+
u = hmac.new(p, salt + b"\x00\x00\x00\x01", hashlib.sha256).digest()
42+
result = u
43+
for _ in range(iterations - 1):
44+
u = hmac.new(p, u, hashlib.sha256).digest()
45+
result = bytes(a ^ b for a, b in zip(result, u))
46+
return result
47+
48+
49+
class TestSCRAMSaltedPassword(unittest.TestCase):
50+
51+
def setUp(self):
52+
self.scram = SCRAMAuthentication(b"SCRAM-SHA-256")
53+
54+
def test_rfc7914_vectors(self):
55+
for password, salt, iterations, expected in RFC7914_VECTORS:
56+
derived = self.scram._generate_salted_password(
57+
password.decode(), base64.b64encode(salt), iterations)
58+
self.assertEqual(derived.hex(), expected)
59+
60+
def test_rfc7677_salted_password(self):
61+
derived = self.scram._generate_salted_password(
62+
RFC7677_PASSWORD, RFC7677_SALT, RFC7677_ITERATIONS)
63+
self.assertEqual(derived.hex(), RFC7677_SALTED_PASSWORD)
64+
65+
def test_matches_reference_hifi(self):
66+
cases = [
67+
("pencil", b"W22ZaJ0SNY7soEsUEjb6gQ==", 4096),
68+
("passwd", b"c2FsdA==", 1),
69+
("passwd", b"c2FsdA==", 2),
70+
("p\u00e4ssw\u00f6rd", b"c2FsdA==", 100),
71+
]
72+
for password, salt, iterations in cases:
73+
expected = reference_hifi(
74+
password, base64.b64decode(salt), iterations)
75+
derived = self.scram._generate_salted_password(
76+
password, salt, iterations)
77+
self.assertEqual(derived, expected)
78+
79+
def test_uses_hashlib_pbkdf2_hmac(self):
80+
calls = []
81+
pbkdf2_hmac = hashlib.pbkdf2_hmac
82+
83+
def spy(digest, password, salt, iterations):
84+
calls.append((digest, password, salt, iterations))
85+
return pbkdf2_hmac(digest, password, salt, iterations)
86+
87+
hashlib.pbkdf2_hmac = spy
88+
try:
89+
derived = self.scram._generate_salted_password(
90+
RFC7677_PASSWORD, RFC7677_SALT, RFC7677_ITERATIONS)
91+
finally:
92+
hashlib.pbkdf2_hmac = pbkdf2_hmac
93+
94+
self.assertEqual(calls, [(
95+
"sha256", RFC7677_PASSWORD.encode(),
96+
base64.b64decode(RFC7677_SALT), RFC7677_ITERATIONS)])
97+
self.assertEqual(derived.hex(), RFC7677_SALTED_PASSWORD)
98+
99+
100+
if __name__ == '__main__':
101+
unittest.main()

0 commit comments

Comments
 (0)