AesPlugin added, work with dropbear encryption, seem ok for decryption (barmetal)
This commit is contained in:
parent
9abe19317d
commit
05e725174c
|
@ -0,0 +1,275 @@
|
||||||
|
package vexriscv.plugin
|
||||||
|
|
||||||
|
import spinal.core._
|
||||||
|
import spinal.lib._
|
||||||
|
import vexriscv.{DecoderService, Stageable, VexRiscv}
|
||||||
|
|
||||||
|
|
||||||
|
case class AesPlugin(encoding : MaskedLiteral = M"-----------------000-----0101011") extends Plugin[VexRiscv]{
|
||||||
|
|
||||||
|
object IS_AES extends Stageable(Bool)
|
||||||
|
object CALC extends Stageable(Bits(32 bits))
|
||||||
|
|
||||||
|
val mapping = new {
|
||||||
|
def DECRYPT = 25 // 0/1 => encrypt/decrypt
|
||||||
|
def LAST_ROUND = 26
|
||||||
|
def ENDIAN = 27 //Not implemented yet
|
||||||
|
def BYTE_SEL = 28 //Which byte should be used in RS2
|
||||||
|
}
|
||||||
|
|
||||||
|
//Callback to setup the plugin and ask for different services
|
||||||
|
override def setup(pipeline: VexRiscv): Unit = {
|
||||||
|
import pipeline.config._
|
||||||
|
|
||||||
|
val decoderService = pipeline.service(classOf[DecoderService])
|
||||||
|
|
||||||
|
decoderService.addDefault(IS_AES, False)
|
||||||
|
decoderService.add(
|
||||||
|
key = encoding,
|
||||||
|
List(
|
||||||
|
IS_AES -> True,
|
||||||
|
REGFILE_WRITE_VALID -> True,
|
||||||
|
BYPASSABLE_EXECUTE_STAGE -> False,
|
||||||
|
BYPASSABLE_MEMORY_STAGE -> False, //Late result
|
||||||
|
RS1_USE -> True,
|
||||||
|
RS2_USE -> True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override def build(pipeline: VexRiscv): Unit = {
|
||||||
|
import pipeline._
|
||||||
|
import pipeline.config._
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def BANK0 = (TE0, SBOX_INV).zipped.map((te0, inv) => (te0.toLong) | (inv.toLong << 24))
|
||||||
|
def BANK1 = TD0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
val onExecute = execute plug new Area{
|
||||||
|
import execute._
|
||||||
|
val byteSel = input(INSTRUCTION)(mapping.BYTE_SEL, 2 bits).asUInt
|
||||||
|
val bankSel = input(INSTRUCTION)(mapping.DECRYPT) && !input(INSTRUCTION)(mapping.LAST_ROUND)
|
||||||
|
val romAddress = U(bankSel ## input(RS2).subdivideIn(8 bits).read(byteSel))
|
||||||
|
}
|
||||||
|
|
||||||
|
memory plug new Area{
|
||||||
|
import memory._
|
||||||
|
|
||||||
|
val rom = new Area {
|
||||||
|
val storage = Mem(Bits(32 bits), 512) initBigInt((BANK0 ++ BANK1).map(BigInt(_)))
|
||||||
|
|
||||||
|
val data = storage.readSync(onExecute.romAddress, !arbitration.isStuck)
|
||||||
|
val bytes = data.subdivideIn(8 bits)
|
||||||
|
|
||||||
|
def VecUInt(l: Int*) = Vec(l.map(U(_, 2 bits)))
|
||||||
|
// remap will be used to decode the rom
|
||||||
|
val remap = Vec(
|
||||||
|
VecUInt(0, 1, 1, 2),
|
||||||
|
VecUInt(1, 1, 1, 1),
|
||||||
|
VecUInt(0, 1, 2, 3),
|
||||||
|
VecUInt(3, 3, 3, 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
val address = U(input(INSTRUCTION)(mapping.DECRYPT) ## input(INSTRUCTION)(mapping.LAST_ROUND))
|
||||||
|
val output = remap(address)
|
||||||
|
}
|
||||||
|
|
||||||
|
val wordDesuffle = new Area{
|
||||||
|
val zero = B"0000"
|
||||||
|
val byteSel = input(INSTRUCTION)(mapping.BYTE_SEL, 2 bits).asUInt
|
||||||
|
val output = Vec(Bits(8 bits), 4)
|
||||||
|
|
||||||
|
def remap(l : Int*) = Vec(l.map(rom.output(_)))
|
||||||
|
val sel = byteSel.mux(
|
||||||
|
3 -> remap(0, 1, 2, 3),
|
||||||
|
2 -> remap(1, 2, 3, 0),
|
||||||
|
1 -> remap(2, 3, 0, 1),
|
||||||
|
0 -> remap(3, 0, 1, 2)
|
||||||
|
)
|
||||||
|
when(input(INSTRUCTION)(mapping.LAST_ROUND)){
|
||||||
|
zero := B"1111"
|
||||||
|
zero(byteSel) := False
|
||||||
|
}
|
||||||
|
|
||||||
|
//Finaly, mux the rom data
|
||||||
|
for(byteId <- 0 to 3){
|
||||||
|
output(byteId) := rom.bytes(sel(byteId))
|
||||||
|
when(zero(byteId)){
|
||||||
|
output(byteId) := 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val xored = wordDesuffle.output.asBits ^ input(RS1)
|
||||||
|
insert(CALC) := xored
|
||||||
|
}
|
||||||
|
|
||||||
|
writeBack plug new Area {
|
||||||
|
import writeBack._
|
||||||
|
|
||||||
|
when(input(IS_AES)) {
|
||||||
|
output(REGFILE_WRITE_DATA) := input(CALC)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encryption table which solve a single byte sbox + column mix. Used for all rounds
|
||||||
|
def TE0 = List(
|
||||||
|
0xc663a5, 0xf87c84, 0xee7799, 0xf67b8d,
|
||||||
|
0xfff20d, 0xd66bbd, 0xde6fb1, 0x91c554,
|
||||||
|
0x603050, 0x020103, 0xce67a9, 0x562b7d,
|
||||||
|
0xe7fe19, 0xb5d762, 0x4dabe6, 0xec769a,
|
||||||
|
0x8fca45, 0x1f829d, 0x89c940, 0xfa7d87,
|
||||||
|
0xeffa15, 0xb259eb, 0x8e47c9, 0xfbf00b,
|
||||||
|
0x41adec, 0xb3d467, 0x5fa2fd, 0x45afea,
|
||||||
|
0x239cbf, 0x53a4f7, 0xe47296, 0x9bc05b,
|
||||||
|
0x75b7c2, 0xe1fd1c, 0x3d93ae, 0x4c266a,
|
||||||
|
0x6c365a, 0x7e3f41, 0xf5f702, 0x83cc4f,
|
||||||
|
0x68345c, 0x51a5f4, 0xd1e534, 0xf9f108,
|
||||||
|
0xe27193, 0xabd873, 0x623153, 0x2a153f,
|
||||||
|
0x08040c, 0x95c752, 0x462365, 0x9dc35e,
|
||||||
|
0x301828, 0x3796a1, 0x0a050f, 0x2f9ab5,
|
||||||
|
0x0e0709, 0x241236, 0x1b809b, 0xdfe23d,
|
||||||
|
0xcdeb26, 0x4e2769, 0x7fb2cd, 0xea759f,
|
||||||
|
0x12091b, 0x1d839e, 0x582c74, 0x341a2e,
|
||||||
|
0x361b2d, 0xdc6eb2, 0xb45aee, 0x5ba0fb,
|
||||||
|
0xa452f6, 0x763b4d, 0xb7d661, 0x7db3ce,
|
||||||
|
0x52297b, 0xdde33e, 0x5e2f71, 0x138497,
|
||||||
|
0xa653f5, 0xb9d168, 0x000000, 0xc1ed2c,
|
||||||
|
0x402060, 0xe3fc1f, 0x79b1c8, 0xb65bed,
|
||||||
|
0xd46abe, 0x8dcb46, 0x67bed9, 0x72394b,
|
||||||
|
0x944ade, 0x984cd4, 0xb058e8, 0x85cf4a,
|
||||||
|
0xbbd06b, 0xc5ef2a, 0x4faae5, 0xedfb16,
|
||||||
|
0x8643c5, 0x9a4dd7, 0x663355, 0x118594,
|
||||||
|
0x8a45cf, 0xe9f910, 0x040206, 0xfe7f81,
|
||||||
|
0xa050f0, 0x783c44, 0x259fba, 0x4ba8e3,
|
||||||
|
0xa251f3, 0x5da3fe, 0x8040c0, 0x058f8a,
|
||||||
|
0x3f92ad, 0x219dbc, 0x703848, 0xf1f504,
|
||||||
|
0x63bcdf, 0x77b6c1, 0xafda75, 0x422163,
|
||||||
|
0x201030, 0xe5ff1a, 0xfdf30e, 0xbfd26d,
|
||||||
|
0x81cd4c, 0x180c14, 0x261335, 0xc3ec2f,
|
||||||
|
0xbe5fe1, 0x3597a2, 0x8844cc, 0x2e1739,
|
||||||
|
0x93c457, 0x55a7f2, 0xfc7e82, 0x7a3d47,
|
||||||
|
0xc864ac, 0xba5de7, 0x32192b, 0xe67395,
|
||||||
|
0xc060a0, 0x198198, 0x9e4fd1, 0xa3dc7f,
|
||||||
|
0x442266, 0x542a7e, 0x3b90ab, 0x0b8883,
|
||||||
|
0x8c46ca, 0xc7ee29, 0x6bb8d3, 0x28143c,
|
||||||
|
0xa7de79, 0xbc5ee2, 0x160b1d, 0xaddb76,
|
||||||
|
0xdbe03b, 0x643256, 0x743a4e, 0x140a1e,
|
||||||
|
0x9249db, 0x0c060a, 0x48246c, 0xb85ce4,
|
||||||
|
0x9fc25d, 0xbdd36e, 0x43acef, 0xc462a6,
|
||||||
|
0x3991a8, 0x3195a4, 0xd3e437, 0xf2798b,
|
||||||
|
0xd5e732, 0x8bc843, 0x6e3759, 0xda6db7,
|
||||||
|
0x018d8c, 0xb1d564, 0x9c4ed2, 0x49a9e0,
|
||||||
|
0xd86cb4, 0xac56fa, 0xf3f407, 0xcfea25,
|
||||||
|
0xca65af, 0xf47a8e, 0x47aee9, 0x100818,
|
||||||
|
0x6fbad5, 0xf07888, 0x4a256f, 0x5c2e72,
|
||||||
|
0x381c24, 0x57a6f1, 0x73b4c7, 0x97c651,
|
||||||
|
0xcbe823, 0xa1dd7c, 0xe8749c, 0x3e1f21,
|
||||||
|
0x964bdd, 0x61bddc, 0x0d8b86, 0x0f8a85,
|
||||||
|
0xe07090, 0x7c3e42, 0x71b5c4, 0xcc66aa,
|
||||||
|
0x9048d8, 0x060305, 0xf7f601, 0x1c0e12,
|
||||||
|
0xc261a3, 0x6a355f, 0xae57f9, 0x69b9d0,
|
||||||
|
0x178691, 0x99c158, 0x3a1d27, 0x279eb9,
|
||||||
|
0xd9e138, 0xebf813, 0x2b98b3, 0x221133,
|
||||||
|
0xd269bb, 0xa9d970, 0x078e89, 0x3394a7,
|
||||||
|
0x2d9bb6, 0x3c1e22, 0x158792, 0xc9e920,
|
||||||
|
0x87ce49, 0xaa55ff, 0x502878, 0xa5df7a,
|
||||||
|
0x038c8f, 0x59a1f8, 0x098980, 0x1a0d17,
|
||||||
|
0x65bfda, 0xd7e631, 0x8442c6, 0xd068b8,
|
||||||
|
0x8241c3, 0x2999b0, 0x5a2d77, 0x1e0f11,
|
||||||
|
0x7bb0cb, 0xa854fc, 0x6dbbd6, 0x2c163a
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// Decryption table which solve a single byte sbox + column mix. Not used in the last round
|
||||||
|
def TD0 = List(
|
||||||
|
0x51f4a750l, 0x7e416553l, 0x1a17a4c3l, 0x3a275e96l,
|
||||||
|
0x3bab6bcbl, 0x1f9d45f1l, 0xacfa58abl, 0x4be30393l,
|
||||||
|
0x2030fa55l, 0xad766df6l, 0x88cc7691l, 0xf5024c25l,
|
||||||
|
0x4fe5d7fcl, 0xc52acbd7l, 0x26354480l, 0xb562a38fl,
|
||||||
|
0xdeb15a49l, 0x25ba1b67l, 0x45ea0e98l, 0x5dfec0e1l,
|
||||||
|
0xc32f7502l, 0x814cf012l, 0x8d4697a3l, 0x6bd3f9c6l,
|
||||||
|
0x038f5fe7l, 0x15929c95l, 0xbf6d7aebl, 0x955259dal,
|
||||||
|
0xd4be832dl, 0x587421d3l, 0x49e06929l, 0x8ec9c844l,
|
||||||
|
0x75c2896al, 0xf48e7978l, 0x99583e6bl, 0x27b971ddl,
|
||||||
|
0xbee14fb6l, 0xf088ad17l, 0xc920ac66l, 0x7dce3ab4l,
|
||||||
|
0x63df4a18l, 0xe51a3182l, 0x97513360l, 0x62537f45l,
|
||||||
|
0xb16477e0l, 0xbb6bae84l, 0xfe81a01cl, 0xf9082b94l,
|
||||||
|
0x70486858l, 0x8f45fd19l, 0x94de6c87l, 0x527bf8b7l,
|
||||||
|
0xab73d323l, 0x724b02e2l, 0xe31f8f57l, 0x6655ab2al,
|
||||||
|
0xb2eb2807l, 0x2fb5c203l, 0x86c57b9al, 0xd33708a5l,
|
||||||
|
0x302887f2l, 0x23bfa5b2l, 0x02036abal, 0xed16825cl,
|
||||||
|
0x8acf1c2bl, 0xa779b492l, 0xf307f2f0l, 0x4e69e2a1l,
|
||||||
|
0x65daf4cdl, 0x0605bed5l, 0xd134621fl, 0xc4a6fe8al,
|
||||||
|
0x342e539dl, 0xa2f355a0l, 0x058ae132l, 0xa4f6eb75l,
|
||||||
|
0x0b83ec39l, 0x4060efaal, 0x5e719f06l, 0xbd6e1051l,
|
||||||
|
0x3e218af9l, 0x96dd063dl, 0xdd3e05ael, 0x4de6bd46l,
|
||||||
|
0x91548db5l, 0x71c45d05l, 0x0406d46fl, 0x605015ffl,
|
||||||
|
0x1998fb24l, 0xd6bde997l, 0x894043ccl, 0x67d99e77l,
|
||||||
|
0xb0e842bdl, 0x07898b88l, 0xe7195b38l, 0x79c8eedbl,
|
||||||
|
0xa17c0a47l, 0x7c420fe9l, 0xf8841ec9l, 0x00000000l,
|
||||||
|
0x09808683l, 0x322bed48l, 0x1e1170acl, 0x6c5a724el,
|
||||||
|
0xfd0efffbl, 0x0f853856l, 0x3daed51el, 0x362d3927l,
|
||||||
|
0x0a0fd964l, 0x685ca621l, 0x9b5b54d1l, 0x24362e3al,
|
||||||
|
0x0c0a67b1l, 0x9357e70fl, 0xb4ee96d2l, 0x1b9b919el,
|
||||||
|
0x80c0c54fl, 0x61dc20a2l, 0x5a774b69l, 0x1c121a16l,
|
||||||
|
0xe293ba0al, 0xc0a02ae5l, 0x3c22e043l, 0x121b171dl,
|
||||||
|
0x0e090d0bl, 0xf28bc7adl, 0x2db6a8b9l, 0x141ea9c8l,
|
||||||
|
0x57f11985l, 0xaf75074cl, 0xee99ddbbl, 0xa37f60fdl,
|
||||||
|
0xf701269fl, 0x5c72f5bcl, 0x44663bc5l, 0x5bfb7e34l,
|
||||||
|
0x8b432976l, 0xcb23c6dcl, 0xb6edfc68l, 0xb8e4f163l,
|
||||||
|
0xd731dccal, 0x42638510l, 0x13972240l, 0x84c61120l,
|
||||||
|
0x854a247dl, 0xd2bb3df8l, 0xaef93211l, 0xc729a16dl,
|
||||||
|
0x1d9e2f4bl, 0xdcb230f3l, 0x0d8652ecl, 0x77c1e3d0l,
|
||||||
|
0x2bb3166cl, 0xa970b999l, 0x119448fal, 0x47e96422l,
|
||||||
|
0xa8fc8cc4l, 0xa0f03f1al, 0x567d2cd8l, 0x223390efl,
|
||||||
|
0x87494ec7l, 0xd938d1c1l, 0x8ccaa2fel, 0x98d40b36l,
|
||||||
|
0xa6f581cfl, 0xa57ade28l, 0xdab78e26l, 0x3fadbfa4l,
|
||||||
|
0x2c3a9de4l, 0x5078920dl, 0x6a5fcc9bl, 0x547e4662l,
|
||||||
|
0xf68d13c2l, 0x90d8b8e8l, 0x2e39f75el, 0x82c3aff5l,
|
||||||
|
0x9f5d80bel, 0x69d0937cl, 0x6fd52da9l, 0xcf2512b3l,
|
||||||
|
0xc8ac993bl, 0x10187da7l, 0xe89c636el, 0xdb3bbb7bl,
|
||||||
|
0xcd267809l, 0x6e5918f4l, 0xec9ab701l, 0x834f9aa8l,
|
||||||
|
0xe6956e65l, 0xaaffe67el, 0x21bccf08l, 0xef15e8e6l,
|
||||||
|
0xbae79bd9l, 0x4a6f36cel, 0xea9f09d4l, 0x29b07cd6l,
|
||||||
|
0x31a4b2afl, 0x2a3f2331l, 0xc6a59430l, 0x35a266c0l,
|
||||||
|
0x744ebc37l, 0xfc82caa6l, 0xe090d0b0l, 0x33a7d815l,
|
||||||
|
0xf104984al, 0x41ecdaf7l, 0x7fcd500el, 0x1791f62fl,
|
||||||
|
0x764dd68dl, 0x43efb04dl, 0xccaa4d54l, 0xe49604dfl,
|
||||||
|
0x9ed1b5e3l, 0x4c6a881bl, 0xc12c1fb8l, 0x4665517fl,
|
||||||
|
0x9d5eea04l, 0x018c355dl, 0xfa877473l, 0xfb0b412el,
|
||||||
|
0xb3671d5al, 0x92dbd252l, 0xe9105633l, 0x6dd64713l,
|
||||||
|
0x9ad7618cl, 0x37a10c7al, 0x59f8148el, 0xeb133c89l,
|
||||||
|
0xcea927eel, 0xb761c935l, 0xe11ce5edl, 0x7a47b13cl,
|
||||||
|
0x9cd2df59l, 0x55f2733fl, 0x1814ce79l, 0x73c737bfl,
|
||||||
|
0x53f7cdeal, 0x5ffdaa5bl, 0xdf3d6f14l, 0x7844db86l,
|
||||||
|
0xcaaff381l, 0xb968c43el, 0x3824342cl, 0xc2a3405fl,
|
||||||
|
0x161dc372l, 0xbce2250cl, 0x283c498bl, 0xff0d9541l,
|
||||||
|
0x39a80171l, 0x080cb3del, 0xd8b4e49cl, 0x6456c190l,
|
||||||
|
0x7bcb8461l, 0xd532b670l, 0x486c5c74l, 0xd0b85742l
|
||||||
|
)
|
||||||
|
|
||||||
|
// Last round decryption sbox
|
||||||
|
def SBOX_INV = List(
|
||||||
|
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
||||||
|
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
|
||||||
|
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||||
|
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
|
||||||
|
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
|
||||||
|
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
||||||
|
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
|
||||||
|
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
|
||||||
|
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
||||||
|
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
|
||||||
|
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
|
||||||
|
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
||||||
|
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
|
||||||
|
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
|
||||||
|
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
||||||
|
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue