MYKeyPair-iPhone.m
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.
     1 //
     2 //  MYKeyPair-iPhone.m
     3 //  MYNetwork-iPhone
     4 //
     5 //  Created by Jens Alfke on 3/22/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 
    10 #import "MYKeyPair.h"
    11 #import "MYCrypto_Private.h"
    12 
    13 #if USE_IPHONE_API
    14 
    15 
    16 @implementation MYKeyPair
    17 
    18 
    19 + (MYKeyPair*) _generateKeyPairOfSize: (unsigned)keySize inKeychain: (MYKeychain*)keychain {
    20     Assert( keySize == 512 || keySize == 1024 || keySize == 2048, @"Unsupported key size %u", keySize );
    21     SecKeyRef pubKey=NULL, privKey=NULL;
    22     OSStatus err;
    23     NSDictionary *pubKeyAttrs = $dict({(id)kSecAttrIsPermanent, $true});
    24     NSDictionary *privKeyAttrs = $dict({(id)kSecAttrIsPermanent, $true});
    25     NSDictionary *keyAttrs = $dict( {(id)kSecAttrKeyType, (id)kSecAttrKeyTypeRSA},
    26                                     {(id)kSecAttrKeySizeInBits, $object(keySize)},
    27                                     {(id)kSecPublicKeyAttrs, pubKeyAttrs},
    28                                     {(id)kSecPrivateKeyAttrs, privKeyAttrs} );
    29     err = SecKeyGeneratePair((CFDictionaryRef)keyAttrs,&pubKey,&privKey);
    30     if (err) {
    31         Warn(@"Failed to create key-pair: %i", err);
    32         return nil;
    33     } else
    34         return [[[self alloc] initWithPublicKeyRef: pubKey privateKeyRef: privKey] autorelease];
    35 }
    36 
    37 - (id) initWithPublicKeyRef: (SecKeyRef)publicKey privateKeyRef: (SecKeyRef)privateKey {
    38     self = [super initWithKeyRef: publicKey];
    39     if (self) {
    40         NSParameterAssert(privateKey);
    41         _privateKey = (SecKeyRef) CFRetain(privateKey);
    42     }
    43     return self;
    44 }
    45 
    46 
    47 - (NSArray*) _itemList {
    48     return $array((id)_privateKey,(id)self.keyRef);
    49 }
    50 
    51 
    52 @synthesize privateKeyRef=_privateKey;
    53 
    54 
    55 - (NSData*) decryptData: (NSData*)data {
    56     return _crypt(_privateKey,data,kCCDecrypt);
    57 }
    58     
    59 
    60 - (NSData*) signData: (NSData*)data {
    61     Assert(data);
    62     uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    63     CC_SHA1(data.bytes,data.length, digest);
    64 
    65     size_t sigLen = 1024;
    66     uint8_t sigBuf[sigLen];
    67     OSStatus err = SecKeyRawSign(_privateKey, kSecPaddingPKCS1SHA1,
    68                                    digest,sizeof(digest), //data.bytes, data.length,
    69                                    sigBuf, &sigLen);
    70     if(err) {
    71         Warn(@"SecKeyRawSign failed: %i",err);
    72         return nil;
    73     } else
    74         return [NSData dataWithBytes: sigBuf length: sigLen];
    75 }
    76 
    77 
    78 @end
    79 
    80 
    81 #endif USE_IPHONE_API