FEATURE_HW_CRYPTO
#const FEATURE_HW_CRYPTO: i32 = 1The bit of Features that says the AES and carry-less multiply instructions are there and the program was built to use them.
import cpu
import mem
func show(label: str, x: u128):
b := mem.AllocateZeroed(16 as u64)
cpu.HwStore(b, x)
p := b as u8*
digits := "0123456789abcdef"
mut s := ""
repeat 16 in i:
v := p[i] as int
s += digits.Substr(v >> 4, 1)
s += digits.Substr(v & 15, 1)
end
println label, s
mem.Free(b)
end
func main:
b := mem.AllocateZeroed(16 as u64)
p := b as u8*
repeat 16 in i:
p[i] = (i * 17) as u8
end
x := cpu.HwLoad(b)
show("load ", x)
show("round", cpu.HwAesEnc(x, x))
show("last ", cpu.HwAesEncLast(x, x))
show("mul ", cpu.HwClmul(x, x, 0))
show("mulhi", cpu.HwClmul(x, x, 17))
show("swap ", cpu.HwSwap64(x))
show("bswap", cpu.HwBswap(x))
cpu.SetHardware(false)
println "off:", cpu.HasHardwareCrypto()
cpu.SetHardware(true)
f := cpu.Features()
ok := (f & cpu.FEATURE_HW_CRYPTO) != 0
println "on matches the processor:", cpu.HasHardwareCrypto() == ok
println "families agree:",
cpu.HasAesNi() == (ok and (f & cpu.FEATURE_X86) != 0),
cpu.HasArmCrypto() == (ok and (f & cpu.FEATURE_ARM64) != 0),
cpu.HasPclmul() == cpu.HasAesNi()
mem.Free(b)
endload 00112233445566778899aabbccddeeff round 6368c4eab0329d01259f964f1e36645c last 63ed8e255fbb4eb44c58394e875fdd15 mul 00000101040405051010111114141515 mulhi 40404141444445455050515154545555 swap 8899aabbccddeeff0011223344556677 bswap ffeeddccbbaa99887766554433221100 off: false on matches the processor: true families agree: true true true