Skip to content

Commit

Permalink
fix: suci profile encryptSharedKey front zero truncated
Browse files Browse the repository at this point in the history
  • Loading branch information
a3828162 committed Oct 18, 2024
1 parent 1e3614f commit a9d5f42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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)

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
5 changes: 5 additions & 0 deletions pkg/suci/suci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func TestToSupi(t *testing.T) {
expectedSupi: "",
expectedErr: fmt.Errorf("crypto/elliptic: attempted operation on invalid point"),
},
{
suci: "suci-0-001-01-0-2-2-03a7b1db2a9db9d44112b59d03d8243dc6089fd91d2ecb78f5d16298634682e94373888b22bdc9293d1681922e17",
expectedSupi: "imsi-001010123456789",
expectedErr: nil,
},
}
for i, tc := range testCases {
supi, err := ToSupi(tc.suci, suciProfiles)
Expand Down

0 comments on commit a9d5f42

Please sign in to comment.