@@ -259,7 +259,7 @@ func GenerateRRDS(oName string, kRDATA dns.DNSRDATADNSKEY, dType dns.DNSSECDiges
259
259
return rr
260
260
}
261
261
262
- // GenerateWrongKey 生成一个具有指定KeyTag,且能通过检验,但错误的 DNSKEY RDATA
262
+ // GenerateWrongKeyWithTag 生成一个具有指定KeyTag,且能通过检验,但错误的 DNSKEY RDATA
263
263
// 传入参数:
264
264
// - algo: DNSSEC 算法
265
265
// - flag: DNSKEY Flag
@@ -279,66 +279,45 @@ func GenerateWrongKeyWithTag(algo dns.DNSSECAlgorithm, flag dns.DNSKEYFlag, tag
279
279
280
280
rTag := CalculateKeyTag (pKey )
281
281
dif := tag - int (rTag )
282
-
283
- fmt .Printf ("rTag:%d, tTag:%d, dif: %d\n " , rTag , tag , dif )
284
-
285
282
if dif < 0 {
286
- dif = - dif
287
- hDif := dif >> 8
288
- lDif := dif & 0xFF
289
-
290
- for tvlr , _ := range pubKey {
291
- if tvlr & 1 == 0 {
292
- if int ( pubKey [ tvlr ]) - int ( hDif ) < 0 {
293
- pubKey [ tvlr ] = 0
294
- hDif -= int (pubKey [tvlr ])
295
- } else {
296
- pubKey [ tvlr ] -= byte ( hDif )
297
- hDif = 0
298
- }
283
+ dif = 0xFFFF + dif
284
+ }
285
+ hDif := dif & 0xFF00 >> 8
286
+ lDif := dif & 0xFF
287
+ for i := 0 ; i < len ( pubKey ); i ++ {
288
+ bVal := int ( pubKey [ i ])
289
+ if lDif != 0 && i & 1 == 1 {
290
+ if bVal + lDif > 255 {
291
+ lDif = 255 - int (pubKey [i ])
292
+ pubKey [ i ] = 255
293
+ } else if bVal + lDif < 0 {
294
+ lDif -= int ( pubKey [ i ])
295
+ pubKey [ i ] = 0
299
296
} else {
300
- if int (pubKey [tvlr ])- int (hDif ) < 0 {
301
- pubKey [tvlr ] = 0
302
- lDif -= int (pubKey [tvlr ])
303
- } else {
304
- pubKey [tvlr ] -= byte (lDif )
305
- lDif = 0
306
- }
307
- }
308
- if hDif == 0 && lDif == 0 {
309
- break
297
+ pubKey [i ] = byte (int (pubKey [i ]) + lDif )
298
+ lDif = 0
310
299
}
311
300
}
312
- } else {
313
- hDif := dif >> 8
314
- lDif := dif & 0xFF
315
-
316
- for tvlr , _ := range pubKey {
317
- if tvlr & 1 == 0 {
318
- if int (pubKey [tvlr ])+ int (hDif ) > 0xFF {
319
- pubKey [tvlr ] = 0xFF
320
- hDif -= int (0xFF ) - int (pubKey [tvlr ])
321
- } else {
322
- pubKey [tvlr ] += byte (hDif )
323
- hDif = 0
324
- }
301
+ if hDif != 0 && i & 1 == 0 {
302
+ if bVal + hDif > 255 {
303
+ hDif = 255 - int (pubKey [i ])
304
+ pubKey [i ] = 255
305
+ } else if bVal + hDif < 0 {
306
+ hDif -= int (pubKey [i ])
307
+ pubKey [i ] = 0
325
308
} else {
326
- if int (pubKey [tvlr ])+ int (lDif ) > 0xFF {
327
- pubKey [tvlr ] = 0xFF
328
- lDif -= int (0xFF ) - int (pubKey [tvlr ])
329
- } else {
330
- pubKey [tvlr ] += byte (lDif )
331
- lDif = 0
332
- }
333
- }
334
- if hDif == 0 && lDif == 0 {
335
- break
309
+ pubKey [i ] = byte (int (pubKey [i ]) + hDif )
310
+ hDif = 0
336
311
}
337
312
}
338
313
}
339
314
315
+ nTag := CalculateKeyTag (pKey )
316
+ fmt .Printf ("rTag:%d, tTag:%d, dif: %d, nTag:%d, ldif: %d, hdif: %d\n " ,
317
+ rTag , tag , dif , nTag , lDif , hDif )
318
+
340
319
// 重新计算 Key Tag, 算法不能保证成功
341
- if rTag != uint16 (tag ) {
320
+ if nTag != uint16 (tag ) {
342
321
return GenerateWrongKeyWithTag (algo , flag , tag )
343
322
}
344
323
0 commit comments