首页 > 解决方案 > Password hashing using CryptoKit

问题描述

I'm using (CryptoKit) to use AES-GCM to encrypt some data and authenticate it as well.

However, I was wondering how I would generate an AES-GCM key from a plain text password. Normally, you would use a KDF function for that, like PBKDF2.

In CryptoKit, there is a HKDF class which does about what I want: https://developer.apple.com/documentation/cryptokit/hkdf

However, I am wondering what KDF algorithm the DeriveKey function uses. Does it use PBKDF2? Does it use bcrypt? If so, how do I specify settings, or are the settings automatically determined?

标签: swiftmacossecurityencryption

解决方案


HKDF is defined in RFC5869. It is intended to generate keys from some cryptographically secure "keying material" (IKM). It is not intended for stretching a human-generated password. As discussed in section 4 Applications of HKDF:

On the other hand, it is anticipated that some applications will not be able to use HKDF "as-is" due to specific operational requirements, or will be able to use it but without the full benefits of the scheme. One significant example is the derivation of cryptographic keys from a source of low entropy, such as a user's password. The extract step in HKDF can concentrate existing entropy but cannot amplify entropy. In the case of password-based KDFs, a main goal is to slow down dictionary attacks using two ingredients: a salt value, and the intentional slowing of the key derivation computation. HKDF naturally accommodates the use of salt; however, a slowing down mechanism is not part of this specification. Applications interested in a password-based KDF should consider whether, for example, [PKCS5] meets their needs better than HKDF.

I don't believe that CryptoKit offers a PBKDF of any kind (PBKDF2, scrypt, bcrypt, argon2). It's a very limited framework (I have yet to find a situation where it was useful). You will likely need to continue to use CommonCrypto for this, or implement it yourself (or use something like CryptoSwift, which I believe implements several).


推荐阅读