Skip to content

Commit bb4c2cb

Browse files
committed
address peer review: typography in linuxkm/linuxkm_memory.c, typography, clarity, and efficiency in wolfcrypt/src/sha256.c and wolfcrypt/src/sha512.c.
1 parent 393bf4a commit bb4c2cb

3 files changed

Lines changed: 85 additions & 104 deletions

File tree

linuxkm/linuxkm_memory.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,7 @@ static struct wc_thread_fpu_count_ent *wc_linuxkm_fpu_state_assoc(int create_p)
237237
* this is not thread-safe and doesn't need to be.
238238
*/
239239
int ret = allocate_wolfcrypt_linuxkm_fpu_states();
240-
if (ret == 0) {
241-
}
242-
else
240+
if (ret != 0)
243241
#endif
244242
{
245243
if (_warned_on_null == 0) {
@@ -307,9 +305,7 @@ static struct wc_thread_fpu_count_ent *wc_linuxkm_fpu_state_assoc_unlikely(int c
307305
* this is not thread-safe and doesn't need to be.
308306
*/
309307
int ret = allocate_wolfcrypt_linuxkm_fpu_states();
310-
if (ret == 0) {
311-
}
312-
else
308+
if (ret != 0)
313309
#endif
314310
{
315311
if (_warned_on_null == 0) {
@@ -458,8 +454,7 @@ WARN_UNUSED_RESULT int can_save_vector_registers_x86(void)
458454
return 0;
459455
else if (test_thread_flag(TIF_NEED_FPU_LOAD))
460456
return 1;
461-
else
462-
return 0;
457+
return 0;
463458
}
464459

465460
WARN_UNUSED_RESULT int save_vector_registers_x86(void)

wolfcrypt/src/sha256.c

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ on the specific device platform.
207207
#define SHA256_UPDATE_REV_BYTES(ctx) (sha256->sha_method == SHA256_C)
208208
#else
209209
#define SHA256_UPDATE_REV_BYTES(ctx) \
210-
(!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
210+
(!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
211211
#endif
212212
#elif defined(FREESCALE_MMCAU_SHA)
213213
#define SHA256_UPDATE_REV_BYTES(ctx) 0 /* reverse not needed on update */
@@ -232,14 +232,14 @@ on the specific device platform.
232232
(!defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)) && \
233233
!defined(WOLFSSL_RENESAS_RX64_HASH)
234234

235-
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
236-
static void Sha256_SetTransform(
235+
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
236+
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
237237
#ifdef WC_C_DYNAMIC_FALLBACK
238-
int *sha_method
238+
#define SHA256_SETTRANSFORM_ARGS int *sha_method
239239
#else
240-
void
240+
#define SHA256_SETTRANSFORM_ARGS void
241241
#endif
242-
);
242+
static void Sha256_SetTransform(SHA256_SETTRANSFORM_ARGS);
243243
#endif
244244

245245
static int InitSha256(wc_Sha256* sha256)
@@ -266,7 +266,8 @@ static int InitSha256(wc_Sha256* sha256)
266266
sha256->used = 0;
267267
#endif
268268

269-
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
269+
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
270+
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
270271
/* choose best Transform function under this runtime environment */
271272
#ifdef WC_C_DYNAMIC_FALLBACK
272273
sha256->sha_method = 0;
@@ -394,6 +395,8 @@ static int InitSha256(wc_Sha256* sha256)
394395
} /* extern "C" */
395396
#endif
396397

398+
static word32 intel_flags = 0;
399+
397400
#if defined(WC_C_DYNAMIC_FALLBACK) && !defined(WC_NO_INTERNAL_FUNCTION_POINTERS)
398401
#define WC_NO_INTERNAL_FUNCTION_POINTERS
399402
#endif
@@ -405,35 +408,28 @@ static int InitSha256(wc_Sha256* sha256)
405408
SHA256_C };
406409

407410
#ifndef WC_C_DYNAMIC_FALLBACK
408-
static word32 intel_flags;
409411
static enum sha_methods sha_method = SHA256_UNSET;
410412
#endif
411413

412-
static void Sha256_SetTransform(
413-
#ifdef WC_C_DYNAMIC_FALLBACK
414-
int *sha_method
415-
#else
416-
void
417-
#endif
418-
)
414+
static void Sha256_SetTransform(SHA256_SETTRANSFORM_ARGS)
419415
{
420-
#ifdef WC_C_DYNAMIC_FALLBACK
421-
#define SHA_METHOD (*sha_method)
422-
word32 intel_flags;
423-
#else
424-
#define SHA_METHOD sha_method
425-
#endif
416+
#ifdef WC_C_DYNAMIC_FALLBACK
417+
#define SHA_METHOD (*sha_method)
418+
#else
419+
#define SHA_METHOD sha_method
420+
#endif
426421
if (SHA_METHOD != SHA256_UNSET)
427422
return;
428423

429-
#ifdef WC_C_DYNAMIC_FALLBACK
424+
#ifdef WC_C_DYNAMIC_FALLBACK
430425
if (! CAN_SAVE_VECTOR_REGISTERS()) {
431426
SHA_METHOD = SHA256_C;
432427
return;
433428
}
434-
#endif
429+
#endif
435430

436-
intel_flags = cpuid_get_flags();
431+
if (intel_flags == 0)
432+
intel_flags = cpuid_get_flags();
437433

438434
if (IS_INTEL_SHA(intel_flags)) {
439435
#ifdef HAVE_INTEL_AVX1
@@ -478,15 +474,15 @@ static int InitSha256(wc_Sha256* sha256)
478474
{
479475
SHA_METHOD = SHA256_C;
480476
}
481-
#undef SHA_METHOD
477+
#undef SHA_METHOD
482478
}
483479

484480
static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
485-
#ifdef WC_C_DYNAMIC_FALLBACK
486-
#define SHA_METHOD (S->sha_method)
487-
#else
488-
#define SHA_METHOD sha_method
489-
#endif
481+
#ifdef WC_C_DYNAMIC_FALLBACK
482+
#define SHA_METHOD (S->sha_method)
483+
#else
484+
#define SHA_METHOD sha_method
485+
#endif
490486
int ret;
491487

492488
if (SHA_METHOD == SHA256_C)
@@ -516,16 +512,16 @@ static int InitSha256(wc_Sha256* sha256)
516512
}
517513
RESTORE_VECTOR_REGISTERS();
518514
return ret;
519-
#undef SHA_METHOD
515+
#undef SHA_METHOD
520516
}
521517
#define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
522518

523519
static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
524-
#ifdef WC_C_DYNAMIC_FALLBACK
525-
#define SHA_METHOD (S->sha_method)
526-
#else
527-
#define SHA_METHOD sha_method
528-
#endif
520+
#ifdef WC_C_DYNAMIC_FALLBACK
521+
#define SHA_METHOD (S->sha_method)
522+
#else
523+
#define SHA_METHOD sha_method
524+
#endif
529525
int ret;
530526
SAVE_VECTOR_REGISTERS(return _svr_ret;);
531527
switch (SHA_METHOD) {
@@ -552,7 +548,7 @@ static int InitSha256(wc_Sha256* sha256)
552548
}
553549
RESTORE_VECTOR_REGISTERS();
554550
return ret;
555-
#undef SHA_METHOD
551+
#undef SHA_METHOD
556552
}
557553
#define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
558554

@@ -568,36 +564,34 @@ static int InitSha256(wc_Sha256* sha256)
568564

569565
static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
570566
int ret;
571-
#ifdef WOLFSSL_LINUXKM
567+
#ifdef WOLFSSL_LINUXKM
572568
if (Transform_Sha256_is_vectorized)
573569
SAVE_VECTOR_REGISTERS(return _svr_ret;);
574-
#endif
570+
#endif
575571
ret = (*Transform_Sha256_p)(S, D);
576-
#ifdef WOLFSSL_LINUXKM
572+
#ifdef WOLFSSL_LINUXKM
577573
if (Transform_Sha256_is_vectorized)
578574
RESTORE_VECTOR_REGISTERS();
579-
#endif
575+
#endif
580576
return ret;
581577
}
582578
#define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
583579

