2013-04-02 19:32:42 -07:00
|
|
|
/*
|
|
|
|
* Secure Remote Password 6a implementation
|
|
|
|
* Copyright (c) 2010 Tom Cocagne. All rights reserved.
|
2013-10-09 06:08:49 -07:00
|
|
|
* https://github.com/cocagne/csrp
|
2013-04-02 19:32:42 -07:00
|
|
|
*
|
2013-10-09 06:08:49 -07:00
|
|
|
* The MIT License (MIT)
|
2015-04-11 18:40:29 -07:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Tom Cocagne, 2015 est31 <MTest31@outlook.com>
|
|
|
|
*
|
2013-10-09 06:08:49 -07:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
* so, subject to the following conditions:
|
2015-04-11 18:40:29 -07:00
|
|
|
*
|
2013-10-09 06:08:49 -07:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2015-04-11 18:40:29 -07:00
|
|
|
*
|
2013-10-09 06:08:49 -07:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2015-04-11 18:40:29 -07:00
|
|
|
*
|
2013-04-02 19:32:42 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <Wincrypt.h>
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
#include <gmp.h>
|
|
|
|
#include <sha/sha2.h>
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
#include "srp.h"
|
|
|
|
|
|
|
|
static int g_initialized = 0;
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
#define RAND_BUFF_MAX 128
|
|
|
|
static unsigned int g_rand_idx;
|
|
|
|
static unsigned char g_rand_buff[RAND_BUFF_MAX];
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
typedef struct
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_t N;
|
|
|
|
mpz_t g;
|
2013-04-02 19:32:42 -07:00
|
|
|
} NGConstant;
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
struct NGHex
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
"EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496"
|
|
|
|
"EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E"
|
|
|
|
"F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA"
|
|
|
|
"9AFD5138FE8376435B9FC61D2FC0EB06E3",
|
|
|
|
"2"
|
|
|
|
},
|
|
|
|
{ /* 2048 */
|
|
|
|
"AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4"
|
|
|
|
"A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF60"
|
|
|
|
"95179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF"
|
|
|
|
"747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B907"
|
|
|
|
"8717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB37861"
|
|
|
|
"60279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DB"
|
|
|
|
"FBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
|
|
|
|
"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 NGConstant * new_ng( SRP_NGType ng_type, const char * n_hex, const char * g_hex )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
NGConstant * ng = (NGConstant *) malloc( sizeof(NGConstant) );
|
|
|
|
mpz_init(ng->N);
|
|
|
|
mpz_init(ng->g);
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
if( !ng || !ng->N || !ng->g )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( ng_type != SRP_NG_CUSTOM )
|
|
|
|
{
|
|
|
|
n_hex = global_Ng_constants[ ng_type ].n_hex;
|
|
|
|
g_hex = global_Ng_constants[ ng_type ].g_hex;
|
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_set_str( ng->N, n_hex, 16 );
|
|
|
|
mpz_set_str( ng->g, g_hex, 16 );
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
return ng;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_ng( NGConstant * ng )
|
|
|
|
{
|
|
|
|
if (ng)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_clear( ng->N );
|
|
|
|
mpz_clear( ng->g );
|
2013-04-02 19:32:42 -07:00
|
|
|
free(ng);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
typedef union
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
SHA_CTX sha;
|
|
|
|
SHA256_CTX sha256;
|
2015-04-11 18:40:29 -07:00
|
|
|
//SHA512_CTX sha512;
|
2013-04-02 19:32:42 -07:00
|
|
|
} HashCTX;
|
|
|
|
|
|
|
|
|
|
|
|
struct SRPVerifier
|
|
|
|
{
|
|
|
|
SRP_HashAlgorithm hash_alg;
|
|
|
|
NGConstant *ng;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
const char * username;
|
|
|
|
const unsigned char * bytes_B;
|
|
|
|
int authenticated;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
unsigned char M [SHA512_DIGEST_LENGTH];
|
|
|
|
unsigned char H_AMK [SHA512_DIGEST_LENGTH];
|
|
|
|
unsigned char session_key [SHA512_DIGEST_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct SRPUser
|
|
|
|
{
|
|
|
|
SRP_HashAlgorithm hash_alg;
|
|
|
|
NGConstant *ng;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_t a;
|
|
|
|
mpz_t A;
|
|
|
|
mpz_t S;
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
const unsigned char * bytes_A;
|
|
|
|
int authenticated;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
const char * username;
|
2015-04-11 18:40:29 -07:00
|
|
|
const char * username_verifier;
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char * password;
|
|
|
|
int password_len;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
unsigned char M [SHA512_DIGEST_LENGTH];
|
|
|
|
unsigned char H_AMK [SHA512_DIGEST_LENGTH];
|
|
|
|
unsigned char session_key [SHA512_DIGEST_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int hash_init( SRP_HashAlgorithm alg, HashCTX *c )
|
|
|
|
{
|
|
|
|
switch (alg)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA1 : return SHA1_Init( &c->sha );
|
|
|
|
case SRP_SHA224: return SHA224_Init( &c->sha256 );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
case SRP_SHA256: return SHA256_Init( &c->sha256 );
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA384: return SHA384_Init( &c->sha512 );
|
|
|
|
case SRP_SHA512: return SHA512_Init( &c->sha512 );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len )
|
|
|
|
{
|
|
|
|
switch (alg)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA1 : return SHA1_Update( &c->sha, data, len );
|
|
|
|
case SRP_SHA224: return SHA224_Update( &c->sha256, data, len );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
case SRP_SHA256: return SHA256_Update( &c->sha256, data, len );
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA384: return SHA384_Update( &c->sha512, data, len );
|
|
|
|
case SRP_SHA512: return SHA512_Update( &c->sha512, data, len );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
|
|
|
|
{
|
|
|
|
switch (alg)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA1 : return SHA1_Final( md, &c->sha );
|
|
|
|
case SRP_SHA224: return SHA224_Final( md, &c->sha256 );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
case SRP_SHA256: return SHA256_Final( md, &c->sha256 );
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA384: return SHA384_Final( md, &c->sha512 );
|
|
|
|
case SRP_SHA512: return SHA512_Final( md, &c->sha512 );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
static unsigned char * hash( SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md )
|
|
|
|
{
|
|
|
|
switch (alg)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA1 : return SHA1( d, n, md );
|
|
|
|
case SRP_SHA224: return SHA224( d, n, md );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
case SRP_SHA256: return SHA256( d, n, md );
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA384: return SHA384( d, n, md );
|
|
|
|
case SRP_SHA512: return SHA512( d, n, md );*/
|
2013-04-02 19:32:42 -07:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
static int hash_length( SRP_HashAlgorithm alg )
|
|
|
|
{
|
|
|
|
switch (alg)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA1 : return SHA_DIGEST_LENGTH;
|
|
|
|
case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/
|
2013-04-02 19:32:42 -07:00
|
|
|
case SRP_SHA256: return SHA256_DIGEST_LENGTH;
|
2015-04-11 18:40:29 -07:00
|
|
|
/*case SRP_SHA384: return SHA384_DIGEST_LENGTH;
|
|
|
|
case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/
|
2013-04-02 19:32:42 -07:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
inline int mpz_num_bytes( const mpz_t op )
|
|
|
|
{
|
|
|
|
return (mpz_sizeinbase (op, 2) + 7) / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mpz_to_bin( const mpz_t op, unsigned char * to )
|
|
|
|
{
|
|
|
|
mpz_export(to, NULL, 1, 1, 1, 0, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mpz_from_bin( const unsigned char * s, int len, mpz_t ret )
|
|
|
|
{
|
|
|
|
mpz_import(ret, len, 1, 1, 1, 0, s);
|
|
|
|
}
|
2013-04-02 19:32:42 -07:00
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static int H_nn( mpz_t result, SRP_HashAlgorithm alg, const mpz_t n1, const mpz_t n2 )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
unsigned char buff[ SHA512_DIGEST_LENGTH ];
|
2015-04-11 18:40:29 -07:00
|
|
|
int len_n1 = mpz_num_bytes(n1);
|
|
|
|
int len_n2 = mpz_num_bytes(n2);
|
2013-04-02 19:32:42 -07:00
|
|
|
int nbytes = len_n1 + len_n2;
|
|
|
|
unsigned char * bin = (unsigned char *) malloc( nbytes );
|
2015-04-11 18:40:29 -07:00
|
|
|
if (!bin) {
|
2013-04-02 19:32:42 -07:00
|
|
|
return 0;
|
2015-04-11 18:40:29 -07:00
|
|
|
}
|
|
|
|
mpz_to_bin(n1, bin);
|
|
|
|
mpz_to_bin(n2, bin + len_n1);
|
2013-04-02 19:32:42 -07:00
|
|
|
hash( alg, bin, nbytes, buff );
|
|
|
|
free(bin);
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_from_bin(buff, hash_length(alg), result);
|
|
|
|
return 1;
|
2013-04-02 19:32:42 -07:00
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static int H_ns( mpz_t result, SRP_HashAlgorithm alg, const unsigned char * n, int len_n, const unsigned char * bytes, int len_bytes )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
unsigned char buff[ SHA512_DIGEST_LENGTH ];
|
|
|
|
int nbytes = len_n + len_bytes;
|
|
|
|
unsigned char * bin = (unsigned char *) malloc( nbytes );
|
|
|
|
if (!bin)
|
|
|
|
return 0;
|
2015-04-11 18:40:29 -07:00
|
|
|
memcpy( bin, n, len_n );
|
2013-04-02 19:32:42 -07:00
|
|
|
memcpy( bin + len_n, bytes, len_bytes );
|
|
|
|
hash( alg, bin, nbytes, buff );
|
|
|
|
free(bin);
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_from_bin(buff, hash_length(alg), result);
|
|
|
|
return 1;
|
2013-04-02 19:32:42 -07:00
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
static int calculate_x( mpz_t result, SRP_HashAlgorithm alg, const unsigned char * salt, int salt_len, const char * username, const unsigned char * password, int password_len )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
|
|
|
|
HashCTX ctx;
|
|
|
|
|
|
|
|
hash_init( alg, &ctx );
|
|
|
|
|
|
|
|
hash_update( alg, &ctx, username, strlen(username) );
|
|
|
|
hash_update( alg, &ctx, ":", 1 );
|
|
|
|
hash_update( alg, &ctx, password, password_len );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_final( alg, &ctx, ucp_hash );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
return H_ns( result, alg, salt, salt_len, ucp_hash, hash_length(alg) );
|
2013-04-02 19:32:42 -07:00
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static void update_hash_n( SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
unsigned long len = mpz_num_bytes(n);
|
2013-04-02 19:32:42 -07:00
|
|
|
unsigned char * n_bytes = (unsigned char *) malloc( len );
|
|
|
|
if (!n_bytes)
|
|
|
|
return;
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_to_bin(n, n_bytes);
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_update(alg, ctx, n_bytes, len);
|
|
|
|
free(n_bytes);
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static void hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char * dest )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
int nbytes = mpz_num_bytes(n);
|
2013-04-02 19:32:42 -07:00
|
|
|
unsigned char * bin = (unsigned char *) malloc( nbytes );
|
|
|
|
if(!bin)
|
|
|
|
return;
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_to_bin(n, bin);
|
2013-04-02 19:32:42 -07:00
|
|
|
hash( alg, bin, nbytes, dest );
|
|
|
|
free(bin);
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static void calculate_M( SRP_HashAlgorithm alg, NGConstant *ng, unsigned char * dest, const char * I, const unsigned char * s_bytes, int s_len,
|
|
|
|
const mpz_t A, const mpz_t B, const unsigned char * K )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
unsigned char H_N[ SHA512_DIGEST_LENGTH ];
|
|
|
|
unsigned char H_g[ SHA512_DIGEST_LENGTH ];
|
|
|
|
unsigned char H_I[ SHA512_DIGEST_LENGTH ];
|
|
|
|
unsigned char H_xor[ SHA512_DIGEST_LENGTH ];
|
|
|
|
HashCTX ctx;
|
|
|
|
int i = 0;
|
|
|
|
int hash_len = hash_length(alg);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_num( alg, ng->N, H_N );
|
|
|
|
hash_num( alg, ng->g, H_g );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash(alg, (const unsigned char *)I, strlen(I), H_I);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
for (i=0; i < hash_len; i++ )
|
|
|
|
H_xor[i] = H_N[i] ^ H_g[i];
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_init( alg, &ctx );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_update( alg, &ctx, H_xor, hash_len );
|
|
|
|
hash_update( alg, &ctx, H_I, hash_len );
|
2015-04-11 18:40:29 -07:00
|
|
|
hash_update( alg, &ctx, s_bytes, s_len );
|
2013-04-02 19:32:42 -07:00
|
|
|
update_hash_n( alg, &ctx, A );
|
|
|
|
update_hash_n( alg, &ctx, B );
|
|
|
|
hash_update( alg, &ctx, K, hash_len );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_final( alg, &ctx, dest );
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static void calculate_H_AMK( SRP_HashAlgorithm alg, unsigned char *dest, const mpz_t A, const unsigned char * M, const unsigned char * K )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
|
|
|
HashCTX ctx;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_init( alg, &ctx );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
update_hash_n( alg, &ctx, A );
|
|
|
|
hash_update( alg, &ctx, M, hash_length(alg) );
|
|
|
|
hash_update( alg, &ctx, K, hash_length(alg) );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_final( alg, &ctx, dest );
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static int fill_buff()
|
|
|
|
{
|
|
|
|
g_rand_idx = 0;
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
HCRYPTPROV wctx;
|
|
|
|
#else
|
|
|
|
FILE *fp = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE*) g_rand_buff);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
CryptReleaseContext(wctx, 0);
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
return 1;
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
#else
|
|
|
|
fp = fopen("/dev/urandom", "r");
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
if (fp)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
fread(g_rand_buff, sizeof(g_rand_buff), 1, fp);
|
2013-04-02 19:32:42 -07:00
|
|
|
fclose(fp);
|
2015-04-11 18:40:29 -07:00
|
|
|
return 1;
|
2013-04-02 19:32:42 -07:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-11 18:40:29 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-02 19:32:42 -07:00
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
static void mpz_fill_random( mpz_t num )
|
|
|
|
{
|
|
|
|
// was call: BN_rand(num, 256, -1, 0);
|
|
|
|
if (RAND_BUFF_MAX - g_rand_idx < 32)
|
|
|
|
fill_buff();
|
|
|
|
mpz_from_bin( (char *) (&g_rand_buff[g_rand_idx]), 32, num );
|
|
|
|
g_rand_idx += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_random()
|
|
|
|
{
|
2013-04-02 19:32:42 -07:00
|
|
|
if (g_initialized)
|
2015-04-11 18:40:29 -07:00
|
|
|
return;
|
|
|
|
g_initialized = fill_buff();
|
2013-04-02 19:32:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************************************
|
|
|
|
*
|
|
|
|
* Exported Functions
|
|
|
|
*
|
|
|
|
***********************************************************************************************************/
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
|
|
|
|
SRP_NGType ng_type, const char * username_for_verifier,
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char * password, int len_password,
|
2015-04-11 18:40:29 -07:00
|
|
|
const unsigned char ** bytes_s, int * len_s,
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char ** bytes_v, int * len_v,
|
|
|
|
const char * n_hex, const char * g_hex )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_t v; mpz_init(v);
|
|
|
|
mpz_t x; mpz_init(x);
|
2013-04-02 19:32:42 -07:00
|
|
|
NGConstant * ng = new_ng( ng_type, n_hex, g_hex );
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
if( !v || !ng )
|
2013-04-02 19:32:42 -07:00
|
|
|
goto cleanup_and_exit;
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
if( bytes_s == NULL ) {
|
|
|
|
*len_s = 16;
|
|
|
|
if ( RAND_BUFF_MAX - g_rand_idx < 16 )
|
|
|
|
fill_buff();
|
|
|
|
*bytes_s = (const unsigned char *) malloc( sizeof(char) * 16 );
|
|
|
|
memcpy( (unsigned char *) *bytes_s, &g_rand_buff + g_rand_idx, sizeof(char) * 16 );
|
|
|
|
g_rand_idx += 16;
|
|
|
|
}
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
if( !calculate_x( x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password ))
|
2013-04-02 19:32:42 -07:00
|
|
|
goto cleanup_and_exit;
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_powm(v, ng->g, x, ng->N);
|
|
|
|
|
|
|
|
*len_v = mpz_num_bytes(v);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
*bytes_v = (const unsigned char *) malloc( *len_v );
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
if (!bytes_v)
|
2013-04-02 19:32:42 -07:00
|
|
|
goto cleanup_and_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_to_bin(v, (unsigned char *) *bytes_v);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
cleanup_and_exit:
|
|
|
|
delete_ng( ng );
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_clear(v);
|
|
|
|
mpz_clear(x);
|
2013-04-02 19:32:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Out: bytes_B, len_B.
|
2015-04-11 18:40:29 -07:00
|
|
|
*
|
2013-04-02 19:32:42 -07:00
|
|
|
* On failure, bytes_B will be set to NULL and len_B will be set to 0
|
|
|
|
*/
|
|
|
|
struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_type, const char * username,
|
2015-04-11 18:40:29 -07:00
|
|
|
const unsigned char * bytes_s, int len_s,
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char * bytes_v, int len_v,
|
|
|
|
const unsigned char * bytes_A, int len_A,
|
|
|
|
const unsigned char ** bytes_B, int * len_B,
|
|
|
|
const char * n_hex, const char * g_hex )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v);
|
|
|
|
mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A);
|
|
|
|
mpz_t u; mpz_init(u);
|
|
|
|
mpz_t B; mpz_init(B);
|
|
|
|
mpz_t S; mpz_init(S);
|
|
|
|
mpz_t b; mpz_init(b);
|
|
|
|
mpz_t k; mpz_init(k);
|
|
|
|
mpz_t tmp1; mpz_init(tmp1);
|
|
|
|
mpz_t tmp2; mpz_init(tmp2);
|
|
|
|
int ulen = strlen(username) + 1;
|
|
|
|
NGConstant *ng = new_ng( ng_type, n_hex, g_hex );
|
|
|
|
struct SRPVerifier *ver = 0;
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
*len_B = 0;
|
|
|
|
*bytes_B = 0;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
if( !v || !A || !B || !S || !b || !tmp1 || !tmp2 || !ng )
|
2013-04-02 19:32:42 -07:00
|
|
|
goto cleanup_and_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
ver = (struct SRPVerifier *) malloc( sizeof(struct SRPVerifier) );
|
|
|
|
|
|
|
|
if (!ver)
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
|
|
|
|
init_random(); /* Only happens once */
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
ver->username = (char *) malloc( ulen );
|
|
|
|
ver->hash_alg = alg;
|
|
|
|
ver->ng = ng;
|
|
|
|
|
|
|
|
if (!ver->username)
|
|
|
|
{
|
|
|
|
free(ver);
|
|
|
|
ver = 0;
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
memcpy( (char*)ver->username, username, ulen );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
ver->authenticated = 0;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
/* SRP-6a safety check */
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_mod(tmp1, A, ng->N);
|
|
|
|
if ( mpz_sgn(tmp1) != 0 )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_fill_random(b);
|
|
|
|
|
|
|
|
H_nn(k, alg, ng->N, ng->g);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
/* B = kv + g^b */
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_mul(tmp1, k, v);
|
|
|
|
mpz_powm(tmp2, ng->g, b, ng->N);
|
|
|
|
mpz_add(B, tmp1, tmp2);
|
|
|
|
|
|
|
|
H_nn(u, alg, A, B);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
/* S = (A *(v^u)) ^ b */
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_powm(tmp1, v, u, ng->N);
|
|
|
|
mpz_mul(tmp2, A, tmp1);
|
|
|
|
mpz_powm(S, tmp2, b, ng->N);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
hash_num(alg, S, ver->session_key);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
calculate_M( alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key );
|
2013-04-02 19:32:42 -07:00
|
|
|
calculate_H_AMK( alg, ver->H_AMK, A, ver->M, ver->session_key );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
*len_B = mpz_num_bytes(B);
|
2013-04-02 19:32:42 -07:00
|
|
|
*bytes_B = malloc( *len_B );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
if( !*bytes_B )
|
|
|
|
{
|
|
|
|
free( (void*) ver->username );
|
|
|
|
free( ver );
|
|
|
|
ver = 0;
|
|
|
|
*len_B = 0;
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_to_bin( B, (unsigned char *) *bytes_B );
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
ver->bytes_B = *bytes_B;
|
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
cleanup_and_exit:
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_clear(v);
|
|
|
|
mpz_clear(A);
|
|
|
|
if (u) mpz_clear(u);
|
|
|
|
if (k) mpz_clear(k);
|
|
|
|
mpz_clear(B);
|
|
|
|
mpz_clear(S);
|
|
|
|
mpz_clear(b);
|
|
|
|
mpz_clear(tmp1);
|
|
|
|
mpz_clear(tmp2);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
return ver;
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
void srp_verifier_delete( struct SRPVerifier * ver )
|
|
|
|
{
|
|
|
|
if (ver)
|
|
|
|
{
|
|
|
|
delete_ng( ver->ng );
|
|
|
|
free( (char *) ver->username );
|
|
|
|
free( (unsigned char *) ver->bytes_B );
|
|
|
|
memset(ver, 0, sizeof(*ver));
|
|
|
|
free( ver );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int srp_verifier_is_authenticated( struct SRPVerifier * ver )
|
|
|
|
{
|
|
|
|
return ver->authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * srp_verifier_get_username( struct SRPVerifier * ver )
|
|
|
|
{
|
|
|
|
return ver->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned char * srp_verifier_get_session_key( struct SRPVerifier * ver, int * key_length )
|
|
|
|
{
|
|
|
|
if (key_length)
|
|
|
|
*key_length = hash_length( ver->hash_alg );
|
|
|
|
return ver->session_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int srp_verifier_get_session_key_length( struct SRPVerifier * ver )
|
|
|
|
{
|
|
|
|
return hash_length( ver->hash_alg );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
|
|
|
|
void srp_verifier_verify_session( struct SRPVerifier * ver, const unsigned char * user_M, const unsigned char ** bytes_HAMK )
|
|
|
|
{
|
|
|
|
if ( memcmp( ver->M, user_M, hash_length(ver->hash_alg) ) == 0 )
|
|
|
|
{
|
|
|
|
ver->authenticated = 1;
|
|
|
|
*bytes_HAMK = ver->H_AMK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*bytes_HAMK = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
|
|
|
|
const char * username, const char * username_for_verifier,
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char * bytes_password, int len_password,
|
|
|
|
const char * n_hex, const char * g_hex )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
struct SRPUser *usr = (struct SRPUser *) malloc( sizeof(struct SRPUser) );
|
|
|
|
int ulen = strlen(username) + 1;
|
|
|
|
int uvlen = strlen(username_for_verifier) + 1;
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
if (!usr)
|
|
|
|
goto err_exit;
|
|
|
|
|
|
|
|
init_random(); /* Only happens once */
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
usr->hash_alg = alg;
|
|
|
|
usr->ng = new_ng( ng_type, n_hex, g_hex );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_init(usr->a);
|
|
|
|
mpz_init(usr->A);
|
|
|
|
mpz_init(usr->S);
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
if (!usr->ng || !usr->a || !usr->A || !usr->S)
|
|
|
|
goto err_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
usr->username = (const char *) malloc(ulen);
|
|
|
|
usr->username_verifier = (const char *) malloc(uvlen);
|
|
|
|
usr->password = (const unsigned char *) malloc(len_password);
|
|
|
|
usr->password_len = len_password;
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
if (!usr->username || !usr->password)
|
|
|
|
goto err_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
memcpy((char *)usr->username, username, ulen);
|
|
|
|
memcpy((char *)usr->username_verifier, username_for_verifier, uvlen);
|
|
|
|
memcpy((char *)usr->password, bytes_password, len_password);
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
usr->authenticated = 0;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
usr->bytes_A = 0;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
return usr;
|
|
|
|
|
|
|
|
err_exit:
|
|
|
|
if (usr)
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_clear(usr->a);
|
|
|
|
mpz_clear(usr->A);
|
|
|
|
mpz_clear(usr->S);
|
2013-04-02 19:32:42 -07:00
|
|
|
if (usr->username)
|
|
|
|
free((void*)usr->username);
|
|
|
|
if (usr->password)
|
|
|
|
{
|
|
|
|
memset((void*)usr->password, 0, usr->password_len);
|
|
|
|
free((void*)usr->password);
|
|
|
|
}
|
|
|
|
free(usr);
|
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void srp_user_delete( struct SRPUser * usr )
|
|
|
|
{
|
|
|
|
if( usr )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_clear( usr->a );
|
|
|
|
mpz_clear( usr->A );
|
|
|
|
mpz_clear( usr->S );
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
delete_ng( usr->ng );
|
|
|
|
|
|
|
|
memset((void*)usr->password, 0, usr->password_len);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
free((char *)usr->username);
|
|
|
|
free((char *)usr->password);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
if (usr->bytes_A)
|
2013-04-02 19:32:42 -07:00
|
|
|
free( (char *)usr->bytes_A );
|
|
|
|
|
|
|
|
memset(usr, 0, sizeof(*usr));
|
|
|
|
free( usr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int srp_user_is_authenticated( struct SRPUser * usr)
|
|
|
|
{
|
|
|
|
return usr->authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * srp_user_get_username( struct SRPUser * usr )
|
|
|
|
{
|
|
|
|
return usr->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned char * srp_user_get_session_key( struct SRPUser * usr, int * key_length )
|
|
|
|
{
|
|
|
|
if (key_length)
|
|
|
|
*key_length = hash_length( usr->hash_alg );
|
|
|
|
return usr->session_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int srp_user_get_session_key_length( struct SRPUser * usr )
|
|
|
|
{
|
|
|
|
return hash_length( usr->hash_alg );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Output: username, bytes_A, len_A */
|
2015-04-11 18:40:29 -07:00
|
|
|
void srp_user_start_authentication( struct SRPUser * usr, const char ** username,
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char ** bytes_A, int * len_A )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_fill_random(usr->a);
|
|
|
|
|
|
|
|
mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
|
|
|
|
|
|
|
|
*len_A = mpz_num_bytes(usr->A);
|
2013-04-02 19:32:42 -07:00
|
|
|
*bytes_A = malloc( *len_A );
|
|
|
|
|
|
|
|
if (!*bytes_A)
|
|
|
|
{
|
|
|
|
*len_A = 0;
|
|
|
|
*bytes_A = 0;
|
|
|
|
*username = 0;
|
|
|
|
return;
|
|
|
|
}
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_to_bin( usr->A, (unsigned char *) *bytes_A );
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
usr->bytes_A = *bytes_A;
|
|
|
|
*username = usr->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
|
2015-04-11 18:40:29 -07:00
|
|
|
void srp_user_process_challenge( struct SRPUser * usr,
|
|
|
|
const unsigned char * bytes_s, int len_s,
|
2013-04-02 19:32:42 -07:00
|
|
|
const unsigned char * bytes_B, int len_B,
|
|
|
|
const unsigned char ** bytes_M, int * len_M )
|
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B);
|
|
|
|
mpz_t u; mpz_init(u);
|
|
|
|
mpz_t x; mpz_init(x);
|
|
|
|
mpz_t k; mpz_init(k);
|
|
|
|
mpz_t v; mpz_init(v);
|
|
|
|
mpz_t tmp1; mpz_init(tmp1);
|
|
|
|
mpz_t tmp2; mpz_init(tmp2);
|
|
|
|
mpz_t tmp3; mpz_init(tmp3);
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
*len_M = 0;
|
|
|
|
*bytes_M = 0;
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
if( !B || !u || !x || !k || !v || !tmp1 || !tmp2 || !tmp3 )
|
2013-04-02 19:32:42 -07:00
|
|
|
goto cleanup_and_exit;
|
|
|
|
|
2015-04-11 18:40:29 -07:00
|
|
|
if (!H_nn(u, usr->hash_alg, usr->A, B))
|
2013-04-02 19:32:42 -07:00
|
|
|
goto cleanup_and_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
calculate_x( x, usr->hash_alg, bytes_s, len_s, usr->username_verifier, usr->password, usr->password_len );
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
if (!x)
|
|
|
|
goto cleanup_and_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
H_nn(k, usr->hash_alg, usr->ng->N, usr->ng->g);
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
if (!k)
|
|
|
|
goto cleanup_and_exit;
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
/* SRP-6a safety check */
|
2015-04-11 18:40:29 -07:00
|
|
|
if ( mpz_sgn(B) != 0 && mpz_sgn(u) != 0 )
|
2013-04-02 19:32:42 -07:00
|
|
|
{
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_powm(v, usr->ng->g, x, usr->ng->N);
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
/* S = (B - k*(g^x)) ^ (a + ux) */
|
2015-04-11 18:40:29 -07:00
|
|
|
mpz_mul(tmp1, u, x);
|
|
|
|
mpz_add(tmp2, usr->a, tmp1); /* tmp2 = (a + ux) */
|
|
|
|
mpz_powm(tmp1, usr->ng->g, x, usr->ng->N);
|
|
|
|
mpz_mul(tmp3, k, tmp1); /* tmp3 = k*(g^x) */
|
|
|
|
mpz_sub(tmp1, B, tmp3); /* tmp1 = (B - K*(g^x)) */
|
|
|
|
mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
hash_num(usr->hash_alg, usr->S, usr->session_key);
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
calculate_M( usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s, usr->A, B, usr->session_key );
|
2013-04-02 19:32:42 -07:00
|
|
|
calculate_H_AMK( usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key );
|
2015-04-11 18:40:29 -07:00
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
*bytes_M = usr->M;
|
|
|
|
if (len_M)
|
|
|
|
*len_M = hash_length( usr->hash_alg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*bytes_M = NULL;
|
2015-04-11 18:40:29 -07:00
|
|
|
if (len_M)
|
2013-04-02 19:32:42 -07:00
|
|
|
*len_M = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_and_exit:
|
2015-04-11 18:40:29 -07:00
|
|
|
|
|
|
|
mpz_clear(B);
|
|
|
|
mpz_clear(u);
|
|
|
|
mpz_clear(x);
|
|
|
|
mpz_clear(k);
|
|
|
|
mpz_clear(v);
|
|
|
|
mpz_clear(tmp1);
|
|
|
|
mpz_clear(tmp2);
|
|
|
|
mpz_clear(tmp3);
|
|
|
|
}
|
|
|
|
|
2013-04-02 19:32:42 -07:00
|
|
|
|
|
|
|
void srp_user_verify_session( struct SRPUser * usr, const unsigned char * bytes_HAMK )
|
|
|
|
{
|
|
|
|
if ( memcmp( usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg) ) == 0 )
|
|
|
|
usr->authenticated = 1;
|
|
|
|
}
|