Programmer’s Guide
Initialization
The software can update the KMAC/SHA3 configurations only when the IP is in the idle state.
The software should check STATUS.sha3_idle before updating the configurations.
The software must first program CFG_SHADOWED.msg_endianness and CFG_SHADOWED.state_endianness at the initialization stage.
These determine the byte order of incoming messages (msg_endianness) and the Keccak state output (state_endianness).
Software Initiated KMAC/SHA3 process
This section describes the expected software process to run the KMAC/SHA3 HWIP.
At first, the software configures CFG_SHADOWED.kmac_en for KMAC operation.
If KMAC is enabled, the software should configure CFG_SHADOWED.mode to cSHAKE and CFG_SHADOWED.kstrength to 128 or 256 bit security strength.
If KMAC is enabled, software must also set ENTROPY_CFG_SHADOWED.entropy_boot_mode to 0 and it is recommended to enable the EDN Mode (see EDN Entropy Mode for details).
Software must then wait for the STATUS.entropy_ready bit to go high.
The software also updates PREFIX registers if cSHAKE mode is used.
Current design does not convert cSHAKE mode to SHAKE even if PREFIX is empty string.
It is the software’s responsibility to change the CFG_SHADOWED.mode to SHAKE in case of empty PREFIX.
The KMAC/SHA3 HWIP uses PREFIX registers as it is.
It means that the software should update PREFIX with encoded values.
If CFG_SHADOWED.kmac_en is set, the software should update the secret key.
The software prepares two shares of the secret key and selects its length in KEY_LEN then writes the shares of the secret key to KEY_SHARE0 and KEY_SHARE1 .
The two shares of the secret key are the values that represent the secret key value when they are XORed together.
The software can XOR the unmasked secret key with entropy.
The XORed value is a share and the entropy used is the other share.
Alternatively, the software can set CFG_SHADOWED.sideload to use the key provided by the key manager.
While the key manager runs an advance or generate operation with KMAC as its KDF engine, it forwards its internal key on the sideload interface.
During that time the sideloaded key is unusable for software, and a software-initiated KMAC operation with the sideloaded key fails with a KeyNotValid error (see Error Handling).
This also happens if the key manager operation starts while the KMAC operation is ongoing.
The software must therefore not run a KMAC operation with the sideloaded key concurrently with a key manager advance or generate operation.
After configuring, the software notifies the KMAC/SHA3 engine to accept incoming messages by issuing Start command into CMD .
If Start command is not issued, the incoming message is discarded.
If KMAC is enabled, the software pushes the right_encode(output_length) value at the end of the message.
For example, if the desired output length is 256 bit, the software writes 0x00020100 to MSG_FIFO.
After the software pushes all messages, it issues Process command to CMD for SHA3 engine to complete the sponge absorbing process.
SHA3 hashing engine pads the incoming message as defined in the SHA3 specification.
After the SHA3 engine completes the sponge absorbing step, it generates kmac_done interrupt.
Or the software can poll the STATUS.squeeze bit until it becomes 1.
In this stage, the software may run the Keccak round manually.
If the desired digest length is greater than the Keccak rate, the software issues Run command for the Keccak round logic to run one full round after the software reads the current available Keccak state.
At this stage, KMAC/SHA3 does not raise an interrupt when the Keccak round completes the software initiated manual run.
The software should check STATUS.squeeze register field for the readiness of STATE value.
After the software reads all the digest values, it issues Done command to CMD register to clear the internal states.
Done command clears the Keccak state, FSM in SHA3 and KMAC, and a few internal variables.
Secret key and other software programmed values won’t be reset.
Endianness
This KMAC HWIP operates in little-endian. Internal SHA3 hashing engine receives in 64-bit granularity. The data written to SHA3 is assumed to be little endian.
The software may write/read the data in big-endian order if CFG_SHADOWED.msg_endianness or CFG_SHADOWED.state_endianness is set.
If the endianness bit is 1, the data is assumed to be big-endian.
So, the internal logic byte-swap the data.
For example, when the software writes 0xDEADBEEF with endianness as 1, the logic converts it to 0xEFBEADDE then writes into MSG_FIFO.
The software managed secret key, and the prefix are always little-endian values.
For example, if the software configures the function name N in KMAC operation, it writes encode_string("KMAC").
The encode_string("KMAC") represents 0x01 0x20 0x4b 0x4d 0x41 0x43 in byte order.
The software writes 0x4d4b2001 into PREFIX0 and 0x????4341 into PREFIX1 .
Upper 2 bytes can vary depending on the customization input string S.
Entropy Configuration
The internal pseudo-random number generator (PRNG) can be configured in different modes.
Entropy Boot Mode
This mode is enabled when the ENTROPY_CFG_SHADOWED.entropy_boot_mode bit reads as 1 which is always the case after reset.
The sole purpose of the Entropy Boot Mode is to enable the KMAC HWIP block to perform the initial ROM integrity check directly after system reset.
Before the module can operate in KMAC mode, software has to disable the Entropy Boot Mode by setting the ENTROPY_CFG_SHADOWED.entropy_boot_mode bit to 0 and either enabling the EDN or Software Entropy Mode using the ENTROPY_CFG_SHADOWED.entropy_mode bit.
Note that the Entropy Mode must be configured before or with the same register write that disables the Entropy Boot Mode.
Because disabling the Entropy Boot Mode immediately starts the initial reseed operation in the currently configured Entropy Mode and the ENTROPY_CFG_SHADOWED register cannot be updated while reseed operations are ongoing.
Once the Entropy Boot Mode is disabled, it cannot be re-enabled again until the next reset.
EDN Entropy Mode
For regular operation, ENTROPY_CFG_SHADOWED.entropy_mode should be set to EDN Mode.
To actually enable this mode, ENTROPY_CFG_SHADOWED.entropy_boot_mode needs to read as 0.
In this mode, the module automatically reseeds the internal PRNG with fresh entropy from EDN after every ENTROPY_CFG_SHADOWED.edn_mode_refresh_threshold key absorption phases in KMAC mode (CFG_SHADOWED.kmac_en set to 1).
Alternatively, software can manually initiate reseeding operations using the CMD.entropy_req bit while the module is idle.
While previous versions of the OpenTitan KMAC hardware IP block could deadlock the system due to the Message FIFO running full while the KMAC HWIP was waiting for fresh entropy from EDN, this version of the KMAC HWIP is entirely deadlock free. In particular:
- Irrespective of the configured entropy mode, any operation including KMAC mode operations will finish without blocking the SHA3 engine.
- Even if a PRNG reseed operation is triggered after the key absorption phase in KMAC mode or during the message absorption phase, the PRNG can continue providing randomness to the SHA3 engine.
- This means software can always push data into the Message FIFO. In case the Message FIFO is full, software is stalled for at most 100 clock cycles.
The only thing that software needs to ensure before starting an operation in KMAC mode is that the STATUS.entropy_ready bit reads as 1, indicating that the previous reseed operation has finished and the PRNG is ready to provide randomness to the SHA3 engine.
If this bit doesn’t read as 1 upon starting a KMAC mode operation, an ErrSwHashingWithoutEntropyReady error is returned to software, while application interfaces return a service rejected error.
Software Entropy Mode
As an alternative to the EDN Mode, ENTROPY_CFG_SHADOWED.entropy_mode can be set to Software Mode.
To actually enable this mode, ENTROPY_CFG_SHADOWED.entropy_boot_mode needs to read as 0.
Software then has to wait for the STATUS.entropy_reseeding bit to get set to provide an initial seed to the PRNG by writing the ENTROPY_SEED register 6 times.
Unlike previous versions of the OpenTitan KMAC hardware IP block, this version of the KMAC HWIP allows to move out of the Software Entropy Mode as well as to reseed the PRNG at a later point, unless the entropy configuration is locked down.
Manually triggering PRNG reseeds
If the value of ENTROPY_REFRESH_HASH_CNT gets sufficiently close to what software considers the maximum value, it should:
- Ensure that the entropy complex is running (only required when running in EDN Mode).
- Check that the KMAC module is idle by reading the
STATUS.sha3_idlebit. - Trigger a manual reseed operation by setting the
CMD.entropy_reqbit. - Wait for the
STATUS.entropy_reseedingbit to get set. Due to the ongoing reseed operation, theSTATUS.entropy_reseedingbit should remain 1 for an extended period of time. - Wait for the
STATUS.entropy_reseedingbit to get cleared and theSTATUS.entropy_readybit to get set when running in EDN Mode. When running in Software Mode, provide 6 times 32 bits of entropy via theENTROPY_SEEDregister. After the last write operation theSTATUS.entropy_readybit will get set back to 1.
The ENTROPY_REFRESH_HASH_CNT register should now read as 0.
Locking down the Entropy Configuration
In some use cases, it may be desirable for software to lock down the entropy configuration and even prevent further reseed operations, e.g., if later software stages aren’t trusted. To lock the entropy configuration, software should:
- Ensure the proper configuration is set in
ENTROPY_CFG_SHADOWED. In particular, if software wants to prevent further reseed operations,ENTROPY_CFG_SHADOWED.edn_mode_refresh_thresholdmust be set to 0 to disable automatic reseed operations in EDN Mode andENTROPY_CFG_SHADOWED.ignore_cmd_entropy_reqmust be set to 0 to ignore manual reseed requests in both EDN and Software Mode. - Set the
ENTROPY_CFG_LOCKregister to 1.
After that point, the entropy configuration is locked until the next reset.
Error Handling
When the KMAC HW IP encounters an error, it raises the kmac_err IRQ.
SW can then read the ERR_CODE CSR to obtain more information about the error.
Having handled that IRQ, SW is expected to clear the kmac_err bit in the INTR_STATE CSR before exiting the ISR.
When SW has handled the error condition, it is expected to set the err_processed bit in the CMD CSR.
The internal SHA3 engine then flushes its FIFOs and state, which may take a few cycles.
The KMAC HW IP is ready for operation again as soon as the sha3_idle bit in the STATUS CSR is set; SW must not change the configuration of or send commands to the KMAC HW IP before that.
If the error occurred while the KMAC HW IP was being used from SW (i.e., not via an HW application interface), the kmac_done IRQ is raised when the KMAC HW IP is ready again.
KMAC/SHA3 context switching
This version of KMAC/SHA3 HWIP does support context switching enabling software to save the current hashing engine state and initiate a new high priority hashing operation. The IP enables software to restore the previous hashing state later and continue the operation.
Context switching is supported for all hashing modes.
The context
The context is the internal Keccak state that is exposed over STATE.
Software must keep this state secret as computing back to the used key is possible.
If EnMasking is enabled, the state is kept in two shares and both of them are part of the context.
Software does not need to unmask the state, saving and restoring both shares as they are is sufficient.
If EnMasking is disabled, the second share reads as zero and writes to it are ignored.
The secret key is not directly part of the context.
It has been absorbed into the Keccak state when the operation was started, so the KEY_SHARE0 and KEY_SHARE1 registers do not need to be restored.
Software must, however, restore CFG_SHADOWED to the value that was used when the context was saved.
CFG_SHADOWED.kstrength determines the block size of the remaining message and CFG_SHADOWED.mode the padding that terminates the operation.
Message block alignment
Saving a context is only possible in the absorb phase at the boundary of complete message blocks. A message block is complete when the message written since the last Start or Continue command is a multiple of the Keccak rate. The Keccak rate depends on the configured mode and security strength:
CFG_SHADOWED.mode | CFG_SHADOWED.kstrength | Block size |
|---|---|---|
| SHA3 | 224 | 144 bytes |
| SHA3 | 256 | 136 bytes |
| SHA3 | 384 | 104 bytes |
| SHA3 | 512 | 72 bytes |
| SHAKE, cSHAKE | 128 | 168 bytes |
| SHAKE, cSHAKE | 256 | 136 bytes |
Bytes that have been written but not yet absorbed are not part of the Keccak state and can therefore not be saved.
If software issues the stop command in this situation, ErrSwStopNotBlockAligned is reported, see Saving a context.
Saving a context
To save a context, software needs to follow this procedure:
- Write the
stopcommand intoCMD. - Poll
STATUS.sha3_stoppedor wait for thekmac_doneinterrupt. - Read
STATE. - Write the
donecommand intoCMD.
The last step clears the Keccak state. It prevents the context of one message stream from leaking into another one and it returns the engine into the idle state, which is required before a context can be restored.
If the message is not block aligned when the stop command is issued, ErrSwStopNotBlockAligned is reported and the absorbed data of the incomplete block is discarded.
The KMAC HWIP still enters the stopped state, so software releases it with the done command and starts the message from the beginning.
No other command is accepted in that state.
Restoring a context
To resume a previously saved context, KMAC HWIP must be idle, and software needs to follow this procedure:
- Restore
CFG_SHADOWEDto the configuration that was used when the context was saved. - In KMAC mode, ensure the entropy is configured by setting
ENTROPY_CFG_SHADOWED.entropy_boot_modeto 0 andENTROPY_CFG_SHADOWED.entropy_modeto the desired mode, as for thestartcommand. - Wait for the
STATUS.entropy_readybit to be set. Otherwise, thecontinuecommand is discarded andErrSwHashingWithoutEntropyReadyis reported. - Write the
state_writecommand intoCMD. - Poll
STATUS.state_writeuntil it is set, or until thekmac_errinterrupt is raised (see below). - Write the saved context back into
STATE. - Write the
continuecommand intoCMD.
The state_write command claims the KMAC/SHA3 HWIP for software, preventing that an application interface takes the block over during the restore and clears the Keccak state.
Hence, writes to the STATE register are only accepted when the IP block was claimed using this command.
The command also clears the Keccak state itself, so a restore always starts from a known state and the continue command resumes only the context written after the claim.
When the application interface already claims the HWIP block, or claims it at the same time, the state_write command is discarded and ERR_CODE reports ErrSwIssuedCmdInAppActive.
STATUS.state_write is then never set, so software must not poll it without also checking for this error, as it would otherwise deadlock.
On this error, software waits until the application interface has finished its operation and issues the state_write command again.
If software decides not to resume a context after claiming the block, it can release the HWIP with the done command.
As after a completed operation, the done command clears the Keccak state.
In contrast to the start command, the continue command does not process the prefix and the secret key block again, as both are already part of the restored context.
Afterwards software writes further message blocks and either saves the context again, or finalizes the operation with the process command as described in Software Initiated KMAC/SHA3 process.
The final message block does not need to be complete.
Restrictions
Context switching is not available when a sideloaded key is used.
The Keccak function is public and invertible, so software can undo the absorb of every message block it wrote itself, one inversion per block, until it reaches the state after the secret key and recovers a key it is not supposed to know.
Saving later in the message does not help, as every state in the absorbing stage leads back to the key.
The stop, state_write, and continue commands are discarded while CFG_SHADOWED.sideload is set.
Requests from the hardware application interfaces are stalled while software holds the block through the state_write command, until it issues the done command.
The continue command does not release it.
Software must therefore also issue done on its error paths.
The following errors are reported in ERR_CODE if the procedures above are not followed:
| Error | Description |
|---|---|
ErrSwStopNotBlockAligned | The stop command was issued but the message is not a multiple of the block size. The absorb is ended anyway and the incomplete block is discarded, so the operation cannot be resumed and has to be started again. |
ErrSwSaveRestoreSideload | The stop, state_write or continue command was issued while a sideloaded key is selected. |
ErrSwIssuedCmdInAppActive | A command, for example state_write, was issued while a hardware application interface was using the block. |
ErrSwCmdSequence | A command was issued that is not allowed in the current state, e.g. run after the stop command, or continue without a preceding state_write command. |
ErrSwHashingWithoutEntropyReady | The continue command was issued in KMAC mode while masking is enabled, but the PRNG is not ready and needs to be reseeded. As for the start command, the command is discarded. |