MYCryptor.h
author snej@snej.local
Sat Apr 04 22:56:13 2009 -0700 (2009-04-04)
changeset 1 60e4cbbb5128
parent 0 0a6527af039b
child 2 8982b8fada63
permissions -rw-r--r--
Code cleanup, more header comments.
     1 //
     2 //  Cryptor.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 3/21/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import <CommonCrypto/CommonCryptor.h>
    11 
    12 
    13 /** Symmetric encryption: a simple Cocoa wrapper for CommonCrypto/commonCryptor.h.
    14     Provides a streaming interface for encrypting/decrypting data.
    15     This class will probably be merged into or integrated with MYSymmetricKey. */
    16 @interface MYCryptor : NSObject
    17 {
    18     @private
    19     NSData *_key;
    20     CCOperation _operation;
    21     CCAlgorithm _algorithm;
    22     CCOptions _options;
    23     CCCryptorRef _cryptor;
    24     NSError *_error;
    25     NSOutputStream *_outputStream;
    26     NSMutableData *_output;
    27     size_t _outputExtraBytes;
    28 }
    29 
    30 /** Returns a block of cryptographically-random data, suitable for use as a symmetric key.
    31     (CommonCryptor.h defines constants for key sizes and size-ranges, like kCCKeySizeAES128.) */
    32 + (NSData*) randomKeyOfLength: (size_t)length;
    33 
    34 /** Converts a passphrase into a block of data of the given size, suitable for use as a symmetric key. */
    35 + (NSData*) keyOfLength: (size_t)lengthInBits fromPassphrase: (NSString*)passphrase;
    36 
    37 /** Creates a MYCryptor configured to encrypt data. */
    38 - (id) initEncryptorWithKey: (NSData*)key
    39                   algorithm: (CCAlgorithm)algorithm;
    40 
    41 /** Creates a MYCryptor configured to decrypt data. */
    42 - (id) initDecryptorWithKey: (NSData*)key
    43                   algorithm: (CCAlgorithm)algorithm;
    44 
    45 /** The encryption/decryption key; same as the 'key' parameter to the initializer. */
    46 @property (readonly) NSData *key;
    47 
    48 /** The cipher to use; initial value is the 'algorithm' parameter to the initializer.
    49     You can change this <i>before</i> the first call to -addData:, but not after. */
    50 @property CCAlgorithm algorithm;
    51 
    52 /** Block-mode cipher options; you can set flags to enable PKCS7 padding or ECB mode
    53     (default is CBC.)
    54     You can change this <i>before</i> the first call to -addData:, but not after. */
    55 @property CCOptions options;
    56 
    57 /** Setting this property tells the cryptor to send its output to the stream,
    58     instead of accumulating it in the outputData property.
    59     You can change this <i>before</i> the first call to -addData:, but not after. */
    60 @property (retain) NSOutputStream *outputStream;
    61 
    62 /** The error state, if any, of this cryptor.
    63     After -addData: or -finish: returns NO, check this property. */
    64 @property (readonly, retain) NSError *error;
    65 
    66 /** Adds input data.
    67     @return  YES if the operation succeeded, NO if it failed. */
    68 - (BOOL) addData: (NSData*)data;
    69 
    70 /** Finishes up the encryption/decryption and flushes the remaining bytes of output.
    71     After this is called, you cannot add any more bytes of data.
    72     @return  YES if the operation succeeded, NO if it failed. */
    73 - (BOOL) finish;
    74 
    75 /** The output of the cryptor. Accessing this property implicitly calls -finish, so don't
    76     do it until you've added all of the input. (And don't add any more input afterwards.)
    77     This property will be nil if the outputStream property has been set. */
    78 @property (readonly) NSData *outputData;
    79 
    80 @end
    81 
    82 
    83 
    84 /** NSError domain for MYCryptor operations. Error code is interpreted as a CCCryptorStatus,
    85     with additional error code(s) defined below. */
    86 extern NSString* const CryptorErrorDomain;
    87 
    88 enum {
    89     /** Indicates that the outputStream couldn't write all the bytes given to it (this is legal
    90         behavior for an NSOutputStream, but MYCryptor can't handle this yet.) */
    91     kMYCryptorErrorOutputStreamChoked = -777000
    92 };