From df18fe6080e6abe3f86c85b40cb51b8dfd282632 Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Mon, 25 Mar 2024 06:40:58 -0700 Subject: [PATCH] Refactored integer casting to use built-in functions --- .../Ceremonies/Shared/AuthenticatorData.swift | 4 +-- Sources/WebAuthn/Helpers/ByteCasting.swift | 16 +++++++++ Sources/WebAuthn/Helpers/Numbers+Bytes.swift | 34 ------------------- 3 files changed, 18 insertions(+), 36 deletions(-) delete mode 100644 Sources/WebAuthn/Helpers/Numbers+Bytes.swift diff --git a/Sources/WebAuthn/Ceremonies/Shared/AuthenticatorData.swift b/Sources/WebAuthn/Ceremonies/Shared/AuthenticatorData.swift index d0f6771..41cc250 100644 --- a/Sources/WebAuthn/Ceremonies/Shared/AuthenticatorData.swift +++ b/Sources/WebAuthn/Ceremonies/Shared/AuthenticatorData.swift @@ -36,7 +36,7 @@ extension AuthenticatorData { let relyingPartyIDHash = Array(bytes[..<32]) let flags = AuthenticatorFlags(bytes[32]) - let counter: UInt32 = Data(bytes[33..<37]).toInteger(endian: .big) + let counter = UInt32(bigEndianBytes: bytes[33..<37]) var remainingCount = bytes.count - minAuthDataLength @@ -90,7 +90,7 @@ extension AuthenticatorData { /// **credentialIdLength** (2): Byte length L of credentialId, 16-bit unsigned big-endian integer. Value MUST be ≤ 1023. let idLengthBytes = data[53..<55] // Length is 2 bytes let idLengthData = Data(idLengthBytes) - let idLength: UInt16 = idLengthData.toInteger(endian: .big) + let idLength = UInt16(bigEndianBytes: idLengthData) guard idLength <= 1023 else { throw WebAuthnError.credentialIDTooLong } diff --git a/Sources/WebAuthn/Helpers/ByteCasting.swift b/Sources/WebAuthn/Helpers/ByteCasting.swift index 210e7d6..611f02d 100644 --- a/Sources/WebAuthn/Helpers/ByteCasting.swift +++ b/Sources/WebAuthn/Helpers/ByteCasting.swift @@ -31,3 +31,19 @@ extension BidirectionalCollection where Element == UInt8 { return result } } + +extension FixedWidthInteger { + /// Initialize a fixed width integer from a contiguous sequence of Bytes representing a big endian type. + /// - Parameter bigEndianBytes: The Bytes to interpret as a big endian integer. + @inlinable + init(bigEndianBytes: some BidirectionalCollection) { + self.init(bigEndian: bigEndianBytes.casting()) + } + + /// Initialize a fixed width integer from a contiguous sequence of Bytes representing a little endian type. + /// - Parameter bigEndianBytes: The Bytes to interpret as a little endian integer. + @inlinable + init(littleEndianBytes: some BidirectionalCollection) { + self.init(littleEndian: littleEndianBytes.casting()) + } +} diff --git a/Sources/WebAuthn/Helpers/Numbers+Bytes.swift b/Sources/WebAuthn/Helpers/Numbers+Bytes.swift deleted file mode 100644 index d02ee1f..0000000 --- a/Sources/WebAuthn/Helpers/Numbers+Bytes.swift +++ /dev/null @@ -1,34 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the WebAuthn Swift open source project -// -// Copyright (c) 2022 the WebAuthn Swift project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of WebAuthn Swift project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// - -import Foundation - -public enum Endian { - case big, little -} - -protocol IntegerTransform: Sequence where Element: FixedWidthInteger { - func toInteger(endian: Endian) -> I -} - -extension IntegerTransform { - func toInteger(endian: Endian) -> I { - // swiftlint:disable:next identifier_name - let f = { (accum: I, next: Element) in accum &<< next.bitWidth | I(next) } - return endian == .big ? reduce(0, f) : reversed().reduce(0, f) - } -} - -extension Data: IntegerTransform {} -extension Array: IntegerTransform where Element: FixedWidthInteger {}