|
| 1 | +// Firmware to advertise a FindMy compatible device aka AirTag |
| 2 | +// see https://github.com/biemster/FindMy for more information. |
| 3 | +// |
| 4 | +// To build: |
| 5 | +// tinygo flash -target nano-rp2040 -ldflags="-X main.DerivationKey='SGVsbG8sIFdvcmxkIQ=='" . |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "crypto/elliptic" |
| 10 | + "machine" |
| 11 | + "crypto/sha256" |
| 12 | + "encoding/base64" |
| 13 | + "encoding/binary" |
| 14 | + "io" |
| 15 | + "math/big" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/hybridgroup/go-haystack/lib/findmy" |
| 19 | + "golang.org/x/crypto/hkdf" |
| 20 | + "tinygo.org/x/bluetooth" |
| 21 | +) |
| 22 | + |
| 23 | +var adapter = bluetooth.DefaultAdapter |
| 24 | + |
| 25 | +func main() { |
| 26 | + // wait for USB serial to be available |
| 27 | + time.Sleep(2 * time.Second) |
| 28 | + |
| 29 | + // Get and increment index. Ideally this would be done at the end of 15 |
| 30 | + // minutes. However writing seems to fail once advertising has started. |
| 31 | + derivIndex := getIndex() |
| 32 | + derivIndex++ |
| 33 | + must("write new index to file", writeIndex(derivIndex)) |
| 34 | + println("derivation secret is", DerivationSecret) |
| 35 | + println("derivation index is", derivIndex) |
| 36 | + |
| 37 | + priv, pub, err := getKeyData(derivIndex) |
| 38 | + must("get key data", err) |
| 39 | + println("private key is", base64.StdEncoding.EncodeToString(priv)) |
| 40 | + println("public key is", base64.StdEncoding.EncodeToString(pub)) |
| 41 | + |
| 42 | + opts := bluetooth.AdvertisementOptions{ |
| 43 | + AdvertisementType: bluetooth.AdvertisingTypeNonConnInd, |
| 44 | + Interval: bluetooth.NewDuration(1285000 * time.Microsecond), // 1285ms |
| 45 | + ManufacturerData: []bluetooth.ManufacturerDataElement{findmy.NewData(pub)}, |
| 46 | + } |
| 47 | + |
| 48 | + must("enable BLE stack", adapter.Enable()) |
| 49 | + |
| 50 | + // Set the address to the first 6 bytes of the public key. |
| 51 | + adapter.SetRandomAddress(bluetooth.MAC{pub[5], pub[4], pub[3], pub[2], pub[1], pub[0] | 0xC0}) |
| 52 | + |
| 53 | + println("configure advertising...") |
| 54 | + adv := adapter.DefaultAdvertisement() |
| 55 | + must("config adv", adv.Configure(opts)) |
| 56 | + |
| 57 | + println("start advertising...") |
| 58 | + must("start adv", adv.Start()) |
| 59 | + |
| 60 | + boot := time.Now() |
| 61 | + address, _ := adapter.Address() |
| 62 | + for uptime := 0; ; uptime++ { |
| 63 | + if uptime%100 == 0 { |
| 64 | + println("FindMy device using", address.MAC.String(), "uptime", uptime) |
| 65 | + } |
| 66 | + time.Sleep(time.Second) |
| 67 | + if time.Since(boot) > 15*time.Minute { |
| 68 | + machine.CPUReset() |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const keySize = 28 |
| 74 | + |
| 75 | +var curve = elliptic.P224() |
| 76 | + |
| 77 | +func getKeyData(i uint64) ([]byte, []byte, error) { |
| 78 | + secret, err := base64.StdEncoding.DecodeString(DerivationSecret) |
| 79 | + if err != nil { |
| 80 | + return nil, nil, err |
| 81 | + } |
| 82 | + |
| 83 | + info := make([]byte, 8) |
| 84 | + binary.LittleEndian.PutUint64(info, i) |
| 85 | + r := hkdf.New(sha256.New, secret, nil, info) |
| 86 | + for { |
| 87 | + priv := make([]byte, keySize) |
| 88 | + if _, err := io.ReadFull(r, priv); err != nil { |
| 89 | + return nil, nil, err |
| 90 | + } |
| 91 | + |
| 92 | + privInt := new(big.Int).SetBytes(priv) |
| 93 | + n := curve.Params().N |
| 94 | + if privInt.Sign() > 0 && privInt.Cmp(n) < 0 { |
| 95 | + xInt, _ := curve.ScalarBaseMult(priv) |
| 96 | + xBytes := xInt.Bytes() |
| 97 | + x := make([]byte, keySize) |
| 98 | + copy(x[keySize-len(xBytes):], xBytes) |
| 99 | + return priv, x, nil |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// must calls a function and fails if an error occurs. |
| 105 | +func must(action string, err error) { |
| 106 | + if err != nil { |
| 107 | + fail("failed to " + action + ": " + err.Error()) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// fail prints a message over and over forever. |
| 112 | +func fail(msg string) { |
| 113 | + for { |
| 114 | + println(msg) |
| 115 | + time.Sleep(time.Second) |
| 116 | + } |
| 117 | +} |
0 commit comments