39
39
40
40
#include "dauth_nonce.h"
41
41
42
- #define RAND_SECRET_LEN 32
43
42
/*
44
43
* Length of nonce string in bytes
45
44
*/
46
45
#define NONCE_LEN 44
47
46
48
47
_Static_assert ((NONCE_LEN * 6 ) % 8 == 0 , "NONCE_LEN should not be padded" );
49
- _Static_assert ((NONCE_LEN * 6 ) / 8 >= RAND_SECRET_LEN , "NONCE_LEN is too small" );
48
+ _Static_assert ((NONCE_LEN * 6 ) / 8 >= AUTH_SECRET_LEN , "NONCE_LEN is too small" );
50
49
51
50
struct nonce_context_priv {
52
51
struct nonce_context pub ;
@@ -65,10 +64,10 @@ struct nonce_payload {
65
64
uint64_t expires_usec :20 ;
66
65
} __attribute__((__packed__ ));
67
66
68
- _Static_assert (sizeof (struct nonce_payload ) <= RAND_SECRET_LEN / 2 ,
67
+ _Static_assert (sizeof (struct nonce_payload ) <= AUTH_SECRET_LEN / 2 ,
69
68
"struct nonce_payload is too big" );
70
- _Static_assert (RAND_SECRET_LEN % sizeof (uint64_t ) == 0 ,
71
- "RAND_SECRET_LEN is not multiple of sizeof(uint64_t)" );
69
+ _Static_assert (AUTH_SECRET_LEN % sizeof (uint64_t ) == 0 ,
70
+ "AUTH_SECRET_LEN is not multiple of sizeof(uint64_t)" );
72
71
73
72
static int Base64Encode (const str_const * message , char * b64buffer );
74
73
static int Base64Decode (const str_const * b64message , unsigned char * obuffer );
@@ -98,16 +97,16 @@ int calc_nonce(const struct nonce_context *pub, char* _nonce,
98
97
const struct nonce_params * npp )
99
98
{
100
99
struct nonce_context_priv * self = (typeof (self ))pub ;
101
- unsigned char ebin [RAND_SECRET_LEN + 1 ];
100
+ unsigned char ebin [AUTH_SECRET_LEN + 1 ];
102
101
int rc , elen ;
103
- unsigned char dbin [RAND_SECRET_LEN ], * bp ;
102
+ unsigned char dbin [AUTH_SECRET_LEN ], * bp ;
104
103
unsigned char * riv = dbin ;
105
104
106
- rc = RAND_bytes (riv , RAND_SECRET_LEN / 2 );
105
+ rc = RAND_bytes (riv , AUTH_SECRET_LEN / 2 );
107
106
if (rc != 1 )
108
107
return -1 ;
109
108
110
- bp = dbin + RAND_SECRET_LEN / 2 ;
109
+ bp = dbin + AUTH_SECRET_LEN / 2 ;
111
110
struct nonce_payload npl ;
112
111
memset (& npl , 0 , sizeof (npl ));
113
112
npl .expires_sec = npp -> expires .tv_sec ;
@@ -119,8 +118,8 @@ int calc_nonce(const struct nonce_context *pub, char* _nonce,
119
118
bp += sizeof (npl );
120
119
memset (bp , 0 , sizeof (dbin ) - (bp - dbin ));
121
120
122
- bp = dbin + RAND_SECRET_LEN / 2 ;
123
- xor_bufs (bp , bp , dbin , RAND_SECRET_LEN / 2 );
121
+ bp = dbin + AUTH_SECRET_LEN / 2 ;
122
+ xor_bufs (bp , bp , dbin , AUTH_SECRET_LEN / 2 );
124
123
125
124
elen = 0 ;
126
125
rc = EVP_EncryptUpdate (self -> ectx , ebin , & elen , dbin , sizeof (dbin ));
@@ -140,10 +139,10 @@ int decr_nonce(const struct nonce_context *pub, const str_const * _n,
140
139
struct nonce_params * npp )
141
140
{
142
141
struct nonce_context_priv * self = (typeof (self ))pub ;
143
- unsigned char bin [RAND_SECRET_LEN + 1 ];
142
+ unsigned char bin [AUTH_SECRET_LEN + 1 ];
144
143
const unsigned char * bp ;
145
144
unsigned char * wbp ;
146
- unsigned char dbin [RAND_SECRET_LEN ];
145
+ unsigned char dbin [AUTH_SECRET_LEN ];
147
146
int rc ;
148
147
149
148
if (_n -> len != NONCE_LEN )
@@ -153,12 +152,12 @@ int decr_nonce(const struct nonce_context *pub, const str_const * _n,
153
152
return -1 ;
154
153
int dlen = 0 ;
155
154
bp = (const unsigned char * )bin ;
156
- rc = EVP_DecryptUpdate (self -> dctx , dbin , & dlen , bp , RAND_SECRET_LEN );
155
+ rc = EVP_DecryptUpdate (self -> dctx , dbin , & dlen , bp , AUTH_SECRET_LEN );
157
156
if (rc != 1 || dlen != sizeof (dbin ))
158
157
return -1 ;
159
158
160
- wbp = dbin + RAND_SECRET_LEN / 2 ;
161
- xor_bufs (wbp , wbp , dbin , RAND_SECRET_LEN / 2 );
159
+ wbp = dbin + AUTH_SECRET_LEN / 2 ;
160
+ xor_bufs (wbp , wbp , dbin , AUTH_SECRET_LEN / 2 );
162
161
163
162
bp = (const unsigned char * )wbp ;
164
163
struct nonce_payload npl ;
@@ -208,21 +207,21 @@ int generate_random_secret(struct nonce_context *pub)
208
207
struct nonce_context_priv * self = (typeof (self ))pub ;
209
208
int rc ;
210
209
211
- self -> sec_rand = (char * )pkg_malloc (RAND_SECRET_LEN );
210
+ self -> sec_rand = (char * )pkg_malloc (AUTH_SECRET_LEN );
212
211
if (!self -> sec_rand ) {
213
212
LM_ERR ("no pkg memory left\n" );
214
213
goto e0 ;
215
214
}
216
215
217
216
/* the generator is seeded from the core */
218
- rc = RAND_bytes ((unsigned char * )self -> sec_rand , RAND_SECRET_LEN );
217
+ rc = RAND_bytes ((unsigned char * )self -> sec_rand , AUTH_SECRET_LEN );
219
218
if (rc != 1 ) {
220
219
LM_ERR ("RAND_bytes() failed, error = %lu\n" , ERR_get_error ());
221
220
goto e1 ;
222
221
}
223
222
224
223
pub -> secret .s = self -> sec_rand ;
225
- pub -> secret .len = RAND_SECRET_LEN ;
224
+ pub -> secret .len = AUTH_SECRET_LEN ;
226
225
227
226
/*LM_DBG("Generated secret: '%.*s'\n", pub->secret.len, pub->secret.s); */
228
227
@@ -347,5 +346,5 @@ static int Base64Decode(const str_const *b64message, unsigned char* obuffer)
347
346
348
347
rval = EVP_DecodeBlock (obuffer , (const unsigned char * )b64message -> s ,
349
348
b64message -> len );
350
- return (rval == (RAND_SECRET_LEN + 1 )) ? 0 : -1 ;
349
+ return (rval == (AUTH_SECRET_LEN + 1 )) ? 0 : -1 ;
351
350
}
0 commit comments