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