This specification is work in progress and will see significant changes before it can be considered final. We invite input of all kind through the standard means of the OpenTitan project; a good starting point is filing an issue in our GitHub issue tracker.
This document specifies functionality of the OpenTitan Big Number Accelerator, or OTBN. OTBN is a coprocessor for asymmetric cryptographic operations like RSA or Elliptic Curve Cryptography (ECC).
This module conforms to the [Comportable guideline for peripheral functionality]({{< relref “doc/rm/comportability_specification” >}}). See that document for integration overview within the broader top level system.
OTBN is a processor, specialized for the execution of security-sensitive asymmetric (public-key) cryptography code, such as RSA or ECC. Such algorithms are dominated by wide integer arithmetic, which are supported by OTBN's 256b wide data path, registers, and instructions which operate these wide data words. On the other hand, the control flow is clearly separated from the data, and reduced to a minimum to avoid data leakage.
The data OTBN processes is security-sensitive, and the processor design centers around that. The design is kept as simple as possible to reduce the attack surface and aid verification and testing. For example, no interrupts or exceptions are included in the design, and all instructions are designed to be executable within a single cycle.
OTBN is designed as a self-contained co-processor with its own instruction and data memory, which is accessible as a bus device.
OTBN is not designed to be compatible with other cryptographic accelerators. It received some inspiration from assembly code available from the Chromium EC project, which has been formally verified within the Fiat Crypto project.
OTBN is a processor with a custom instruction set. The full ISA description can be found in our [ISA manual]({{< relref “hw/ip/otbn/doc/isa” >}}). The instruction set is split into two groups:
OTBN has 32 General Purpose Registers (GPRs), each of which is 32b wide. The GPRs are defined in line with RV32I and are mainly used for control flow. They are accessed through the base instruction subset. GPRs aren't used by the main data path; this operates on the wide data registers, a separate register file, controlled by the big number instructions.
Access to the call stack
Note: Currently, OTBN has no “standard calling convention,” and GPRs other than x0
and x1
can be used for any purpose. If a calling convention is needed at some point, it is expected to be aligned with the RISC-V standard calling conventions, and the roles assigned to registers in that convention. Even without a agreed-on calling convention, software authors are encouraged to follow the RISC-V calling convention where it makes sense. For example, good choices for temporary registers are x6
, x7
, x28
, x29
, x30
, and x31
.
OTBN has an in-built call stack which is accessed through the x1
GPR. This is intended to be used as a return address stack, containing return addresses for the current stack of function calls. See the documentation for [JAL
]({{< relref “hw/ip/otbn/doc/isa#jal” >}}) and [JALR
]({{< relref “hw/ip/otbn/doc/isa#jalr” >}}) for a description of how to use it for this purpose.
The call stack has a maximum depth of 8 elements. Each instruction that reads from x1
pops a single element from the stack. Each instruction that writes to x1
pushes a single element onto the stack. An instruction that reads from an empty stack or writes to a full stack, causes OTBN to stop, raising an alert and setting the ERR_CODE
register to ErrCodeCallStack
.
A single instruction can both read and write to the stack. In this case, the read is ordered before the write. Providing the stack has at least one element, this is allowed, even if the stack is full.
Control and Status Registers (CSRs) are 32b wide registers used for “special” purposes, as detailed in their description; they are not related to the GPRs. CSRs can be accessed through dedicated instructions, CSRRS
and CSRRW
.
In addition to the 32b wide GPRs, OTBN has a second “wide” register file, which is used by the big number instruction subset. This register file consists of NWDR = 32 Wide Data Registers (WDRs). Each WDR is WLEN = 256b wide.
Wide Data Registers (WDRs) and the 32b General Purpose Registers (GPRs) are separate register files. They are only accessible through their respective instruction subset: GPRs are accessible from the base instruction subset, and WDRs are accessible from the big number instruction subset (BN
instructions).
Register |
---|
w0 |
w1 |
... |
w31 |
OTBN has 256b Wide Special purpose Registers (WSRs). These are analogous to the 32b CSRs, but are used by big number instructions. They can be accessed with the [BN.WSRRS
]({{< relref “hw/ip/otbn/doc/isa#bnwsrrs” >}}) and [BN.WSRRW
]({{< relref “hw/ip/otbn/doc/isa#bnwsrrw” >}}) instructions.
The modulus used by the [BN.ADDM
]({{< relref “hw/ip/otbn/doc/isa#bnaddm” >}}) and [BN.SUBM
]({{< relref “hw/ip/otbn/doc/isa#bnsubm” >}}) instructions. This WSR is also visible as CSRs MOD0
through to MOD7
.
In addition to the wide register file, OTBN maintains global state in two groups of flags for the use by wide integer operations. Flag groups are named Flag Group 0 (FG0
), and Flag Group 1 (FG1
). Each group consists of four flags. Each flag is a single bit.
C
(Carry flag). Set to 1 an overflow occurred in the last arithmetic instruction.
L
(LSb flag). The least significant bit of the result of the last arithmetic or shift instruction.
M
(MSb flag) The most significant bit of the result of the last arithmetic or shift instruction.
Z
(Zero Flag) Set to 1 if the result of the last operation was zero; otherwise 0.
The L
, M
, and Z
flags are determined based on the result of the operation as it is written back into the result register, without considering the overflow bit.
OTBN has two instructions for hardware-assisted loops: [LOOP
]({{< relref “hw/ip/otbn/doc/isa#loop” >}}) and [LOOPI
]({{< relref “hw/ip/otbn/doc/isa#loopi” >}}). Both use the same state for tracking control flow. This is a stack of tuples containing a loop count, start address and end address. The stack has a maximum depth of eight and the top of the stack is the current loop.
{{< hwcfg “hw/ip/otbn/data/otbn.hjson” >}}
To be filled in as we create the implementation.
By design, OTBN is a simple processor and has essentially no error handling support. When anything goes wrong (an out-of-bounds memory operation, an invalid instruction encoding, etc.), OTBN will stop fetching instructions, and set the ERR_CODE
register and the err
bit of the INTR_STATE
register.
This section will be written as we move on in the design and implementation process.
The OTBN processor core has access to two dedicated memories: an instruction memory (IMEM), and a data memory (DMEM). Each memory is 4 kiB in size.
The memory layout follows the Harvard architecture. Both memories are byte-addressed, with addresses starting at 0.
The instruction memory (IMEM) is 32b wide and provides the instruction stream to the OTBN processor; it cannot be read or written from user code through load or store instructions.
The data memory (DMEM) is 256b wide and read-write accessible from the base and big number instruction subsets of the OTBN processor core. When accessed from the base instruction subset through the LW
or SW
instructions, accesses must read or write 32b-aligned 32b words. When accessed from the big number instruction subset through the BN.LID
or BN.SID
instructions, accesses must read or write 256b-aligned 256b words.
Both memories can be accessed through OTBN's register interface ({{< regref “DMEM” >}} and {{< regref “IMEM” >}}) only when OTBN is idle, as indicated by the {{< regref “STATUS.busy”>}} flag. All memory accesses through the register interface must be word-aligned 32b word accesses.
The exact sequence of operations is not yet finalized.
Rough expected process:
1
to {{< regref “CMD.start” >}}done
interrupt{{< dif_listing “sw/device/lib/dif/dif_otbn.h” >}}
{{< registers “hw/ip/otbn/data/otbn.hjson” >}}
The big number instruction subset of OTBN generally operates on WLEN bit numbers. BN.MULQACC
operates with WLEN/4 bit operands (with a full WLEN accumulator). This section outlines two techniques to perform larger multiplies by composing multiple BN.MULQACC
instructions.
This instruction sequence multiplies the lower half of w0
by the upper half of w0
placing the result in w1
.
BN.MULQACC.Z w0.0, w0.2, 0 BN.MULQACC w0.0, w0.3, 64 BN.MULQACC w0.1, w0.2, 64 BN.MULQACC.WO w1, w0.1, w0.3, 128
The shift out functionality can be used to perform larger multiplications without extra adds. The table below shows how two registers w0
and w1
can be multiplied together to give a result in w2
and w3
. The cells on the right show how the result is built up a0:a3 = w0.0:w0.3
and b0:b3 = w1.0:w1.3
. The sum of a column represents WLEN/4 bits of a destination register, where c0:c3 = w2.0:w2.3
and d0:d3 = w3.0:w3.3
. Each cell with a multiply in takes up two WLEN/4-bit columns to represent the WLEN/2-bit multiply result. The current accumulator in each instruction is represented by highlighted cells where the accumulator value will be the sum of the highlighted cell and all cells above it.
The outlined technique can be extended to arbitrary bit widths but requires unrolled code with all operands in registers.
Code snippets giving examples of 256x256 and 384x384 multiplies can be found in sw/otbn/code-snippets/mul256.s
and sw/otbn/code-snippets/mul384.s
.