srp.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * Secure Remote Password 6a implementation
  3. * https://github.com/est31/csrp-gmp
  4. *
  5. * The MIT License (MIT)
  6. *
  7. * Copyright (c) 2010, 2013 Tom Cocagne, 2015 est31 <MTest31@outlook.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. * this software and associated documentation files (the "Software"), to deal in
  11. * the Software without restriction, including without limitation the rights to
  12. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  13. * of the Software, and to permit persons to whom the Software is furnished to do
  14. * so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *
  27. */
  28. // clang-format off
  29. #include <cstddef>
  30. #ifdef WIN32
  31. #include <windows.h>
  32. #include <wincrypt.h>
  33. #else
  34. #include <ctime>
  35. #endif
  36. // clang-format on
  37. #include <cstdlib>
  38. #include <cstring>
  39. #include <cstdio>
  40. #include <cstdint>
  41. #include <config.h>
  42. #if USE_SYSTEM_GMP
  43. #include <gmp.h>
  44. #else
  45. #include <mini-gmp.h>
  46. #endif
  47. #include <util/sha2.h>
  48. #include "srp.h"
  49. //#define CSRP_USE_SHA1
  50. #define CSRP_USE_SHA256
  51. #define srp_dbg_data(data, datalen, prevtext) ;
  52. /*void srp_dbg_data(unsigned char * data, size_t datalen, char * prevtext)
  53. {
  54. printf(prevtext);
  55. size_t i;
  56. for (i = 0; i < datalen; i++)
  57. {
  58. printf("%02X", data[i]);
  59. }
  60. printf("\n");
  61. }*/
  62. static int g_initialized = 0;
  63. #define RAND_BUFF_MAX 128
  64. static unsigned int g_rand_idx;
  65. static unsigned char g_rand_buff[RAND_BUFF_MAX];
  66. void *(*srp_alloc)(size_t) = &malloc;
  67. void *(*srp_realloc)(void *, size_t) = &realloc;
  68. void (*srp_free)(void *) = &free;
  69. // clang-format off
  70. void srp_set_memory_functions(
  71. void *(*new_srp_alloc)(size_t),
  72. void *(*new_srp_realloc)(void *, size_t),
  73. void (*new_srp_free)(void *))
  74. {
  75. srp_alloc = new_srp_alloc;
  76. srp_realloc = new_srp_realloc;
  77. srp_free = new_srp_free;
  78. }
  79. // clang-format on
  80. typedef struct {
  81. mpz_t N;
  82. mpz_t g;
  83. } NGConstant;
  84. struct NGHex {
  85. const char *n_hex;
  86. const char *g_hex;
  87. };
  88. /* All constants here were pulled from Appendix A of RFC 5054 */
  89. static struct NGHex global_Ng_constants[] = {
  90. {/* 1024 */
  91. "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
  92. "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
  93. "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
  94. "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
  95. "FD5138FE8376435B9FC61D2FC0EB06E3",
  96. "2"},
  97. {/* 2048 */
  98. "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294"
  99. "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D"
  100. "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB"
  101. "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74"
  102. "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A"
  103. "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D"
  104. "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73"
  105. "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6"
  106. "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F"
  107. "9E4AFF73",
  108. "2"},
  109. {/* 4096 */
  110. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
  111. "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
  112. "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
  113. "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
  114. "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
  115. "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
  116. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
  117. "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
  118. "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
  119. "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
  120. "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
  121. "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
  122. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
  123. "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
  124. "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
  125. "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
  126. "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
  127. "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
  128. "FFFFFFFFFFFFFFFF",
  129. "5"},
  130. {/* 8192 */
  131. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
  132. "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
  133. "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
  134. "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
  135. "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
  136. "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
  137. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
  138. "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
  139. "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
  140. "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
  141. "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
  142. "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
  143. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
  144. "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
  145. "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
  146. "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
  147. "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
  148. "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
  149. "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
  150. "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
  151. "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
  152. "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
  153. "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
  154. "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
  155. "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
  156. "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
  157. "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
  158. "6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA"
  159. "3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C"
  160. "5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
  161. "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC886"
  162. "2F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A6"
  163. "6D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC5"
  164. "0846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268"
  165. "359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6"
  166. "FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
  167. "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
  168. "13"},
  169. {0, 0} /* null sentinel */
  170. };
  171. static void delete_ng(NGConstant *ng)
  172. {
  173. if (ng) {
  174. mpz_clear(ng->N);
  175. mpz_clear(ng->g);
  176. srp_free(ng);
  177. }
  178. }
  179. static NGConstant *new_ng(SRP_NGType ng_type, const char *n_hex, const char *g_hex)
  180. {
  181. NGConstant *ng = (NGConstant *)srp_alloc(sizeof(NGConstant));
  182. if (!ng) return 0;
  183. mpz_init(ng->N);
  184. mpz_init(ng->g);
  185. if (ng_type != SRP_NG_CUSTOM) {
  186. n_hex = global_Ng_constants[ng_type].n_hex;
  187. g_hex = global_Ng_constants[ng_type].g_hex;
  188. }
  189. int rv = 0;
  190. rv = mpz_set_str(ng->N, n_hex, 16);
  191. rv = rv | mpz_set_str(ng->g, g_hex, 16);
  192. if (rv) {
  193. delete_ng(ng);
  194. return 0;
  195. }
  196. return ng;
  197. }
  198. typedef union {
  199. SHA_CTX sha;
  200. SHA256_CTX sha256;
  201. // SHA512_CTX sha512;
  202. } HashCTX;
  203. struct SRPVerifier {
  204. SRP_HashAlgorithm hash_alg;
  205. NGConstant *ng;
  206. char *username;
  207. unsigned char *bytes_B;
  208. int authenticated;
  209. unsigned char M[SHA512_DIGEST_LENGTH];
  210. unsigned char H_AMK[SHA512_DIGEST_LENGTH];
  211. unsigned char session_key[SHA512_DIGEST_LENGTH];
  212. };
  213. struct SRPUser {
  214. SRP_HashAlgorithm hash_alg;
  215. NGConstant *ng;
  216. mpz_t a;
  217. mpz_t A;
  218. mpz_t S;
  219. unsigned char *bytes_A;
  220. int authenticated;
  221. char *username;
  222. char *username_verifier;
  223. unsigned char *password;
  224. size_t password_len;
  225. unsigned char M[SHA512_DIGEST_LENGTH];
  226. unsigned char H_AMK[SHA512_DIGEST_LENGTH];
  227. unsigned char session_key[SHA512_DIGEST_LENGTH];
  228. };
  229. // clang-format off
  230. static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
  231. {
  232. switch (alg) {
  233. #ifdef CSRP_USE_SHA1
  234. case SRP_SHA1: return SHA1_Init(&c->sha);
  235. #endif
  236. /*
  237. case SRP_SHA224: return SHA224_Init(&c->sha256);
  238. */
  239. #ifdef CSRP_USE_SHA256
  240. case SRP_SHA256: return SHA256_Init(&c->sha256);
  241. #endif
  242. /*
  243. case SRP_SHA384: return SHA384_Init(&c->sha512);
  244. case SRP_SHA512: return SHA512_Init(&c->sha512);
  245. */
  246. default: return -1;
  247. };
  248. }
  249. static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len )
  250. {
  251. switch (alg) {
  252. #ifdef CSRP_USE_SHA1
  253. case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
  254. #endif
  255. /*
  256. case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);
  257. */
  258. #ifdef CSRP_USE_SHA256
  259. case SRP_SHA256: return SHA256_Update(&c->sha256, data, len);
  260. #endif
  261. /*
  262. case SRP_SHA384: return SHA384_Update(&c->sha512, data, len);
  263. case SRP_SHA512: return SHA512_Update(&c->sha512, data, len);
  264. */
  265. default: return -1;
  266. };
  267. }
  268. static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
  269. {
  270. switch (alg) {
  271. #ifdef CSRP_USE_SHA1
  272. case SRP_SHA1: return SHA1_Final(md, &c->sha);
  273. #endif
  274. /*
  275. case SRP_SHA224: return SHA224_Final(md, &c->sha256);
  276. */
  277. #ifdef CSRP_USE_SHA256
  278. case SRP_SHA256: return SHA256_Final(md, &c->sha256);
  279. #endif
  280. /*
  281. case SRP_SHA384: return SHA384_Final(md, &c->sha512);
  282. case SRP_SHA512: return SHA512_Final(md, &c->sha512);
  283. */
  284. default: return -1;
  285. };
  286. }
  287. static unsigned char *hash(SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md)
  288. {
  289. switch (alg) {
  290. #ifdef CSRP_USE_SHA1
  291. case SRP_SHA1: return SHA1(d, n, md);
  292. #endif
  293. /*
  294. case SRP_SHA224: return SHA224( d, n, md );
  295. */
  296. #ifdef CSRP_USE_SHA256
  297. case SRP_SHA256: return SHA256(d, n, md);
  298. #endif
  299. /*
  300. case SRP_SHA384: return SHA384( d, n, md );
  301. case SRP_SHA512: return SHA512( d, n, md );
  302. */
  303. default: return 0;
  304. };
  305. }
  306. static size_t hash_length(SRP_HashAlgorithm alg)
  307. {
  308. switch (alg) {
  309. #ifdef CSRP_USE_SHA1
  310. case SRP_SHA1: return SHA_DIGEST_LENGTH;
  311. #endif
  312. /*
  313. case SRP_SHA224: return SHA224_DIGEST_LENGTH;
  314. */
  315. #ifdef CSRP_USE_SHA256
  316. case SRP_SHA256: return SHA256_DIGEST_LENGTH;
  317. #endif
  318. /*
  319. case SRP_SHA384: return SHA384_DIGEST_LENGTH;
  320. case SRP_SHA512: return SHA512_DIGEST_LENGTH;
  321. */
  322. default: return -1;
  323. };
  324. }
  325. // clang-format on
  326. inline static int mpz_num_bytes(const mpz_t op)
  327. {
  328. return (mpz_sizeinbase(op, 2) + 7) / 8;
  329. }
  330. inline static void mpz_to_bin(const mpz_t op, unsigned char *to)
  331. {
  332. mpz_export(to, NULL, 1, 1, 1, 0, op);
  333. }
  334. inline static void mpz_from_bin(const unsigned char *s, size_t len, mpz_t ret)
  335. {
  336. mpz_import(ret, len, 1, 1, 1, 0, s);
  337. }
  338. // set op to (op1 * op2) mod d, using tmp for the calculation
  339. inline static void mpz_mulm(
  340. mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
  341. {
  342. mpz_mul(tmp, op1, op2);
  343. mpz_mod(op, tmp, d);
  344. }
  345. // set op to (op1 + op2) mod d, using tmp for the calculation
  346. inline static void mpz_addm(
  347. mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
  348. {
  349. mpz_add(tmp, op1, op2);
  350. mpz_mod(op, tmp, d);
  351. }
  352. // set op to (op1 - op2) mod d, using tmp for the calculation
  353. inline static void mpz_subm(
  354. mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
  355. {
  356. mpz_sub(tmp, op1, op2);
  357. mpz_mod(op, tmp, d);
  358. }
  359. static SRP_Result H_nn(
  360. mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2)
  361. {
  362. unsigned char buff[SHA512_DIGEST_LENGTH];
  363. size_t len_N = mpz_num_bytes(N);
  364. size_t len_n1 = mpz_num_bytes(n1);
  365. size_t len_n2 = mpz_num_bytes(n2);
  366. size_t nbytes = len_N + len_N;
  367. unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
  368. if (!bin) return SRP_ERR;
  369. if (len_n1 > len_N || len_n2 > len_N) {
  370. srp_free(bin);
  371. return SRP_ERR;
  372. }
  373. memset(bin, 0, nbytes);
  374. mpz_to_bin(n1, bin + (len_N - len_n1));
  375. mpz_to_bin(n2, bin + (len_N + len_N - len_n2));
  376. hash(alg, bin, nbytes, buff);
  377. srp_free(bin);
  378. mpz_from_bin(buff, hash_length(alg), result);
  379. return SRP_OK;
  380. }
  381. static SRP_Result H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *n,
  382. size_t len_n, const unsigned char *bytes, uint32_t len_bytes)
  383. {
  384. unsigned char buff[SHA512_DIGEST_LENGTH];
  385. size_t nbytes = len_n + len_bytes;
  386. unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
  387. if (!bin) return SRP_ERR;
  388. memcpy(bin, n, len_n);
  389. memcpy(bin + len_n, bytes, len_bytes);
  390. hash(alg, bin, nbytes, buff);
  391. srp_free(bin);
  392. mpz_from_bin(buff, hash_length(alg), result);
  393. return SRP_OK;
  394. }
  395. static int calculate_x(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *salt,
  396. size_t salt_len, const char *username, const unsigned char *password,
  397. size_t password_len)
  398. {
  399. unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
  400. HashCTX ctx;
  401. hash_init(alg, &ctx);
  402. srp_dbg_data((char *)username, strlen(username), "Username for x: ");
  403. srp_dbg_data((char *)password, password_len, "Password for x: ");
  404. hash_update(alg, &ctx, username, strlen(username));
  405. hash_update(alg, &ctx, ":", 1);
  406. hash_update(alg, &ctx, password, password_len);
  407. hash_final(alg, &ctx, ucp_hash);
  408. return H_ns(result, alg, salt, salt_len, ucp_hash, hash_length(alg));
  409. }
  410. static SRP_Result update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
  411. {
  412. size_t len = mpz_num_bytes(n);
  413. unsigned char *n_bytes = (unsigned char *)srp_alloc(len);
  414. if (!n_bytes) return SRP_ERR;
  415. mpz_to_bin(n, n_bytes);
  416. hash_update(alg, ctx, n_bytes, len);
  417. srp_free(n_bytes);
  418. return SRP_OK;
  419. }
  420. static SRP_Result hash_num(SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest)
  421. {
  422. int nbytes = mpz_num_bytes(n);
  423. unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
  424. if (!bin) return SRP_ERR;
  425. mpz_to_bin(n, bin);
  426. hash(alg, bin, nbytes, dest);
  427. srp_free(bin);
  428. return SRP_OK;
  429. }
  430. static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest,
  431. const char *I, const unsigned char *s_bytes, size_t s_len, const mpz_t A,
  432. const mpz_t B, const unsigned char *K)
  433. {
  434. unsigned char H_N[SHA512_DIGEST_LENGTH];
  435. unsigned char H_g[SHA512_DIGEST_LENGTH];
  436. unsigned char H_I[SHA512_DIGEST_LENGTH];
  437. unsigned char H_xor[SHA512_DIGEST_LENGTH];
  438. HashCTX ctx;
  439. size_t i = 0;
  440. size_t hash_len = hash_length(alg);
  441. if (!hash_num(alg, ng->N, H_N)) return SRP_ERR;
  442. if (!hash_num(alg, ng->g, H_g)) return SRP_ERR;
  443. hash(alg, (const unsigned char *)I, strlen(I), H_I);
  444. for (i = 0; i < hash_len; i++)
  445. H_xor[i] = H_N[i] ^ H_g[i];
  446. hash_init(alg, &ctx);
  447. hash_update(alg, &ctx, H_xor, hash_len);
  448. hash_update(alg, &ctx, H_I, hash_len);
  449. hash_update(alg, &ctx, s_bytes, s_len);
  450. if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
  451. if (!update_hash_n(alg, &ctx, B)) return SRP_ERR;
  452. hash_update(alg, &ctx, K, hash_len);
  453. hash_final(alg, &ctx, dest);
  454. return SRP_OK;
  455. }
  456. static SRP_Result calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest,
  457. const mpz_t A, const unsigned char *M, const unsigned char *K)
  458. {
  459. HashCTX ctx;
  460. hash_init(alg, &ctx);
  461. if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
  462. hash_update(alg, &ctx, M, hash_length(alg));
  463. hash_update(alg, &ctx, K, hash_length(alg));
  464. hash_final(alg, &ctx, dest);
  465. return SRP_OK;
  466. }
  467. static SRP_Result fill_buff()
  468. {
  469. g_rand_idx = 0;
  470. #ifdef WIN32
  471. HCRYPTPROV wctx;
  472. #else
  473. FILE *fp = 0;
  474. #endif
  475. #ifdef WIN32
  476. if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  477. return SRP_ERR;
  478. if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE *)g_rand_buff)) return SRP_ERR;
  479. if (!CryptReleaseContext(wctx, 0)) return SRP_ERR;
  480. #else
  481. fp = fopen("/dev/urandom", "r");
  482. if (!fp) return SRP_ERR;
  483. if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) { fclose(fp); return SRP_ERR; }
  484. if (fclose(fp)) return SRP_ERR;
  485. #endif
  486. return SRP_OK;
  487. }
  488. static SRP_Result mpz_fill_random(mpz_t num)
  489. {
  490. // was call: BN_rand(num, 256, -1, 0);
  491. if (RAND_BUFF_MAX - g_rand_idx < 32)
  492. if (fill_buff() != SRP_OK) return SRP_ERR;
  493. mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num);
  494. g_rand_idx += 32;
  495. return SRP_OK;
  496. }
  497. static SRP_Result init_random()
  498. {
  499. if (g_initialized) return SRP_OK;
  500. SRP_Result ret = fill_buff();
  501. g_initialized = (ret == SRP_OK);
  502. return ret;
  503. }
  504. #define srp_dbg_num(num, text) ;
  505. /*void srp_dbg_num(mpz_t num, char * prevtext)
  506. {
  507. int len_num = mpz_num_bytes(num);
  508. char *bytes_num = (char*) srp_alloc(len_num);
  509. mpz_to_bin(num, (unsigned char *) bytes_num);
  510. srp_dbg_data(bytes_num, len_num, prevtext);
  511. srp_free(bytes_num);
  512. }*/
  513. /***********************************************************************************************************
  514. *
  515. * Exported Functions
  516. *
  517. ***********************************************************************************************************/
  518. // clang-format off
  519. SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
  520. SRP_NGType ng_type, const char *username_for_verifier,
  521. const unsigned char *password, size_t len_password,
  522. unsigned char **bytes_s, size_t *len_s,
  523. unsigned char **bytes_v, size_t *len_v,
  524. const char *n_hex, const char *g_hex )
  525. {
  526. SRP_Result ret = SRP_OK;
  527. mpz_t v; mpz_init(v);
  528. mpz_t x; mpz_init(x);
  529. // clang-format on
  530. NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
  531. if (!ng) goto error_and_exit;
  532. if (init_random() != SRP_OK) /* Only happens once */
  533. goto error_and_exit;
  534. if (*bytes_s == NULL) {
  535. size_t size_to_fill = 16;
  536. *len_s = size_to_fill;
  537. if (RAND_BUFF_MAX - g_rand_idx < size_to_fill)
  538. if (fill_buff() != SRP_OK) goto error_and_exit;
  539. *bytes_s = (unsigned char *)srp_alloc(size_to_fill);
  540. if (!*bytes_s) goto error_and_exit;
  541. memcpy(*bytes_s, &g_rand_buff[g_rand_idx], size_to_fill);
  542. g_rand_idx += size_to_fill;
  543. }
  544. if (!calculate_x(
  545. x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password))
  546. goto error_and_exit;
  547. srp_dbg_num(x, "Server calculated x: ");
  548. mpz_powm(v, ng->g, x, ng->N);
  549. *len_v = mpz_num_bytes(v);
  550. *bytes_v = (unsigned char *)srp_alloc(*len_v);
  551. if (!*bytes_v) goto error_and_exit;
  552. mpz_to_bin(v, *bytes_v);
  553. cleanup_and_exit:
  554. delete_ng(ng);
  555. mpz_clear(v);
  556. mpz_clear(x);
  557. return ret;
  558. error_and_exit:
  559. ret = SRP_ERR;
  560. goto cleanup_and_exit;
  561. }
  562. // clang-format off
  563. /* Out: bytes_B, len_B.
  564. *
  565. * On failure, bytes_B will be set to NULL and len_B will be set to 0
  566. */
  567. struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
  568. SRP_NGType ng_type, const char *username,
  569. const unsigned char *bytes_s, size_t len_s,
  570. const unsigned char *bytes_v, size_t len_v,
  571. const unsigned char *bytes_A, size_t len_A,
  572. const unsigned char *bytes_b, size_t len_b,
  573. unsigned char **bytes_B, size_t *len_B,
  574. const char *n_hex, const char *g_hex )
  575. {
  576. mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v);
  577. mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A);
  578. mpz_t u; mpz_init(u);
  579. mpz_t B; mpz_init(B);
  580. mpz_t S; mpz_init(S);
  581. mpz_t b; mpz_init(b);
  582. mpz_t k; mpz_init(k);
  583. mpz_t tmp1; mpz_init(tmp1);
  584. mpz_t tmp2; mpz_init(tmp2);
  585. mpz_t tmp3; mpz_init(tmp3);
  586. // clang-format on
  587. size_t ulen = strlen(username) + 1;
  588. NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
  589. struct SRPVerifier *ver = 0;
  590. *len_B = 0;
  591. *bytes_B = 0;
  592. if (!ng) goto cleanup_and_exit;
  593. ver = (struct SRPVerifier *)srp_alloc(sizeof(struct SRPVerifier));
  594. if (!ver) goto cleanup_and_exit;
  595. if (init_random() != SRP_OK) { /* Only happens once */
  596. srp_free(ver);
  597. ver = 0;
  598. goto cleanup_and_exit;
  599. }
  600. ver->username = (char *)srp_alloc(ulen);
  601. ver->hash_alg = alg;
  602. ver->ng = ng;
  603. if (!ver->username) {
  604. srp_free(ver);
  605. ver = 0;
  606. goto cleanup_and_exit;
  607. }
  608. memcpy(ver->username, username, ulen);
  609. ver->authenticated = 0;
  610. /* SRP-6a safety check */
  611. mpz_mod(tmp1, A, ng->N);
  612. if (mpz_sgn(tmp1) != 0) {
  613. if (bytes_b) {
  614. mpz_from_bin(bytes_b, len_b, b);
  615. } else {
  616. if (!mpz_fill_random(b)) goto ver_cleanup_and_exit;
  617. }
  618. if (!H_nn(k, alg, ng->N, ng->N, ng->g)) goto ver_cleanup_and_exit;
  619. /* B = kv + g^b */
  620. mpz_mulm(tmp1, k, v, ng->N, tmp3);
  621. mpz_powm(tmp2, ng->g, b, ng->N);
  622. mpz_addm(B, tmp1, tmp2, ng->N, tmp3);
  623. if (!H_nn(u, alg, ng->N, A, B)) goto ver_cleanup_and_exit;
  624. srp_dbg_num(u, "Server calculated u: ");
  625. /* S = (A *(v^u)) ^ b */
  626. mpz_powm(tmp1, v, u, ng->N);
  627. mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
  628. mpz_powm(S, tmp2, b, ng->N);
  629. if (!hash_num(alg, S, ver->session_key)) goto ver_cleanup_and_exit;
  630. if (!calculate_M(
  631. alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key)) {
  632. goto ver_cleanup_and_exit;
  633. }
  634. if (!calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key)) {
  635. goto ver_cleanup_and_exit;
  636. }
  637. *len_B = mpz_num_bytes(B);
  638. *bytes_B = (unsigned char *)srp_alloc(*len_B);
  639. if (!*bytes_B) {
  640. *len_B = 0;
  641. goto ver_cleanup_and_exit;
  642. }
  643. mpz_to_bin(B, *bytes_B);
  644. ver->bytes_B = *bytes_B;
  645. } else {
  646. srp_free(ver);
  647. ver = 0;
  648. }
  649. cleanup_and_exit:
  650. mpz_clear(v);
  651. mpz_clear(A);
  652. mpz_clear(u);
  653. mpz_clear(k);
  654. mpz_clear(B);
  655. mpz_clear(S);
  656. mpz_clear(b);
  657. mpz_clear(tmp1);
  658. mpz_clear(tmp2);
  659. mpz_clear(tmp3);
  660. return ver;
  661. ver_cleanup_and_exit:
  662. srp_free(ver->username);
  663. srp_free(ver);
  664. ver = 0;
  665. goto cleanup_and_exit;
  666. }
  667. void srp_verifier_delete(struct SRPVerifier *ver)
  668. {
  669. if (ver) {
  670. delete_ng(ver->ng);
  671. srp_free(ver->username);
  672. srp_free(ver->bytes_B);
  673. memset(ver, 0, sizeof(*ver));
  674. srp_free(ver);
  675. }
  676. }
  677. int srp_verifier_is_authenticated(struct SRPVerifier *ver)
  678. {
  679. return ver->authenticated;
  680. }
  681. const char *srp_verifier_get_username(struct SRPVerifier *ver)
  682. {
  683. return ver->username;
  684. }
  685. const unsigned char *srp_verifier_get_session_key(
  686. struct SRPVerifier *ver, size_t *key_length)
  687. {
  688. if (key_length) *key_length = hash_length(ver->hash_alg);
  689. return ver->session_key;
  690. }
  691. size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
  692. {
  693. return hash_length(ver->hash_alg);
  694. }
  695. /* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
  696. void srp_verifier_verify_session(
  697. struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
  698. {
  699. if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) {
  700. ver->authenticated = 1;
  701. *bytes_HAMK = ver->H_AMK;
  702. } else
  703. *bytes_HAMK = NULL;
  704. }
  705. /*******************************************************************************/
  706. struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
  707. const char *username, const char *username_for_verifier,
  708. const unsigned char *bytes_password, size_t len_password, const char *n_hex,
  709. const char *g_hex)
  710. {
  711. struct SRPUser *usr = (struct SRPUser *)srp_alloc(sizeof(struct SRPUser));
  712. size_t ulen = strlen(username) + 1;
  713. size_t uvlen = strlen(username_for_verifier) + 1;
  714. if (!usr) goto err_exit;
  715. if (init_random() != SRP_OK) /* Only happens once */
  716. goto err_exit;
  717. usr->hash_alg = alg;
  718. usr->ng = new_ng(ng_type, n_hex, g_hex);
  719. mpz_init(usr->a);
  720. mpz_init(usr->A);
  721. mpz_init(usr->S);
  722. if (!usr->ng) goto err_exit;
  723. usr->username = (char *)srp_alloc(ulen);
  724. usr->username_verifier = (char *)srp_alloc(uvlen);
  725. usr->password = (unsigned char *)srp_alloc(len_password);
  726. usr->password_len = len_password;
  727. if (!usr->username || !usr->password || !usr->username_verifier) goto err_exit;
  728. memcpy(usr->username, username, ulen);
  729. memcpy(usr->username_verifier, username_for_verifier, uvlen);
  730. memcpy(usr->password, bytes_password, len_password);
  731. usr->authenticated = 0;
  732. usr->bytes_A = 0;
  733. return usr;
  734. err_exit:
  735. if (usr) {
  736. mpz_clear(usr->a);
  737. mpz_clear(usr->A);
  738. mpz_clear(usr->S);
  739. delete_ng(usr->ng);
  740. srp_free(usr->username);
  741. srp_free(usr->username_verifier);
  742. if (usr->password) {
  743. memset(usr->password, 0, usr->password_len);
  744. srp_free(usr->password);
  745. }
  746. srp_free(usr);
  747. }
  748. return 0;
  749. }
  750. void srp_user_delete(struct SRPUser *usr)
  751. {
  752. if (usr) {
  753. mpz_clear(usr->a);
  754. mpz_clear(usr->A);
  755. mpz_clear(usr->S);
  756. delete_ng(usr->ng);
  757. memset(usr->password, 0, usr->password_len);
  758. srp_free(usr->username);
  759. srp_free(usr->username_verifier);
  760. srp_free(usr->password);
  761. if (usr->bytes_A) srp_free(usr->bytes_A);
  762. memset(usr, 0, sizeof(*usr));
  763. srp_free(usr);
  764. }
  765. }
  766. int srp_user_is_authenticated(struct SRPUser *usr)
  767. {
  768. return usr->authenticated;
  769. }
  770. const char *srp_user_get_username(struct SRPUser *usr)
  771. {
  772. return usr->username;
  773. }
  774. const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length)
  775. {
  776. if (key_length) *key_length = hash_length(usr->hash_alg);
  777. return usr->session_key;
  778. }
  779. size_t srp_user_get_session_key_length(struct SRPUser *usr)
  780. {
  781. return hash_length(usr->hash_alg);
  782. }
  783. // clang-format off
  784. /* Output: username, bytes_A, len_A */
  785. SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
  786. const unsigned char *bytes_a, size_t len_a,
  787. unsigned char **bytes_A, size_t *len_A)
  788. {
  789. // clang-format on
  790. if (bytes_a) {
  791. mpz_from_bin(bytes_a, len_a, usr->a);
  792. } else {
  793. if (!mpz_fill_random(usr->a)) goto error_and_exit;
  794. }
  795. mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
  796. *len_A = mpz_num_bytes(usr->A);
  797. *bytes_A = (unsigned char *)srp_alloc(*len_A);
  798. if (!*bytes_A) goto error_and_exit;
  799. mpz_to_bin(usr->A, *bytes_A);
  800. usr->bytes_A = *bytes_A;
  801. if (username) *username = usr->username;
  802. return SRP_OK;
  803. error_and_exit:
  804. *len_A = 0;
  805. *bytes_A = 0;
  806. *username = 0;
  807. return SRP_ERR;
  808. }
  809. // clang-format off
  810. /* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
  811. void srp_user_process_challenge(struct SRPUser *usr,
  812. const unsigned char *bytes_s, size_t len_s,
  813. const unsigned char *bytes_B, size_t len_B,
  814. unsigned char **bytes_M, size_t *len_M)
  815. {
  816. mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B);
  817. mpz_t u; mpz_init(u);
  818. mpz_t x; mpz_init(x);
  819. mpz_t k; mpz_init(k);
  820. mpz_t v; mpz_init(v);
  821. mpz_t tmp1; mpz_init(tmp1);
  822. mpz_t tmp2; mpz_init(tmp2);
  823. mpz_t tmp3; mpz_init(tmp3);
  824. mpz_t tmp4; mpz_init(tmp4);
  825. // clang-format on
  826. *len_M = 0;
  827. *bytes_M = 0;
  828. if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B)) goto cleanup_and_exit;
  829. srp_dbg_num(u, "Client calculated u: ");
  830. if (!calculate_x(x, usr->hash_alg, bytes_s, len_s, usr->username_verifier,
  831. usr->password, usr->password_len))
  832. goto cleanup_and_exit;
  833. srp_dbg_num(x, "Client calculated x: ");
  834. if (!H_nn(k, usr->hash_alg, usr->ng->N, usr->ng->N, usr->ng->g))
  835. goto cleanup_and_exit;
  836. /* SRP-6a safety check */
  837. if (mpz_sgn(B) != 0 && mpz_sgn(u) != 0) {
  838. mpz_powm(v, usr->ng->g, x, usr->ng->N);
  839. srp_dbg_num(v, "Client calculated v: ");
  840. // clang-format off
  841. /* S = (B - k*(g^x)) ^ (a + ux) */
  842. mpz_mul(tmp1, u, x);
  843. mpz_add(tmp2, usr->a, tmp1); /* tmp2 = (a + ux) */
  844. mpz_powm(tmp1, usr->ng->g, x, usr->ng->N); /* tmp1 = g^x */
  845. mpz_mulm(tmp3, k, tmp1, usr->ng->N, tmp4); /* tmp3 = k*(g^x) */
  846. mpz_subm(tmp1, B, tmp3, usr->ng->N, tmp4); /* tmp1 = (B - K*(g^x)) */
  847. mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
  848. // clang-format on
  849. if (!hash_num(usr->hash_alg, usr->S, usr->session_key)) goto cleanup_and_exit;
  850. if (!calculate_M(usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s,
  851. usr->A, B, usr->session_key))
  852. goto cleanup_and_exit;
  853. if (!calculate_H_AMK(usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key))
  854. goto cleanup_and_exit;
  855. *bytes_M = usr->M;
  856. *len_M = hash_length(usr->hash_alg);
  857. } else {
  858. *bytes_M = NULL;
  859. *len_M = 0;
  860. }
  861. cleanup_and_exit:
  862. mpz_clear(B);
  863. mpz_clear(u);
  864. mpz_clear(x);
  865. mpz_clear(k);
  866. mpz_clear(v);
  867. mpz_clear(tmp1);
  868. mpz_clear(tmp2);
  869. mpz_clear(tmp3);
  870. mpz_clear(tmp4);
  871. }
  872. void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK)
  873. {
  874. if (memcmp(usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg)) == 0)
  875. usr->authenticated = 1;
  876. }