mirror of
https://github.com/apple/swift-crypto
synced 2026-11-28 05:04:21 +00:00
## Motivation We would like to provide support for the P384-SHA384 Verifiable Oblivious Pseudorandom Function (VOPRF) as defined in [RFC 9497: VOPRF Protocol](https://www.rfc-editor.org/rfc/rfc9497.html#name-voprf-protocol). ## Modifications - Add protocols for some "ECToolbox" abstractions: `Group`, `GroupElement`, `GroupScalar`, `HashToGroup`. - Add implementations backed by BoringSSL. - Duplicate some utilities from Crypto into _CryptoExtras: hexadecimal bytes and I2OSP. - Add VOPRF API, rooted at `enum P384._VOPRF`. ## Results - New internal abstractions that make it easier to bring up new implementations. - New API for `OPRF(P-384, SHA-384)` in VOPRF mode. ## Tests - Test vectors for internal `HashToField` implementation. - Test vectors for internal VOPRF implementation. - Tests of the VOPRF public API.
31 lines
1.1 KiB
Swift
31 lines
1.1 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import Foundation
|
|
|
|
func I2OSP(value: Int, outputByteCount: Int) -> Data {
|
|
precondition(outputByteCount > 0, "Cannot I2OSP with no output length.")
|
|
precondition(value >= 0, "I2OSP requires a non-null value.")
|
|
|
|
let requiredBytes = Int(ceil(log2(Double(max(value, 1) + 1)) / 8))
|
|
precondition(outputByteCount >= requiredBytes)
|
|
|
|
var data = Data(repeating: 0, count: outputByteCount)
|
|
|
|
for i in (outputByteCount - requiredBytes)...(outputByteCount - 1) {
|
|
data[i] = UInt8(truncatingIfNeeded: (value >> (8 * (outputByteCount - 1 - i))))
|
|
}
|
|
|
|
return data
|
|
}
|