-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof-of-concept for MAC logging anonymization
- Loading branch information
Showing
12 changed files
with
139 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) halo https://github.com/halo/LinkLiar | ||
// SPDX-License-Identifier: MIT | ||
|
||
import XCTest | ||
@testable import LinkLiar | ||
|
||
class MACAnonymizerTest: XCTestCase { | ||
let userDefaultsSuiteName = "io.github.halo.LinkLiar.MACAnonymizerTest" | ||
|
||
func testAnonymization() { | ||
let input = MAC("00:01:02:00:aa:ff")! | ||
let userDefaults = UserDefaults(suiteName: userDefaultsSuiteName)! | ||
userDefaults.set("00:03:05:00:01:01", forKey: "seed") | ||
let output = MACAnonymizer.anonymize(input, stubUserDefaults: userDefaults) | ||
|
||
XCTAssertEqual("00:04:07:00:ab:f0", output) | ||
} | ||
|
||
func testAnonymizationModulo() { | ||
let input = MAC("ff:ff:ff:ff:ff:ff")! | ||
let userDefaults = UserDefaults(suiteName: userDefaultsSuiteName)! | ||
userDefaults.set("11:00:00:00:10:01", forKey: "seed") | ||
let output = MACAnonymizer.anonymize(input, stubUserDefaults: userDefaults) | ||
|
||
XCTAssertEqual("00:ff:ff:ff:0f:f0", output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) halo https://github.com/halo/LinkLiar | ||
// SPDX-License-Identifier: MIT | ||
|
||
import Foundation | ||
|
||
/// Checks a potential MAC address for validity and normalizes it. | ||
/// | ||
struct MACAnonymizer { | ||
// MARK: - Class Methods | ||
|
||
static func anonymize(_ input: MAC, stubUserDefaults: UserDefaults = .standard) -> String { | ||
self.init(input, userDefaults: stubUserDefaults).anonymous | ||
} | ||
|
||
// MARK: - Private Class Methods | ||
|
||
private init(_ input: MAC, userDefaults: UserDefaults = .standard) { | ||
self.input = input | ||
self.userDefaults = userDefaults | ||
} | ||
|
||
// MARK: - Instance Properties | ||
|
||
var anonymous: String { | ||
guard let seedAddress = MAC(seed) else { | ||
return "??:??:??:??:??:??" | ||
} | ||
|
||
let plaintext = input.integers | ||
let key = seedAddress.integers | ||
let ciphertext = plaintext.enumerated().map { ($1 + key[$0]) % 16 } | ||
let anonmousAddress = ciphertext.map { String($0, radix: 16) }.joined() | ||
|
||
guard let output = MAC(anonmousAddress) else { | ||
return "??:??:??:??:??:??" | ||
} | ||
|
||
return output.address | ||
} | ||
|
||
/// Fetches the system-wide persisted anonymization seed. | ||
/// Persists a new one if there is none. | ||
/// | ||
private var seed: String { | ||
if let knownSeed = userDefaults.string(forKey: seedKey) { | ||
return knownSeed | ||
} else { | ||
let newSeed = generateSeed() | ||
userDefaults.setValue(newSeed, forKey: seedKey) | ||
return newSeed | ||
} | ||
} | ||
|
||
/// Generates a completely random MAC address. | ||
/// | ||
func generateSeed() -> String { | ||
[ | ||
String(Int.random(in: 0..<256), radix: 16, uppercase: false), | ||
String(Int.random(in: 0..<256), radix: 16, uppercase: false), | ||
String(Int.random(in: 0..<256), radix: 16, uppercase: false), | ||
String(Int.random(in: 0..<256), radix: 16, uppercase: false), | ||
String(Int.random(in: 0..<256), radix: 16, uppercase: false), | ||
String(Int.random(in: 0..<256), radix: 16, uppercase: false) | ||
].joined() | ||
} | ||
|
||
private let input: MAC | ||
private let userDefaults: UserDefaults | ||
private let seedKey = "seed" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters