h1=. MYCrypto p=. Version 0.2 -- 12 April 2009 p=. By "Jens Alfke":mailto:jens@mooseyard.com
Based in part on code by Wade Tregaskis,
and sample code by Apple Computer. h2. Introduction *MYCrypto* is a high-level cryptography API for Mac OS X and iPhone. It's an Objective-C wrapper around the system "*Keychain*":http://developer.apple.com/documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html#//apple_ref/doc/uid/TP30000897-CH204-TP9 and *CSSM* APIs, which are notoriously hard to use, as well as *CommonCrypto*, which is easier but quite limited. MYCrypto gives you easy object-oriented interfaces to: * Symmmetric cryptography (session keys and password-based encryption) * Asymmetric cryptography (public and private keys; digital signatures) * Identity certificates (for use with SSL and CMS) * Cryptographic digests/hashes (effectively-unique IDs for data) * The Keychain (a secure, encrypted storage system for keys and passwords) * Cryptographic Message Syntax [CMS] for signing/encrypting data It's open source, released under a friendly BSD license. h3. Requirements * Mac OS X 10.5 or later _[has been tested on 10.5.6]_ * or iPhone OS 2.0 or later _[not yet tested; see Limitations section below]_ * or iPhone Simulator, for iPhone OS 2.0 or later * The "MYUtilities":http://mooseyard.com/hg/hgwebdir.cgi/MYUtilities library, which is used by MYCrypto. * _Some understanding of security and cryptography on your part!_ Even with convenient APIs, cryptographic operations still require care and knowledge to be used safely. There are already too many "examples":http://en.wikipedia.org/wiki/Wired_Equivalent_Privacy#Flaws of insecure systems that were incorrectly assembled from secure primitives; don't add your app to that list. Please read a good overview like "??Practical Cryptography??":http://www.schneier.com/book-practical.html before attempting anything the least bit fancy. h3. How To Get It * "Download the current source code":http://mooseyard.com/hg/hgwebdir.cgi/MYCrypto/archive/tip.zip * or to check out the source code using "Mercurial":http://selenic.com/mercurial/:
@hg clone http://mooseyard.com/hg/hgwebdir.cgi/MYCrypto/ MYCrypto@ * As described above, you'll also need to download or check out MYUtilities and put it in a directory next to MYCrypto. * To file or view bug reports, visit "the project tracker page":http://mooseyard.lighthouseapp.com/projects/29227/home. * Or if you're just looking: ** "Browse the source code":http://mooseyard.com/hg/hgwebdir.cgi/MYCrypto/file/tip ** "Browse the class documentation":Documentation/html/hierarchy.html h3. How To Build It With Xcode, of course. But before the _first_ time you build MYCrypto.xcode, you'll need to tell Xcode where the MYUtilities sources are. You do this by setting up a named 'Source Tree': # Open Xcode's Preferences panel # Click the "Source Trees" icon at the top # Click the "+" button to add a new item to the list # Fill in the Setting Name as "@MYUtilities@", the Display Name also as "@MYUtilities@", and the Path as the absolute filesystem path to where you downloaded MYUtilities to. _Do not use a "~" in this path!_ The compiler won't understand it and will give you errors. Now you're golden. From now on you can just open MYCrypto.xcode and press the Build button. (So far, the MYCrypto project doesn't build anything that's useful to you, like a framework ... just a tiny program that runs the unit-tests. You can add the source files to your own projects to use them.) h2. Overview The class hierarchy of MYCrypto looks like this: * "_MYKeychain_":Documentation/html/interfaceMYKeychain.html * "_MYKeychainItem_":Documentation/html/interfaceMYKeychainItem.html ** "_MYKey_":Documentation/html/interfaceMYKey.html *** "MYSymmetricKey":Documentation/html/interfaceMYSymmetricKey.html *** "MYPublicKey":Documentation/html/interfaceMYPublicKey.html *** "MYPrivateKey":Documentation/html/interfaceMYPrivateKey.html *** "MYCertificate":Documentation/html/interfaceMYCertificate.html **** "MYIdentity":Documentation/html/interfaceMYIdentity.html * "_MYDigest_":Documentation/html/interfaceMYDigest.html ** "MYSHA1Digest":Documentation/html/interfaceMYSHA1Digest.html ** "MYSHA256Digest":Documentation/html/interfaceMYSHA256Digest.html * "MYCryptor":Documentation/html/interfaceMYCryptor.html * "MYEncoder":Documentation/html/interfaceMYEncoder.html * "MYDecoder":Documentation/html/interfaceMYDecoder.html * "MYSigner":Documentation/html/interfaceMYSigner.html (_Italicized_ classes are abstract.) h2. Examples h3. Creating an RSA key-pair
MYPrivateKey *keyPair = [[MYKeychain defaultKeychain] generateRSAKeyPairOfSize: 2048];
h3. Creating a self-signed identity certificate:
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
		@"alice", @"Common Name",
		@"Alice", @"Given Name",
		@"Lidell", @"Surname",
		nil];
