| 1 |
""" |
| 2 |
Copyright (c) 2023 Proton AG |
| 3 |
|
| 4 |
This file is part of Proton. |
| 5 |
|
| 6 |
Proton is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
Proton is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
""" |
| 19 |
# N A large safe prime (N = 2q+1, where q is prime) |
| 20 |
# All arithmetic is done modulo N. |
| 21 |
# g A generator modulo N |
| 22 |
# k Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6) |
| 23 |
# s User's salt |
| 24 |
# I Username |
| 25 |
# p Cleartext Password |
| 26 |
# H() One-way hash function |
| 27 |
# ^ (Modular) Exponentiation |
| 28 |
# u Random scrambling parameter |
| 29 |
# a,b Secret ephemeral values |
| 30 |
# A,B Public ephemeral values |
| 31 |
# x Private key (derived from p and s) |
| 32 |
# v Password verifier |
| 33 |
from .pmhash import pmhash |
| 34 |
from .util import (PM_VERSION, SRP_LEN_BYTES, SALT_LEN_BYTES, bytes_to_long, custom_hash, |
| 35 |
get_random_of_length, hash_password, long_to_bytes) |
| 36 |
|
| 37 |
|
| 38 |
def get_ng(n_bin, g_hex): |
| 39 |
return bytes_to_long(n_bin), int(g_hex, 16) |
| 40 |
|
| 41 |
|
| 42 |
def hash_k(hash_class, g, modulus, width): |
| 43 |
h = hash_class() |
| 44 |
h.update(g.to_bytes(width, 'little')) |
| 45 |
h.update(modulus.to_bytes(width, 'little')) |
| 46 |
return bytes_to_long(h.digest()) |
| 47 |
|
| 48 |
|
| 49 |
def calculate_x(hash_class, salt, password, modulus, version): |
| 50 |
exp = hash_password( |
| 51 |
hash_class, password, salt, long_to_bytes(modulus, SRP_LEN_BYTES), version |
| 52 |
) |
| 53 |
return bytes_to_long(exp) |
| 54 |
|
| 55 |
|
| 56 |
def calculate_client_proof(hash_class, A, B, K): |
| 57 |
h = hash_class() |
| 58 |
h.update(long_to_bytes(A, SRP_LEN_BYTES)) |
| 59 |
h.update(long_to_bytes(B, SRP_LEN_BYTES)) |
| 60 |
h.update(K) |
| 61 |
return h.digest() |
| 62 |
|
| 63 |
|
| 64 |
def calculate_server_proof(hash_class, A, M, K): |
| 65 |
h = hash_class() |
| 66 |
h.update(long_to_bytes(A, SRP_LEN_BYTES)) |
| 67 |
h.update(M) |
| 68 |
h.update(K) |
| 69 |
return h.digest() |
| 70 |
|
| 71 |
|
| 72 |
class User(object): |
| 73 |
def __init__(self, password, n_bin, g_hex=b"2", bytes_a=None, bytes_A=None): # noqa |
| 74 |
if bytes_a and len(bytes_a) != 32: |
| 75 |
raise ValueError("32 bytes required for bytes_a") |
| 76 |
|
| 77 |
if not isinstance(password, str) or len(password) == 0: |
| 78 |
raise ValueError("Invalid password") |
| 79 |
|
| 80 |
self.N, self.g = get_ng(n_bin, g_hex) |
| 81 |
self.hash_class = pmhash |
| 82 |
self.k = hash_k( |
| 83 |
self.hash_class, self.g, |
| 84 |
self.N, SRP_LEN_BYTES |
| 85 |
) |
| 86 |
|
| 87 |
self.p = password.encode() |
| 88 |
if bytes_a: |
| 89 |
self.a = bytes_to_long(bytes_a) |
| 90 |
else: |
| 91 |
self.a = get_random_of_length(32) |
| 92 |
if bytes_A: |
| 93 |
self.A = bytes_to_long(bytes_A) |
| 94 |
else: |
| 95 |
self.A = pow(self.g, self.a, self.N) |
| 96 |
self.v = None |
| 97 |
self.M = None |
| 98 |
self.K = None |
| 99 |
self.expected_server_proof = None |
| 100 |
self._authenticated = False |
| 101 |
self.bytes_s = None |
| 102 |
self.S = None |
| 103 |
self.B = None |
| 104 |
self.u = None |
| 105 |
self.x = None |
| 106 |
|
| 107 |
def authenticated(self): |
| 108 |
return self._authenticated |
| 109 |
|
| 110 |
def get_ephemeral_secret(self): |
| 111 |
return long_to_bytes(self.a, SRP_LEN_BYTES) |
| 112 |
|
| 113 |
def get_session_key(self): |
| 114 |
return self.K if self._authenticated else None |
| 115 |
|
| 116 |
def get_challenge(self): |
| 117 |
return long_to_bytes(self.A, SRP_LEN_BYTES) |
| 118 |
|
| 119 |
# Returns M or None if SRP-6a safety check is violated |
| 120 |
def process_challenge( |
| 121 |
self, bytes_s, bytes_server_challenge, version=PM_VERSION |
| 122 |
): |
| 123 |
self.bytes_s = bytes_s |
| 124 |
self.B = bytes_to_long(bytes_server_challenge) |
| 125 |
|
| 126 |
# SRP-6a safety check |
| 127 |
if (self.B % self.N) == 0: |
| 128 |
return None |
| 129 |
|
| 130 |
self.u = custom_hash(self.hash_class, self.A, self.B) |
| 131 |
|
| 132 |
# SRP-6a safety check |
| 133 |
if self.u == 0: |
| 134 |
return None |
| 135 |
|
| 136 |
self.x = calculate_x(self.hash_class, self.bytes_s, self.p, self.N, version) |
| 137 |
|
| 138 |
self.v = pow(self.g, self.x, self.N) |
| 139 |
|
| 140 |
self.S = pow( |
| 141 |
(self.B - self.k * self.v), (self.a + self.u * self.x), self.N |
| 142 |
) |
| 143 |
|
| 144 |
self.K = long_to_bytes(self.S, SRP_LEN_BYTES) |
| 145 |
self.M = calculate_client_proof(self.hash_class, self.A, self.B, self.K) # noqa |
| 146 |
self.expected_server_proof = calculate_server_proof( |
| 147 |
self.hash_class, self.A, self.M, self.K |
| 148 |
) |
| 149 |
|
| 150 |
return self.M |
| 151 |
|
| 152 |
def verify_session(self, server_proof): |
| 153 |
if self.expected_server_proof == server_proof: |
| 154 |
self._authenticated = True |
| 155 |
|
| 156 |
def compute_v(self, bytes_s=None, version=PM_VERSION): |
| 157 |
self.bytes_s = long_to_bytes(get_random_of_length(SALT_LEN_BYTES), SALT_LEN_BYTES) if bytes_s is None else bytes_s # noqa |
| 158 |
self.x = calculate_x(self.hash_class, self.bytes_s, self.p, self.N, version) |
| 159 |
|
| 160 |
return self.bytes_s, long_to_bytes(pow(self.g, self.x, self.N), SRP_LEN_BYTES) # noqa |