2023-11-03 16:10:11 -04:00
|
|
|
package shadowsocks2022
|
|
|
|
|
|
|
|
import (
|
|
|
|
"lukechampine.com/blake3"
|
2023-11-18 19:42:20 -05:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
2023-11-03 16:10:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func newBLAKE3KeyDerivation() *BLAKE3KeyDerivation {
|
|
|
|
return &BLAKE3KeyDerivation{}
|
|
|
|
}
|
|
|
|
|
2023-11-18 19:42:20 -05:00
|
|
|
type BLAKE3KeyDerivation struct{}
|
2023-11-03 16:10:11 -04:00
|
|
|
|
2023-11-18 19:42:20 -05:00
|
|
|
func (b BLAKE3KeyDerivation) GetSessionSubKey(effectivePsk, salt []byte, outKey []byte) error {
|
2023-11-03 16:10:11 -04:00
|
|
|
keyingMaterialBuffer := buf.New()
|
|
|
|
keyingMaterialBuffer.Write(effectivePsk)
|
2023-11-18 19:42:20 -05:00
|
|
|
keyingMaterialBuffer.Write(salt)
|
|
|
|
blake3.DeriveKey(outKey, "shadowsocks 2022 session subkey", keyingMaterialBuffer.Bytes())
|
2023-11-03 16:10:11 -04:00
|
|
|
keyingMaterialBuffer.Release()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-18 19:42:20 -05:00
|
|
|
func (b BLAKE3KeyDerivation) GetIdentitySubKey(effectivePsk, salt []byte, outKey []byte) error {
|
2023-11-03 16:10:11 -04:00
|
|
|
keyingMaterialBuffer := buf.New()
|
|
|
|
keyingMaterialBuffer.Write(effectivePsk)
|
2023-11-18 19:42:20 -05:00
|
|
|
keyingMaterialBuffer.Write(salt)
|
|
|
|
blake3.DeriveKey(outKey, "shadowsocks 2022 identity subkey", keyingMaterialBuffer.Bytes())
|
2023-11-03 16:10:11 -04:00
|
|
|
keyingMaterialBuffer.Release()
|
|
|
|
return nil
|
|
|
|
}
|