// SPDX-License-Identifier: GPL-2.0

THEORY OF OPERATION
-------------------
The Intel Device Specific Methods (DSM) specification v1.7 and v1.8 [1]
introduced the following security management operations:
enable passphrase, update passphrase, unlock DIMM, disable security,
freeze security, secure (crypto) erase, overwrite, master passphrase
enable, master passphrase update, and master passphrase secure erase.

The security management for NVDIMMs is comprised of two parts. The front end
uses the Linux key management framework (trusted and encrypted keys [2]) to
store the encrypted passphrases in the kernel-managed keyring. The interface
for this is the 'keyutils' utility which uses the key management APIs in the
Linux kernel. The back end takes the decrypted payload (which is the DIMM
passphrase) and passes it to the DIMM.

Unlike other DSMs which are composed by libndctl and sent to the kernel via
an ioctl, the security DSMs are managed through the 'security' sysfs attribute
under the 'dimm' device. A 'key-ID' is written to the 'security' attribute and
the kernel pulls the associated key material from the user keyring that is
maintained by the kernel.

The security process begins with the generation of a 'master key' that is
used to seal (encrypt) the passphrase for the DIMM. There can either be one
common 'master key' that is used to encrypt every DIMM's passphrase, or a
separate key can be generated for each DIMM. The 'master key' is also referred
to as the 'key-encryption-key' (kek). The 'kek' can either be generated by the
TPM (Trusted Platform Module) on the system, or alternatively, the 'System
Master Key' can also be used as the 'kek'

For testing purposes a user key with randomized payload can also be used as
a 'kek'. See [2] for details. To perform any security operations, it is
expected that the 'kek' has been added to the kernel's user keyring as shown
in example below:

----
# keyctl show
Session Keyring
 736023423 --alswrv      0     0  keyring: _ses
 675104189 --alswrv      0 65534   \_ keyring: _uid.0
 680187394 --alswrv      0     0       \_ trusted: nvdimm-master
----

Before performing any of the security operations, all the regions associated
with the DIMM in question need to be disabled. For the 'overwrite' operation,
in addition to the 'regions', the 'dimm' also needs to be disabled.

[1] http://pmem.io/documents/NVDIMM_DSM_Interface-V1.8.pdf +
[2] https://www.kernel.org/doc/Documentation/security/keys/trusted-encrypted.rst

The following sub-sections describe specifics of each security feature.

=== UNLOCK

Unlock is performed by the kernel, however a preparation step must happen
before the unlock DSM can be issued by the kernel. It is expected that
from the initramfs, a setup command (ndctl 'load-keys') is executed before
the libnvdimm module is loaded by modprobe. This command will inject the
'kek' and the encrypted passphrases into the kernel's user keyring. During
the 'probe' of the libnvdimm driver, it will:

. Check the security state of the device and see if the DIMM is locked
. Request the associated encrypted passphrase from the kernel's user key ring
. Use the 'kek' to decrypt the passphrase
. Create the unlock DSM, copy the decrypted payload into the DSM
. Issue the DSM to unlock the DIMM

If the DIMM is already unlocked, the kernel will attempt to revalidate the
passphrase. If we fail to revalidate the passphrase, the kernel will freeze
the security and disallow any further security configuration changes. A kernel
module parameter is available to override this behavior.

=== SETUP USER PASSPHRASE

To setup the passphrase for a DIMM, it is expected that the 'kek' to be used
is present in the kernel's user keyring. The 'kek' encrypts the DIMM passphrase
using the 'enc32' key format. The plaintext passphrase is never provided by
or made visible to the user. It is instead randomly generated by the kernel and
userspace does not have access to it. Upon encryption, a binary blob of the
passphrase is written to the passphrase blob storage directory ({ndctl_keysdir}).
The user is responsible for backing up the passphrase blobs to a secure location.

=== UPDATE USER PASSPHRASE

The update user passphrase operation uses the same DSM command as enable user
passphrase. Most of the work is done on the key management side. The user has
the option of providing a new 'kek' for the new passphrase, but continuing to
use the existing 'kek' is also acceptable.
The following operations are performed for 'update-passphrase':

. Remove the encrypted passphrase from the kernel's user keyring.
. Rename the passphrase blob to old.
. Load this old passphrase blob into the keyring with an "old" name.
. Create the new passphrase and encrypt with the  'kek'.
. Send DSM with the old and new decrypted passphrases.
. Remove old passphrase and the passphrase blob from the keyring.

=== REMOVE USER PASSPHRASE

The 'key-ID' for the passphrase to be removed is written to sysfs. The kernel
then sends the DSM to disable security, and the passphrase is then removed
from the keyring, and the associated passphrase blob is deleted.

=== CRYPTO (SECURE) ERASE

This operation is similar to remove-passphrase. The kernel issues a WBINVD
instruction before and after the operation to ensure no data corruption from
a stale CPU cache. Use ndctl's sanitize-dimm command with the `--crypto-erase`
option to perform this operation.

=== OVERWRITE

This is invoked using `--overwrite` option for ndctl 'sanitize-dimm'.
The overwrite operation wipes the entire NVDIMM. The operation can take a
significant amount of time. NOTE: When the command returns successfully,
it just means overwrite has been successfully started, and not that the
overwrite is complete. Subsequently, 'ndctl wait-overwrite' can be used
to wait for the NVDIMMs that are performing overwrite. Upon successful
completion of an overwrite, the WBINVD instruction is issued by the kernel.
If both --crypto-erase and --overwrite options are supplied, then
crypto-erase is performed before overwrite.

=== SECURITY FREEZE

This operation does not require a passphrase. This will cause any security
command other than a status query to be locked out until the next boot.

=== MASTER PASSPHRASE SETUP, UPDATE, and CRYPTO ERASE

These operations are similar to the user passphrase enable and update. The only
difference is that a different passphrase is used. The master passphrase has no
relation to the master key ('kek') which is used for encryption of either
passphrase.
