Skip to content

Commit

Permalink
lea test, rename avo utils
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuaNerin committed Oct 2, 2023
1 parent 721557a commit 73181d6
Show file tree
Hide file tree
Showing 20 changed files with 6,709 additions and 145 deletions.
105 changes: 105 additions & 0 deletions avoutil/simd/simd_sse2.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,108 @@ const (
C_MM_HINT_T2 = 1
C_MM_HINT_NTA = 0
)

/*
*
Synopsis
__m128i _mm_srli_si128 (__m128i a, int imm8)
#include <emmintrin.h>
Instruction: psrldq xmm, imm8
CPUID Flags: SSE2
Description
Shift a right by imm8 bytes while shifting in zeros, and store the results in dst.
Operation
tmp := imm8[7:0]
IF tmp > 15
tmp := 16
FI
dst[127:0] := a[127:0] >> (tmp*8)
*/
func F_mm_srli_si128(dst VecVirtual, a, imm8 Op) VecVirtual {
if dst != a {
MOVOad(dst, a)
}

CheckType(
`
// PSRLDQ imm8 xmm
`,
imm8, dst,
)

PSRLDQ(imm8, dst)
return dst
}

/*
*
Synopsis
__m128i _mm_slli_si128 (__m128i a, int imm8)
#include <emmintrin.h>
Instruction: pslldq xmm, imm8
CPUID Flags: SSE2
Description
Shift a left by imm8 bytes while shifting in zeros, and store the results in dst.
Operation
tmp := imm8[7:0]
IF tmp > 15
tmp := 16
FI
dst[127:0] := a[127:0] << (tmp*8)
*/
func F_mm_slli_si128(dst VecVirtual, a, imm8 Op) VecVirtual {
if dst != a {
MOVOad(dst, a)
}

CheckType(
`
// PSLLDQ imm8 xmm
`,
imm8, dst,
)

PSLLDQ(imm8, dst)
return dst
}

/*
*
Synopsis
int _mm_extract_epi8 (__m128i a, const int imm8)
#include <smmintrin.h>
Instruction: pextrb r32, xmm, imm8
CPUID Flags: SSE4.1
Description
Extract an 8-bit integer from a, selected with imm8, and store the result in the lower element of dst.
Operation
dst[7:0] := (a[127:0] >> (imm8[3:0] * 8))[7:0]
dst[31:8] := 0
*/
func F_mm_extract_epi8(dst Register, a, imm8 Op) Register {
CheckType(
`
// PEXTRB imm8 xmm m8
// PEXTRB imm8 xmm r32
`,
imm8, a, dst,
)

PEXTRB(imm8, a, dst)
return dst
}
52 changes: 42 additions & 10 deletions avoutil/simd/simd_ssse3.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,51 @@ dst = a
*/

func F_mm_shuffle_epi8(dst VecVirtual, a, b Op) VecVirtual {
if dst != a {
// _mm_shuffle_epi8(a, b);
// -> pshufb a, b
// -> PSHUFB (B, A)

switch {
case dst == a:
CheckType(
`
// PSHUFB m128 xmm
// PSHUFB xmm xmm
`,
b, dst,
)

PSHUFB(b, dst)

case dst == b:
CheckType(
`
// PSHUFB m128 xmm
// PSHUFB xmm xmm
`,
a, dst,
)

tmp := XMM()
MOVOad(tmp, b)
MOVOad(dst, a)
}
PSHUFB(tmp, dst)

default:
if dst != a {
MOVOad(dst, a)
}

CheckType(
`
// PSHUFB m128 xmm
// PSHUFB xmm xmm
`,
b, dst,
)
CheckType(
`
// PSHUFB m128 xmm
// PSHUFB xmm xmm
`,
b, dst,
)

PSHUFB(b, dst)
PSHUFB(b, dst)
}

return dst
}
12 changes: 0 additions & 12 deletions internal/kryptoutil/avo/avo.go

This file was deleted.

41 changes: 41 additions & 0 deletions internal/kryptoutil/memset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kryptoutil

func MemsetByte(a []byte, v byte) {
if len(a) == 0 {
return
}
a[0] = v
for bp := 1; bp < len(a); bp *= 2 {
copy(a[bp:], a[:bp])
}
}

func MemsetUint32(a []uint32, v uint32) {
if len(a) == 0 {
return
}
a[0] = v
for bp := 1; bp < len(a); bp *= 2 {
copy(a[bp:], a[:bp])
}
}

func MemsetUint64(a []uint64, v uint64) {
if len(a) == 0 {
return
}
a[0] = v
for bp := 1; bp < len(a); bp *= 2 {
copy(a[bp:], a[:bp])
}
}

func MemsetInt(a []int, v int) {
if len(a) == 0 {
return
}
a[0] = v
for bp := 1; bp < len(a); bp *= 2 {
copy(a[bp:], a[:bp])
}
}
46 changes: 46 additions & 0 deletions lea/lea.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
// Package lea implements LEA encryption, as defined in TTAK.KO-12.0223
package lea

import (
"crypto/cipher"
"fmt"
)

type funcNew func(key []byte) (cipher.Block, error)
type funcBlock func(ctx *leaContext, dst, src []byte)

type leaContext struct {
round uint8
rk [192]uint32
ecb bool
}

var (
leaEnc1 funcBlock = leaEnc1Go
leaEnc4 funcBlock = leaEnc4Go
leaEnc8 funcBlock = leaEnc8Go

leaDec1 funcBlock = leaDec1Go
leaDec4 funcBlock = leaDec4Go
leaDec8 funcBlock = leaDec8Go

leaNew funcNew = newCipherGo
leaNewECB funcNew = newCipherECBGo
)

const (
// The LEA block size in bytes.
BlockSize = 16
)

type KeySizeError int

func (k KeySizeError) Error() string {
return fmt.Sprintf("krypto/lea: invalid key size %d", int(k))
}

// NewCipher creates and returns a new cipher.Block.
// The key argument should be the LEA key, either 16, 24, or 32 bytes to select LEA-128, LEA-192, or LEA-256.
func NewCipher(key []byte) (cipher.Block, error) {
return leaNew(key)
}

// NewCipherECB creates and returns a new cipher.Block by ECB mode.
// This function can be useful in amd64.
// The key argument should be the LEA key, either 16, 24, or 32 bytes to select LEA-128, LEA-192, or LEA-256.
func NewCipherECB(key []byte) (cipher.Block, error) {
return leaNewECB(key)
}
Loading

0 comments on commit 73181d6

Please sign in to comment.