|
| 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