How to use ECC in iOS

前端 未结 2 1311
误落风尘
误落风尘 2021-01-17 19:03

Is there any example for use ECC in iOS?

I noticed that the kSecAttrKeyTypeEC in Apple Developer Documents, but I can\'t use it to generic Key pair.

Below co

相关标签:
2条回答
  • 2021-01-17 19:52
    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);
    

    This works, double check your key size parameter.

    Just a note, currently EC keys can only be used for signing/verifying data. Encryption/decryption returns errSecUnimplemented = -4.

    0 讨论(0)
  • 2021-01-17 19:56

    CryptoKit now supports Ed25519 in iOS13+

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

    0 讨论(0)
提交回复
热议问题