@@ -530,10 +530,10 @@ static void BF_swap(BF_word *x, int count)
530530 *(ptr - 1) = R; \
531531 } while (ptr < &data.ctx.S[3][0xFF]);
532532
533- static void BF_set_key (const char * key , size_t key_len , BF_key expanded ,
534- BF_key initial , unsigned char flags )
533+ static void BF_set_key (const char * key , BF_key expanded , BF_key initial ,
534+ unsigned char flags )
535535{
536- size_t key_pos = 0 ;
536+ const char * ptr = key ;
537537 unsigned int bug , i , j ;
538538 BF_word safety , sign , diff , tmp [2 ];
539539
@@ -559,7 +559,8 @@ static void BF_set_key(const char *key, size_t key_len, BF_key expanded,
559559 * information - that is, we mostly use fixed-cost bitwise operations instead
560560 * of branches or table lookups. (One conditional branch based on password
561561 * length remains. It is not part of the bug aftermath, though, and is
562- * difficult and possibly unreasonable to avoid here.)
562+ * difficult and possibly unreasonable to avoid given the use of C strings by
563+ * the caller, which results in similar timing leaks anyway.)
563564 *
564565 * For actual implementation, we set an array index in the variable "bug"
565566 * (0 means no bug, 1 means sign extension bug emulation) and a flag in the
@@ -576,33 +577,25 @@ static void BF_set_key(const char *key, size_t key_len, BF_key expanded,
576577
577578 sign = diff = 0 ;
578579
579- /*
580- * bcrypt cycles over the password bytes plus a trailing NUL terminator.
581- * The explicit length keeps embedded NUL bytes significant while
582- * preserving the historical behavior for ordinary C strings.
583- */
584580 for (i = 0 ; i < BF_N + 2 ; i ++ ) {
585581 tmp [0 ] = tmp [1 ] = 0 ;
586582 for (j = 0 ; j < 4 ; j ++ ) {
587- unsigned char c = key_pos < key_len ? (unsigned char ) key [key_pos ] : 0 ;
588-
589583 tmp [0 ] <<= 8 ;
590- tmp [0 ] |= c ; /* correct */
584+ tmp [0 ] |= ( unsigned char ) * ptr ; /* correct */
591585 tmp [1 ] <<= 8 ;
592- tmp [1 ] |= (BF_word_signed )(signed char )c ; /* bug */
586+ tmp [1 ] |= (BF_word_signed )(signed char )* ptr ; /* bug */
593587/*
594588 * Sign extension in the first char has no effect - nothing to overwrite yet,
595589 * and those extra 24 bits will be fully shifted out of the 32-bit word. For
596590 * chars 2, 3, 4 in each four-char block, we set bit 7 of "sign" if sign
597591 * extension in tmp[1] occurs. Once this flag is set, it remains set.
598592 */
599- if (j ) {
593+ if (j )
600594 sign |= tmp [1 ] & 0x80 ;
601- }
602- key_pos ++ ;
603- if (key_pos > key_len ) {
604- key_pos = 0 ;
605- }
595+ if (!* ptr )
596+ ptr = key ;
597+ else
598+ ptr ++ ;
606599 }
607600 diff |= tmp [0 ] ^ tmp [1 ]; /* Non-zero on any differences */
608601
@@ -643,7 +636,7 @@ static const unsigned char flags_by_subtype[26] =
643636 {2 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
644637 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 4 , 0 };
645638
646- static char * BF_crypt (const char * key , size_t key_len , const char * setting ,
639+ static char * BF_crypt (const char * key , const char * setting ,
647640 char * output , int size ,
648641 BF_word min )
649642{
@@ -686,7 +679,7 @@ static char *BF_crypt(const char *key, size_t key_len, const char *setting,
686679 }
687680 BF_swap (data .binary .salt , 4 );
688681
689- BF_set_key (key , key_len , data .expanded_key , data .ctx .P ,
682+ BF_set_key (key , data .expanded_key , data .ctx .P ,
690683 flags_by_subtype [(unsigned int )(unsigned char )setting [2 ] - 'a' ]);
691684
692685 memcpy (data .ctx .S , BF_init_state .S , sizeof (data .ctx .S ));
@@ -807,10 +800,10 @@ static int _crypt_output_magic(const char *setting, char *output, int size)
807800 * The performance cost of this quick self-test is around 0.6% at the "$2a$08"
808801 * setting.
809802 */
810- char * php_crypt_blowfish_rn (const char * key , size_t key_len ,
811- const char * setting , char * output , int size )
803+ char * php_crypt_blowfish_rn (const char * key , const char * setting ,
804+ char * output , int size )
812805{
813- static const char test_key [] = "8b \xd0\xc1\xd2\xcf\xcc\xd8" ;
806+ const char * test_key = "8b \xd0\xc1\xd2\xcf\xcc\xd8" ;
814807 const char * test_setting = "$2a$00$abcdefghijklmnopqrstuu" ;
815808 static const char * const test_hashes [2 ] =
816809 {"i1D709vfamulimlGcq0qq3UvuUasvEa\0\x55" , /* 'a', 'b', 'y' */
@@ -826,7 +819,7 @@ char *php_crypt_blowfish_rn(const char *key, size_t key_len,
826819
827820/* Hash the supplied password */
828821 _crypt_output_magic (setting , output , size );
829- retval = BF_crypt (key , key_len , setting , output , size , 16 );
822+ retval = BF_crypt (key , setting , output , size , 16 );
830823 save_errno = errno ;
831824
832825/*
@@ -845,17 +838,17 @@ char *php_crypt_blowfish_rn(const char *key, size_t key_len,
845838 }
846839 memset (buf .o , 0x55 , sizeof (buf .o ));
847840 buf .o [sizeof (buf .o ) - 1 ] = 0 ;
848- p = BF_crypt (test_key , sizeof ( test_key ) - 1 , buf .s , buf .o , sizeof (buf .o ) - (1 + 1 ), 1 );
841+ p = BF_crypt (test_key , buf .s , buf .o , sizeof (buf .o ) - (1 + 1 ), 1 );
849842
850843 ok = (p == buf .o &&
851844 !memcmp (p , buf .s , 7 + 22 ) &&
852845 !memcmp (p + (7 + 22 ), test_hash , 31 + 1 + 1 + 1 ));
853846
854847 {
855- static const char k [] = "\xff\xa3" "34" "\xff\xff\xff\xa3" "345" ;
848+ const char * k = "\xff\xa3" "34" "\xff\xff\xff\xa3" "345" ;
856849 BF_key ae , ai , ye , yi ;
857- BF_set_key (k , sizeof ( k ) - 1 , ae , ai , 2 ); /* $2a$ */
858- BF_set_key (k , sizeof ( k ) - 1 , ye , yi , 4 ); /* $2y$ */
850+ BF_set_key (k , ae , ai , 2 ); /* $2a$ */
851+ BF_set_key (k , ye , yi , 4 ); /* $2y$ */
859852 ai [0 ] ^= 0x10000 ; /* undo the safety (for comparison) */
860853 ok = ok && ai [0 ] == 0xdb9c59bc && ye [17 ] == 0x33343500 &&
861854 !memcmp (ae , ye , sizeof (ae )) &&
0 commit comments