From 6b8de0bc95c45f6df58371707ae19ab14f5b0eb4 Mon Sep 17 00:00:00 2001 From: TerryHu Date: Tue, 22 Oct 2019 18:20:26 +0800 Subject: [PATCH] Fix up CTR mode wrong options --- IDZSwiftCommonCrypto/StreamCryptor.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/IDZSwiftCommonCrypto/StreamCryptor.swift b/IDZSwiftCommonCrypto/StreamCryptor.swift index e0cccce..747c099 100644 --- a/IDZSwiftCommonCrypto/StreamCryptor.swift +++ b/IDZSwiftCommonCrypto/StreamCryptor.swift @@ -458,8 +458,15 @@ open class StreamCryptor guard !mode.requiresInitializationVector() || algorithm.blockSize() == ivByteCount else { fatalError("FATAL_ERROR: Invalid initialization vector size.") } + + let modeOptions: CCModeOptions = { + if case .CTR = mode { + return CCModeOptions(kCCModeOptionCTR_BE) + } + return CCModeOptions(0) + }() - let rawStatus = CCCryptorCreateWithMode(operation.nativeValue(), mode.nativeValue(), algorithm.nativeValue(), padding.nativeValue(), ivBuffer, keyBuffer, keyByteCount, nil, 0, 0, 0, context) + let rawStatus = CCCryptorCreateWithMode(operation.nativeValue(), mode.nativeValue(), algorithm.nativeValue(), padding.nativeValue(), ivBuffer, keyBuffer, keyByteCount, nil, 0, 0, modeOptions, context) if let status = Status.fromRaw(status: rawStatus) { self.status = status