emmansun / gmsm

ShangMi (SM) cipher suites for golang (Go语言商用密码软件)

Home Page:https://emmansun.github.io/gmsm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ZUC on Windows 10 64-bit

pedroalbanese opened this issue · comments

Greetings!

I'm having problems with the zuc ciphers on Windows 8 and 10. They don't seem to be working when I run my program on Windows 10:

pipe_text_to_edgetk
redirect_piped_text_in_editor

And this also happens in Windows 8:

zuc_windows8

But on Windows 7 it runs perfectly, see:

zuc256

Could you tell me why this could be happening, I mean, it was a user of my EDGETk software who encountered this problem and I don't know what could be causing it, any information is of great value.

Thanks in advance.

I made a small example program, and the problem seems to be in the architecture:

EEA-256

package main
import (
	"crypto/rand"
	"encoding/hex"
	"flag"
	"fmt"
	"golang.org/x/crypto/pbkdf2"
	"io"
	"log"
	"os"

	"github.com/emmansun/gmsm/zuc"
	"github.com/emmansun/gmsm/sm3"
)

var (
	derive = flag.Bool("d", false, "Derive password-based key.")
	iter = flag.Int("i", 1024, "Iterations. (for PBKDF2)")
	key = flag.String("k", "", "256-bit key to Encrypt/Decrypt.")
	vector = flag.String("v", "", "128-bit nonce.")
	pbkdf = flag.String("p", "", "PBKDF2.")
	random = flag.Bool("r", false, "Generate random 128-bit cryptographic key.")
	salt = flag.String("s", "", "Salt. (for PBKDF2)")
)

func main() {
    flag.Parse()

        if (len(os.Args) < 2) {
		fmt.Println("EEA-256 Encryption Tool - ALBANESE Lab (c) 2020-2023\n")
		fmt.Println("Usage of",os.Args[0]+":")
	        flag.PrintDefaults()
	        os.Exit(1)
        }

        if *derive == true {
		prvRaw := pbkdf2.Key([]byte(*pbkdf), []byte(*salt), *iter, 16, sm3.New)
		fmt.Println(hex.EncodeToString(prvRaw))
	        os.Exit(1)
	}
	
	if *random == true {
	var key []byte
	var err error
		key = make([]byte, 32)
		_, err = io.ReadFull(rand.Reader, key)
		if err != nil {
                        log.Fatal(err)
		}
		fmt.Println(hex.EncodeToString(key))
        	os.Exit(0)
	}

	var keyHex string
	var prvRaw []byte
	if *pbkdf != "" {
		prvRaw = pbkdf2.Key([]byte(*pbkdf), []byte(*salt), *iter, 32, sm3.New)
		keyHex = hex.EncodeToString(prvRaw)
	} else {
		keyHex = *key
	}
	var key []byte
	var err error
	if keyHex == "" {
		key = make([]byte, 32)
		_, err = io.ReadFull(rand.Reader, key)
		if err != nil {
                        log.Fatal(err)
		}
		fmt.Fprintln(os.Stderr, "Key=", hex.EncodeToString(key))
	} else {
		key, err = hex.DecodeString(keyHex)
		if err != nil {
                        log.Fatal(err)
		}
		if len(key) != 32 {
                        log.Fatal(err)
		}
	}
	var nonce []byte
	if *vector != "" {
		nonce, _ = hex.DecodeString(*vector)
	} else {
		nonce = make([]byte, 23)
		fmt.Fprintf(os.Stderr, "IV= %x\n", nonce)
	}
	ciph, _ := zuc.NewCipher(key, nonce)
	buf := make([]byte, 64*1<<10)
	var n int
	for {
		n, err = os.Stdin.Read(buf)
		if err != nil && err != io.EOF {
                        log.Fatal(err)
		}
		ciph.XORKeyStream(buf[:n], buf[:n])
		if _, err := os.Stdout.Write(buf[:n]); err != nil {
                        log.Fatal(err)
		}
		if err == io.EOF {
			break
		}
	}
        os.Exit(0)
}

So:

$ echo "Secret Message" | EEA-256_i386 -k ""
$ echo "Secret Message" | EEA-256_amd64 -k ""

eea_screencap

Outputs:

EEA-256_i386

eaa_i386_hex

EEA-256_amd64

eaa_amd64_hex

amd64 implementation , src and dst can't share same buffer now, will fix it soon.

fixed it in v0.21.3, further optimize it later

Thank You very much!