Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: src/pkg/crypto/twofish/twofish_test.go

Issue 2687042: code review 2687042: Additional crypto library: Schneier's Twofish
Patch Set: code review 2687042: Additional crypto library: Schneier's Twofish Created 14 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Perform self-test of the Twofish block cipher
6 package twofish
7
8 import (
9 "bytes"
10 "crypto/block"
11 "testing"
12 )
13
14 var qbox = [2][4][16]byte{
15 {
16 {0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE , 0xC, 0xA, 0x4},
17 {0xE, 0xC, 0xB, 0x8, 0x1, 0x2, 0x3, 0x5, 0xF, 0x4, 0xA, 0x6, 0x7 , 0x0, 0x9, 0xD},
18 {0xB, 0xA, 0x5, 0xE, 0x6, 0xD, 0x9, 0x0, 0xC, 0x8, 0xF, 0x3, 0x2 , 0x4, 0x7, 0x1},
19 {0xD, 0x7, 0xF, 0x4, 0x1, 0x2, 0x6, 0xE, 0x9, 0xB, 0x3, 0x0, 0x8 , 0x5, 0xC, 0xA},
20 },
21 {
22 {0x2, 0x8, 0xB, 0xD, 0xF, 0x7, 0x6, 0xE, 0x3, 0x1, 0x9, 0x4, 0x0 , 0xA, 0xC, 0x5},
23 {0x1, 0xE, 0x2, 0xB, 0x4, 0xC, 0x3, 0x7, 0x6, 0xD, 0xA, 0x5, 0xF , 0x9, 0x0, 0x8},
24 {0x4, 0xC, 0x7, 0x5, 0x1, 0x6, 0x9, 0xA, 0x0, 0xE, 0xD, 0x8, 0x2 , 0xB, 0x3, 0xF},
25 {0xB, 0x9, 0x5, 0x1, 0xC, 0x3, 0xD, 0xE, 0x6, 0x4, 0x7, 0xF, 0x2 , 0x0, 0x8, 0xA},
26 },
27 }
28
29 // genSbox generates the variable sbox
30 func genSbox(qi int, x byte) byte {
31 a0, b0 := x/16, x%16
32 for i := 0; i < 2; i++ {
33 a1 := a0 ^ b0
34 b1 := (a0 ^ ((b0 << 3) | (b0 >> 1)) ^ (a0 << 3)) & 15
35 a0 = qbox[qi][2*i][a1]
36 b0 = qbox[qi][2*i+1][b1]
37 }
38 return (b0 << 4) + a0
39 }
40
41 // All test are designed for ECB mode
r 2011/01/09 17:40:00 s/test/tests/
B-Ranger 2011/01/09 19:32:44 Done.
42 var twofishTests = []struct {
43 key []byte
44 dec []byte
45 enc []byte
46 }{
47 // This tests are extracted from LibTom
r 2011/01/09 17:40:00 s/This/These/
B-Ranger 2011/01/09 19:32:44 Done.
48 {
49 []byte{0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32, 0xB6, 0xB F, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A},
50 []byte{0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E, 0x86, 0xC B, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19},
51 []byte{0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85, 0x8F, 0xA A, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3},
52 },
53 {
54 []byte{0x88, 0xB2, 0xB2, 0x70, 0x6B, 0x10, 0x5E, 0x36, 0xB4, 0x4 6, 0xBB, 0x6D, 0x73, 0x1A, 0x1E, 0x88,
55 0xEF, 0xA7, 0x1F, 0x78, 0x89, 0x65, 0xBD, 0x44},
56 []byte{0x39, 0xDA, 0x69, 0xD6, 0xBA, 0x49, 0x97, 0xD5, 0x85, 0xB 6, 0xDC, 0x07, 0x3C, 0xA3, 0x41, 0xB2},
57 []byte{0x18, 0x2B, 0x02, 0xD8, 0x14, 0x97, 0xEA, 0x45, 0xF9, 0xD A, 0xAC, 0xDC, 0x29, 0x19, 0x3A, 0x65},
58 },
59 {
60 []byte{0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46, 0xF2, 0xA 2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
61 0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B, 0xD7, 0x FC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F},
62 []byte{0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F, 0x2C, 0x3 2, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6},
63 []byte{0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97, 0x05, 0x9 3, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA},
64 },
65 // This test are derived from http://www.schneier.com/code/ecb_ival.txt
66 {
67 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
68 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
69 []byte{0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32, 0xB6, 0xB F, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A},
70 },
71 {
72 []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xD C, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
73 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
74 },
75 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
76 []byte{0xCF, 0xD1, 0xD2, 0xE5, 0xA9, 0xBE, 0x9C, 0xDF, 0x50, 0x1 F, 0x13, 0xB8, 0x92, 0xBD, 0x22, 0x48},
77 },
78 {
79 []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xD C, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
80 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x 99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
81 },
82 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
83 []byte{0x37, 0x52, 0x7B, 0xE0, 0x05, 0x23, 0x34, 0xB8, 0x9F, 0x0 C, 0xFC, 0xCA, 0xE8, 0x7C, 0xFA, 0x20},
84 },
85 }
86
87 func TestTwofish(t *testing.T) {
88 // Test if the sbox saved as variable containes the values as defined by it's function
agl1 2011/01/09 15:20:23 comments should be wrapped to 80 chars.
r 2011/01/09 17:40:00 s/containes/contains/ s/it's/its/
r 2011/01/09 17:40:00 i think they're fine. the code is long anyway.
B-Ranger 2011/01/09 19:32:44 Done.
89 for n := 0; n < 2; n++ {
90 for m := 0; m < 256; m++ {
91 if genSbox(n, byte(m)) != sbox[n][m] {
92 t.Errorf("#%d|%d: sbox value = %d want %d", n, m , sbox[n][m], genSbox(n, byte(m)))
93 }
94
95 }
96 }
97 for n, tt := range twofishTests {
98 // Test if the known plaintext (dec) is encrypted into the known crypttext (enc)
agl1 2011/01/09 15:20:23 ditto
99 // using the known key. Test also if enc can be decrypted again into dec.
100 key, err := NewCipher(tt.key)
101 if err != nil {
102 t.Errorf("#%d: NewCipher: %v", n, err)
103 return
104 }
105
106 enc := bytes.NewBuffer(make([]byte, 0))
107 block.NewECBEncrypter(key, enc).Write(tt.dec)
108 if !bytes.Equal(enc.Bytes(), tt.enc) {
109 t.Errorf("#%d: encrypt = %x want %x", n, enc.Bytes(), tt .enc)
110 }
111 dec := make([]byte, 16)
112 block.NewECBDecrypter(key, enc).Read(dec)
113 if !bytes.Equal(dec, tt.dec) {
114 t.Errorf("#%d: decrypt = %x want %x", n, dec, tt.dec)
115 }
116
117 // Test if 16 zero bytes can be encrypt 1000 times, decrypted a nd come
agl1 2011/01/09 15:20:23 ditto
118 // back where we started using all different test keys
119 buf := make([]byte, 16)
120 zero := make([]byte, 16)
121 for i := 0; i < 1000; i++ {
122 key.Encrypt(buf, buf)
123 }
124 for i := 0; i < 1000; i++ {
125 key.Decrypt(buf, buf)
126 }
127 if !bytes.Equal(buf, zero) {
128 t.Errorf("#%d: encrypt/decrypt 1000: have %x want %x", n , buf, zero)
129 }
130 }
131 }
OLDNEW
« src/pkg/crypto/twofish/twofish.go ('K') | « src/pkg/crypto/twofish/twofish.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b