Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
srp.cpp File Reference
#include <cstddef>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstdint>
#include <config.h>
#include <mini-gmp.h>
#include "my_sha256.h"
#include "porting.h"
#include "srp.h"
+ Include dependency graph for srp.cpp:

Classes

struct  NGConstant
 
struct  NGHex
 
union  HashCTX
 
struct  SRPVerifier
 
struct  SRPUser
 

Macros

#define CSRP_USE_SHA256
 
#define CSRP_MAX_HASH   (SHA256_DIGEST_LENGTH)
 
#define srp_dbg_data(data, datalen, prevtext)   ;
 
#define srp_dbg_num(num, text)   ;
 

Functions

static void delete_ng (NGConstant *ng)
 
static NGConstantnew_ng (SRP_NGType ng_type, const char *n_hex, const char *g_hex)
 
static int hash_init (SRP_HashAlgorithm alg, HashCTX *c)
 
static int hash_update (SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len)
 
static int hash_final (SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md)
 
static unsigned char * hash (SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md)
 
static size_t hash_length (SRP_HashAlgorithm alg)
 
static int mpz_num_bytes (const mpz_t op)
 
static void mpz_to_bin (const mpz_t op, unsigned char *to)
 
static void mpz_from_bin (const unsigned char *s, size_t len, mpz_t ret)
 
static void mpz_mulm (mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
 
static void mpz_addm (mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
 
static void mpz_subm (mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
 
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_ns (mpz_t result, SRP_HashAlgorithm alg, const unsigned char *n, size_t len_n, const unsigned char *bytes, size_t len_bytes)
 
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)
 
static SRP_Result update_hash_n (SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
 
static SRP_Result hash_num (SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest)
 
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)
 
static SRP_Result calculate_H_AMK (SRP_HashAlgorithm alg, unsigned char *dest, const mpz_t A, const unsigned char *M, const unsigned char *K)
 
static SRP_Result mpz_fill_random (mpz_t num)
 
SRP_Result srp_create_salted_verification_key (SRP_HashAlgorithm alg, SRP_NGType ng_type, const char *username_for_verifier, const unsigned char *password, size_t len_password, unsigned char **bytes_s, size_t *len_s, unsigned char **bytes_v, size_t *len_v, const char *n_hex, const char *g_hex)
 
struct SRPVerifiersrp_verifier_new (SRP_HashAlgorithm alg, SRP_NGType ng_type, const char *username, const unsigned char *bytes_s, size_t len_s, const unsigned char *bytes_v, size_t len_v, const unsigned char *bytes_A, size_t len_A, const unsigned char *bytes_b, size_t len_b, unsigned char **bytes_B, size_t *len_B, const char *n_hex, const char *g_hex)
 
void srp_verifier_delete (struct SRPVerifier *ver)
 
int srp_verifier_is_authenticated (struct SRPVerifier *ver)
 
const char * srp_verifier_get_username (struct SRPVerifier *ver)
 
const unsigned char * srp_verifier_get_session_key (struct SRPVerifier *ver, size_t *key_length)
 
size_t srp_verifier_get_session_key_length (struct SRPVerifier *ver)
 
void srp_verifier_verify_session (struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
 
struct SRPUsersrp_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)
 
void srp_user_delete (struct SRPUser *usr)
 
int srp_user_is_authenticated (struct SRPUser *usr)
 
const char * srp_user_get_username (struct SRPUser *usr)
 
const unsigned char * srp_user_get_session_key (struct SRPUser *usr, size_t *key_length)
 
size_t srp_user_get_session_key_length (struct SRPUser *usr)
 
SRP_Result srp_user_start_authentication (struct SRPUser *usr, char **username, const unsigned char *bytes_a, size_t len_a, unsigned char **bytes_A, size_t *len_A)
 
void srp_user_process_challenge (struct SRPUser *usr, const unsigned char *bytes_s, size_t len_s, const unsigned char *bytes_B, size_t len_B, unsigned char **bytes_M, size_t *len_M)
 
void srp_user_verify_session (struct SRPUser *usr, const unsigned char *bytes_HAMK)
 

Variables

static struct NGHex global_Ng_constants []
 

Macro Definition Documentation

◆ CSRP_MAX_HASH

#define CSRP_MAX_HASH   (SHA256_DIGEST_LENGTH)

Referenced by calculate_M(), calculate_x(), H_nn(), and H_ns().

◆ CSRP_USE_SHA256

#define CSRP_USE_SHA256

◆ srp_dbg_data

#define srp_dbg_data ( data,
datalen,
prevtext )   ;

Referenced by calculate_x().

◆ srp_dbg_num

#define srp_dbg_num ( num,
text )   ;

