diff options
Diffstat (limited to '3rd_party/libsrp6a-sha512/srp.h')
| -rw-r--r-- | 3rd_party/libsrp6a-sha512/srp.h | 372 |
1 files changed, 372 insertions, 0 deletions
diff --git a/3rd_party/libsrp6a-sha512/srp.h b/3rd_party/libsrp6a-sha512/srp.h new file mode 100644 index 0000000..b1d46af --- /dev/null +++ b/3rd_party/libsrp6a-sha512/srp.h | |||
| @@ -0,0 +1,372 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997-2007 The Stanford SRP Authentication Project | ||
| 3 | * All Rights Reserved. | ||
| 4 | * | ||
| 5 | * Permission is hereby granted, free of charge, to any person obtaining | ||
| 6 | * a copy of this software and associated documentation files (the | ||
| 7 | * "Software"), to deal in the Software without restriction, including | ||
| 8 | * without limitation the rights to use, copy, modify, merge, publish, | ||
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to | ||
| 10 | * permit persons to whom the Software is furnished to do so, subject to | ||
| 11 | * the following conditions: | ||
| 12 | * | ||
| 13 | * The above copyright notice and this permission notice shall be | ||
| 14 | * included in all copies or substantial portions of the Software. | ||
| 15 | * | ||
| 16 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | ||
| 17 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | ||
| 18 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 19 | * | ||
| 20 | * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL, | ||
| 21 | * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER | ||
| 22 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF | ||
| 23 | * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT | ||
| 24 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 25 | * | ||
| 26 | * Redistributions in source or binary form must retain an intact copy | ||
| 27 | * of this copyright notice. | ||
| 28 | */ | ||
| 29 | #ifndef _SRP_H_ | ||
| 30 | #define _SRP_H_ | ||
| 31 | |||
| 32 | #include "cstr.h" | ||
| 33 | #include "srp_aux.h" | ||
| 34 | |||
| 35 | #ifdef __cplusplus | ||
| 36 | extern "C" { | ||
| 37 | #endif | ||
| 38 | |||
| 39 | /* SRP library version identification */ | ||
| 40 | #define SRP_VERSION_MAJOR 2 | ||
| 41 | #define SRP_VERSION_MINOR 0 | ||
| 42 | #define SRP_VERSION_PATCHLEVEL 1 | ||
| 43 | |||
| 44 | typedef int SRP_RESULT; | ||
| 45 | /* Returned codes for SRP API functions */ | ||
| 46 | #define SRP_OK(v) ((v) == SRP_SUCCESS) | ||
| 47 | #define SRP_SUCCESS 0 | ||
| 48 | #define SRP_ERROR -1 | ||
| 49 | |||
| 50 | /* Set the minimum number of bits acceptable in an SRP modulus */ | ||
| 51 | #define SRP_DEFAULT_MIN_BITS 512 | ||
| 52 | _TYPE( SRP_RESULT ) SRP_set_modulus_min_bits P((int minbits)); | ||
| 53 | _TYPE( int ) SRP_get_modulus_min_bits P((void)); | ||
| 54 | |||
| 55 | /* | ||
| 56 | * Sets the "secret size callback" function. | ||
| 57 | * This function is called with the modulus size in bits, | ||
| 58 | * and returns the size of the secret exponent in bits. | ||
| 59 | * The default function always returns 256 bits. | ||
| 60 | */ | ||
| 61 | typedef int (_CDECL * SRP_SECRET_BITS_CB)(int modsize); | ||
| 62 | _TYPE( SRP_RESULT ) SRP_set_secret_bits_cb P((SRP_SECRET_BITS_CB cb)); | ||
| 63 | _TYPE( int ) SRP_get_secret_bits P((int modsize)); | ||
| 64 | |||
| 65 | typedef struct srp_st SRP; | ||
| 66 | |||
| 67 | #if 0 | ||
| 68 | /* Server Lookup API */ | ||
| 69 | typedef struct srp_server_lu_st SRP_SERVER_LOOKUP; | ||
| 70 | |||
| 71 | typedef struct srp_s_lu_meth_st { | ||
| 72 | const char * name; | ||
| 73 | |||
| 74 | SRP_RESULT (_CDECL * init)(SRP_SERVER_LOOKUP * slu); | ||
| 75 | SRP_RESULT (_CDECL * finish)(SRP_SERVER_LOOKUP * slu); | ||
| 76 | |||
| 77 | SRP_RESULT (_CDECL * lookup)(SRP_SERVER_LOOKUP * slu, SRP * srp, cstr * username); | ||
| 78 | |||
| 79 | void * meth_data; | ||
| 80 | } SRP_SERVER_LOOKUP_METHOD; | ||
| 81 | |||
| 82 | struct srp_server_lu_st { | ||
| 83 | SRP_SERVER_LOOKUP_METHOD * meth; | ||
| 84 | void * data; | ||
| 85 | }; | ||
| 86 | |||
| 87 | /* | ||
| 88 | * The Server Lookup API deals with the server-side issue of | ||
| 89 | * mapping usernames to verifiers. Given a username, a lookup | ||
| 90 | * mechanism needs to provide parameters (N, g), salt (s), and | ||
| 91 | * password verifier (v) for that user. | ||
| 92 | * | ||
| 93 | * A SRP_SERVER_LOOKUP_METHOD describes the general mechanism | ||
| 94 | * for performing lookups (e.g. files, LDAP, database, etc.) | ||
| 95 | * A SRP_SERVER_LOOKUP is an active "object" that is actually | ||
| 96 | * called to do lookups. | ||
| 97 | */ | ||
| 98 | _TYPE( SRP_SERVER_LOOKUP * ) | ||
| 99 | SRP_SERVER_LOOKUP_new P((SRP_SERVER_LOOKUP_METHOD * meth)); | ||
| 100 | _TYPE( SRP_RESULT ) SRP_SERVER_LOOKUP_free P((SRP_SERVER_LOOKUP * slu)); | ||
| 101 | _TYPE( SRP_RESULT ) SRP_SERVER_do_lookup P((SRP_SERVER_LOOKUP * slu, | ||
| 102 | SRP * srp, cstr * username)); | ||
| 103 | |||
| 104 | /* | ||
| 105 | * SRP_SERVER_system_lookup supercedes SRP_server_init_user. | ||
| 106 | */ | ||
| 107 | _TYPE( SRP_SERVER_LOOKUP * ) SRP_SERVER_system_lookup P((void)); | ||
| 108 | #endif | ||
| 109 | |||
| 110 | /* | ||
| 111 | * Client Parameter Verification API | ||
| 112 | * | ||
| 113 | * This callback is called from the SRP client when the | ||
| 114 | * parameters (modulus and generator) are set. The callback | ||
| 115 | * should return SRP_SUCCESS if the parameters are okay, | ||
| 116 | * otherwise some error code to indicate that the parameters | ||
| 117 | * should be rejected. | ||
| 118 | */ | ||
| 119 | typedef SRP_RESULT (_CDECL * SRP_CLIENT_PARAM_VERIFY_CB)(SRP * srp, const unsigned char * mod, int modlen, const unsigned char * gen, int genlen); | ||
| 120 | |||
| 121 | #if 0 | ||
| 122 | /* The default parameter verifier function */ | ||
| 123 | _TYPE( SRP_RESULT ) SRP_CLIENT_default_param_verify_cb(SRP * srp, const unsigned char * mod, int modlen, const unsigned char * gen, int genlen); | ||
| 124 | /* A parameter verifier that only accepts builtin params (no prime test) */ | ||
| 125 | _TYPE( SRP_RESULT ) SRP_CLIENT_builtin_param_verify_cb(SRP * srp, const unsigned char * mod, int modlen, const unsigned char * gen, int genlen); | ||
| 126 | /* The "classic" parameter verifier that accepts either builtin params | ||
| 127 | * immediately, and performs safe-prime tests on N and primitive-root | ||
| 128 | * tests on g otherwise. SECURITY WARNING: This may allow for certain | ||
| 129 | * attacks based on "trapdoor" moduli, so this is not recommended. */ | ||
| 130 | _TYPE( SRP_RESULT ) SRP_CLIENT_compat_param_verify_cb(SRP * srp, const unsigned char * mod, int modlen, const unsigned char * gen, int genlen); | ||
| 131 | |||
| 132 | #endif | ||
| 133 | |||
| 134 | /* | ||
| 135 | * Main SRP API - SRP and SRP_METHOD | ||
| 136 | */ | ||
| 137 | |||
| 138 | /* SRP method definitions */ | ||
| 139 | typedef struct srp_meth_st { | ||
| 140 | const char * name; | ||
| 141 | |||
| 142 | SRP_RESULT (_CDECL * init)(SRP * srp); | ||
| 143 | SRP_RESULT (_CDECL * finish)(SRP * srp); | ||
| 144 | |||
| 145 | SRP_RESULT (_CDECL * params)(SRP * srp, | ||
| 146 | const unsigned char * modulus, int modlen, | ||
| 147 | const unsigned char * generator, int genlen, | ||
| 148 | const unsigned char * salt, int saltlen); | ||
| 149 | SRP_RESULT (_CDECL * auth)(SRP * srp, const unsigned char * a, int alen); | ||
| 150 | SRP_RESULT (_CDECL * passwd)(SRP * srp, | ||
| 151 | const unsigned char * pass, int passlen); | ||
| 152 | SRP_RESULT (_CDECL * genpub)(SRP * srp, cstr ** result); | ||
| 153 | SRP_RESULT (_CDECL * key)(SRP * srp, cstr ** result, | ||
| 154 | const unsigned char * pubkey, int pubkeylen); | ||
| 155 | SRP_RESULT (_CDECL * verify)(SRP * srp, | ||
| 156 | const unsigned char * proof, int prooflen); | ||
| 157 | SRP_RESULT (_CDECL * respond)(SRP * srp, cstr ** proof); | ||
| 158 | |||
| 159 | void * data; | ||
| 160 | } SRP_METHOD; | ||
| 161 | |||
| 162 | /* Magic numbers for the SRP context header */ | ||
| 163 | #define SRP_MAGIC_CLIENT 12 | ||
| 164 | #define SRP_MAGIC_SERVER 28 | ||
| 165 | |||
| 166 | /* Flag bits for SRP struct */ | ||
| 167 | #define SRP_FLAG_MOD_ACCEL 0x1 /* accelerate modexp operations */ | ||
| 168 | #define SRP_FLAG_LEFT_PAD 0x2 /* left-pad to length-of-N inside hashes */ | ||
| 169 | |||
| 170 | /* | ||
| 171 | * A hybrid structure that represents either client or server state. | ||
| 172 | */ | ||
| 173 | struct srp_st { | ||
| 174 | int magic; /* To distinguish client from server (and for sanity) */ | ||
| 175 | |||
| 176 | int flags; | ||
| 177 | |||
| 178 | cstr * username; | ||
| 179 | |||
| 180 | BigInteger modulus; | ||
| 181 | BigInteger generator; | ||
| 182 | cstr * salt; | ||
| 183 | |||
| 184 | BigInteger verifier; | ||
| 185 | BigInteger password; | ||
| 186 | |||
| 187 | BigInteger pubkey; | ||
| 188 | BigInteger secret; | ||
| 189 | BigInteger u; | ||
| 190 | |||
| 191 | BigInteger key; | ||
| 192 | |||
| 193 | cstr * ex_data; | ||
| 194 | |||
| 195 | SRP_METHOD * meth; | ||
| 196 | void * meth_data; | ||
| 197 | |||
| 198 | BigIntegerCtx bctx; /* to cache temporaries if available */ | ||
| 199 | BigIntegerModAccel accel; /* to accelerate modexp if available */ | ||
| 200 | |||
| 201 | SRP_CLIENT_PARAM_VERIFY_CB param_cb; /* to verify params */ | ||
| 202 | //SRP_SERVER_LOOKUP * slu; /* to look up users */ | ||
| 203 | }; | ||
| 204 | |||
| 205 | /* | ||
| 206 | * Global initialization/de-initialization functions. | ||
| 207 | * Call SRP_initialize_library before using the library, | ||
| 208 | * and SRP_finalize_library when done. | ||
| 209 | */ | ||
| 210 | _TYPE( SRP_RESULT ) SRP_initialize_library(); | ||
| 211 | _TYPE( SRP_RESULT ) SRP_finalize_library(); | ||
| 212 | |||
| 213 | /* | ||
| 214 | * SRP_new() creates a new SRP context object - | ||
| 215 | * the method determines which "sense" (client or server) | ||
| 216 | * the object operates in. SRP_free() frees it. | ||
| 217 | * (See RFC2945 method definitions below.) | ||
| 218 | */ | ||
| 219 | _TYPE( SRP * ) SRP_new P((SRP_METHOD * meth)); | ||
| 220 | _TYPE( SRP_RESULT ) SRP_free P((SRP * srp)); | ||
| 221 | |||
| 222 | #if 0 | ||
| 223 | /* | ||
| 224 | * Use the supplied lookup object to look up user parameters and | ||
| 225 | * password verifier. The lookup function gets called during | ||
| 226 | * SRP_set_username/SRP_set_user_raw below. Using this function | ||
| 227 | * means that the server can avoid calling SRP_set_params and | ||
| 228 | * SRP_set_authenticator, since the lookup function handles that | ||
| 229 | * internally. | ||
| 230 | */ | ||
| 231 | _TYPE( SRP_RESULT ) SRP_set_server_lookup P((SRP * srp, | ||
| 232 | SRP_SERVER_LOOKUP * lookup)); | ||
| 233 | #endif | ||
| 234 | |||
| 235 | /* | ||
| 236 | * Use the supplied callback function to verify parameters | ||
| 237 | * (modulus, generator) given to the client. | ||
| 238 | */ | ||
| 239 | _TYPE( SRP_RESULT ) | ||
| 240 | SRP_set_client_param_verify_cb P((SRP * srp, | ||
| 241 | SRP_CLIENT_PARAM_VERIFY_CB cb)); | ||
| 242 | |||
| 243 | /* | ||
| 244 | * Both client and server must call both SRP_set_username and | ||
| 245 | * SRP_set_params, in that order, before calling anything else. | ||
| 246 | * SRP_set_user_raw is an alternative to SRP_set_username that | ||
| 247 | * accepts an arbitrary length-bounded octet string as input. | ||
| 248 | */ | ||
| 249 | _TYPE( SRP_RESULT ) SRP_set_username P((SRP * srp, const char * username)); | ||
| 250 | _TYPE( SRP_RESULT ) SRP_set_user_raw P((SRP * srp, const unsigned char * user, | ||
| 251 | int userlen)); | ||
| 252 | _TYPE( SRP_RESULT ) | ||
| 253 | SRP_set_params P((SRP * srp, | ||
| 254 | const unsigned char * modulus, int modlen, | ||
| 255 | const unsigned char * generator, int genlen, | ||
| 256 | const unsigned char * salt, int saltlen)); | ||
| 257 | |||
| 258 | /* | ||
| 259 | * On the client, SRP_set_authenticator, SRP_gen_exp, and | ||
| 260 | * SRP_add_ex_data can be called in any order. | ||
| 261 | * On the server, SRP_set_authenticator must come first, | ||
| 262 | * followed by SRP_gen_exp and SRP_add_ex_data in either order. | ||
| 263 | */ | ||
| 264 | /* | ||
| 265 | * The authenticator is the secret possessed by either side. | ||
| 266 | * For the server, this is the bigendian verifier, as an octet string. | ||
| 267 | * For the client, this is the bigendian raw secret, as an octet string. | ||
| 268 | * The server's authenticator must be the generator raised to the power | ||
| 269 | * of the client's raw secret modulo the common modulus for authentication | ||
| 270 | * to succeed. | ||
| 271 | * | ||
| 272 | * SRP_set_auth_password computes the authenticator from a plaintext | ||
| 273 | * password and then calls SRP_set_authenticator automatically. This is | ||
| 274 | * usually used on the client side, while the server usually uses | ||
| 275 | * SRP_set_authenticator (since it doesn't know the plaintext password). | ||
| 276 | */ | ||
| 277 | _TYPE( SRP_RESULT ) | ||
| 278 | SRP_set_authenticator P((SRP * srp, const unsigned char * a, int alen)); | ||
| 279 | _TYPE( SRP_RESULT ) | ||
| 280 | SRP_set_auth_password P((SRP * srp, const char * password)); | ||
| 281 | _TYPE( SRP_RESULT ) | ||
| 282 | SRP_set_auth_password_raw P((SRP * srp, | ||
| 283 | const unsigned char * password, | ||
| 284 | int passlen)); | ||
| 285 | |||
| 286 | /* | ||
| 287 | * SRP_gen_pub generates the random exponential residue to send | ||
| 288 | * to the other side. If using SRP-3/RFC2945, the server must | ||
| 289 | * withhold its result until it receives the client's number. | ||
| 290 | * If using SRP-6, the server can send its value immediately | ||
| 291 | * without waiting for the client. | ||
| 292 | * | ||
| 293 | * If "result" points to a NULL pointer, a new cstr object will be | ||
| 294 | * created to hold the result, and "result" will point to it. | ||
| 295 | * If "result" points to a non-NULL cstr pointer, the result will be | ||
| 296 | * placed there. | ||
| 297 | * If "result" itself is NULL, no result will be returned, | ||
| 298 | * although the big integer value will still be available | ||
| 299 | * through srp->pubkey in the SRP struct. | ||
| 300 | */ | ||
| 301 | _TYPE( SRP_RESULT ) SRP_gen_pub P((SRP * srp, cstr ** result)); | ||
| 302 | /* | ||
| 303 | * Append the data to the extra data segment. Authentication will | ||
| 304 | * not succeed unless both sides add precisely the same data in | ||
| 305 | * the same order. | ||
| 306 | */ | ||
| 307 | _TYPE( SRP_RESULT ) SRP_add_ex_data P((SRP * srp, const unsigned char * data, | ||
| 308 | int datalen)); | ||
| 309 | |||
| 310 | /* | ||
| 311 | * SRP_compute_key must be called after the previous three methods. | ||
| 312 | */ | ||
| 313 | _TYPE( SRP_RESULT ) SRP_compute_key P((SRP * srp, cstr ** result, | ||
| 314 | const unsigned char * pubkey, | ||
| 315 | int pubkeylen)); | ||
| 316 | |||
| 317 | /* | ||
| 318 | * On the client, call SRP_respond first to get the response to send | ||
| 319 | * to the server, and call SRP_verify to verify the server's response. | ||
| 320 | * On the server, call SRP_verify first to verify the client's response, | ||
| 321 | * and call SRP_respond ONLY if verification succeeds. | ||
| 322 | * | ||
| 323 | * It is an error to call SRP_respond with a NULL pointer. | ||
| 324 | */ | ||
| 325 | _TYPE( SRP_RESULT ) SRP_verify P((SRP * srp, | ||
| 326 | const unsigned char * proof, int prooflen)); | ||
| 327 | _TYPE( SRP_RESULT ) SRP_respond P((SRP * srp, cstr ** response)); | ||
| 328 | |||
| 329 | /* RFC2945-style SRP authentication */ | ||
| 330 | |||
| 331 | #define RFC2945_KEY_LEN 40 /* length of session key (bytes) */ | ||
| 332 | #define RFC2945_RESP_LEN 20 /* length of proof hashes (bytes) */ | ||
| 333 | |||
| 334 | /* | ||
| 335 | * RFC2945-style SRP authentication methods. Use these like: | ||
| 336 | * SRP * srp = SRP_new(SRP_RFC2945_client_method()); | ||
| 337 | */ | ||
| 338 | _TYPE( SRP_METHOD * ) SRP_RFC2945_client_method P((void)); | ||
| 339 | _TYPE( SRP_METHOD * ) SRP_RFC2945_server_method P((void)); | ||
| 340 | |||
| 341 | /* | ||
| 342 | * SRP-6 and SRP-6a authentication methods. | ||
| 343 | * SRP-6a is recommended for better resistance to 2-for-1 attacks. | ||
| 344 | */ | ||
| 345 | _TYPE( SRP_METHOD * ) SRP6_client_method P((void)); | ||
| 346 | _TYPE( SRP_METHOD * ) SRP6_server_method P((void)); | ||
| 347 | _TYPE( SRP_METHOD * ) SRP6a_client_method P((void)); | ||
| 348 | _TYPE( SRP_METHOD * ) SRP6a_server_method P((void)); | ||
| 349 | |||
| 350 | _TYPE( SRP_METHOD * ) SRP6a_sha512_client_method P((void)); | ||
| 351 | |||
| 352 | /* | ||
| 353 | * Convenience function - SRP_server_init_user | ||
| 354 | * Looks up the username from the system EPS configuration and calls | ||
| 355 | * SRP_set_username, SRP_set_params, and SRP_set_authenticator to | ||
| 356 | * initialize server state for that user. | ||
| 357 | * | ||
| 358 | * This is deprecated in favor of SRP_SERVER_system_lookup() and | ||
| 359 | * the Server Lookup API. | ||
| 360 | */ | ||
| 361 | _TYPE( SRP_RESULT ) SRP_server_init_user P((SRP * srp, const char * username)); | ||
| 362 | |||
| 363 | /* | ||
| 364 | * Use the named engine for acceleration. | ||
| 365 | */ | ||
| 366 | _TYPE( SRP_RESULT ) SRP_use_engine P((const char * engine)); | ||
| 367 | |||
| 368 | #ifdef __cplusplus | ||
| 369 | } | ||
| 370 | #endif | ||
| 371 | |||
| 372 | #endif /* _SRP_H_ */ | ||
