Debian Codegraph @

skein/threefish/threefish.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file
3
4 // Package threefish implements the Threefish tweakable block cipher.
5 // Threefish is designed to be the core function of the Skein hash function
6 // family.
7 // There are three versions of Threefish
8 // - Threefish256 processes 256 bit blocks
9 // - Threefish512 processes 512 bit blocks
10 // - Threefish1024 processes 1024 bit blocks
11 package threefish
12
13 import (
14 "crypto/cipher"
15
16 "github.com/enceve/crypto"
17 )
18
19 const (
20 // The size of the tweak in bytes.
21 TweakSize = 16
22 // C240 is the key schedule constant
23 C240 = 0x1bd11bdaa9fc1a22
24 // The block size of Threefish-256 in bytes.
25 BlockSize256 = 32
26 // The block size of Threefish-512 in bytes.
27 BlockSize512 = 64
28 // The block size of Threefish-1024 in bytes.
29 BlockSize1024 = 128
30 )
31
32 // NewCipher returns a cipher.Block implementing the Threefish cipher.
33 // The length of the key must be 32, 64 or 128 byte.
34 // The length of the tweak must be TweakSize.
35 // The returned cipher implements:
36 // - Threefish-256 - if len(key) = 32
37 // - Threefish-512 - if len(key) = 64
38 // - Threefish-1024 - if len(key) = 128
39 func NewCipher(tweak *[TweakSize]byte, key []byte) (cipher.Block, error) {
40 switch k := len(key); k {
41 default:
42 return nil, crypto.KeySizeError(k)
43 case BlockSize256:
44 return newCipher256(tweak, key), nil
45 case BlockSize512:
46 return newCipher512(tweak, key), nil
47 case BlockSize1024:
48 return newCipher1024(tweak, key), nil
49 }
50 }
51
52 // Increment the tweak by the ctr argument.
53 // Skein can consume messages up to 2^96 -1 bytes.
54 func IncrementTweak(tweak *[3]uint64, ctr uint64) {
55 t0 := tweak[0]
56 tweak[0] += ctr
57 if tweak[0] < t0 {
58 t1 := tweak[1]
59 tweak[1] = (t1 + 1) & 0x00000000FFFFFFFF
60 }
61 }
62
63 // The threefish-256 tweakable blockcipher
64 type threefish256 struct {
65 keys [5]uint64
66 tweak [3]uint64
67 }
68
69 func (t *threefish256) BlockSize() int { return BlockSize256 }
70
71 // The threefish-512 tweakable blockcipher
72 type threefish512 struct {
73 keys [9]uint64
74 tweak [3]uint64
75 }
76
77 func (t *threefish512) BlockSize() int { return BlockSize512 }
78
79 // The threefish-1024 tweakable blockcipher
80 type threefish1024 struct {
81 keys [17]uint64
82 tweak [3]uint64
83 }
84
85 func (t *threefish1024) BlockSize() int { return BlockSize1024 }