2012-07-19 11 views
10

¿Hay algún ejemplo para usar ECC en iOS?Cómo usar ECC en iOS

Me di cuenta de que el kSecAttrKeyTypeEC en Apple Developer Documents, pero no puedo usarlo para un par de clave genérico.

A continuación se modifica el código del ejemplo CryptoExercise

// Container dictionaries. 
NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init]; 
NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init]; 
NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init]; 

// Set top level dictionary for the keypair. 
[keyPairAttr setObject:(id)kSecAttrKeyTypeEC forKey:(id)kSecAttrKeyType]; 
[keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:keySize] forKey:(id)kSecAttrKeySizeInBits]; 

// Set the private key dictionary. 
[privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrIsPermanent]; 
[privateKeyAttr setObject:privateTag forKey:(id)kSecAttrApplicationTag]; 
// See SecKey.h to set other flag values. 

// Set the public key dictionary. 
[publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrIsPermanent]; 
[publicKeyAttr setObject:publicTag forKey:(id)kSecAttrApplicationTag]; 
// See SecKey.h to set other flag values. 

// Set attributes to top level dictionary. 
[keyPairAttr setObject:privateKeyAttr forKey:(id)kSecPrivateKeyAttrs]; 
[keyPairAttr setObject:publicKeyAttr forKey:(id)kSecPublicKeyAttrs]; 

// SecKeyGeneratePair returns the SecKeyRefs just for educational purposes. 
sanityCheck = SecKeyGeneratePair((CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef); 
LOGGING_FACILITY(sanityCheck == noErr && publicKeyRef != NULL && privateKeyRef != NULL, @"Something really bad went wrong with generating the key pair."); 

El SanityCheck siempre vuelven -50 que significa 'errSecParam'.

Realmente no sé cómo usarlo, gracias por leer esto.

+1

El código pasó cuando keysize = 256, pero otro problema es que cuando uso SecKeyRawSign, devuelve -1, y no puedo encontrar la descripción para -1 return value, es alguien con este ¿problema? – Cylon

+0

¿ha encontrado alguna solución a este problema? – Gunhan

Respuesta

1
NSDictionary *parameters = @{ 
          (__bridge id)kSecAttrKeyType: (__bridge id)kSecAttrKeyTypeEC, 
          (__bridge id)kSecAttrKeySizeInBits: @256, 
          (__bridge id)kSecPrivateKeyAttrs: @{ 
            (__bridge id)kSecAttrIsPermanent: @YES, 
            (__bridge id)kSecAttrApplicationTag: [@"my.key.tag" dataUsingEncoding:NSUTF8StringEncoding], 
            }, 
          (__bridge id)kSecPublicKeyAttrs: @{ 
            (__bridge id)kSecAttrIsPermanent: @YES, 
            (__bridge id)kSecAttrApplicationTag: [@"my.key.pubtag" dataUsingEncoding:NSUTF8StringEncoding], 
            } 
          }; 

SecKeyRef publicKey, privateKey; 
OSStatus status = SecKeyGeneratePair((__bridge CFDictionaryRef)parameters, &publicKey, &privateKey); 

Esto funciona, compruebe dos veces el parámetro del tamaño de la clave.

Solo una nota, actualmente las claves EC solo se pueden usar para firmar/verificar datos. La encriptación/descifrado devuelve errSecUnimplemented = -4.

+0

Please [bugreport] (http://bugreport.apple.com). Apple está muy atrás en cuanto a proporcionar capacidades criptográficas para iOS. – zaph

Cuestiones relacionadas