Skip to content

Commit

Permalink
Commands Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroalbanese authored Oct 13, 2021
1 parent 5249df7 commit b57da47
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/cmac/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"encoding/hex"
"flag"
"fmt"
"io"
"os"

"github.com/pedroalbanese/gogost/gost28147"
)

func main() {
keyHex := flag.String("key", "", "Key")
flag.Parse()
if len(*keyHex) != 256/8 {
fmt.Println("Secret key must have 128-bit.")
os.Exit(1)
}
c := gost28147.NewCipher([]byte(*keyHex), &gost28147.SboxIdGostR341194CryptoProParamSet)
var iv [8]byte
h, _ := c.NewMAC(8, iv[:])
io.Copy(h, os.Stdin)
fmt.Println(hex.EncodeToString(h.Sum(nil)))
os.Exit(0)
}
47 changes: 47 additions & 0 deletions cmd/gost94/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// GoGOST -- Pure Go GOST cryptographic functions library
// Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
// Copyright (C) 2020-2021 Pedro Albanese <pedroalbanese@hotmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Command-line 34.11-94 CryptoPro 256-bit hash function.
package main

import (
"encoding/hex"
"flag"
"fmt"
"io"
"os"

"github.com/pedroalbanese/gogost"
"github.com/pedroalbanese/gogost/gost28147"
"github.com/pedroalbanese/gogost/gost341194"
)

var (
version = flag.Bool("version", false, "Print version information")
)

func main() {
flag.Parse()
if *version {
fmt.Println(gogost.Version)
return
}
h := gost341194.New(&gost28147.SboxIdGostR341194CryptoProParamSet)
if _, err := io.Copy(h, os.Stdin); err != nil {
panic(err)
}
fmt.Println(hex.EncodeToString(h.Sum(nil)))
}
43 changes: 43 additions & 0 deletions cmd/hmac/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// GoGOST -- Pure Go GOST cryptographic functions library
// Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
// Copyright (C) 2020-2021 Pedro Albanese <pedroalbanese@hotmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Command-line 34.11-2012 256-bit HMAC function.
package main

import (
"crypto/hmac"
"encoding/hex"
"flag"
"fmt"
"io"
"os"

"github.com/pedroalbanese/gogost/gost34112012256"
)

func main() {
keyHex := flag.String("key", "", "Key")
flag.Parse()
key, err := hex.DecodeString(*keyHex)
if err != nil {
panic(err)
}
h := hmac.New(gost34112012256.New, key)
if _, err = io.Copy(h, os.Stdin); err != nil {
panic(err)
}
fmt.Println("MAC:", hex.EncodeToString(h.Sum(nil)))
}
72 changes: 72 additions & 0 deletions cmd/kuznechik/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// GoGOST -- Pure Go GOST cryptographic functions library
// Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
// Copyright (C) 2020-2021 Pedro Albanese <pedroalbanese@hotmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Command-line 34.12-2015 128-bit Block cipher Kuznyechik crypter.
package main

import (
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"os"

"github.com/pedroalbanese/gogost/gost3412128"
)

func main() {
keyHex := flag.String("key", "", "Key")
flag.Parse()
var key []byte
var err error
if *keyHex == "" {
key = make([]byte, gost3412128.KeySize)
_, err = io.ReadFull(rand.Reader, key)
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, "Key:", hex.EncodeToString(key))
} else {
key, err = hex.DecodeString(*keyHex)
if err != nil {
panic(err)
}
if len(key) != gost3412128.KeySize {
panic(errors.New("provided key has wrong length"))
}
}
ciph := gost3412128.NewCipher(key)
iv := make([]byte, gost3412128.BlockSize)
stream := cipher.NewCTR(ciph, iv)
buf := make([]byte, 128*1<<10)
var n int
for {
n, err = os.Stdin.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
stream.XORKeyStream(buf[:n], buf[:n])
if _, err := os.Stdout.Write(buf[:n]); err != nil {
panic(err)
}
if err == io.EOF {
break
}
}
}
164 changes: 164 additions & 0 deletions cmd/signer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// GoGOST -- Pure Go GOST cryptographic functions library
// Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
// Copyright (C) 2020-2021 Pedro Albanese <pedroalbanese@hotmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Command-line 34.10-2012 Public key algorithm signer.
package main

