什么是双棘轮算法
双棘轮算法用于通信双方基于共享密钥交换加密消息。通常,通信双方将先使用某种密钥协商协议(例如 X3DH1)以协商共享密钥。此后,通信双方即可使用双棘轮算法发送接收加密消息了。
通信双方将为每一个双棘轮消息派生出新的密钥,使得旧的密钥不能从新的密钥计算得到。通信双方还将在消息中附上迪菲-赫尔曼公钥值。迪菲-赫尔曼计算的结果将被混入派生出的密钥中,使得新的密钥不能从旧的密钥计算得到。这些特性将在某一方的密钥泄漏后保护此前或此后的加密消息。
代码实现
var a PseronA
a.IdentityPri, a.IdentityPub = GetCurve25519KeypPair()
a.SignedPri, a.SignedPub = GetCurve25519KeypPair()
a.OneTimePri, a.OneTimePub = GetCurve25519KeypPair()
a.EphemeralPri, a.EphemeralPub = GetCurve25519KeypPair()
var b PseronB
b.IdentityPri, b.IdentityPub = GetCurve25519KeypPair()
b.SignedPri, b.SignedPub = GetCurve25519KeypPair()
b.OneTimePri, b.OneTimePub = GetCurve25519KeypPair()
b.EphemeralPri, b.EphemeralPub = GetCurve25519KeypPair()
//DH1 = DH(IPK-A私钥, SPK-B公钥)
//DH2 = DH(EPK-A私钥, IPK-B公钥)
//DH3= DH(EPK-A私钥, SPK-B公钥)
//DH4 = DH(IPK-A私钥, OPK--B公钥)
a.DH1 = GetCurve25519Key(a.IdentityPri, b.SignedPub)
a.DH2 = GetCurve25519Key(a.EphemeralPri, b.IdentityPub)
a.DH3 = GetCurve25519Key(a.EphemeralPri, b.SignedPub)
a.DH4 = GetCurve25519Key(a.EphemeralPri, b.OneTimePub)
b.DH1 = GetCurve25519Key(b.SignedPri, a.IdentityPub)
b.DH2 = GetCurve25519Key(b.IdentityPri, a.EphemeralPub)
b.DH3 = GetCurve25519Key(b.SignedPri, a.EphemeralPub)
b.DH4 = GetCurve25519Key(b.OneTimePri, a.EphemeralPub)
var aKey = bytes.Join([][]byte{a.DH1[:], a.DH2[:], a.DH3[:], a.DH4[:]}, []byte{})
var bKey = bytes.Join([][]byte{b.DH1[:], b.DH2[:], b.DH3[:], b.DH4[:]}, []byte{})
fmt.Println("ADH1", base64.StdEncoding.EncodeToString(a.DH1[:]))
fmt.Println("ADH2", base64.StdEncoding.EncodeToString(a.DH2[:]))
fmt.Println("ADH3", base64.StdEncoding.EncodeToString(a.DH3[:]))
fmt.Println("ADH4", base64.StdEncoding.EncodeToString(a.DH4[:]))
fmt.Println("BDH1", base64.StdEncoding.EncodeToString(b.DH1[:]))
fmt.Println("BDH2", base64.StdEncoding.EncodeToString(b.DH2[:]))
fmt.Println("BDH3", base64.StdEncoding.EncodeToString(b.DH3[:]))
fmt.Println("BDH4", base64.StdEncoding.EncodeToString(b.DH4[:]))
fmt.Println("aKey", base64.StdEncoding.EncodeToString(aKey))
fmt.Println("aKey", base64.StdEncoding.EncodeToString(kdf(aKey)))
fmt.Println("bKey", base64.StdEncoding.EncodeToString(bKey))
fmt.Println("bKey", base64.StdEncoding.EncodeToString(kdf(bKey)))
fmt.Println("x3DH结束")
fmt.Println("开始计算Signal protocol(双棘轮)")
for i := 1; i <= 3; i++ {
aSalt := GetCurve25519Key(a.EphemeralPri, b.EphemeralPub)
fmt.Println("计算A的salt", base64.StdEncoding.EncodeToString(aSalt[:]))
fmt.Println("计算A的KEY", base64.StdEncoding.EncodeToString(Signalkdf(aKey, aSalt, "A")))
bSalt := GetCurve25519Key(b.EphemeralPri, a.EphemeralPub)
fmt.Println("计算B的salt", base64.StdEncoding.EncodeToString(bSalt[:]))
fmt.Println("计算B的KEY", base64.StdEncoding.EncodeToString(Signalkdf(bKey, bSalt, "B")))
}
输出值
ADH1 /+X9wl2KPWdV+nUlglwqCOwfhXMddZIQz6b22b9TVVM=
ADH2 0BCGm+Fn0ue8xyvZAQlT6Nt7XGiI5tVFm8RJT8mxlz0=
ADH3 +QtJECdZdn3wipebo/7MTJEzNsqzU4z9VyIAIZ2oPgw=
ADH4 B5NQhL/2UwTs2HYjlog06QuwR7GLnNOCUbFCNKJSLhE=
BDH1 /+X9wl2KPWdV+nUlglwqCOwfhXMddZIQz6b22b9TVVM=
BDH2 0BCGm+Fn0ue8xyvZAQlT6Nt7XGiI5tVFm8RJT8mxlz0=
BDH3 +QtJECdZdn3wipebo/7MTJEzNsqzU4z9VyIAIZ2oPgw=
BDH4 B5NQhL/2UwTs2HYjlog06QuwR7GLnNOCUbFCNKJSLhE=
aKey /+X9wl2KPWdV+nUlglwqCOwfhXMddZIQz6b22b9TVVPQEIab4WfS57zHK9kBCVPo23tcaIjm1UWbxElPybGXPfkLSRAnWXZ98IqXm6P+zEyRMzbKs1OM/VciACGdqD4MB5NQhL/2UwTs2HYjlog06QuwR7GLnNOCUbFCNKJSLhE=
aKey jByyQR2sNtJLdC9lD3OIHQe4X/iPzCiZMoiq+TzCGCM=
bKey /+X9wl2KPWdV+nUlglwqCOwfhXMddZIQz6b22b9TVVPQEIab4WfS57zHK9kBCVPo23tcaIjm1UWbxElPybGXPfkLSRAnWXZ98IqXm6P+zEyRMzbKs1OM/VciACGdqD4MB5NQhL/2UwTs2HYjlog06QuwR7GLnNOCUbFCNKJSLhE=
bKey jByyQR2sNtJLdC9lD3OIHQe4X/iPzCiZMoiq+TzCGCM=
x3DH结束
开始计算Signal protocol(双棘轮)
计算A的salt第 1 次 tTh8Pt9qZYXGLo5xX0SCLRHXgytK7KLN4Kp0l8IFVEQ=
计算A的KEY第 1 次 XX/5C4toO4AtidozmQU6zUVImoI3EqTUNWFM6bVu9Nk=
计算B的salt第 1 次 tTh8Pt9qZYXGLo5xX0SCLRHXgytK7KLN4Kp0l8IFVEQ=
计算B的KEY第 1 次 XX/5C4toO4AtidozmQU6zUVImoI3EqTUNWFM6bVu9Nk=
计算A的salt第 2 次 tTh8Pt9qZYXGLo5xX0SCLRHXgytK7KLN4Kp0l8IFVEQ=
计算A的KEY第 2 次 /EjtTIfRbB6z0+GxRW8Y8ifVOITMKbqsUKdHHqs36Sw=
计算B的salt第 2 次 tTh8Pt9qZYXGLo5xX0SCLRHXgytK7KLN4Kp0l8IFVEQ=
计算B的KEY第 2 次 /EjtTIfRbB6z0+GxRW8Y8ifVOITMKbqsUKdHHqs36Sw=
计算A的salt第 3 次 tTh8Pt9qZYXGLo5xX0SCLRHXgytK7KLN4Kp0l8IFVEQ=
计算A的KEY第 3 次 FweRXcj+QyaZC56PVGCd8vSVV0CvBZIrFYbDMIPdlsQ=
计算B的salt第 3 次 tTh8Pt9qZYXGLo5xX0SCLRHXgytK7KLN4Kp0l8IFVEQ=
计算B的KEY第 3 次 FweRXcj+QyaZC56PVGCd8vSVV0CvBZIrFYbDMIPdlsQ=