MYPublicKey-iPhone.m
changeset 0 0a6527af039b
child 1 60e4cbbb5128
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/MYPublicKey-iPhone.m	Sat Apr 04 20:42:03 2009 -0700
     1.3 @@ -0,0 +1,96 @@
     1.4 +//
     1.5 +//  MYPublicKey-iPhone.m
     1.6 +//  MYCrypto-iPhone
     1.7 +//
     1.8 +//  Created by Jens Alfke on 3/30/09.
     1.9 +//  Copyright 2009 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "MYPublicKey.h"
    1.13 +#import "MYCrypto_Private.h"
    1.14 +
    1.15 +#if USE_IPHONE_API
    1.16 +
    1.17 +#import "MYDigest.h"
    1.18 +#import "MYErrorUtils.h"
    1.19 +
    1.20 +
    1.21 +@implementation MYPublicKey
    1.22 +
    1.23 +
    1.24 +- (void) dealloc
    1.25 +{
    1.26 +    [_digest release];
    1.27 +    [super dealloc];
    1.28 +}
    1.29 +
    1.30 +
    1.31 +- (SecExternalItemType) keyType {
    1.32 +    return kSecAttrKeyClassPublic;
    1.33 +}
    1.34 +
    1.35 +
    1.36 +- (MYPublicKey*) asPublicKey {
    1.37 +    return self;
    1.38 +}
    1.39 +
    1.40 +
    1.41 +
    1.42 +- (MYSHA1Digest*) publicKeyDigest {
    1.43 +    NSData *digestData = [self _attribute: kSecAttrApplicationLabel];
    1.44 +    if (digestData)
    1.45 +        return (MYSHA1Digest*) [MYSHA1Digest digestFromDigestData: digestData];
    1.46 +    else {
    1.47 +        Warn(@"MYKeyPair: public key didn't have digest attribute");
    1.48 +        return nil;
    1.49 +    }
    1.50 +}
    1.51 +
    1.52 +
    1.53 +- (NSData*) encryptData: (NSData*)data {
    1.54 +    return _crypt(self.keyRef,data,kCCEncrypt);
    1.55 +}
    1.56 +
    1.57 +
    1.58 +- (BOOL) verifySignature: (NSData*)signature ofData: (NSData*)data {
    1.59 +    Assert(data);
    1.60 +    Assert(signature);
    1.61 +    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    1.62 +    CC_SHA1(data.bytes,data.length, digest);
    1.63 +    OSStatus err = SecKeyRawVerify(self.keyRef, kSecPaddingPKCS1SHA1,
    1.64 +                                   digest,sizeof(digest), //data.bytes, data.length,
    1.65 +                                   signature.bytes, signature.length);
    1.66 +    return err==noErr;
    1.67 +}
    1.68 +
    1.69 +
    1.70 +@end
    1.71 +
    1.72 +
    1.73 +
    1.74 +
    1.75 +NSData* _crypt(SecKeyRef key, NSData *data, CCOperation op) {
    1.76 +    CAssert(data);
    1.77 +    size_t dataLength = data.length;
    1.78 +    size_t outputLength = MAX(dataLength, SecKeyGetBlockSize(key));
    1.79 +    void *outputBuf = malloc(outputLength);
    1.80 +    if (!outputBuf) return nil;
    1.81 +    OSStatus err;
    1.82 +    if (op==kCCEncrypt)
    1.83 +        err = SecKeyEncrypt(key, kSecPaddingNone,//PKCS1, 
    1.84 +                            data.bytes, dataLength,
    1.85 +                            outputBuf, &outputLength);
    1.86 +    else
    1.87 +        err = SecKeyDecrypt(key, kSecPaddingNone,//PKCS1, 
    1.88 +                            data.bytes, dataLength,
    1.89 +                            outputBuf, &outputLength);
    1.90 +    if (err) {
    1.91 +        free(outputBuf);
    1.92 +        Warn(@"%scrypting failed (%i)", (op==kCCEncrypt ?"En" :"De"), err);
    1.93 +        // Note: One of the errors I've seen is -9809, which is errSSLCrypto (SecureTransport.h)
    1.94 +        return nil;
    1.95 +    } else
    1.96 +        return [NSData dataWithBytesNoCopy: outputBuf length: outputLength freeWhenDone: YES];
    1.97 +}
    1.98 +
    1.99 +#endif USE_IPHONE_API