import (
"bufio"
"crypto/rand"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"log"
"os"

"github.com/pedroalbanese/gogost/gost3410"
"github.com/pedroalbanese/gogost/gost34112012256"
)

func main() {
curveName := flag.String("curve", "id-tc26-gost-3410-2012-256-paramSetA", "Curve name")
keygen := flag.Bool("gen", false, "Generate keypair")
sign := flag.Bool("sign", false, "Sign with private key")
verify := flag.Bool("verify", false, "Verify with public key")
sig := flag.String("sig", "", "Input signature.")
key := flag.String("key", "", "Private/Public key, depending on operation.")
flag.Parse()

var curve *gost3410.Curve
switch *curveName {
case "id-tc26-gost-3410-2012-256-paramSetA":
curve = gost3410.CurveIdtc26gost34102012256paramSetA()
case "id-tc26-gost-3410-2012-256-paramSetB":
curve = gost3410.CurveIdtc26gost34102012256paramSetD()
case "id-tc26-gost-3410-2012-256-paramSetC":
curve = gost3410.CurveIdtc26gost34102012256paramSetC()
case "id-tc26-gost-3410-2012-256-paramSetD":
curve = gost3410.CurveIdtc26gost34102012256paramSetD()
default:
panic(errors.New("unknown curve specified"))
}

if *keygen {
var err error
var prvRaw []byte
var pubRaw []byte
var prv *gost3410.PrivateKey
var pub *gost3410.PublicKey
prvRaw = make([]byte, 256/8)
_, err = io.ReadFull(rand.Reader, prvRaw)
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, "Private:", hex.EncodeToString(prvRaw))
prv, err = gost3410.NewPrivateKey(curve, prvRaw)
if err != nil {
panic(err)
}

pub, err = prv.PublicKey()
if err != nil {
panic(err)
}
pubRaw = pub.Raw()
fmt.Fprintln(os.Stderr, "Public:", hex.EncodeToString(pubRaw))
os.Exit(0)
}

if *sign == true || *verify == true {

scannerWrite := bufio.NewScanner(os.Stdin)
if !scannerWrite.Scan() {
log.Printf("Failed to read: %v", scannerWrite.Err())
return
}

var err error
var prvRaw []byte
var pubRaw []byte
var prv *gost3410.PrivateKey
var pub *gost3410.PublicKey

var inputsig []byte
inputsig, err = hex.DecodeString(*sig)
if err != nil {
panic(err)
}

if *sign == true {
hash := scannerWrite.Bytes()
data := []byte(hash)
hasher := gost34112012256.New()
_, err := hasher.Write(data)
if err != nil {
log.Fatal(err)
}
dgst := hasher.Sum(nil)
prvRaw, err = hex.DecodeString(*key)
if err != nil {
panic(err)
}
if len(prvRaw) != 256/8 {
log.Fatal(err, "private key has wrong length")
}
prv, err = gost3410.NewPrivateKey(curve, prvRaw)
if err != nil {
panic(err)
}

signature, err := prv.Sign(rand.Reader, dgst, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(hex.EncodeToString(signature))
os.Exit(0)
}

if *verify == true {
hash := scannerWrite.Bytes()
data := []byte(hash)
hasher := gost34112012256.New()
_, err := hasher.Write(data)
if err != nil {
panic(err)
}
dgst := hasher.Sum(nil)
pubRaw, err = hex.DecodeString(*key)
if err != nil {
panic(err)
}
if len(pubRaw) != 2*256/8 {
log.Fatal(err, "public key has wrong length")
}
pub, err = gost3410.NewPublicKey(curve, pubRaw)
if err != nil {
panic(err)
}
isValid, err := pub.VerifyDigest(dgst, inputsig)
if err != nil {
panic(err)
}
if !isValid {
panic(errors.New("signature is invalid"))
}
fmt.Println("Verify correct.")
os.Exit(0)
}
}
}
Loading

0 comments on commit b57da47

Please sign in to comment.