Go Assembly by Example: AES

This example is taken from the AES package of the standard Go library. It makes use of Go Assembly to leverage Intel’s hardware support for AES, calling the AES-NI CPU instructions that can perform a “round” of encryption or decrpytion of the AES algorithm.

  1. package aes

nr is the number of rounds that the src plaintext will go through to get encrypted. xk points to the first element of a []uint32 slice containing all the 128-bit round keys that were derived from the main key. dst points to a []byte slice that will be written with the resulting ciphertext.

  1. func encryptBlockAsm(nr int, xk *uint32, dst, src *byte)
  2. #include "textflag.h"

As you can see, the total length of the arguments and return values has been omitted: the last argument of TEXT could have been $0-20. This information is optional but advised as it can be easily checked via the go vet command line tool.

  1. TEXT ·encryptBlockAsm(SB),NOSPLIT,$0
  2. MOVQ nr+0(FP), CX
  3. MOVQ xk+8(FP), AX
  4. MOVQ dst+16(FP), DX
  5. MOVQ src+24(FP), BX

X0, X1, … are SSE registers allowing you to operate on large numbers (128-bit) via SIMD instructions. Lucky us, the sizes of the input, output and state of AES are always 128-bit. MOVUPS is used to load the first 128-bit round key pointed by xk (derived from the main key) and the input to the state src into these special registers. Before starting the encryption process, AES “whiten” the input (at this point still a plaintext) by XORing it with the first round key. This is done on the 128-bit registers via the the PXOR instruction.

  1. MOVUPS 0(AX), X1
  2. MOVUPS 0(BX), X0
  3. ADDQ $16, AX // next round key
  4. PXOR X1, X0

AES accepts keys of various length: 128-bit, 192-bit and 256-bit. These three versions all accept a different number of rounds. We compare the nr argument with the number of rounds for AES-192 (12 rounds) to know what algorithm we’re dealing with. If nr is equal to 12 (JE) we jump to the label Lenc196, if it’s less (JB) we jump to the label Lenc128, if it’s more we continue.

  1. SUBQ $12, CX
  2. JE Lenc196
  3. JB Lenc128
  4. Lenc256:

A round always works the same way. The relevant 128-bit round key is loaded in a 128-bit SSE register (X1) then the AESENC instruction is called on the state (X0) and the subkey.

  1. MOVUPS 0(AX), X1
  2. AESENC X1, X0
  3. MOVUPS 16(AX), X1
  4. AESENC X1, X0
  5. ADDQ $32, AX // next round keys

AES-256 has two more rounds compare to AES-196, so when the previous two operations are done we can fall through to the AES-192 branch of the code.

  1. Lenc196:
  2. MOVUPS 0(AX), X1
  3. AESENC X1, X0
  4. MOVUPS 16(AX), X1
  5. AESENC X1, X0
  6. ADDQ $32, AX

The previous note respectively applies to AES-192 and AES-128.

  1. Lenc128:
  2. MOVUPS 0(AX), X1
  3. AESENC X1, X0
  4. MOVUPS 16(AX), X1
  5. AESENC X1, X0
  6. MOVUPS 32(AX), X1
  7. AESENC X1, X0
  8. MOVUPS 48(AX), X1
  9. AESENC X1, X0
  10. MOVUPS 64(AX), X1
  11. AESENC X1, X0
  12. MOVUPS 80(AX), X1
  13. AESENC X1, X0
  14. MOVUPS 96(AX), X1
  15. AESENC X1, X0
  16. MOVUPS 112(AX), X1
  17. AESENC X1, X0
  18. MOVUPS 128(AX), X1
  19. AESENC X1, X0
  20. MOVUPS 144(AX), X1
  22. MOVUPS X0, 0(DX)
  23. RET

Next example: Sqrt.

ft_authoradmin  ft_create_time2018-02-03 13:10
 ft_update_time2018-02-03 13:11