| 1 | // Use of this source code is governed by a license |
| 2 | // that can be found in the LICENSE file |
| 3 | |
| 4 | // Package skein implements the Skein512 hash function |
| 5 | // based on the Threefish tweakable block cipher. |
| 6 | // |
| 7 | // |
| 8 | // Overview |
| 9 | // |
| 10 | // Skein is a hash function family using the tweakable block cipher |
| 11 | // Threefish in Unique Block Iteration (UBI) chaining mode while |
| 12 | // leveraging an optional low-overhead argument-system |
| 13 | // for flexibility. There are three versions of Skein, each of them |
| 14 | // using the corresponding Threefish version: |
| 15 | // - Skein256 using Threefish256 and processes 256 bit blocks |
| 16 | // - Skein512 using Threefish512 and processes 512 bit blocks |
| 17 | // - Skein1024 using Threefish1024 and processes 1024 bit blocks |
| 18 | // |
| 19 | // Skein can be used as hash function, MAC or KDF and supports personalized, |
| 20 | // randomized (salted) and public-key-bound hashing. Furthermore Skein |
| 21 | // has some additional features (currently) not implemented here. For |
| 22 | // details see http://www.skein-hash.info/ |
| 23 | // Skein was submitted to the SHA-3 challenge. |
| 24 | // |
| 25 | // Skein can produce hash values of any size (up to (2^64 -1) x BlockSize bytes) |
| 26 | // not only the common sizes 160, 224, 256, 384 and 512 bit. |
| 27 | // |
| 28 | // |
| 29 | // Security and Recommendations |
| 30 | // |
| 31 | // All Skein varaiants (as far as known) secure. The Skein authors recommend to use |
| 32 | // Skein512 for most applications. Skein256 should be used for small devices |
| 33 | // like smartcards. Skein1024 is the ultra-conservative variant providing a level of |
| 34 | // security (mostly) not needed. |
| 35 | package skein |
| 36 | |
| 37 | import ( |
| 38 | "hash" |
| 39 | ) |
| 40 | |
| 41 | // Config contains the Skein configuration: |
| 42 | // - Key for computing MACs |
| 43 | // - Personal for personalized hashing |
| 44 | // - PublicKey for public-key-bound hashing |
| 45 | // - KeyID for key derivation |
| 46 | // - Nonce for randomized hashing |
| 47 | // All fields are optional and can be nil. |
| 48 | type Config struct { |
| 49 | Key []byte // Optional: The secret key for MAC |
| 50 | Personal []byte // Optional: The personalization for unique hashing |
| 51 | PublicKey []byte // Optional: The public key for public-key bound hashing |
| 52 | KeyID []byte // Optional: The key id for key derivation |
| 53 | Nonce []byte // Optional: The nonce for randomized hashing |
| 54 | } |
| 55 | |
| 56 | // Sum512 computes the 512 bit Skein512 checksum (or MAC if key is set) of msg |
| 57 | // and writes it to out. The key is optional and can be nil. |
| 58 | func Sum512(out *[64]byte, msg, key []byte) { |
| 59 | s := new(hashFunc) |
| 60 | |
| 61 | if len(key) > 0 { |
| 62 | s.initialize(64, &Config{Key: key}) |
| 63 | } else { |
| 64 | s.hVal = iv512 |
| 65 | s.hValCpy = iv512 |
| 66 | s.hashsize = BlockSize |
| 67 | s.tweak[0] = 0 |
| 68 | s.tweak[1] = CfgMessage<<56 | FirstBlock |
| 69 | } |
| 70 | |
| 71 | s.Write(msg) |
| 72 | |
| 73 | s.finalizeHash() |
| 74 | s.output(out, 0) |
| 75 | } |
| 76 | |
| 77 | // Sum384 computes the 384 bit Skein512 checksum (or MAC if key is set) of msg |
| 78 | // and writes it to out. The key is optional and can be nil. |
| 79 | func Sum384(out *[48]byte, msg, key []byte) { |
| 80 | var out512 [64]byte |
| 81 | s := new(hashFunc) |
| 82 | |
| 83 | if len(key) > 0 { |
| 84 | s.initialize(48, &Config{Key: key}) |
| 85 | } else { |
| 86 | s.hVal = iv384 |
| 87 | s.hValCpy = iv384 |
| 88 | s.hashsize = 48 |
| 89 | s.tweak[0] = 0 |
| 90 | s.tweak[1] = CfgMessage<<56 | FirstBlock |
| 91 | } |
| 92 | |
| 93 | s.Write(msg) |
| 94 | |
| 95 | s.finalizeHash() |
| 96 | s.output(&out512, 0) |
| 97 | |
| 98 | copy(out[:], out512[:48]) |
| 99 | } |
| 100 | |
| 101 | // Sum256 computes the 256 bit Skein512 checksum (or MAC if key is set) of msg |
| 102 | // and writes it to out. The key is optional and can be nil. |
| 103 | func Sum256(out *[32]byte, msg, key []byte) { |
| 104 | var out512 [64]byte |
| 105 | s := new(hashFunc) |
| 106 | |
| 107 | if len(key) > 0 { |
| 108 | s.initialize(32, &Config{Key: key}) |
| 109 | } else { |
| 110 | s.hVal = iv256 |
| 111 | s.hValCpy = iv256 |
| 112 | s.hashsize = 32 |
| 113 | s.tweak[0] = 0 |
| 114 | s.tweak[1] = CfgMessage<<56 | FirstBlock |
| 115 | } |
| 116 | |
| 117 | s.Write(msg) |
| 118 | |
| 119 | s.finalizeHash() |
| 120 | s.output(&out512, 0) |
| 121 | |
| 122 | copy(out[:], out512[:32]) |
| 123 | } |
| 124 | |
| 125 | // Sum160 computes the 160 bit Skein512 checksum (or MAC if key is set) of msg |
| 126 | // and writes it to out. The key is optional and can be nil. |
| 127 | func Sum160(out *[20]byte, msg, key []byte) { |
| 128 | var out512 [64]byte |
| 129 | s := new(hashFunc) |
| 130 | |
| 131 | if len(key) > 0 { |
| 132 | s.initialize(20, &Config{Key: key}) |
| 133 | } else { |
| 134 | s.hVal = iv160 |
| 135 | s.hValCpy = iv160 |
| 136 | s.hashsize = 20 |
| 137 | s.tweak[0] = 0 |
| 138 | s.tweak[1] = CfgMessage<<56 | FirstBlock |
| 139 | } |
| 140 | |
| 141 | s.Write(msg) |
| 142 | |
| 143 | s.finalizeHash() |
| 144 | s.output(&out512, 0) |
| 145 | |
| 146 | copy(out[:], out512[:20]) |
| 147 | } |
| 148 | |
| 149 | // Sum returns the Skein512 checksum with the given hash size of msg using the (optional) |
| 150 | // conf for configuration. The hashsize must be > 0. |
| 151 | func Sum(msg []byte, hashsize int, conf *Config) []byte { |
| 152 | s := New(hashsize, conf) |
| 153 | s.Write(msg) |
| 154 | return s.Sum(nil) |
| 155 | } |
| 156 | |
| 157 | // New512 returns a hash.Hash computing the Skein512 512 bit checksum. |
| 158 | // The key is optional and turns the hash into a MAC. |
| 159 | func New512(key []byte) hash.Hash { |
| 160 | s := new(hashFunc) |
| 161 | |
| 162 | if len(key) > 0 { |
| 163 | s.initialize(BlockSize, &Config{Key: key}) |
| 164 | } else { |
| 165 | copy(s.hVal[:8], iv512[:]) |
| 166 | s.hValCpy = s.hVal |
| 167 | s.hashsize = BlockSize |
| 168 | s.tweak[0] = 0 |
| 169 | s.tweak[1] = CfgMessage<<56 | FirstBlock |
| 170 | } |
| 171 | |
| 172 | return s |
| 173 | } |
| 174 | |
| 175 | // New256 returns a hash.Hash computing the Skein512 256 bit checksum. |
| 176 | // The key is optional and turns the hash into a MAC. |
| 177 | func New256(key []byte) hash.Hash { |
| 178 | s := new(hashFunc) |
| 179 | |
| 180 | if len(key) > 0 { |
| 181 | s.initialize(32, &Config{Key: key}) |
| 182 | } else { |
| 183 | copy(s.hVal[:8], iv256[:]) |
| 184 | s.hValCpy = s.hVal |
| 185 | s.hashsize = 32 |
| 186 | s.tweak[0] = 0 |
| 187 | s.tweak[1] = CfgMessage<<56 | FirstBlock |
| 188 | } |
| 189 | |
| 190 | return s |
| 191 | } |
| 192 | |
| 193 | // New returns a hash.Hash computing the Skein512 checksum with the given hash size. |
| 194 | // The conf is optional and configurates the hash.Hash |
| 195 | func New(hashsize int, conf *Config) hash.Hash { |
| 196 | s := new(hashFunc) |
| 197 | s.initialize(hashsize, conf) |
| 198 | return s |
| 199 | } |