Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Allow encrypting a file without custom padding used for bucketing
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-signal authored Jun 3, 2024
1 parent 0e1ee42 commit 52afaa8
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion SignalCoreKit/src/Cryptography.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,45 @@ public extension Cryptography {
/// - parameter encryptedUrl: Where to write the encrypted output file.
/// - parameter encryptionKey: The key to encrypt with; the AES key and the hmac key concatenated together.
/// (The same format as ``EncryptionMetadata/key``). A random key will be generated if none is provided.
static func encryptFile(
at unencryptedUrl: URL,
output encryptedUrl: URL,
encryptionKey inputKey: Data? = nil
) throws -> EncryptionMetadata {
return try _encryptFile(
at: unencryptedUrl,
output: encryptedUrl,
encryptionKey: inputKey,
applyExtraPadding: false
)
}

/// Encrypt an input file to a provided output file location.
/// The encrypted output is prefixed with the random iv and postfixed with the hmac. The ciphertext is padded
/// using standard pkcs7 padding AND with custom bucketing padding applied to the plaintext prior to encryption.
///
/// - parameter unencryptedUrl: The file to encrypt.
/// - parameter encryptedUrl: Where to write the encrypted output file.
/// - parameter encryptionKey: The key to encrypt with; the AES key and the hmac key concatenated together.
/// (The same format as ``EncryptionMetadata/key``). A random key will be generated if none is provided.
static func encryptAttachment(
at unencryptedUrl: URL,
output encryptedUrl: URL,
encryptionKey inputKey: Data? = nil
) throws -> EncryptionMetadata {
return try _encryptFile(
at: unencryptedUrl,
output: encryptedUrl,
encryptionKey: inputKey,
applyExtraPadding: true
)
}

static func _encryptFile(
at unencryptedUrl: URL,
output encryptedUrl: URL,
encryptionKey inputKey: Data?,
applyExtraPadding: Bool
) throws -> EncryptionMetadata {
if let inputKey, inputKey.count != concatenatedEncryptionKeyLength {
throw OWSAssertionError("Invalid encryption key length")
Expand Down Expand Up @@ -235,7 +270,7 @@ public extension Cryptography {
},
encryptionKey: encryptionKey,
hmacKey: hmacKey,
applyExtraPadding: true
applyExtraPadding: applyExtraPadding
)
}

Expand Down Expand Up @@ -426,6 +461,17 @@ public extension Cryptography {
)
}

static func encryptedFileHandle(
at encryptedUrl: URL,
encryptionKey: Data
) throws -> EncryptedFileHandle {
return try EncryptedFileHandleImpl(
encryptedUrl: encryptedUrl,
paddingDecryptionStrategy: .pkcs7Only,
encryptionKey: encryptionKey
)
}

static func decryptFile(
at encryptedUrl: URL,
metadata: EncryptionMetadata,
Expand Down

0 comments on commit 52afaa8

Please sign in to comment.