Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: suci profile B decryptSharedKey front zero truncated #41

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions pkg/suci/suci.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,16 @@ func profileB(input, supiType, privateKey string) (string, error) {
}

// x-coordinate is the shared key
decryptSharedKey, _ := elliptic.P256().ScalarMult(xUncompressed, yUncompressed, bHNPriv)
// fmt.Printf("deShared: %x\n", decryptSharedKey.Bytes())
decryptSharedKeyTmp, _ := elliptic.P256().ScalarMult(xUncompressed, yUncompressed, bHNPriv)
decryptSharedKey := FillFrontZero(decryptSharedKeyTmp, len(xUncompressed.Bytes()))
// fmt.Printf("deShared: %x\n", decryptSharedKey)
a3828162 marked this conversation as resolved.
Show resolved Hide resolved

decryptPublicKeyForKDF := decryptPublicKey
if uncompressed {
decryptPublicKeyForKDF = CompressKey(decryptPublicKey, yUncompressed)
}

kdfKey := AnsiX963KDF(decryptSharedKey.Bytes(), decryptPublicKeyForKDF, ProfileBEncKeyLen, ProfileBMacKeyLen,
kdfKey := AnsiX963KDF(decryptSharedKey, decryptPublicKeyForKDF, ProfileBEncKeyLen, ProfileBMacKeyLen,
ProfileBHashLen)
// fmt.Printf("kdfKey: %x\n", kdfKey)
decryptEncKey := kdfKey[:ProfileBEncKeyLen]
Expand All @@ -320,6 +321,16 @@ func profileB(input, supiType, privateKey string) (string, error) {
return calcSchemeResult(decryptPlainText, supiType), nil
}

func FillFrontZero(input *big.Int, length int) []byte {
if len(input.Bytes()) >= length {
return input.Bytes()
}
result := make([]byte, length)
inputBytes := input.Bytes()
copy(result[length-len(inputBytes):], input.Bytes())
return result
}

// suci-0(SUPI type: IMSI)-mcc-mnc-routingIndicator-protectionScheme-homeNetworkPublicKeyID-schemeOutput.
// TODO:
// suci-1(SUPI type: NAI)-homeNetworkID-routingIndicator-protectionScheme-homeNetworkPublicKeyID-schemeOutput.
Expand Down
6 changes: 6 additions & 0 deletions pkg/suci/suci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func TestToSupi(t *testing.T) {
expectedSupi: "",
expectedErr: fmt.Errorf("crypto/elliptic: attempted operation on invalid point"),
},
{
suci: "suci-0-001-01-0-2-2-03a7b1db2a9db9d44112b59d03d8243dc6089fd91d2ecb" +
"78f5d16298634682e94373888b22bdc9293d1681922e17",
expectedSupi: "imsi-001010123456789",
expectedErr: nil,
},
}
for i, tc := range testCases {
supi, err := ToSupi(tc.suci, suciProfiles)
Expand Down
Loading