go threefish
v1.0.2
Threefish 是一種可調整的分組密碼,作為 Skein 哈希函數的一部分而開發,作為 NIST 哈希函數競賽的提交材料。 Threefish 支援 256、512 和 1024 位元的區塊大小。
完整的 Threefish 規格可在註腳1中找到。
測試向量是從最新的參考實作中提取的2 。
加密和解密循環已展開,每次迭代包含八輪。這允許將旋轉常數嵌入到程式碼中而無需重複。論文1詳細描述了這種做法,該論文還提供了詳細的性能資訊。
若要在 go 專案中作為依賴項安裝:
go get -U github.com/schultz-is/go-threefish
此套件中的密碼實作實作了crypto/cipher
cipher.Block
介面。此程式庫傳回的執行個體可與支援 256、512 或 1024 位元區塊大小的任何區塊密碼模式一起使用。
package main
import (
"crypto/cipher"
"crypto/rand"
"fmt"
"github.com/schultz-is/go-threefish"
)
func main () {
message := make ([] byte , 128 )
copy ( message , [] byte ( "secret message" ))
// Assign a key. Generally this is derived from a known secret value. Often
// a passphrase is derived using a key derivation function such as PBKDF2.
key := make ([] byte , 128 )
_ , err := rand . Read ( key )
if err != nil {
panic ( err )
}
// Assign a tweak value. This allows customization of the block cipher as in
// the UBI block chaining mode. Support for the tweak value is not available
// in the block ciphers modes supported by the standard library.
tweak := make ([] byte , 16 )
_ , err = rand . Read ( tweak )
if err != nil {
panic ( err )
}
// Instantiate and initialize a block cipher.
block , err := threefish . New1024 ( key , tweak )
if err != nil {
panic ( err )
}
// When using CBC mode, the IV needs to be unique but does not need to be
// secure. For this reason, it can be prepended to the ciphertext.
ciphertext := make ([] byte , block . BlockSize () + len ( message ))
iv := ciphertext [: block . BlockSize ()]
_ , err = rand . Read ( iv )
if err != nil {
panic ( err )
}
mode := cipher . NewCBCEncrypter ( block , iv )
mode . CryptBlocks ( ciphertext [ block . BlockSize ():], message )
fmt . Printf ( "%x n " , ciphertext )
}
可以執行單元測試,並可以透過提供的 Makefile 查看測試覆蓋率。
make test
make cover
可以運行基準測試,並可以透過提供的 Makefile 產生 CPU 和記憶體設定檔。
make benchmark
go tool pprof cpu.prof
go tool pprof mem.prof
name time/op speed
Threefish256/encrypt-8 85 ns 372 MB/s
Threefish256/decrypt-8 111 ns 287 MB/s
Threefish512/encrypt-8 234 ns 272 MB/s
Threefish512/decrypt-8 363 ns 175 MB/s
Threefish1024/encrypt-8 581 ns 220 MB/s
Threefish1024/decrypt-8 685 ns 186 MB/s
name time/op speed
Threefish256/encrypt-16 124 ns 259 MB/s
Threefish256/decrypt-16 156 ns 206 MB/s
Threefish512/encrypt-16 338 ns 189 MB/s
Threefish512/decrypt-16 310 ns 206 MB/s
Threefish1024/encrypt-16 804 ns 159 MB/s
Threefish1024/decrypt-16 778 ns 165 MB/s
http://www.skein-hash.info/sites/default/files/skein1.3.pdf ↩ ↩ 2
http://www.skein-hash.info/sites/default/files/NIST_CD_102610.zip ↩