附录 A. 示例 20 位哈希函数
本附录提供一个可用于生成 Flow Label 值的哈希函数示例. 这不是强制算法. 实现可以自由使用任何能够产生合适 Flow Label 值分布的方法.
目的
此哈希函数的目的在于生成满足以下条件的 Flow Label 值:
- 近似离散均匀分布
- 难以被第三方预测
- 对属于同一流的分组保持一致
- 可以高效计算
算法描述
该哈希函数以以下内容作为输入:
- Source Address (128 位)
- Destination Address (128 位)
- Source Port (16 位)
- Destination Port (16 位)
- Protocol Number (8 位)
- 源节点唯一的秘密密钥 (128 位)
输出是 20 位 Flow Label 值.
实现方法
一种可能的实现方法如下:
-
连接输入: 将所有输入值连接成单个位串.
-
应用密码学哈希: 对连接后的输入应用密码学哈希函数 (例如 MD5, SHA-1 或 SHA-256).
-
提取 20 位: 从哈希输出中提取 20 位形成 Flow Label 值.
使用 MD5 的示例
Input:
- Source Address: 128 bits
- Destination Address: 128 bits
- Source Port: 16 bits
- Destination Port: 16 bits
- Protocol: 8 bits
- Secret Key: 128 bits
Process:
1. Concatenate all inputs: 424 bits total
2. Compute MD5 hash: 128-bit output
3. Take the first 20 bits of the MD5 output as the Flow Label
Output:
- Flow Label: 20 bits
安全考虑事项
秘密密钥对于防止攻击者预测 Flow Label 值至关重要. 秘密密钥应:
- 长度至少为 128 位
- 使用密码学安全随机数生成器生成
- 对每个源节点唯一
- 保持机密, 且不通过网络传输
- 定期更改 (例如每天或每周一次)
性能考虑事项
为每个分组计算密码学哈希可能计算开销较高. 实现可以通过以下方式优化性能:
-
缓存: 缓存活动流的 Flow Label 值, 并为同一流中的后续分组复用该值.
-
轻量级哈希: 使用轻量级哈希函数替代完整密码学哈希, 只要它提供足够的不可预测性.
-
硬件加速: 在可用时使用硬件加速进行哈希计算.
替代方法
其他可使用的方法包括:
-
Linear Congruential Generator (LCG): 使用流 5 元组和秘密密钥作为种子的简单伪随机数生成器.
-
SipHash: 为哈希表查找设计的快速且密码学强的哈希函数.
-
基于 AES 的 PRF: 使用计数器模式或 CMAC 模式下的 AES 作为伪随机函数.
哈希函数的选择取决于实现的具体要求和约束. 关键要求是输出应对攻击者不可预测, 并应近似均匀分布.
示例代码 (伪代码)
function compute_flow_label(src_addr, dst_addr, src_port, dst_port, protocol, secret_key):
// Concatenate inputs
input = concatenate(src_addr, dst_addr, src_port, dst_port, protocol, secret_key)
// Compute hash
hash_output = MD5(input)
// Extract 20 bits
flow_label = hash_output[0:20] // First 20 bits
// Ensure non-zero (optional, depending on requirements)
if flow_label == 0:
flow_label = 1
return flow_label
Notes
-
The Flow Label value of 0 has special meaning (unlabeled packets). Implementations may choose to avoid generating a Flow Label of 0, or they may allow it.
-
The hash function should be applied consistently for all packets in a flow to ensure that they all receive the same Flow Label value.
-
The secret key should be protected and not exposed to potential attackers.