【问题标题】:How to use ECC in iOS如何在 iOS 中使用 ECC
【发布时间】:2012-07-19 14:39:37
【问题描述】:

有在iOS中使用ECC的例子吗?

我注意到 Apple Developer Documents 中的 kSecAttrKeyTypeEC,但我不能将它用于通用密钥对。

以下代码根据 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." );

sanityCheck 总是返回 -50,这意味着 'errSecParam'。

我真的不知道怎么用,谢谢阅读。

【问题讨论】:

  • keysize=256时代码通过了,但是另一个问题是,当我使用SecKeyRawSign时,它返回-1,我找不到-1返回值的描述,有没有人遇到这个有问题吗?
  • 你找到解决这个问题的办法了吗?

标签: ios elliptic-curve


【解决方案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);

这可行,请仔细检查您的密钥大小参数。

请注意,目前 EC 密钥只能用于签名/验证数据。加解密返回errSecUnimplemented = -4.

【讨论】:

  • bugreport。 Apple 在为 iOS 提供加密功能方面远远落后。
【解决方案2】:

CryptoKit 现在在 iOS13+ 中支持 Ed25519

https://developer.apple.com/documentation/cryptokit/curve25519/signing

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    相关资源
    最近更新 更多