| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
#include <stdarg.h> |
| 8 |
#include <stddef.h> |
| 9 |
#include <setjmp.h> |
| 10 |
#include <stdint.h> |
| 11 |
#include <assert.h> |
| 12 |
#include <cmocka.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include "librist/librist_config.h" |
| 16 |
|
| 17 |
#include "src/crypto/srp.h" |
| 18 |
#include "src/crypto/srp_constants.h" |
| 19 |
#define DEBUG_USE_EXAMPLE_CONSTANTS 1 |
| 20 |
|
| 21 |
#if HAVE_MBEDTLS |
| 22 |
|
| 23 |
|
| 24 |
#ifndef _WIN32 |
| 25 |
#include <sched.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#define malloc(size) _test_malloc(size, __FILE__, __LINE__) |
| 29 |
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) |
| 30 |
#define free(obj) _test_free(obj, __FILE__, __LINE__) |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "src/crypto/srp.c" |
| 34 |
#include "src/crypto/srp_constants.c" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include "librist/logging.h" |
| 42 |
void rist_log_priv3(enum rist_log_level level, const char *format, ...) |
| 43 |
{ |
| 44 |
(void)level; |
| 45 |
(void)format; |
| 46 |
} |
| 47 |
|
| 48 |
static void hexstr_to_uint(const char *hexstr, uint8_t *buf, size_t buf_len) { |
| 49 |
for (size_t i = 0, j = 0; j < buf_len; i += 2, j++) |
| 50 |
buf[j] = (hexstr[i] % 32 + 9) % 25 * 16 + (hexstr[i + 1] % 32 + 9) % 25; |
| 51 |
} |
| 52 |
|
| 53 |
static void uint_to_hex(const uint8_t *buf, size_t buf_len, char *outbuf) { |
| 54 |
size_t j; |
| 55 |
for (j = 0; j < buf_len; j++) { |
| 56 |
outbuf[2 * j] = (buf[j] >> 4) + 48; |
| 57 |
outbuf[2 * j + 1] = (buf[j] & 15) + 48; |
| 58 |
if (outbuf[2 * j] > 57) |
| 59 |
outbuf[2 * j] += 7; |
| 60 |
if (outbuf[2 * j + 1] > 57) |
| 61 |
outbuf[2 * j + 1] += 7; |
| 62 |
} |
| 63 |
outbuf[2 * j] = '\0'; |
| 64 |
} |
| 65 |
|
| 66 |
struct srp_test_state { |
| 67 |
const char *n; |
| 68 |
const char *g; |
| 69 |
uint8_t *salt; |
| 70 |
size_t salt_len; |
| 71 |
uint8_t *incorrect_hash_verifier; |
| 72 |
uint8_t *correct_hash_verifier; |
| 73 |
size_t verifier_len; |
| 74 |
struct librist_crypto_srp_authenticator_ctx *wrong_hash_authenticator; |
| 75 |
struct librist_crypto_srp_authenticator_ctx *correct_hash_authenticator; |
| 76 |
struct librist_crypto_srp_client_ctx *wrong_hash_client; |
| 77 |
struct librist_crypto_srp_client_ctx *correct_hash_client; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static const char SRP_TEST_N_2048[] = |
| 87 |
"AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4" |
| 88 |
"A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF60" |
| 89 |
"95179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF" |
| 90 |
"747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B907" |
| 91 |
"8717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB37861" |
| 92 |
"60279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DB" |
| 93 |
"FBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73"; |
| 94 |
static const char SRP_TEST_G_2048[] = "2"; |
| 95 |
|
| 96 |
static int srp_test_state_setup(void **state) { |
| 97 |
*state = calloc(sizeof(struct srp_test_state), 1); |
| 98 |
struct srp_test_state *s = *state; |
| 99 |
s->n = SRP_TEST_N_2048; |
| 100 |
s->g = SRP_TEST_G_2048; |
| 101 |
#if HAVE_MBEDTLS |
| 102 |
librist_crypto_srp_create_verifier(s->n, s->g, "rist", "mainprofile", &s->salt, &s->salt_len, &s->incorrect_hash_verifier, &s->verifier_len, false); |
| 103 |
s->wrong_hash_authenticator = librist_crypto_srp_authenticator_ctx_create(s->n, s->g, s->incorrect_hash_verifier, s->verifier_len, s->salt, s->salt_len, false, false); |
| 104 |
size_t v_len = s->verifier_len; |
| 105 |
free(s->salt); |
| 106 |
s->salt = NULL; |
| 107 |
#endif |
| 108 |
librist_crypto_srp_create_verifier(s->n, s->g, "rist", "mainprofile", &s->salt, &s->salt_len, &s->correct_hash_verifier, &s->verifier_len, true); |
| 109 |
#if HAVE_MBEDTLS |
| 110 |
assert(v_len == s->verifier_len); |
| 111 |
#endif |
| 112 |
s->correct_hash_authenticator = librist_crypto_srp_authenticator_ctx_create(s->n, s->g, s->correct_hash_verifier, s->verifier_len, s->salt, s->salt_len, true, false); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#if HAVE_MBEDTLS |
| 118 |
s->wrong_hash_client = librist_crypto_srp_client_ctx_create(true, NULL, 0, NULL, 0, s->salt, s->salt_len, false, false); |
| 119 |
#endif |
| 120 |
s->correct_hash_client = librist_crypto_srp_client_ctx_create(true, NULL, 0, NULL, 0, s->salt, s->salt_len, true, false); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
static int srp_test_state_teardown(void **state) { |
| 125 |
struct srp_test_state *s = *state; |
| 126 |
free(s->salt); |
| 127 |
free(s->incorrect_hash_verifier); |
| 128 |
free(s->correct_hash_verifier); |
| 129 |
librist_crypto_srp_authenticator_ctx_free(s->wrong_hash_authenticator); |
| 130 |
librist_crypto_srp_authenticator_ctx_free(s->correct_hash_authenticator); |
| 131 |
librist_crypto_srp_client_ctx_free(s->wrong_hash_client); |
| 132 |
librist_crypto_srp_client_ctx_free(s->correct_hash_client); |
| 133 |
free(s); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static void test_hash_func(void **state) { |
| 138 |
(void)(state); |
| 139 |
|
| 140 |
const char expected_hash[] = "8427F6E0E69DC9B99DFE1052DDAF7E50D4FEA316C63C6AD23FE197C9C1DA2AF1"; |
| 141 |
const char test_string[] = "rist:mainprofile"; |
| 142 |
uint8_t hash_data[SHA256_DIGEST_LENGTH]; |
| 143 |
assert_int_equal(librist_crypto_srp_hash((const uint8_t *)test_string, sizeof(test_string) -1, hash_data), 0); |
| 144 |
char outhash[sizeof(expected_hash)]; |
| 145 |
uint_to_hex(hash_data, sizeof(hash_data), outhash); |
| 146 |
assert_string_equal(outhash, expected_hash); |
| 147 |
} |
| 148 |
|
| 149 |
static void test_hash_update_func(void **state) { |
| 150 |
(void)(state); |
| 151 |
const char expected_hash[] = "8427F6E0E69DC9B99DFE1052DDAF7E50D4FEA316C63C6AD23FE197C9C1DA2AF1"; |
| 152 |
HASH_CONTEXT ctx; |
| 153 |
HASH_CONTEXT_INIT(&ctx, true); |
| 154 |
assert_int_equal(librist_crypto_srp_hash_update(&ctx, "rist", strlen("rist")), 0); |
| 155 |
assert_int_equal(librist_crypto_srp_hash_update(&ctx, ":", 1), 0); |
| 156 |
assert_int_equal(librist_crypto_srp_hash_update(&ctx, "mainprofile", strlen("mainprofile")), 0); |
| 157 |
uint8_t hash_data[SHA256_DIGEST_LENGTH]; |
| 158 |
assert_int_equal(librist_crypto_srp_hash_final(&ctx, hash_data), 0); |
| 159 |
char outhash[sizeof(expected_hash)]; |
| 160 |
uint_to_hex(hash_data, sizeof(hash_data), outhash); |
| 161 |
assert_string_equal(outhash, expected_hash); |
| 162 |
} |
| 163 |
|
| 164 |
static void test_get_default_ng(void **state) { |
| 165 |
(void)(state); |
| 166 |
const char *n = NULL; |
| 167 |
const char *g = NULL; |
| 168 |
assert_int_equal(librist_get_ng_constants(LIBRIST_SRP_NG_DEFAULT, &n, &g), 0); |
| 169 |
assert_string_equal(n, |
| 170 |
"AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4" |
| 171 |
"A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF60" |
| 172 |
"95179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF" |
| 173 |
"747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B907" |
| 174 |
"8717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB37861" |
| 175 |
"60279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DB" |
| 176 |
"FBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73" |
| 177 |
); |
| 178 |
assert_string_equal(g, "2"); |
| 179 |
} |
| 180 |
|
| 181 |
#if HAVE_MBEDTLS |
| 182 |
static void test_srp_wrong_hashing_verifier_create(void **state) { |
| 183 |
struct srp_test_state *s = *state; |
| 184 |
const char sample_salt[] = "72F9D5383B7EB7599FB63028F47475B60A55F313D40E0BE023E026C97C0A2C32"; |
| 185 |
const char sample_verifier[] = "8A5F1D5A055C15889711054E7E3BA993D5A28773A345751BA24236AB35355B3536064A4D54D67FD41044CE543FCFD9DB99378ABD8AC5FA949BD2FC093A93E62CC501F5B70359254C9E895048CC884DF8FEA57233D985FD613EF5CA6D821B37D12C4836F4736EBBEDCFA7C944A906721355CB7ADE19756CF487807A9EDB527B78D4097D4B84EFF2F1CFE978B80F464A0002D6AE8058E37556E81154F889AE15EE1698E9F8F60281B3D1BBEDDD2EB28F5F83B9F88142E8204675F3D1F81E3FFD3149880C38299F975A757594162F70465E6BD0868A6C578319BA1262C2789B0E8CD13CF36AFC4332566EB4BCDC1FE1681B21B66D55879004DFC510FC4B33EEBB08"; |
| 186 |
|
| 187 |
uint8_t *salt = NULL; |
| 188 |
size_t salt_len = 0; |
| 189 |
uint8_t *verifier = NULL; |
| 190 |
size_t verifier_len = 0; |
| 191 |
assert_int_equal(librist_crypto_srp_create_verifier(s->n, s->g, "rist", "mainprofile", &salt, &salt_len, &verifier, &verifier_len, false), 0); |
| 192 |
|
| 193 |
assert_int_equal(verifier_len, (sizeof(sample_verifier) -1)/2); |
| 194 |
assert_int_equal(salt_len, (sizeof(sample_salt) -1)/2); |
| 195 |
|
| 196 |
char salt_hex[sizeof(sample_salt)]; |
| 197 |
uint_to_hex(salt, salt_len, salt_hex); |
| 198 |
assert_string_equal(salt_hex, sample_salt); |
| 199 |
|
| 200 |
char verifier_hex[sizeof(sample_verifier)]; |
| 201 |
uint_to_hex(verifier, verifier_len, verifier_hex); |
| 202 |
assert_string_equal(sample_verifier, verifier_hex); |
| 203 |
free(verifier); |
| 204 |
free(salt); |
| 205 |
} |
| 206 |
|
| 207 |
static void test_srp_wrong_hashing_auth_ctx_create(void **state) { |
| 208 |
struct srp_test_state *s = *state; |
| 209 |
struct librist_crypto_srp_authenticator_ctx * ctx = librist_crypto_srp_authenticator_ctx_create(s->n, s->g, s->incorrect_hash_verifier, s->verifier_len, s->salt, s->salt_len, false, false); |
| 210 |
assert_true(ctx != NULL); |
| 211 |
|
| 212 |
const char well_known_n[] = "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73"; |
| 213 |
uint8_t n[(sizeof(well_known_n) -1)/2]; |
| 214 |
uint8_t g[1]; |
| 215 |
|
| 216 |
assert_int_equal(librist_crypto_srp_authenticator_write_n_bytes(ctx, n, sizeof(n)), sizeof(n)); |
| 217 |
assert_int_equal(librist_crypto_srp_authenticator_write_g_bytes(ctx, g, sizeof(g)), sizeof(g)); |
| 218 |
|
| 219 |
char n_hex[sizeof(well_known_n)]; |
| 220 |
char g_hex[sizeof("02")]; |
| 221 |
|
| 222 |
uint_to_hex(n, sizeof(n), n_hex); |
| 223 |
assert_string_equal(n_hex, well_known_n); |
| 224 |
|
| 225 |
uint_to_hex(g, sizeof(g), g_hex); |
| 226 |
assert_string_equal(g_hex, "02"); |
| 227 |
|
| 228 |
librist_crypto_srp_authenticator_ctx_free(ctx); |
| 229 |
} |
| 230 |
|
| 231 |
static void test_srp_wrong_hashing_auth_handle_A(void **state) { |
| 232 |
struct srp_test_state *s = *state; |
| 233 |
const char client_A_hex[] = "545DD89CD403BA71172016F156A537A2D369B8551004AB521CC62D76B71BD278E687294A3D265B96393A582D8823E4BB3A7960F641D7A01DD7E13C982F06B0522EC147B1451C63F099FD08A9D5A6FD5CA73907B13E0672DEFAEF976BEA78E8F4C3E60E85B86FE68F84658D3A792D90F2FB834E657C5F1E6AAA532A3D3F4F2D747D8F3D0C0CC8F999773ED4FFE159A8B8ACB2761C6C523C68BC866EE464091B6F86720EFFB02824AC1FB31675B7F07DD2292B937C9EDE73C2420A3204CA0BBD519274B5D35771019265BE5E213C9634540A0D56EA94BA306AD1965EFF986AF8963ECE5E30E057517A0D0082205E1086520039A03D60D739FCD7BB335CBB3AF39A"; |
| 234 |
uint8_t client_A[(sizeof(client_A_hex) -1)/2]; |
| 235 |
hexstr_to_uint(client_A_hex, client_A, sizeof(client_A)); |
| 236 |
assert_int_equal(librist_crypto_srp_authenticator_handle_A(s->wrong_hash_authenticator, client_A, sizeof(client_A)), 0); |
| 237 |
|
| 238 |
const char expected_B[] = "0C455F4A7D081DF6E3AFA0E41D121D6BAE8A28C40ABFB7898E24417C18EE6279D3A8D87A64B0B05D5B9EBB304BDEA075478052F9ED69F49AF56E5F7B321DA414289FB66B62CF7D109386AFB19B1B80C49A63B14B7F8E5649D3E6AA5C09C91BAAD31A049A719975D40F05C6B11A8E64E6CA4C0FE9D8CF45079117C78649AB058CFC36746051E6792FF143851DF3F370B56F3F3630E47B39AE42987B294762214BE29B90B9E9B980CFB5C86151DE43D1D27ECE270CE2D9FD8AB688F50311D2A786D4E339BDFE12DB544A1F782CF2ECB1B3236BDFC40152CEAB9E54851CBEDB7B1E21F068D7D9627C2A94EF91F255269993F194FC1FB07C1E466AE852B18C7A5FED"; |
| 239 |
|
| 240 |
uint8_t B[(sizeof(expected_B) -1)/2]; |
| 241 |
assert_int_equal(librist_crypto_srp_authenticator_write_B_bytes(s->wrong_hash_authenticator, B, sizeof(B)), sizeof(B)); |
| 242 |
|
| 243 |
char B_hex[sizeof(expected_B)]; |
| 244 |
uint_to_hex(B, sizeof(B), B_hex); |
| 245 |
assert_string_equal(expected_B, B_hex); |
| 246 |
} |
| 247 |
|
| 248 |
static void test_srp_wrong_hashing_auth_verify_M1(void **state) { |
| 249 |
struct srp_test_state *s = *state; |
| 250 |
const char client_M1_hex[] = "75C3E739333518C4F8C6636D1F3D54663C4CBDB6FEDC49CD82354BC145C79040"; |
| 251 |
uint8_t client_M1[(sizeof(client_M1_hex) -1)/2]; |
| 252 |
hexstr_to_uint(client_M1_hex, client_M1, sizeof(client_M1)); |
| 253 |
|
| 254 |
assert_int_equal(librist_crypto_srp_authenticator_verify_m1(s->wrong_hash_authenticator, "rist", client_M1), 0); |
| 255 |
|
| 256 |
const char expected_m2[] = "D20842186401BE2179E01C49130B6113CD0A89E28EE4B363529C60477E32AA4A"; |
| 257 |
char m2_hex[sizeof(expected_m2)]; |
| 258 |
uint8_t m2[SHA256_DIGEST_LENGTH]; |
| 259 |
|
| 260 |
librist_crypto_srp_authenticator_write_M2_bytes(s->wrong_hash_authenticator, m2); |
| 261 |
|
| 262 |
uint_to_hex(m2, sizeof(m2), m2_hex); |
| 263 |
|
| 264 |
assert_string_equal(m2_hex, expected_m2); |
| 265 |
} |
| 266 |
#endif |
| 267 |
|
| 268 |
|
| 269 |
static void test_srp_client_ctx_create(void **state) { |
| 270 |
(void)(state); |
| 271 |
const char salt_hex[] = "72F9D5383B7EB7599FB63028F47475B60A55F313D40E0BE023E026C97C0A2C32"; |
| 272 |
|
| 273 |
uint8_t salt[(sizeof(salt_hex) -1)/2]; |
| 274 |
hexstr_to_uint(salt_hex, salt, sizeof(salt)); |
| 275 |
|
| 276 |
|
| 277 |
struct librist_crypto_srp_client_ctx *ctx = librist_crypto_srp_client_ctx_create(true, NULL, 0, NULL, 0, salt, sizeof(salt), true, false); |
| 278 |
assert_true(ctx != NULL); |
| 279 |
|
| 280 |
|
| 281 |
*/ |
| 282 |
uint8_t A[256]; |
| 283 |
assert_int_equal(librist_crypto_srp_client_write_A_bytes(ctx, A, sizeof(A)), (ssize_t)sizeof(A)); |
| 284 |
librist_crypto_srp_client_ctx_free(ctx); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
uint8_t N[256]; |
| 289 |
hexstr_to_uint(SRP_TEST_N_2048, N, sizeof(N)); |
| 290 |
uint8_t g[1] = {0x02}; |
| 291 |
ctx = librist_crypto_srp_client_ctx_create(false, N, sizeof(N), g, sizeof(g), salt, sizeof(salt), true, false); |
| 292 |
assert_true(ctx != NULL); |
| 293 |
assert_int_equal(librist_crypto_srp_client_write_A_bytes(ctx, A, sizeof(A)), (ssize_t)sizeof(A)); |
| 294 |
librist_crypto_srp_client_ctx_free(ctx); |
| 295 |
} |
| 296 |
|
| 297 |
#if HAVE_MBEDTLS |
| 298 |
|
| 299 |
static void test_srp_wrong_hashing_client_handle_B(void **state) { |
| 300 |
struct srp_test_state *ctx = *state; |
| 301 |
const char server_B[] = "0C455F4A7D081DF6E3AFA0E41D121D6BAE8A28C40ABFB7898E24417C18EE6279D3A8D87A64B0B05D5B9EBB304BDEA075478052F9ED69F49AF56E5F7B321DA414289FB66B62CF7D109386AFB19B1B80C49A63B14B7F8E5649D3E6AA5C09C91BAAD31A049A719975D40F05C6B11A8E64E6CA4C0FE9D8CF45079117C78649AB058CFC36746051E6792FF143851DF3F370B56F3F3630E47B39AE42987B294762214BE29B90B9E9B980CFB5C86151DE43D1D27ECE270CE2D9FD8AB688F50311D2A786D4E339BDFE12DB544A1F782CF2ECB1B3236BDFC40152CEAB9E54851CBEDB7B1E21F068D7D9627C2A94EF91F255269993F194FC1FB07C1E466AE852B18C7A5FED"; |
| 302 |
uint8_t B[(sizeof(server_B) -1)/2]; |
| 303 |
hexstr_to_uint(server_B, B, sizeof(B)); |
| 304 |
|
| 305 |
assert_int_equal(librist_crypto_srp_client_handle_B(ctx->wrong_hash_client, B, sizeof(B), "rist", "mainprofile"), 0); |
| 306 |
|
| 307 |
const char expected_M1[] = "75C3E739333518C4F8C6636D1F3D54663C4CBDB6FEDC49CD82354BC145C79040"; |
| 308 |
uint8_t m1[SHA256_DIGEST_LENGTH]; |
| 309 |
|
| 310 |
librist_crypto_srp_client_write_M1_bytes(ctx->wrong_hash_client, m1); |
| 311 |
|
| 312 |
char m1_hex[sizeof(expected_M1)]; |
| 313 |
uint_to_hex(m1, sizeof(m1), m1_hex); |
| 314 |
assert_string_equal(m1_hex, expected_M1); |
| 315 |
} |
| 316 |
|
| 317 |
static void test_srp_wrong_hashing_client_verify_M2(void **state) { |
| 318 |
struct srp_test_state *ctx = *state; |
| 319 |
const char server_M2[] = "D20842186401BE2179E01C49130B6113CD0A89E28EE4B363529C60477E32AA4A"; |
| 320 |
uint8_t M2[(sizeof(server_M2)-1)/2]; |
| 321 |
hexstr_to_uint(server_M2, M2, sizeof(M2)); |
| 322 |
assert_int_equal(librist_crypto_srp_client_verify_m2(ctx->wrong_hash_client, M2), 0); |
| 323 |
} |
| 324 |
|
| 325 |
#endif |
| 326 |
|
| 327 |
static void test_srp_correct_hashing_verifier_create(void **state) { |
| 328 |
struct srp_test_state *s = *state; |
| 329 |
const char sample_salt[] = "72F9D5383B7EB7599FB63028F47475B60A55F313D40E0BE023E026C97C0A2C32"; |
| 330 |
const char sample_verifier[] = "16B380409C1D6A43A96B42DD0FAC130D54A1932205F51F26AC13FB5332331C7B66A313ED969E24CB2AC5447C04FFC6565BC9FEA75A79D865FF7BB0DD65C62065EAAE7A27048F3B4C1FC0502C622FFE5B196400AD9470DB9F9DFB55CC4710081FDAEE3B63B69C15D43E189EF3E6E1C1FB1A9268F8E6DCDF16E1726585B883960EE09B318D3DD9E1C93D1B3EC98C148C00927028C1ED14D342B72811B962C233B71096BDD2EE505539DDC04ED03FDAA69926417E86016406480F8EB41317FF3D5E3B4735C76BCE67333B1F1E5E6A467E7E45A70D66EE1FC474A179697C5690AC1A525D2ADD050CC9D9824232AEC6FD8206CBEA5144AA2AC31B9865CEACF3BA2A72"; |
| 331 |
|
| 332 |
uint8_t *salt = NULL; |
| 333 |
size_t salt_len = 0; |
| 334 |
uint8_t *verifier = NULL; |
| 335 |
size_t verifier_len = 0; |
| 336 |
assert_int_equal(librist_crypto_srp_create_verifier(s->n, s->g, "rist", "mainprofile", &salt, &salt_len, &verifier, &verifier_len, true), 0); |
| 337 |
|
| 338 |
assert_int_equal(verifier_len, (sizeof(sample_verifier) -1)/2); |
| 339 |
assert_int_equal(salt_len, (sizeof(sample_salt) -1)/2); |
| 340 |
|
| 341 |
char salt_hex[sizeof(sample_salt)]; |
| 342 |
uint_to_hex(salt, salt_len, salt_hex); |
| 343 |
assert_string_equal(salt_hex, sample_salt); |
| 344 |
|
| 345 |
char verifier_hex[sizeof(sample_verifier)]; |
| 346 |
uint_to_hex(verifier, verifier_len, verifier_hex); |
| 347 |
assert_string_equal(sample_verifier, verifier_hex); |
| 348 |
free(verifier); |
| 349 |
free(salt); |
| 350 |
} |
| 351 |
|
| 352 |
static void test_srp_correct_hashing_auth_ctx_create(void **state) { |
| 353 |
struct srp_test_state *s = *state; |
| 354 |
struct librist_crypto_srp_authenticator_ctx * ctx = librist_crypto_srp_authenticator_ctx_create(s->n, s->g, s->correct_hash_verifier, s->verifier_len, s->salt, s->salt_len, true, false); |
| 355 |
assert_true(ctx != NULL); |
| 356 |
|
| 357 |
const char well_known_n[] = "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73"; |
| 358 |
uint8_t n[(sizeof(well_known_n) -1)/2]; |
| 359 |
uint8_t g[1]; |
| 360 |
|
| 361 |
assert_int_equal(librist_crypto_srp_authenticator_write_n_bytes(ctx, n, sizeof(n)), sizeof(n)); |
| 362 |
assert_int_equal(librist_crypto_srp_authenticator_write_g_bytes(ctx, g, sizeof(g)), sizeof(g)); |
| 363 |
|
| 364 |
char n_hex[sizeof(well_known_n)]; |
| 365 |
char g_hex[sizeof("02")]; |
| 366 |
|
| 367 |
uint_to_hex(n, sizeof(n), n_hex); |
| 368 |
assert_string_equal(n_hex, well_known_n); |
| 369 |
|
| 370 |
uint_to_hex(g, sizeof(g), g_hex); |
| 371 |
assert_string_equal(g_hex, "02"); |
| 372 |
|
| 373 |
librist_crypto_srp_authenticator_ctx_free(ctx); |
| 374 |
} |
| 375 |
|
| 376 |
static void test_srp_correct_hashing_auth_handle_A(void **state) { |
| 377 |
struct srp_test_state *s = *state; |
| 378 |
const char client_A_hex[] = "545DD89CD403BA71172016F156A537A2D369B8551004AB521CC62D76B71BD278E687294A3D265B96393A582D8823E4BB3A7960F641D7A01DD7E13C982F06B0522EC147B1451C63F099FD08A9D5A6FD5CA73907B13E0672DEFAEF976BEA78E8F4C3E60E85B86FE68F84658D3A792D90F2FB834E657C5F1E6AAA532A3D3F4F2D747D8F3D0C0CC8F999773ED4FFE159A8B8ACB2761C6C523C68BC866EE464091B6F86720EFFB02824AC1FB31675B7F07DD2292B937C9EDE73C2420A3204CA0BBD519274B5D35771019265BE5E213C9634540A0D56EA94BA306AD1965EFF986AF8963ECE5E30E057517A0D0082205E1086520039A03D60D739FCD7BB335CBB3AF39A"; |
| 379 |
uint8_t client_A[(sizeof(client_A_hex) -1)/2]; |
| 380 |
hexstr_to_uint(client_A_hex, client_A, sizeof(client_A)); |
| 381 |
assert_int_equal(librist_crypto_srp_authenticator_handle_A(s->correct_hash_authenticator, client_A, sizeof(client_A)), 0); |
| 382 |
|
| 383 |
const char expected_B[] = "461F82DB9BBD64DD580800C38B854437F0AE29CA14B0AD4A03797CA4EB6A27CD3C1B90E06E1C539A5FFE61E905497E78E8433F5303BEC8ECB23008DA86EBFB1B1B2FED35129BBC2ED346A810CC2A0AB20E44E2B94E048C9F9A17ABD87651CD1F2642873E487E0DDB3987D68F1B831CA8598AB88B377FAA7B06DCFE0E83A6D97FFB50D429285518209A4AEFA66F5A2BA499918209362CF0907EDC9E265156FCB8A945027F4DCDE178B8169D796187B79AA133E3BE02AF81C6AEC0B675D5F9E25E78CE00D5A0FE3BADC7106A2DAFB078BF30EF8677DD4D1EE60B50B110446C576CDDA3FA930C837938FE4AC4CF2F28185A2DD87F9524F1D5746E93D9A8FFF53626"; |
| 384 |
|
| 385 |
uint8_t B[(sizeof(expected_B) -1)/2]; |
| 386 |
assert_int_equal(librist_crypto_srp_authenticator_write_B_bytes(s->correct_hash_authenticator, B, sizeof(B)), sizeof(B)); |
| 387 |
|
| 388 |
char B_hex[sizeof(expected_B)]; |
| 389 |
uint_to_hex(B, sizeof(B), B_hex); |
| 390 |
assert_string_equal(expected_B, B_hex); |
| 391 |
} |
| 392 |
|
| 393 |
static void test_srp_correct_hashing_auth_verify_M1(void **state) { |
| 394 |
struct srp_test_state *s = *state; |
| 395 |
const char client_M1_hex[] = "2EE41138D2C447E7469EB589B89CF96FAF869B55DD684897DAB173056F1D8F90"; |
| 396 |
uint8_t client_M1[(sizeof(client_M1_hex) -1)/2]; |
| 397 |
hexstr_to_uint(client_M1_hex, client_M1, sizeof(client_M1)); |
| 398 |
|
| 399 |
assert_int_equal(librist_crypto_srp_authenticator_verify_m1(s->correct_hash_authenticator, "rist", client_M1), 0); |
| 400 |
|
| 401 |
const char expected_m2[] = "28E0412112CD83DDC97B3395AB0D27F5C0A1EB4FA89205CD505957F53988A639"; |
| 402 |
char m2_hex[sizeof(expected_m2)]; |
| 403 |
uint8_t m2[SHA256_DIGEST_LENGTH]; |
| 404 |
|
| 405 |
librist_crypto_srp_authenticator_write_M2_bytes(s->correct_hash_authenticator, m2); |
| 406 |
|
| 407 |
uint_to_hex(m2, sizeof(m2), m2_hex); |
| 408 |
|
| 409 |
assert_string_equal(m2_hex, expected_m2); |
| 410 |
} |
| 411 |
|
| 412 |
static void test_srp_correct_hashing_client_handle_B(void **state) { |
| 413 |
struct srp_test_state *ctx = *state; |
| 414 |
const char server_B[] = "461F82DB9BBD64DD580800C38B854437F0AE29CA14B0AD4A03797CA4EB6A27CD3C1B90E06E1C539A5FFE61E905497E78E8433F5303BEC8ECB23008DA86EBFB1B1B2FED35129BBC2ED346A810CC2A0AB20E44E2B94E048C9F9A17ABD87651CD1F2642873E487E0DDB3987D68F1B831CA8598AB88B377FAA7B06DCFE0E83A6D97FFB50D429285518209A4AEFA66F5A2BA499918209362CF0907EDC9E265156FCB8A945027F4DCDE178B8169D796187B79AA133E3BE02AF81C6AEC0B675D5F9E25E78CE00D5A0FE3BADC7106A2DAFB078BF30EF8677DD4D1EE60B50B110446C576CDDA3FA930C837938FE4AC4CF2F28185A2DD87F9524F1D5746E93D9A8FFF53626"; |
| 415 |
uint8_t B[(sizeof(server_B) -1)/2]; |
| 416 |
hexstr_to_uint(server_B, B, sizeof(B)); |
| 417 |
|
| 418 |
assert_int_equal(librist_crypto_srp_client_handle_B(ctx->correct_hash_client, B, sizeof(B), "rist", "mainprofile"), 0); |
| 419 |
|
| 420 |
const char expected_M1[] = "2EE41138D2C447E7469EB589B89CF96FAF869B55DD684897DAB173056F1D8F90"; |
| 421 |
uint8_t m1[SHA256_DIGEST_LENGTH]; |
| 422 |
|
| 423 |
librist_crypto_srp_client_write_M1_bytes(ctx->correct_hash_client, m1); |
| 424 |
|
| 425 |
char m1_hex[sizeof(expected_M1)]; |
| 426 |
uint_to_hex(m1, sizeof(m1), m1_hex); |
| 427 |
assert_string_equal(m1_hex, expected_M1); |
| 428 |
} |
| 429 |
|
| 430 |
static void test_srp_correct_hashing_client_verify_M2(void **state) { |
| 431 |
struct srp_test_state *ctx = *state; |
| 432 |
const char server_M2[] = "28E0412112CD83DDC97B3395AB0D27F5C0A1EB4FA89205CD505957F53988A639"; |
| 433 |
uint8_t M2[(sizeof(server_M2)-1)/2]; |
| 434 |
hexstr_to_uint(server_M2, M2, sizeof(M2)); |
| 435 |
assert_int_equal(librist_crypto_srp_client_verify_m2(ctx->correct_hash_client, M2), 0); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
0) |
| 444 |
0) |
| 445 |
0) |
| 446 |
0) |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
re |
| 452 |
|
| 453 |
|
| 454 |
static int srp_run_exchange(bool auth_legacy, bool client_legacy) { |
| 455 |
const char *username = "rist"; |
| 456 |
const char *password = "mainprofile"; |
| 457 |
const char *n = NULL; |
| 458 |
const char *g = NULL; |
| 459 |
if (librist_get_ng_constants(LIBRIST_SRP_NG_DEFAULT, &n, &g) != 0) |
| 460 |
return -100; |
| 461 |
|
| 462 |
uint8_t *salt = NULL; |
| 463 |
size_t salt_len = 0; |
| 464 |
uint8_t *verifier = NULL; |
| 465 |
size_t verifier_len = 0; |
| 466 |
if (librist_crypto_srp_create_verifier(n, g, username, password, &salt, &salt_len, &verifier, &verifier_len, true) != 0) |
| 467 |
return -101; |
| 468 |
|
| 469 |
struct librist_crypto_srp_authenticator_ctx *auth = |
| 470 |
librist_crypto_srp_authenticator_ctx_create(n, g, verifier, verifier_len, salt, salt_len, true, auth_legacy); |
| 471 |
struct librist_crypto_srp_client_ctx *client = |
| 472 |
librist_crypto_srp_client_ctx_create(true, NULL, 0, NULL, 0, salt, salt_len, true, client_legacy); |
| 473 |
int ret = -102; |
| 474 |
if (auth == NULL || client == NULL) |
| 475 |
goto out; |
| 476 |
|
| 477 |
uint8_t A[256]; |
| 478 |
int alen = librist_crypto_srp_client_write_A_bytes(client, A, sizeof(A)); |
| 479 |
if (alen != (int)sizeof(A)) |
| 480 |
goto out; |
| 481 |
if (librist_crypto_srp_authenticator_handle_A(auth, A, sizeof(A)) != 0) |
| 482 |
goto out; |
| 483 |
|
| 484 |
uint8_t B[256]; |
| 485 |
if (librist_crypto_srp_authenticator_write_B_bytes(auth, B, sizeof(B)) != (int)sizeof(B)) |
| 486 |
goto out; |
| 487 |
if (librist_crypto_srp_client_handle_B(client, B, sizeof(B), username, password) != 0) |
| 488 |
goto out; |
| 489 |
|
| 490 |
uint8_t M1[SHA256_DIGEST_LENGTH]; |
| 491 |
librist_crypto_srp_client_write_M1_bytes(client, M1); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
ret = librist_crypto_srp_authenticator_verify_m1(auth, username, M1); |
| 496 |
|
| 497 |
out: |
| 498 |
librist_crypto_srp_authenticator_ctx_free(auth); |
| 499 |
librist_crypto_srp_client_ctx_free(client); |
| 500 |
free(verifier); |
| 501 |
free(salt); |
| 502 |
return ret; |
| 503 |
} |
| 504 |
|
| 505 |
static void test_srp_compat_both_pad(void **state) { |
| 506 |
(void)state; |
| 507 |
assert_int_equal(srp_run_exchange(false, false), 0); |
| 508 |
} |
| 509 |
static void test_srp_compat_both_legacy(void **state) { |
| 510 |
(void)state; |
| 511 |
assert_int_equal(srp_run_exchange(true, true), 0); |
| 512 |
} |
| 513 |
static void test_srp_compat_mismatch_auth_pad_client_legacy(void **state) { |
| 514 |
(void)state; |
| 515 |
assert_int_not_equal(srp_run_exchange(false, true), 0); |
| 516 |
} |
| 517 |
static void test_srp_compat_mismatch_auth_legacy_client_pad(void **state) { |
| 518 |
(void)state; |
| 519 |
assert_int_not_equal(srp_run_exchange(true, false), 0); |
| 520 |
} |
| 521 |
|
| 522 |
int main(void) { |
| 523 |
const struct CMUnitTest tests[] = { |
| 524 |
cmocka_unit_test(test_hash_func), |
| 525 |
cmocka_unit_test(test_hash_update_func), |
| 526 |
cmocka_unit_test(test_get_default_ng), |
| 527 |
#if HAVE_MBEDTLS |
| 528 |
cmocka_unit_test(test_srp_wrong_hashing_auth_handle_A), |
| 529 |
cmocka_unit_test(test_srp_wrong_hashing_auth_verify_M1), |
| 530 |
cmocka_unit_test(test_srp_wrong_hashing_verifier_create), |
| 531 |
cmocka_unit_test(test_srp_wrong_hashing_auth_ctx_create), |
| 532 |
#endif |
| 533 |
cmocka_unit_test(test_srp_correct_hashing_verifier_create), |
| 534 |
cmocka_unit_test(test_srp_correct_hashing_auth_ctx_create), |
| 535 |
cmocka_unit_test(test_srp_correct_hashing_auth_handle_A), |
| 536 |
cmocka_unit_test(test_srp_correct_hashing_auth_verify_M1), |
| 537 |
cmocka_unit_test(test_srp_client_ctx_create), |
| 538 |
#if HAVE_MBEDTLS |
| 539 |
cmocka_unit_test(test_srp_wrong_hashing_client_handle_B), |
| 540 |
cmocka_unit_test(test_srp_wrong_hashing_client_verify_M2), |
| 541 |
#endif |
| 542 |
cmocka_unit_test(test_srp_correct_hashing_client_handle_B), |
| 543 |
cmocka_unit_test(test_srp_correct_hashing_client_verify_M2), |
| 544 |
cmocka_unit_test(test_srp_compat_both_pad), |
| 545 |
cmocka_unit_test(test_srp_compat_both_legacy), |
| 546 |
cmocka_unit_test(test_srp_compat_mismatch_auth_pad_client_legacy), |
| 547 |
cmocka_unit_test(test_srp_compat_mismatch_auth_legacy_client_pad), |
| 548 |
}; |
| 549 |
|
| 550 |
return cmocka_run_group_tests(tests, srp_test_state_setup, srp_test_state_teardown); |
| 551 |
} |