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

Provide CryptoError Typealias #282

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extension AES.GCM {
let aesGCMOverhead = 12 + 16

if combined.count < aesGCMOverhead {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

self.init(combined: Data(combined))
Expand All @@ -181,7 +181,7 @@ extension AES.GCM {
/// - tag: The authentication tag.
public init<C: DataProtocol, T: DataProtocol>(nonce: AES.GCM.Nonce, ciphertext: C, tag: T) throws {
guard tag.count == AES.GCM.tagByteCount else {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

let nonceByteCount = nonce.bytes.count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum OpenSSLAESGCMImpl {
case 256:
return .aes256gcm
default:
throw CryptoKitError.incorrectKeySize
throw CryptoError.incorrectKeySize
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension BoringSSLAEAD {
let context = try AEADContext(cipher: self, key: key)
return try context.seal(message: message, nonce: nonce, authenticatedData: authenticatedData)
} catch CryptoBoringWrapperError.underlyingCoreCryptoError(let errorCode) {
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
throw CryptoError.underlyingCoreCryptoError(error: errorCode)
}
}

Expand All @@ -36,15 +36,15 @@ extension BoringSSLAEAD {
let context = try AEADContext(cipher: self, key: key)
return try context.open(ciphertext: ciphertext, nonce: nonce, tag: tag, authenticatedData: authenticatedData)
} catch CryptoBoringWrapperError.underlyingCoreCryptoError(let errorCode) {
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
throw CryptoError.underlyingCoreCryptoError(error: errorCode)
}
}
}

enum OpenSSLChaChaPolyImpl {
static func encrypt<M: DataProtocol, AD: DataProtocol>(key: SymmetricKey, message: M, nonce: ChaChaPoly.Nonce?, authenticatedData: AD?) throws -> ChaChaPoly.SealedBox {
guard key.bitCount == ChaChaPoly.keyBitsCount else {
throw CryptoKitError.incorrectKeySize
throw CryptoError.incorrectKeySize
}
let nonce = nonce ?? ChaChaPoly.Nonce()

Expand All @@ -61,7 +61,7 @@ enum OpenSSLChaChaPolyImpl {

static func decrypt<AD: DataProtocol>(key: SymmetricKey, ciphertext: ChaChaPoly.SealedBox, authenticatedData: AD?) throws -> Data {
guard key.bitCount == ChaChaPoly.keyBitsCount else {
throw CryptoKitError.incorrectKeySize
throw CryptoError.incorrectKeySize
}

if let ad = authenticatedData {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension ChaChaPoly {
let chachaPolyOverhead = 12 + 16

if combined.count < chachaPolyOverhead {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

self.combined = Data(combined)
Expand All @@ -153,7 +153,7 @@ extension ChaChaPoly {
/// - tag: An authentication tag.
public init<C: DataProtocol, T: DataProtocol>(nonce: ChaChaPoly.Nonce, ciphertext: C, tag: T) throws {
guard tag.count == ChaChaPoly.tagByteCount else {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

self.combined = Data(nonce) + ciphertext + tag
Expand Down
4 changes: 2 additions & 2 deletions Sources/Crypto/AEADs/Nonces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension AES.GCM {
/// error if the data has a length other than 12 bytes.
public init<D: DataProtocol>(data: D) throws {
if data.count < AES.GCM.defaultNonceByteCount {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

self.bytes = Data(data)
Expand Down Expand Up @@ -113,7 +113,7 @@ extension ChaChaPoly {
/// error if the data has a length other than 12 bytes.
public init<D: DataProtocol>(data: D) throws {
if data.count != ChaChaPoly.nonceByteCount {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

self.bytes = Data(data)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Crypto/AEADs/Nonces.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension ${name} {
/// error if the data has a length other than 12 bytes.
public init<D: DataProtocol>(data: D) throws {
if data.count ${nonceValidation} {
throw CryptoKitError.incorrectParameterSize
throw CryptoError.incorrectParameterSize
}

self.bytes = Data(data)
Expand Down
40 changes: 20 additions & 20 deletions Sources/Crypto/ASN1/ASN1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,23 @@ extension ASN1 {
/// Parse the node as an ASN.1 sequence.
internal static func sequence<T>(_ node: ASN1Node, identifier: ASN1.ASN1Identifier, _ builder: (inout ASN1.ASN1NodeCollection.Iterator) throws -> T) throws -> T {
guard node.identifier == identifier, case .constructed(let nodes) = node.content else {
throw CryptoKitASN1Error.unexpectedFieldType
throw CryptoASN1Error.unexpectedFieldType
}

var iterator = nodes.makeIterator()

let result = try builder(&iterator)

guard iterator.next() == nil else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

return result
}

internal static func sequence<T: ASN1Parseable>(of: T.Type = T.self, identifier: ASN1.ASN1Identifier, rootNode: ASN1Node) throws -> [T] {
guard rootNode.identifier == identifier, case .constructed(let nodes) = rootNode.content else {
throw CryptoKitASN1Error.unexpectedFieldType
throw CryptoASN1Error.unexpectedFieldType
}

return try nodes.map { try T(asn1Encoded: $0) }
Expand All @@ -162,7 +162,7 @@ extension ASN1 {
internal static func sequence<T: ASN1Parseable>(of: T.Type = T.self, identifier: ASN1.ASN1Identifier, nodes: inout ASN1.ASN1NodeCollection.Iterator) throws -> [T] {
guard let node = nodes.next() else {
// Not present, throw.
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

return try sequence(of: T.self, identifier: identifier, rootNode: node)
Expand Down Expand Up @@ -205,7 +205,7 @@ extension ASN1 {

var nodeIterator = nodes.makeIterator()
guard let child = nodeIterator.next(), nodeIterator.next() == nil else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

return try builder(child)
Expand Down Expand Up @@ -239,7 +239,7 @@ extension ASN1 {
// DER forbids encoding DEFAULT values at their default state.
// We can lift this in BER.
guard parsed != defaultValue else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

return parsed
Expand All @@ -250,7 +250,7 @@ extension ASN1 {
guard result != defaultValue else {
// DER forbids encoding DEFAULT values at their default state.
// We can lift this in BER.
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

return result
Expand Down Expand Up @@ -279,7 +279,7 @@ extension ASN1 {

try parseNode(from: &data, depth: 1, into: &nodes)
guard data.count == 0 else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}
return ASN1ParseResult(nodes[...])
}
Expand All @@ -290,28 +290,28 @@ extension ASN1 {
guard depth <= ASN1.ASN1ParseResult.maximumNodeDepth else {
// We defend ourselves against stack overflow by refusing to allocate more than 10 stack frames to
// the parsing.
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

guard let rawIdentifier = data.popFirst() else {
throw CryptoKitASN1Error.truncatedASN1Field
throw CryptoASN1Error.truncatedASN1Field
}

let identifier = try ASN1Identifier(rawIdentifier: rawIdentifier)
guard let wideLength = try data.readASN1Length() else {
throw CryptoKitASN1Error.truncatedASN1Field
throw CryptoASN1Error.truncatedASN1Field
}

// UInt is sometimes too large for us!
guard let length = Int(exactly: wideLength) else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

var subData = data.prefix(length)
data = data.dropFirst(length)

guard subData.count == length else {
throw CryptoKitASN1Error.truncatedASN1Field
throw CryptoASN1Error.truncatedASN1Field
}

if identifier.constructed {
Expand Down Expand Up @@ -553,7 +553,7 @@ internal protocol ASN1Parseable {
extension ASN1Parseable {
internal init(asn1Encoded sequenceNodeIterator: inout ASN1.ASN1NodeCollection.Iterator) throws {
guard let node = sequenceNodeIterator.next() else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

self = try .init(asn1Encoded: node)
Expand Down Expand Up @@ -587,7 +587,7 @@ extension ASN1ImplicitlyTaggable {
internal init(asn1Encoded sequenceNodeIterator: inout ASN1.ASN1NodeCollection.Iterator,
withIdentifier identifier: ASN1.ASN1Identifier = Self.defaultIdentifier) throws {
guard let node = sequenceNodeIterator.next() else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

self = try .init(asn1Encoded: node, withIdentifier: identifier)
Expand Down Expand Up @@ -619,7 +619,7 @@ extension ArraySlice where Element == UInt8 {
switch firstByte {
case 0x80:
// Indefinite form. Unsupported.
throw CryptoKitASN1Error.unsupportedFieldLength
throw CryptoASN1Error.unsupportedFieldLength
case let val where val & 0x80 == 0x80:
// Top bit is set, this is the long form. The remaining 7 bits of this octet
// determine how long the length field is.
Expand All @@ -638,16 +638,16 @@ extension ArraySlice where Element == UInt8 {
switch requiredBits {
case 0...7:
// For 0 to 7 bits, the long form is unacceptable and we require the short.
throw CryptoKitASN1Error.unsupportedFieldLength
throw CryptoASN1Error.unsupportedFieldLength
case 8...:
// For 8 or more bits, fieldLength should be the minimum required.
let requiredBytes = (requiredBits + 7) / 8
if fieldLength > requiredBytes {
throw CryptoKitASN1Error.unsupportedFieldLength
throw CryptoASN1Error.unsupportedFieldLength
}
default:
// This is not reachable, but we'll error anyway.
throw CryptoKitASN1Error.unsupportedFieldLength
throw CryptoASN1Error.unsupportedFieldLength
}

return length
Expand All @@ -661,7 +661,7 @@ extension ArraySlice where Element == UInt8 {
extension FixedWidthInteger {
internal init<Bytes: Collection>(bigEndianBytes bytes: Bytes) throws where Bytes.Element == UInt8 {
guard bytes.count <= (Self.bitWidth / 8) else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

self = 0
Expand Down
4 changes: 2 additions & 2 deletions Sources/Crypto/ASN1/Basic ASN1 Types/ASN1BitString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension ASN1 {

init(asn1Encoded node: ASN1.ASN1Node, withIdentifier identifier: ASN1.ASN1Identifier) throws {
guard node.identifier == identifier else {
throw CryptoKitASN1Error.unexpectedFieldType
throw CryptoASN1Error.unexpectedFieldType
}

guard case .primitive(let content) = node.content else {
Expand All @@ -37,7 +37,7 @@ extension ASN1 {
// The initial octet explains how many of the bits in the _final_ octet are not part of the bitstring.
// The only value we support here is 0.
guard content.first == 0 else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

self.bytes = content.dropFirst()
Expand Down
6 changes: 3 additions & 3 deletions Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Boolean.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ extension Bool: ASN1ImplicitlyTaggable {

init(asn1Encoded node: ASN1.ASN1Node, withIdentifier identifier: ASN1.ASN1Identifier) throws {
guard node.identifier == identifier else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

guard case .primitive(let bytes) = node.content, bytes.count == 1 else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}

switch bytes[bytes.startIndex] {
Expand All @@ -39,7 +39,7 @@ extension Bool: ASN1ImplicitlyTaggable {
self = true
default:
// If we come to support BER then these values are all "true" as well.
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Identifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension ASN1 {
init(rawIdentifier: UInt8) throws {
// We don't support multibyte identifiers, which are signalled when the bottom 5 bits are all 1.
guard rawIdentifier & 0x1F != 0x1F else {
throw CryptoKitASN1Error.invalidFieldIdentifier
throw CryptoASN1Error.invalidFieldIdentifier
}

self.baseTag = rawIdentifier
Expand Down
8 changes: 4 additions & 4 deletions Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension ASN1IntegerRepresentable {

internal init(asn1Encoded node: ASN1.ASN1Node, withIdentifier identifier: ASN1.ASN1Identifier) throws {
guard node.identifier == identifier else {
throw CryptoKitASN1Error.unexpectedFieldType
throw CryptoASN1Error.unexpectedFieldType
}

guard case .primitive(var dataBytes) = node.content else {
Expand All @@ -50,7 +50,7 @@ extension ASN1IntegerRepresentable {

// Zero bytes of integer is not an acceptable encoding.
guard dataBytes.count > 0 else {
throw CryptoKitASN1Error.invalidASN1IntegerEncoding
throw CryptoASN1Error.invalidASN1IntegerEncoding
}

// 8.3.2 If the contents octets of an integer value encoding consist of more than one octet, then the bits of the first octet and bit 8 of the second octet:
Expand All @@ -62,7 +62,7 @@ extension ASN1IntegerRepresentable {
if let first = dataBytes.first, let second = dataBytes.dropFirst().first {
if (first == 0xFF) && second.topBitSet ||
(first == 0x00) && !second.topBitSet {
throw CryptoKitASN1Error.invalidASN1IntegerEncoding
throw CryptoASN1Error.invalidASN1IntegerEncoding
}
}

Expand All @@ -72,7 +72,7 @@ extension ASN1IntegerRepresentable {
if first == 0x00 {
dataBytes = dataBytes.dropFirst()
} else if first & 0x80 == 0x80 {
throw CryptoKitASN1Error.invalidASN1IntegerEncoding
throw CryptoASN1Error.invalidASN1IntegerEncoding
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Null.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ extension ASN1 {

init(asn1Encoded node: ASN1.ASN1Node, withIdentifier identifier: ASN1.ASN1Identifier) throws {
guard node.identifier == identifier, case .primitive(let content) = node.content else {
throw CryptoKitASN1Error.unexpectedFieldType
throw CryptoASN1Error.unexpectedFieldType
}

guard content.count == 0 else {
throw CryptoKitASN1Error.invalidASN1Object
throw CryptoASN1Error.invalidASN1Object
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Crypto/ASN1/Basic ASN1 Types/ASN1OctetString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension ASN1 {

init(asn1Encoded node: ASN1.ASN1Node, withIdentifier identifier: ASN1.ASN1Identifier) throws {
guard node.identifier == identifier else {
throw CryptoKitASN1Error.unexpectedFieldType
throw CryptoASN1Error.unexpectedFieldType
}

guard case .primitive(let content) = node.content else {
Expand Down
Loading