Minetest  5.4.0
srp.cpp File Reference
#include <cstddef>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstdint>
#include <config.h>
#include <mini-gmp.h>
#include <util/sha2.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 srp_dbg_data(data, datalen, prevtext)   ;
 
#define RAND_BUFF_MAX   128
 
#define srp_dbg_num(num, text)   ;
 

Functions

void srp_set_memory_functions (void *(*new_srp_alloc)(size_t), void *(*new_srp_realloc)(void *, size_t), void(*new_srp_free)(void *))
 
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, uint32_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 fill_buff ()
 
static SRP_Result mpz_fill_random (mpz_t num)
 
static SRP_Result init_random ()
 
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 int g_initialized = 0
 
static unsigned int g_rand_idx
 
static unsigned char g_rand_buff [RAND_BUFF_MAX]
 
void *(* srp_alloc )(size_t) = &malloc
 
void *(* srp_realloc )(void *, size_t) = &realloc
 
void(* srp_free )(void *) = &free
 
static struct NGHex global_Ng_constants []
 

Macro Definition Documentation

◆ CSRP_USE_SHA256

#define CSRP_USE_SHA256

◆ RAND_BUFF_MAX

#define RAND_BUFF_MAX   128

◆ srp_dbg_data

#define srp_dbg_data (   data,
  datalen,
  prevtext 
)    ;

◆ 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 NGConstant::g, hash(), hash_final(), hash_init(), hash_length(), hash_num(), hash_update(), NGConstant::N, SHA512_DIGEST_LENGTH, 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 H_ns(), hash_final(), hash_init(), hash_length(), hash_update(), SHA512_DIGEST_LENGTH, 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, NGConstant::N, and srp_free.

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:

◆ fill_buff()

static SRP_Result fill_buff ( )
static

References g_rand_buff, g_rand_idx, SRP_ERR, and SRP_OK.

Referenced by init_random(), mpz_fill_random(), and srp_create_salted_verification_key().

+ 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 hash(), hash_length(), mpz_from_bin(), mpz_num_bytes(), mpz_to_bin(), SHA512_DIGEST_LENGTH, srp_alloc, SRP_ERR, srp_free, 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,
uint32_t  len_bytes 
)
static

References hash(), hash_length(), mpz_from_bin(), SHA512_DIGEST_LENGTH, srp_alloc, SRP_ERR, srp_free, 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 SHA256(), and 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(), and ModApiUtil::l_get_password_hash().

+ Here is the call graph for this function:
+ 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::sha, HashCTX::sha256, SHA256_Final(), and SRP_SHA256.

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

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

◆ hash_init()

static int hash_init ( SRP_HashAlgorithm  alg,
HashCTX c 
)
static

References HashCTX::sha, HashCTX::sha256, SHA256_Init(), and SRP_SHA256.

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

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

◆ hash_length()

static size_t hash_length ( SRP_HashAlgorithm  alg)
static

References SHA256_DIGEST_LENGTH, SHA_DIGEST_LENGTH, and 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_alloc, SRP_ERR, srp_free, 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::sha, HashCTX::sha256, SHA256_Update(), and SRP_SHA256.

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

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

◆ init_random()

static SRP_Result init_random ( )
static

References fill_buff(), g_initialized, and SRP_OK.

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:

◆ 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 fill_buff(), g_rand_buff, g_rand_idx, mpz_from_bin(), RAND_BUFF_MAX, 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, srp_alloc, 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(), fill_buff(), NGConstant::g, g_rand_buff, g_rand_idx, init_random(), mpz_num_bytes(), mpz_to_bin(), NGConstant::N, new_ng(), RAND_BUFF_MAX, srp_alloc, 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_set_memory_functions()

void srp_set_memory_functions ( void *(*)(size_t)  new_srp_alloc,
void *(*)(void *, size_t)  new_srp_realloc,
void(*)(void *)  new_srp_free 
)

◆ 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, srp_free, 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, init_random(), new_ng(), SRPUser::ng, SRPUser::password, SRPUser::password_len, SRPUser::S, srp_alloc, srp_free, SRP_OK, 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_alloc, 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, srp_free, and SRPVerifier::username.

Referenced by RemoteClient::notifyEvent().

+ 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(), init_random(), 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_alloc, srp_dbg_num, srp_free, SRP_OK, 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_alloc, SRP_ERR, srp_free, 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

◆ g_initialized

int g_initialized = 0
static

Referenced by init_random().

◆ g_rand_buff

unsigned char g_rand_buff[RAND_BUFF_MAX]
static

◆ g_rand_idx

unsigned int g_rand_idx
static

◆ global_Ng_constants

struct NGHex global_Ng_constants[]
static

Referenced by new_ng().

◆ srp_alloc

◆ srp_free

void(* srp_free) (void *) ( void *  ) = &free

◆ srp_realloc

void*(* srp_realloc) (void *, size_t) ( void *  ,
size_t   
) = &realloc