wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers srp.h Source File

srp.h

00001 /* srp.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifdef WOLFCRYPT_HAVE_SRP
00024 
00025 #ifndef WOLFCRYPT_SRP_H
00026 #define WOLFCRYPT_SRP_H
00027 
00028 #include <wolfssl/wolfcrypt/types.h>
00029 #include <wolfssl/wolfcrypt/sha.h>
00030 #include <wolfssl/wolfcrypt/sha256.h>
00031 #include <wolfssl/wolfcrypt/sha512.h>
00032 #include <wolfssl/wolfcrypt/integer.h>
00033 
00034 #ifdef __cplusplus
00035     extern "C" {
00036 #endif
00037 
00038 /* Select the largest available hash for the buffer size. */
00039 #if defined(WOLFSSL_SHA512)
00040     #define SRP_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
00041 #elif defined(WOLFSSL_SHA384)
00042     #define SRP_MAX_DIGEST_SIZE SHA384_DIGEST_SIZE
00043 #elif !defined(NO_SHA256)
00044     #define SRP_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
00045 #elif !defined(NO_SHA)
00046     #define SRP_MAX_DIGEST_SIZE SHA_DIGEST_SIZE
00047 #else
00048     #error "You have to have some kind of SHA hash if you want to use SRP."
00049 #endif
00050 
00051 /* Set the minimum number of bits acceptable in an SRP modulus */
00052 #define SRP_MODULUS_MIN_BITS 512
00053 
00054 /* Set the minimum number of bits acceptable for private keys (RFC 5054) */
00055 #define SRP_PRIVATE_KEY_MIN_BITS 256
00056 
00057 /**
00058  * SRP side, client or server.
00059  */
00060 typedef enum {
00061     SRP_CLIENT_SIDE  = 0,
00062     SRP_SERVER_SIDE  = 1,
00063 } SrpSide;
00064 
00065 /**
00066  * SRP hash type, SHA[1|256|384|512].
00067  */
00068 typedef enum {
00069         SRP_TYPE_SHA    = 1,
00070         SRP_TYPE_SHA256 = 2,
00071         SRP_TYPE_SHA384 = 3,
00072         SRP_TYPE_SHA512 = 4,
00073 } SrpType;
00074 
00075 /**
00076  * SRP hash struct.
00077  */
00078 typedef struct {
00079     byte type;
00080     union {
00081         #ifndef NO_SHA
00082             Sha sha;
00083         #endif
00084         #ifndef NO_SHA256
00085             Sha256 sha256;
00086         #endif
00087         #ifdef WOLFSSL_SHA384
00088             Sha384 sha384;
00089         #endif
00090         #ifdef WOLFSSL_SHA512
00091             Sha512 sha512;
00092         #endif
00093     } data;
00094 } SrpHash;
00095 
00096 typedef struct Srp {
00097     SrpSide side;                   /**< Client or Server, @see SrpSide.      */
00098     SrpType type;                   /**< Hash type, @see SrpType.             */
00099     byte*   user;                   /**< Username, login.                     */
00100     word32  userSz;                 /**< Username length.                     */
00101     byte*   salt;                   /**< Small salt.                          */
00102     word32  saltSz;                 /**< Salt length.                         */
00103     mp_int  N;                      /**< Modulus. N = 2q+1, [q, N] are primes.*/
00104     mp_int  g;                      /**< Generator. A generator modulo N.     */
00105     byte    k[SRP_MAX_DIGEST_SIZE]; /**< Multiplier parameter. k = H(N, g)   */
00106     mp_int  auth;                   /**< Client: x = H(salt + H(user:pswd))   */
00107                                     /**< Server: v = g ^ x % N                */
00108     mp_int  priv;                   /**< Private ephemeral value.             */
00109     SrpHash client_proof;           /**< Client proof. Sent to the Server.    */
00110     SrpHash server_proof;           /**< Server proof. Sent to the Client.    */
00111     byte*   key;                    /**< Session key.                         */
00112     word32  keySz;                  /**< Session key length.                  */
00113     int (*keyGenFunc_cb) (struct Srp* srp, byte* secret, word32 size);
00114         /**< Function responsible for generating the session key.             */
00115         /**< It MUST use XMALLOC with type DYNAMIC_TYPE_SRP to allocate the   */
00116         /**< key buffer for this structure and set keySz to the buffer size.  */
00117         /**< The default function used by this implementation is a modified   */
00118         /**< version of t_mgf1 that uses the proper hash function according   */
00119         /**< to srp->type.                                                    */
00120     void*   heap;                   /**< heap hint pointer                    */
00121 } Srp;
00122 
00123 /**
00124  * Initializes the Srp struct for usage.
00125  *
00126  * @param[out] srp   the Srp structure to be initialized.
00127  * @param[in]  type  the hash type to be used.
00128  * @param[in]  side  the side of the communication.
00129  *
00130  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00131  */
00132 WOLFSSL_API int wc_SrpInit(Srp* srp, SrpType type, SrpSide side);
00133 
00134 /**
00135  * Releases the Srp struct resources after usage.
00136  *
00137  * @param[in,out] srp    the Srp structure to be terminated.
00138  */
00139 WOLFSSL_API void wc_SrpTerm(Srp* srp);
00140 
00141 /**
00142  * Sets the username.
00143  *
00144  * This function MUST be called after wc_SrpInit.
00145  *
00146  * @param[in,out] srp       the Srp structure.
00147  * @param[in]     username  the buffer containing the username.
00148  * @param[in]     size      the username size in bytes
00149  *
00150  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00151  */
00152 WOLFSSL_API int wc_SrpSetUsername(Srp* srp, const byte* username, word32 size);
00153 
00154 
00155 /**
00156  * Sets the srp parameters based on the username.
00157  *
00158  * This function MUST be called after wc_SrpSetUsername.
00159  *
00160  * @param[in,out] srp       the Srp structure.
00161  * @param[in]     N         the Modulus. N = 2q+1, [q, N] are primes.
00162  * @param[in]     nSz       the N size in bytes.
00163  * @param[in]     g         the Generator modulo N.
00164  * @param[in]     gSz       the g size in bytes
00165  * @param[in]     salt      a small random salt. Specific for each username.
00166  * @param[in]     saltSz    the salt size in bytes
00167  *
00168  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00169  */
00170 WOLFSSL_API int wc_SrpSetParams(Srp* srp, const byte* N,    word32 nSz,
00171                                           const byte* g,    word32 gSz,
00172                                           const byte* salt, word32 saltSz);
00173 
00174 /**
00175  * Sets the password.
00176  *
00177  * Setting the password does not persists the clear password data in the
00178  * srp structure. The client calculates x = H(salt + H(user:pswd)) and stores
00179  * it in the auth field.
00180  *
00181  * This function MUST be called after wc_SrpSetParams and is CLIENT SIDE ONLY.
00182  *
00183  * @param[in,out] srp       the Srp structure.
00184  * @param[in]     password  the buffer containing the password.
00185  * @param[in]     size      the password size in bytes.
00186  *
00187  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00188  */
00189 WOLFSSL_API int wc_SrpSetPassword(Srp* srp, const byte* password, word32 size);
00190 
00191 /**
00192  * Sets the verifier.
00193  *
00194  * This function MUST be called after wc_SrpSetParams and is SERVER SIDE ONLY.
00195  *
00196  * @param[in,out] srp       the Srp structure.
00197  * @param[in]     verifier  the buffer containing the verifier.
00198  * @param[in]     size      the verifier size in bytes.
00199  *
00200  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00201  */
00202 WOLFSSL_API int wc_SrpSetVerifier(Srp* srp, const byte* verifier, word32 size);
00203 
00204 /**
00205  * Gets the verifier.
00206  *
00207  * The client calculates the verifier with v = g ^ x % N.
00208  * This function MAY be called after wc_SrpSetPassword and is CLIENT SIDE ONLY.
00209  *
00210  * @param[in,out] srp       the Srp structure.
00211  * @param[out]    verifier  the buffer to write the verifier.
00212  * @param[in,out] size      the buffer size in bytes. Will be updated with the
00213  *                          verifier size.
00214  *
00215  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00216  */
00217 WOLFSSL_API int wc_SrpGetVerifier(Srp* srp, byte* verifier, word32* size);
00218 
00219 /**
00220  * Sets the private ephemeral value.
00221  *
00222  * The private ephemeral value is known as:
00223  *   a at the client side. a = random()
00224  *   b at the server side. b = random()
00225  * This function is handy for unit test cases or if the developer wants to use
00226  * an external random source to set the ephemeral value.
00227  * This function MAY be called before wc_SrpGetPublic.
00228  *
00229  * @param[in,out] srp       the Srp structure.
00230  * @param[in]     priv      the ephemeral value.
00231  * @param[in]     size      the private size in bytes.
00232  *
00233  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00234  */
00235 WOLFSSL_API int wc_SrpSetPrivate(Srp* srp, const byte* priv, word32 size);
00236 
00237 /**
00238  * Gets the public ephemeral value.
00239  *
00240  * The public ephemeral value is known as:
00241  *   A at the client side. A = g ^ a % N
00242  *   B at the server side. B = (k * v + (g ˆ b % N)) % N
00243  * This function MUST be called after wc_SrpSetPassword or wc_SrpSetVerifier.
00244  *
00245  * @param[in,out] srp       the Srp structure.
00246  * @param[out]    pub       the buffer to write the public ephemeral value.
00247  * @param[in,out] size      the the buffer size in bytes. Will be updated with
00248  *                          the ephemeral value size.
00249  *
00250  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00251  */
00252 WOLFSSL_API int wc_SrpGetPublic(Srp* srp, byte* pub, word32* size);
00253 
00254 
00255 /**
00256  * Computes the session key.
00257  *
00258  * The key can be accessed at srp->key after success.
00259  *
00260  * @param[in,out] srp               the Srp structure.
00261  * @param[in]     clientPubKey      the client's public ephemeral value.
00262  * @param[in]     clientPubKeySz    the client's public ephemeral value size.
00263  * @param[in]     serverPubKey      the server's public ephemeral value.
00264  * @param[in]     serverPubKeySz    the server's public ephemeral value size.
00265  *
00266  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00267  */
00268 WOLFSSL_API int wc_SrpComputeKey(Srp* srp,
00269                                  byte* clientPubKey, word32 clientPubKeySz,
00270                                  byte* serverPubKey, word32 serverPubKeySz);
00271 
00272 /**
00273  * Gets the proof.
00274  *
00275  * This function MUST be called after wc_SrpComputeKey.
00276  *
00277  * @param[in,out] srp   the Srp structure.
00278  * @param[out]    proof the buffer to write the proof.
00279  * @param[in,out] size  the buffer size in bytes. Will be updated with the
00280  *                          proof size.
00281  *
00282  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00283  */
00284 WOLFSSL_API int wc_SrpGetProof(Srp* srp, byte* proof, word32* size);
00285 
00286 /**
00287  * Verifies the peers proof.
00288  *
00289  * This function MUST be called before wc_SrpGetSessionKey.
00290  *
00291  * @param[in,out] srp   the Srp structure.
00292  * @param[in]     proof the peers proof.
00293  * @param[in]     size  the proof size in bytes.
00294  *
00295  * @return 0 on success, {@literal <} 0 on error. @see error-crypt.h
00296  */
00297 WOLFSSL_API int wc_SrpVerifyPeersProof(Srp* srp, byte* proof, word32 size);
00298 
00299 #ifdef __cplusplus
00300    } /* extern "C" */
00301 #endif
00302 
00303 #endif /* WOLFCRYPT_SRP_H */
00304 #endif /* WOLFCRYPT_HAVE_SRP */
00305