srp.h
Go to the documentation of this file.
1 /* srp.h
2  *
3  * Copyright (C) 2006-2020 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
26 #ifdef WOLFCRYPT_HAVE_SRP
27 
28 #ifndef WOLFCRYPT_SRP_H
29 #define WOLFCRYPT_SRP_H
30 
32 #include <wolfssl/wolfcrypt/sha.h>
35 #include <wolfssl/wolfcrypt/integer.h>
36 
37 #ifdef __cplusplus
38  extern "C" {
39 #endif
40 
41 /* Select the largest available hash for the buffer size. */
42 #if defined(WOLFSSL_SHA512)
43  #define SRP_MAX_DIGEST_SIZE WC_SHA512_DIGEST_SIZE
44 #elif defined(WOLFSSL_SHA384)
45  #define SRP_MAX_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
46 #elif !defined(NO_SHA256)
47  #define SRP_MAX_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
48 #elif !defined(NO_SHA)
49  #define SRP_MAX_DIGEST_SIZE WC_SHA_DIGEST_SIZE
50 #else
51  #error "You have to have some kind of SHA hash if you want to use SRP."
52 #endif
53 
54 /* Set the minimum number of bits acceptable in an SRP modulus */
55 #define SRP_MODULUS_MIN_BITS 512
56 
57 /* Set the minimum number of bits acceptable for private keys (RFC 5054) */
58 #define SRP_PRIVATE_KEY_MIN_BITS 256
59 
60 /* salt size for SRP password */
61 #define SRP_SALT_SIZE 16
62 
66 typedef enum {
67  SRP_CLIENT_SIDE = 0,
68  SRP_SERVER_SIDE = 1,
69 } SrpSide;
70 
74 typedef enum {
75  SRP_TYPE_SHA = 1,
76  SRP_TYPE_SHA256 = 2,
77  SRP_TYPE_SHA384 = 3,
78  SRP_TYPE_SHA512 = 4,
79 } SrpType;
80 
81 
85 typedef struct {
86  byte type;
87  union {
88  #ifndef NO_SHA
89  wc_Sha sha;
90  #endif
91  #ifndef NO_SHA256
92  wc_Sha256 sha256;
93  #endif
94  #ifdef WOLFSSL_SHA384
95  wc_Sha384 sha384;
96  #endif
97  #ifdef WOLFSSL_SHA512
98  wc_Sha512 sha512;
99  #endif
100  } data;
101 } SrpHash;
102 
103 typedef struct Srp {
104  SrpSide side;
106  byte* user;
107  word32 userSz;
108  byte* salt;
109  word32 saltSz;
112  byte k[SRP_MAX_DIGEST_SIZE];
118  byte* key;
119  word32 keySz;
120  int (*keyGenFunc_cb) (struct Srp* srp, byte* secret, word32 size);
127  void* heap;
128 } Srp;
129 
139 WOLFSSL_API int wc_SrpInit(Srp* srp, SrpType type, SrpSide side);
140 
146 WOLFSSL_API void wc_SrpTerm(Srp* srp);
147 
159 WOLFSSL_API int wc_SrpSetUsername(Srp* srp, const byte* username, word32 size);
160 
161 
177 WOLFSSL_API int wc_SrpSetParams(Srp* srp, const byte* N, word32 nSz,
178  const byte* g, word32 gSz,
179  const byte* salt, word32 saltSz);
180 
196 WOLFSSL_API int wc_SrpSetPassword(Srp* srp, const byte* password, word32 size);
197 
209 WOLFSSL_API int wc_SrpSetVerifier(Srp* srp, const byte* verifier, word32 size);
210 
224 WOLFSSL_API int wc_SrpGetVerifier(Srp* srp, byte* verifier, word32* size);
225 
242 WOLFSSL_API int wc_SrpSetPrivate(Srp* srp, const byte* priv, word32 size);
243 
259 WOLFSSL_API int wc_SrpGetPublic(Srp* srp, byte* pub, word32* size);
260 
261 
275 WOLFSSL_API int wc_SrpComputeKey(Srp* srp,
276  byte* clientPubKey, word32 clientPubKeySz,
277  byte* serverPubKey, word32 serverPubKeySz);
278 
291 WOLFSSL_API int wc_SrpGetProof(Srp* srp, byte* proof, word32* size);
292 
304 WOLFSSL_API int wc_SrpVerifyPeersProof(Srp* srp, byte* proof, word32 size);
305 
306 #ifdef __cplusplus
307  } /* extern "C" */
308 #endif
309 
310 #endif /* WOLFCRYPT_SRP_H */
311 #endif /* WOLFCRYPT_HAVE_SRP */
mp_int N
Definition: srp.h:110
mp_int g
Definition: srp.h:111
byte k[SRP_MAX_DIGEST_SIZE]
Definition: srp.h:112
WOLFSSL_API void wc_SrpTerm(Srp *srp)
Releases the Srp struct resources after usage.
Definition: srp.c:267
Definition: sha256.h:132
mp_int priv
Definition: srp.h:115
byte * key
Definition: srp.h:118
int(* keyGenFunc_cb)(struct Srp *srp, byte *secret, word32 size)
Definition: srp.h:120
Definition: srp.h:103
WOLFSSL_API int wc_SrpGetVerifier(Srp *srp, byte *verifier, word32 *size)
Gets the verifier. The client calculates the verifier with v = g ^ x % N. This function MAY be called...
Definition: srp.c:424
word32 saltSz
Definition: srp.h:109
SrpHash client_proof
Definition: srp.h:116
WOLFSSL_API int wc_SrpSetPassword(Srp *srp, const byte *password, word32 size)
Sets the password. Setting the password does not persists the clear password data in the srp structur...
Definition: srp.c:388
word32 keySz
Definition: srp.h:119
WOLFSSL_API int wc_SrpInit(Srp *srp, SrpType type, SrpSide side)
Initializes the Srp struct for usage.
Definition: srp.c:191
SrpType type
Definition: srp.h:105
Definition: integer.h:200
word32 userSz
Definition: srp.h:107
WOLFSSL_API int wc_SrpSetUsername(Srp *srp, const byte *username, word32 size)
Sets the username. This function MUST be called after wc_SrpInit.
Definition: srp.c:289
SrpSide side
Definition: srp.h:104
SrpType
Definition: srp.h:74
Definition: sha512.h:116
WOLFSSL_API int wc_SrpSetPrivate(Srp *srp, const byte *priv, word32 size)
Sets the private ephemeral value. The private ephemeral value is known as: a at the client side...
Definition: srp.c:458
WOLFSSL_API int wc_SrpSetVerifier(Srp *srp, const byte *verifier, word32 size)
Sets the verifier. This function MUST be called after wc_SrpSetParams and is SERVER SIDE ONLY...
Definition: srp.c:450
byte * salt
Definition: srp.h:108
WOLFSSL_API int wc_SrpVerifyPeersProof(Srp *srp, byte *proof, word32 size)
Verifies the peers proof. This function MUST be called before wc_SrpGetSessionKey.
Definition: srp.c:730
byte * user
Definition: srp.h:106
WOLFSSL_API int wc_SrpGetPublic(Srp *srp, byte *pub, word32 *size)
Gets the public ephemeral value. The public ephemeral value is known as: A at the client side...
Definition: srp.c:494
void * heap
Definition: srp.h:127
Definition: wolfcaam_sha.h:68
WOLFSSL_API int wc_SrpGetProof(Srp *srp, byte *proof, word32 *size)
Gets the proof. This function MUST be called after wc_SrpComputeKey.
Definition: srp.c:704
WOLFSSL_API int wc_SrpComputeKey(Srp *srp, byte *clientPubKey, word32 clientPubKeySz, byte *serverPubKey, word32 serverPubKeySz)
Computes the session key. The key can be accessed at srp->key after success.
Definition: srp.c:590
Definition: srp.h:85
SrpHash server_proof
Definition: srp.h:117
WOLFSSL_API int wc_SrpSetParams(Srp *srp, const byte *N, word32 nSz, const byte *g, word32 gSz, const byte *salt, word32 saltSz)
Sets the srp parameters based on the username.. Must be called after wc_SrpSetUsername.
Definition: srp.c:304
mp_int auth
Definition: srp.h:113