Skip to content

Commit

Permalink
Add multiple compressed/uncompressed key tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linouxis9 committed Feb 17, 2025
1 parent 9d6da8c commit fc1ce89
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pkg/suci/suci.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ func profileA(input, supiType, privateKey string) (string, error) {
return calcSchemeResult(decryptPlainText, supiType), nil
}

var (
InvalidPointError = fmt.Errorf("crypto/elliptic: attempted operation on invalid point")
)

func checkOnCurve(curve elliptic.Curve, x, y *big.Int) error {
// (0, 0) is the point at infinity by convention. It's ok to operate on it,
// although IsOnCurve is documented to return false for it. See Issue 37294.
Expand All @@ -249,7 +253,7 @@ func checkOnCurve(curve elliptic.Curve, x, y *big.Int) error {
}

if !curve.IsOnCurve(x, y) {
return fmt.Errorf("crypto/elliptic: attempted operation on invalid point")
return InvalidPointError
}

return nil
Expand Down
45 changes: 41 additions & 4 deletions pkg/suci/suci_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package suci

import (
"fmt"
"errors"
"testing"
)

Expand All @@ -15,9 +15,16 @@ func TestToSupi(t *testing.T) {
{
ProtectionScheme: "2", // Protect Scheme: Profile B
PrivateKey: "F1AB1074477EBCC7F554EA1C5FC368B1616730155E0041AC447D6301975FECDA",
// Uncompressed keys
PublicKey: "0472DA71976234CE833A6907425867B82E074D44EF907DFB4B3E21C1C2256EBCD" +
"15A7DED52FCBB097A4ED250E036C7B9C8C7004C4EEDC4F068CD7BF8D3F900E3B4",
},
{
ProtectionScheme: "2", // Protect Scheme: Profile B
PrivateKey: "F1AB1074477EBCC7F554EA1C5FC368B1616730155E0041AC447D6301975FECDA",
// Compressed keys
PublicKey: "0272DA71976234CE833A6907425867B82E074D44EF907DFB4B3E21C1C2256EBCD1",
},
}
testCases := []struct {
suci string
Expand Down Expand Up @@ -45,20 +52,50 @@ func TestToSupi(t *testing.T) {
suci: "suci-0-208-93-0-2-2-0434a66778799d52fedd9326db4b690d092e05c9ba0ace5b413da" +
"fc0a40aa28ee00a79f790fa4da6a2ece892423adb130dc1b30e270b7d0088bdd716b93894891d5221a74c810d6b9350cc067c76",
expectedSupi: "",
expectedErr: fmt.Errorf("crypto/elliptic: attempted operation on invalid point"),
expectedErr: InvalidPointError,
},
{
suci: "suci-0-001-01-0-2-2-03a7b1db2a9db9d44112b59d03d8243dc6089fd91d2ecb" +
"78f5d16298634682e94373888b22bdc9293d1681922e17",
expectedSupi: "imsi-001010123456789",
expectedErr: nil,
}, {
// Uncompressed Ephemeral Public Key + Compressed Home Public Key
suci: "suci-0-001-01-0-2-2-049AAB8376597021E855679A9778EA0B67396E68C66DF32C0F41E9ACCA2DA9B9D1D1F44EA1C" +
"87AA7478B954537BDE79951E748A43294A4F4CF86EAFF1789C9C81F46A33FC2716AC7DAE96AA30A4D",
expectedSupi: "imsi-00101001002086",
expectedErr: nil,
}, {
suci: "suci-0-208-93-0-2-3-039aab8376597021e855679a9778ea0b67396e68c66d" +
"f32c0f41e9acca2da9b9d146a33fc2716ac7dae96aa30a4d",
expectedSupi: "imsi-20893001002086",
expectedErr: nil,
},
{
suci: "suci-0-208-93-0-2-3-0434a66778799d52fedd9326db4b690d092e05c9ba0ace5b413da" +
"fc0a40aa28ee00a79f790fa4da6a2ece892423adb130dc1b30e270b7d0088bdd716b93894891d5221a74c810d6b9350cc067c76",
expectedSupi: "",
expectedErr: InvalidPointError,
},
{
suci: "suci-0-001-01-0-2-3-03a7b1db2a9db9d44112b59d03d8243dc6089fd91d2ecb" +
"78f5d16298634682e94373888b22bdc9293d1681922e17",
expectedSupi: "imsi-001010123456789",
expectedErr: nil,
},
{
// Uncompressed Ephemeral Public Key + Uncompressed Home Public Key
suci: "suci-0-001-01-0-2-3-049AAB8376597021E855679A9778EA0B67396E68C66DF32C0F41E9ACCA2DA9B9D1D1F44EA1C" +
"87AA7478B954537BDE79951E748A43294A4F4CF86EAFF1789C9C81F46A33FC2716AC7DAE96AA30A4D",
expectedSupi: "imsi-00101001002086",
expectedErr: nil,
},
}
for i, tc := range testCases {
supi, err := ToSupi(tc.suci, suciProfiles)
if err != nil {
if err.Error() != tc.expectedErr.Error() {
t.Errorf("TC%d fail: err[%s], expected[%s]\n", i, err, tc.expectedErr)
if !errors.Is(err, tc.expectedErr) {
t.Errorf("TC%d fail: err[%v], expected[%v]\n", i, err, tc.expectedErr)
}
} else if supi != tc.expectedSupi {
t.Errorf("TC%d fail: supi[%s], expected[%s]\n", i, supi, tc.expectedSupi)
Expand Down

0 comments on commit fc1ce89

Please sign in to comment.