wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /* srp.h
wolfSSL 7:481bce714567 2 *
wolfSSL 7:481bce714567 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 7:481bce714567 4 *
wolfSSL 7:481bce714567 5 * This file is part of wolfSSL.
wolfSSL 7:481bce714567 6 *
wolfSSL 7:481bce714567 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 7:481bce714567 8 * it under the terms of the GNU General Public License as published by
wolfSSL 7:481bce714567 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 7:481bce714567 10 * (at your option) any later version.
wolfSSL 7:481bce714567 11 *
wolfSSL 7:481bce714567 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 7:481bce714567 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 7:481bce714567 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 7:481bce714567 15 * GNU General Public License for more details.
wolfSSL 7:481bce714567 16 *
wolfSSL 7:481bce714567 17 * You should have received a copy of the GNU General Public License
wolfSSL 7:481bce714567 18 * along with this program; if not, write to the Free Software
wolfSSL 7:481bce714567 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 7:481bce714567 20 */
wolfSSL 7:481bce714567 21
wolfSSL 7:481bce714567 22
wolfSSL 7:481bce714567 23 #ifdef WOLFCRYPT_HAVE_SRP
wolfSSL 7:481bce714567 24
wolfSSL 7:481bce714567 25 #ifndef WOLFCRYPT_SRP_H
wolfSSL 7:481bce714567 26 #define WOLFCRYPT_SRP_H
wolfSSL 7:481bce714567 27
wolfSSL 7:481bce714567 28 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 7:481bce714567 29 #include <wolfssl/wolfcrypt/sha.h>
wolfSSL 7:481bce714567 30 #include <wolfssl/wolfcrypt/sha256.h>
wolfSSL 7:481bce714567 31 #include <wolfssl/wolfcrypt/sha512.h>
wolfSSL 7:481bce714567 32 #include <wolfssl/wolfcrypt/integer.h>
wolfSSL 7:481bce714567 33
wolfSSL 7:481bce714567 34 #ifdef __cplusplus
wolfSSL 7:481bce714567 35 extern "C" {
wolfSSL 7:481bce714567 36 #endif
wolfSSL 7:481bce714567 37
wolfSSL 7:481bce714567 38 /* Select the largest available hash for the buffer size. */
wolfSSL 7:481bce714567 39 #if defined(WOLFSSL_SHA512)
wolfSSL 7:481bce714567 40 #define SRP_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
wolfSSL 7:481bce714567 41 #elif defined(WOLFSSL_SHA384)
wolfSSL 7:481bce714567 42 #define SRP_MAX_DIGEST_SIZE SHA384_DIGEST_SIZE
wolfSSL 7:481bce714567 43 #elif !defined(NO_SHA256)
wolfSSL 7:481bce714567 44 #define SRP_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
wolfSSL 7:481bce714567 45 #elif !defined(NO_SHA)
wolfSSL 7:481bce714567 46 #define SRP_MAX_DIGEST_SIZE SHA_DIGEST_SIZE
wolfSSL 7:481bce714567 47 #else
wolfSSL 7:481bce714567 48 #error "You have to have some kind of SHA hash if you want to use SRP."
wolfSSL 7:481bce714567 49 #endif
wolfSSL 7:481bce714567 50
wolfSSL 7:481bce714567 51 /* Set the minimum number of bits acceptable in an SRP modulus */
wolfSSL 7:481bce714567 52 #define SRP_MODULUS_MIN_BITS 512
wolfSSL 7:481bce714567 53
wolfSSL 7:481bce714567 54 /* Set the minimum number of bits acceptable for private keys (RFC 5054) */
wolfSSL 7:481bce714567 55 #define SRP_PRIVATE_KEY_MIN_BITS 256
wolfSSL 7:481bce714567 56
wolfSSL 7:481bce714567 57 /**
wolfSSL 7:481bce714567 58 * SRP side, client or server.
wolfSSL 7:481bce714567 59 */
wolfSSL 7:481bce714567 60 typedef enum {
wolfSSL 7:481bce714567 61 SRP_CLIENT_SIDE = 0,
wolfSSL 7:481bce714567 62 SRP_SERVER_SIDE = 1,
wolfSSL 7:481bce714567 63 } SrpSide;
wolfSSL 7:481bce714567 64
wolfSSL 7:481bce714567 65 /**
wolfSSL 7:481bce714567 66 * SRP hash type, SHA[1|256|384|512].
wolfSSL 7:481bce714567 67 */
wolfSSL 7:481bce714567 68 typedef enum {
wolfSSL 7:481bce714567 69 SRP_TYPE_SHA = 1,
wolfSSL 7:481bce714567 70 SRP_TYPE_SHA256 = 2,
wolfSSL 7:481bce714567 71 SRP_TYPE_SHA384 = 3,
wolfSSL 7:481bce714567 72 SRP_TYPE_SHA512 = 4,
wolfSSL 7:481bce714567 73 } SrpType;
wolfSSL 7:481bce714567 74
wolfSSL 7:481bce714567 75 /**
wolfSSL 7:481bce714567 76 * SRP hash struct.
wolfSSL 7:481bce714567 77 */
wolfSSL 7:481bce714567 78 typedef struct {
wolfSSL 7:481bce714567 79 byte type;
wolfSSL 7:481bce714567 80 union {
wolfSSL 7:481bce714567 81 #ifndef NO_SHA
wolfSSL 7:481bce714567 82 Sha sha;
wolfSSL 7:481bce714567 83 #endif
wolfSSL 7:481bce714567 84 #ifndef NO_SHA256
wolfSSL 7:481bce714567 85 Sha256 sha256;
wolfSSL 7:481bce714567 86 #endif
wolfSSL 7:481bce714567 87 #ifdef WOLFSSL_SHA384
wolfSSL 7:481bce714567 88 Sha384 sha384;
wolfSSL 7:481bce714567 89 #endif
wolfSSL 7:481bce714567 90 #ifdef WOLFSSL_SHA512
wolfSSL 7:481bce714567 91 Sha512 sha512;
wolfSSL 7:481bce714567 92 #endif
wolfSSL 7:481bce714567 93 } data;
wolfSSL 7:481bce714567 94 } SrpHash;
wolfSSL 7:481bce714567 95
wolfSSL 7:481bce714567 96 typedef struct Srp {
wolfSSL 7:481bce714567 97 SrpSide side; /**< Client or Server, @see SrpSide. */
wolfSSL 7:481bce714567 98 SrpType type; /**< Hash type, @see SrpType. */
wolfSSL 7:481bce714567 99 byte* user; /**< Username, login. */
wolfSSL 7:481bce714567 100 word32 userSz; /**< Username length. */
wolfSSL 7:481bce714567 101 byte* salt; /**< Small salt. */
wolfSSL 7:481bce714567 102 word32 saltSz; /**< Salt length. */
wolfSSL 7:481bce714567 103 mp_int N; /**< Modulus. N = 2q+1, [q, N] are primes.*/
wolfSSL 7:481bce714567 104 mp_int g; /**< Generator. A generator modulo N. */
wolfSSL 7:481bce714567 105 byte k[SRP_MAX_DIGEST_SIZE]; /**< Multiplier parameter. k = H(N, g) */
wolfSSL 7:481bce714567 106 mp_int auth; /**< Client: x = H(salt + H(user:pswd)) */
wolfSSL 7:481bce714567 107 /**< Server: v = g ^ x % N */
wolfSSL 7:481bce714567 108 mp_int priv; /**< Private ephemeral value. */
wolfSSL 7:481bce714567 109 SrpHash client_proof; /**< Client proof. Sent to the Server. */
wolfSSL 7:481bce714567 110 SrpHash server_proof; /**< Server proof. Sent to the Client. */
wolfSSL 7:481bce714567 111 byte* key; /**< Session key. */
wolfSSL 7:481bce714567 112 word32 keySz; /**< Session key length. */
wolfSSL 7:481bce714567 113 int (*keyGenFunc_cb) (struct Srp* srp, byte* secret, word32 size);
wolfSSL 7:481bce714567 114 /**< Function responsible for generating the session key. */
wolfSSL 7:481bce714567 115 /**< It MUST use XMALLOC with type DYNAMIC_TYPE_SRP to allocate the */
wolfSSL 7:481bce714567 116 /**< key buffer for this structure and set keySz to the buffer size. */
wolfSSL 7:481bce714567 117 /**< The default function used by this implementation is a modified */
wolfSSL 7:481bce714567 118 /**< version of t_mgf1 that uses the proper hash function according */
wolfSSL 7:481bce714567 119 /**< to srp->type. */
wolfSSL 7:481bce714567 120 void* heap; /**< heap hint pointer */
wolfSSL 7:481bce714567 121 } Srp;
wolfSSL 7:481bce714567 122
wolfSSL 7:481bce714567 123 /**
wolfSSL 7:481bce714567 124 * Initializes the Srp struct for usage.
wolfSSL 7:481bce714567 125 *
wolfSSL 7:481bce714567 126 * @param[out] srp the Srp structure to be initialized.
wolfSSL 7:481bce714567 127 * @param[in] type the hash type to be used.
wolfSSL 7:481bce714567 128 * @param[in] side the side of the communication.
wolfSSL 7:481bce714567 129 *
wolfSSL 7:481bce714567 130 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 131 */
wolfSSL 7:481bce714567 132 WOLFSSL_API int wc_SrpInit(Srp* srp, SrpType type, SrpSide side);
wolfSSL 7:481bce714567 133
wolfSSL 7:481bce714567 134 /**
wolfSSL 7:481bce714567 135 * Releases the Srp struct resources after usage.
wolfSSL 7:481bce714567 136 *
wolfSSL 7:481bce714567 137 * @param[in,out] srp the Srp structure to be terminated.
wolfSSL 7:481bce714567 138 */
wolfSSL 7:481bce714567 139 WOLFSSL_API void wc_SrpTerm(Srp* srp);
wolfSSL 7:481bce714567 140
wolfSSL 7:481bce714567 141 /**
wolfSSL 7:481bce714567 142 * Sets the username.
wolfSSL 7:481bce714567 143 *
wolfSSL 7:481bce714567 144 * This function MUST be called after wc_SrpInit.
wolfSSL 7:481bce714567 145 *
wolfSSL 7:481bce714567 146 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 147 * @param[in] username the buffer containing the username.
wolfSSL 7:481bce714567 148 * @param[in] size the username size in bytes
wolfSSL 7:481bce714567 149 *
wolfSSL 7:481bce714567 150 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 151 */
wolfSSL 7:481bce714567 152 WOLFSSL_API int wc_SrpSetUsername(Srp* srp, const byte* username, word32 size);
wolfSSL 7:481bce714567 153
wolfSSL 7:481bce714567 154
wolfSSL 7:481bce714567 155 /**
wolfSSL 7:481bce714567 156 * Sets the srp parameters based on the username.
wolfSSL 7:481bce714567 157 *
wolfSSL 7:481bce714567 158 * This function MUST be called after wc_SrpSetUsername.
wolfSSL 7:481bce714567 159 *
wolfSSL 7:481bce714567 160 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 161 * @param[in] N the Modulus. N = 2q+1, [q, N] are primes.
wolfSSL 7:481bce714567 162 * @param[in] nSz the N size in bytes.
wolfSSL 7:481bce714567 163 * @param[in] g the Generator modulo N.
wolfSSL 7:481bce714567 164 * @param[in] gSz the g size in bytes
wolfSSL 7:481bce714567 165 * @param[in] salt a small random salt. Specific for each username.
wolfSSL 7:481bce714567 166 * @param[in] saltSz the salt size in bytes
wolfSSL 7:481bce714567 167 *
wolfSSL 7:481bce714567 168 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 169 */
wolfSSL 7:481bce714567 170 WOLFSSL_API int wc_SrpSetParams(Srp* srp, const byte* N, word32 nSz,
wolfSSL 7:481bce714567 171 const byte* g, word32 gSz,
wolfSSL 7:481bce714567 172 const byte* salt, word32 saltSz);
wolfSSL 7:481bce714567 173
wolfSSL 7:481bce714567 174 /**
wolfSSL 7:481bce714567 175 * Sets the password.
wolfSSL 7:481bce714567 176 *
wolfSSL 7:481bce714567 177 * Setting the password does not persists the clear password data in the
wolfSSL 7:481bce714567 178 * srp structure. The client calculates x = H(salt + H(user:pswd)) and stores
wolfSSL 7:481bce714567 179 * it in the auth field.
wolfSSL 7:481bce714567 180 *
wolfSSL 7:481bce714567 181 * This function MUST be called after wc_SrpSetParams and is CLIENT SIDE ONLY.
wolfSSL 7:481bce714567 182 *
wolfSSL 7:481bce714567 183 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 184 * @param[in] password the buffer containing the password.
wolfSSL 7:481bce714567 185 * @param[in] size the password size in bytes.
wolfSSL 7:481bce714567 186 *
wolfSSL 7:481bce714567 187 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 188 */
wolfSSL 7:481bce714567 189 WOLFSSL_API int wc_SrpSetPassword(Srp* srp, const byte* password, word32 size);
wolfSSL 7:481bce714567 190
wolfSSL 7:481bce714567 191 /**
wolfSSL 7:481bce714567 192 * Sets the verifier.
wolfSSL 7:481bce714567 193 *
wolfSSL 7:481bce714567 194 * This function MUST be called after wc_SrpSetParams and is SERVER SIDE ONLY.
wolfSSL 7:481bce714567 195 *
wolfSSL 7:481bce714567 196 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 197 * @param[in] verifier the buffer containing the verifier.
wolfSSL 7:481bce714567 198 * @param[in] size the verifier size in bytes.
wolfSSL 7:481bce714567 199 *
wolfSSL 7:481bce714567 200 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 201 */
wolfSSL 7:481bce714567 202 WOLFSSL_API int wc_SrpSetVerifier(Srp* srp, const byte* verifier, word32 size);
wolfSSL 7:481bce714567 203
wolfSSL 7:481bce714567 204 /**
wolfSSL 7:481bce714567 205 * Gets the verifier.
wolfSSL 7:481bce714567 206 *
wolfSSL 7:481bce714567 207 * The client calculates the verifier with v = g ^ x % N.
wolfSSL 7:481bce714567 208 * This function MAY be called after wc_SrpSetPassword and is CLIENT SIDE ONLY.
wolfSSL 7:481bce714567 209 *
wolfSSL 7:481bce714567 210 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 211 * @param[out] verifier the buffer to write the verifier.
wolfSSL 7:481bce714567 212 * @param[in,out] size the buffer size in bytes. Will be updated with the
wolfSSL 7:481bce714567 213 * verifier size.
wolfSSL 7:481bce714567 214 *
wolfSSL 7:481bce714567 215 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 216 */
wolfSSL 7:481bce714567 217 WOLFSSL_API int wc_SrpGetVerifier(Srp* srp, byte* verifier, word32* size);
wolfSSL 7:481bce714567 218
wolfSSL 7:481bce714567 219 /**
wolfSSL 7:481bce714567 220 * Sets the private ephemeral value.
wolfSSL 7:481bce714567 221 *
wolfSSL 7:481bce714567 222 * The private ephemeral value is known as:
wolfSSL 7:481bce714567 223 * a at the client side. a = random()
wolfSSL 7:481bce714567 224 * b at the server side. b = random()
wolfSSL 7:481bce714567 225 * This function is handy for unit test cases or if the developer wants to use
wolfSSL 7:481bce714567 226 * an external random source to set the ephemeral value.
wolfSSL 7:481bce714567 227 * This function MAY be called before wc_SrpGetPublic.
wolfSSL 7:481bce714567 228 *
wolfSSL 7:481bce714567 229 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 230 * @param[in] priv the ephemeral value.
wolfSSL 7:481bce714567 231 * @param[in] size the private size in bytes.
wolfSSL 7:481bce714567 232 *
wolfSSL 7:481bce714567 233 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 234 */
wolfSSL 7:481bce714567 235 WOLFSSL_API int wc_SrpSetPrivate(Srp* srp, const byte* priv, word32 size);
wolfSSL 7:481bce714567 236
wolfSSL 7:481bce714567 237 /**
wolfSSL 7:481bce714567 238 * Gets the public ephemeral value.
wolfSSL 7:481bce714567 239 *
wolfSSL 7:481bce714567 240 * The public ephemeral value is known as:
wolfSSL 7:481bce714567 241 * A at the client side. A = g ^ a % N
wolfSSL 7:481bce714567 242 * B at the server side. B = (k * v + (g ˆ b % N)) % N
wolfSSL 7:481bce714567 243 * This function MUST be called after wc_SrpSetPassword or wc_SrpSetVerifier.
wolfSSL 7:481bce714567 244 *
wolfSSL 7:481bce714567 245 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 246 * @param[out] pub the buffer to write the public ephemeral value.
wolfSSL 7:481bce714567 247 * @param[in,out] size the the buffer size in bytes. Will be updated with
wolfSSL 7:481bce714567 248 * the ephemeral value size.
wolfSSL 7:481bce714567 249 *
wolfSSL 7:481bce714567 250 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 251 */
wolfSSL 7:481bce714567 252 WOLFSSL_API int wc_SrpGetPublic(Srp* srp, byte* pub, word32* size);
wolfSSL 7:481bce714567 253
wolfSSL 7:481bce714567 254
wolfSSL 7:481bce714567 255 /**
wolfSSL 7:481bce714567 256 * Computes the session key.
wolfSSL 7:481bce714567 257 *
wolfSSL 7:481bce714567 258 * The key can be accessed at srp->key after success.
wolfSSL 7:481bce714567 259 *
wolfSSL 7:481bce714567 260 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 261 * @param[in] clientPubKey the client's public ephemeral value.
wolfSSL 7:481bce714567 262 * @param[in] clientPubKeySz the client's public ephemeral value size.
wolfSSL 7:481bce714567 263 * @param[in] serverPubKey the server's public ephemeral value.
wolfSSL 7:481bce714567 264 * @param[in] serverPubKeySz the server's public ephemeral value size.
wolfSSL 7:481bce714567 265 *
wolfSSL 7:481bce714567 266 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 267 */
wolfSSL 7:481bce714567 268 WOLFSSL_API int wc_SrpComputeKey(Srp* srp,
wolfSSL 7:481bce714567 269 byte* clientPubKey, word32 clientPubKeySz,
wolfSSL 7:481bce714567 270 byte* serverPubKey, word32 serverPubKeySz);
wolfSSL 7:481bce714567 271
wolfSSL 7:481bce714567 272 /**
wolfSSL 7:481bce714567 273 * Gets the proof.
wolfSSL 7:481bce714567 274 *
wolfSSL 7:481bce714567 275 * This function MUST be called after wc_SrpComputeKey.
wolfSSL 7:481bce714567 276 *
wolfSSL 7:481bce714567 277 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 278 * @param[out] proof the buffer to write the proof.
wolfSSL 7:481bce714567 279 * @param[in,out] size the buffer size in bytes. Will be updated with the
wolfSSL 7:481bce714567 280 * proof size.
wolfSSL 7:481bce714567 281 *
wolfSSL 7:481bce714567 282 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 283 */
wolfSSL 7:481bce714567 284 WOLFSSL_API int wc_SrpGetProof(Srp* srp, byte* proof, word32* size);
wolfSSL 7:481bce714567 285
wolfSSL 7:481bce714567 286 /**
wolfSSL 7:481bce714567 287 * Verifies the peers proof.
wolfSSL 7:481bce714567 288 *
wolfSSL 7:481bce714567 289 * This function MUST be called before wc_SrpGetSessionKey.
wolfSSL 7:481bce714567 290 *
wolfSSL 7:481bce714567 291 * @param[in,out] srp the Srp structure.
wolfSSL 7:481bce714567 292 * @param[in] proof the peers proof.
wolfSSL 7:481bce714567 293 * @param[in] size the proof size in bytes.
wolfSSL 7:481bce714567 294 *
wolfSSL 7:481bce714567 295 * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
wolfSSL 7:481bce714567 296 */
wolfSSL 7:481bce714567 297 WOLFSSL_API int wc_SrpVerifyPeersProof(Srp* srp, byte* proof, word32 size);
wolfSSL 7:481bce714567 298
wolfSSL 7:481bce714567 299 #ifdef __cplusplus
wolfSSL 7:481bce714567 300 } /* extern "C" */
wolfSSL 7:481bce714567 301 #endif
wolfSSL 7:481bce714567 302
wolfSSL 7:481bce714567 303 #endif /* WOLFCRYPT_SRP_H */
wolfSSL 7:481bce714567 304 #endif /* WOLFCRYPT_HAVE_SRP */
wolfSSL 7:481bce714567 305