aboutsummaryrefslogtreecommitdiff
path: root/src/util/srp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/srp.cpp')
-rw-r--r--src/util/srp.cpp352
1 files changed, 200 insertions, 152 deletions
diff --git a/src/util/srp.cpp b/src/util/srp.cpp
index f4d369d68..e7ccd5940 100644
--- a/src/util/srp.cpp
+++ b/src/util/srp.cpp
@@ -47,9 +47,9 @@
#include <config.h>
#if USE_SYSTEM_GMP
- #include <gmp.h>
+#include <gmp.h>
#else
- #include <mini-gmp.h>
+#include <mini-gmp.h>
#endif
#include <util/sha2.h>
@@ -92,98 +92,100 @@ void srp_set_memory_functions(
}
// clang-format on
-typedef struct {
+typedef struct
+{
mpz_t N;
mpz_t g;
} NGConstant;
-struct NGHex {
+struct NGHex
+{
const char *n_hex;
const char *g_hex;
};
/* All constants here were pulled from Appendix A of RFC 5054 */
static struct NGHex global_Ng_constants[] = {
- {/* 1024 */
- "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
- "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
- "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
- "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
- "FD5138FE8376435B9FC61D2FC0EB06E3",
- "2"},
- {/* 2048 */
- "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294"
- "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D"
- "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB"
- "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74"
- "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A"
- "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D"
- "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73"
- "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6"
- "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F"
- "9E4AFF73",
- "2"},
- {/* 4096 */
- "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
- "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
- "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
- "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
- "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
- "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
- "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
- "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
- "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
- "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
- "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
- "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
- "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
- "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
- "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
- "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
- "FFFFFFFFFFFFFFFF",
- "5"},
- {/* 8192 */
- "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
- "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
- "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
- "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
- "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
- "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
- "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
- "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
- "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
- "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
- "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
- "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
- "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
- "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
- "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
- "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
- "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
- "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
- "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
- "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
- "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
- "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
- "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
- "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
- "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
- "6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA"
- "3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C"
- "5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
- "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC886"
- "2F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A6"
- "6D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC5"
- "0846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268"
- "359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6"
- "FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
- "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
- "13"},
- {0, 0} /* null sentinel */
+ {/* 1024 */
+ "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
+ "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
+ "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
+ "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
+ "FD5138FE8376435B9FC61D2FC0EB06E3",
+ "2"},
+ {/* 2048 */
+ "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294"
+ "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D"
+ "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB"
+ "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74"
+ "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A"
+ "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D"
+ "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73"
+ "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6"
+ "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F"
+ "9E4AFF73",
+ "2"},
+ {/* 4096 */
+ "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
+ "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
+ "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
+ "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
+ "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
+ "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
+ "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
+ "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
+ "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
+ "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
+ "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
+ "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
+ "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
+ "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
+ "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
+ "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
+ "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
+ "FFFFFFFFFFFFFFFF",
+ "5"},
+ {/* 8192 */
+ "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
+ "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
+ "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
+ "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
+ "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
+ "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
+ "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
+ "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
+ "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
+ "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
+ "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
+ "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
+ "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
+ "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
+ "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
+ "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
+ "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
+ "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
+ "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
+ "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
+ "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
+ "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
+ "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
+ "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
+ "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
+ "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
+ "6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA"
+ "3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C"
+ "5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
+ "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC886"
+ "2F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A6"
+ "6D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC5"
+ "0846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268"
+ "359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6"
+ "FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
+ "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
+ "13"},
+ {0, 0} /* null sentinel */
};
static void delete_ng(NGConstant *ng)
@@ -199,7 +201,8 @@ static NGConstant *new_ng(SRP_NGType ng_type, const char *n_hex, const char *g_h
{
NGConstant *ng = (NGConstant *)srp_alloc(sizeof(NGConstant));
- if (!ng) return 0;
+ if (!ng)
+ return 0;
mpz_init(ng->N);
mpz_init(ng->g);
@@ -221,13 +224,15 @@ static NGConstant *new_ng(SRP_NGType ng_type, const char *n_hex, const char *g_h
return ng;
}
-typedef union {
+typedef union
+{
SHA_CTX sha;
SHA256_CTX sha256;
// SHA512_CTX sha512;
} HashCTX;
-struct SRPVerifier {
+struct SRPVerifier
+{
SRP_HashAlgorithm hash_alg;
NGConstant *ng;
@@ -240,7 +245,8 @@ struct SRPVerifier {
unsigned char session_key[SHA512_DIGEST_LENGTH];
};
-struct SRPUser {
+struct SRPUser
+{
SRP_HashAlgorithm hash_alg;
NGConstant *ng;
@@ -376,7 +382,7 @@ inline static void mpz_from_bin(const unsigned char *s, size_t len, mpz_t ret)
// set op to (op1 * op2) mod d, using tmp for the calculation
inline static void mpz_mulm(
- mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
+ mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
{
mpz_mul(tmp, op1, op2);
mpz_mod(op, tmp, d);
@@ -384,7 +390,7 @@ inline static void mpz_mulm(
// set op to (op1 + op2) mod d, using tmp for the calculation
inline static void mpz_addm(
- mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
+ mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
{
mpz_add(tmp, op1, op2);
mpz_mod(op, tmp, d);
@@ -392,14 +398,14 @@ inline static void mpz_addm(
// set op to (op1 - op2) mod d, using tmp for the calculation
inline static void mpz_subm(
- mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
+ mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
{
mpz_sub(tmp, op1, op2);
mpz_mod(op, tmp, d);
}
-static SRP_Result H_nn(
- mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2)
+static SRP_Result H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1,
+ const mpz_t n2)
{
unsigned char buff[SHA512_DIGEST_LENGTH];
size_t len_N = mpz_num_bytes(N);
@@ -407,7 +413,8 @@ static SRP_Result H_nn(
size_t len_n2 = mpz_num_bytes(n2);
size_t nbytes = len_N + len_N;
unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
- if (!bin) return SRP_ERR;
+ if (!bin)
+ return SRP_ERR;
if (len_n1 > len_N || len_n2 > len_N) {
srp_free(bin);
return SRP_ERR;
@@ -422,12 +429,13 @@ static SRP_Result H_nn(
}
static SRP_Result H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *n,
- size_t len_n, const unsigned char *bytes, uint32_t len_bytes)
+ size_t len_n, const unsigned char *bytes, uint32_t len_bytes)
{
unsigned char buff[SHA512_DIGEST_LENGTH];
size_t nbytes = len_n + len_bytes;
unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
- if (!bin) return SRP_ERR;
+ if (!bin)
+ return SRP_ERR;
memcpy(bin, n, len_n);
memcpy(bin + len_n, bytes, len_bytes);
hash(alg, bin, nbytes, buff);
@@ -437,8 +445,8 @@ static SRP_Result H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char
}
static int calculate_x(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *salt,
- size_t salt_len, const char *username, const unsigned char *password,
- size_t password_len)
+ size_t salt_len, const char *username, const unsigned char *password,
+ size_t password_len)
{
unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
HashCTX ctx;
@@ -459,7 +467,8 @@ static SRP_Result update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t
{
size_t len = mpz_num_bytes(n);
unsigned char *n_bytes = (unsigned char *)srp_alloc(len);
- if (!n_bytes) return SRP_ERR;
+ if (!n_bytes)
+ return SRP_ERR;
mpz_to_bin(n, n_bytes);
hash_update(alg, ctx, n_bytes, len);
srp_free(n_bytes);
@@ -470,7 +479,8 @@ static SRP_Result hash_num(SRP_HashAlgorithm alg, const mpz_t n, unsigned char *
{
int nbytes = mpz_num_bytes(n);
unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
- if (!bin) return SRP_ERR;
+ if (!bin)
+ return SRP_ERR;
mpz_to_bin(n, bin);
hash(alg, bin, nbytes, dest);
srp_free(bin);
@@ -478,8 +488,8 @@ static SRP_Result hash_num(SRP_HashAlgorithm alg, const mpz_t n, unsigned char *
}
static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest,
- const char *I, const unsigned char *s_bytes, size_t s_len, const mpz_t A,
- const mpz_t B, const unsigned char *K)
+ const char *I, const unsigned char *s_bytes, size_t s_len, const mpz_t A,
+ const mpz_t B, const unsigned char *K)
{
unsigned char H_N[SHA512_DIGEST_LENGTH];
unsigned char H_g[SHA512_DIGEST_LENGTH];
@@ -489,8 +499,10 @@ static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned ch
size_t i = 0;
size_t hash_len = hash_length(alg);
- if (!hash_num(alg, ng->N, H_N)) return SRP_ERR;
- if (!hash_num(alg, ng->g, H_g)) return SRP_ERR;
+ if (!hash_num(alg, ng->N, H_N))
+ return SRP_ERR;
+ if (!hash_num(alg, ng->g, H_g))
+ return SRP_ERR;
hash(alg, (const unsigned char *)I, strlen(I), H_I);
@@ -502,8 +514,10 @@ static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned ch
hash_update(alg, &ctx, H_xor, hash_len);
hash_update(alg, &ctx, H_I, hash_len);
hash_update(alg, &ctx, s_bytes, s_len);
- if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
- if (!update_hash_n(alg, &ctx, B)) return SRP_ERR;
+ if (!update_hash_n(alg, &ctx, A))
+ return SRP_ERR;
+ if (!update_hash_n(alg, &ctx, B))
+ return SRP_ERR;
hash_update(alg, &ctx, K, hash_len);
hash_final(alg, &ctx, dest);
@@ -511,13 +525,14 @@ static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned ch
}
static SRP_Result calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest,
- const mpz_t A, const unsigned char *M, const unsigned char *K)
+ const mpz_t A, const unsigned char *M, const unsigned char *K)
{
HashCTX ctx;
hash_init(alg, &ctx);
- if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
+ if (!update_hash_n(alg, &ctx, A))
+ return SRP_ERR;
hash_update(alg, &ctx, M, hash_length(alg));
hash_update(alg, &ctx, K, hash_length(alg));
@@ -539,16 +554,23 @@ static SRP_Result fill_buff()
if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return SRP_ERR;
- if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE *)g_rand_buff)) return SRP_ERR;
- if (!CryptReleaseContext(wctx, 0)) return SRP_ERR;
+ if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE *)g_rand_buff))
+ return SRP_ERR;
+ if (!CryptReleaseContext(wctx, 0))
+ return SRP_ERR;
#else
fp = fopen("/dev/urandom", "r");
- if (!fp) return SRP_ERR;
+ if (!fp)
+ return SRP_ERR;
- if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) { fclose(fp); return SRP_ERR; }
- if (fclose(fp)) return SRP_ERR;
+ if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) {
+ fclose(fp);
+ return SRP_ERR;
+ }
+ if (fclose(fp))
+ return SRP_ERR;
#endif
return SRP_OK;
}
@@ -557,7 +579,8 @@ static SRP_Result mpz_fill_random(mpz_t num)
{
// was call: BN_rand(num, 256, -1, 0);
if (RAND_BUFF_MAX - g_rand_idx < 32)
- if (fill_buff() != SRP_OK) return SRP_ERR;
+ if (fill_buff() != SRP_OK)
+ return SRP_ERR;
mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num);
g_rand_idx += 32;
return SRP_OK;
@@ -565,7 +588,8 @@ static SRP_Result mpz_fill_random(mpz_t num)
static SRP_Result init_random()
{
- if (g_initialized) return SRP_OK;
+ if (g_initialized)
+ return SRP_OK;
SRP_Result ret = fill_buff();
g_initialized = (ret == SRP_OK);
return ret;
@@ -604,7 +628,8 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
- if (!ng) goto error_and_exit;
+ if (!ng)
+ goto error_and_exit;
if (init_random() != SRP_OK) /* Only happens once */
goto error_and_exit;
@@ -613,15 +638,17 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
size_t size_to_fill = 16;
*len_s = size_to_fill;
if (RAND_BUFF_MAX - g_rand_idx < size_to_fill)
- if (fill_buff() != SRP_OK) goto error_and_exit;
+ if (fill_buff() != SRP_OK)
+ goto error_and_exit;
*bytes_s = (unsigned char *)srp_alloc(size_to_fill);
- if (!*bytes_s) goto error_and_exit;
+ if (!*bytes_s)
+ goto error_and_exit;
memcpy(*bytes_s, &g_rand_buff[g_rand_idx], size_to_fill);
g_rand_idx += size_to_fill;
}
- if (!calculate_x(
- x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password))
+ if (!calculate_x(x, alg, *bytes_s, *len_s, username_for_verifier, password,
+ len_password))
goto error_and_exit;
srp_dbg_num(x, "Server calculated x: ");
@@ -632,7 +659,8 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
*bytes_v = (unsigned char *)srp_alloc(*len_v);
- if (!*bytes_v) goto error_and_exit;
+ if (!*bytes_v)
+ goto error_and_exit;
mpz_to_bin(v, *bytes_v);
@@ -679,11 +707,13 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
*len_B = 0;
*bytes_B = 0;
- if (!ng) goto cleanup_and_exit;
+ if (!ng)
+ goto cleanup_and_exit;
ver = (struct SRPVerifier *)srp_alloc(sizeof(struct SRPVerifier));
- if (!ver) goto cleanup_and_exit;
+ if (!ver)
+ goto cleanup_and_exit;
if (init_random() != SRP_OK) { /* Only happens once */
srp_free(ver);
@@ -711,17 +741,20 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
if (bytes_b) {
mpz_from_bin(bytes_b, len_b, b);
} else {
- if (!mpz_fill_random(b)) goto ver_cleanup_and_exit;
+ if (!mpz_fill_random(b))
+ goto ver_cleanup_and_exit;
}
- if (!H_nn(k, alg, ng->N, ng->N, ng->g)) goto ver_cleanup_and_exit;
+ if (!H_nn(k, alg, ng->N, ng->N, ng->g))
+ goto ver_cleanup_and_exit;
/* B = kv + g^b */
mpz_mulm(tmp1, k, v, ng->N, tmp3);
mpz_powm(tmp2, ng->g, b, ng->N);
mpz_addm(B, tmp1, tmp2, ng->N, tmp3);
- if (!H_nn(u, alg, ng->N, A, B)) goto ver_cleanup_and_exit;
+ if (!H_nn(u, alg, ng->N, A, B))
+ goto ver_cleanup_and_exit;
srp_dbg_num(u, "Server calculated u: ");
@@ -730,10 +763,11 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
mpz_powm(S, tmp2, b, ng->N);
- if (!hash_num(alg, S, ver->session_key)) goto ver_cleanup_and_exit;
+ if (!hash_num(alg, S, ver->session_key))
+ goto ver_cleanup_and_exit;
- if (!calculate_M(
- alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key)) {
+ if (!calculate_M(alg, ng, ver->M, username, bytes_s, len_s, A, B,
+ ver->session_key)) {
goto ver_cleanup_and_exit;
}
if (!calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key)) {
@@ -797,9 +831,10 @@ const char *srp_verifier_get_username(struct SRPVerifier *ver)
}
const unsigned char *srp_verifier_get_session_key(
- struct SRPVerifier *ver, size_t *key_length)
+ struct SRPVerifier *ver, size_t *key_length)
{
- if (key_length) *key_length = hash_length(ver->hash_alg);
+ if (key_length)
+ *key_length = hash_length(ver->hash_alg);
return ver->session_key;
}
@@ -809,8 +844,8 @@ size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
}
/* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
-void srp_verifier_verify_session(
- struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
+void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *user_M,
+ unsigned char **bytes_HAMK)
{
if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) {
ver->authenticated = 1;
@@ -822,15 +857,16 @@ void srp_verifier_verify_session(
/*******************************************************************************/
struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
- const char *username, const char *username_for_verifier,
- const unsigned char *bytes_password, size_t len_password, const char *n_hex,
- const char *g_hex)
+ const char *username, const char *username_for_verifier,
+ const unsigned char *bytes_password, size_t len_password,
+ const char *n_hex, const char *g_hex)
{
struct SRPUser *usr = (struct SRPUser *)srp_alloc(sizeof(struct SRPUser));
size_t ulen = strlen(username) + 1;
size_t uvlen = strlen(username_for_verifier) + 1;
- if (!usr) goto err_exit;
+ if (!usr)
+ goto err_exit;
if (init_random() != SRP_OK) /* Only happens once */
goto err_exit;
@@ -842,14 +878,16 @@ struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
mpz_init(usr->A);
mpz_init(usr->S);
- if (!usr->ng) goto err_exit;
+ if (!usr->ng)
+ goto err_exit;
usr->username = (char *)srp_alloc(ulen);
usr->username_verifier = (char *)srp_alloc(uvlen);
usr->password = (unsigned char *)srp_alloc(len_password);
usr->password_len = len_password;
- if (!usr->username || !usr->password || !usr->username_verifier) goto err_exit;
+ if (!usr->username || !usr->password || !usr->username_verifier)
+ goto err_exit;
memcpy(usr->username, username, ulen);
memcpy(usr->username_verifier, username_for_verifier, uvlen);
@@ -894,7 +932,8 @@ void srp_user_delete(struct SRPUser *usr)
srp_free(usr->username_verifier);
srp_free(usr->password);
- if (usr->bytes_A) srp_free(usr->bytes_A);
+ if (usr->bytes_A)
+ srp_free(usr->bytes_A);
memset(usr, 0, sizeof(*usr));
srp_free(usr);
@@ -913,7 +952,8 @@ const char *srp_user_get_username(struct SRPUser *usr)
const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length)
{
- if (key_length) *key_length = hash_length(usr->hash_alg);
+ if (key_length)
+ *key_length = hash_length(usr->hash_alg);
return usr->session_key;
}
@@ -932,7 +972,8 @@ SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
if (bytes_a) {
mpz_from_bin(bytes_a, len_a, usr->a);
} else {
- if (!mpz_fill_random(usr->a)) goto error_and_exit;
+ if (!mpz_fill_random(usr->a))
+ goto error_and_exit;
}
mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
@@ -940,12 +981,14 @@ SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
*len_A = mpz_num_bytes(usr->A);
*bytes_A = (unsigned char *)srp_alloc(*len_A);
- if (!*bytes_A) goto error_and_exit;
+ if (!*bytes_A)
+ goto error_and_exit;
mpz_to_bin(usr->A, *bytes_A);
usr->bytes_A = *bytes_A;
- if (username) *username = usr->username;
+ if (username)
+ *username = usr->username;
return SRP_OK;
@@ -977,12 +1020,13 @@ void srp_user_process_challenge(struct SRPUser *usr,
*len_M = 0;
*bytes_M = 0;
- if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B)) goto cleanup_and_exit;
+ if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B))
+ goto cleanup_and_exit;
srp_dbg_num(u, "Client calculated u: ");
if (!calculate_x(x, usr->hash_alg, bytes_s, len_s, usr->username_verifier,
- usr->password, usr->password_len))
+ usr->password, usr->password_len))
goto cleanup_and_exit;
srp_dbg_num(x, "Client calculated x: ");
@@ -1006,19 +1050,23 @@ void srp_user_process_challenge(struct SRPUser *usr,
mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
// clang-format on
- if (!hash_num(usr->hash_alg, usr->S, usr->session_key)) goto cleanup_and_exit;
+ if (!hash_num(usr->hash_alg, usr->S, usr->session_key))
+ goto cleanup_and_exit;
- if (!calculate_M(usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s,
- usr->A, B, usr->session_key))
+ if (!calculate_M(usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s,
+ len_s, usr->A, B, usr->session_key))
goto cleanup_and_exit;
- if (!calculate_H_AMK(usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key))
+ if (!calculate_H_AMK(usr->hash_alg, usr->H_AMK, usr->A, usr->M,
+ usr->session_key))
goto cleanup_and_exit;
*bytes_M = usr->M;
- if (len_M) *len_M = hash_length(usr->hash_alg);
+ if (len_M)
+ *len_M = hash_length(usr->hash_alg);
} else {
*bytes_M = NULL;
- if (len_M) *len_M = 0;
+ if (len_M)
+ *len_M = 0;
}
cleanup_and_exit: