Quellcode durchsuchen

[crypto] Eliminate repetitions in MD5 round constant table

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown vor 7 Jahren
Ursprung
Commit
32d54691e9
1 geänderte Dateien mit 10 neuen und 7 gelöschten Zeilen
  1. 10
    7
      src/crypto/md5.c

+ 10
- 7
src/crypto/md5.c Datei anzeigen

@@ -66,11 +66,11 @@ static const uint32_t k[64] = {
66 66
 };
67 67
 
68 68
 /** MD5 shift amounts */
69
-static const uint8_t r[64] = {
70
-	7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
71
-	5,  9, 14, 20, 5,  9, 14, 20, 5,  9, 14, 20, 5,  9, 14, 20,
72
-	4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
73
-	6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
69
+static const uint8_t r[4][4] = {
70
+	{  7, 12, 17, 22 },
71
+	{  5,  9, 14, 20 },
72
+	{  4, 11, 16, 23 },
73
+	{  6, 10, 15, 21 },
74 74
 };
75 75
 
76 76
 /**
@@ -174,6 +174,7 @@ static void md5_digest ( struct md5_context *context ) {
174 174
 	uint32_t g;
175 175
 	uint32_t temp;
176 176
 	struct md5_step *step;
177
+	unsigned int round;
177 178
 	unsigned int i;
178 179
 
179 180
 	/* Sanity checks */
@@ -201,13 +202,15 @@ static void md5_digest ( struct md5_context *context ) {
201 202
 
202 203
 	/* Main loop */
203 204
 	for ( i = 0 ; i < 64 ; i++ ) {
204
-		step = &md5_steps[ i / 16 ];
205
+		round = ( i / 16 );
206
+		step = &md5_steps[round];
205 207
 		f = step->f ( &u.v );
206 208
 		g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
207 209
 		temp = *d;
208 210
 		*d = *c;
209 211
 		*c = *b;
210
-		*b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ), r[i] ) );
212
+		*b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ),
213
+				    r[round][ i % 4 ] ) );
211 214
 		*a = temp;
212 215
 		DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
213 216
 			i, *a, *b, *c, *d );

Laden…
Abbrechen
Speichern