Function Documentation

◆ calculate_H_AMK()

static SRP_Result calculate_H_AMK ( SRP_HashAlgorithm alg,
unsigned char * dest,
const mpz_t A,
const unsigned char * M,
const unsigned char * K )
static

References hash_final(), hash_init(), hash_length(), hash_update(), SRP_ERR, SRP_OK, and update_hash_n().

Referenced by srp_user_process_challenge(), and srp_verifier_new().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ calculate_M()

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 )
static

References CSRP_MAX_HASH, NGConstant::g, hash(), hash_final(), hash_init(), hash_length(), hash_num(), hash_update(), NGConstant::N, SRP_ERR, SRP_OK, and update_hash_n().

Referenced by srp_user_process_challenge(), and srp_verifier_new().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ calculate_x()

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 )
static

References CSRP_MAX_HASH, H_ns(), hash_final(), hash_init(), hash_length(), hash_update(), and srp_dbg_data.

Referenced by srp_create_salted_verification_key(), and srp_user_process_challenge().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ delete_ng()

static void delete_ng ( NGConstant * ng)
static

References NGConstant::g, and NGConstant::N.

Referenced by new_ng(), srp_create_salted_verification_key(), srp_user_delete(), srp_user_new(), and srp_verifier_delete().

+ Here is the caller graph for this function:

◆ H_nn()

static SRP_Result H_nn ( mpz_t result,
SRP_HashAlgorithm alg,
const mpz_t N,
const mpz_t n1,
const mpz_t n2 )
static

References CSRP_MAX_HASH, hash(), hash_length(), mpz_from_bin(), mpz_num_bytes(), mpz_to_bin(), SRP_ERR, and SRP_OK.

Referenced by srp_user_process_challenge(), and srp_verifier_new().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ H_ns()

static SRP_Result H_ns ( mpz_t result,
SRP_HashAlgorithm alg,
const unsigned char * n,
size_t len_n,
const unsigned char * bytes,
size_t len_bytes )
static

References CSRP_MAX_HASH, hash(), hash_length(), mpz_from_bin(), SRP_ERR, and SRP_OK.

Referenced by calculate_x().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hash()

static unsigned char * hash ( SRP_HashAlgorithm alg,
const unsigned char * d,
size_t n,
unsigned char * md )
static

References SRP_SHA256.

Referenced by calculate_M(), check_field_or_nil(), CCraftDefManager::getCraftResult(), H_nn(), H_ns(), hash_num(), CCraftDefManager::initHashes(), ModApiUtil::l_check_password_entry(), ModApiBase::l_deprecated_function(), ModApiUtil::l_get_password_hash(), logOnce(), and script_log_unique().

+ Here is the caller graph for this function:

◆ hash_final()

static int hash_final ( SRP_HashAlgorithm alg,
HashCTX * c,
unsigned char * md )
static

References HashCTX::sha256, and SRP_SHA256.

Referenced by calculate_H_AMK(), calculate_M(), and calculate_x().

+ Here is the caller graph for this function:

◆ hash_init()

static int hash_init ( SRP_HashAlgorithm alg,
HashCTX * c )
static

References HashCTX::sha256, and SRP_SHA256.

Referenced by calculate_H_AMK(), calculate_M(), and calculate_x().

+ Here is the caller graph for this function:

◆ hash_length()

static size_t hash_length ( SRP_HashAlgorithm alg)
static

References SRP_SHA256.

Referenced by calculate_H_AMK(), calculate_M(), calculate_x(), H_nn(), H_ns(), srp_user_get_session_key(), srp_user_get_session_key_length(), srp_user_process_challenge(), srp_user_verify_session(), srp_verifier_get_session_key(), srp_verifier_get_session_key_length(), and srp_verifier_verify_session().

+ Here is the caller graph for this function:

◆ hash_num()

static SRP_Result hash_num ( SRP_HashAlgorithm alg,
const mpz_t n,
unsigned char * dest )
static

References hash(), mpz_num_bytes(), mpz_to_bin(), SRP_ERR, and SRP_OK.

Referenced by calculate_M(), srp_user_process_challenge(), and srp_verifier_new().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hash_update()

static int hash_update ( SRP_HashAlgorithm alg,
HashCTX * c,
const void * data,
size_t len )
static

References HashCTX::sha256, and SRP_SHA256.

Referenced by calculate_H_AMK(), calculate_M(), calculate_x(), and update_hash_n().

+ Here is the caller graph for this function:

◆ mpz_addm()

