MYSymmetricKey.h
author snej@snej.local
Sat Apr 18 18:12:06 2009 -0700 (2009-04-18)
changeset 12 e4c971be4079
parent 2 8982b8fada63
child 13 6fd9177eb6da
permissions -rw-r--r--
Working on export/import of symmetric keys, and passphrase entry. Not ready for release quite yet.
     1 //
     2 //  MYSymmetricKey.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 4/2/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYKey.h"
    10 #import <CommonCrypto/CommonCryptor.h>
    11 
    12 
    13 @interface MYSymmetricKey : MYKey <MYEncryption, MYDecryption>
    14 {
    15 #if !MYCRYPTO_USE_IPHONE_API
    16     CSSM_KEY *_ownedCSSMKey;
    17 #endif
    18 }
    19 
    20 /** Initializes a symmetric key from the given key data and algorithm. */
    21 - (id) initWithKeyData: (NSData*)keyData
    22              algorithm: (CCAlgorithm)algorithm;
    23 
    24 /** Randomly generates a new symmetric key, using the given algorithm and key-size in bits.
    25     The key is not added to any keychain; if you want to keep the key persistently, use
    26     the method of the same name in the MYKeychain class. */
    27 + (MYSymmetricKey*) generateSymmetricKeyOfSize: (unsigned)keySizeInBits
    28                                      algorithm: (CCAlgorithm)algorithm;
    29 
    30 /** Converts a passphrase into a symmetric key.
    31     The same passphrase (and salt) will always return the same key, so you can use this method
    32     to encrypt and decrypt data using a user-entered passphrase, without having to store the key
    33     itself in the keychain.
    34     @param alertTitle  A title for the alert (this seems to be ignored by the OS).
    35     @param prompt  A prompt string displayed in the alert.
    36     @param creating  Is a new passphrase being created? If YES, the user will have to enter the
    37         passphrase twice, to check for errors, and the nifty passphrase-strength meter will be
    38         displayed. If NO, there's only one text-field, and an option to display its contents in
    39         the clear.
    40     @param salt  An arbitrary value whose data will be mixed in with the passphrase before
    41         hashing, to perturb the resulting bits. The purpose of this is to make it harder for
    42         an attacker to brute-force the key using a precompiled list of digests of common
    43         passwords. Changing the salt changes the key, so you need to pass the same value when
    44         re-deriving the key as you did when first generating it. */
    45  + (MYSymmetricKey*) generateFromUserPassphraseWithAlertTitle: (NSString*)alertTitle
    46                                                  alertPrompt: (NSString*)prompt
    47                                                     creating: (BOOL)creating
    48                                                         salt: (id)saltObj;
    49 
    50 /** The key's algorithm. */
    51 @property (readonly) CCAlgorithm algorithm;
    52 
    53 /** The key's size/length, in bits. */
    54 @property (readonly) unsigned keySizeInBits;
    55 
    56 
    57 /** A utility that prompts for a passphrase, using the Security agent's nice modal panel,
    58     and returns the raw passphrase as a string.
    59     @param alertTitle  A title for the alert (this seems to be ignored by the OS).
    60     @param prompt  A prompt string displayed in the alert.
    61     @param creating  Is a new passphrase being created? 
    62         (See description in +generateFromUserPassphrase... method.) */
    63 + (NSString*) promptForPassphraseWithAlertTitle: (NSString*)alertTitle
    64                                     alertPrompt: (NSString*)prompt
    65                                        creating: (BOOL)creating;
    66 
    67 @end