annotate mysrp.py @ 37:4f9a79f733ff

Don't explode when the link is down.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 21 Nov 2020 10:31:42 +1030
parents 1af6865189ce
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 # Modified version of https://github.com/cocagne/pysrp/blob/master/srp/_pysrp.py
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 # N A large safe prime (N = 2q+1, where q is prime)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # All arithmetic is done modulo N.
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 # g A generator modulo N
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 # k Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 # s User's salt
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 # I Username
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 # p Cleartext Password
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 # H() One-way hash function
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 # ^ (Modular) Exponentiation
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 # u Random scrambling parameter
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 # a,b Secret ephemeral values
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 # A,B Public ephemeral values
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # x Private key (derived from p and s)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 # v Password verifier
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 import hashlib
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 import os
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 import binascii
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 import six
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 SHA1 = 0
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 SHA224 = 1
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 SHA256 = 2
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 SHA384 = 3
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 SHA512 = 4
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 NG_1024 = 0
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 NG_2048 = 1
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 NG_4096 = 2
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 NG_8192 = 3
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 NG_CUSTOM = 4
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 _hash_map = { SHA1 : hashlib.sha1,
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 SHA224 : hashlib.sha224,
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 SHA256 : hashlib.sha256,
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 SHA384 : hashlib.sha384,
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 SHA512 : hashlib.sha512 }
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 _ng_const = (
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 # 1024-bit
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 ('''\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 9AFD5138FE8376435B9FC61D2FC0EB06E3''',
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 "2"),
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 # 2048
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 ('''\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF60\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 95179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B907\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 8717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB37861\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 60279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DB\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 FBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73''',
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 "2"),
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 # 4096
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 ('''\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 FFFFFFFFFFFFFFFF''',
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 "5"),
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 # 8192
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 ('''\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC886\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 2F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A6\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 6D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC5\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 0846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71\
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 60C980DD98EDD3DFFFFFFFFFFFFFFFFF''',
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 '0x13')
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 def get_ng( ng_type, n_hex, g_hex ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 if ng_type < NG_CUSTOM:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 n_hex, g_hex = _ng_const[ ng_type ]
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 return int(n_hex,16), int(g_hex,16)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 def bytes_to_long(s):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 n = 0
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 for b in six.iterbytes(s):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 n = (n << 8) | b
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 return n
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 def long_to_bytes(n):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 l = list()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 x = 0
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 off = 0
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 while x != n:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 b = (n >> off) & 0xFF
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 l.append( chr(b) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 x = x | (b << off)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 off += 8
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 l.reverse()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 return six.b(''.join(l))
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 def get_random( nbytes ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 return bytes_to_long( os.urandom( nbytes ) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 def get_random_of_length( nbytes ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 offset = (nbytes*8) - 1
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 return get_random( nbytes ) | (1 << offset)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 def old_H( hash_class, s1, s2 = '', s3=''):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 if isinstance(s1, six.integer_types):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 s1 = long_to_bytes(s1)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 if s2 and isinstance(s2, six.integer_types):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 s2 = long_to_bytes(s2)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 if s3 and isinstance(s3, six.integer_types):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 s3 = long_to_bytes(s3)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 s = s1 + s2 + s3
32
1af6865189ce Update to work with Python 3.
Daniel O'Connor <darius@dons.net.au>
parents: 22
diff changeset
167 return int(hash_class(s).hexdigest(), 16)
22
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 def H( hash_class, *args, **kwargs ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 h = hash_class()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 for s in args:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 if s is not None:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 h.update( long_to_bytes(s) if isinstance(s, six.integer_types) else s )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 return int( h.hexdigest(), 16 )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 #N = 0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73;
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 #g = 2;
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 #k = H(N,g)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 def HNxorg( hash_class, N, g ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 hN = hash_class( long_to_bytes(N) ).digest()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 hg = hash_class( long_to_bytes(g) ).digest()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 return six.b( ''.join( chr( six.indexbytes(hN, i) ^ six.indexbytes(hg, i) ) for i in range(0,len(hN)) ) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 def gen_x( hash_class, salt, username, password ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 return H( hash_class, salt, H( hash_class, username + six.b(':') + password ) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 def create_salted_verification_key( username, password, hash_alg=SHA1, ng_type=NG_2048, n_hex=None, g_hex=None ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 if ng_type == NG_CUSTOM and (n_hex is None or g_hex is None):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 raise ValueError("Both n_hex and g_hex are required when ng_type = NG_CUSTOM")
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 hash_class = _hash_map[ hash_alg ]
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 N,g = get_ng( ng_type, n_hex, g_hex )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 _s = long_to_bytes( get_random( 4 ) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 _v = long_to_bytes( pow(g, gen_x( hash_class, _s, username, password ), N) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 return _s, _v
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 def calculate_M( hash_class, N, g, I, s, A, B, K ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 h = hash_class()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 h.update( HNxorg( hash_class, N, g ) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 h.update( hash_class(I).digest() )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 h.update( long_to_bytes(s) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 h.update( long_to_bytes(A) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 h.update( long_to_bytes(B) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 h.update( K )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 return h.digest()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 def calculate_H_AMK( hash_class, A, M, K ):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 h = hash_class()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 h.update( long_to_bytes(A) )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 h.update( M )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 h.update( K )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 return h.digest()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 class User (object):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 def __init__(self, username, password, hash_alg=SHA1, ng_type=NG_2048, n_hex=None, g_hex=None, bytes_a=None):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 if ng_type == NG_CUSTOM and (n_hex is None or g_hex is None):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 raise ValueError("Both n_hex and g_hex are required when ng_type = NG_CUSTOM")
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 if bytes_a and len(bytes_a) != 32:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 raise ValueError("32 bytes required for bytes_a")
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 N,g = get_ng( ng_type, n_hex, g_hex )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 hash_class = _hash_map[ hash_alg ]
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 #k = H( hash_class, N, g )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 # XXX: modified to be SRP-6 as that is what the router uses
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 k = int('05b9e8ef059c6b32ea59fc1d322d37f04aa30bae5aa9003b8321e21ddb04e300', 16)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 self.I = username
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 self.p = password
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 if bytes_a:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 self.a = bytes_to_long(bytes_a)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 else:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 self.a = get_random_of_length( 32 )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 self.A = pow(g, self.a, N)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 self.v = None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 self.M = None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 self.K = None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 self.H_AMK = None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 self._authenticated = False
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 self.hash_class = hash_class
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257 self.N = N
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 self.g = g
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259 self.k = k
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 def authenticated(self):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263 return self._authenticated
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266 def get_username(self):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 return self.I
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 def get_ephemeral_secret(self):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 return long_to_bytes(self.a)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 def get_session_key(self):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 return self.K if self._authenticated else None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 def start_authentication(self):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 return (self.I, long_to_bytes(self.A))
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 # Returns M or None if SRP-6a safety check is violated
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 def process_challenge(self, bytes_s, bytes_B):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285 self.s = bytes_to_long( bytes_s )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 self.B = bytes_to_long( bytes_B )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288 N = self.N
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 g = self.g
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290 k = self.k
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
291
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 hash_class = self.hash_class
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
294 # SRP-6a safety check
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
295 if (self.B % N) == 0:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296 return None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
297
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
298 self.u = H( hash_class, self.A, self.B )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
299
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
300 # SRP-6a safety check
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
301 if self.u == 0:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
302 return None
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
303
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
304 self.x = gen_x( hash_class, self.s, self.I, self.p )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
306 self.v = pow(g, self.x, N)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
307
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
308 self.S = pow((self.B - k*self.v), (self.a + self.u*self.x), N)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
309
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 self.K = hash_class( long_to_bytes(self.S) ).digest()
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
311 self.M = calculate_M( hash_class, N, g, self.I, self.s, self.A, self.B, self.K )
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312 self.H_AMK = calculate_H_AMK(hash_class, self.A, self.M, self.K)
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
313
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314 return self.M
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
316
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
317 def verify_session(self, host_HAMK):
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
318 if self.H_AMK == host_HAMK:
a53f90508a06 Switch to TG-1.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
319 self._authenticated = True