static void mpz_addm ( mpz_t op,
const mpz_t op1,
const mpz_t op2,
const mpz_t d,
mpz_t tmp )
inlinestatic

Referenced by srp_verifier_new().

+ Here is the caller graph for this function:

◆ mpz_fill_random()

static SRP_Result mpz_fill_random ( mpz_t num)
static

References mpz_from_bin(), porting::secure_rand_fill_buf(), SRP_ERR, and SRP_OK.

Referenced by srp_user_start_authentication(), and srp_verifier_new().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mpz_from_bin()

static void mpz_from_bin ( const unsigned char * s,
size_t len,
mpz_t ret )
inlinestatic

Referenced by H_nn(), H_ns(), mpz_fill_random(), srp_user_process_challenge(), srp_user_start_authentication(), and srp_verifier_new().

+ Here is the caller graph for this function:

◆ mpz_mulm()

static void mpz_mulm ( mpz_t op,
const mpz_t op1,
const mpz_t op2,
const mpz_t d,
mpz_t tmp )
inlinestatic

Referenced by srp_user_process_challenge(), and srp_verifier_new().

+ Here is the caller graph for this function:

◆ mpz_num_bytes()

static int mpz_num_bytes ( const mpz_t op)
inlinestatic

Referenced by H_nn(), hash_num(), srp_create_salted_verification_key(), srp_user_start_authentication(), srp_verifier_new(), and update_hash_n().

+ Here is the caller graph for this function:

◆ mpz_subm()

static void mpz_subm ( mpz_t op,
const mpz_t op1,
const mpz_t op2,
const mpz_t d,
mpz_t tmp )
inlinestatic

Referenced by srp_user_process_challenge().

+ Here is the caller graph for this function:

◆ mpz_to_bin()

static void mpz_to_bin ( const mpz_t op,
unsigned char * to )
inlinestatic

Referenced by H_nn(), hash_num(), srp_create_salted_verification_key(), srp_user_start_authentication(), srp_verifier_new(), and update_hash_n().

+ Here is the caller graph for this function:

◆ new_ng()

static NGConstant * new_ng ( SRP_NGType ng_type,
const char * n_hex,
const char * g_hex )
static

References delete_ng(), NGConstant::g, NGHex::g_hex, global_Ng_constants, NGConstant::N, NGHex::n_hex, and SRP_NG_CUSTOM.

Referenced by srp_create_salted_verification_key(), srp_user_new(), and srp_verifier_new().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_create_salted_verification_key()

SRP_Result srp_create_salted_verification_key ( SRP_HashAlgorithm alg,
SRP_NGType ng_type,
const char * username_for_verifier,
const unsigned char * password,
size_t len_password,
unsigned char ** bytes_s,
size_t * len_s,
unsigned char ** bytes_v,
size_t * len_v,
const char * n_hex,
const char * g_hex )

References calculate_x(), delete_ng(), NGConstant::g, mpz_num_bytes(), mpz_to_bin(), NGConstant::N, new_ng(), porting::secure_rand_fill_buf(), srp_dbg_num, SRP_ERR, and SRP_OK.

Referenced by gen_srp_v().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_user_delete()

void srp_user_delete ( struct SRPUser * usr)

References SRPUser::A, SRPUser::a, SRPUser::bytes_A, delete_ng(), SRPUser::ng, SRPUser::password, SRPUser::password_len, SRPUser::S, SRPUser::username, and SRPUser::username_verifier.

Referenced by Client::deleteAuthData(), and Client::handleCommand_Hello().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_user_get_session_key()

const unsigned char * srp_user_get_session_key ( struct SRPUser * usr,
size_t * key_length )

References SRPUser::hash_alg, hash_length(), and SRPUser::session_key.

+ Here is the call graph for this function:

◆ srp_user_get_session_key_length()

size_t srp_user_get_session_key_length ( struct SRPUser * usr)

References SRPUser::hash_alg, and hash_length().

+ Here is the call graph for this function:

◆ srp_user_get_username()

const char * srp_user_get_username ( struct SRPUser * usr)

References SRPUser::username.

◆ srp_user_is_authenticated()

int srp_user_is_authenticated ( struct SRPUser * usr)

◆ srp_user_new()

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 )

References SRPUser::A, SRPUser::a, SRPUser::authenticated, SRPUser::bytes_A, delete_ng(), SRPUser::hash_alg, new_ng(), SRPUser::ng, SRPUser::password, SRPUser::password_len, SRPUser::S, SRPUser::username, and SRPUser::username_verifier.

Referenced by Client::startAuth().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_user_process_challenge()

