25
25
26
26
#include " spake2p.h"
27
27
28
- #include < errno.h>
29
- #include < stdio.h>
28
+ #include < cstring>
29
+ #include < fstream>
30
+ #include < iomanip>
31
+ #include < iostream>
30
32
31
33
#include < CHIPVersion.h>
32
34
#include < crypto/CHIPCryptoPAL.h>
@@ -157,30 +159,29 @@ uint8_t gSalt[BASE64_MAX_DECODED_LEN(BASE64_ENCODED_LEN(kSpake2p_Max_PBKDF_Salt_
157
159
uint8_t gSaltDecodedLen = 0 ;
158
160
uint8_t gSaltLen = 0 ;
159
161
const char * gOutFileName = nullptr ;
160
- FILE * gPinCodeFile = nullptr ;
162
+ std::ifstream gPinCodeFile ;
161
163
162
164
static uint32_t GetNextPinCode ()
163
165
{
164
- if (!gPinCodeFile )
166
+ if (!gPinCodeFile . is_open () )
165
167
{
166
168
return chip::kSetupPINCodeUndefinedValue ;
167
169
}
168
- char * pinCodeStr = nullptr ;
169
- size_t readSize = 8 ;
170
- uint32_t pinCode = chip:: kSetupPINCodeUndefinedValue ;
171
- if (getline (&pinCodeStr, &readSize, gPinCodeFile ) != - 1 )
170
+ std::string pinCodeStr;
171
+ uint32_t pinCode = chip:: kSetupPINCodeUndefinedValue ;
172
+ std::getline ( gPinCodeFile , pinCodeStr) ;
173
+ if (! gPinCodeFile . fail () )
172
174
{
173
- if (readSize > 8 )
175
+ if (pinCodeStr. length () > 8 )
174
176
{
175
- pinCodeStr[ 8 ] = 0 ;
177
+ pinCodeStr = pinCodeStr. substr ( 0 , 8 ) ;
176
178
}
177
- pinCode = static_cast <uint32_t >(atoi (pinCodeStr));
179
+ pinCode = static_cast <uint32_t >(atoi (pinCodeStr. c_str () ));
178
180
if (!chip::SetupPayload::IsValidSetupPIN (pinCode))
179
181
{
180
- fprintf (stderr, " The line %s in PIN codes file is invalid, using a random PIN code.\n " , pinCodeStr) ;
182
+ std::cerr << " The line " << pinCodeStr << " in PIN codes file is invalid, using a random PIN code.\n " ;
181
183
pinCode = chip::kSetupPINCodeUndefinedValue ;
182
184
}
183
- free (pinCodeStr);
184
185
}
185
186
return pinCode;
186
187
}
@@ -206,8 +207,8 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
206
207
break ;
207
208
208
209
case ' f' :
209
- gPinCodeFile = fopen (arg, " r " );
210
- if (! gPinCodeFile )
210
+ gPinCodeFile . open (arg, std::ios::in );
211
+ if (gPinCodeFile . fail () )
211
212
{
212
213
PrintArgError (" %s: Failed to open the PIN code file: %s\n " , progName, arg);
213
214
return false ;
@@ -235,7 +236,7 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
235
236
case ' s' :
236
237
if (strlen (arg) > BASE64_ENCODED_LEN (kSpake2p_Max_PBKDF_Salt_Length ))
237
238
{
238
- fprintf (stderr, " %s : Salt parameter too long: %s \n " , progName, arg) ;
239
+ std::cerr << progName << " : Salt parameter too long: " << arg << " \n " ;
239
240
return false ;
240
241
}
241
242
@@ -245,13 +246,13 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
245
246
// Now double-check if the length is correct.
246
247
if (gSaltDecodedLen > kSpake2p_Max_PBKDF_Salt_Length )
247
248
{
248
- fprintf (stderr, " %s : Salt parameter too long: %s \n " , progName, arg) ;
249
+ std::cerr << progName << " : Salt parameter too long: " << arg << " \n " ;
249
250
return false ;
250
251
}
251
252
252
253
if (gSaltDecodedLen < kSpake2p_Min_PBKDF_Salt_Length )
253
254
{
254
- fprintf (stderr, " %s : Salt parameter too short: %s \n " , progName, arg) ;
255
+ std::cerr << progName << " : Salt parameter too short: " << arg << " \n " ;
255
256
return false ;
256
257
}
257
258
@@ -273,31 +274,31 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
273
274
274
275
bool Cmd_GenVerifier (int argc, char * argv[])
275
276
{
276
- FILE * outFile = nullptr ;
277
-
277
+ std::ofstream outFile;
278
+ std::ostream * outStream = &outFile;
278
279
if (argc == 1 )
279
280
{
280
281
gHelpOptions .PrintBriefUsage (stderr);
281
282
return true ;
282
283
}
283
284
284
285
bool res = ParseArgs (CMD_NAME, argc, argv, gCmdOptionSets );
285
- VerifyOrReturnError (res, false );
286
+ VerifyOrReturnValue (res, false );
286
287
287
288
if (gIterationCount == 0 )
288
289
{
289
- fprintf (stderr, " Please specify the iteration-count parameter.\n " ) ;
290
+ std::cerr << " Please specify the iteration-count parameter.\n " ;
290
291
return false ;
291
292
}
292
293
293
294
if (gSaltDecodedLen == 0 && gSaltLen == 0 )
294
295
{
295
- fprintf (stderr, " Please specify at least one of the 'salt' or 'salt-len' parameters.\n " ) ;
296
+ std::cerr << " Please specify at least one of the 'salt' or 'salt-len' parameters.\n " ;
296
297
return false ;
297
298
}
298
299
if (gSaltDecodedLen != 0 && gSaltLen != 0 && gSaltDecodedLen != gSaltLen )
299
300
{
300
- fprintf (stderr, " The specified 'salt-len' doesn't match the length of 'salt' parameter.\n " ) ;
301
+ std::cerr << " The specified 'salt-len' doesn't match the length of 'salt' parameter.\n " ;
301
302
return false ;
302
303
}
303
304
if (gSaltLen == 0 )
@@ -307,28 +308,27 @@ bool Cmd_GenVerifier(int argc, char * argv[])
307
308
308
309
if (gOutFileName == nullptr )
309
310
{
310
- fprintf (stderr, " Please specify the output file name, or - for stdout.\n " ) ;
311
+ std::cerr << " Please specify the output file name, or - for stdout.\n " ;
311
312
return false ;
312
313
}
313
314
314
315
if (strcmp (gOutFileName , " -" ) != 0 )
315
316
{
316
- outFile = fopen (gOutFileName , " w+b " );
317
- if (outFile == nullptr )
317
+ outFile. open (gOutFileName , std::ios::binary | std::ios:: trunc );
318
+ if (! outFile. is_open () )
318
319
{
319
- fprintf (stderr, " Unable to create file %s \n %s \n " , gOutFileName , strerror (errno)) ;
320
+ std::cerr << " Unable to create file " << gOutFileName << " \n " << strerror (errno) << " \n " ;
320
321
return false ;
321
322
}
322
323
}
323
324
else
324
325
{
325
- outFile = stdout ;
326
+ outStream = &std::cout ;
326
327
}
327
-
328
- if (fprintf (outFile, " Index,PIN Code,Iteration Count,Salt,Verifier \n " ) < 0 || ferror (outFile ))
328
+ (*outStream) << " Index,PIN Code,Iteration Count,Salt,Verifier \n " ;
329
+ if (outStream-> fail ( ))
329
330
{
330
- fprintf (stderr, " Error writing to output file: %s\n " , strerror (errno));
331
- return false ;
331
+ std::cerr << " Error writing to output file: " << strerror (errno) << " \n " ;
332
332
}
333
333
334
334
for (uint32_t i = 0 ; i < gCount ; i++)
@@ -339,7 +339,7 @@ bool Cmd_GenVerifier(int argc, char * argv[])
339
339
CHIP_ERROR err = chip::Crypto::DRBG_get_bytes (salt, gSaltLen );
340
340
if (err != CHIP_NO_ERROR)
341
341
{
342
- fprintf (stderr, " DRBG_get_bytes() failed.\n " ) ;
342
+ std::cerr << " DRBG_get_bytes() failed.\n " ;
343
343
return false ;
344
344
}
345
345
}
@@ -353,7 +353,7 @@ bool Cmd_GenVerifier(int argc, char * argv[])
353
353
(gPinCode == chip::kSetupPINCodeUndefinedValue ), gPinCode );
354
354
if (err != CHIP_NO_ERROR)
355
355
{
356
- fprintf (stderr, " GeneratePASEVerifier() failed.\n " ) ;
356
+ std::cerr << " GeneratePASEVerifier() failed.\n " ;
357
357
return false ;
358
358
}
359
359
@@ -362,7 +362,7 @@ bool Cmd_GenVerifier(int argc, char * argv[])
362
362
err = verifier.Serialize (serializedVerifierSpan);
363
363
if (err != CHIP_NO_ERROR)
364
364
{
365
- fprintf (stderr, " Spake2pVerifier::Serialize() failed.\n " ) ;
365
+ std::cerr << " Spake2pVerifier::Serialize() failed.\n " ;
366
366
return false ;
367
367
}
368
368
@@ -374,9 +374,11 @@ bool Cmd_GenVerifier(int argc, char * argv[])
374
374
uint32_t verifierB64Len = chip::Base64Encode32 (serializedVerifier, kSpake2p_VerifierSerialized_Length , verifierB64);
375
375
verifierB64[verifierB64Len] = ' \0 ' ;
376
376
377
- if (fprintf (outFile, " %d,%08d,%d,%s,%s\n " , i, gPinCode , gIterationCount , saltB64, verifierB64) < 0 || ferror (outFile))
377
+ (*outStream) << i << " ," << std::setfill (' 0' ) << std::setw (8 ) << gPinCode << " ," << gIterationCount << " ," << saltB64 << " ,"
378
+ << verifierB64 << " \n " ;
379
+ if (outStream->fail ())
378
380
{
379
- fprintf (stderr, " Error writing to output file: %s \n " , strerror (errno)) ;
381
+ std::cerr << " Error writing to output file: " << strerror (errno) << " \n " ;
380
382
return false ;
381
383
}
382
384
@@ -386,9 +388,6 @@ bool Cmd_GenVerifier(int argc, char * argv[])
386
388
gSaltDecodedLen = 0 ;
387
389
}
388
390
389
- if (gPinCodeFile )
390
- {
391
- fclose (gPinCodeFile );
392
- }
391
+ gPinCodeFile .close ();
393
392
return true ;
394
393
}
0 commit comments