Giter Club home page Giter Club logo

3-way's Introduction

3-Way

3-Way algorithm implemented in Go

3-Way is a block cipher designed by Joan Daemen. It has 96-bit block length and key length. Algorithm works in n rounds. Daemen recommends 11.
So far, there has been no successful cryptoanalysis of 3-Way.
The algorithm is unpatented.

Example: How to test

The test file called 'threeway_test.go' is in repository.
For test run command: go test -v
Test vectors are from implementation of 3-Way in book "Applied Cryptography" - author Bruce Schneier.

// for test run command: go test -v

package threeway

import (
	"fmt"
	"testing"
)

func vectorString(text string, a []uint32) string {
	return fmt.Sprintf("%20s : %08x, %08x, %08x", text, a[2], a[1], a[0])
}

func Test_1(t *testing.T) {	
	tw := New()
	tw.KeyGenerator(0, 0, 0)
	a0, a1, a2 := uint32(1), uint32(1), uint32(1) // plain text
	e0, e1, e2 := uint32(0x4059c76e), uint32(0x83ae9dc4), uint32(0xad21ecf7) // expected cipher text
	c0, c1, c2 := tw.EncryptBlock(a0, a1, a2) // cipher text
	r0, r1, r2 := tw.DecryptBlock(c0, c1, c2) // plain text again

	fmt.Println("**********************************************************************")
	fmt.Println(vectorString("key", tw.k[:]))
	fmt.Println(vectorString("plain", []uint32{a0, a1, a2}))
	fmt.Println(vectorString("cipher", []uint32{c0, c1, c2}))
	fmt.Println(vectorString("result", []uint32{r0, r1, r2}))
	if (c0 != e0) || (c1 != e1) || (c2 != e2) {
		t.Errorf("Invalid encryption.\n%s\n", vectorString("should be", []uint32{e0, e1, e2}))
	}
}

func Test_2(t *testing.T) {	
	tw := New()
	tw.KeyGenerator(6, 5, 4)
	a0, a1, a2 := uint32(3), uint32(2), uint32(1) // plain text
	e0, e1, e2 := uint32(0xd2f05b5e), uint32(0xd6144138), uint32(0xcab920cd) // expected cipher text
	c0, c1, c2 := tw.EncryptBlock(a0, a1, a2) // cipher text
	r0, r1, r2 := tw.DecryptBlock(c0, c1, c2) // plain text again

	fmt.Println("**********************************************************************")
	fmt.Println(vectorString("key", tw.k[:]))
	fmt.Println(vectorString("plain", []uint32{a0, a1, a2}))
	fmt.Println(vectorString("cipher", []uint32{c0, c1, c2}))
	fmt.Println(vectorString("result", []uint32{r0, r1, r2}))
	if (c0 != e0) || (c1 != e1) || (c2 != e2) {
		t.Errorf("Invalid encryption.\n%s\n", vectorString("should be", []uint32{e0, e1, e2}))
	}
}

func Test_3(t *testing.T) {	
	tw := New()
	tw.KeyGenerator(0xdef01234, 0x456789ab, 0xbcdef012)
	a0, a1, a2 := uint32(0x23456789), uint32(0x9abcdef0), uint32(0x01234567) // plain text
	e0, e1, e2 := uint32(0x0aa55dbb), uint32(0x9cdddb6d), uint32(0x7cdb76b2) // expected cipher text
	c0, c1, c2 := tw.EncryptBlock(a0, a1, a2) // cipher text
	r0, r1, r2 := tw.DecryptBlock(c0, c1, c2) // plain text again

	fmt.Println("**********************************************************************")
	fmt.Println(vectorString("key", tw.k[:]))
	fmt.Println(vectorString("plain", []uint32{a0, a1, a2}))
	fmt.Println(vectorString("cipher", []uint32{c0, c1, c2}))
	fmt.Println(vectorString("result", []uint32{r0, r1, r2}))
	if (c0 != e0) || (c1 != e1) || (c2 != e2) {
		t.Errorf("Invalid encryption.\n%s\n", vectorString("should be", []uint32{e0, e1, e2}))
	}
}

func Test_4(t *testing.T) {	
	tw := New()
	tw.KeyGenerator(0xd2f05b5e, 0xd6144138, 0xcab920cd)
	a0, a1, a2 := uint32(0x4059c76e), uint32(0x83ae9dc4), uint32(0xad21ecf7) // plain text
	e0, e1, e2 := uint32(0x478ea871), uint32(0x6b13f17c), uint32(0x15b155ed) // expected cipher text
	c0, c1, c2 := tw.EncryptBlock(a0, a1, a2) // cipher text
	r0, r1, r2 := tw.DecryptBlock(c0, c1, c2) // plain text again

	fmt.Println("**********************************************************************")
	fmt.Println(vectorString("key", tw.k[:]))
	fmt.Println(vectorString("plain", []uint32{a0, a1, a2}))
	fmt.Println(vectorString("cipher", []uint32{c0, c1, c2}))
	fmt.Println(vectorString("result", []uint32{r0, r1, r2}))
	if (c0 != e0) || (c1 != e1) || (c2 != e2) {
		t.Errorf("Invalid encryption.\n%s\n", vectorString("should be", []uint32{e0, e1, e2}))
	}
}

3-way's People

Stargazers

Pedro F. Albanese avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.