return err;
 }
 
+/*
+ * Derive a SipHash key from the given fscrypt master key and the given
+ * application-specific information string.
+ *
+ * Note that the KDF produces a byte array, but the SipHash APIs expect the key
+ * as a pair of 64-bit words.  Therefore, on big endian CPUs we have to do an
+ * endianness swap in order to get the same results as on little endian CPUs.
+ */
+static int fscrypt_derive_siphash_key(const struct fscrypt_master_key *mk,
+                                     u8 context, const u8 *info,
+                                     unsigned int infolen, siphash_key_t *key)
+{
+       int err;
+
+       err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen,
+                                 (u8 *)key, sizeof(*key));
+       if (err)
+               return err;
+
+       BUILD_BUG_ON(sizeof(*key) != 16);
+       BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2);
+       le64_to_cpus(&key->key[0]);
+       le64_to_cpus(&key->key[1]);
+       return 0;
+}
+
 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
                               const struct fscrypt_master_key *mk)
 {
        int err;
 
-       err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, HKDF_CONTEXT_DIRHASH_KEY,
-                                 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
-                                 (u8 *)&ci->ci_dirhash_key,
-                                 sizeof(ci->ci_dirhash_key));
+       err = fscrypt_derive_siphash_key(mk, HKDF_CONTEXT_DIRHASH_KEY,
+                                        ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
+                                        &ci->ci_dirhash_key);
        if (err)
                return err;
        ci->ci_dirhash_key_initialized = true;
                if (mk->mk_ino_hash_key_initialized)
                        goto unlock;
 
-               err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
-                                         HKDF_CONTEXT_INODE_HASH_KEY, NULL, 0,
-                                         (u8 *)&mk->mk_ino_hash_key,
-                                         sizeof(mk->mk_ino_hash_key));
+               err = fscrypt_derive_siphash_key(mk,
+                                                HKDF_CONTEXT_INODE_HASH_KEY,
+                                                NULL, 0, &mk->mk_ino_hash_key);
                if (err)
                        goto unlock;
                /* pairs with smp_load_acquire() above */