void srp_user_process_challenge ( struct SRPUser * usr,
const unsigned char * bytes_s,
size_t len_s,
const unsigned char * bytes_B,
size_t len_B,
unsigned char ** bytes_M,
size_t * len_M )

References SRPUser::A, SRPUser::a, calculate_H_AMK(), calculate_M(), calculate_x(), NGConstant::g, SRPUser::H_AMK, H_nn(), SRPUser::hash_alg, hash_length(), hash_num(), SRPUser::M, mpz_from_bin(), mpz_mulm(), mpz_subm(), NGConstant::N, SRPUser::ng, SRPUser::password, SRPUser::password_len, SRPUser::S, SRPUser::session_key, srp_dbg_num, SRPUser::username, and SRPUser::username_verifier.

Referenced by Client::handleCommand_SrpBytesSandB().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_user_start_authentication()

SRP_Result srp_user_start_authentication ( struct SRPUser * usr,
char ** username,
const unsigned char * bytes_a,
size_t len_a,
unsigned char ** bytes_A,
size_t * len_A )

References SRPUser::A, SRPUser::a, SRPUser::bytes_A, NGConstant::g, mpz_fill_random(), mpz_from_bin(), mpz_num_bytes(), mpz_to_bin(), NGConstant::N, SRPUser::ng, SRP_ERR, SRP_OK, and SRPUser::username.

Referenced by Client::startAuth().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_user_verify_session()

void srp_user_verify_session ( struct SRPUser * usr,
const unsigned char * bytes_HAMK )

References SRPUser::authenticated, SRPUser::H_AMK, SRPUser::hash_alg, and hash_length().

+ Here is the call graph for this function:

◆ srp_verifier_delete()

void srp_verifier_delete ( struct SRPVerifier * ver)

References SRPVerifier::bytes_B, delete_ng(), SRPVerifier::ng, and SRPVerifier::username.

Referenced by RemoteClient::resetChosenMech().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_verifier_get_session_key()

const unsigned char * srp_verifier_get_session_key ( struct SRPVerifier * ver,
size_t * key_length )

References SRPVerifier::hash_alg, hash_length(), and SRPVerifier::session_key.

+ Here is the call graph for this function:

◆ srp_verifier_get_session_key_length()

size_t srp_verifier_get_session_key_length ( struct SRPVerifier * ver)

References SRPVerifier::hash_alg, and hash_length().

Referenced by Server::handleCommand_SrpBytesM().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_verifier_get_username()

const char * srp_verifier_get_username ( struct SRPVerifier * ver)

References SRPVerifier::username.

◆ srp_verifier_is_authenticated()

int srp_verifier_is_authenticated ( struct SRPVerifier * ver)

◆ srp_verifier_new()

struct SRPVerifier * srp_verifier_new ( SRP_HashAlgorithm alg,
SRP_NGType ng_type,
const char * username,
const unsigned char * bytes_s,
size_t len_s,
const unsigned char * bytes_v,
size_t len_v,
const unsigned char * bytes_A,
size_t len_A,
const unsigned char * bytes_b,
size_t len_b,
unsigned char ** bytes_B,
size_t * len_B,
const char * n_hex,
const char * g_hex )

References SRPVerifier::authenticated, SRPVerifier::bytes_B, calculate_H_AMK(), calculate_M(), NGConstant::g, SRPVerifier::H_AMK, H_nn(), SRPVerifier::hash_alg, hash_num(), SRPVerifier::M, mpz_addm(), mpz_fill_random(), mpz_from_bin(), mpz_mulm(), mpz_num_bytes(), mpz_to_bin(), NGConstant::N, new_ng(), SRPVerifier::ng, SRPVerifier::session_key, srp_dbg_num, and SRPVerifier::username.

Referenced by Server::handleCommand_SrpBytesA().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ srp_verifier_verify_session()

void srp_verifier_verify_session ( struct SRPVerifier * ver,
const unsigned char * user_M,
unsigned char ** bytes_HAMK )

References SRPVerifier::authenticated, SRPVerifier::H_AMK, SRPVerifier::hash_alg, hash_length(), and SRPVerifier::M.

Referenced by Server::handleCommand_SrpBytesM().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update_hash_n()

static SRP_Result update_hash_n ( SRP_HashAlgorithm alg,
HashCTX * ctx,
const mpz_t n )
static

References hash_update(), mpz_num_bytes(), mpz_to_bin(), SRP_ERR, and SRP_OK.

Referenced by calculate_H_AMK(), and calculate_M().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ global_Ng_constants

struct NGHex global_Ng_constants[]
static

Referenced by new_ng().