MYKeyPair-iPhone.m
changeset 0 0a6527af039b
child 1 60e4cbbb5128
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/MYKeyPair-iPhone.m	Sat Apr 04 20:42:03 2009 -0700
     1.3 @@ -0,0 +1,81 @@
     1.4 +//
     1.5 +//  MYKeyPair-iPhone.m
     1.6 +//  MYNetwork-iPhone
     1.7 +//
     1.8 +//  Created by Jens Alfke on 3/22/09.
     1.9 +//  Copyright 2009 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +
    1.13 +#import "MYKeyPair.h"
    1.14 +#import "MYCrypto_Private.h"
    1.15 +
    1.16 +#if USE_IPHONE_API
    1.17 +
    1.18 +
    1.19 +@implementation MYKeyPair
    1.20 +
    1.21 +
    1.22 ++ (MYKeyPair*) _generateKeyPairOfSize: (unsigned)keySize inKeychain: (MYKeychain*)keychain {
    1.23 +    Assert( keySize == 512 || keySize == 1024 || keySize == 2048, @"Unsupported key size %u", keySize );
    1.24 +    SecKeyRef pubKey=NULL, privKey=NULL;
    1.25 +    OSStatus err;
    1.26 +    NSDictionary *pubKeyAttrs = $dict({(id)kSecAttrIsPermanent, $true});
    1.27 +    NSDictionary *privKeyAttrs = $dict({(id)kSecAttrIsPermanent, $true});
    1.28 +    NSDictionary *keyAttrs = $dict( {(id)kSecAttrKeyType, (id)kSecAttrKeyTypeRSA},
    1.29 +                                    {(id)kSecAttrKeySizeInBits, $object(keySize)},
    1.30 +                                    {(id)kSecPublicKeyAttrs, pubKeyAttrs},
    1.31 +                                    {(id)kSecPrivateKeyAttrs, privKeyAttrs} );
    1.32 +    err = SecKeyGeneratePair((CFDictionaryRef)keyAttrs,&pubKey,&privKey);
    1.33 +    if (err) {
    1.34 +        Warn(@"Failed to create key-pair: %i", err);
    1.35 +        return nil;
    1.36 +    } else
    1.37 +        return [[[self alloc] initWithPublicKeyRef: pubKey privateKeyRef: privKey] autorelease];
    1.38 +}
    1.39 +
    1.40 +- (id) initWithPublicKeyRef: (SecKeyRef)publicKey privateKeyRef: (SecKeyRef)privateKey {
    1.41 +    self = [super initWithKeyRef: publicKey];
    1.42 +    if (self) {
    1.43 +        NSParameterAssert(privateKey);
    1.44 +        _privateKey = (SecKeyRef) CFRetain(privateKey);
    1.45 +    }
    1.46 +    return self;
    1.47 +}
    1.48 +
    1.49 +
    1.50 +- (NSArray*) _itemList {
    1.51 +    return $array((id)_privateKey,(id)self.keyRef);
    1.52 +}
    1.53 +
    1.54 +
    1.55 +@synthesize privateKeyRef=_privateKey;
    1.56 +
    1.57 +
    1.58 +- (NSData*) decryptData: (NSData*)data {
    1.59 +    return _crypt(_privateKey,data,kCCDecrypt);
    1.60 +}
    1.61 +    
    1.62 +
    1.63 +- (NSData*) signData: (NSData*)data {
    1.64 +    Assert(data);
    1.65 +    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    1.66 +    CC_SHA1(data.bytes,data.length, digest);
    1.67 +
    1.68 +    size_t sigLen = 1024;
    1.69 +    uint8_t sigBuf[sigLen];
    1.70 +    OSStatus err = SecKeyRawSign(_privateKey, kSecPaddingPKCS1SHA1,
    1.71 +                                   digest,sizeof(digest), //data.bytes, data.length,
    1.72 +                                   sigBuf, &sigLen);
    1.73 +    if(err) {
    1.74 +        Warn(@"SecKeyRawSign failed: %i",err);
    1.75 +        return nil;
    1.76 +    } else
    1.77 +        return [NSData dataWithBytes: sigBuf length: sigLen];
    1.78 +}
    1.79 +
    1.80 +
    1.81 +@end
    1.82 +
    1.83 +
    1.84 +#endif USE_IPHONE_API