File & Streams

  • ZDCInputStream is an abstract NSInputStream that’s designed to be subclassed.

    Its benefit is that it automatically handles the NSRunLoop/CFRunLoop stuff. That is, it makes it easier to plug a custom NSInputStream into an NSRunLoop or NSURLSessionTask.

    See more

    Declaration

    Objective-C

    @interface ZDCInputStream : NSInputStream <NSStreamDelegate> {
      NSInputStream *inputStream;
      NSStreamStatus streamStatus;
      NSError *streamError;
      id<NSStreamDelegate> delegate;
      NSNumber *fileMinOffset;
      NSNumber *fileMaxOffset;
      NSNumber *returnEOFOnWouldBlock;
    }

    Swift

    class ZDCInputStream : InputStream, StreamDelegate
  • ZDCInterruptingInputStream will automatically close itself with an error if it detects the underlying file was modified.

    It can be used to safely stream a file directly from disk, which may be modified by other processes on the OS.

    See more

    Declaration

    Objective-C

    @interface ZDCInterruptingInputStream : ZDCInputStream <NSCopying>

    Swift

    class ZDCInterruptingInputStream : ZDCInputStream, NSCopying
  • Converts from cacheFile (encrypted) format to cleartext (non-encrypted) format.

    In other words, the stream takes as input a cacheFile source (via file/stream/data). And as output (what you receive when you invoke -read:maxLength:), it gives you the decrypted/cleartext version.

    Use this for reading encrypted files in ZDCCryptoFileFormat_CacheFile.

    You can stream the output to an unencrypted file for use with the previewer. Or you can use the output as the input to a different encrypter (e.g. cloud file format).

    How it works:

    • Create an instance of this class with the encryption key, and an input source for reading the cacheFile.
    • Then continually invoke the read:maxLength: method, passing in a buffer for the unencrypted data to be copied into.
    • This class will read and decrypt the next chuck of data from cacheFile source, and write the decrypted version to your buffer.
    See more

    Declaration

    Objective-C

    @interface CacheFile2CleartextInputStream : ZDCInputStream <NSCopying>

    Swift

    class CacheFile2CleartextInputStream : ZDCInputStream, NSCopying
  • Converts from cleartext (non-encrypted) format to cacheFile (encrypted) format.

    In other words, the stream takes as input a cleartext source (via file/stream/data). And as output (what you receive when you invoke -read:maxLength:), it gives you the cacheFile version.

    Use this class for creating files in ZDCCryptoFileFormat_CacheFile.

    How it works:

    • Create an instance of this class with the encryption key, and the cleartext input source.
    • Then continually invoke the [stream read:maxLength:] method, passing in the your buffer.
    • This class will then encrypt read and encrypt the underlying cleartext data, and place the encrypted version in your buffer.
    See more

    Declaration

    Objective-C

    @interface Cleartext2CacheFileInputStream : ZDCInputStream <NSCopying>

    Swift

    class Cleartext2CacheFileInputStream : ZDCInputStream, NSCopying
  • Converts from cleartext (non-encrypted) format to cloudFile (encrypted) format.

    In other words, the stream takes as input a cleartext source (via file/stream/data). And as output (what you receive when you invoke -read:maxLength:), it gives you the cloudFile version.

    Use this class for creating files in ZDCCryptoFileFormat_CloudFile.

    The data read from this stream can be:

    • the input to a network connection (i.e. directly uploading to cloud)
    • the input for writing the cloud file to disk
    • the input for another stream

    Keep in mind that multiple input streams can be piped together. So if you have a file in cache file format, and you need to convert it to cloud file format, then you can do the following:

    How to use this class:

    • Create an instance of this class with an input in cleartext format.
    • Optionally assign the rawMetadata & rawThumbnail properties.
    • If needed, assign the cleartextFileSize property (needed if this information cannot be extracted from init parameters).
    • Then continually invoke the -read:maxLength: method, passing in a buffer for the cloud file data (the output) to be copied into.
    • Continue until you read the end of the input source.
    See more

    Declaration

    Objective-C

    @interface Cleartext2CloudFileInputStream : ZDCInputStream <NSCopying>

    Swift

    class Cleartext2CloudFileInputStream : ZDCInputStream, NSCopying
  • Converts from cloudFile (encrypted) format to cleartext (non-encrypted) format.

    In other words, the stream takes as input a cloudFile source (via file/stream/data). And as output (what you receive when you invoke -read:maxLength:), it gives you the decrypted/cleartext version.

    Use this for reading encrypted files in ZDCCryptoFileFormat_CloudFile.

    How it works:

  • Create an instance of this class configured to read the raw cloudFile data, along with the encryption key for decrypting the cloudFile data.
  • Then continually invoke the -read:maxLength: method, passing in a buffer for the output (the decrypted/cleartext data).
  • This class will read and decrypt the next chuck of data, and write the decrypted version to your buffer.

  • Warning

    A CloudFile contains multiple sections.

    Recall that a CloudFile is composed of multiple different sections:

    • header (always present)
    • metadata (optional, may be present)
    • thumbnail (optiona, may be present)
    • data (always present)

    This class allows you to read every section. Which is a bit different from your average stream. So, to simplify its use, this class will perform a soft break between each section via the -read:maxLength: method. That is, the ‘read:maxLength’ method will return a 0 (zero) when you reach the end of a section. This ensures you will only ever receive data from a single section at a time.

    Thus, you can simply invoke ‘read:maxLength:’ until the -cloudFileSection reflects the section you’re interested in. If you want to jump to a particular section, you can use NSInputStream’s setProperty:forKey: method, and use the ZDCStreamCloudFileSection key.

    See more

    Declaration

    Objective-C

    @interface CloudFile2CleartextInputStream : ZDCInputStream <NSCopying>

    Swift

    class CloudFile2CleartextInputStream : ZDCInputStream, NSCopying
  • The FileReader class provides random access to an encrypted file.

    That is, it allows you to open an encrypted file on disk, and read from that file (using random access) as if the file were cleartext (not encrypted).

    See more

    Declaration

    Objective-C

    @interface ZDCFileReader : NSObject

    Swift

    class ZDCFileReader : NSObject
  • ZDCFileChecksum can generate 1 or more checksums of a file in a single pass. It supports all the algorithms in the S4Crypto library.

    See more

    Declaration

    Objective-C

    @interface ZDCFileChecksum : NSObject

    Swift

    class ZDCFileChecksum : NSObject
  • /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    See more

    Declaration

    Objective-C

    @interface ZDCFileChecksumInstruction : NSObject <NSCopying>

    Swift

    class ZDCFileChecksumInstruction : NSObject, NSCopying
  • The ZDCFileConverstion class contains many utility methods to encrypt & decrypt files.

    See more

    Declaration

    Objective-C

    @interface ZDCFileConversion : NSObject

    Swift

    class ZDCFileConversion : NSObject
  • Monitors the filesystem for changes to a file or directory, and uses a block-based notification system to alert you when changes are signaled by the OS.

    See more

    Declaration

    Objective-C

    @interface ZDCFilesystemMonitor : NSObject

    Swift

    class ZDCFilesystemMonitor : NSObject
  • ZeroDark.cloud uses 2 different types of encrypted files:

    • cache files
    • cloud files

    A cache file is very simple, and it’s what we prefer to use when storing a file on the local device. It’s created like this:

    1. append this header to the beginning of the plaintext (non-encrypted) file
    2. encrypt the data (header + plaintext) using the encryption key

    The output will be an encrypted file whose size is rounded up to the nearest kZDCNode_TweakBlockSizeInBytes. When attempting decryption, we can verify the decryption key is correct by inspecting the decrypted header.

    See more

    Declaration

    Objective-C

    struct ZDCCacheFileHeader {}

    Swift

    struct ZDCCacheFileHeader
  • Standard typedef for struct ZDCCacheFileHeader.

    Declaration

    Objective-C

    typedef struct ZDCCacheFileHeader ZDCCacheFileHeader
  • ZeroDark.cloud uses 2 different types of encrypted files:

    • cache files
    • cloud files

    A cloud file is what we store in the cloud, and it contains 4 separate sections:

    • header prefix
    • metadata (optional)
    • thumbnail (optional)
    • data

    By storing the metadata & thumbnail sections separately, we make it possible for client devices to download only what they need. For example, a mobile device might just download the thumbnails for a picture or video. This significantly decreases bandwidth demand, while maintaining a proper user experience.

    A cloud file is created like so:

    1. append this header to the beginning of the plaintext (non-encrypted) file
    2. append a metadata section (JSON) (optional)
    3. append a thumbnail (such as small jpg/png) (optional)
    4. append the raw file data
    5. encrypt the data (header + metadata + thumbnail + data) using the encryption key

    The output will be an encrypted file whose size is rounded up to the nearest kZDCNode_TweakBlockSizeInBytes. When attempting decryption, we can verify the decryption key is correct by inspecting the decrypted header.

    See more

    Declaration

    Objective-C

    struct ZDCCloudFileHeader {}

    Swift

    struct ZDCCloudFileHeader
  • Standard typedef for struct ZDCCloudFileHeader.

    Declaration

    Objective-C

    typedef struct ZDCCloudFileHeader ZDCCloudFileHeader