Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for visionOS #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Sources/Cryptor/Digest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import Foundation

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
import CommonCrypto
#elseif os(Linux)
import OpenSSL
Expand Down Expand Up @@ -87,56 +87,56 @@ public class Digest: Updatable {
switch algorithm {

case .md2:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_MD2_CTX>(initializer:CC_MD2_Init, updater:CC_MD2_Update, finalizer:CC_MD2_Final, length:CC_MD2_DIGEST_LENGTH)
#elseif os(Linux)
fatalError("MD2 digest not supported by OpenSSL")
#endif

case .md4:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_MD4_CTX>(initializer:CC_MD4_Init, updater:CC_MD4_Update, finalizer:CC_MD4_Final, length:CC_MD4_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<MD4_CTX>(initializer:MD4_Init, updater:MD4_Update, finalizer:MD4_Final, length:MD4_DIGEST_LENGTH)
#endif

case .md5:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_MD5_CTX>(initializer:CC_MD5_Init, updater:CC_MD5_Update, finalizer:CC_MD5_Final, length:CC_MD5_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<MD5_CTX>(initializer:MD5_Init, updater:MD5_Update, finalizer:MD5_Final, length:MD5_DIGEST_LENGTH)
#endif

case .sha1:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_SHA1_CTX>(initializer:CC_SHA1_Init, updater:CC_SHA1_Update, finalizer:CC_SHA1_Final, length:CC_SHA1_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<SHA_CTX>(initializer:SHA1_Init, updater:SHA1_Update, finalizer:SHA1_Final, length:SHA_DIGEST_LENGTH)
#endif

case .sha224:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_SHA256_CTX>(initializer:CC_SHA224_Init, updater:CC_SHA224_Update, finalizer:CC_SHA224_Final, length:CC_SHA224_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<SHA256_CTX>(initializer:SHA224_Init, updater:SHA224_Update, finalizer:SHA224_Final, length:SHA224_DIGEST_LENGTH)
#endif

case .sha256:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_SHA256_CTX>(initializer:CC_SHA256_Init, updater:CC_SHA256_Update, finalizer:CC_SHA256_Final, length:CC_SHA256_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<SHA256_CTX>(initializer: SHA256_Init, updater:SHA256_Update, finalizer:SHA256_Final, length:SHA256_DIGEST_LENGTH)
#endif

case .sha384:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_SHA512_CTX>(initializer:CC_SHA384_Init, updater:CC_SHA384_Update, finalizer:CC_SHA384_Final, length:CC_SHA384_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<SHA512_CTX>(initializer:SHA384_Init, updater:SHA384_Update, finalizer:SHA384_Final, length:SHA384_DIGEST_LENGTH)
#endif

case .sha512:
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
engine = DigestEngineCC<CC_SHA512_CTX>(initializer:CC_SHA512_Init, updater:CC_SHA512_Update, finalizer:CC_SHA512_Final, length:CC_SHA512_DIGEST_LENGTH)
#elseif os(Linux)
engine = DigestEngineCC<SHA512_CTX>(initializer:SHA512_Init, updater:SHA512_Update, finalizer:SHA512_Final, length:SHA512_DIGEST_LENGTH)
Expand Down
30 changes: 15 additions & 15 deletions Sources/Cryptor/HMAC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import Foundation

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
import CommonCrypto
#elseif os(Linux)
import OpenSSL
Expand Down Expand Up @@ -51,8 +51,8 @@ public class HMAC: Updatable {
/// Secure Hash Algorithm 2 512-bit
case sha512

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

static let fromNative: [CCHmacAlgorithm: Algorithm] = [
CCHmacAlgorithm(kCCHmacAlgSHA1): .sha1,
CCHmacAlgorithm(kCCHmacAlgSHA1): .md5,
Expand Down Expand Up @@ -114,8 +114,8 @@ public class HMAC: Updatable {
///
public func digestLength() -> Int {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

switch self {

case .sha1:
Expand Down Expand Up @@ -156,8 +156,8 @@ public class HMAC: Updatable {
}

/// Context
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

typealias Context = UnsafeMutablePointer<CCHmacContext>

#elseif os(Linux)
Expand Down Expand Up @@ -189,7 +189,7 @@ public class HMAC: Updatable {
init(using algorithm: Algorithm, keyBuffer: UnsafeRawPointer, keyByteCount: Int) {

self.algorithm = algorithm
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
CCHmacInit(context, algorithm.nativeValue(), keyBuffer, size_t(keyByteCount))
#elseif os(Linux)
HMAC_Init_wrapper(context, keyBuffer, Int32(keyByteCount), .make(optional: algorithm.nativeValue()))
Expand All @@ -207,7 +207,7 @@ public class HMAC: Updatable {

self.algorithm = algorithm
#if swift(>=5.0)
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
key.withUnsafeBytes() {
CCHmacInit(context, algorithm.nativeValue(), $0.baseAddress, size_t(key.count))
}
Expand All @@ -217,7 +217,7 @@ public class HMAC: Updatable {
}
#endif
#else
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
key.withUnsafeBytes() { (buffer: UnsafePointer<UInt8>) in
CCHmacInit(context, algorithm.nativeValue(), buffer, size_t(key.count))
}
Expand All @@ -239,7 +239,7 @@ public class HMAC: Updatable {
public init(using algorithm: Algorithm, key: NSData) {

self.algorithm = algorithm
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
CCHmacInit(context, algorithm.nativeValue(), key.bytes, size_t(key.length))
#elseif os(Linux)
HMAC_Init_wrapper(context, key.bytes, Int32(key.length), .make(optional: algorithm.nativeValue()))
Expand All @@ -256,7 +256,7 @@ public class HMAC: Updatable {
public init(using algorithm: Algorithm, key: [UInt8]) {

self.algorithm = algorithm
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
CCHmacInit(context, algorithm.nativeValue(), key, size_t(key.count))
#elseif os(Linux)
HMAC_Init_wrapper(context, key, Int32(key.count), .make(optional: algorithm.nativeValue()))
Expand All @@ -274,7 +274,7 @@ public class HMAC: Updatable {
public init(using algorithm: Algorithm, key: String) {

self.algorithm = algorithm
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
CCHmacInit(context, algorithm.nativeValue(), key, size_t(key.lengthOfBytes(using: String.Encoding.utf8)))
#elseif os(Linux)
HMAC_Init_wrapper(context, key, Int32(key.utf8.count), .make(optional: algorithm.nativeValue()))
Expand Down Expand Up @@ -307,7 +307,7 @@ public class HMAC: Updatable {
///
public func update(from buffer: UnsafeRawPointer, byteCount: size_t) -> Self? {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
CCHmacUpdate(context, buffer, byteCount)
#elseif os(Linux)
HMAC_Update(context, buffer.assumingMemoryBound(to: UInt8.self), byteCount)
Expand All @@ -323,7 +323,7 @@ public class HMAC: Updatable {
public func final() -> [UInt8] {

var hmac = Array<UInt8>(repeating: 0, count:algorithm.digestLength())
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
CCHmacFinal(context, &hmac)
#elseif os(Linux)
var length: UInt32 = 0
Expand Down
12 changes: 6 additions & 6 deletions Sources/Cryptor/KeyDerivation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import Foundation

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
import CommonCrypto
#elseif os(Linux)
import OpenSSL
Expand Down Expand Up @@ -46,7 +46,7 @@ public class PBKDF {
/// Secure Hash Algorithm 2 512-bit
case sha512

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
/// Return the OS native value
func nativeValue() -> CCPseudoRandomAlgorithm {

Expand Down Expand Up @@ -103,7 +103,7 @@ public class PBKDF {
///
public class func calibrate(passwordLength: Int, saltLength: Int, algorithm: PseudoRandomAlgorithm, derivedKeyLength: Int, msec: UInt32) -> UInt {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
return UInt(CCCalibratePBKDF(CCPBKDFAlgorithm(kCCPBKDF2), passwordLength, saltLength, algorithm.nativeValue(), derivedKeyLength, msec))
#elseif os(Linux)
// Value as per RFC 2898.
Expand All @@ -127,7 +127,7 @@ public class PBKDF {
public class func deriveKey(fromPassword password: String, salt: String, prf: PseudoRandomAlgorithm, rounds: uint, derivedKeyLength: UInt) throws -> [UInt8] {

var derivedKey = Array<UInt8>(repeating: 0, count:Int(derivedKeyLength))
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
let status: Int32 = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), password, password.utf8.count, salt, salt.utf8.count, prf.nativeValue(), rounds, &derivedKey, derivedKey.count)
if status != Int32(kCCSuccess) {

Expand Down Expand Up @@ -159,7 +159,7 @@ public class PBKDF {
public class func deriveKey(fromPassword password: String, salt: [UInt8], prf: PseudoRandomAlgorithm, rounds: uint, derivedKeyLength: UInt) throws -> [UInt8] {

var derivedKey = Array<UInt8>(repeating: 0, count:Int(derivedKeyLength))
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
let status: Int32 = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), password, password.utf8.count, salt, salt.count, prf.nativeValue(), rounds, &derivedKey, derivedKey.count)
if status != Int32(kCCSuccess) {

Expand Down Expand Up @@ -193,7 +193,7 @@ public class PBKDF {
///
public class func deriveKey(fromPassword password: UnsafePointer<Int8>, passwordLen: Int, salt: UnsafePointer<UInt8>, saltLen: Int, prf: PseudoRandomAlgorithm, rounds: uint, derivedKey: UnsafeMutablePointer<UInt8>, derivedKeyLen: Int) throws {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
let status: Int32 = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), password, passwordLen, salt, saltLen, prf.nativeValue(), rounds, derivedKey, derivedKeyLen)
if status != Int32(kCCSuccess) {

Expand Down
4 changes: 2 additions & 2 deletions Sources/Cryptor/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import Foundation

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
import CommonCrypto
#elseif os(Linux)
import OpenSSL
Expand All @@ -41,7 +41,7 @@ public class Random {
///
public class func generate(bytes: UnsafeMutablePointer<UInt8>, byteCount: Int) -> RNGStatus {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
let statusCode = CCRandomGenerateBytes(bytes, byteCount)
guard let status = Status(rawValue: statusCode) else {
fatalError("CCRandomGenerateBytes returned unexpected status code: \(statusCode)")
Expand Down
4 changes: 2 additions & 2 deletions Sources/Cryptor/Status.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import Foundation

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
import CommonCrypto
#elseif os(Linux)
import OpenSSL
#endif

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
///
/// Links the native CommonCryptoStatus enumeration to Swift versions.
///
Expand Down
40 changes: 20 additions & 20 deletions Sources/Cryptor/StreamCryptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import Foundation

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)
import CommonCrypto
#elseif os(Linux)
import OpenSSL
Expand Down Expand Up @@ -74,8 +74,8 @@ public class StreamCryptor {
/// Decrypting
case decrypt

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

/// Convert to native `CCOperation`
func nativeValue() -> CCOperation {

Expand Down Expand Up @@ -185,10 +185,10 @@ public class StreamCryptor {
}

/// No options
public static let none = Options(rawValue: 0)

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
public static let none = Options([])

#if canImport(CommonCrypto)

/// Use padding. Needed unless the input is a integral number of blocks long.
public static var pkcs7Padding = Options(rawValue:kCCOptionPKCS7Padding)

Expand Down Expand Up @@ -285,8 +285,8 @@ public class StreamCryptor {
}
}

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

/// Native, CommonCrypto constant for algorithm.
func nativeValue() -> CCAlgorithm {

Expand Down Expand Up @@ -389,8 +389,8 @@ public class StreamCryptor {
///
func validKeySize() -> ValidKeySize {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

switch self {

case .aes, .aes128, .aes192, .aes256:
Expand Down Expand Up @@ -478,8 +478,8 @@ public class StreamCryptor {
///
private var haveContext: Bool = false

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

/// CommonCrypto Context
private var context = UnsafeMutablePointer<CCCryptorRef?>.allocate(capacity: 1)

Expand Down Expand Up @@ -522,8 +522,8 @@ public class StreamCryptor {
throw CryptorError.invalidIVSizeOrLength
}

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

let rawStatus = CCCryptorCreate(operation.nativeValue(), algorithm.nativeValue(), CCOptions(options.rawValue), keyBuffer, keyByteCount, ivBuffer, self.context)

if let status = Status.fromRaw(status: rawStatus) {
Expand Down Expand Up @@ -644,8 +644,8 @@ public class StreamCryptor {
return
}

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

// Ensure we've got a context before attempting to get rid of it...
if self.context.pointee == nil {
return
Expand Down Expand Up @@ -798,7 +798,7 @@ public class StreamCryptor {

if self.status == .success {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

let rawStatus = CCCryptorUpdate(self.context.pointee, bufferIn, byteCountIn, bufferOut, byteCapacityOut, &byteCountOut)
if let status = Status.fromRaw(status: rawStatus) {
Expand Down Expand Up @@ -864,8 +864,8 @@ public class StreamCryptor {

if self.status == Status.success {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

let rawStatus = CCCryptorFinal(self.context.pointee, bufferOut, byteCapacityOut, &byteCountOut)
if let status = Status.fromRaw(status: rawStatus) {
self.status = status
Expand Down Expand Up @@ -921,7 +921,7 @@ public class StreamCryptor {
///
public func getOutputLength(inputByteCount: Int, isFinal: Bool = false) -> Int {

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(CommonCrypto)

return CCCryptorGetOutputLength(self.context.pointee, inputByteCount, isFinal)

Expand Down
Loading