Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Implementation of HKDF ("HMAC-based Extract-and-Expand Key Derivation
0004  * Function"), aka RFC 5869.  See also the original paper (Krawczyk 2010):
0005  * "Cryptographic Extraction and Key Derivation: The HKDF Scheme".
0006  *
0007  * This is used to derive keys from the fscrypt master keys.
0008  *
0009  * Copyright 2019 Google LLC
0010  */
0011 
0012 #include <crypto/hash.h>
0013 #include <crypto/sha2.h>
0014 
0015 #include "fscrypt_private.h"
0016 
0017 /*
0018  * HKDF supports any unkeyed cryptographic hash algorithm, but fscrypt uses
0019  * SHA-512 because it is well-established, secure, and reasonably efficient.
0020  *
0021  * HKDF-SHA256 was also considered, as its 256-bit security strength would be
0022  * sufficient here.  A 512-bit security strength is "nice to have", though.
0023  * Also, on 64-bit CPUs, SHA-512 is usually just as fast as SHA-256.  In the
0024  * common case of deriving an AES-256-XTS key (512 bits), that can result in
0025  * HKDF-SHA512 being much faster than HKDF-SHA256, as the longer digest size of
0026  * SHA-512 causes HKDF-Expand to only need to do one iteration rather than two.
0027  */
0028 #define HKDF_HMAC_ALG       "hmac(sha512)"
0029 #define HKDF_HASHLEN        SHA512_DIGEST_SIZE
0030 
0031 /*
0032  * HKDF consists of two steps:
0033  *
0034  * 1. HKDF-Extract: extract a pseudorandom key of length HKDF_HASHLEN bytes from
0035  *    the input keying material and optional salt.
0036  * 2. HKDF-Expand: expand the pseudorandom key into output keying material of
0037  *    any length, parameterized by an application-specific info string.
0038  *
0039  * HKDF-Extract can be skipped if the input is already a pseudorandom key of
0040  * length HKDF_HASHLEN bytes.  However, cipher modes other than AES-256-XTS take
0041  * shorter keys, and we don't want to force users of those modes to provide
0042  * unnecessarily long master keys.  Thus fscrypt still does HKDF-Extract.  No
0043  * salt is used, since fscrypt master keys should already be pseudorandom and
0044  * there's no way to persist a random salt per master key from kernel mode.
0045  */
0046 
0047 /* HKDF-Extract (RFC 5869 section 2.2), unsalted */
0048 static int hkdf_extract(struct crypto_shash *hmac_tfm, const u8 *ikm,
0049             unsigned int ikmlen, u8 prk[HKDF_HASHLEN])
0050 {
0051     static const u8 default_salt[HKDF_HASHLEN];
0052     int err;
0053 
0054     err = crypto_shash_setkey(hmac_tfm, default_salt, HKDF_HASHLEN);
0055     if (err)
0056         return err;
0057 
0058     return crypto_shash_tfm_digest(hmac_tfm, ikm, ikmlen, prk);
0059 }
0060 
0061 /*
0062  * Compute HKDF-Extract using the given master key as the input keying material,
0063  * and prepare an HMAC transform object keyed by the resulting pseudorandom key.
0064  *
0065  * Afterwards, the keyed HMAC transform object can be used for HKDF-Expand many
0066  * times without having to recompute HKDF-Extract each time.
0067  */
0068 int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
0069               unsigned int master_key_size)
0070 {
0071     struct crypto_shash *hmac_tfm;
0072     u8 prk[HKDF_HASHLEN];
0073     int err;
0074 
0075     hmac_tfm = crypto_alloc_shash(HKDF_HMAC_ALG, 0, 0);
0076     if (IS_ERR(hmac_tfm)) {
0077         fscrypt_err(NULL, "Error allocating " HKDF_HMAC_ALG ": %ld",
0078                 PTR_ERR(hmac_tfm));
0079         return PTR_ERR(hmac_tfm);
0080     }
0081 
0082     if (WARN_ON(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
0083         err = -EINVAL;
0084         goto err_free_tfm;
0085     }
0086 
0087     err = hkdf_extract(hmac_tfm, master_key, master_key_size, prk);
0088     if (err)
0089         goto err_free_tfm;
0090 
0091     err = crypto_shash_setkey(hmac_tfm, prk, sizeof(prk));
0092     if (err)
0093         goto err_free_tfm;
0094 
0095     hkdf->hmac_tfm = hmac_tfm;
0096     goto out;
0097 
0098 err_free_tfm:
0099     crypto_free_shash(hmac_tfm);
0100 out:
0101     memzero_explicit(prk, sizeof(prk));
0102     return err;
0103 }
0104 
0105 /*
0106  * HKDF-Expand (RFC 5869 section 2.3).  This expands the pseudorandom key, which
0107  * was already keyed into 'hkdf->hmac_tfm' by fscrypt_init_hkdf(), into 'okmlen'
0108  * bytes of output keying material parameterized by the application-specific
0109  * 'info' of length 'infolen' bytes, prefixed by "fscrypt\0" and the 'context'
0110  * byte.  This is thread-safe and may be called by multiple threads in parallel.
0111  *
0112  * ('context' isn't part of the HKDF specification; it's just a prefix fscrypt
0113  * adds to its application-specific info strings to guarantee that it doesn't
0114  * accidentally repeat an info string when using HKDF for different purposes.)
0115  */
0116 int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
0117             const u8 *info, unsigned int infolen,
0118             u8 *okm, unsigned int okmlen)
0119 {
0120     SHASH_DESC_ON_STACK(desc, hkdf->hmac_tfm);
0121     u8 prefix[9];
0122     unsigned int i;
0123     int err;
0124     const u8 *prev = NULL;
0125     u8 counter = 1;
0126     u8 tmp[HKDF_HASHLEN];
0127 
0128     if (WARN_ON(okmlen > 255 * HKDF_HASHLEN))
0129         return -EINVAL;
0130 
0131     desc->tfm = hkdf->hmac_tfm;
0132 
0133     memcpy(prefix, "fscrypt\0", 8);
0134     prefix[8] = context;
0135 
0136     for (i = 0; i < okmlen; i += HKDF_HASHLEN) {
0137 
0138         err = crypto_shash_init(desc);
0139         if (err)
0140             goto out;
0141 
0142         if (prev) {
0143             err = crypto_shash_update(desc, prev, HKDF_HASHLEN);
0144             if (err)
0145                 goto out;
0146         }
0147 
0148         err = crypto_shash_update(desc, prefix, sizeof(prefix));
0149         if (err)
0150             goto out;
0151 
0152         err = crypto_shash_update(desc, info, infolen);
0153         if (err)
0154             goto out;
0155 
0156         BUILD_BUG_ON(sizeof(counter) != 1);
0157         if (okmlen - i < HKDF_HASHLEN) {
0158             err = crypto_shash_finup(desc, &counter, 1, tmp);
0159             if (err)
0160                 goto out;
0161             memcpy(&okm[i], tmp, okmlen - i);
0162             memzero_explicit(tmp, sizeof(tmp));
0163         } else {
0164             err = crypto_shash_finup(desc, &counter, 1, &okm[i]);
0165             if (err)
0166                 goto out;
0167         }
0168         counter++;
0169         prev = &okm[i];
0170     }
0171     err = 0;
0172 out:
0173     if (unlikely(err))
0174         memzero_explicit(okm, okmlen); /* so caller doesn't need to */
0175     shash_desc_zero(desc);
0176     return err;
0177 }
0178 
0179 void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf)
0180 {
0181     crypto_free_shash(hkdf->hmac_tfm);
0182 }