Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
srp.h
Go to the documentation of this file.
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
29/*
30 *
31 * Purpose: This is a direct implementation of the Secure Remote Password
32 * Protocol version 6a as described by
33 * http://srp.stanford.edu/design.html
34 *
35 * Author: tom.cocagne@gmail.com (Tom Cocagne)
36 *
37 * Dependencies: LibGMP
38 *
39 * Usage: Refer to test_srp.c for a demonstration
40 *
41 * Notes:
42 * This library allows multiple combinations of hashing algorithms and
43 * prime number constants. For authentication to succeed, the hash and
44 * prime number constants must match between
45 * srp_create_salted_verification_key(), srp_user_new(),
46 * and srp_verifier_new(). A recommended approach is to determine the
47 * desired level of security for an application and globally define the
48 * hash and prime number constants to the predetermined values.
49 *
50 * As one might suspect, more bits means more security. As one might also
51 * suspect, more bits also means more processing time. The test_srp.c
52 * program can be easily modified to profile various combinations of
53 * hash & prime number pairings.
54 */
55
56#pragma once
57
58#include <cstddef>
59
60struct SRPVerifier;
61struct SRPUser;
62
70
71typedef enum {
72 /*SRP_SHA1,*/
73 /*SRP_SHA224,*/
75 /*SRP_SHA384,
76 SRP_SHA512*/
78
79typedef enum {
83
84
85/* Out: bytes_v, len_v
86 *
87 * The caller is responsible for freeing the memory allocated for bytes_v
88 *
89 * The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type.
90 * If provided, they must contain ASCII text of the hexidecimal notation.
91 *
92 * If bytes_s == NULL, it is filled with random data.
93 * The caller is responsible for freeing.
94 *
95 * Returns SRP_OK on success, and SRP_ERR on error.
96 * bytes_s might be in this case invalid, don't free it.
97 */
99 SRP_NGType ng_type, const char *username_for_verifier,
100 const unsigned char *password, size_t len_password,
101 unsigned char **bytes_s, size_t *len_s,
102 unsigned char **bytes_v, size_t *len_v,
103 const char *n_hex, const char *g_hex);
104
105/* Out: bytes_B, len_B.
106 *
107 * On failure, bytes_B will be set to NULL and len_B will be set to 0
108 *
109 * The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type
110 *
111 * If bytes_b == NULL, random data is used for b.
112 *
113 * Returns pointer to SRPVerifier on success, and NULL on error.
114 */
116 const char *username,
117 const unsigned char *bytes_s, size_t len_s,
118 const unsigned char *bytes_v, size_t len_v,
119 const unsigned char *bytes_A, size_t len_A,
120 const unsigned char *bytes_b, size_t len_b,
121 unsigned char** bytes_B, size_t *len_B,
122 const char* n_hex, const char* g_hex);
123
124void srp_verifier_delete(struct SRPVerifier *ver);
125
126// srp_verifier_verify_session must have been called before
128
129const char *srp_verifier_get_username(struct SRPVerifier *ver);
130
131/* key_length may be null */
132const unsigned char *srp_verifier_get_session_key(
133 struct SRPVerifier *ver, size_t *key_length);
134
136
137/* Verifies session, on success, it writes bytes_HAMK.
138 * user_M must be exactly srp_verifier_get_session_key_length() bytes in size
139 */
141 struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK);
142
143/*******************************************************************************/
144
145/* The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type */
147 const char *username, const char *username_for_verifier,
148 const unsigned char *bytes_password, size_t len_password, const char *n_hex,
149 const char *g_hex);
150
151void srp_user_delete(struct SRPUser *usr);
152
153int srp_user_is_authenticated(struct SRPUser *usr);
154
155const char *srp_user_get_username(struct SRPUser *usr);
156
157/* key_length may be null */
158const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length);
159
160size_t srp_user_get_session_key_length(struct SRPUser *usr);
161
162/* Output: username, bytes_A, len_A.
163 * If you don't want it get written, set username to NULL.
164 * If bytes_a == NULL, random data is used for a. */
166 const unsigned char *bytes_a, size_t len_a,
167 unsigned char **bytes_A, size_t* len_A);
168
169/* Output: bytes_M, len_M (len_M may be null and will always be
170 * srp_user_get_session_key_length() bytes in size) */
171void srp_user_process_challenge(struct SRPUser *usr,
172 const unsigned char *bytes_s, size_t len_s,
173 const unsigned char *bytes_B, size_t len_B,
174 unsigned char **bytes_M, size_t *len_M);
175
176/* bytes_HAMK must be exactly srp_user_get_session_key_length() bytes in size */
177void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK);
const unsigned char * srp_user_get_session_key(struct SRPUser *usr, size_t *key_length)
Definition srp.cpp:835
SRP_HashAlgorithm
Definition srp.h:71
@ SRP_SHA256
Definition srp.h:74
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)
Definition srp.cpp:586
int srp_user_is_authenticated(struct SRPUser *usr)
Definition srp.cpp:825
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)
Definition srp.cpp:748
SRP_NGType
Definition srp.h:63
@ SRP_NG_CUSTOM
Definition srp.h:68
@ SRP_NG_4096
Definition srp.h:66
@ SRP_NG_2048
Definition srp.h:65
@ SRP_NG_1024
Definition srp.h:64
@ SRP_NG_8192
Definition srp.h:67
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)
Definition srp.cpp:530
void srp_user_delete(struct SRPUser *usr)
Definition srp.cpp:803
void srp_verifier_delete(struct SRPVerifier *ver)
Definition srp.cpp:702
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)
Definition srp.cpp:847
const char * srp_user_get_username(struct SRPUser *usr)
Definition srp.cpp:830
size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
Definition srp.cpp:730
void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK)
Definition srp.cpp:951
SRP_Result
Definition srp.h:79
@ SRP_ERR
Definition srp.h:80
@ SRP_OK
Definition srp.h:81
const unsigned char * srp_verifier_get_session_key(struct SRPVerifier *ver, size_t *key_length)
Definition srp.cpp:723
const char * srp_verifier_get_username(struct SRPVerifier *ver)
Definition srp.cpp:718
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)
Definition srp.cpp:879
void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
Definition srp.cpp:736
size_t srp_user_get_session_key_length(struct SRPUser *usr)
Definition srp.cpp:841
int srp_verifier_is_authenticated(struct SRPVerifier *ver)
Definition srp.cpp:713
Definition srp.cpp:221
char * username
Definition srp.cpp:232
unsigned char * password
Definition srp.cpp:234
unsigned char * bytes_A
Definition srp.cpp:229
Definition srp.cpp:208
char * username
Definition srp.cpp:212
unsigned char * bytes_B
Definition srp.cpp:213