Skip to content

Commit

Permalink
Clarify endianness in integer byte accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Dec 31, 2022
1 parent 475fd1e commit 8262d98
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Sources/HAP/Security/Cryptographer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Cryptographer {
tag: T) throws -> Data {
defer { decryptCount += 1 }

let nonce = try ChaChaPoly.Nonce(data: Data(count: 4) + decryptCount.bigEndian.bytes)
let nonce = try ChaChaPoly.Nonce(data: Data(count: 4) + decryptCount.littleEndianBytes)
let box = try ChaChaPoly.SealedBox(nonce: nonce, ciphertext: ciphertext, tag: tag)

return try ChaChaPoly.open(box, using: decryptKey, authenticating: lengthBytes)
Expand All @@ -53,8 +53,8 @@ class Cryptographer {
func encrypt(plaintext: ByteBuffer) throws -> Data {
defer { encryptCount += 1 }

let nonce = try ChaChaPoly.Nonce(data: Data(count: 4) + encryptCount.bigEndian.bytes)
let authenticationData = UInt16(plaintext.readableBytes).bigEndian.bytes
let nonce = try ChaChaPoly.Nonce(data: Data(count: 4) + encryptCount.littleEndianBytes)
let authenticationData = UInt16(plaintext.readableBytes).littleEndianBytes

let box = try ChaChaPoly.seal(plaintext.readableBytesView,
using: encryptKey,
Expand Down
13 changes: 13 additions & 0 deletions Sources/HAP/Utils/FixedWidthInteger+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

extension FixedWidthInteger {
/// Returns bytes in Big Endian or Network Byte Ordering.
public var bigEndianBytes: Data {
withUnsafeBytes(of: bigEndian, { Data(bytes: $0.baseAddress!, count: $0.count) })
}

/// Returns bytes in Little Endian.
public var littleEndianBytes: Data {
withUnsafeBytes(of: littleEndian, { Data(bytes: $0.baseAddress!, count: $0.count) })
}
}
29 changes: 0 additions & 29 deletions Sources/HAP/Utils/Integer+Data.swift

This file was deleted.

32 changes: 32 additions & 0 deletions Tests/HAPTests/FixedWithInteger+ExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@testable import HAP
import XCTest

final class FixedWidthIntegerExtensionTests: XCTestCase {
func testBigEndianBytes() throws {
XCTAssertEqual(UInt8(1).bigEndianBytes, Data([1]))
XCTAssertEqual(UInt16(1).bigEndianBytes, Data([0, 1]))
XCTAssertEqual(UInt32(1).bigEndianBytes, Data([0, 0, 0, 1]))
XCTAssertEqual(UInt64(1).bigEndianBytes, Data([0, 0, 0, 0, 0, 0, 0, 1]))
XCTAssertEqual(UInt16(1 << 8).bigEndianBytes, Data([1, 0]))
XCTAssertEqual(UInt32(1 << 24).bigEndianBytes, Data([1, 0, 0, 0]))
XCTAssertEqual(UInt64(1 << 56).bigEndianBytes, Data([1, 0, 0, 0, 0, 0, 0, 0]))
}

func testLittleEndianBytes() throws {
XCTAssertEqual(UInt8(1).littleEndianBytes, Data([1]))
XCTAssertEqual(UInt16(1).littleEndianBytes, Data([1, 0]))
XCTAssertEqual(UInt32(1).littleEndianBytes, Data([1, 0, 0, 0]))
XCTAssertEqual(UInt64(1).littleEndianBytes, Data([1, 0, 0, 0, 0, 0, 0, 0]))
XCTAssertEqual(UInt16(1 << 8).littleEndianBytes, Data([0, 1]))
XCTAssertEqual(UInt32(1 << 24).littleEndianBytes, Data([0, 0, 0, 1]))
XCTAssertEqual(UInt64(1 << 56).littleEndianBytes, Data([0, 0, 0, 0, 0, 0, 0, 1]))
}

func testBE() throws {
try XCTSkipUnless(UInt(16) == UInt(16).bigEndian, "Platform is LE")
}

func testLE() throws {
try XCTSkipIf(UInt(16) == UInt(16).bigEndian, "Platform is BE")
}
}

0 comments on commit 8262d98

Please sign in to comment.