mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
+6








431013afaf
This change updates Crypto to WWDC25 equivalent code. Each of these changes were individually merged to the WWDC25 branch. This includes: - #359 - #363 - #365 - #367 - #368 - #397 - #403 - #410 - #407 - #401 - #415 --------- Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com> Co-authored-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Rick Newton-Rogers <rnro@apple.com> Co-authored-by: Raphael <josephnoir@users.noreply.github.com> Co-authored-by: Evan Wilde <ewilde@apple.com> Co-authored-by: Tim Condon <0xTim@users.noreply.github.com> Co-authored-by: Gus Cairo <gus.cairo@apple.com> Co-authored-by: George Barnett <gbarnett@apple.com> Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com> Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com> Co-authored-by: aryan-25 <aryan_shah@apple.com> Co-authored-by: Gwynne Raskind <gwynne@vapor.codes> Co-authored-by: 3405691582 <dsk@google.com> Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
101 lines
3.5 KiB
Swift
101 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) 2024 Apple Inc. and the SwiftCrypto project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#if canImport(FoundationEssentials)
|
|
#if os(Windows)
|
|
import ucrt
|
|
#elseif canImport(Darwin)
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
import Glibc
|
|
#elseif canImport(Musl)
|
|
import Musl
|
|
#elseif canImport(Android)
|
|
import Android
|
|
#elseif canImport(WASILibc)
|
|
import WASILibc
|
|
#endif
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
import Crypto
|
|
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
extension Data {
|
|
static func ^ (left: Data, right: Data) -> Data {
|
|
precondition(left.count == right.count)
|
|
var result = Data()
|
|
result.reserveCapacity(left.count)
|
|
for value in zip(left, right) {
|
|
result.append(value.0 ^ value.1)
|
|
}
|
|
return result
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
enum Hash2FieldErrors: Error {
|
|
case outputSizeIsTooLarge
|
|
}
|
|
|
|
/// HashToField hashes a byte string msg of arbitrary length into one or more elements of a finite field
|
|
@available(macOS 10.15, iOS 13.2, tvOS 13.2, watchOS 6.1, macCatalyst 13.2, visionOS 1.2, *)
|
|
struct HashToField<C: SupportedCurveDetailsImpl> {
|
|
static func expandMessageXMD(_ msg: Data, DST: Data, outputByteCount L: Int) throws -> Data {
|
|
typealias H = C.H
|
|
let digestByteCount = H.Digest.byteCount
|
|
|
|
let ell = Int(ceil(Double(L) / Double(digestByteCount)))
|
|
|
|
if ell > 255 {
|
|
throw Hash2FieldErrors.outputSizeIsTooLarge
|
|
}
|
|
|
|
let DST_prime = DST + I2OSP(value: DST.count, outputByteCount: 1)
|
|
let z_pad = Data(repeating: 0, count: H.blockByteCount)
|
|
let l_i_b_str = I2OSP(value: L, outputByteCount: 2)
|
|
let msg_prime = z_pad + msg + l_i_b_str + I2OSP(value: 0, outputByteCount: 1) + DST_prime
|
|
|
|
let b0 = Data(H.hash(data: msg_prime))
|
|
var bis = Data()
|
|
|
|
for i in 1...ell {
|
|
let chaining = ((i == 1) ? b0 : (b0 ^ bis.suffix(digestByteCount)))
|
|
bis.append(Data(H.hash(data: (chaining + I2OSP(value: i, outputByteCount: 1) + DST_prime))))
|
|
}
|
|
|
|
return Data(bis.prefix(L))
|
|
}
|
|
|
|
static func hashToField(_ data: Data, outputElementCount: Int, dst: Data, outputSize L: Int, reductionIsModOrder: Bool) throws -> [GroupImpl<C>.Scalar] {
|
|
precondition(outputElementCount > 0)
|
|
let byteCount = outputElementCount * L
|
|
let uniformBytes = try expandMessageXMD(data,
|
|
DST: dst,
|
|
outputByteCount: byteCount)
|
|
|
|
var u_i = [GroupImpl<C>.Scalar]()
|
|
u_i.reserveCapacity(outputElementCount)
|
|
|
|
for i in 0..<outputElementCount {
|
|
let offset = i * L
|
|
let tv = uniformBytes.subdata(in: offset..<(offset + L))
|
|
u_i.append(try GroupImpl<C>.Scalar(bytes: tv, reductionIsModOrder: reductionIsModOrder))
|
|
}
|
|
|
|
precondition(u_i.count == outputElementCount)
|
|
return u_i
|
|
}
|
|
}
|