MYIdentity *ident = [keyPair createSelfSignedIdentityWithAttributes: attrs];

NSData *certData = ident.certificateData;
h3. Signing and encrypting a message:
NSData *cleartext = [@"Attack at dawn" dataUsingEncoding: NSUTF8StringEncoding];
MYEncoder *encoder = [[MYEncoder alloc] init];
[encoder addSigner: ident];
[encoder addRecipient: bob];
[encoder addRecipient: carla];
[encoder addData: cleartext];
[encoder finish];
NSData *ciphertext = encoder.encodedData;

sendMessage(ciphertext);
h3. Verifying and decoding a message:
NSData *ciphertext = receiveMessage();
NSError *error;
MYDecoder *decoder = [[MYDecoder alloc] initWithData: ciphertext error: &error];
if (!decoder)
    return NO;

if (!decoder.isSigned)
    return NO;
decoder.policy = [MYCertificate X509Policy];
NSMutableArray *signerCerts = [NSMutableArray array];
for (MYSigner *signer in decoder.signers) {
    if (signer.status != kCMSSignerValid) {
        return NO;
    [signerCerts addObject: signer.certificate];
}

NSData *plaintext = decoder.content;
processMessage(plaintext, signerCerts);
h2. Current Limitations h3. First off, the biggest caveat of all: * *MYCrypto 0.2 is new code and has not yet been used in any real projects. Expect bugs.* (I'm talking about my wrapper/glue code. The underlying cryptographic functionality provided by the OS is robust.) h3. Further issues with the 0.2 release: * *MYCrypto does not yet work on the iPhone.* It currently builds, but runs into problems at runtime. I'm currently trying to figure these out. (The iPhone OS Security APIs are very different from the Mac OS X ones, and I'm much less familiar with them.) However, it does work in the iPhone Simulator, which uses the OS X APIs. * Exporting symmetric keys in wrapped (encrypted) form will fail. Currently they can be exported only as raw key data. * Importing symmetric keys, in any form, will fail ... kind of a deal-breaker for using them across two computers, unfortunately. h3. Current API limitations, to be remedied in the future: * No API for accessing passwords; fortunately there are several other utility libraries that provide this. And if your code is doing cryptographic operations, it probably needs to store the keys themselves, not passwords. * No evaluation of trust in certificates (i.e. SecTrust and related APIs.) * Error reporting is too limited. Most methods indicate an error by returning nil, NULL or NO, but don't provide the standard "out" NSError parameter to provide more information. Expect the API to be refactored in the near future to remedy this. h2. References * "??Security Overview??":http://developer.apple.com/documentation/Security/Conceptual/Security_Overview/Introduction/Introduction.html (Apple) * "??Secure Coding Guide??":http://developer.apple.com/documentation/Security/Conceptual/SecureCodingGuide/Introduction.html (Apple) * "??Common Security: CDSA and CSSM, Version 2??":http://www.opengroup.org/publications/catalog/c914.htm (The Open Group) * "??Practical Cryptography??":http://www.schneier.com/book-practical.html (Ferguson and Schneier) * "??Handbook of Applied Cryptography??":http://www.cacr.math.uwaterloo.ca/hac/ (Menezes, van Oorschot, Vanstone) -- free download! * "??The Devil's InfoSec Dictionary??":http://www.csoonline.com/article/220527/The_Devil_s_Infosec_Dictionary (CSO Online)