MYCryptor.h
author Jens Alfke <jens@mooseyard.com>
Fri Aug 07 11:24:53 2009 -0700 (2009-08-07)
changeset 28 54b373aa65ab
parent 1 60e4cbbb5128
permissions -rw-r--r--
Fixed iPhone OS build. (issue 3)
     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 streaming interface for encrypting/decrypting data.
    14     This is a simple Cocoa wrapper for CommonCrypto/commonCryptor.h. It will probably be
    15     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 randomly-generated symmetric key of the desired length (in bits).
    31  *  @param lengthInBits  The length of the desired key, in bits (not bytes).
    32  */
    33 + (NSData*) randomKeyOfLength: (size_t)lengthInBits;
    34 
    35 /** Converts a passphrase into a symmetric key of the desired length (in bits).
    36  *  The same passphrase (and salt) will always return the same key, so you can use this method
    37  *  to encrypt and decrypt data using a user-entered passphrase, without having to store the key
    38  *  itself in the keychain.
    39  *  @param lengthInBits  The length of the desired key, in bits (not bytes).
    40  *  @param passphrase  The user-entered passphrase.
    41  *  @param salt  An arbitrary value whose description will be appended to the passphrase before
    42  *          hashing, to perturb the resulting bits. The purpose of this is to make it harder for
    43  *          an attacker to brute-force the key using a precompiled list of digests of common
    44  *          passwords. Changing the salt changes the key, so you need to pass the same value when
    45  *          re-deriving the key as you did when first generating it.
    46  */
    47 + (NSData*) keyOfLength: (size_t)lengthInBits
    48          fromPassphrase: (NSString*)passphrase
    49                    salt: (id)salt;
    50 
    51 /** Creates a MYCryptor configured to encrypt data. */
    52 - (id) initEncryptorWithKey: (NSData*)key
    53                   algorithm: (CCAlgorithm)algorithm;
    54 
    55 /** Creates a MYCryptor configured to decrypt data. */
    56 - (id) initDecryptorWithKey: (NSData*)key
    57                   algorithm: (CCAlgorithm)algorithm;
    58 
    59 /** The encryption/decryption key; same as the 'key' parameter to the initializer. */
    60 @property (readonly) NSData *key;
    61 
    62 /** The cipher to use; initial value is the 'algorithm' parameter to the initializer.
    63     You can change this <i>before</i> the first call to -addData:, but not after. */
    64 @property CCAlgorithm algorithm;
    65 
    66 /** Block-mode cipher options; you can set flags to enable PKCS7 padding or ECB mode
    67     (default is CBC.)
    68     You can change this <i>before</i> the first call to -addData:, but not after. */
    69 @property CCOptions options;
    70 
    71 /** Setting this property tells the cryptor to send its output to the stream,
    72     instead of accumulating it in the outputData property.
    73     You can change this <i>before</i> the first call to -addData:, but not after. */
    74 @property (retain) NSOutputStream *outputStream;
    75 
    76 /** The error state, if any, of this cryptor.
    77     After -addData: or -finish: returns NO, check this property. */
    78 @property (readonly, retain) NSError *error;
    79 
    80 /** Adds input data.
    81     @return  YES if the operation succeeded, NO if it failed. */
    82 - (BOOL) addData: (NSData*)data;
    83 
    84 /** Finishes up the encryption/decryption and flushes the remaining bytes of output.
    85     After this is called, you cannot add any more bytes of data.
    86     @return  YES if the operation succeeded, NO if it failed. */
    87 - (BOOL) finish;
    88 
    89 /** The output of the cryptor. Accessing this property implicitly calls -finish, so don't
    90     do it until you've added all of the input. (And don't add any more input afterwards.)
    91     This property will be nil if the outputStream property has been set. */
    92 @property (readonly) NSData *outputData;
    93 
    94 @end
    95 
    96 
    97 
    98 /** NSError domain for MYCryptor operations. Error code is interpreted as a CCCryptorStatus,
    99     with additional error code(s) defined below. */
   100 extern NSString* const CryptorErrorDomain;
   101 
   102 enum {
   103     /** Indicates that the outputStream couldn't write all the bytes given to it (this is legal
   104         behavior for an NSOutputStream, but MYCryptor can't handle this yet.) */
   105     kMYCryptorErrorOutputStreamChoked = -777000
   106 };