| 1 | package personapi |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // The native apps sign DeviceProofMessage themselves (native/apple, |
| 13 | // EquipoKit's DeviceProof). This file is the server's word on those bytes: |
| 14 | // the Swift tests must reproduce each message exactly and verify each |
| 15 | // signature. Regenerate with EQUIPO_UPDATE_NATIVE_VECTOR=1. |
| 16 | const nativeProofVectorPath = "../../../native/apple/Tests/EquipoKitTests/Fixtures/device-proof-vectors.json" |
| 17 | |
| 18 | type nativeProofVector struct { |
| 19 | Name string `json:"name"` |
| 20 | Seed string `json:"seed"` |
| 21 | PublicKey string `json:"public_key"` |
| 22 | Instance string `json:"instance"` |
| 23 | ID string `json:"id"` |
| 24 | Challenge string `json:"challenge"` |
| 25 | Nonce string `json:"nonce"` |
| 26 | Message string `json:"message"` |
| 27 | Signature string `json:"signature"` |
| 28 | } |
| 29 | |
| 30 | func nativeProofVectors() []nativeProofVector { |
| 31 | enc := base64.RawURLEncoding |
| 32 | challenge := func(instance string) string { |
| 33 | b, _ := json.Marshal([]string{instance, "Q4J7XRZ2M3KP6TBN"}) |
| 34 | return enc.EncodeToString(b) |
| 35 | } |
| 36 | in := []nativeProofVector{ |
| 37 | {Name: "plain", Instance: "inst_7f3a9c", ID: "dev_01JD5Z4Q2X", Nonce: "nPq-7_Rs0tUvWxYz12345A"}, |
| 38 | // Go escapes <, > and & and leaves / alone; Swift's encoders differ on |
| 39 | // both, so the native side must build the array itself. |
| 40 | {Name: "escapes", Instance: "a/b<c>&d é
", ID: "dev_\"quoted\"\\", Nonce: "n/o<n>&ce"}, |
| 41 | // Since Go 1.22, \b and \f are written as short escapes; other control |
| 42 | // characters as \u00XX. |
| 43 | {Name: "controls", Instance: "a\bb\fc\x01d\te\nf", ID: "dev_\x7f", Nonce: "n\rx"}, |
| 44 | } |
| 45 | for i := range in { |
| 46 | seed := make([]byte, ed25519.SeedSize) |
| 47 | for j := range seed { |
| 48 | seed[j] = byte(i*31 + j) |
| 49 | } |
| 50 | key := ed25519.NewKeyFromSeed(seed) |
| 51 | p := PersonDeviceProof{ID: in[i].ID, Challenge: challenge(in[i].Instance), Nonce: in[i].Nonce} |
| 52 | msg := DeviceProofMessage(in[i].Instance, p) |
| 53 | in[i].Seed = enc.EncodeToString(seed) |
| 54 | in[i].PublicKey = enc.EncodeToString(key.Public().(ed25519.PublicKey)) |
| 55 | in[i].Challenge = p.Challenge |
| 56 | in[i].Message = string(msg) |
| 57 | in[i].Signature = enc.EncodeToString(ed25519.Sign(key, msg)) |
| 58 | } |
| 59 | return in |
| 60 | } |
| 61 | |
| 62 | func TestNativeDeviceProofVectors(t *testing.T) { |
| 63 | want := nativeProofVectors() |
| 64 | if os.Getenv("EQUIPO_UPDATE_NATIVE_VECTOR") == "1" { |
| 65 | b, err := json.MarshalIndent(want, "", " ") |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if err := os.WriteFile(filepath.FromSlash(nativeProofVectorPath), append(b, '\n'), 0o644); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | } |
| 73 | raw, err := os.ReadFile(filepath.FromSlash(nativeProofVectorPath)) |
| 74 | if err != nil { |
| 75 | t.Fatalf("the native proof vectors are missing; run with EQUIPO_UPDATE_NATIVE_VECTOR=1: %v", err) |
| 76 | } |
| 77 | var got []nativeProofVector |
| 78 | if err := json.Unmarshal(raw, &got); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | if len(got) != len(want) { |
| 82 | t.Fatalf("%d vectors, want %d", len(got), len(want)) |
| 83 | } |
| 84 | for i := range want { |
| 85 | if got[i] != want[i] { |
| 86 | t.Fatalf("vector %q drifted from DeviceProofMessage:\n got %+v\nwant %+v", want[i].Name, got[i], want[i]) |
| 87 | } |
| 88 | pub, _ := base64.RawURLEncoding.DecodeString(got[i].PublicKey) |
| 89 | sig, _ := base64.RawURLEncoding.DecodeString(got[i].Signature) |
| 90 | p := PersonDeviceProof{ID: got[i].ID, Challenge: got[i].Challenge, Nonce: got[i].Nonce} |
| 91 | if !ed25519.Verify(pub, DeviceProofMessage(got[i].Instance, p), sig) { |
| 92 | t.Fatalf("vector %q does not verify", got[i].Name) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |