Added SHA1, SHA384 and SHA512 for completeness

This commit is contained in:
Adam Fowler 2020-02-01 12:23:59 +00:00
parent fd86446eff
commit e14efed87e
5 changed files with 241 additions and 42 deletions

View File

@ -0,0 +1,5 @@
// Insecure.swift
// based on the Vapor/open-crypto project which tries to replicate the CryptoKit framework interface
// written by AdamFowler 2020/01/30
public enum Insecure {}

View File

@ -5,35 +5,38 @@
import CommonCrypto
public struct MD5Digest : ByteDigest {
public static var byteCount: Int { return Int(CC_MD5_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public struct MD5: CCHashFunction {
public typealias Digest = MD5Digest
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgMD5) }
var context: CC_MD5_CTX
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_MD5(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
public init() {
context = CC_MD5_CTX()
CC_MD5_Init(&context)
}
public extension Insecure {
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_MD5_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
struct MD5Digest : ByteDigest {
public static var byteCount: Int { return Int(CC_MD5_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public mutating func finalize() -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_MD5_Final(&digest, &context)
return .init(bytes: digest)
struct MD5: CCHashFunction {
public typealias Digest = MD5Digest
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgMD5) }
var context: CC_MD5_CTX
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_MD5(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
public init() {
context = CC_MD5_CTX()
CC_MD5_Init(&context)
}
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_MD5_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
}
public mutating func finalize() -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_MD5_Final(&digest, &context)
return .init(bytes: digest)
}
}
}
@ -41,15 +44,17 @@ public struct MD5: CCHashFunction {
import CAWSCrypto
public struct MD5Digest : ByteDigest {
public static var byteCount: Int { return Int(MD5_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public extension Insecure {
struct MD5Digest : ByteDigest {
public static var byteCount: Int { return Int(MD5_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public struct MD5: _OpenSSLHashFunction {
public typealias Digest = MD5Digest
public static var algorithm: OpaquePointer { return EVP_md5() }
var context: OpaquePointer
struct MD5: _OpenSSLHashFunction {
public typealias Digest = MD5Digest
public static var algorithm: OpaquePointer { return EVP_md5() }
var context: OpaquePointer
}
}
#endif

View File

@ -0,0 +1,61 @@
// SHA1.swift
// based on the Vapor/open-crypto project which tries to replicate the CryptoKit framework interface
// written by AdamFowler 2020/01/30
#if canImport(CommonCrypto)
import CommonCrypto
public extension Insecure {
struct SHA1Digest : ByteDigest {
public static var byteCount: Int { return Int(CC_SHA1_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
struct SHA1: CCHashFunction {
public typealias Digest = SHA1Digest
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgSHA1) }
var context: CC_SHA1_CTX
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA1(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
public init() {
context = CC_SHA1_CTX()
CC_SHA1_Init(&context)
}
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_SHA1_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
}
public mutating func finalize() -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA1_Final(&digest, &context)
return .init(bytes: digest)
}
}
}
#else
import CAWSCrypto
public extension Insecure {
struct SHA1Digest : ByteDigest {
public static var byteCount: Int { return Int(SHA_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
struct SHA1: _OpenSSLHashFunction {
public typealias Digest = SHA1Digest
public static var algorithm: OpaquePointer { return EVP_sha1() }
var context: OpaquePointer
}
}
#endif

View File

@ -3,13 +3,13 @@
// written by AdamFowler 2020/01/30
#if canImport(CommonCrypto)
import CommonCrypto
public struct SHA256Digest : ByteDigest {
public static var byteCount: Int { return Int(CC_SHA256_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
import CommonCrypto
public struct SHA256: CCHashFunction {
public typealias Digest = SHA256Digest
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgSHA256) }
@ -37,6 +37,70 @@ public struct SHA256: CCHashFunction {
}
}
public struct SHA384Digest : ByteDigest {
public static var byteCount: Int { return Int(CC_SHA384_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public struct SHA384: CCHashFunction {
public typealias Digest = SHA384Digest
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgSHA384) }
var context: CC_SHA512_CTX
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA384(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
public init() {
context = CC_SHA512_CTX()
CC_SHA384_Init(&context)
}
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_SHA384_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
}
public mutating func finalize() -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA384_Final(&digest, &context)
return .init(bytes: digest)
}
}
public struct SHA512Digest : ByteDigest {
public static var byteCount: Int { return Int(CC_SHA512_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public struct SHA512: CCHashFunction {
public typealias Digest = SHA512Digest
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgSHA512) }
var context: CC_SHA512_CTX
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA512(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
public init() {
context = CC_SHA512_CTX()
CC_SHA512_Init(&context)
}
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_SHA512_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
}
public mutating func finalize() -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA512_Final(&digest, &context)
return .init(bytes: digest)
}
}
#else
import CAWSCrypto
@ -52,4 +116,26 @@ public struct SHA256: _OpenSSLHashFunction {
var context: OpaquePointer
}
public struct SHA384Digest : ByteDigest {
public static var byteCount: Int { return Int(SHA384_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public struct SHA384: _OpenSSLHashFunction {
public typealias Digest = SHA384Digest
public static var algorithm: OpaquePointer { return EVP_sha384() }
var context: OpaquePointer
}
public struct SHA512Digest : ByteDigest {
public static var byteCount: Int { return Int(SHA512_DIGEST_LENGTH) }
public var bytes: [UInt8]
}
public struct SHA512: _OpenSSLHashFunction {
public typealias Digest = SHA256Digest
public static var algorithm: OpaquePointer { return EVP_sha512() }
var context: OpaquePointer
}
#endif

View File

@ -23,26 +23,68 @@ final class AWSCryptoTests: XCTestCase {
func testMD5() {
let data = createRandomBuffer(34, 2345, size: 234896)
let digest = MD5.hash(data: data)
print(digest)
let digest = Insecure.MD5.hash(data: data)
XCTAssertEqual(digest.description, "3abdd8d79be09bc250d60ada1f000912")
}
func testSHA1() {
let data = createRandomBuffer(41, 83, size: 562384)
let digest = Insecure.SHA1.hash(data: data)
XCTAssertEqual(digest.description, "2ea7cc3ce53e940a5877cbf5c8dfc992e4833fb3")
}
func testSHA256() {
let data = createRandomBuffer(872, 12489, size: 562741)
let digest = SHA256.hash(data: data)
print(digest)
XCTAssertEqual(digest.description, "3cff070559024d8652d1257e5f455787e95ebd8e95378d62df1a466f78860f74")
}
func testSHA384() {
let data = createRandomBuffer(872, 12489, size: 562741)
let digest = SHA384.hash(data: data)
XCTAssertEqual(digest.description, "d03a6a749dd66fb7bb261e34014c69e217684440b0c853727ac5bc12147edddc304cadbec8df8f77ec2ee44cc6b53bc3")
}
func testSHA512() {
let data = createRandomBuffer(872, 12489, size: 562741)
let digest = SHA512.hash(data: data)
XCTAssertEqual(digest.description, "15fc2df3a1c3649b83baf0f28d1a611bee8339a050d9d2c2ac4afad18f3187f725530b09bb6b2044131648d11d608c394804bc02ce2110b76d231ea75201000d")
}
func testSHA256InitUpdateFinal() {
let data = createRandomBuffer(8372, 12489, size: 562741)
let digest = SHA256.hash(data: data)
var sha256 = SHA256()
sha256.update(data: data[0..<238768])
sha256.update(data: data[238768..<562741])
let digest2 = sha256.finalize()
XCTAssertEqual(digest, digest2)
XCTAssertEqual(digest.description, digest2.description)
}
func testHMAC() {
let data = createRandomBuffer(1, 91, size: 347237)
let key = createRandomBuffer(102, 3, size: 32)
let authenticationKey = HMAC<SHA256>.authenticationCode(for: data, using: SymmetricKey(data: key))
print(authenticationKey)
let authenticationKey = HMAC<SHA256>.authenticationCode(for: data, using: SymmetricKey(bytes: key))
XCTAssertEqual(authenticationKey.description, "ddec250211f1b546254bab3fb027af1acc4842898e8af6eeadcdbf8e2c6c1ff5")
}
func testHMACInitUpdateFinal() {
let data = createRandomBuffer(21, 81, size: 762061)
let key = createRandomBuffer(102, 3, size: 32)
let authenticationKey = HMAC<SHA256>.authenticationCode(for: data, using: SymmetricKey(bytes: key))
var hmac = HMAC<SHA256>(key: SymmetricKey(bytes: key))
hmac.update(data: data[0..<126749])
hmac.update(data: data[126749..<762061])
let authenticationKey2 = hmac.finalize()
XCTAssertEqual(authenticationKey, authenticationKey2)
XCTAssertEqual(authenticationKey.description, authenticationKey2.description)
}
static var allTests = [
("testMD5", testMD5),
("testSHA256", testSHA256),