Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #52 from vapor/core-3-1
Browse files Browse the repository at this point in the history
core 3.1 updates
  • Loading branch information
tanner0101 authored Apr 12, 2018
2 parents 6d2ebdd + 16c4ce7 commit a5598ab
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Sources/Crypto/Cipher/Cipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public final class Cipher {
///
/// - throws: `CryptoError` if reset fails, data conversion fails, or key/iv lengths are not correct.
public func reset(key: LosslessDataConvertible, iv: LosslessDataConvertible? = nil, mode: CipherMode = .encrypt) throws {
let key = try key.convertToData()
let iv = try iv?.convertToData()
let key = key.convertToData()
let iv = iv?.convertToData()

guard EVP_CipherInit_ex(ctx, algorithm.c, nil, key.withUnsafeBytes { $0 }, iv?.withUnsafeBytes { $0 }, mode.rawValue) == 1 else {
throw CryptoError.openssl(identifier: "EVP_CipherInit_ex", reason: "Failed initializing cipher context.")
Expand Down Expand Up @@ -156,7 +156,7 @@ public final class Cipher {
/// - buffer: Mutable buffer to append newly encrypted or decrypted data to.
/// - throws: `CryptoError` if update fails or data conversion fails.
public func update(data: LosslessDataConvertible, into buffer: inout Data) throws {
let input = try data.convertToData()
let input = data.convertToData()
let inputLength: Int32 = numericCast(input.count)

var chunk = Data(repeating: 0, count: numericCast(inputLength + algorithm.blockSize - 1))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Crypto/Digest/Digest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public final class Digest {
/// - data: Message chunk to digest.
/// - throws: `CryptoError` if update fails or data conversion fails.
public func update(data: LosslessDataConvertible) throws {
let data = try data.convertToData()
let data = data.convertToData()
guard EVP_DigestUpdate(ctx, .init(data.withUnsafeBytes { $0 }), data.count) == 1 else {
throw CryptoError.openssl(identifier: "EVP_DigestUpdate", reason: "Failed updating digest.")
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/Crypto/Digest/DigestAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CNIOOpenSSL
///
/// https://en.wikipedia.org/wiki/Cryptographic_hash_function
/// https://www.openssl.org/docs/man1.1.0/crypto/EVP_MD_CTX_free.html
public final class DigestAlgorithm {
public final class DigestAlgorithm: Equatable {
/// Looks up a hash function algorithm by name (e.g., "sha256").
/// Uses OpenSSL's `EVP_get_digestbyname` function.
///
Expand All @@ -23,6 +23,11 @@ public final class DigestAlgorithm {
return .init(c: digest)
}

/// See `Equatable`.
public static func == (lhs: DigestAlgorithm, rhs: DigestAlgorithm) -> Bool {
return lhs.type == rhs.type
}

/// OpenSSL `EVP_MD` context.
let c: UnsafePointer<EVP_MD>

Expand Down
4 changes: 2 additions & 2 deletions Sources/Crypto/MAC/HMAC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public final class HMAC {
/// - key: HMAC key
/// - throws: `CryptoError` if the initialization / reset fails or data conversion fails.
public func reset(key: LosslessDataConvertible) throws {
let key = try key.convertToData()
let key = key.convertToData()
guard HMAC_Init_ex(&ctx, .init(key.withUnsafeBytes { $0 }), Int32(key.count), algorithm.c, nil) == 1 else {
throw CryptoError.openssl(identifier: "HMAC_Init_ex", reason: "Failed initializing HMAC context.")
}
Expand All @@ -122,7 +122,7 @@ public final class HMAC {
/// - data: Message chunk to digest / authenticate
/// - throws: `CryptoError` if the update fails or data conversion fails.
public func update(data: LosslessDataConvertible) throws {
let data = try data.convertToData()
let data = data.convertToData()
guard HMAC_Update(&ctx, .init(data.withUnsafeBytes { $0 }), data.count) == 1 else {
throw CryptoError.openssl(identifier: "HMAC_Update", reason: "Failed updating HMAC digest.")
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Crypto/RSA/RSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public final class RSA {
count: Int(RSA_size(key.c.pointer))
)

var input = try input.convertToData()
var input = input.convertToData()

switch format {
case .digest: break // leave input as is
Expand Down Expand Up @@ -108,8 +108,8 @@ public final class RSA {
/// - returns: `true` if signature matches plaintext input.
/// - throws: `CryptoError` if verification fails or data conversion fails.
public func verify(_ signature: LosslessDataConvertible, signs input: LosslessDataConvertible, format: RSAInputFormat = .message, key: RSAKey) throws -> Bool {
var input = try input.convertToData()
var signature = try signature.convertToData()
var input = input.convertToData()
var signature = signature.convertToData()

switch format {
case .digest: break // leave input as is
Expand Down
8 changes: 4 additions & 4 deletions Sources/Crypto/RSA/RSAKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public struct RSAKey {
///
/// And then instantiate the key as:
///
/// try RSAKey(n: key.n, e: key.e, d: key.d)
/// try RSAKey.components(n: key.n, e: key.e, d: key.d)
///
/// - throws: `CryptoError` if key generation fails.
public init(n: String, e: String, d: String? = nil) throws {
public static func components(n: String, e: String, d: String? = nil) throws -> RSAKey {
func parseBignum(_ s: String) -> UnsafeMutablePointer<BIGNUM>? {
return Data(base64URLEncoded: s)?.withByteBuffer { p in
return BN_bin2bn(p.baseAddress, Int32(p.count), nil)
Expand All @@ -80,9 +80,9 @@ public struct RSAKey {

if let d = d {
rsa.pointee.d = parseBignum(d)
self = try .init(type: .private, key: CRSAKey(rsa))
return try .init(type: .private, key: CRSAKey(rsa))
} else {
self = try .init(type: .public, key: CRSAKey(rsa))
return try .init(type: .public, key: CRSAKey(rsa))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/CryptoTests/CipherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CipherTests: XCTestCase {
}

extension Data {
public func convert<T>(to type: T.Type = T.self) throws -> T where T: LosslessDataConvertible {
return try T.convertFromData(self)
public func convert<T>(to type: T.Type = T.self) -> T where T: LosslessDataConvertible {
return T.convertFromData(self)
}
}
15 changes: 15 additions & 0 deletions Tests/CryptoTests/RSATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ class RSATests: XCTestCase {
XCTAssertNotEqual(data1.hexDebug, data2.hexDebug)
}

func testComps() throws {
//"kty": "RSA",
//"alg": "RS256",
//"use": "sig",
//"kid": "3b547886ff85a3428df4f61db73c1c23982a928e",
//"n": "mjJLokVSf3F_7MAPPEZzT0fQO2AQwlpzDdYG1EHH9WTxm0Dk4KB8vIBCp6lWm0fV8-pv0N7zF9rJ0CHKgkxuC02VwHVtuegE7XikfRCZJaPAn-MHm-eowW2SpSmsudi0_Gs1cvjxms_lVvoHUBaDTjhHWqCRGX_oOiNCglJKPFaYtyTA4ZiUfQ3FE_uVeoC_gYTYxuUzVxLsKJynrFaOVGIvnp9uRdbS0WtUhs7BY-tgqzJEt42_PFo-DAgWFIpdUzfG0AxAHZQ7TxDM09MaWBVoUMrBMqpMT6TaRtWiKOYeGEfV-ZH2d8qWoJHaKbZjMiSL64sgTNw2T_pZAyTI3Q",
//"e": "AQAB"
let key: RSAKey = try .components(
n: "mjJLokVSf3F_7MAPPEZzT0fQO2AQwlpzDdYG1EHH9WTxm0Dk4KB8vIBCp6lWm0fV8-pv0N7zF9rJ0CHKgkxuC02VwHVtuegE7XikfRCZJaPAn-MHm-eowW2SpSmsudi0_Gs1cvjxms_lVvoHUBaDTjhHWqCRGX_oOiNCglJKPFaYtyTA4ZiUfQ3FE_uVeoC_gYTYxuUzVxLsKJynrFaOVGIvnp9uRdbS0WtUhs7BY-tgqzJEt42_PFo-DAgWFIpdUzfG0AxAHZQ7TxDM09MaWBVoUMrBMqpMT6TaRtWiKOYeGEfV-ZH2d8qWoJHaKbZjMiSL64sgTNw2T_pZAyTI3Q",
e: "AQAB"
)
XCTAssertEqual(key.type, .public)
}

static var allTests = [
("testPrivateKey", testPrivateKey),
("testPublicKey", testPublicKey),
Expand All @@ -72,6 +86,7 @@ class RSATests: XCTestCase {
("testKey4096", testKey4096),
("testKeyCert", testKeyCert),
("testRand", testRand),
("testComps", testComps),
]
}

Expand Down

0 comments on commit a5598ab

Please # to comment.