584580
static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
585581
int ret;
586-
#ifdef WOLFSSL_LINUXKM
582+
#ifdef WOLFSSL_LINUXKM
587583
if (Transform_Sha256_is_vectorized)
588584
SAVE_VECTOR_REGISTERS(return _svr_ret;);
589-
#endif
585+
#endif
590586
ret = (*Transform_Sha256_Len_p)(S, D, L);
591-
#ifdef WOLFSSL_LINUXKM
587+
#ifdef WOLFSSL_LINUXKM
592588
if (Transform_Sha256_is_vectorized)
593589
RESTORE_VECTOR_REGISTERS();
594-
#endif
590+
#endif
595591
return ret;
596592
}
597593
#define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
598594

599-
static word32 intel_flags;
600-
601595
static void Sha256_SetTransform(void)
602596
{
603597

@@ -1958,18 +1952,18 @@ static int InitSha256(wc_Sha256* sha256)
19581952
sha224->loLen = 0;
19591953
sha224->hiLen = 0;
19601954

1961-
#ifdef WC_C_DYNAMIC_FALLBACK
1955+
#ifdef WC_C_DYNAMIC_FALLBACK
19621956
sha224->sha_method = 0;
1963-
#endif
1957+
#endif
19641958

19651959
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
19661960
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
19671961
/* choose best Transform function under this runtime environment */
1968-
#ifdef WC_C_DYNAMIC_FALLBACK
1962+
#ifdef WC_C_DYNAMIC_FALLBACK
19691963
Sha256_SetTransform(&sha224->sha_method);
1970-
#else
1964+
#else
19711965
Sha256_SetTransform();
1972-
#endif
1966+
#endif
19731967
#endif
19741968
#ifdef WOLFSSL_HASH_FLAGS
19751969
sha224->flags = 0;

0 commit comments

Comments
 (0)