├── .gitignore ├── README.md ├── build └── build.go ├── config.go ├── config.json ├── database.go ├── difficulty.go ├── go.mod ├── go.sum ├── httpServer.go ├── jobMake.go ├── log.go ├── main.go ├── miner.go ├── pool.go ├── proto.go ├── rpcClient.go ├── util.go ├── x17r.go └── x17r ├── CMakeLists.txt ├── aes_helper.c ├── compat.h ├── gost_streebog.c ├── gost_streebog.h ├── haval_helper.c ├── md_helper.c ├── mod_blakecoin.c ├── sph_blake.c ├── sph_blake.h ├── sph_bmw.c ├── sph_bmw.h ├── sph_cubehash.c ├── sph_cubehash.h ├── sph_echo.c ├── sph_echo.h ├── sph_fugue.c ├── sph_fugue.h ├── sph_groestl.c ├── sph_groestl.h ├── sph_hamsi.c ├── sph_hamsi.h ├── sph_hamsi_helper.c ├── sph_haval.c ├── sph_haval.h ├── sph_hefty1.c ├── sph_hefty1.h ├── sph_jh.c ├── sph_jh.h ├── sph_keccak.c ├── sph_keccak.h ├── sph_luffa.c ├── sph_luffa.h ├── sph_ripemd.c ├── sph_ripemd.h ├── sph_sha2.c ├── sph_sha2.h ├── sph_sha2big.c ├── sph_shabal.c ├── sph_shabal.h ├── sph_shavite.c ├── sph_shavite.h ├── sph_simd.c ├── sph_simd.h ├── sph_skein.c ├── sph_skein.h ├── sph_types.h ├── sph_whirlpool.c ├── sph_whirlpool.h ├── x17r.c └── x17r.h /.gitignore: -------------------------------------------------------------------------------- 1 | # golang deps 2 | vendor -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minepool-backend 2 | 3 | How to deploy a private minerpool. 4 | 5 | ## Download files 6 | 7 | Click on the link: 8 | Download the corresponding file according to the system and extract it. 9 | 10 | ## Create a wallet(If you hava keywords of wallet,skip this step) 11 | 12 | Enter the unzipped folder,exec command:`ufo-wallet init`; 13 | 14 | After entering and confirming the password, you will get a series of 12 English words separated by semicolons 15 | 16 | Please make a good backup and keep it carefully to avoid being acquired by others. 17 | 18 | chalk;husband;exit;another;vessel;slam;federal;idle;horror;traffic;lobster;random; 19 | 20 | After this step, a file called `wallet.db` is generated under the current path. 21 | 22 | ## Restore wallet database files using keywords(if already has wallet.db,skip this stop) 23 | 24 | Execute `ufo-wallet restore --seed_phrase=keywords` (keywords separated by semicolon) 25 | 26 | After this step, a file called `wallet.db` is generated under the current path. 27 | 28 | ## Start UFO node 29 | 30 | (1) Need to export `secret subkey1` as miner key: 31 | 32 | Execute `ufo-wallet export_miner_key --subkey=1`,get `Secret Subkey 1` in result. 33 | 34 | (2) Need to export `owner key`: 35 | 36 | Execute `ufo-wallet export_owner_key`,get `Owner Viewer key` in result. 37 | 38 | (3) Start ufo node: 39 | 40 | Execute ufo-node --port=20001 --treasury_path=treasury.bin --stratum_port=20002 --stratum_use_tls=0 --mining_threads=0 --miner_key=MINERKEY --owner_key=OWNERKEY --pass=PASSWORD 41 | 42 | Parameter Specification: 43 | | Parameter | Implication | 44 | | ---------:|:----:| 45 | | port |P2P listening port| 46 | |stratum_port|stratum listening port,provides mining function| 47 | |stratum_use_tls|Whether to use TLS to encrypt the transport channel, the stratum_secrets_path option also needs to be configured if enabled, and not if not| 48 | |stratum_secrets_path|The corresponding catalog contains the TLS certificate file (stratum.crt), the key file corresponding to the TLS certificate (stratum.key), and the TLS API's key file (stratum.api.keys).This file is a text file with more than 8 bytes of characters, such as abcd1234| 49 | |mining_threads|The number of mining threads is recommended to be set to the number of CPU cores| 50 | |miner_key|Miner key exported from previous steps| 51 | |owner_key|Owner key exported from previous steps| 52 | |pass|Wallet password| 53 | 54 | ## Start Wallet API 55 | 56 | (1) Enter the unzipped folder,exec command:`wallet-api.exe --node_addr=127.0.0.1:20001 --pass=12345678 --use_http=1`; 57 | 58 | | parameter | implication | 59 | | ---------:|:----:| 60 | | node_addr|ufo node ip address and p2p port| 61 | |pass|ufo node set password| 62 | 63 | ## Config And Start UFOPool 64 | 65 | (1) Downloadd minerpool source code from: 66 | 67 | (2) Enter x17r path,execute: 68 | 69 | cmake . 70 | make 71 | (3) Back to ufo-pool path,execute: 72 | 73 | go build 74 | 75 | After this stop,will generated a file named ufo-pool under current path. 76 | 77 | 78 | (4)Edit `config.json` file 79 | 80 | `"WalletUrl": "127.0.0.1:20002"` (ufo node ip address:node stratum port) 81 | 82 | `"WalletApiUrl": "http://127.0.0.1:10000/api/wallet"` (walet-api ip address:port) 83 | 84 | (5)Start ufo pool 85 | 86 | Execute:`./ufo-ppol` 87 | -------------------------------------------------------------------------------- /build/build.go: -------------------------------------------------------------------------------- 1 | package build 2 | 3 | var DEBUG bool = true 4 | var BuildName string 5 | var BuildVersion string 6 | var BuildTime string 7 | var GoVersion string 8 | var CommitID string -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "log" 6 | "os" 7 | "path/filepath" 8 | ) 9 | 10 | type Config struct { 11 | Coin string `json:"coin"` 12 | DebugLevel int `json:"debugLevel"` 13 | 14 | WalletUrl string `json:"WalletUrl"` 15 | WalletApiUrl string `json:"WalletApiUrl"` 16 | ApiKey string `json:"apikey"` 17 | 18 | MongoDB MongoDb `json:"mongoDB"` 19 | LevelDB string `json:"leveldb"` 20 | 21 | PoolIPPort string `json:"PoolIPPort"` 22 | 23 | PoolId int `json:"PoolId"` 24 | ENonceLen uint `json:"ENonceLen"` 25 | 26 | HttpIPPort string `json:"httpIPPort"` 27 | LimitHeadersSize int `json:"limitHeadersSize"` 28 | LimitBodySize int64 `json:"limitBodySize"` 29 | 30 | Timeout string `json:"timeout"` 31 | 32 | SecondsPerShare uint `json:"SecondsPerShare"` 33 | WindowSize uint `json:"WindowSize"` 34 | TotalSeconds uint `json:"TotalSeconds,omitempty"` 35 | 36 | StartDiff float64 `json:"StartDiff"` 37 | MinDiff float64 `json:"MinDiff"` 38 | MaxDiff float64 `json:"MaxDiff"` 39 | 40 | RewardPeriod int64 `json:"RewardPeriod"` 41 | OneBlockReward int64 `json:"OneBlockReward"` 42 | SendMinUfo int64 `json:"SendMinUfo"` 43 | HalveHeight int64 `json:"HalveHeight"` 44 | SendRewardsTime string `json:"SendRewardsTime"` 45 | ReSendTime string `json:"ReSendTime"` 46 | PoolFeeRate int64 `json:"PoolFeeRate"` 47 | TxFee int64 `json:"TxFee"` 48 | } 49 | 50 | type MongoDb struct { 51 | Url string `json:"url"` 52 | DBname string `json:"dbname"` 53 | ShareCol string `json:"share"` 54 | BlockCol string `json:"block"` 55 | MinerInfo string `json:"miner"` 56 | SendTx string `json:"sendtx"` 57 | AddrBalance string `json:"balance"` 58 | User string `json:"user"` 59 | Password string `json:"password"` 60 | } 61 | 62 | func LoadConfig(cfgFileName string, cfg *Config) { 63 | if cfgFileName == "" { 64 | cfgFileName = "config.json" 65 | } 66 | cfgPath, err := filepath.Abs(cfgFileName) 67 | if err != nil { 68 | log.Panicln("filepath.Abs ", cfgFileName, " error:", err) 69 | } 70 | log.Println("cfgPath:", cfgPath) 71 | 72 | fd, err := os.Open(cfgPath) 73 | if err != nil { 74 | log.Panicln("Open ", cfgPath, " error:", err) 75 | } 76 | defer fd.Close() 77 | 78 | decoder := json.NewDecoder(fd) 79 | 80 | if err = decoder.Decode(cfg); err != nil { 81 | log.Panicln("Open ", cfgPath, " error:", err) 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "coin": "ufo", 3 | "debugLevel": 7, 4 | 5 | "WalletUrl": "127.0.0.1:20002", 6 | "WalletApiUrl": "http://127.0.0.1:10000/api/wallet", 7 | "apikey":"aaaa1234", 8 | 9 | "mongoDB": { 10 | "url": "127.0.0.1:27017/ufodb", 11 | "dbname":"ufodb", 12 | "share":"share_t", 13 | "block":"foundblock_t", 14 | "miner":"minerinfo_t", 15 | "sendtx":"sendtx_t", 16 | "balance":"balance_t", 17 | "user":"test", 18 | "password":"test" 19 | }, 20 | 21 | "levelDB": "./.ufo.lvdb", 22 | 23 | "PoolIPPort": "0.0.0.0:8080", 24 | 25 | "PoolId":0, 26 | "ENonceLen":3, 27 | 28 | "httpIPPort": "0.0.0.0:8081", 29 | "limitHeadersSize": 1024, 30 | "limitBodySize": 256, 31 | 32 | "timeout": "12000s", 33 | 34 | "SecondsPerShare":1, 35 | "WindowSize":10, 36 | 37 | "StartDiff":50, 38 | "MinDiff":256, 39 | "MaxDiff":268435456, 40 | 41 | "RewardPeriod":900, 42 | "OneBlockReward":500000000, 43 | "SendMinUfo":100000000, 44 | "SendRewardsTime":"10:00", 45 | "ReSendTime":"16:00", 46 | "HalveHeight":1050000, 47 | "TxFee":100, 48 | "PoolFeeRate":3 49 | } -------------------------------------------------------------------------------- /difficulty.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | const ( 8 | rateLow = 0.4 9 | rateHigh = 1.4 10 | ) 11 | 12 | type diffController struct { 13 | startTime time.Time 14 | curDiff float64 15 | 16 | lastRingIdx uint 17 | elements []uint 18 | } 19 | 20 | func newDiffController(diff float64) *diffController { 21 | return &diffController{ 22 | startTime: time.Now(), 23 | curDiff: diff, 24 | 25 | lastRingIdx: 0, 26 | elements: make([]uint, cfg.WindowSize), 27 | } 28 | } 29 | 30 | func (dc *diffController) addShare() { 31 | curRingIdx := uint(time.Now().Unix()) / cfg.SecondsPerShare 32 | 33 | if dc.lastRingIdx == 0 || curRingIdx-dc.lastRingIdx > cfg.WindowSize { 34 | dc.Clear() 35 | dc.lastRingIdx = curRingIdx 36 | } 37 | 38 | for dc.lastRingIdx < curRingIdx { 39 | dc.lastRingIdx++ 40 | dc.elements[dc.lastRingIdx%cfg.WindowSize] = 0 41 | } 42 | 43 | dc.elements[curRingIdx%cfg.WindowSize] += 1 44 | } 45 | 46 | func (dc *diffController) calcCurDiff() float64 { 47 | timestamp := uint(time.Now().Unix()) 48 | index := timestamp / cfg.SecondsPerShare 49 | 50 | sharesNum := dc.Sum(index) 51 | expectedNum := cfg.WindowSize 52 | 53 | if float64(sharesNum) > float64(expectedNum)*rateHigh { 54 | for { 55 | diff := dc.curDiff * 2 56 | if sharesNum > expectedNum && diff < cfg.MaxDiff { 57 | dc.Divide(index, 2) 58 | dc.curDiff = diff 59 | 60 | sharesNum = dc.Sum(index) 61 | } 62 | return dc.curDiff 63 | } 64 | } 65 | 66 | if float64(sharesNum) < float64(expectedNum)*rateLow && dc.isFullWindow(timestamp) { 67 | for { 68 | diff := dc.curDiff / 2 69 | if sharesNum < expectedNum && diff > cfg.MinDiff { 70 | dc.Multiply(index, 2) 71 | dc.curDiff = diff 72 | sharesNum = dc.Sum(index) 73 | } 74 | return dc.curDiff 75 | } 76 | } 77 | 78 | return dc.curDiff 79 | } 80 | 81 | func (dc *diffController) isFullWindow(timestamp uint) bool { 82 | return timestamp >= uint(dc.startTime.Unix())+cfg.TotalSeconds 83 | } 84 | 85 | func (dc *diffController) Clear() { 86 | for i := range dc.elements { 87 | dc.elements[i] = 0 88 | } 89 | } 90 | 91 | func (dc *diffController) Sum(curRingIdx uint) uint { 92 | var sum uint 93 | 94 | if curRingIdx-cfg.WindowSize >= dc.lastRingIdx { 95 | return 0 96 | } 97 | 98 | startRingIdx := curRingIdx - cfg.WindowSize 99 | endRingIdx := dc.lastRingIdx 100 | 101 | for endRingIdx > startRingIdx { 102 | i := endRingIdx % cfg.WindowSize 103 | sum += dc.elements[i] 104 | endRingIdx-- 105 | } 106 | return sum 107 | } 108 | 109 | func (dc *diffController) Divide(curRingIdx, divisor uint) { 110 | 111 | if curRingIdx-cfg.WindowSize >= dc.lastRingIdx { 112 | return 113 | } 114 | 115 | startRingIdx := curRingIdx - cfg.WindowSize 116 | endRingIdx := dc.lastRingIdx 117 | 118 | for endRingIdx > startRingIdx { 119 | i := endRingIdx % cfg.WindowSize 120 | dc.elements[i] /= divisor 121 | endRingIdx-- 122 | } 123 | } 124 | 125 | func (dc *diffController) Multiply(curRingIdx, multiplier uint) { 126 | 127 | if curRingIdx-cfg.WindowSize >= dc.lastRingIdx { 128 | return 129 | } 130 | 131 | startRingIdx := curRingIdx - cfg.WindowSize 132 | endRingIdx := dc.lastRingIdx 133 | 134 | for endRingIdx > startRingIdx { 135 | i := endRingIdx % cfg.WindowSize 136 | dc.elements[i] *= multiplier 137 | endRingIdx-- 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module minepool-backend 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/gorilla/mux v1.7.4 7 | github.com/jasonlvhit/gocron v0.0.0-20191228163020-98b59b546dee 8 | github.com/jrick/bitset v1.0.0 9 | github.com/pkg/errors v0.9.1 10 | github.com/syndtr/goleveldb v1.0.0 11 | gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 12 | ) 13 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 3 | github.com/go-redis/redis v6.15.5+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= 4 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 5 | github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= 6 | github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 7 | github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= 8 | github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 9 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 10 | github.com/jasonlvhit/gocron v0.0.0-20191228163020-98b59b546dee h1:vIYpscVc1753m7svyoDldyaEi2MDa7808yh3hMXVZjg= 11 | github.com/jasonlvhit/gocron v0.0.0-20191228163020-98b59b546dee/go.mod h1:1nXLkt6gXojCECs34KL3+LlZ3gTpZlkPUA8ejW3WeP0= 12 | github.com/jrick/bitset v1.0.0 h1:Ws0PXV3PwXqWK2n7Vz6idCdrV/9OrBXgHEJi27ZB9Dw= 13 | github.com/jrick/bitset v1.0.0/go.mod h1:ZOYB5Uvkla7wIEY4FEssPVi3IQXa02arznRaYaAEPe4= 14 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 15 | github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 16 | github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 17 | github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= 18 | github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= 19 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 20 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 21 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 22 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 23 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 24 | github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= 25 | github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= 26 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 27 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 28 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 29 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 30 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 31 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 32 | gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= 33 | gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= 34 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 35 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 36 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 37 | -------------------------------------------------------------------------------- /jobMake.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "encoding/json" 6 | "fmt" 7 | "math/big" 8 | "net" 9 | "time" 10 | ) 11 | 12 | func (pool *Pool) updateWork() { 13 | pool.isLogin = false 14 | pool.ConnectWallet() 15 | pool.Login() 16 | 17 | for { 18 | line, isPrefix, err := pool.connbuff.ReadLine() 19 | if isPrefix { 20 | Warning.Printf("Socket flood detected from %s", pool.WalletUrl) 21 | 22 | } else if err != nil { 23 | Warning.Println("pool", pool.IP, "error:", err) 24 | pool.conn.Close() 25 | pool.conn = nil 26 | pool.ConnectWallet() 27 | pool.Login() 28 | continue 29 | } 30 | 31 | if len(line) > 1 { 32 | Info.Println(string(line)) 33 | 34 | var resp Response 35 | err := json.Unmarshal(line, &resp) 36 | if err != nil { 37 | Warning.Println("Unmarshal Response error:", err) 38 | continue 39 | } 40 | if resp.Method == "job" { 41 | pool.handleGetWork(resp.Id, resp.PrevHash, resp.Input, resp.Height, resp.NBits) 42 | 43 | } else if resp.Method == "result" { 44 | if resp.Id == "login" { 45 | Info.Println("login") 46 | gPool.ForkHeight = resp.ForkHeight 47 | 48 | } else { 49 | if resp.Description == "accepted" || resp.Description == "expired" || resp.Description == "rejected" { 50 | pool.SubmitSol <- resp 51 | if resp.Description == "expired" { 52 | pool.conn.Close() 53 | pool.conn = nil 54 | pool.ConnectWallet() 55 | pool.Login() 56 | continue 57 | } 58 | } 59 | Info.Println("result") 60 | } 61 | } 62 | } 63 | } 64 | } 65 | 66 | var ( 67 | big100 = new(big.Int).SetInt64(100) 68 | big99 = new(big.Int).SetInt64(99) 69 | ) 70 | 71 | func (pool *Pool) handleGetWork(id, prevHash, input string, height int64, nbits uint32) { 72 | if height < pool.height { 73 | Warning.Printf("Slow block at height %d, current height:%d, input:%s", height, pool.height, input) 74 | return 75 | } 76 | 77 | if height > pool.height { 78 | if height > cfg.HalveHeight { 79 | cfg.OneBlockReward /= 2 80 | } 81 | go pool.broadcastNewJobs(id, prevHash, input) 82 | 83 | pool.shareSet.shareLock.Lock() 84 | pool.shareSet.shares = make(map[string]struct{}) 85 | pool.shareSet.shareLock.Unlock() 86 | 87 | target := CompactToBig(nbits) 88 | targetReal := new(big.Int).Div(new(big.Int).Mul(target, big100), big99) 89 | netDiff := new(big.Int).Div(pow256, target).Int64() 90 | diffstr := ToFloat(nbits) 91 | currDiffStr := fmt.Sprintf("%.6f", float64(diffstr)) 92 | netDiffStr := fmt.Sprintf("%f", float64(netDiff)/4/1024/1024/1024) 93 | 94 | pool.JobsLock.Lock() 95 | defer pool.JobsLock.Unlock() 96 | 97 | pool.Jobs = make(map[string]Job) 98 | pool.LastJob = Job{ 99 | Input: input, 100 | Height: height, 101 | Id: id, 102 | PrevHash: prevHash, 103 | Target: targetReal, 104 | netDiffStr: netDiffStr, 105 | currDiffStr: currDiffStr, 106 | } 107 | pool.Jobs[id] = pool.LastJob 108 | pool.height = height 109 | pool.isFirstWork = true 110 | 111 | Info.Printf("New block %d :%s", height, input) 112 | Info.Printf("nbits:0x%x, target:%d, targetReal:%d, netDiff:%d, %s", nbits, target, targetReal, netDiff, netDiffStr) 113 | 114 | } 115 | 116 | if !pool.ready { 117 | pool.ready = true 118 | pool.walletReady <- true 119 | } 120 | } 121 | 122 | func (pool *Pool) ConnectWallet() { 123 | var retry int 124 | 125 | for { 126 | conn, err := net.Dial("tcp", pool.WalletUrl) 127 | if err == nil { 128 | pool.conn = conn 129 | pool.connbuff = bufio.NewReaderSize(pool.conn, 1024) 130 | Info.Println("Connect Agent", pool.WalletUrl, "success") 131 | return 132 | } 133 | 134 | retry++ 135 | if retry%10 == 0 { 136 | Info.Println("Dial", pool.WalletUrl, retry+1, "times,error:", err) 137 | } 138 | if retry > 100 { 139 | Alert.Panicln("Dial", pool.WalletUrl, "failed over 100 times, exit...") 140 | } 141 | time.Sleep(time.Second) 142 | } 143 | } 144 | 145 | func (pool *Pool) SubmitBlock(data string) { 146 | 147 | for { 148 | if pool.conn != nil { 149 | break 150 | } 151 | time.Sleep(time.Second) 152 | } 153 | 154 | _, err := pool.conn.Write([]byte(data)) 155 | if err != nil { 156 | BlockLog.Println("SubmitBlock error:", err) 157 | } 158 | } 159 | 160 | type Job struct { 161 | Input string 162 | Height int64 163 | Id string 164 | PrevHash string 165 | 166 | Target *big.Int 167 | netDiffStr string 168 | currDiffStr string 169 | } 170 | 171 | func (pool *Pool) broadcastNewJobs(jobid, prehash, input string) { 172 | pool.minersLock.RLock() 173 | defer pool.minersLock.RUnlock() 174 | 175 | start := time.Now() 176 | bcast := make(chan int, 512) 177 | n := 0 178 | 179 | for m, _ := range pool.miners { 180 | n++ 181 | bcast <- n 182 | 183 | go func(miner *Miner) { 184 | newDiff := miner.dc.calcCurDiff() 185 | 186 | miner.Lock() 187 | if miner.diff != newDiff { 188 | Info.Println("diff:", miner.diff, "newdiff:", newDiff) 189 | 190 | miner.diff = newDiff 191 | miner.diffStr = fmt.Sprintf("%f", miner.diff) 192 | miner.target = new(big.Int).Div(pow256, new(big.Int).SetInt64(int64(miner.diff*DiffUnit))) 193 | 194 | miner.SetDifficulty() 195 | } 196 | 197 | var respStr string 198 | if miner.minertype == "cpu" { 199 | respStr = fmt.Sprintf("{\"jobid\":\"%s\",\"prev\":\"%s\",\"input\":\"%s\",\"id\":\"1\",\"jsonrpc\":\"2.0\",\"method\":\"mining_notify\"}\n", jobid, prehash, input) 200 | } else { 201 | respStr = fmt.Sprintf("{\"id\":null,\"method\":\"mining.notify\",\"params\":[\"%s\",\"%s\",\"%s\",true]}\n", jobid, prehash, input) 202 | } 203 | pool.LastNotify = respStr 204 | _, err := miner.conn.Write([]byte(respStr)) 205 | miner.Unlock() 206 | 207 | if err != nil { 208 | Warning.Printf("BroadcastNewJobs to %s.%s@%s error: %v", miner.username, miner.workername, miner.IP, err) 209 | } 210 | 211 | <-bcast 212 | }(m) 213 | } 214 | pool.lastNewBlockTime = time.Now() 215 | 216 | Info.Printf("Broadcast Jobs to %d miner finished %s", n, time.Since(start)) 217 | } 218 | 219 | func (pool *Pool) Login() { 220 | loginCmd := fmt.Sprintf("{\"method\":\"login\",\"api_key\":\"%s\",\"id\":\"login\",\"jsonrpc\":\"2.0\"}\n", cfg.ApiKey) 221 | _, err := pool.conn.Write([]byte(loginCmd)) 222 | if err != nil { 223 | Info.Println("Login error:", err) 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /log.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "build" 5 | "fmt" 6 | "io" 7 | "log" 8 | "os" 9 | ) 10 | 11 | var ( 12 | Debug Logger 13 | Info *log.Logger 14 | Warning *log.Logger 15 | Alert *log.Logger 16 | 17 | ShareLog *log.Logger 18 | BlockLog *log.Logger 19 | ) 20 | 21 | const ( 22 | DEBUG_LEVEL = 7 23 | INFO_LEVEL = 6 24 | WARNING_LEVEL = 4 25 | ALERT_LEVEL = 1 26 | ) 27 | 28 | func InitLog(infoFile, errorFile, shareFile, blockFile string) { 29 | log.Println("infoFile:", infoFile) 30 | log.Println("errorFile:", errorFile) 31 | log.Println("shareFile:", shareFile) 32 | log.Println("blockFile:", blockFile) 33 | infoFd, err := os.OpenFile(infoFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) 34 | if err != nil { 35 | log.Fatalln("Failed to open error log file:", err) 36 | } 37 | 38 | errorFd, err := os.OpenFile(errorFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) 39 | if err != nil { 40 | log.Fatalln("Failed to open error log file:", err) 41 | } 42 | 43 | shareFd, err := os.OpenFile(shareFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) 44 | if err != nil { 45 | log.Fatalln("Failed to open error log file:", err) 46 | } 47 | 48 | blockFd, err := os.OpenFile(blockFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) 49 | if err != nil { 50 | log.Fatalln("Failed to open error log file:", err) 51 | } 52 | 53 | Debug.l = log.New(io.MultiWriter(os.Stdout, infoFd), "[DEBUG] ", log.Ldate|log.Lmicroseconds|log.Lshortfile) 54 | Info = log.New(io.MultiWriter(os.Stdout, infoFd), "[INFO] ", log.Ldate|log.Lmicroseconds|log.Lshortfile) 55 | Warning = log.New(io.MultiWriter(os.Stdout, infoFd, errorFd), "[WARN] ", log.Ldate|log.Lmicroseconds|log.Lshortfile) 56 | Alert = log.New(io.MultiWriter(os.Stdout, infoFd, errorFd), "[ALERT] ", log.Ldate|log.Lmicroseconds|log.Lshortfile) 57 | 58 | ShareLog = log.New(io.MultiWriter(shareFd), "", log.Ldate|log.Lmicroseconds) 59 | BlockLog = log.New(io.MultiWriter(blockFd, os.Stdout), "", log.Ldate|log.Lmicroseconds) 60 | } 61 | 62 | type Logger struct { 63 | l *log.Logger 64 | } 65 | 66 | func (l *Logger) Print(v ...interface{}) { 67 | if !build.DEBUG { 68 | return 69 | } 70 | l.l.Output(2, fmt.Sprint(v...)) 71 | } 72 | 73 | func (l *Logger) Println(v ...interface{}) { 74 | if !build.DEBUG { 75 | return 76 | } 77 | l.l.Output(2, fmt.Sprintln(v...)) 78 | } 79 | 80 | func (l *Logger) Printf(format string, v ...interface{}) { 81 | if !build.DEBUG { 82 | return 83 | } 84 | l.l.Output(2, fmt.Sprintf(format, v...)) 85 | } 86 | 87 | func (l *Logger) Fatal(v ...interface{}) { 88 | if !build.DEBUG { 89 | return 90 | } 91 | l.l.Output(2, fmt.Sprint(v...)) 92 | os.Exit(1) 93 | } 94 | 95 | func (l *Logger) Fatalf(format string, v ...interface{}) { 96 | if !build.DEBUG { 97 | return 98 | } 99 | l.l.Output(2, fmt.Sprintf(format, v...)) 100 | os.Exit(1) 101 | } 102 | 103 | func (l *Logger) Fatalln(v ...interface{}) { 104 | if !build.DEBUG { 105 | return 106 | } 107 | l.l.Output(2, fmt.Sprintln(v...)) 108 | os.Exit(1) 109 | } 110 | 111 | func (l *Logger) Panic(v ...interface{}) { 112 | s := fmt.Sprint(v...) 113 | if !build.DEBUG { 114 | return 115 | } 116 | l.l.Output(2, s) 117 | panic(s) 118 | } 119 | 120 | func (l *Logger) Panicf(format string, v ...interface{}) { 121 | s := fmt.Sprintf(format, v...) 122 | if !build.DEBUG { 123 | return 124 | } 125 | l.l.Output(2, s) 126 | panic(s) 127 | } 128 | 129 | func (l *Logger) Panicln(v ...interface{}) { 130 | s := fmt.Sprintln(v...) 131 | if !build.DEBUG { 132 | return 133 | } 134 | l.l.Output(2, s) 135 | panic(s) 136 | } 137 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "build" 5 | //"fmt" 6 | ) 7 | 8 | import ( 9 | "github.com/jrick/bitset" 10 | "math/big" 11 | "os" 12 | "path/filepath" 13 | ) 14 | 15 | var ( 16 | pow256 = BigPow(2, 256) 17 | cfg Config 18 | gPool *Pool 19 | ) 20 | 21 | const ( 22 | DiffUnit = 4 * 1024 * 1024 * 1024 / 256 23 | UnitChange = float64(4*1024*1024*1024) / 1000 / 1000 / 100 // 24 | ) 25 | 26 | func PreparePool() { 27 | 28 | LoadConfig("", &cfg) 29 | 30 | //IP := GetPublicIP() 31 | //day := time.Now().Format("20060102") 32 | 33 | //logFile := "pool_" + cfg.Coin + "_" + IP + "_" + day 34 | infoFile := "info.log" 35 | errroFile := "error.log" 36 | 37 | infoFile = filepath.Join("logs", infoFile) 38 | errroFile = filepath.Join("logs", errroFile) 39 | 40 | blockFile := filepath.Join("logs", "block.log") 41 | shareFile := filepath.Join("logs", "share.log") 42 | MkdirIfNoExist("logs") 43 | 44 | InitLog(infoFile, errroFile, shareFile, blockFile) 45 | 46 | Info.Println("Name:", build.BuildName) 47 | Info.Println("Version:", build.BuildVersion) 48 | Info.Println("Build Time:", build.BuildTime) 49 | Info.Println("Go version:", build.GoVersion) 50 | Info.Println("Commit id:", build.CommitID) 51 | } 52 | 53 | func InitPool() { 54 | cfg.TotalSeconds = cfg.SecondsPerShare * cfg.WindowSize 55 | 56 | gPool = &Pool{Coin: cfg.Coin, IP: GetPublicIP(), version: build.BuildVersion, cfg: cfg} 57 | gPool.found_block_map = make(map[string]string) 58 | 59 | diff := int64(cfg.StartDiff * DiffUnit) 60 | gPool.StartTarget = new(big.Int).Div(pow256, new(big.Int).SetInt64(diff)) 61 | gPool.StartDiff4G = cfg.StartDiff / 256 62 | Info.Println("StartDiff:", cfg.StartDiff, diff, gPool.StartDiff4G, gPool.StartTarget) 63 | 64 | gPool.WalletUrl = cfg.WalletUrl 65 | gPool.WalletApiUrl = cfg.WalletApiUrl 66 | gPool.walletReady = make(chan bool) 67 | gPool.SubmitSol = make(chan Response) 68 | 69 | gPool.miners = make(map[*Miner]struct{}) 70 | gPool.shareSet.shares = make(map[string]struct{}) 71 | gPool.timeout = ParseDuration(cfg.Timeout) 72 | 73 | gPool.Jobs = make(map[string]Job) 74 | gPool.ValidAddress = make(map[string]bool) 75 | 76 | if cfg.ENonceLen < 2 || cfg.ENonceLen > 4 { 77 | Warning.Printf("Error: ENonceLen:%d out of range [2,3,4]", cfg.ENonceLen) 78 | os.Exit(0) 79 | } 80 | gPool.preEnonce = cfg.PoolId << (cfg.ENonceLen*8 - 4) 81 | gPool.enonceRange = 1 << (cfg.ENonceLen*8 - 4) 82 | gPool.enonce = bitset.NewBytes(gPool.enonceRange) 83 | Info.Println("Init enonce:", gPool.preEnonce) 84 | 85 | gPool.openLevelDB() 86 | 87 | if !gPool.OpenMongoDB() { 88 | Alert.Fatalln("OpenMongoDB error") 89 | } 90 | } 91 | 92 | func main() { 93 | PreparePool() 94 | InitPool() 95 | go gPool.updateWork() 96 | 97 | <-gPool.walletReady 98 | go gPool.ListenTCP() 99 | 100 | Info.Println("pool is running...") 101 | 102 | go gPool.PushShareService() 103 | go gPool.PrintDailyShareServer() 104 | go gPool.CalcUserReward() 105 | go gPool.SendRewardToUsers() 106 | go gPool.CheckTxState() 107 | gPool.httpServer() 108 | 109 | gPool.closeDB() 110 | Warning.Println("exit pool.") 111 | 112 | } 113 | -------------------------------------------------------------------------------- /pool.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "time" 5 | 6 | "bufio" 7 | "encoding/hex" 8 | "fmt" 9 | "github.com/jrick/bitset" 10 | "github.com/syndtr/goleveldb/leveldb" 11 | "gopkg.in/mgo.v2" 12 | "math/big" 13 | "net" 14 | "os" 15 | "sync" 16 | ) 17 | 18 | const ( 19 | HashSize = 32 20 | ) 21 | 22 | type Pool struct { 23 | Coin string 24 | IP string 25 | version string 26 | isLogin bool 27 | 28 | cfg Config 29 | 30 | levelDB *leveldb.DB 31 | mgoSession *mgo.Session 32 | share_t *mgo.Collection 33 | foundblock_t *mgo.Collection 34 | 35 | found_block_mapLock sync.Mutex 36 | found_block_map map[string]string 37 | 38 | WalletUrl string 39 | WalletApiUrl string 40 | conn net.Conn 41 | connbuff *bufio.Reader 42 | 43 | height int64 44 | isFirstWork bool 45 | ready bool 46 | walletReady chan bool 47 | walletLock sync.RWMutex 48 | 49 | JobsLock sync.RWMutex 50 | Jobs map[string]Job 51 | LastJob Job 52 | LastNotify string 53 | ForkHeight int 54 | 55 | StartTarget *big.Int 56 | StartDiff4G float64 57 | 58 | lastNewBlockTime time.Time 59 | interval time.Duration 60 | 61 | minersLock sync.RWMutex 62 | miners map[*Miner]struct{} 63 | preEnonce int 64 | enonceRange int 65 | enonce bitset.Bytes 66 | 67 | shareSet ShareSet 68 | 69 | timeout time.Duration 70 | 71 | staleCount int64 72 | duplicateCount int64 73 | lowDiffCount int64 74 | acceptCount int64 75 | 76 | blockCount int 77 | 78 | shareDaily float64 79 | 80 | WalletCheckInterval time.Duration 81 | 82 | SubmitSol chan Response 83 | ValidAddress map[string]bool 84 | } 85 | 86 | func (pool *Pool) ListenTCP() { 87 | addr, err := net.ResolveTCPAddr("tcp", pool.cfg.PoolIPPort) 88 | if err != nil { 89 | Alert.Fatalf("ResolveTCPAddr error: %v", err) 90 | } 91 | server, err := net.ListenTCP("tcp", addr) 92 | if err != nil { 93 | Alert.Fatalf("ListenTCP error: %v", err) 94 | } 95 | defer server.Close() 96 | 97 | Info.Printf("listening on %s", pool.cfg.PoolIPPort) 98 | 99 | var accept = make(chan int, pool.enonceRange) 100 | n := 0 101 | 102 | for { 103 | conn, err := server.AcceptTCP() 104 | if err != nil { 105 | continue 106 | } 107 | conn.SetKeepAlive(true) 108 | 109 | ip, port, _ := net.SplitHostPort(conn.RemoteAddr().String()) 110 | 111 | n += 1 112 | miner := &Miner{conn: conn, IP: ip, Port: port} 113 | 114 | accept <- n 115 | go func(miner *Miner) { 116 | miner.pool = pool 117 | err := miner.handleTCPClient() 118 | 119 | pool.removeMiner(miner) 120 | conn.Close() 121 | 122 | if miner.isAuthorize { 123 | Info.Printf("RemoveMiner %s.%s error:%v\n", miner.username, miner.workername, err) 124 | } 125 | <-accept 126 | }(miner) 127 | } 128 | } 129 | 130 | func (pool *Pool) addMiner(miner *Miner) { 131 | pool.minersLock.Lock() 132 | defer pool.minersLock.Unlock() 133 | 134 | miner.ENonce, miner.EnonceNum = pool.generateENonce() 135 | pool.miners[miner] = struct{}{} 136 | 137 | } 138 | 139 | func (pool *Pool) removeMiner(miner *Miner) { 140 | pool.minersLock.Lock() 141 | defer pool.minersLock.Unlock() 142 | 143 | if _, found := pool.miners[miner]; found { 144 | pool.reuseENonce(miner.EnonceNum) 145 | delete(pool.miners, miner) 146 | } 147 | } 148 | 149 | func (pool *Pool) generateENonce() (string, int) { 150 | var enonce int 151 | for enonce = 0; enonce < pool.enonceRange; enonce++ { 152 | if !pool.enonce.Get(enonce) { 153 | break 154 | } 155 | } 156 | 157 | if enonce < pool.enonceRange { 158 | pool.enonce.Set(enonce) 159 | 160 | enonce = pool.preEnonce + enonce 161 | 162 | if cfg.ENonceLen == 2 { 163 | return fmt.Sprintf("%04x", enonce), enonce 164 | 165 | } else if cfg.ENonceLen == 3 { 166 | return fmt.Sprintf("%06x", enonce), enonce 167 | 168 | } else if cfg.ENonceLen == 4 { 169 | return fmt.Sprintf("%08x", enonce), enonce 170 | } 171 | 172 | } else { 173 | Warning.Println("generateENonce error: enonce exhaust") 174 | } 175 | 176 | return "", 0 177 | } 178 | 179 | func (pool *Pool) reuseENonce(enonce int) { 180 | pool.enonce.Unset(enonce) 181 | } 182 | 183 | func (pool *Pool) PrintDailyShareServer() { 184 | 185 | daySeconds := int64(86400) 186 | now := time.Now() 187 | time8hour := time.Date(now.Year(), now.Month(), now.Day(), 8, 0, 0, 0, now.Location()) 188 | 189 | diff := time8hour.Unix() - now.Unix() 190 | 191 | if diff < 0 { 192 | diff = daySeconds + diff 193 | } 194 | 195 | Info.Println("PrintDailyShareServer will start timer after", diff, "second") 196 | 197 | dailyTimer := time.NewTimer(time.Second * time.Duration(diff)) 198 | for { 199 | select { 200 | case <-dailyTimer.C: 201 | 202 | dailyTimer.Reset(time.Second * time.Duration(daySeconds)) 203 | pool.PrintDailyShare(float64(diff)) 204 | pool.shareDaily = 0 205 | diff = 86400 206 | } 207 | } 208 | } 209 | 210 | func (pool *Pool) PrintDailyShare(sec float64) { 211 | 212 | hashRate := pool.shareDaily * UnitChange / sec 213 | BlockLog.Println("Daily share", hashRate, "GH/s") 214 | } 215 | 216 | func CalcPowHash(data string, poolTarget, netTarget *big.Int) (bool, bool, string) { 217 | isValidShare := false 218 | isValidBlock := false 219 | 220 | var powHash Hash 221 | hash := X17r_Sum256(data) 222 | copy(powHash[:], hash) 223 | hashBig := HashToBig(&powHash) 224 | 225 | Info.Println("header:", data) 226 | Info.Println("hash:", hex.EncodeToString(hash)) 227 | Info.Println("poolTarget:", fmt.Sprintf("%064s", hex.EncodeToString(poolTarget.Bytes()))) 228 | Info.Println("netTarget:", fmt.Sprintf("%064s", hex.EncodeToString(netTarget.Bytes()))) 229 | Info.Println("hashBig:", fmt.Sprintf("%064s", hex.EncodeToString(hashBig.Bytes()))) 230 | 231 | if hashBig.Cmp(poolTarget) <= 0 { 232 | isValidShare = true 233 | if hashBig.Cmp(netTarget) <= 0 { 234 | isValidBlock = true 235 | } 236 | } 237 | 238 | return isValidShare, isValidBlock, fmt.Sprintf("%064s", hex.EncodeToString(hashBig.Bytes())) 239 | } 240 | 241 | func TestPow() { 242 | Info.Println("TestPow+") 243 | var powHash Hash 244 | data := "00000000b4f0a89aa1ca0da9fddd326bcb41cc7864e602252b07287b204cd4ccef01000000000000f5c8b2a75e9ac4de3a48bb80bd6d1a2f78c87572b88d9c2109e745e349bc4dfe00004c00a989d3c0" 245 | hash := X17r_Sum256(data) 246 | copy(powHash[:], hash) 247 | hashBig := HashToBig(&powHash) 248 | 249 | Info.Println("header:", data) 250 | Info.Println("hash:", hex.EncodeToString(hash)) 251 | Info.Println("diff:", new(big.Int).Div(pow256, hashBig).String()) 252 | Info.Println("hash:", hash) 253 | os.Exit(1) 254 | } 255 | -------------------------------------------------------------------------------- /proto.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | //For miner 8 | type MinerRequest struct { 9 | Id string `json:"id"` 10 | Method string `json:"method"` 11 | Minertype string `json:"minertype"` 12 | Miner string `json:"miner"` 13 | JobID string `json:"jobid"` 14 | Nonce string `json:"nonce"` 15 | JsonRPC string `json:"jsonrpc,omitempty"` 16 | Params []interface{} `json:"params"` 17 | } 18 | 19 | type MinerRequest2 struct { 20 | Id int `json:"id"` 21 | Method string `json:"method"` 22 | Minertype string `json:"minertype,omitempty"` 23 | Miner string `json:"miner"` 24 | JobID string `json:"jobid"` 25 | Nonce string `json:"nonce"` 26 | JsonRPC string `json:"jsonrpc,omitempty"` 27 | Params []interface{} `json:"params"` 28 | } 29 | 30 | //From wallet 31 | type Response struct { 32 | Id string `json:"id"` 33 | Method string `json:"method"` 34 | JsonRPC string `json:"jsonrpc,omitempty"` 35 | 36 | //For job 37 | NBits uint32 `json:"nbits,omitempty"` 38 | Height int64 `json:"height,omitempty"` 39 | Input string `json:"input,omitempty"` 40 | PrevHash string `json:"prev,omitempty"` 41 | 42 | //For login 43 | Code int `json:"code,omitempty"` 44 | Description string `json:"description,omitempty"` 45 | ForkHeight int `json:"forkheight,omitempty"` 46 | } 47 | 48 | type Response2 struct { 49 | Id int `json:"id"` 50 | Method string `json:"method"` 51 | JsonRPC string `json:"jsonrpc,omitempty"` 52 | 53 | //For job 54 | NBits uint32 `json:"nbits,omitempty"` 55 | Height int64 `json:"height,omitempty"` 56 | Input string `json:"input,omitempty"` 57 | PrevHash string `json:"prev,omitempty"` 58 | 59 | //For login 60 | Code int `json:"code,omitempty"` 61 | Description string `json:"description,omitempty"` 62 | ForkHeight int `json:"forkheight,omitempty"` 63 | } 64 | 65 | type ShareSet struct { 66 | shareLock sync.Mutex 67 | shares map[string]struct{} 68 | } 69 | -------------------------------------------------------------------------------- /rpcClient.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "github.com/pkg/errors" 8 | "io/ioutil" 9 | "net/http" 10 | "strings" 11 | ) 12 | 13 | type getUtxoRequest struct { 14 | JsonRpc string `json:"jsonrpc"` 15 | Id int64 `json:"id"` 16 | Method string `json:"method"` 17 | Params sendtxParams `json:"params"` 18 | } 19 | 20 | type getUtxoParams struct { 21 | Value int64 `json:"count"` 22 | Fee int64 `json:"skip"` 23 | } 24 | 25 | type getUtxoResponse struct { 26 | JsonRpc string `json:"jsonrpc"` 27 | Id int64 `json:"id"` 28 | Result []getUtxoResult `json:"result,omitempty"` 29 | Error ErrorInfo `json:"error,omitempty"` 30 | } 31 | 32 | type getUtxoResult struct { 33 | Amount int64 `json:"amount"` 34 | CreateTxId string `json:"createTxId"` 35 | Id string `json:"id"` 36 | Maturity uint64 `json:"maturity"` 37 | Session int64 `json:"session"` 38 | SpentTxId string `json:"spentTxId"` 39 | Status int64 `json:"status"` 40 | Status_string string `json:"status_string"` 41 | Type string `json:"type"` 42 | } 43 | 44 | func GetUtxos() (balance int64, err error) { 45 | var reqUtxo getUtxoRequest 46 | reqUtxo.Method = "get_utxo" 47 | reqUtxo.JsonRpc = "2.0" 48 | reqUtxo.Id = 1 49 | 50 | client := &http.Client{} 51 | bytesData, _ := json.Marshal(reqUtxo) 52 | req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData)) 53 | resp, _ := client.Do(req) 54 | defer resp.Body.Close() 55 | body, _ := ioutil.ReadAll(resp.Body) 56 | fmt.Println(string(body)) 57 | 58 | var res getUtxoResponse 59 | err = json.Unmarshal(body, &res) 60 | if err != nil { 61 | Warning.Println("Unmarshal Response error:", err) 62 | return 0, err 63 | } 64 | if strings.Contains(string(body), "error") { 65 | err = errors.New(res.Error.Message) 66 | return 0, err 67 | } 68 | 69 | var totalAmount int64 70 | for _, item := range res.Result { 71 | if item.Type != "mine" { 72 | continue 73 | } 74 | 75 | if item.Status != 2 && item.Status != 1 { 76 | continue 77 | } 78 | amount := item.Amount 79 | 80 | totalAmount += amount 81 | } 82 | 83 | return totalAmount, nil 84 | } 85 | 86 | type getWalletStatusRequest struct { 87 | JsonRpc string `json:"jsonrpc"` 88 | Id int64 `json:"id"` 89 | Method string `json:"method"` 90 | } 91 | 92 | type getWalletStatusResponse struct { 93 | JsonRpc string `json:"jsonrpc"` 94 | Id int64 `json:"id"` 95 | Result getWalletStatusResult `json:"result,omitempty"` 96 | Error ErrorInfo `json:"error,omitempty"` 97 | } 98 | 99 | type getWalletStatusResult struct { 100 | Current_height int64 `json:"current_height"` 101 | Current_state_hash string `json:"current_state_hash"` 102 | Prev_state_hash string `json:"prev_state_hash"` 103 | Available int64 `json:"available"` 104 | Receiving int64 `json:"receiving"` 105 | Sending int64 `json:"sending"` 106 | Maturing int64 `json:"maturing"` 107 | Locked int64 `json:"locked"` 108 | Difficulty float64 `json:"difficulty"` 109 | } 110 | 111 | func getWalletStatus() (balance int64, err error) { 112 | var reqUtxo getUtxoRequest 113 | reqUtxo.Method = "wallet_status" 114 | reqUtxo.JsonRpc = "2.0" 115 | reqUtxo.Id = 1 116 | 117 | client := &http.Client{} 118 | bytesData, _ := json.Marshal(reqUtxo) 119 | req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData)) 120 | resp, _ := client.Do(req) 121 | defer resp.Body.Close() 122 | body, _ := ioutil.ReadAll(resp.Body) 123 | fmt.Println(string(body)) 124 | 125 | var res getWalletStatusResponse 126 | err = json.Unmarshal(body, &res) 127 | if err != nil { 128 | Warning.Println("Unmarshal Response error:", err.Error()) 129 | return 0, err 130 | } 131 | if strings.Contains(string(body), "error") { 132 | err = errors.New(res.Error.Message) 133 | return 0, err 134 | } 135 | 136 | return res.Result.Available, nil 137 | } 138 | 139 | type sendTxRequest struct { 140 | JsonRpc string `json:"jsonrpc"` 141 | Id int64 `json:"id"` 142 | Method string `json:"method"` 143 | Params sendtxParams `json:"params"` 144 | } 145 | 146 | type sendtxParams struct { 147 | Value int64 `json:"value"` 148 | Fee int64 `json:"fee,omitempty"` 149 | From string `json:"from,omitempty"` 150 | Address string `json:"address"` 151 | Comment string `json:"comment,omitempty"` 152 | } 153 | 154 | type sendTxResponse struct { 155 | JsonRpc string `json:"jsonrpc"` 156 | Id int64 `json:"id"` 157 | Result sendtxResult `json:"result,omitempty"` 158 | Error ErrorInfo `json:"error,omitempty"` 159 | } 160 | 161 | type sendtxResult struct { 162 | Txid string `json:"txId"` 163 | } 164 | 165 | type ErrorInfo struct { 166 | Code int64 `json:"code"` 167 | Data string `json:"data"` 168 | Message string `json:"message"` 169 | } 170 | 171 | func sendTx(amount int64, toaddr string) (txId string, err error) { 172 | client := &http.Client{} 173 | var reqSendtx sendTxRequest 174 | reqSendtx.JsonRpc = "2.0" 175 | reqSendtx.Id = 1 176 | reqSendtx.Method = "tx_send" 177 | var reqparams sendtxParams 178 | reqparams.Value = amount 179 | reqparams.Address = toaddr 180 | reqparams.Fee = cfg.TxFee 181 | reqSendtx.Params = reqparams 182 | bytesData, err := json.Marshal(reqSendtx) 183 | if err != nil { 184 | Warning.Println("Marshal reqSendtx error:", err.Error()) 185 | } 186 | Info.Println("bytesData:", string(bytesData)) 187 | req, err := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData)) 188 | if err != nil { 189 | Warning.Println("NewRequest error:", err.Error()) 190 | } 191 | resp, _ := client.Do(req) 192 | defer resp.Body.Close() 193 | body, _ := ioutil.ReadAll(resp.Body) 194 | fmt.Println(string(body)) 195 | 196 | var res sendTxResponse 197 | err = json.Unmarshal(body, &res) 198 | if err != nil { 199 | Warning.Println("Unmarshal Response error:", err.Error()) 200 | return "", err 201 | } 202 | 203 | if res.Error.Message != "" { 204 | err = errors.New(res.Error.Message) 205 | return "", err 206 | } 207 | 208 | if res.Result.Txid != "" { 209 | return res.Result.Txid, nil 210 | } 211 | return 212 | } 213 | 214 | type validAddrRequest struct { 215 | JsonRpc string `json:"jsonrpc"` 216 | Id int64 `json:"id"` 217 | Method string `json:"method"` 218 | Params validAddrParams `json:"params"` 219 | } 220 | 221 | type validAddrParams struct { 222 | Address string `json:"address"` 223 | } 224 | 225 | type validAddrResponse struct { 226 | JsonRpc string `json:"jsonrpc"` 227 | Id int64 `json:"id"` 228 | Result validAddrResult `json:"result,omitempty"` 229 | Error ErrorInfo `json:"error,omitempty"` 230 | } 231 | 232 | type validAddrResult struct { 233 | IsMine bool `json:"is_mine"` 234 | IsValid bool `json:"is_valid"` 235 | } 236 | 237 | func validAddrress(addr string) (isvalid bool, err error) { 238 | client := &http.Client{} 239 | var validaddr validAddrRequest 240 | validaddr.JsonRpc = "2.0" 241 | validaddr.Id = 1 242 | validaddr.Method = "validate_address" 243 | var reqparams validAddrParams 244 | reqparams.Address = addr 245 | validaddr.Params = reqparams 246 | bytesData, _ := json.Marshal(validaddr) 247 | req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData)) 248 | resp, _ := client.Do(req) 249 | if resp != nil { 250 | defer resp.Body.Close() 251 | } else { 252 | err = errors.New("Connect wallet-api faild,please check.") 253 | return false, err 254 | } 255 | body, _ := ioutil.ReadAll(resp.Body) 256 | fmt.Println(string(body)) 257 | 258 | var res validAddrResponse 259 | err = json.Unmarshal(body, &res) 260 | if err != nil { 261 | Warning.Println("Unmarshal Response error:", err) 262 | return false, err 263 | } 264 | 265 | if res.Error.Message != "" { 266 | err = errors.New(res.Error.Message) 267 | return false, err 268 | } 269 | 270 | return res.Result.IsValid, nil 271 | } 272 | 273 | type checkTxRequest struct { 274 | JsonRpc string `json:"jsonrpc"` 275 | Id int64 `json:"id"` 276 | Method string `json:"method"` 277 | Params checkTxParams `json:"params"` 278 | } 279 | 280 | type checkTxParams struct { 281 | TxId string `json:"txId"` 282 | } 283 | 284 | type checkTxResponse struct { 285 | JsonRpc string `json:"jsonrpc"` 286 | Id int64 `json:"id"` 287 | Result checkTxResult `json:"result,omitempty"` 288 | Error ErrorInfo `json:"error,omitempty"` 289 | } 290 | 291 | type checkTxResult struct { 292 | TxId string `json:"txId"` 293 | Comment string `json:"comment"` 294 | Fee int64 `json:"fee"` 295 | Kernel string `json:"kernel"` 296 | Receiver string `json:"receiver"` 297 | Sender string `json:"sender"` 298 | Status int64 `json:"status"` 299 | Status_string string `json:"status_string"` 300 | Failure_reason string `json:"failure_reason"` 301 | Value int64 `json:"value"` 302 | Height int64 `json:"height"` 303 | Confirmations int64 `json:"confirmations"` 304 | Create_time int64 `json:"create_time"` 305 | Income bool `json:"income"` 306 | } 307 | 308 | func getTxState(txid string) (state int64, err error) { 309 | client := &http.Client{} 310 | var checkTx checkTxRequest 311 | checkTx.JsonRpc = "2.0" 312 | checkTx.Id = 1 313 | checkTx.Method = "tx_status" 314 | var reqparams checkTxParams 315 | reqparams.TxId = txid 316 | checkTx.Params = reqparams 317 | bytesData, _ := json.Marshal(checkTx) 318 | req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData)) 319 | resp, _ := client.Do(req) 320 | if resp != nil { 321 | defer resp.Body.Close() 322 | } else { 323 | err = errors.New("Connect wallet-api faild,please check.") 324 | return -1, err 325 | } 326 | body, _ := ioutil.ReadAll(resp.Body) 327 | fmt.Println(string(body)) 328 | 329 | var res checkTxResponse 330 | err = json.Unmarshal(body, &res) 331 | if err != nil { 332 | Warning.Println("Unmarshal Response error:", err) 333 | return -1, err 334 | } 335 | 336 | if res.Error.Message != "" { 337 | err = errors.New(res.Error.Message) 338 | return -1, err 339 | } 340 | 341 | return res.Result.Status, nil 342 | } 343 | 344 | type CancelTxResponse struct { 345 | JsonRpc string `json:"jsonrpc"` 346 | Id int64 `json:"id"` 347 | Result bool `json:"result,omitempty"` 348 | Error ErrorInfo `json:"error,omitempty"` 349 | } 350 | 351 | func cancelTx(txid string) (success bool, err error) { 352 | client := &http.Client{} 353 | var cancelTx checkTxRequest 354 | cancelTx.JsonRpc = "2.0" 355 | cancelTx.Id = 1 356 | cancelTx.Method = "tx_cancel" 357 | var reqparams checkTxParams 358 | reqparams.TxId = txid 359 | cancelTx.Params = reqparams 360 | bytesData, _ := json.Marshal(cancelTx) 361 | req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData)) 362 | resp, _ := client.Do(req) 363 | if resp != nil { 364 | defer resp.Body.Close() 365 | } else { 366 | err = errors.New("Connect wallet-api faild,please check.") 367 | return false, err 368 | } 369 | body, _ := ioutil.ReadAll(resp.Body) 370 | fmt.Println(string(body)) 371 | 372 | var res CancelTxResponse 373 | err = json.Unmarshal(body, &res) 374 | if err != nil { 375 | Warning.Println("Unmarshal Response error:", err) 376 | return false, err 377 | } 378 | 379 | if res.Error.Message != "" { 380 | err = errors.New(res.Error.Message) 381 | return false, err 382 | } 383 | 384 | return res.Result, nil 385 | } 386 | -------------------------------------------------------------------------------- /util.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/hex" 5 | "fmt" 6 | "log" 7 | "math/big" 8 | "net" 9 | "os" 10 | "strings" 11 | "time" 12 | ) 13 | 14 | func BigPow(a, b int64) *big.Int { 15 | r := big.NewInt(a) 16 | return r.Exp(r, big.NewInt(b), nil) 17 | } 18 | 19 | func GetTargetHex(diff *big.Int) string { 20 | 21 | target := new(big.Int).Div(pow256, diff) 22 | 23 | hex := hex.EncodeToString(target.Bytes()) 24 | 25 | return "0x" + fmt.Sprintf("%064s", hex) 26 | 27 | } 28 | 29 | func GetBytesFromHex(s string) []byte { 30 | if len(s) > 1 { 31 | if s[0:2] == "0x" || s[0:2] == "0X" { 32 | s = s[2:] 33 | } 34 | } 35 | if len(s)%2 == 1 { 36 | s = "0" + s 37 | } 38 | 39 | h, _ := hex.DecodeString(s) 40 | return h 41 | } 42 | 43 | func TargetHexToBigDiff(targetHex string) *big.Int { 44 | targetBytes := GetBytesFromHex(targetHex) 45 | return new(big.Int).Div(pow256, new(big.Int).SetBytes(targetBytes)) 46 | } 47 | 48 | func GetPublicIP() string { 49 | addrs, err := net.InterfaceAddrs() 50 | if err != nil { 51 | Warning.Println("getPublicIP error:", err) 52 | return "default" 53 | } 54 | 55 | var localIP net.IP 56 | var publicIP net.IP 57 | 58 | for _, address := range addrs { 59 | if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 60 | if ip4 := ipnet.IP.To4(); ip4 != nil { 61 | if ip4[0] == 10 || ip4[0] == 192 { 62 | localIP = ip4 63 | } else { 64 | publicIP = ip4 65 | } 66 | } 67 | } 68 | } 69 | if publicIP == nil { 70 | publicIP = localIP 71 | } 72 | if publicIP == nil { 73 | return "default" 74 | } 75 | 76 | return publicIP.String() 77 | 78 | } 79 | 80 | func ParseDuration(s string) time.Duration { 81 | value, err := time.ParseDuration(s) 82 | if err != nil { 83 | panic("util: Can't parse duration `" + s + "`: " + err.Error()) 84 | } 85 | return value 86 | } 87 | 88 | func MkdirIfNoExist(dir string) error { 89 | _, err := os.Stat(dir) 90 | 91 | if os.IsNotExist(err) { 92 | err := os.Mkdir(dir, os.ModePerm) 93 | if err != nil { 94 | log.Println("mkdir failed![%v]\n", err) 95 | } else { 96 | log.Println("mkdir success!\n") 97 | } 98 | return err 99 | } 100 | 101 | return err 102 | } 103 | 104 | var ( 105 | kilo float64 = 1000 106 | mega float64 = 1000000 107 | giga float64 = 1000000000 108 | tera float64 = 1000000000000 109 | ) 110 | 111 | func PrintHashRateSuffix(hashrate int) { 112 | var shareStr string 113 | rate := float64(hashrate) 114 | if rate > tera { 115 | val := rate / tera 116 | shareStr = fmt.Sprintf("pool hashrate:%.3fEH/s", val) 117 | } else if rate > giga { 118 | val := rate / giga 119 | shareStr = fmt.Sprintf("pool hashrate:%.3fGH/s", val) 120 | } else if rate > mega { 121 | val := rate / mega 122 | shareStr = fmt.Sprintf("pool hashrate:%.3fMH/s", val) 123 | } else if rate > kilo { 124 | val := rate / kilo 125 | shareStr = fmt.Sprintf("pool hashrate:%.3fKH/s", val) 126 | } else { 127 | shareStr = fmt.Sprintf("pool hashrate:%dH/s", hashrate) 128 | } 129 | ShareLog.Println(shareStr) 130 | } 131 | 132 | func GetErrorCodeString(errorCode int) string { 133 | var ret string 134 | switch errorCode { 135 | case STALE_SHARE: 136 | ret = "Stale share" 137 | case DUPLICATE_SHARE: 138 | ret = "Duplicate share" 139 | case LOW_DIFFICULTY: 140 | ret = "LOW_DIFFICULTY" 141 | case NOT_LOGIN: 142 | ret = "Not login" 143 | case NOT_GETWORK: 144 | ret = "Not getwork" 145 | case ILLEGAL_PARARMS: 146 | ret = "Illegal params" 147 | case JOB_NOT_FOUND: 148 | ret = "Job not found" 149 | default: 150 | ret = "unknown" 151 | } 152 | 153 | return ret 154 | } 155 | 156 | func CompactToBig(compact uint32) *big.Int { 157 | mantissa := compact & 0x007fffff 158 | isNegative := compact&0x00800000 != 0 159 | exponent := uint(compact >> 24) 160 | 161 | var bn *big.Int 162 | if exponent <= 3 { 163 | mantissa >>= 8 * (3 - exponent) 164 | bn = big.NewInt(int64(mantissa)) 165 | } else { 166 | bn = big.NewInt(int64(mantissa)) 167 | bn.Lsh(bn, 8*(exponent-3)) 168 | } 169 | 170 | if isNegative { 171 | bn = bn.Neg(bn) 172 | } 173 | 174 | return bn 175 | } 176 | 177 | type Hash [HashSize]byte 178 | 179 | func (hash *Hash) SetBytes(newHash []byte) error { 180 | nhlen := len(newHash) 181 | if nhlen != HashSize { 182 | return fmt.Errorf("invalid hash length of %v, want %v", nhlen, 183 | HashSize) 184 | } 185 | copy(hash[:], newHash) 186 | 187 | return nil 188 | } 189 | 190 | func HashToBig(hash *Hash) *big.Int { 191 | // A Hash is in little-endian, but the big package wants the bytes in 192 | // big-endian, so reverse them. 193 | buf := *hash 194 | blen := len(buf) 195 | for i := 0; i < blen/2; i++ { 196 | buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i] 197 | } 198 | 199 | return new(big.Int).SetBytes(buf[:]) 200 | } 201 | 202 | func reverseS(input string) (string, error) { 203 | a := strings.Split(input, "") 204 | sRev := "" 205 | //fmt.Println("a:", a, " sRev:", sRev) 206 | if len(a)%2 != 0 { 207 | return "", fmt.Errorf("Incorrect input length") 208 | } 209 | for i := 0; i < len(a); i += 2 { 210 | tmp := []string{a[i], a[i+1], sRev} 211 | sRev = strings.Join(tmp, "") 212 | } 213 | return sRev, nil 214 | } 215 | 216 | func ToFloat(compact uint32) float64 { 217 | var nShift = (compact >> 24) & 0xff 218 | var dDiff = float64(0x0000ffff) / (float64(compact & 0x00ffffff)) 219 | fmt.Println(nShift) 220 | fmt.Println(dDiff) 221 | 222 | for i := nShift; i < 30; { 223 | dDiff *= 256.0 224 | i++ 225 | } 226 | fmt.Println(nShift) 227 | for j := nShift; j > 30; { 228 | dDiff /= 256.0 229 | j-- 230 | } 231 | 232 | return dDiff 233 | } 234 | -------------------------------------------------------------------------------- /x17r.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // #include 4 | // #include 5 | // #include "./x17r/x17r.h" 6 | // #cgo CFLAGS: -std=gnu99 -Wall -I. 7 | // #cgo LDFLAGS: -L./x17r/ -lx17r 8 | import "C" 9 | 10 | import ( 11 | "encoding/hex" 12 | "unsafe" 13 | ) 14 | 15 | func X17r_Sum256(input string) []byte { 16 | in, err := hex.DecodeString(input) 17 | if err != nil { 18 | Warning.Println("X17r_Sum256 DecodeString error:", err) 19 | return nil 20 | } 21 | in1 := (*C.char)(unsafe.Pointer(&in[0])) 22 | 23 | output := make([]byte, 32) 24 | out := (*C.char)(unsafe.Pointer(&output[0])) 25 | 26 | C.x17r_hash(unsafe.Pointer(out), unsafe.Pointer(in1), C.int(len(input)/2)) 27 | 28 | return output 29 | } 30 | -------------------------------------------------------------------------------- /x17r/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_definitions() 2 | 3 | set(X17R_SRC 4 | aes_helper.c 5 | gost_streebog.c 6 | mod_blakecoin.c 7 | sph_blake.c 8 | sph_bmw.c 9 | sph_cubehash.c 10 | sph_echo.c 11 | sph_fugue.c 12 | sph_groestl.c 13 | sph_hamsi_helper.c 14 | sph_hamsi.c 15 | sph_haval.c 16 | sph_hefty1.c 17 | sph_jh.c 18 | sph_keccak.c 19 | sph_luffa.c 20 | sph_ripemd.c 21 | sph_sha2.c 22 | sph_sha2big.c 23 | sph_shabal.c 24 | sph_shavite.c 25 | sph_simd.c 26 | sph_skein.c 27 | sph_whirlpool.c 28 | x17r.c 29 | ) 30 | 31 | add_library(x17r STATIC ${X17R_SRC}) 32 | target_link_libraries(x17r) 33 | 34 | -------------------------------------------------------------------------------- /x17r/compat.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPAT_H__ 2 | #define __COMPAT_H__ 3 | #ifdef WIN32 4 | 5 | #include 6 | #include 7 | 8 | #ifndef localtime_r 9 | #define localtime_r(src, dst) localtime_s(dst, src) 10 | #endif 11 | 12 | #define sleep(secs) Sleep((secs) * 1000) 13 | 14 | enum { 15 | PRIO_PROCESS = 0, 16 | }; 17 | 18 | extern int opt_priority; 19 | static __inline int setpriority(int which, int who, int prio) 20 | { 21 | switch (opt_priority) { 22 | case 5: 23 | prio = THREAD_PRIORITY_TIME_CRITICAL; 24 | break; 25 | case 4: 26 | prio = THREAD_PRIORITY_HIGHEST; 27 | break; 28 | case 3: 29 | prio = THREAD_PRIORITY_ABOVE_NORMAL; 30 | break; 31 | case 2: 32 | prio = THREAD_PRIORITY_NORMAL; 33 | break; 34 | case 1: 35 | prio = THREAD_PRIORITY_BELOW_NORMAL; 36 | break; 37 | case 0: 38 | default: 39 | prio = THREAD_PRIORITY_IDLE; 40 | } 41 | return -!SetThreadPriority(GetCurrentThread(), prio); 42 | } 43 | 44 | #ifdef _MSC_VER 45 | #define snprintf(...) _snprintf(__VA_ARGS__) 46 | #define strdup(...) _strdup(__VA_ARGS__) 47 | #define strncasecmp(x,y,z) _strnicmp(x,y,z) 48 | #define strcasecmp(x,y) _stricmp(x,y) 49 | #define __func__ __FUNCTION__ 50 | #define __thread __declspec(thread) 51 | #define _ALIGN(x) __declspec(align(x)) 52 | typedef int ssize_t; 53 | 54 | __inline int msver(void) { 55 | switch (_MSC_VER) { 56 | case 1500: return 2008; 57 | case 1600: return 2010; 58 | case 1700: return 2012; 59 | case 1800: return 2013; 60 | case 1900: return 2015; 61 | default: return (_MSC_VER/100); 62 | } 63 | } 64 | 65 | #include 66 | // This static var is made to be compatible with linux/mingw (no free on string result) 67 | // This is not thread safe but we only use that once on process start 68 | static char dirname_buffer[_MAX_PATH] = { 0 }; 69 | static __inline char * dirname(char *file) { 70 | char drive[_MAX_DRIVE] = { 0 }; 71 | char dir[_MAX_DIR] = { 0 }; 72 | char fname[_MAX_FNAME], ext[_MAX_EXT]; 73 | _splitpath_s(file, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT); 74 | if (dir && strlen(dir) && dir[strlen(dir)-1] == '\\') { 75 | dir[strlen(dir) - 1] = '\0'; 76 | } 77 | sprintf(dirname_buffer, "%s%s", drive, dir); 78 | return &dirname_buffer[0]; 79 | } 80 | #endif 81 | 82 | #endif /* WIN32 */ 83 | 84 | #ifndef _MSC_VER 85 | #define _ALIGN(x) __attribute__ ((aligned(x))) 86 | #endif 87 | 88 | #undef unlikely 89 | #undef likely 90 | #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 91 | #define unlikely(expr) (__builtin_expect(!!(expr), 0)) 92 | #define likely(expr) (__builtin_expect(!!(expr), 1)) 93 | #else 94 | #define unlikely(expr) (expr) 95 | #define likely(expr) (expr) 96 | #endif 97 | 98 | #ifndef WIN32 99 | #define MAX_PATH PATH_MAX 100 | #endif 101 | 102 | #endif /* __COMPAT_H__ */ 103 | -------------------------------------------------------------------------------- /x17r/gost_streebog.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_gost.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * GOST interface. This is the interface for GOST R 12 with the 4 | * recommended parameters for SHA-3, with output lengths 256 5 | * and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_gost.h 33 | * @author Mish 34 | */ 35 | 36 | #ifndef SPH_GOST_H__ 37 | #define SPH_GOST_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for GOST-256. 48 | */ 49 | #define SPH_SIZE_gost256 256 50 | 51 | /** 52 | * Output size (in bits) for GOST-512. 53 | */ 54 | #define SPH_SIZE_gost512 512 55 | 56 | /** 57 | * This structure is a context for Keccak computations: it contains the 58 | * intermediate values and some data from the last entered block. Once a 59 | * GOST computation has been performed, the context can be reused for 60 | * another computation. 61 | * 62 | * The contents of this structure are private. A running GOST computation 63 | * can be cloned by copying the context (e.g. with a simple 64 | * memcpy()). 65 | */ 66 | 67 | /** 68 | * This structure is a context for Gost-256 computations. 69 | */ 70 | 71 | typedef struct { 72 | #ifndef DOXYGEN_IGNORE 73 | unsigned char buf[32]; /* first field, for alignment */ 74 | size_t ptr; 75 | sph_u32 V[3][8]; 76 | #endif 77 | } sph_gost256_context; 78 | 79 | /** 80 | * This structure is a context for Gost-512 computations. 81 | */ 82 | typedef struct { 83 | #ifndef DOXYGEN_IGNORE 84 | unsigned char buf[64]; /* first field, for alignment */ 85 | size_t ptr; 86 | sph_u32 V[5][8]; 87 | #endif 88 | } sph_gost512_context; 89 | 90 | 91 | /** 92 | * Initialize a GOST-256 context. This process performs no memory allocation. 93 | * 94 | * @param cc the GOST-256 context (pointer to a 95 | * sph_gost256_context) 96 | */ 97 | void sph_gost256_init(void *cc); 98 | 99 | /** 100 | * Process some data bytes. It is acceptable that len is zero 101 | * (in which case this function does nothing). 102 | * 103 | * @param cc the Gost-256 context 104 | * @param data the input data 105 | * @param len the input data length (in bytes) 106 | */ 107 | void sph_gost256(void *cc, const void *data, size_t len); 108 | 109 | /** 110 | * Terminate the current GOST-256 computation and output the result into 111 | * the provided buffer. The destination buffer must be wide enough to 112 | * accomodate the result (32 bytes). The context is automatically 113 | * reinitialized. 114 | * 115 | * @param cc the GOST-256 context 116 | * @param dst the destination buffer 117 | */ 118 | void sph_gost256_close(void *cc, void *dst); 119 | 120 | /** 121 | * Add a few additional bits (0 to 7) to the current computation, then 122 | * terminate it and output the result in the provided buffer, which must 123 | * be wide enough to accomodate the result (32 bytes). If bit number i 124 | * in ub has value 2^i, then the extra bits are those 125 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 126 | * level). The context is automatically reinitialized. 127 | * 128 | * @param cc the GOST-256 context 129 | * @param ub the extra bits 130 | * @param n the number of extra bits (0 to 7) 131 | * @param dst the destination buffer 132 | */ 133 | void sph_gost256_addbits_and_close( 134 | void *cc, unsigned ub, unsigned n, void *dst); 135 | 136 | /** 137 | * Initialize a Gost-512 context. This process performs no memory allocation. 138 | * 139 | * @param cc the GOST-512 context (pointer to a 140 | * sph_gost512_context) 141 | */ 142 | void sph_gost512_init(void *cc); 143 | 144 | /** 145 | * Process some data bytes. It is acceptable that len is zero 146 | * (in which case this function does nothing). 147 | * 148 | * @param cc the GOST-512 context 149 | * @param data the input data 150 | * @param len the input data length (in bytes) 151 | */ 152 | void sph_gost512(void *cc, const void *data, size_t len); 153 | 154 | /** 155 | * Terminate the current GOST-512 computation and output the result into 156 | * the provided buffer. The destination buffer must be wide enough to 157 | * accomodate the result (64 bytes). The context is automatically 158 | * reinitialized. 159 | * 160 | * @param cc the GOST-512 context 161 | * @param dst the destination buffer 162 | */ 163 | void sph_gost512_close(void *cc, void *dst); 164 | 165 | /** 166 | * Add a few additional bits (0 to 7) to the current computation, then 167 | * terminate it and output the result in the provided buffer, which must 168 | * be wide enough to accomodate the result (64 bytes). If bit number i 169 | * in ub has value 2^i, then the extra bits are those 170 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 171 | * level). The context is automatically reinitialized. 172 | * 173 | * @param cc the GOST-512 context 174 | * @param ub the extra bits 175 | * @param n the number of extra bits (0 to 7) 176 | * @param dst the destination buffer 177 | */ 178 | void sph_gost512_addbits_and_close( 179 | void *cc, unsigned ub, unsigned n, void *dst); 180 | 181 | #ifdef __cplusplus 182 | } 183 | #endif 184 | 185 | #endif 186 | -------------------------------------------------------------------------------- /x17r/haval_helper.c: -------------------------------------------------------------------------------- 1 | /* $Id: haval_helper.c 218 2010-06-08 17:06:34Z tp $ */ 2 | /* 3 | * Helper code, included (three times !) by HAVAL implementation. 4 | * 5 | * TODO: try to merge this with md_helper.c. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @author Thomas Pornin 33 | */ 34 | 35 | #undef SPH_XCAT 36 | #define SPH_XCAT(a, b) SPH_XCAT_(a, b) 37 | #undef SPH_XCAT_ 38 | #define SPH_XCAT_(a, b) a ## b 39 | 40 | static void 41 | #ifdef SPH_UPTR 42 | SPH_XCAT(SPH_XCAT(haval, PASSES), _short) 43 | #else 44 | SPH_XCAT(haval, PASSES) 45 | #endif 46 | (sph_haval_context *sc, const void *data, size_t len) 47 | { 48 | unsigned current; 49 | 50 | #if SPH_64 51 | current = (unsigned)sc->count & 127U; 52 | #else 53 | current = (unsigned)sc->count_low & 127U; 54 | #endif 55 | while (len > 0) { 56 | unsigned clen; 57 | #if !SPH_64 58 | sph_u32 clow, clow2; 59 | #endif 60 | 61 | clen = 128U - current; 62 | if (clen > len) 63 | clen = (unsigned) len; 64 | memcpy(sc->buf + current, data, clen); 65 | data = (const unsigned char *)data + clen; 66 | current += clen; 67 | len -= clen; 68 | if (current == 128U) { 69 | DSTATE; 70 | IN_PREPARE(sc->buf); 71 | 72 | RSTATE; 73 | SPH_XCAT(CORE, PASSES)(INW); 74 | WSTATE; 75 | current = 0; 76 | } 77 | #if SPH_64 78 | sc->count += clen; 79 | #else 80 | clow = sc->count_low; 81 | clow2 = SPH_T32(clow + clen); 82 | sc->count_low = clow2; 83 | if (clow2 < clow) 84 | sc->count_high ++; 85 | #endif 86 | } 87 | } 88 | 89 | #ifdef SPH_UPTR 90 | static void 91 | SPH_XCAT(haval, PASSES)(sph_haval_context *sc, const void *data, size_t len) 92 | { 93 | unsigned current; 94 | size_t orig_len; 95 | #if !SPH_64 96 | sph_u32 clow, clow2; 97 | #endif 98 | DSTATE; 99 | 100 | if (len < 256U) { 101 | SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len); 102 | return; 103 | } 104 | #if SPH_64 105 | current = (unsigned)sc->count & 127U; 106 | #else 107 | current = (unsigned)sc->count_low & 127U; 108 | #endif 109 | if (current > 0) { 110 | unsigned clen; 111 | 112 | clen = 128U - current; 113 | SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, clen); 114 | data = (const unsigned char *)data + clen; 115 | len -= clen; 116 | } 117 | #if !SPH_UNALIGNED 118 | if (((SPH_UPTR)data & 3U) != 0) { 119 | SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len); 120 | return; 121 | } 122 | #endif 123 | orig_len = len; 124 | RSTATE; 125 | while (len >= 128U) { 126 | IN_PREPARE(data); 127 | 128 | SPH_XCAT(CORE, PASSES)(INW); 129 | data = (const unsigned char *)data + 128U; 130 | len -= 128U; 131 | } 132 | WSTATE; 133 | if (len > 0) 134 | memcpy(sc->buf, data, len); 135 | #if SPH_64 136 | sc->count += (sph_u64)orig_len; 137 | #else 138 | clow = sc->count_low; 139 | clow2 = SPH_T32(clow + orig_len); 140 | sc->count_low = clow2; 141 | if (clow2 < clow) 142 | sc->count_high ++; 143 | orig_len >>= 12; 144 | orig_len >>= 10; 145 | orig_len >>= 10; 146 | sc->count_high += orig_len; 147 | #endif 148 | } 149 | #endif 150 | 151 | static void 152 | SPH_XCAT(SPH_XCAT(haval, PASSES), _close)(sph_haval_context *sc, 153 | unsigned ub, unsigned n, void *dst) 154 | { 155 | unsigned current; 156 | DSTATE; 157 | 158 | #if SPH_64 159 | current = (unsigned)sc->count & 127U; 160 | #else 161 | current = (unsigned)sc->count_low & 127U; 162 | #endif 163 | sc->buf[current ++] = (0x01 << n) | ((ub & 0xFF) >> (8 - n)); 164 | RSTATE; 165 | if (current > 118U) { 166 | memset(sc->buf + current, 0, 128U - current); 167 | 168 | do { 169 | IN_PREPARE(sc->buf); 170 | 171 | SPH_XCAT(CORE, PASSES)(INW); 172 | } while (0); 173 | current = 0; 174 | } 175 | memset(sc->buf + current, 0, 118U - current); 176 | sc->buf[118] = 0x01 | (PASSES << 3); 177 | sc->buf[119] = sc->olen << 3; 178 | #if SPH_64 179 | sph_enc64le_aligned(sc->buf + 120, SPH_T64(sc->count << 3)); 180 | #else 181 | sph_enc32le_aligned(sc->buf + 120, SPH_T32(sc->count_low << 3)); 182 | sph_enc32le_aligned(sc->buf + 124, 183 | SPH_T32((sc->count_high << 3) | (sc->count_low >> 29))); 184 | #endif 185 | do { 186 | IN_PREPARE(sc->buf); 187 | 188 | SPH_XCAT(CORE, PASSES)(INW); 189 | } while (0); 190 | 191 | WSTATE; 192 | haval_out(sc, dst); 193 | haval_init(sc, sc->olen, sc->passes); 194 | } 195 | 196 | -------------------------------------------------------------------------------- /x17r/md_helper.c: -------------------------------------------------------------------------------- 1 | /* $Id: md_helper.c 216 2010-06-08 09:46:57Z tp $ */ 2 | /* 3 | * This file contains some functions which implement the external data 4 | * handling and padding for Merkle-Damgard hash functions which follow 5 | * the conventions set out by MD4 (little-endian) or SHA-1 (big-endian). 6 | * 7 | * API: this file is meant to be included, not compiled as a stand-alone 8 | * file. Some macros must be defined: 9 | * RFUN name for the round function 10 | * HASH "short name" for the hash function 11 | * BE32 defined for big-endian, 32-bit based (e.g. SHA-1) 12 | * LE32 defined for little-endian, 32-bit based (e.g. MD5) 13 | * BE64 defined for big-endian, 64-bit based (e.g. SHA-512) 14 | * LE64 defined for little-endian, 64-bit based (no example yet) 15 | * PW01 if defined, append 0x01 instead of 0x80 (for Tiger) 16 | * BLEN if defined, length of a message block (in bytes) 17 | * PLW1 if defined, length is defined on one 64-bit word only (for Tiger) 18 | * PLW4 if defined, length is defined on four 64-bit words (for WHIRLPOOL) 19 | * SVAL if defined, reference to the context state information 20 | * 21 | * BLEN is used when a message block is not 16 (32-bit or 64-bit) words: 22 | * this is used for instance for Tiger, which works on 64-bit words but 23 | * uses 512-bit message blocks (eight 64-bit words). PLW1 and PLW4 are 24 | * ignored if 32-bit words are used; if 64-bit words are used and PLW1 is 25 | * set, then only one word (64 bits) will be used to encode the input 26 | * message length (in bits), otherwise two words will be used (as in 27 | * SHA-384 and SHA-512). If 64-bit words are used and PLW4 is defined (but 28 | * not PLW1), four 64-bit words will be used to encode the message length 29 | * (in bits). Note that regardless of those settings, only 64-bit message 30 | * lengths are supported (in bits): messages longer than 2 Exabytes will be 31 | * improperly hashed (this is unlikely to happen soon: 2 Exabytes is about 32 | * 2 millions Terabytes, which is huge). 33 | * 34 | * If CLOSE_ONLY is defined, then this file defines only the sph_XXX_close() 35 | * function. This is used for Tiger2, which is identical to Tiger except 36 | * when it comes to the padding (Tiger2 uses the standard 0x80 byte instead 37 | * of the 0x01 from original Tiger). 38 | * 39 | * The RFUN function is invoked with two arguments, the first pointing to 40 | * aligned data (as a "const void *"), the second being state information 41 | * from the context structure. By default, this state information is the 42 | * "val" field from the context, and this field is assumed to be an array 43 | * of words ("sph_u32" or "sph_u64", depending on BE32/LE32/BE64/LE64). 44 | * from the context structure. The "val" field can have any type, except 45 | * for the output encoding which assumes that it is an array of "sph_u32" 46 | * values. By defining NO_OUTPUT, this last step is deactivated; the 47 | * includer code is then responsible for writing out the hash result. When 48 | * NO_OUTPUT is defined, the third parameter to the "close()" function is 49 | * ignored. 50 | * 51 | * ==========================(LICENSE BEGIN)============================ 52 | * 53 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 54 | * 55 | * Permission is hereby granted, free of charge, to any person obtaining 56 | * a copy of this software and associated documentation files (the 57 | * "Software"), to deal in the Software without restriction, including 58 | * without limitation the rights to use, copy, modify, merge, publish, 59 | * distribute, sublicense, and/or sell copies of the Software, and to 60 | * permit persons to whom the Software is furnished to do so, subject to 61 | * the following conditions: 62 | * 63 | * The above copyright notice and this permission notice shall be 64 | * included in all copies or substantial portions of the Software. 65 | * 66 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 67 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 68 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 69 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 70 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 71 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 72 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 73 | * 74 | * ===========================(LICENSE END)============================= 75 | * 76 | * @author Thomas Pornin 77 | */ 78 | 79 | #ifdef _MSC_VER 80 | #pragma warning (disable: 4146) 81 | #endif 82 | 83 | #undef SPH_XCAT 84 | #define SPH_XCAT(a, b) SPH_XCAT_(a, b) 85 | #undef SPH_XCAT_ 86 | #define SPH_XCAT_(a, b) a ## b 87 | 88 | #undef SPH_BLEN 89 | #undef SPH_WLEN 90 | #if defined BE64 || defined LE64 91 | #define SPH_BLEN 128U 92 | #define SPH_WLEN 8U 93 | #else 94 | #define SPH_BLEN 64U 95 | #define SPH_WLEN 4U 96 | #endif 97 | 98 | #ifdef BLEN 99 | #undef SPH_BLEN 100 | #define SPH_BLEN BLEN 101 | #endif 102 | 103 | #undef SPH_MAXPAD 104 | #if defined PLW1 105 | #define SPH_MAXPAD (SPH_BLEN - SPH_WLEN) 106 | #elif defined PLW4 107 | #define SPH_MAXPAD (SPH_BLEN - (SPH_WLEN << 2)) 108 | #else 109 | #define SPH_MAXPAD (SPH_BLEN - (SPH_WLEN << 1)) 110 | #endif 111 | 112 | #undef SPH_VAL 113 | #undef SPH_NO_OUTPUT 114 | #ifdef SVAL 115 | #define SPH_VAL SVAL 116 | #define SPH_NO_OUTPUT 1 117 | #else 118 | #define SPH_VAL sc->val 119 | #endif 120 | 121 | #ifndef CLOSE_ONLY 122 | 123 | #ifdef SPH_UPTR 124 | static void 125 | SPH_XCAT(HASH, _short)(void *cc, const void *data, size_t len) 126 | #else 127 | void 128 | SPH_XCAT(sph_, HASH)(void *cc, const void *data, size_t len) 129 | #endif 130 | { 131 | SPH_XCAT(sph_, SPH_XCAT(HASH, _context)) *sc; 132 | size_t current; 133 | 134 | sc = cc; 135 | #if SPH_64 136 | current = (unsigned)sc->count & (SPH_BLEN - 1U); 137 | #else 138 | current = (unsigned)sc->count_low & (SPH_BLEN - 1U); 139 | #endif 140 | while (len > 0) { 141 | size_t clen; 142 | #if !SPH_64 143 | sph_u32 clow, clow2; 144 | #endif 145 | 146 | clen = SPH_BLEN - current; 147 | if (clen > len) 148 | clen = len; 149 | memcpy(sc->buf + current, data, clen); 150 | data = (const unsigned char *)data + clen; 151 | current += clen; 152 | len -= clen; 153 | if (current == SPH_BLEN) { 154 | RFUN(sc->buf, SPH_VAL); 155 | current = 0; 156 | } 157 | #if SPH_64 158 | sc->count += clen; 159 | #else 160 | clow = sc->count_low; 161 | clow2 = SPH_T32(clow + clen); 162 | sc->count_low = clow2; 163 | if (clow2 < clow) 164 | sc->count_high ++; 165 | #endif 166 | } 167 | } 168 | 169 | #ifdef SPH_UPTR 170 | void 171 | SPH_XCAT(sph_, HASH)(void *cc, const void *data, size_t len) 172 | { 173 | SPH_XCAT(sph_, SPH_XCAT(HASH, _context)) *sc; 174 | unsigned current; 175 | size_t orig_len; 176 | #if !SPH_64 177 | sph_u32 clow, clow2; 178 | #endif 179 | 180 | if (len < (2 * SPH_BLEN)) { 181 | SPH_XCAT(HASH, _short)(cc, data, len); 182 | return; 183 | } 184 | sc = cc; 185 | #if SPH_64 186 | current = (unsigned)sc->count & (SPH_BLEN - 1U); 187 | #else 188 | current = (unsigned)sc->count_low & (SPH_BLEN - 1U); 189 | #endif 190 | if (current > 0) { 191 | unsigned t; 192 | 193 | t = SPH_BLEN - current; 194 | SPH_XCAT(HASH, _short)(cc, data, t); 195 | data = (const unsigned char *)data + t; 196 | len -= t; 197 | } 198 | #if !SPH_UNALIGNED 199 | if (((SPH_UPTR)data & (SPH_WLEN - 1U)) != 0) { 200 | SPH_XCAT(HASH, _short)(cc, data, len); 201 | return; 202 | } 203 | #endif 204 | orig_len = len; 205 | while (len >= SPH_BLEN) { 206 | RFUN(data, SPH_VAL); 207 | len -= SPH_BLEN; 208 | data = (const unsigned char *)data + SPH_BLEN; 209 | } 210 | if (len > 0) 211 | memcpy(sc->buf, data, len); 212 | #if SPH_64 213 | sc->count += (sph_u64)orig_len; 214 | #else 215 | clow = sc->count_low; 216 | clow2 = SPH_T32(clow + orig_len); 217 | sc->count_low = clow2; 218 | if (clow2 < clow) 219 | sc->count_high ++; 220 | /* 221 | * This code handles the improbable situation where "size_t" is 222 | * greater than 32 bits, and yet we do not have a 64-bit type. 223 | */ 224 | orig_len >>= 12; 225 | orig_len >>= 10; 226 | orig_len >>= 10; 227 | sc->count_high += orig_len; 228 | #endif 229 | } 230 | #endif 231 | 232 | #endif 233 | 234 | /* 235 | * Perform padding and produce result. The context is NOT reinitialized 236 | * by this function. 237 | */ 238 | static void 239 | SPH_XCAT(HASH, _addbits_and_close)(void *cc, 240 | unsigned ub, unsigned n, void *dst, unsigned rnum) 241 | { 242 | SPH_XCAT(sph_, SPH_XCAT(HASH, _context)) *sc; 243 | unsigned current, u; 244 | #if !SPH_64 245 | sph_u32 low, high; 246 | #endif 247 | 248 | sc = cc; 249 | #if SPH_64 250 | current = (unsigned)sc->count & (SPH_BLEN - 1U); 251 | #else 252 | current = (unsigned)sc->count_low & (SPH_BLEN - 1U); 253 | #endif 254 | #ifdef PW01 255 | sc->buf[current ++] = (0x100 | (ub & 0xFF)) >> (8 - n); 256 | #else 257 | { 258 | unsigned z; 259 | 260 | z = 0x80 >> n; 261 | sc->buf[current ++] = ((ub & -z) | z) & 0xFF; 262 | } 263 | #endif 264 | if (current > SPH_MAXPAD) { 265 | memset(sc->buf + current, 0, SPH_BLEN - current); 266 | RFUN(sc->buf, SPH_VAL); 267 | memset(sc->buf, 0, SPH_MAXPAD); 268 | } else { 269 | memset(sc->buf + current, 0, SPH_MAXPAD - current); 270 | } 271 | #if defined BE64 272 | #if defined PLW1 273 | sph_enc64be_aligned(sc->buf + SPH_MAXPAD, 274 | SPH_T64(sc->count << 3) + (sph_u64)n); 275 | #elif defined PLW4 276 | memset(sc->buf + SPH_MAXPAD, 0, 2 * SPH_WLEN); 277 | sph_enc64be_aligned(sc->buf + SPH_MAXPAD + 2 * SPH_WLEN, 278 | sc->count >> 61); 279 | sph_enc64be_aligned(sc->buf + SPH_MAXPAD + 3 * SPH_WLEN, 280 | SPH_T64(sc->count << 3) + (sph_u64)n); 281 | #else 282 | sph_enc64be_aligned(sc->buf + SPH_MAXPAD, sc->count >> 61); 283 | sph_enc64be_aligned(sc->buf + SPH_MAXPAD + SPH_WLEN, 284 | SPH_T64(sc->count << 3) + (sph_u64)n); 285 | #endif 286 | #elif defined LE64 287 | #if defined PLW1 288 | sph_enc64le_aligned(sc->buf + SPH_MAXPAD, 289 | SPH_T64(sc->count << 3) + (sph_u64)n); 290 | #elif defined PLW1 291 | sph_enc64le_aligned(sc->buf + SPH_MAXPAD, 292 | SPH_T64(sc->count << 3) + (sph_u64)n); 293 | sph_enc64le_aligned(sc->buf + SPH_MAXPAD + SPH_WLEN, sc->count >> 61); 294 | memset(sc->buf + SPH_MAXPAD + 2 * SPH_WLEN, 0, 2 * SPH_WLEN); 295 | #else 296 | sph_enc64le_aligned(sc->buf + SPH_MAXPAD, 297 | SPH_T64(sc->count << 3) + (sph_u64)n); 298 | sph_enc64le_aligned(sc->buf + SPH_MAXPAD + SPH_WLEN, sc->count >> 61); 299 | #endif 300 | #else 301 | #if SPH_64 302 | #ifdef BE32 303 | sph_enc64be_aligned(sc->buf + SPH_MAXPAD, 304 | SPH_T64(sc->count << 3) + (sph_u64)n); 305 | #else 306 | sph_enc64le_aligned(sc->buf + SPH_MAXPAD, 307 | SPH_T64(sc->count << 3) + (sph_u64)n); 308 | #endif 309 | #else 310 | low = sc->count_low; 311 | high = SPH_T32((sc->count_high << 3) | (low >> 29)); 312 | low = SPH_T32(low << 3) + (sph_u32)n; 313 | #ifdef BE32 314 | sph_enc32be(sc->buf + SPH_MAXPAD, high); 315 | sph_enc32be(sc->buf + SPH_MAXPAD + SPH_WLEN, low); 316 | #else 317 | sph_enc32le(sc->buf + SPH_MAXPAD, low); 318 | sph_enc32le(sc->buf + SPH_MAXPAD + SPH_WLEN, high); 319 | #endif 320 | #endif 321 | #endif 322 | RFUN(sc->buf, SPH_VAL); 323 | #ifdef SPH_NO_OUTPUT 324 | (void)dst; 325 | (void)rnum; 326 | (void)u; 327 | #else 328 | for (u = 0; u < rnum; u ++) { 329 | #if defined BE64 330 | sph_enc64be((unsigned char *)dst + 8 * u, sc->val[u]); 331 | #elif defined LE64 332 | sph_enc64le((unsigned char *)dst + 8 * u, sc->val[u]); 333 | #elif defined BE32 334 | sph_enc32be((unsigned char *)dst + 4 * u, sc->val[u]); 335 | #else 336 | sph_enc32le((unsigned char *)dst + 4 * u, sc->val[u]); 337 | #endif 338 | } 339 | #endif 340 | } 341 | 342 | static void 343 | SPH_XCAT(HASH, _close)(void *cc, void *dst, unsigned rnum) 344 | { 345 | SPH_XCAT(HASH, _addbits_and_close)(cc, 0, 0, dst, rnum); 346 | } 347 | -------------------------------------------------------------------------------- /x17r/sph_bmw.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_bmw.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * BMW interface. BMW (aka "Blue Midnight Wish") is a family of 4 | * functions which differ by their output size; this implementation 5 | * defines BMW for output sizes 224, 256, 384 and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_bmw.h 33 | * @author Thomas Pornin 34 | */ 35 | 36 | #ifndef SPH_BMW_H__ 37 | #define SPH_BMW_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for BMW-224. 48 | */ 49 | #define SPH_SIZE_bmw224 224 50 | 51 | /** 52 | * Output size (in bits) for BMW-256. 53 | */ 54 | #define SPH_SIZE_bmw256 256 55 | 56 | #if SPH_64 57 | 58 | /** 59 | * Output size (in bits) for BMW-384. 60 | */ 61 | #define SPH_SIZE_bmw384 384 62 | 63 | /** 64 | * Output size (in bits) for BMW-512. 65 | */ 66 | #define SPH_SIZE_bmw512 512 67 | 68 | #endif 69 | 70 | /** 71 | * This structure is a context for BMW-224 and BMW-256 computations: 72 | * it contains the intermediate values and some data from the last 73 | * entered block. Once a BMW computation has been performed, the 74 | * context can be reused for another computation. 75 | * 76 | * The contents of this structure are private. A running BMW 77 | * computation can be cloned by copying the context (e.g. with a simple 78 | * memcpy()). 79 | */ 80 | typedef struct { 81 | #ifndef DOXYGEN_IGNORE 82 | unsigned char buf[64]; /* first field, for alignment */ 83 | size_t ptr; 84 | sph_u32 H[16]; 85 | #if SPH_64 86 | sph_u64 bit_count; 87 | #else 88 | sph_u32 bit_count_high, bit_count_low; 89 | #endif 90 | #endif 91 | } sph_bmw_small_context; 92 | 93 | /** 94 | * This structure is a context for BMW-224 computations. It is 95 | * identical to the common sph_bmw_small_context. 96 | */ 97 | typedef sph_bmw_small_context sph_bmw224_context; 98 | 99 | /** 100 | * This structure is a context for BMW-256 computations. It is 101 | * identical to the common sph_bmw_small_context. 102 | */ 103 | typedef sph_bmw_small_context sph_bmw256_context; 104 | 105 | #if SPH_64 106 | 107 | /** 108 | * This structure is a context for BMW-384 and BMW-512 computations: 109 | * it contains the intermediate values and some data from the last 110 | * entered block. Once a BMW computation has been performed, the 111 | * context can be reused for another computation. 112 | * 113 | * The contents of this structure are private. A running BMW 114 | * computation can be cloned by copying the context (e.g. with a simple 115 | * memcpy()). 116 | */ 117 | typedef struct { 118 | #ifndef DOXYGEN_IGNORE 119 | unsigned char buf[128]; /* first field, for alignment */ 120 | size_t ptr; 121 | sph_u64 H[16]; 122 | sph_u64 bit_count; 123 | #endif 124 | } sph_bmw_big_context; 125 | 126 | /** 127 | * This structure is a context for BMW-384 computations. It is 128 | * identical to the common sph_bmw_small_context. 129 | */ 130 | typedef sph_bmw_big_context sph_bmw384_context; 131 | 132 | /** 133 | * This structure is a context for BMW-512 computations. It is 134 | * identical to the common sph_bmw_small_context. 135 | */ 136 | typedef sph_bmw_big_context sph_bmw512_context; 137 | 138 | #endif 139 | 140 | /** 141 | * Initialize a BMW-224 context. This process performs no memory allocation. 142 | * 143 | * @param cc the BMW-224 context (pointer to a 144 | * sph_bmw224_context) 145 | */ 146 | void sph_bmw224_init(void *cc); 147 | 148 | /** 149 | * Process some data bytes. It is acceptable that len is zero 150 | * (in which case this function does nothing). 151 | * 152 | * @param cc the BMW-224 context 153 | * @param data the input data 154 | * @param len the input data length (in bytes) 155 | */ 156 | void sph_bmw224(void *cc, const void *data, size_t len); 157 | 158 | /** 159 | * Terminate the current BMW-224 computation and output the result into 160 | * the provided buffer. The destination buffer must be wide enough to 161 | * accomodate the result (28 bytes). The context is automatically 162 | * reinitialized. 163 | * 164 | * @param cc the BMW-224 context 165 | * @param dst the destination buffer 166 | */ 167 | void sph_bmw224_close(void *cc, void *dst); 168 | 169 | /** 170 | * Add a few additional bits (0 to 7) to the current computation, then 171 | * terminate it and output the result in the provided buffer, which must 172 | * be wide enough to accomodate the result (28 bytes). If bit number i 173 | * in ub has value 2^i, then the extra bits are those 174 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 175 | * level). The context is automatically reinitialized. 176 | * 177 | * @param cc the BMW-224 context 178 | * @param ub the extra bits 179 | * @param n the number of extra bits (0 to 7) 180 | * @param dst the destination buffer 181 | */ 182 | void sph_bmw224_addbits_and_close( 183 | void *cc, unsigned ub, unsigned n, void *dst); 184 | 185 | /** 186 | * Initialize a BMW-256 context. This process performs no memory allocation. 187 | * 188 | * @param cc the BMW-256 context (pointer to a 189 | * sph_bmw256_context) 190 | */ 191 | void sph_bmw256_init(void *cc); 192 | 193 | /** 194 | * Process some data bytes. It is acceptable that len is zero 195 | * (in which case this function does nothing). 196 | * 197 | * @param cc the BMW-256 context 198 | * @param data the input data 199 | * @param len the input data length (in bytes) 200 | */ 201 | void sph_bmw256(void *cc, const void *data, size_t len); 202 | 203 | /** 204 | * Terminate the current BMW-256 computation and output the result into 205 | * the provided buffer. The destination buffer must be wide enough to 206 | * accomodate the result (32 bytes). The context is automatically 207 | * reinitialized. 208 | * 209 | * @param cc the BMW-256 context 210 | * @param dst the destination buffer 211 | */ 212 | void sph_bmw256_close(void *cc, void *dst); 213 | 214 | /** 215 | * Add a few additional bits (0 to 7) to the current computation, then 216 | * terminate it and output the result in the provided buffer, which must 217 | * be wide enough to accomodate the result (32 bytes). If bit number i 218 | * in ub has value 2^i, then the extra bits are those 219 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 220 | * level). The context is automatically reinitialized. 221 | * 222 | * @param cc the BMW-256 context 223 | * @param ub the extra bits 224 | * @param n the number of extra bits (0 to 7) 225 | * @param dst the destination buffer 226 | */ 227 | void sph_bmw256_addbits_and_close( 228 | void *cc, unsigned ub, unsigned n, void *dst); 229 | 230 | #if SPH_64 231 | 232 | /** 233 | * Initialize a BMW-384 context. This process performs no memory allocation. 234 | * 235 | * @param cc the BMW-384 context (pointer to a 236 | * sph_bmw384_context) 237 | */ 238 | void sph_bmw384_init(void *cc); 239 | 240 | /** 241 | * Process some data bytes. It is acceptable that len is zero 242 | * (in which case this function does nothing). 243 | * 244 | * @param cc the BMW-384 context 245 | * @param data the input data 246 | * @param len the input data length (in bytes) 247 | */ 248 | void sph_bmw384(void *cc, const void *data, size_t len); 249 | 250 | /** 251 | * Terminate the current BMW-384 computation and output the result into 252 | * the provided buffer. The destination buffer must be wide enough to 253 | * accomodate the result (48 bytes). The context is automatically 254 | * reinitialized. 255 | * 256 | * @param cc the BMW-384 context 257 | * @param dst the destination buffer 258 | */ 259 | void sph_bmw384_close(void *cc, void *dst); 260 | 261 | /** 262 | * Add a few additional bits (0 to 7) to the current computation, then 263 | * terminate it and output the result in the provided buffer, which must 264 | * be wide enough to accomodate the result (48 bytes). If bit number i 265 | * in ub has value 2^i, then the extra bits are those 266 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 267 | * level). The context is automatically reinitialized. 268 | * 269 | * @param cc the BMW-384 context 270 | * @param ub the extra bits 271 | * @param n the number of extra bits (0 to 7) 272 | * @param dst the destination buffer 273 | */ 274 | void sph_bmw384_addbits_and_close( 275 | void *cc, unsigned ub, unsigned n, void *dst); 276 | 277 | /** 278 | * Initialize a BMW-512 context. This process performs no memory allocation. 279 | * 280 | * @param cc the BMW-512 context (pointer to a 281 | * sph_bmw512_context) 282 | */ 283 | void sph_bmw512_init(void *cc); 284 | 285 | /** 286 | * Process some data bytes. It is acceptable that len is zero 287 | * (in which case this function does nothing). 288 | * 289 | * @param cc the BMW-512 context 290 | * @param data the input data 291 | * @param len the input data length (in bytes) 292 | */ 293 | void sph_bmw512(void *cc, const void *data, size_t len); 294 | 295 | /** 296 | * Terminate the current BMW-512 computation and output the result into 297 | * the provided buffer. The destination buffer must be wide enough to 298 | * accomodate the result (64 bytes). The context is automatically 299 | * reinitialized. 300 | * 301 | * @param cc the BMW-512 context 302 | * @param dst the destination buffer 303 | */ 304 | void sph_bmw512_close(void *cc, void *dst); 305 | 306 | /** 307 | * Add a few additional bits (0 to 7) to the current computation, then 308 | * terminate it and output the result in the provided buffer, which must 309 | * be wide enough to accomodate the result (64 bytes). If bit number i 310 | * in ub has value 2^i, then the extra bits are those 311 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 312 | * level). The context is automatically reinitialized. 313 | * 314 | * @param cc the BMW-512 context 315 | * @param ub the extra bits 316 | * @param n the number of extra bits (0 to 7) 317 | * @param dst the destination buffer 318 | */ 319 | void sph_bmw512_addbits_and_close( 320 | void *cc, unsigned ub, unsigned n, void *dst); 321 | 322 | #endif 323 | 324 | #ifdef __cplusplus 325 | } 326 | #endif 327 | 328 | #endif 329 | -------------------------------------------------------------------------------- /x17r/sph_cubehash.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_cubehash.h 180 2010-05-08 02:29:25Z tp $ */ 2 | /** 3 | * CubeHash interface. CubeHash is a family of functions which differ by 4 | * their output size; this implementation defines CubeHash for output 5 | * sizes 224, 256, 384 and 512 bits, with the "standard parameters" 6 | * (CubeHash16/32 with the CubeHash specification notations). 7 | * 8 | * ==========================(LICENSE BEGIN)============================ 9 | * 10 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining 13 | * a copy of this software and associated documentation files (the 14 | * "Software"), to deal in the Software without restriction, including 15 | * without limitation the rights to use, copy, modify, merge, publish, 16 | * distribute, sublicense, and/or sell copies of the Software, and to 17 | * permit persons to whom the Software is furnished to do so, subject to 18 | * the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be 21 | * included in all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 26 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 27 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 28 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 29 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | * 31 | * ===========================(LICENSE END)============================= 32 | * 33 | * @file sph_cubehash.h 34 | * @author Thomas Pornin 35 | */ 36 | 37 | #ifndef SPH_CUBEHASH_H__ 38 | #define SPH_CUBEHASH_H__ 39 | 40 | #ifdef __cplusplus 41 | extern "C"{ 42 | #endif 43 | 44 | #include 45 | #include "sph_types.h" 46 | 47 | /** 48 | * Output size (in bits) for CubeHash-224. 49 | */ 50 | #define SPH_SIZE_cubehash224 224 51 | 52 | /** 53 | * Output size (in bits) for CubeHash-256. 54 | */ 55 | #define SPH_SIZE_cubehash256 256 56 | 57 | /** 58 | * Output size (in bits) for CubeHash-384. 59 | */ 60 | #define SPH_SIZE_cubehash384 384 61 | 62 | /** 63 | * Output size (in bits) for CubeHash-512. 64 | */ 65 | #define SPH_SIZE_cubehash512 512 66 | 67 | /** 68 | * This structure is a context for CubeHash computations: it contains the 69 | * intermediate values and some data from the last entered block. Once 70 | * a CubeHash computation has been performed, the context can be reused for 71 | * another computation. 72 | * 73 | * The contents of this structure are private. A running CubeHash computation 74 | * can be cloned by copying the context (e.g. with a simple 75 | * memcpy()). 76 | */ 77 | typedef struct { 78 | #ifndef DOXYGEN_IGNORE 79 | unsigned char buf[32]; /* first field, for alignment */ 80 | size_t ptr; 81 | sph_u32 state[32]; 82 | #endif 83 | } sph_cubehash_context; 84 | 85 | /** 86 | * Type for a CubeHash-224 context (identical to the common context). 87 | */ 88 | typedef sph_cubehash_context sph_cubehash224_context; 89 | 90 | /** 91 | * Type for a CubeHash-256 context (identical to the common context). 92 | */ 93 | typedef sph_cubehash_context sph_cubehash256_context; 94 | 95 | /** 96 | * Type for a CubeHash-384 context (identical to the common context). 97 | */ 98 | typedef sph_cubehash_context sph_cubehash384_context; 99 | 100 | /** 101 | * Type for a CubeHash-512 context (identical to the common context). 102 | */ 103 | typedef sph_cubehash_context sph_cubehash512_context; 104 | 105 | /** 106 | * Initialize a CubeHash-224 context. This process performs no memory 107 | * allocation. 108 | * 109 | * @param cc the CubeHash-224 context (pointer to a 110 | * sph_cubehash224_context) 111 | */ 112 | void sph_cubehash224_init(void *cc); 113 | 114 | /** 115 | * Process some data bytes. It is acceptable that len is zero 116 | * (in which case this function does nothing). 117 | * 118 | * @param cc the CubeHash-224 context 119 | * @param data the input data 120 | * @param len the input data length (in bytes) 121 | */ 122 | void sph_cubehash224(void *cc, const void *data, size_t len); 123 | 124 | /** 125 | * Terminate the current CubeHash-224 computation and output the result into 126 | * the provided buffer. The destination buffer must be wide enough to 127 | * accomodate the result (28 bytes). The context is automatically 128 | * reinitialized. 129 | * 130 | * @param cc the CubeHash-224 context 131 | * @param dst the destination buffer 132 | */ 133 | void sph_cubehash224_close(void *cc, void *dst); 134 | 135 | /** 136 | * Add a few additional bits (0 to 7) to the current computation, then 137 | * terminate it and output the result in the provided buffer, which must 138 | * be wide enough to accomodate the result (28 bytes). If bit number i 139 | * in ub has value 2^i, then the extra bits are those 140 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 141 | * level). The context is automatically reinitialized. 142 | * 143 | * @param cc the CubeHash-224 context 144 | * @param ub the extra bits 145 | * @param n the number of extra bits (0 to 7) 146 | * @param dst the destination buffer 147 | */ 148 | void sph_cubehash224_addbits_and_close( 149 | void *cc, unsigned ub, unsigned n, void *dst); 150 | 151 | /** 152 | * Initialize a CubeHash-256 context. This process performs no memory 153 | * allocation. 154 | * 155 | * @param cc the CubeHash-256 context (pointer to a 156 | * sph_cubehash256_context) 157 | */ 158 | void sph_cubehash256_init(void *cc); 159 | 160 | /** 161 | * Process some data bytes. It is acceptable that len is zero 162 | * (in which case this function does nothing). 163 | * 164 | * @param cc the CubeHash-256 context 165 | * @param data the input data 166 | * @param len the input data length (in bytes) 167 | */ 168 | void sph_cubehash256(void *cc, const void *data, size_t len); 169 | 170 | /** 171 | * Terminate the current CubeHash-256 computation and output the result into 172 | * the provided buffer. The destination buffer must be wide enough to 173 | * accomodate the result (32 bytes). The context is automatically 174 | * reinitialized. 175 | * 176 | * @param cc the CubeHash-256 context 177 | * @param dst the destination buffer 178 | */ 179 | void sph_cubehash256_close(void *cc, void *dst); 180 | 181 | /** 182 | * Add a few additional bits (0 to 7) to the current computation, then 183 | * terminate it and output the result in the provided buffer, which must 184 | * be wide enough to accomodate the result (32 bytes). If bit number i 185 | * in ub has value 2^i, then the extra bits are those 186 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 187 | * level). The context is automatically reinitialized. 188 | * 189 | * @param cc the CubeHash-256 context 190 | * @param ub the extra bits 191 | * @param n the number of extra bits (0 to 7) 192 | * @param dst the destination buffer 193 | */ 194 | void sph_cubehash256_addbits_and_close( 195 | void *cc, unsigned ub, unsigned n, void *dst); 196 | 197 | /** 198 | * Initialize a CubeHash-384 context. This process performs no memory 199 | * allocation. 200 | * 201 | * @param cc the CubeHash-384 context (pointer to a 202 | * sph_cubehash384_context) 203 | */ 204 | void sph_cubehash384_init(void *cc); 205 | 206 | /** 207 | * Process some data bytes. It is acceptable that len is zero 208 | * (in which case this function does nothing). 209 | * 210 | * @param cc the CubeHash-384 context 211 | * @param data the input data 212 | * @param len the input data length (in bytes) 213 | */ 214 | void sph_cubehash384(void *cc, const void *data, size_t len); 215 | 216 | /** 217 | * Terminate the current CubeHash-384 computation and output the result into 218 | * the provided buffer. The destination buffer must be wide enough to 219 | * accomodate the result (48 bytes). The context is automatically 220 | * reinitialized. 221 | * 222 | * @param cc the CubeHash-384 context 223 | * @param dst the destination buffer 224 | */ 225 | void sph_cubehash384_close(void *cc, void *dst); 226 | 227 | /** 228 | * Add a few additional bits (0 to 7) to the current computation, then 229 | * terminate it and output the result in the provided buffer, which must 230 | * be wide enough to accomodate the result (48 bytes). If bit number i 231 | * in ub has value 2^i, then the extra bits are those 232 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 233 | * level). The context is automatically reinitialized. 234 | * 235 | * @param cc the CubeHash-384 context 236 | * @param ub the extra bits 237 | * @param n the number of extra bits (0 to 7) 238 | * @param dst the destination buffer 239 | */ 240 | void sph_cubehash384_addbits_and_close( 241 | void *cc, unsigned ub, unsigned n, void *dst); 242 | 243 | /** 244 | * Initialize a CubeHash-512 context. This process performs no memory 245 | * allocation. 246 | * 247 | * @param cc the CubeHash-512 context (pointer to a 248 | * sph_cubehash512_context) 249 | */ 250 | void sph_cubehash512_init(void *cc); 251 | 252 | /** 253 | * Process some data bytes. It is acceptable that len is zero 254 | * (in which case this function does nothing). 255 | * 256 | * @param cc the CubeHash-512 context 257 | * @param data the input data 258 | * @param len the input data length (in bytes) 259 | */ 260 | void sph_cubehash512(void *cc, const void *data, size_t len); 261 | 262 | /** 263 | * Terminate the current CubeHash-512 computation and output the result into 264 | * the provided buffer. The destination buffer must be wide enough to 265 | * accomodate the result (64 bytes). The context is automatically 266 | * reinitialized. 267 | * 268 | * @param cc the CubeHash-512 context 269 | * @param dst the destination buffer 270 | */ 271 | void sph_cubehash512_close(void *cc, void *dst); 272 | 273 | /** 274 | * Add a few additional bits (0 to 7) to the current computation, then 275 | * terminate it and output the result in the provided buffer, which must 276 | * be wide enough to accomodate the result (64 bytes). If bit number i 277 | * in ub has value 2^i, then the extra bits are those 278 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 279 | * level). The context is automatically reinitialized. 280 | * 281 | * @param cc the CubeHash-512 context 282 | * @param ub the extra bits 283 | * @param n the number of extra bits (0 to 7) 284 | * @param dst the destination buffer 285 | */ 286 | void sph_cubehash512_addbits_and_close( 287 | void *cc, unsigned ub, unsigned n, void *dst); 288 | #ifdef __cplusplus 289 | } 290 | #endif 291 | 292 | #endif 293 | -------------------------------------------------------------------------------- /x17r/sph_echo.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_echo.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * ECHO interface. ECHO is a family of functions which differ by 4 | * their output size; this implementation defines ECHO for output 5 | * sizes 224, 256, 384 and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_echo.h 33 | * @author Thomas Pornin 34 | */ 35 | 36 | #ifndef SPH_ECHO_H__ 37 | #define SPH_ECHO_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for ECHO-224. 48 | */ 49 | #define SPH_SIZE_echo224 224 50 | 51 | /** 52 | * Output size (in bits) for ECHO-256. 53 | */ 54 | #define SPH_SIZE_echo256 256 55 | 56 | /** 57 | * Output size (in bits) for ECHO-384. 58 | */ 59 | #define SPH_SIZE_echo384 384 60 | 61 | /** 62 | * Output size (in bits) for ECHO-512. 63 | */ 64 | #define SPH_SIZE_echo512 512 65 | 66 | /** 67 | * This structure is a context for ECHO computations: it contains the 68 | * intermediate values and some data from the last entered block. Once 69 | * an ECHO computation has been performed, the context can be reused for 70 | * another computation. This specific structure is used for ECHO-224 71 | * and ECHO-256. 72 | * 73 | * The contents of this structure are private. A running ECHO computation 74 | * can be cloned by copying the context (e.g. with a simple 75 | * memcpy()). 76 | */ 77 | typedef struct { 78 | #ifndef DOXYGEN_IGNORE 79 | unsigned char buf[192]; /* first field, for alignment */ 80 | size_t ptr; 81 | union { 82 | sph_u32 Vs[4][4]; 83 | #if SPH_64 84 | sph_u64 Vb[4][2]; 85 | #endif 86 | } u; 87 | sph_u32 C0, C1, C2, C3; 88 | #endif 89 | } sph_echo_small_context; 90 | 91 | /** 92 | * This structure is a context for ECHO computations: it contains the 93 | * intermediate values and some data from the last entered block. Once 94 | * an ECHO computation has been performed, the context can be reused for 95 | * another computation. This specific structure is used for ECHO-384 96 | * and ECHO-512. 97 | * 98 | * The contents of this structure are private. A running ECHO computation 99 | * can be cloned by copying the context (e.g. with a simple 100 | * memcpy()). 101 | */ 102 | typedef struct { 103 | #ifndef DOXYGEN_IGNORE 104 | unsigned char buf[128]; /* first field, for alignment */ 105 | size_t ptr; 106 | union { 107 | sph_u32 Vs[8][4]; 108 | #if SPH_64 109 | sph_u64 Vb[8][2]; 110 | #endif 111 | } u; 112 | sph_u32 C0, C1, C2, C3; 113 | #endif 114 | } sph_echo_big_context; 115 | 116 | /** 117 | * Type for a ECHO-224 context (identical to the common "small" context). 118 | */ 119 | typedef sph_echo_small_context sph_echo224_context; 120 | 121 | /** 122 | * Type for a ECHO-256 context (identical to the common "small" context). 123 | */ 124 | typedef sph_echo_small_context sph_echo256_context; 125 | 126 | /** 127 | * Type for a ECHO-384 context (identical to the common "big" context). 128 | */ 129 | typedef sph_echo_big_context sph_echo384_context; 130 | 131 | /** 132 | * Type for a ECHO-512 context (identical to the common "big" context). 133 | */ 134 | typedef sph_echo_big_context sph_echo512_context; 135 | 136 | /** 137 | * Initialize an ECHO-224 context. This process performs no memory allocation. 138 | * 139 | * @param cc the ECHO-224 context (pointer to a 140 | * sph_echo224_context) 141 | */ 142 | void sph_echo224_init(void *cc); 143 | 144 | /** 145 | * Process some data bytes. It is acceptable that len is zero 146 | * (in which case this function does nothing). 147 | * 148 | * @param cc the ECHO-224 context 149 | * @param data the input data 150 | * @param len the input data length (in bytes) 151 | */ 152 | void sph_echo224(void *cc, const void *data, size_t len); 153 | 154 | /** 155 | * Terminate the current ECHO-224 computation and output the result into 156 | * the provided buffer. The destination buffer must be wide enough to 157 | * accomodate the result (28 bytes). The context is automatically 158 | * reinitialized. 159 | * 160 | * @param cc the ECHO-224 context 161 | * @param dst the destination buffer 162 | */ 163 | void sph_echo224_close(void *cc, void *dst); 164 | 165 | /** 166 | * Add a few additional bits (0 to 7) to the current computation, then 167 | * terminate it and output the result in the provided buffer, which must 168 | * be wide enough to accomodate the result (28 bytes). If bit number i 169 | * in ub has value 2^i, then the extra bits are those 170 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 171 | * level). The context is automatically reinitialized. 172 | * 173 | * @param cc the ECHO-224 context 174 | * @param ub the extra bits 175 | * @param n the number of extra bits (0 to 7) 176 | * @param dst the destination buffer 177 | */ 178 | void sph_echo224_addbits_and_close( 179 | void *cc, unsigned ub, unsigned n, void *dst); 180 | 181 | /** 182 | * Initialize an ECHO-256 context. This process performs no memory allocation. 183 | * 184 | * @param cc the ECHO-256 context (pointer to a 185 | * sph_echo256_context) 186 | */ 187 | void sph_echo256_init(void *cc); 188 | 189 | /** 190 | * Process some data bytes. It is acceptable that len is zero 191 | * (in which case this function does nothing). 192 | * 193 | * @param cc the ECHO-256 context 194 | * @param data the input data 195 | * @param len the input data length (in bytes) 196 | */ 197 | void sph_echo256(void *cc, const void *data, size_t len); 198 | 199 | /** 200 | * Terminate the current ECHO-256 computation and output the result into 201 | * the provided buffer. The destination buffer must be wide enough to 202 | * accomodate the result (32 bytes). The context is automatically 203 | * reinitialized. 204 | * 205 | * @param cc the ECHO-256 context 206 | * @param dst the destination buffer 207 | */ 208 | void sph_echo256_close(void *cc, void *dst); 209 | 210 | /** 211 | * Add a few additional bits (0 to 7) to the current computation, then 212 | * terminate it and output the result in the provided buffer, which must 213 | * be wide enough to accomodate the result (32 bytes). If bit number i 214 | * in ub has value 2^i, then the extra bits are those 215 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 216 | * level). The context is automatically reinitialized. 217 | * 218 | * @param cc the ECHO-256 context 219 | * @param ub the extra bits 220 | * @param n the number of extra bits (0 to 7) 221 | * @param dst the destination buffer 222 | */ 223 | void sph_echo256_addbits_and_close( 224 | void *cc, unsigned ub, unsigned n, void *dst); 225 | 226 | /** 227 | * Initialize an ECHO-384 context. This process performs no memory allocation. 228 | * 229 | * @param cc the ECHO-384 context (pointer to a 230 | * sph_echo384_context) 231 | */ 232 | void sph_echo384_init(void *cc); 233 | 234 | /** 235 | * Process some data bytes. It is acceptable that len is zero 236 | * (in which case this function does nothing). 237 | * 238 | * @param cc the ECHO-384 context 239 | * @param data the input data 240 | * @param len the input data length (in bytes) 241 | */ 242 | void sph_echo384(void *cc, const void *data, size_t len); 243 | 244 | /** 245 | * Terminate the current ECHO-384 computation and output the result into 246 | * the provided buffer. The destination buffer must be wide enough to 247 | * accomodate the result (48 bytes). The context is automatically 248 | * reinitialized. 249 | * 250 | * @param cc the ECHO-384 context 251 | * @param dst the destination buffer 252 | */ 253 | void sph_echo384_close(void *cc, void *dst); 254 | 255 | /** 256 | * Add a few additional bits (0 to 7) to the current computation, then 257 | * terminate it and output the result in the provided buffer, which must 258 | * be wide enough to accomodate the result (48 bytes). If bit number i 259 | * in ub has value 2^i, then the extra bits are those 260 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 261 | * level). The context is automatically reinitialized. 262 | * 263 | * @param cc the ECHO-384 context 264 | * @param ub the extra bits 265 | * @param n the number of extra bits (0 to 7) 266 | * @param dst the destination buffer 267 | */ 268 | void sph_echo384_addbits_and_close( 269 | void *cc, unsigned ub, unsigned n, void *dst); 270 | 271 | /** 272 | * Initialize an ECHO-512 context. This process performs no memory allocation. 273 | * 274 | * @param cc the ECHO-512 context (pointer to a 275 | * sph_echo512_context) 276 | */ 277 | void sph_echo512_init(void *cc); 278 | 279 | /** 280 | * Process some data bytes. It is acceptable that len is zero 281 | * (in which case this function does nothing). 282 | * 283 | * @param cc the ECHO-512 context 284 | * @param data the input data 285 | * @param len the input data length (in bytes) 286 | */ 287 | void sph_echo512(void *cc, const void *data, size_t len); 288 | 289 | /** 290 | * Terminate the current ECHO-512 computation and output the result into 291 | * the provided buffer. The destination buffer must be wide enough to 292 | * accomodate the result (64 bytes). The context is automatically 293 | * reinitialized. 294 | * 295 | * @param cc the ECHO-512 context 296 | * @param dst the destination buffer 297 | */ 298 | void sph_echo512_close(void *cc, void *dst); 299 | 300 | /** 301 | * Add a few additional bits (0 to 7) to the current computation, then 302 | * terminate it and output the result in the provided buffer, which must 303 | * be wide enough to accomodate the result (64 bytes). If bit number i 304 | * in ub has value 2^i, then the extra bits are those 305 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 306 | * level). The context is automatically reinitialized. 307 | * 308 | * @param cc the ECHO-512 context 309 | * @param ub the extra bits 310 | * @param n the number of extra bits (0 to 7) 311 | * @param dst the destination buffer 312 | */ 313 | void sph_echo512_addbits_and_close( 314 | void *cc, unsigned ub, unsigned n, void *dst); 315 | 316 | #ifdef __cplusplus 317 | } 318 | #endif 319 | 320 | #endif 321 | -------------------------------------------------------------------------------- /x17r/sph_fugue.h: -------------------------------------------------------------------------------- 1 | #ifndef SPH_FUGUE_H__ 2 | #define SPH_FUGUE_H__ 3 | 4 | #include 5 | #include "sph_types.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C"{ 9 | #endif 10 | 11 | #define SPH_SIZE_fugue224 224 12 | 13 | #define SPH_SIZE_fugue256 256 14 | 15 | #define SPH_SIZE_fugue384 384 16 | 17 | #define SPH_SIZE_fugue512 512 18 | 19 | typedef struct { 20 | #ifndef DOXYGEN_IGNORE 21 | sph_u32 partial; 22 | unsigned partial_len; 23 | unsigned round_shift; 24 | sph_u32 S[36]; 25 | #if SPH_64 26 | sph_u64 bit_count; 27 | #else 28 | sph_u32 bit_count_high, bit_count_low; 29 | #endif 30 | #endif 31 | } sph_fugue_context; 32 | 33 | typedef sph_fugue_context sph_fugue224_context; 34 | 35 | typedef sph_fugue_context sph_fugue256_context; 36 | 37 | typedef sph_fugue_context sph_fugue384_context; 38 | 39 | typedef sph_fugue_context sph_fugue512_context; 40 | 41 | void sph_fugue224_init(void *cc); 42 | 43 | void sph_fugue224(void *cc, const void *data, size_t len); 44 | 45 | void sph_fugue224_close(void *cc, void *dst); 46 | 47 | void sph_fugue224_addbits_and_close( 48 | void *cc, unsigned ub, unsigned n, void *dst); 49 | 50 | void sph_fugue256_init(void *cc); 51 | 52 | void sph_fugue256(void *cc, const void *data, size_t len); 53 | 54 | void sph_fugue256_close(void *cc, void *dst); 55 | 56 | void sph_fugue256_addbits_and_close( 57 | void *cc, unsigned ub, unsigned n, void *dst); 58 | 59 | void sph_fugue384_init(void *cc); 60 | 61 | void sph_fugue384(void *cc, const void *data, size_t len); 62 | 63 | void sph_fugue384_close(void *cc, void *dst); 64 | 65 | void sph_fugue384_addbits_and_close( 66 | void *cc, unsigned ub, unsigned n, void *dst); 67 | 68 | void sph_fugue512_init(void *cc); 69 | 70 | void sph_fugue512(void *cc, const void *data, size_t len); 71 | 72 | void sph_fugue512_close(void *cc, void *dst); 73 | 74 | void sph_fugue512_addbits_and_close( 75 | void *cc, unsigned ub, unsigned n, void *dst); 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif -------------------------------------------------------------------------------- /x17r/sph_hamsi.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_hamsi.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * Hamsi interface. This code implements Hamsi with the recommended 4 | * parameters for SHA-3, with outputs of 224, 256, 384 and 512 bits. 5 | * 6 | * ==========================(LICENSE BEGIN)============================ 7 | * 8 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining 11 | * a copy of this software and associated documentation files (the 12 | * "Software"), to deal in the Software without restriction, including 13 | * without limitation the rights to use, copy, modify, merge, publish, 14 | * distribute, sublicense, and/or sell copies of the Software, and to 15 | * permit persons to whom the Software is furnished to do so, subject to 16 | * the following conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 24 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 25 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 26 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 27 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | * 29 | * ===========================(LICENSE END)============================= 30 | * 31 | * @file sph_hamsi.h 32 | * @author Thomas Pornin 33 | */ 34 | 35 | #ifndef SPH_HAMSI_H__ 36 | #define SPH_HAMSI_H__ 37 | 38 | #include 39 | #include "sph_types.h" 40 | 41 | #ifdef __cplusplus 42 | extern "C"{ 43 | #endif 44 | 45 | /** 46 | * Output size (in bits) for Hamsi-224. 47 | */ 48 | #define SPH_SIZE_hamsi224 224 49 | 50 | /** 51 | * Output size (in bits) for Hamsi-256. 52 | */ 53 | #define SPH_SIZE_hamsi256 256 54 | 55 | /** 56 | * Output size (in bits) for Hamsi-384. 57 | */ 58 | #define SPH_SIZE_hamsi384 384 59 | 60 | /** 61 | * Output size (in bits) for Hamsi-512. 62 | */ 63 | #define SPH_SIZE_hamsi512 512 64 | 65 | /** 66 | * This structure is a context for Hamsi-224 and Hamsi-256 computations: 67 | * it contains the intermediate values and some data from the last 68 | * entered block. Once a Hamsi computation has been performed, the 69 | * context can be reused for another computation. 70 | * 71 | * The contents of this structure are private. A running Hamsi 72 | * computation can be cloned by copying the context (e.g. with a simple 73 | * memcpy()). 74 | */ 75 | typedef struct { 76 | #ifndef DOXYGEN_IGNORE 77 | unsigned char partial[4]; 78 | size_t partial_len; 79 | sph_u32 h[8]; 80 | #if SPH_64 81 | sph_u64 count; 82 | #else 83 | sph_u32 count_high, count_low; 84 | #endif 85 | #endif 86 | } sph_hamsi_small_context; 87 | 88 | /** 89 | * This structure is a context for Hamsi-224 computations. It is 90 | * identical to the common sph_hamsi_small_context. 91 | */ 92 | typedef sph_hamsi_small_context sph_hamsi224_context; 93 | 94 | /** 95 | * This structure is a context for Hamsi-256 computations. It is 96 | * identical to the common sph_hamsi_small_context. 97 | */ 98 | typedef sph_hamsi_small_context sph_hamsi256_context; 99 | 100 | /** 101 | * This structure is a context for Hamsi-384 and Hamsi-512 computations: 102 | * it contains the intermediate values and some data from the last 103 | * entered block. Once a Hamsi computation has been performed, the 104 | * context can be reused for another computation. 105 | * 106 | * The contents of this structure are private. A running Hamsi 107 | * computation can be cloned by copying the context (e.g. with a simple 108 | * memcpy()). 109 | */ 110 | typedef struct { 111 | #ifndef DOXYGEN_IGNORE 112 | unsigned char partial[8]; 113 | size_t partial_len; 114 | sph_u32 h[16]; 115 | #if SPH_64 116 | sph_u64 count; 117 | #else 118 | sph_u32 count_high, count_low; 119 | #endif 120 | #endif 121 | } sph_hamsi_big_context; 122 | 123 | /** 124 | * This structure is a context for Hamsi-384 computations. It is 125 | * identical to the common sph_hamsi_small_context. 126 | */ 127 | typedef sph_hamsi_big_context sph_hamsi384_context; 128 | 129 | /** 130 | * This structure is a context for Hamsi-512 computations. It is 131 | * identical to the common sph_hamsi_small_context. 132 | */ 133 | typedef sph_hamsi_big_context sph_hamsi512_context; 134 | 135 | /** 136 | * Initialize a Hamsi-224 context. This process performs no memory allocation. 137 | * 138 | * @param cc the Hamsi-224 context (pointer to a 139 | * sph_hamsi224_context) 140 | */ 141 | void sph_hamsi224_init(void *cc); 142 | 143 | /** 144 | * Process some data bytes. It is acceptable that len is zero 145 | * (in which case this function does nothing). 146 | * 147 | * @param cc the Hamsi-224 context 148 | * @param data the input data 149 | * @param len the input data length (in bytes) 150 | */ 151 | void sph_hamsi224(void *cc, const void *data, size_t len); 152 | 153 | /** 154 | * Terminate the current Hamsi-224 computation and output the result into 155 | * the provided buffer. The destination buffer must be wide enough to 156 | * accomodate the result (28 bytes). The context is automatically 157 | * reinitialized. 158 | * 159 | * @param cc the Hamsi-224 context 160 | * @param dst the destination buffer 161 | */ 162 | void sph_hamsi224_close(void *cc, void *dst); 163 | 164 | /** 165 | * Add a few additional bits (0 to 7) to the current computation, then 166 | * terminate it and output the result in the provided buffer, which must 167 | * be wide enough to accomodate the result (28 bytes). If bit number i 168 | * in ub has value 2^i, then the extra bits are those 169 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 170 | * level). The context is automatically reinitialized. 171 | * 172 | * @param cc the Hamsi-224 context 173 | * @param ub the extra bits 174 | * @param n the number of extra bits (0 to 7) 175 | * @param dst the destination buffer 176 | */ 177 | void sph_hamsi224_addbits_and_close( 178 | void *cc, unsigned ub, unsigned n, void *dst); 179 | 180 | /** 181 | * Initialize a Hamsi-256 context. This process performs no memory allocation. 182 | * 183 | * @param cc the Hamsi-256 context (pointer to a 184 | * sph_hamsi256_context) 185 | */ 186 | void sph_hamsi256_init(void *cc); 187 | 188 | /** 189 | * Process some data bytes. It is acceptable that len is zero 190 | * (in which case this function does nothing). 191 | * 192 | * @param cc the Hamsi-256 context 193 | * @param data the input data 194 | * @param len the input data length (in bytes) 195 | */ 196 | void sph_hamsi256(void *cc, const void *data, size_t len); 197 | 198 | /** 199 | * Terminate the current Hamsi-256 computation and output the result into 200 | * the provided buffer. The destination buffer must be wide enough to 201 | * accomodate the result (32 bytes). The context is automatically 202 | * reinitialized. 203 | * 204 | * @param cc the Hamsi-256 context 205 | * @param dst the destination buffer 206 | */ 207 | void sph_hamsi256_close(void *cc, void *dst); 208 | 209 | /** 210 | * Add a few additional bits (0 to 7) to the current computation, then 211 | * terminate it and output the result in the provided buffer, which must 212 | * be wide enough to accomodate the result (32 bytes). If bit number i 213 | * in ub has value 2^i, then the extra bits are those 214 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 215 | * level). The context is automatically reinitialized. 216 | * 217 | * @param cc the Hamsi-256 context 218 | * @param ub the extra bits 219 | * @param n the number of extra bits (0 to 7) 220 | * @param dst the destination buffer 221 | */ 222 | void sph_hamsi256_addbits_and_close( 223 | void *cc, unsigned ub, unsigned n, void *dst); 224 | 225 | /** 226 | * Initialize a Hamsi-384 context. This process performs no memory allocation. 227 | * 228 | * @param cc the Hamsi-384 context (pointer to a 229 | * sph_hamsi384_context) 230 | */ 231 | void sph_hamsi384_init(void *cc); 232 | 233 | /** 234 | * Process some data bytes. It is acceptable that len is zero 235 | * (in which case this function does nothing). 236 | * 237 | * @param cc the Hamsi-384 context 238 | * @param data the input data 239 | * @param len the input data length (in bytes) 240 | */ 241 | void sph_hamsi384(void *cc, const void *data, size_t len); 242 | 243 | /** 244 | * Terminate the current Hamsi-384 computation and output the result into 245 | * the provided buffer. The destination buffer must be wide enough to 246 | * accomodate the result (48 bytes). The context is automatically 247 | * reinitialized. 248 | * 249 | * @param cc the Hamsi-384 context 250 | * @param dst the destination buffer 251 | */ 252 | void sph_hamsi384_close(void *cc, void *dst); 253 | 254 | /** 255 | * Add a few additional bits (0 to 7) to the current computation, then 256 | * terminate it and output the result in the provided buffer, which must 257 | * be wide enough to accomodate the result (48 bytes). If bit number i 258 | * in ub has value 2^i, then the extra bits are those 259 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 260 | * level). The context is automatically reinitialized. 261 | * 262 | * @param cc the Hamsi-384 context 263 | * @param ub the extra bits 264 | * @param n the number of extra bits (0 to 7) 265 | * @param dst the destination buffer 266 | */ 267 | void sph_hamsi384_addbits_and_close( 268 | void *cc, unsigned ub, unsigned n, void *dst); 269 | 270 | /** 271 | * Initialize a Hamsi-512 context. This process performs no memory allocation. 272 | * 273 | * @param cc the Hamsi-512 context (pointer to a 274 | * sph_hamsi512_context) 275 | */ 276 | void sph_hamsi512_init(void *cc); 277 | 278 | /** 279 | * Process some data bytes. It is acceptable that len is zero 280 | * (in which case this function does nothing). 281 | * 282 | * @param cc the Hamsi-512 context 283 | * @param data the input data 284 | * @param len the input data length (in bytes) 285 | */ 286 | void sph_hamsi512(void *cc, const void *data, size_t len); 287 | 288 | /** 289 | * Terminate the current Hamsi-512 computation and output the result into 290 | * the provided buffer. The destination buffer must be wide enough to 291 | * accomodate the result (64 bytes). The context is automatically 292 | * reinitialized. 293 | * 294 | * @param cc the Hamsi-512 context 295 | * @param dst the destination buffer 296 | */ 297 | void sph_hamsi512_close(void *cc, void *dst); 298 | 299 | /** 300 | * Add a few additional bits (0 to 7) to the current computation, then 301 | * terminate it and output the result in the provided buffer, which must 302 | * be wide enough to accomodate the result (64 bytes). If bit number i 303 | * in ub has value 2^i, then the extra bits are those 304 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 305 | * level). The context is automatically reinitialized. 306 | * 307 | * @param cc the Hamsi-512 context 308 | * @param ub the extra bits 309 | * @param n the number of extra bits (0 to 7) 310 | * @param dst the destination buffer 311 | */ 312 | void sph_hamsi512_addbits_and_close( 313 | void *cc, unsigned ub, unsigned n, void *dst); 314 | 315 | 316 | 317 | #ifdef __cplusplus 318 | } 319 | #endif 320 | 321 | #endif -------------------------------------------------------------------------------- /x17r/sph_hefty1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * HEFTY1 cryptographic hash function 3 | * 4 | * Copyright (c) 2014, dbcc14 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, this 11 | * list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * The views and conclusions contained in the software and documentation are those 28 | * of the authors and should not be interpreted as representing official policies, 29 | * either expressed or implied, of the FreeBSD Project. 30 | */ 31 | 32 | #ifndef __HEFTY1_H__ 33 | #define __HEFTY1_H__ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #ifndef WIN32 40 | #include 41 | #endif 42 | 43 | #include 44 | 45 | #define HEFTY1_DIGEST_BYTES 32 46 | #define HEFTY1_BLOCK_BYTES 64 47 | #define HEFTY1_STATE_WORDS 8 48 | #define HEFTY1_SPONGE_WORDS 4 49 | 50 | typedef struct HEFTY1_CTX { 51 | uint32_t h[HEFTY1_STATE_WORDS]; 52 | uint8_t block[HEFTY1_BLOCK_BYTES]; 53 | uint64_t written; 54 | uint32_t sponge[HEFTY1_SPONGE_WORDS]; 55 | } HEFTY1_CTX; 56 | 57 | void HEFTY1_Init(HEFTY1_CTX *cxt); 58 | void HEFTY1_Update(HEFTY1_CTX *cxt, const void *data, size_t len); 59 | void HEFTY1_Final(unsigned char *digest, HEFTY1_CTX *cxt); 60 | unsigned char* HEFTY1(const unsigned char *data, size_t len, unsigned char *digest); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* __HEFTY1_H__ */ -------------------------------------------------------------------------------- /x17r/sph_jh.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_jh.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * JH interface. JH is a family of functions which differ by 4 | * their output size; this implementation defines JH for output 5 | * sizes 224, 256, 384 and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_jh.h 33 | * @author Thomas Pornin 34 | */ 35 | 36 | #ifndef SPH_JH_H__ 37 | #define SPH_JH_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for JH-224. 48 | */ 49 | #define SPH_SIZE_jh224 224 50 | 51 | /** 52 | * Output size (in bits) for JH-256. 53 | */ 54 | #define SPH_SIZE_jh256 256 55 | 56 | /** 57 | * Output size (in bits) for JH-384. 58 | */ 59 | #define SPH_SIZE_jh384 384 60 | 61 | /** 62 | * Output size (in bits) for JH-512. 63 | */ 64 | #define SPH_SIZE_jh512 512 65 | 66 | /** 67 | * This structure is a context for JH computations: it contains the 68 | * intermediate values and some data from the last entered block. Once 69 | * a JH computation has been performed, the context can be reused for 70 | * another computation. 71 | * 72 | * The contents of this structure are private. A running JH computation 73 | * can be cloned by copying the context (e.g. with a simple 74 | * memcpy()). 75 | */ 76 | typedef struct { 77 | #ifndef DOXYGEN_IGNORE 78 | unsigned char buf[64]; /* first field, for alignment */ 79 | size_t ptr; 80 | union { 81 | #if SPH_64 82 | sph_u64 wide[16]; 83 | #endif 84 | sph_u32 narrow[32]; 85 | } H; 86 | #if SPH_64 87 | sph_u64 block_count; 88 | #else 89 | sph_u32 block_count_high, block_count_low; 90 | #endif 91 | #endif 92 | } sph_jh_context; 93 | 94 | /** 95 | * Type for a JH-224 context (identical to the common context). 96 | */ 97 | typedef sph_jh_context sph_jh224_context; 98 | 99 | /** 100 | * Type for a JH-256 context (identical to the common context). 101 | */ 102 | typedef sph_jh_context sph_jh256_context; 103 | 104 | /** 105 | * Type for a JH-384 context (identical to the common context). 106 | */ 107 | typedef sph_jh_context sph_jh384_context; 108 | 109 | /** 110 | * Type for a JH-512 context (identical to the common context). 111 | */ 112 | typedef sph_jh_context sph_jh512_context; 113 | 114 | /** 115 | * Initialize a JH-224 context. This process performs no memory allocation. 116 | * 117 | * @param cc the JH-224 context (pointer to a 118 | * sph_jh224_context) 119 | */ 120 | void sph_jh224_init(void *cc); 121 | 122 | /** 123 | * Process some data bytes. It is acceptable that len is zero 124 | * (in which case this function does nothing). 125 | * 126 | * @param cc the JH-224 context 127 | * @param data the input data 128 | * @param len the input data length (in bytes) 129 | */ 130 | void sph_jh224(void *cc, const void *data, size_t len); 131 | 132 | /** 133 | * Terminate the current JH-224 computation and output the result into 134 | * the provided buffer. The destination buffer must be wide enough to 135 | * accomodate the result (28 bytes). The context is automatically 136 | * reinitialized. 137 | * 138 | * @param cc the JH-224 context 139 | * @param dst the destination buffer 140 | */ 141 | void sph_jh224_close(void *cc, void *dst); 142 | 143 | /** 144 | * Add a few additional bits (0 to 7) to the current computation, then 145 | * terminate it and output the result in the provided buffer, which must 146 | * be wide enough to accomodate the result (28 bytes). If bit number i 147 | * in ub has value 2^i, then the extra bits are those 148 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 149 | * level). The context is automatically reinitialized. 150 | * 151 | * @param cc the JH-224 context 152 | * @param ub the extra bits 153 | * @param n the number of extra bits (0 to 7) 154 | * @param dst the destination buffer 155 | */ 156 | void sph_jh224_addbits_and_close( 157 | void *cc, unsigned ub, unsigned n, void *dst); 158 | 159 | /** 160 | * Initialize a JH-256 context. This process performs no memory allocation. 161 | * 162 | * @param cc the JH-256 context (pointer to a 163 | * sph_jh256_context) 164 | */ 165 | void sph_jh256_init(void *cc); 166 | 167 | /** 168 | * Process some data bytes. It is acceptable that len is zero 169 | * (in which case this function does nothing). 170 | * 171 | * @param cc the JH-256 context 172 | * @param data the input data 173 | * @param len the input data length (in bytes) 174 | */ 175 | void sph_jh256(void *cc, const void *data, size_t len); 176 | 177 | /** 178 | * Terminate the current JH-256 computation and output the result into 179 | * the provided buffer. The destination buffer must be wide enough to 180 | * accomodate the result (32 bytes). The context is automatically 181 | * reinitialized. 182 | * 183 | * @param cc the JH-256 context 184 | * @param dst the destination buffer 185 | */ 186 | void sph_jh256_close(void *cc, void *dst); 187 | 188 | /** 189 | * Add a few additional bits (0 to 7) to the current computation, then 190 | * terminate it and output the result in the provided buffer, which must 191 | * be wide enough to accomodate the result (32 bytes). If bit number i 192 | * in ub has value 2^i, then the extra bits are those 193 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 194 | * level). The context is automatically reinitialized. 195 | * 196 | * @param cc the JH-256 context 197 | * @param ub the extra bits 198 | * @param n the number of extra bits (0 to 7) 199 | * @param dst the destination buffer 200 | */ 201 | void sph_jh256_addbits_and_close( 202 | void *cc, unsigned ub, unsigned n, void *dst); 203 | 204 | /** 205 | * Initialize a JH-384 context. This process performs no memory allocation. 206 | * 207 | * @param cc the JH-384 context (pointer to a 208 | * sph_jh384_context) 209 | */ 210 | void sph_jh384_init(void *cc); 211 | 212 | /** 213 | * Process some data bytes. It is acceptable that len is zero 214 | * (in which case this function does nothing). 215 | * 216 | * @param cc the JH-384 context 217 | * @param data the input data 218 | * @param len the input data length (in bytes) 219 | */ 220 | void sph_jh384(void *cc, const void *data, size_t len); 221 | 222 | /** 223 | * Terminate the current JH-384 computation and output the result into 224 | * the provided buffer. The destination buffer must be wide enough to 225 | * accomodate the result (48 bytes). The context is automatically 226 | * reinitialized. 227 | * 228 | * @param cc the JH-384 context 229 | * @param dst the destination buffer 230 | */ 231 | void sph_jh384_close(void *cc, void *dst); 232 | 233 | /** 234 | * Add a few additional bits (0 to 7) to the current computation, then 235 | * terminate it and output the result in the provided buffer, which must 236 | * be wide enough to accomodate the result (48 bytes). If bit number i 237 | * in ub has value 2^i, then the extra bits are those 238 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 239 | * level). The context is automatically reinitialized. 240 | * 241 | * @param cc the JH-384 context 242 | * @param ub the extra bits 243 | * @param n the number of extra bits (0 to 7) 244 | * @param dst the destination buffer 245 | */ 246 | void sph_jh384_addbits_and_close( 247 | void *cc, unsigned ub, unsigned n, void *dst); 248 | 249 | /** 250 | * Initialize a JH-512 context. This process performs no memory allocation. 251 | * 252 | * @param cc the JH-512 context (pointer to a 253 | * sph_jh512_context) 254 | */ 255 | void sph_jh512_init(void *cc); 256 | 257 | /** 258 | * Process some data bytes. It is acceptable that len is zero 259 | * (in which case this function does nothing). 260 | * 261 | * @param cc the JH-512 context 262 | * @param data the input data 263 | * @param len the input data length (in bytes) 264 | */ 265 | void sph_jh512(void *cc, const void *data, size_t len); 266 | 267 | /** 268 | * Terminate the current JH-512 computation and output the result into 269 | * the provided buffer. The destination buffer must be wide enough to 270 | * accomodate the result (64 bytes). The context is automatically 271 | * reinitialized. 272 | * 273 | * @param cc the JH-512 context 274 | * @param dst the destination buffer 275 | */ 276 | void sph_jh512_close(void *cc, void *dst); 277 | 278 | /** 279 | * Add a few additional bits (0 to 7) to the current computation, then 280 | * terminate it and output the result in the provided buffer, which must 281 | * be wide enough to accomodate the result (64 bytes). If bit number i 282 | * in ub has value 2^i, then the extra bits are those 283 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 284 | * level). The context is automatically reinitialized. 285 | * 286 | * @param cc the JH-512 context 287 | * @param ub the extra bits 288 | * @param n the number of extra bits (0 to 7) 289 | * @param dst the destination buffer 290 | */ 291 | void sph_jh512_addbits_and_close( 292 | void *cc, unsigned ub, unsigned n, void *dst); 293 | 294 | #ifdef __cplusplus 295 | } 296 | #endif 297 | 298 | #endif 299 | -------------------------------------------------------------------------------- /x17r/sph_keccak.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_keccak.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * Keccak interface. This is the interface for Keccak with the 4 | * recommended parameters for SHA-3, with output lengths 224, 256, 5 | * 384 and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_keccak.h 33 | * @author Thomas Pornin 34 | */ 35 | 36 | #ifndef SPH_KECCAK_H__ 37 | #define SPH_KECCAK_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for Keccak-224. 48 | */ 49 | #define SPH_SIZE_keccak224 224 50 | 51 | /** 52 | * Output size (in bits) for Keccak-256. 53 | */ 54 | #define SPH_SIZE_keccak256 256 55 | 56 | /** 57 | * Output size (in bits) for Keccak-384. 58 | */ 59 | #define SPH_SIZE_keccak384 384 60 | 61 | /** 62 | * Output size (in bits) for Keccak-512. 63 | */ 64 | #define SPH_SIZE_keccak512 512 65 | 66 | /** 67 | * This structure is a context for Keccak computations: it contains the 68 | * intermediate values and some data from the last entered block. Once a 69 | * Keccak computation has been performed, the context can be reused for 70 | * another computation. 71 | * 72 | * The contents of this structure are private. A running Keccak computation 73 | * can be cloned by copying the context (e.g. with a simple 74 | * memcpy()). 75 | */ 76 | typedef struct { 77 | #ifndef DOXYGEN_IGNORE 78 | unsigned char buf[144]; /* first field, for alignment */ 79 | size_t ptr, lim; 80 | union { 81 | #if SPH_64 82 | sph_u64 wide[25]; 83 | #endif 84 | sph_u32 narrow[50]; 85 | } u; 86 | #endif 87 | } sph_keccak_context; 88 | 89 | /** 90 | * Type for a Keccak-224 context (identical to the common context). 91 | */ 92 | typedef sph_keccak_context sph_keccak224_context; 93 | 94 | /** 95 | * Type for a Keccak-256 context (identical to the common context). 96 | */ 97 | typedef sph_keccak_context sph_keccak256_context; 98 | 99 | /** 100 | * Type for a Keccak-384 context (identical to the common context). 101 | */ 102 | typedef sph_keccak_context sph_keccak384_context; 103 | 104 | /** 105 | * Type for a Keccak-512 context (identical to the common context). 106 | */ 107 | typedef sph_keccak_context sph_keccak512_context; 108 | 109 | /** 110 | * Initialize a Keccak-224 context. This process performs no memory allocation. 111 | * 112 | * @param cc the Keccak-224 context (pointer to a 113 | * sph_keccak224_context) 114 | */ 115 | void sph_keccak224_init(void *cc); 116 | 117 | /** 118 | * Process some data bytes. It is acceptable that len is zero 119 | * (in which case this function does nothing). 120 | * 121 | * @param cc the Keccak-224 context 122 | * @param data the input data 123 | * @param len the input data length (in bytes) 124 | */ 125 | void sph_keccak224(void *cc, const void *data, size_t len); 126 | 127 | /** 128 | * Terminate the current Keccak-224 computation and output the result into 129 | * the provided buffer. The destination buffer must be wide enough to 130 | * accomodate the result (28 bytes). The context is automatically 131 | * reinitialized. 132 | * 133 | * @param cc the Keccak-224 context 134 | * @param dst the destination buffer 135 | */ 136 | void sph_keccak224_close(void *cc, void *dst); 137 | 138 | /** 139 | * Add a few additional bits (0 to 7) to the current computation, then 140 | * terminate it and output the result in the provided buffer, which must 141 | * be wide enough to accomodate the result (28 bytes). If bit number i 142 | * in ub has value 2^i, then the extra bits are those 143 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 144 | * level). The context is automatically reinitialized. 145 | * 146 | * @param cc the Keccak-224 context 147 | * @param ub the extra bits 148 | * @param n the number of extra bits (0 to 7) 149 | * @param dst the destination buffer 150 | */ 151 | void sph_keccak224_addbits_and_close( 152 | void *cc, unsigned ub, unsigned n, void *dst); 153 | 154 | /** 155 | * Initialize a Keccak-256 context. This process performs no memory allocation. 156 | * 157 | * @param cc the Keccak-256 context (pointer to a 158 | * sph_keccak256_context) 159 | */ 160 | void sph_keccak256_init(void *cc); 161 | 162 | /** 163 | * Process some data bytes. It is acceptable that len is zero 164 | * (in which case this function does nothing). 165 | * 166 | * @param cc the Keccak-256 context 167 | * @param data the input data 168 | * @param len the input data length (in bytes) 169 | */ 170 | void sph_keccak256(void *cc, const void *data, size_t len); 171 | 172 | /** 173 | * Terminate the current Keccak-256 computation and output the result into 174 | * the provided buffer. The destination buffer must be wide enough to 175 | * accomodate the result (32 bytes). The context is automatically 176 | * reinitialized. 177 | * 178 | * @param cc the Keccak-256 context 179 | * @param dst the destination buffer 180 | */ 181 | void sph_keccak256_close(void *cc, void *dst); 182 | 183 | /** 184 | * Add a few additional bits (0 to 7) to the current computation, then 185 | * terminate it and output the result in the provided buffer, which must 186 | * be wide enough to accomodate the result (32 bytes). If bit number i 187 | * in ub has value 2^i, then the extra bits are those 188 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 189 | * level). The context is automatically reinitialized. 190 | * 191 | * @param cc the Keccak-256 context 192 | * @param ub the extra bits 193 | * @param n the number of extra bits (0 to 7) 194 | * @param dst the destination buffer 195 | */ 196 | void sph_keccak256_addbits_and_close( 197 | void *cc, unsigned ub, unsigned n, void *dst); 198 | 199 | /** 200 | * Initialize a Keccak-384 context. This process performs no memory allocation. 201 | * 202 | * @param cc the Keccak-384 context (pointer to a 203 | * sph_keccak384_context) 204 | */ 205 | void sph_keccak384_init(void *cc); 206 | 207 | /** 208 | * Process some data bytes. It is acceptable that len is zero 209 | * (in which case this function does nothing). 210 | * 211 | * @param cc the Keccak-384 context 212 | * @param data the input data 213 | * @param len the input data length (in bytes) 214 | */ 215 | void sph_keccak384(void *cc, const void *data, size_t len); 216 | 217 | /** 218 | * Terminate the current Keccak-384 computation and output the result into 219 | * the provided buffer. The destination buffer must be wide enough to 220 | * accomodate the result (48 bytes). The context is automatically 221 | * reinitialized. 222 | * 223 | * @param cc the Keccak-384 context 224 | * @param dst the destination buffer 225 | */ 226 | void sph_keccak384_close(void *cc, void *dst); 227 | 228 | /** 229 | * Add a few additional bits (0 to 7) to the current computation, then 230 | * terminate it and output the result in the provided buffer, which must 231 | * be wide enough to accomodate the result (48 bytes). If bit number i 232 | * in ub has value 2^i, then the extra bits are those 233 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 234 | * level). The context is automatically reinitialized. 235 | * 236 | * @param cc the Keccak-384 context 237 | * @param ub the extra bits 238 | * @param n the number of extra bits (0 to 7) 239 | * @param dst the destination buffer 240 | */ 241 | void sph_keccak384_addbits_and_close( 242 | void *cc, unsigned ub, unsigned n, void *dst); 243 | 244 | /** 245 | * Initialize a Keccak-512 context. This process performs no memory allocation. 246 | * 247 | * @param cc the Keccak-512 context (pointer to a 248 | * sph_keccak512_context) 249 | */ 250 | void sph_keccak512_init(void *cc); 251 | 252 | /** 253 | * Process some data bytes. It is acceptable that len is zero 254 | * (in which case this function does nothing). 255 | * 256 | * @param cc the Keccak-512 context 257 | * @param data the input data 258 | * @param len the input data length (in bytes) 259 | */ 260 | void sph_keccak512(void *cc, const void *data, size_t len); 261 | 262 | /** 263 | * Terminate the current Keccak-512 computation and output the result into 264 | * the provided buffer. The destination buffer must be wide enough to 265 | * accomodate the result (64 bytes). The context is automatically 266 | * reinitialized. 267 | * 268 | * @param cc the Keccak-512 context 269 | * @param dst the destination buffer 270 | */ 271 | void sph_keccak512_close(void *cc, void *dst); 272 | 273 | /** 274 | * Add a few additional bits (0 to 7) to the current computation, then 275 | * terminate it and output the result in the provided buffer, which must 276 | * be wide enough to accomodate the result (64 bytes). If bit number i 277 | * in ub has value 2^i, then the extra bits are those 278 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 279 | * level). The context is automatically reinitialized. 280 | * 281 | * @param cc the Keccak-512 context 282 | * @param ub the extra bits 283 | * @param n the number of extra bits (0 to 7) 284 | * @param dst the destination buffer 285 | */ 286 | void sph_keccak512_addbits_and_close( 287 | void *cc, unsigned ub, unsigned n, void *dst); 288 | 289 | #ifdef __cplusplus 290 | } 291 | #endif 292 | 293 | #endif 294 | -------------------------------------------------------------------------------- /x17r/sph_luffa.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_luffa.h 154 2010-04-26 17:00:24Z tp $ */ 2 | /** 3 | * Luffa interface. Luffa is a family of functions which differ by 4 | * their output size; this implementation defines Luffa for output 5 | * sizes 224, 256, 384 and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_luffa.h 33 | * @author Thomas Pornin 34 | */ 35 | 36 | #ifndef SPH_LUFFA_H__ 37 | #define SPH_LUFFA_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for Luffa-224. 48 | */ 49 | #define SPH_SIZE_luffa224 224 50 | 51 | /** 52 | * Output size (in bits) for Luffa-256. 53 | */ 54 | #define SPH_SIZE_luffa256 256 55 | 56 | /** 57 | * Output size (in bits) for Luffa-384. 58 | */ 59 | #define SPH_SIZE_luffa384 384 60 | 61 | /** 62 | * Output size (in bits) for Luffa-512. 63 | */ 64 | #define SPH_SIZE_luffa512 512 65 | 66 | /** 67 | * This structure is a context for Luffa-224 computations: it contains 68 | * the intermediate values and some data from the last entered block. 69 | * Once a Luffa computation has been performed, the context can be 70 | * reused for another computation. 71 | * 72 | * The contents of this structure are private. A running Luffa 73 | * computation can be cloned by copying the context (e.g. with a simple 74 | * memcpy()). 75 | */ 76 | typedef struct { 77 | #ifndef DOXYGEN_IGNORE 78 | unsigned char buf[32]; /* first field, for alignment */ 79 | size_t ptr; 80 | sph_u32 V[3][8]; 81 | #endif 82 | } sph_luffa224_context; 83 | 84 | /** 85 | * This structure is a context for Luffa-256 computations. It is 86 | * identical to sph_luffa224_context. 87 | */ 88 | typedef sph_luffa224_context sph_luffa256_context; 89 | 90 | /** 91 | * This structure is a context for Luffa-384 computations. 92 | */ 93 | typedef struct { 94 | #ifndef DOXYGEN_IGNORE 95 | unsigned char buf[32]; /* first field, for alignment */ 96 | size_t ptr; 97 | sph_u32 V[4][8]; 98 | #endif 99 | } sph_luffa384_context; 100 | 101 | /** 102 | * This structure is a context for Luffa-512 computations. 103 | */ 104 | typedef struct { 105 | #ifndef DOXYGEN_IGNORE 106 | unsigned char buf[32]; /* first field, for alignment */ 107 | size_t ptr; 108 | sph_u32 V[5][8]; 109 | #endif 110 | } sph_luffa512_context; 111 | 112 | /** 113 | * Initialize a Luffa-224 context. This process performs no memory allocation. 114 | * 115 | * @param cc the Luffa-224 context (pointer to a 116 | * sph_luffa224_context) 117 | */ 118 | void sph_luffa224_init(void *cc); 119 | 120 | /** 121 | * Process some data bytes. It is acceptable that len is zero 122 | * (in which case this function does nothing). 123 | * 124 | * @param cc the Luffa-224 context 125 | * @param data the input data 126 | * @param len the input data length (in bytes) 127 | */ 128 | void sph_luffa224(void *cc, const void *data, size_t len); 129 | 130 | /** 131 | * Terminate the current Luffa-224 computation and output the result into 132 | * the provided buffer. The destination buffer must be wide enough to 133 | * accomodate the result (28 bytes). The context is automatically 134 | * reinitialized. 135 | * 136 | * @param cc the Luffa-224 context 137 | * @param dst the destination buffer 138 | */ 139 | void sph_luffa224_close(void *cc, void *dst); 140 | 141 | /** 142 | * Add a few additional bits (0 to 7) to the current computation, then 143 | * terminate it and output the result in the provided buffer, which must 144 | * be wide enough to accomodate the result (28 bytes). If bit number i 145 | * in ub has value 2^i, then the extra bits are those 146 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 147 | * level). The context is automatically reinitialized. 148 | * 149 | * @param cc the Luffa-224 context 150 | * @param ub the extra bits 151 | * @param n the number of extra bits (0 to 7) 152 | * @param dst the destination buffer 153 | */ 154 | void sph_luffa224_addbits_and_close( 155 | void *cc, unsigned ub, unsigned n, void *dst); 156 | 157 | /** 158 | * Initialize a Luffa-256 context. This process performs no memory allocation. 159 | * 160 | * @param cc the Luffa-256 context (pointer to a 161 | * sph_luffa256_context) 162 | */ 163 | void sph_luffa256_init(void *cc); 164 | 165 | /** 166 | * Process some data bytes. It is acceptable that len is zero 167 | * (in which case this function does nothing). 168 | * 169 | * @param cc the Luffa-256 context 170 | * @param data the input data 171 | * @param len the input data length (in bytes) 172 | */ 173 | void sph_luffa256(void *cc, const void *data, size_t len); 174 | 175 | /** 176 | * Terminate the current Luffa-256 computation and output the result into 177 | * the provided buffer. The destination buffer must be wide enough to 178 | * accomodate the result (32 bytes). The context is automatically 179 | * reinitialized. 180 | * 181 | * @param cc the Luffa-256 context 182 | * @param dst the destination buffer 183 | */ 184 | void sph_luffa256_close(void *cc, void *dst); 185 | 186 | /** 187 | * Add a few additional bits (0 to 7) to the current computation, then 188 | * terminate it and output the result in the provided buffer, which must 189 | * be wide enough to accomodate the result (32 bytes). If bit number i 190 | * in ub has value 2^i, then the extra bits are those 191 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 192 | * level). The context is automatically reinitialized. 193 | * 194 | * @param cc the Luffa-256 context 195 | * @param ub the extra bits 196 | * @param n the number of extra bits (0 to 7) 197 | * @param dst the destination buffer 198 | */ 199 | void sph_luffa256_addbits_and_close( 200 | void *cc, unsigned ub, unsigned n, void *dst); 201 | 202 | /** 203 | * Initialize a Luffa-384 context. This process performs no memory allocation. 204 | * 205 | * @param cc the Luffa-384 context (pointer to a 206 | * sph_luffa384_context) 207 | */ 208 | void sph_luffa384_init(void *cc); 209 | 210 | /** 211 | * Process some data bytes. It is acceptable that len is zero 212 | * (in which case this function does nothing). 213 | * 214 | * @param cc the Luffa-384 context 215 | * @param data the input data 216 | * @param len the input data length (in bytes) 217 | */ 218 | void sph_luffa384(void *cc, const void *data, size_t len); 219 | 220 | /** 221 | * Terminate the current Luffa-384 computation and output the result into 222 | * the provided buffer. The destination buffer must be wide enough to 223 | * accomodate the result (48 bytes). The context is automatically 224 | * reinitialized. 225 | * 226 | * @param cc the Luffa-384 context 227 | * @param dst the destination buffer 228 | */ 229 | void sph_luffa384_close(void *cc, void *dst); 230 | 231 | /** 232 | * Add a few additional bits (0 to 7) to the current computation, then 233 | * terminate it and output the result in the provided buffer, which must 234 | * be wide enough to accomodate the result (48 bytes). If bit number i 235 | * in ub has value 2^i, then the extra bits are those 236 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 237 | * level). The context is automatically reinitialized. 238 | * 239 | * @param cc the Luffa-384 context 240 | * @param ub the extra bits 241 | * @param n the number of extra bits (0 to 7) 242 | * @param dst the destination buffer 243 | */ 244 | void sph_luffa384_addbits_and_close( 245 | void *cc, unsigned ub, unsigned n, void *dst); 246 | 247 | /** 248 | * Initialize a Luffa-512 context. This process performs no memory allocation. 249 | * 250 | * @param cc the Luffa-512 context (pointer to a 251 | * sph_luffa512_context) 252 | */ 253 | void sph_luffa512_init(void *cc); 254 | 255 | /** 256 | * Process some data bytes. It is acceptable that len is zero 257 | * (in which case this function does nothing). 258 | * 259 | * @param cc the Luffa-512 context 260 | * @param data the input data 261 | * @param len the input data length (in bytes) 262 | */ 263 | void sph_luffa512(void *cc, const void *data, size_t len); 264 | 265 | /** 266 | * Terminate the current Luffa-512 computation and output the result into 267 | * the provided buffer. The destination buffer must be wide enough to 268 | * accomodate the result (64 bytes). The context is automatically 269 | * reinitialized. 270 | * 271 | * @param cc the Luffa-512 context 272 | * @param dst the destination buffer 273 | */ 274 | void sph_luffa512_close(void *cc, void *dst); 275 | 276 | /** 277 | * Add a few additional bits (0 to 7) to the current computation, then 278 | * terminate it and output the result in the provided buffer, which must 279 | * be wide enough to accomodate the result (64 bytes). If bit number i 280 | * in ub has value 2^i, then the extra bits are those 281 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 282 | * level). The context is automatically reinitialized. 283 | * 284 | * @param cc the Luffa-512 context 285 | * @param ub the extra bits 286 | * @param n the number of extra bits (0 to 7) 287 | * @param dst the destination buffer 288 | */ 289 | void sph_luffa512_addbits_and_close( 290 | void *cc, unsigned ub, unsigned n, void *dst); 291 | 292 | #ifdef __cplusplus 293 | } 294 | #endif 295 | 296 | #endif 297 | -------------------------------------------------------------------------------- /x17r/sph_ripemd.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_ripemd.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * RIPEMD, RIPEMD-128 and RIPEMD-160 interface. 4 | * 5 | * RIPEMD was first described in: Research and Development in Advanced 6 | * Communication Technologies in Europe, "RIPE Integrity Primitives: 7 | * Final Report of RACE Integrity Primitives Evaluation (R1040)", RACE, 8 | * June 1992. 9 | * 10 | * A new, strengthened version, dubbed RIPEMD-160, was published in: H. 11 | * Dobbertin, A. Bosselaers, and B. Preneel, "RIPEMD-160, a strengthened 12 | * version of RIPEMD", Fast Software Encryption - FSE'96, LNCS 1039, 13 | * Springer (1996), pp. 71--82. 14 | * 15 | * This article describes both RIPEMD-160, with a 160-bit output, and a 16 | * reduced version called RIPEMD-128, which has a 128-bit output. RIPEMD-128 17 | * was meant as a "drop-in" replacement for any hash function with 128-bit 18 | * output, especially the original RIPEMD. 19 | * 20 | * @warning Collisions, and an efficient method to build other collisions, 21 | * have been published for the original RIPEMD, which is thus considered as 22 | * cryptographically broken. It is also very rarely encountered, and there 23 | * seems to exist no free description or implementation of RIPEMD (except 24 | * the sphlib code, of course). As of january 2007, RIPEMD-128 and RIPEMD-160 25 | * seem as secure as their output length allows. 26 | * 27 | * ==========================(LICENSE BEGIN)============================ 28 | * 29 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 30 | * 31 | * Permission is hereby granted, free of charge, to any person obtaining 32 | * a copy of this software and associated documentation files (the 33 | * "Software"), to deal in the Software without restriction, including 34 | * without limitation the rights to use, copy, modify, merge, publish, 35 | * distribute, sublicense, and/or sell copies of the Software, and to 36 | * permit persons to whom the Software is furnished to do so, subject to 37 | * the following conditions: 38 | * 39 | * The above copyright notice and this permission notice shall be 40 | * included in all copies or substantial portions of the Software. 41 | * 42 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 43 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 44 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 45 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 46 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 47 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 48 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 49 | * 50 | * ===========================(LICENSE END)============================= 51 | * 52 | * @file sph_ripemd.h 53 | * @author Thomas Pornin 54 | */ 55 | 56 | #ifndef SPH_RIPEMD_H__ 57 | #define SPH_RIPEMD_H__ 58 | 59 | #include 60 | #include "sph_types.h" 61 | 62 | /** 63 | * Output size (in bits) for RIPEMD. 64 | */ 65 | #define SPH_SIZE_ripemd 128 66 | 67 | /** 68 | * Output size (in bits) for RIPEMD-128. 69 | */ 70 | #define SPH_SIZE_ripemd128 128 71 | 72 | /** 73 | * Output size (in bits) for RIPEMD-160. 74 | */ 75 | #define SPH_SIZE_ripemd160 160 76 | 77 | /** 78 | * This structure is a context for RIPEMD computations: it contains the 79 | * intermediate values and some data from the last entered block. Once 80 | * a RIPEMD computation has been performed, the context can be reused for 81 | * another computation. 82 | * 83 | * The contents of this structure are private. A running RIPEMD computation 84 | * can be cloned by copying the context (e.g. with a simple 85 | * memcpy()). 86 | */ 87 | typedef struct { 88 | #ifndef DOXYGEN_IGNORE 89 | unsigned char buf[64]; /* first field, for alignment */ 90 | sph_u32 val[4]; 91 | #if SPH_64 92 | sph_u64 count; 93 | #else 94 | sph_u32 count_high, count_low; 95 | #endif 96 | #endif 97 | } sph_ripemd_context; 98 | 99 | /** 100 | * Initialize a RIPEMD context. This process performs no memory allocation. 101 | * 102 | * @param cc the RIPEMD context (pointer to 103 | * a sph_ripemd_context) 104 | */ 105 | void sph_ripemd_init(void *cc); 106 | 107 | /** 108 | * Process some data bytes. It is acceptable that len is zero 109 | * (in which case this function does nothing). 110 | * 111 | * @param cc the RIPEMD context 112 | * @param data the input data 113 | * @param len the input data length (in bytes) 114 | */ 115 | void sph_ripemd(void *cc, const void *data, size_t len); 116 | 117 | /** 118 | * Terminate the current RIPEMD computation and output the result into the 119 | * provided buffer. The destination buffer must be wide enough to 120 | * accomodate the result (16 bytes). The context is automatically 121 | * reinitialized. 122 | * 123 | * @param cc the RIPEMD context 124 | * @param dst the destination buffer 125 | */ 126 | void sph_ripemd_close(void *cc, void *dst); 127 | 128 | /** 129 | * Apply the RIPEMD compression function on the provided data. The 130 | * msg parameter contains the 16 32-bit input blocks, 131 | * as numerical values (hence after the little-endian decoding). The 132 | * val parameter contains the 5 32-bit input blocks for 133 | * the compression function; the output is written in place in this 134 | * array. 135 | * 136 | * @param msg the message block (16 values) 137 | * @param val the function 128-bit input and output 138 | */ 139 | void sph_ripemd_comp(const sph_u32 msg[16], sph_u32 val[4]); 140 | 141 | /* ===================================================================== */ 142 | 143 | /** 144 | * This structure is a context for RIPEMD-128 computations: it contains the 145 | * intermediate values and some data from the last entered block. Once 146 | * a RIPEMD-128 computation has been performed, the context can be reused for 147 | * another computation. 148 | * 149 | * The contents of this structure are private. A running RIPEMD-128 computation 150 | * can be cloned by copying the context (e.g. with a simple 151 | * memcpy()). 152 | */ 153 | typedef struct { 154 | #ifndef DOXYGEN_IGNORE 155 | unsigned char buf[64]; /* first field, for alignment */ 156 | sph_u32 val[4]; 157 | #if SPH_64 158 | sph_u64 count; 159 | #else 160 | sph_u32 count_high, count_low; 161 | #endif 162 | #endif 163 | } sph_ripemd128_context; 164 | 165 | /** 166 | * Initialize a RIPEMD-128 context. This process performs no memory allocation. 167 | * 168 | * @param cc the RIPEMD-128 context (pointer to 169 | * a sph_ripemd128_context) 170 | */ 171 | void sph_ripemd128_init(void *cc); 172 | 173 | /** 174 | * Process some data bytes. It is acceptable that len is zero 175 | * (in which case this function does nothing). 176 | * 177 | * @param cc the RIPEMD-128 context 178 | * @param data the input data 179 | * @param len the input data length (in bytes) 180 | */ 181 | void sph_ripemd128(void *cc, const void *data, size_t len); 182 | 183 | /** 184 | * Terminate the current RIPEMD-128 computation and output the result into the 185 | * provided buffer. The destination buffer must be wide enough to 186 | * accomodate the result (16 bytes). The context is automatically 187 | * reinitialized. 188 | * 189 | * @param cc the RIPEMD-128 context 190 | * @param dst the destination buffer 191 | */ 192 | void sph_ripemd128_close(void *cc, void *dst); 193 | 194 | /** 195 | * Apply the RIPEMD-128 compression function on the provided data. The 196 | * msg parameter contains the 16 32-bit input blocks, 197 | * as numerical values (hence after the little-endian decoding). The 198 | * val parameter contains the 5 32-bit input blocks for 199 | * the compression function; the output is written in place in this 200 | * array. 201 | * 202 | * @param msg the message block (16 values) 203 | * @param val the function 128-bit input and output 204 | */ 205 | void sph_ripemd128_comp(const sph_u32 msg[16], sph_u32 val[4]); 206 | 207 | /* ===================================================================== */ 208 | 209 | /** 210 | * This structure is a context for RIPEMD-160 computations: it contains the 211 | * intermediate values and some data from the last entered block. Once 212 | * a RIPEMD-160 computation has been performed, the context can be reused for 213 | * another computation. 214 | * 215 | * The contents of this structure are private. A running RIPEMD-160 computation 216 | * can be cloned by copying the context (e.g. with a simple 217 | * memcpy()). 218 | */ 219 | typedef struct { 220 | #ifndef DOXYGEN_IGNORE 221 | unsigned char buf[64]; /* first field, for alignment */ 222 | sph_u32 val[5]; 223 | #if SPH_64 224 | sph_u64 count; 225 | #else 226 | sph_u32 count_high, count_low; 227 | #endif 228 | #endif 229 | } sph_ripemd160_context; 230 | 231 | /** 232 | * Initialize a RIPEMD-160 context. This process performs no memory allocation. 233 | * 234 | * @param cc the RIPEMD-160 context (pointer to 235 | * a sph_ripemd160_context) 236 | */ 237 | void sph_ripemd160_init(void *cc); 238 | 239 | /** 240 | * Process some data bytes. It is acceptable that len is zero 241 | * (in which case this function does nothing). 242 | * 243 | * @param cc the RIPEMD-160 context 244 | * @param data the input data 245 | * @param len the input data length (in bytes) 246 | */ 247 | void sph_ripemd160(void *cc, const void *data, size_t len); 248 | 249 | /** 250 | * Terminate the current RIPEMD-160 computation and output the result into the 251 | * provided buffer. The destination buffer must be wide enough to 252 | * accomodate the result (20 bytes). The context is automatically 253 | * reinitialized. 254 | * 255 | * @param cc the RIPEMD-160 context 256 | * @param dst the destination buffer 257 | */ 258 | void sph_ripemd160_close(void *cc, void *dst); 259 | 260 | /** 261 | * Apply the RIPEMD-160 compression function on the provided data. The 262 | * msg parameter contains the 16 32-bit input blocks, 263 | * as numerical values (hence after the little-endian decoding). The 264 | * val parameter contains the 5 32-bit input blocks for 265 | * the compression function; the output is written in place in this 266 | * array. 267 | * 268 | * @param msg the message block (16 values) 269 | * @param val the function 160-bit input and output 270 | */ 271 | void sph_ripemd160_comp(const sph_u32 msg[16], sph_u32 val[5]); 272 | 273 | #endif 274 | 275 | -------------------------------------------------------------------------------- /x17r/sph_sha2big.c: -------------------------------------------------------------------------------- 1 | /* $Id: sha2big.c 216 2010-06-08 09:46:57Z tp $ */ 2 | /* 3 | * SHA-384 / SHA-512 implementation. 4 | * 5 | * ==========================(LICENSE BEGIN)============================ 6 | * 7 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining 10 | * a copy of this software and associated documentation files (the 11 | * "Software"), to deal in the Software without restriction, including 12 | * without limitation the rights to use, copy, modify, merge, publish, 13 | * distribute, sublicense, and/or sell copies of the Software, and to 14 | * permit persons to whom the Software is furnished to do so, subject to 15 | * the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 24 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 25 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 26 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 | * 28 | * ===========================(LICENSE END)============================= 29 | * 30 | * @author Thomas Pornin 31 | */ 32 | 33 | #include 34 | #include 35 | 36 | #include "sph_sha2.h" 37 | 38 | #if SPH_64 39 | 40 | #define CH(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z)) 41 | #define MAJ(X, Y, Z) (((X) & (Y)) | (((X) | (Y)) & (Z))) 42 | 43 | #define ROTR64 SPH_ROTR64 44 | 45 | #define BSG5_0(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39)) 46 | #define BSG5_1(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41)) 47 | #define SSG5_0(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ SPH_T64((x) >> 7)) 48 | #define SSG5_1(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ SPH_T64((x) >> 6)) 49 | 50 | static const sph_u64 K512[80] = { 51 | SPH_C64(0x428A2F98D728AE22), SPH_C64(0x7137449123EF65CD), 52 | SPH_C64(0xB5C0FBCFEC4D3B2F), SPH_C64(0xE9B5DBA58189DBBC), 53 | SPH_C64(0x3956C25BF348B538), SPH_C64(0x59F111F1B605D019), 54 | SPH_C64(0x923F82A4AF194F9B), SPH_C64(0xAB1C5ED5DA6D8118), 55 | SPH_C64(0xD807AA98A3030242), SPH_C64(0x12835B0145706FBE), 56 | SPH_C64(0x243185BE4EE4B28C), SPH_C64(0x550C7DC3D5FFB4E2), 57 | SPH_C64(0x72BE5D74F27B896F), SPH_C64(0x80DEB1FE3B1696B1), 58 | SPH_C64(0x9BDC06A725C71235), SPH_C64(0xC19BF174CF692694), 59 | SPH_C64(0xE49B69C19EF14AD2), SPH_C64(0xEFBE4786384F25E3), 60 | SPH_C64(0x0FC19DC68B8CD5B5), SPH_C64(0x240CA1CC77AC9C65), 61 | SPH_C64(0x2DE92C6F592B0275), SPH_C64(0x4A7484AA6EA6E483), 62 | SPH_C64(0x5CB0A9DCBD41FBD4), SPH_C64(0x76F988DA831153B5), 63 | SPH_C64(0x983E5152EE66DFAB), SPH_C64(0xA831C66D2DB43210), 64 | SPH_C64(0xB00327C898FB213F), SPH_C64(0xBF597FC7BEEF0EE4), 65 | SPH_C64(0xC6E00BF33DA88FC2), SPH_C64(0xD5A79147930AA725), 66 | SPH_C64(0x06CA6351E003826F), SPH_C64(0x142929670A0E6E70), 67 | SPH_C64(0x27B70A8546D22FFC), SPH_C64(0x2E1B21385C26C926), 68 | SPH_C64(0x4D2C6DFC5AC42AED), SPH_C64(0x53380D139D95B3DF), 69 | SPH_C64(0x650A73548BAF63DE), SPH_C64(0x766A0ABB3C77B2A8), 70 | SPH_C64(0x81C2C92E47EDAEE6), SPH_C64(0x92722C851482353B), 71 | SPH_C64(0xA2BFE8A14CF10364), SPH_C64(0xA81A664BBC423001), 72 | SPH_C64(0xC24B8B70D0F89791), SPH_C64(0xC76C51A30654BE30), 73 | SPH_C64(0xD192E819D6EF5218), SPH_C64(0xD69906245565A910), 74 | SPH_C64(0xF40E35855771202A), SPH_C64(0x106AA07032BBD1B8), 75 | SPH_C64(0x19A4C116B8D2D0C8), SPH_C64(0x1E376C085141AB53), 76 | SPH_C64(0x2748774CDF8EEB99), SPH_C64(0x34B0BCB5E19B48A8), 77 | SPH_C64(0x391C0CB3C5C95A63), SPH_C64(0x4ED8AA4AE3418ACB), 78 | SPH_C64(0x5B9CCA4F7763E373), SPH_C64(0x682E6FF3D6B2B8A3), 79 | SPH_C64(0x748F82EE5DEFB2FC), SPH_C64(0x78A5636F43172F60), 80 | SPH_C64(0x84C87814A1F0AB72), SPH_C64(0x8CC702081A6439EC), 81 | SPH_C64(0x90BEFFFA23631E28), SPH_C64(0xA4506CEBDE82BDE9), 82 | SPH_C64(0xBEF9A3F7B2C67915), SPH_C64(0xC67178F2E372532B), 83 | SPH_C64(0xCA273ECEEA26619C), SPH_C64(0xD186B8C721C0C207), 84 | SPH_C64(0xEADA7DD6CDE0EB1E), SPH_C64(0xF57D4F7FEE6ED178), 85 | SPH_C64(0x06F067AA72176FBA), SPH_C64(0x0A637DC5A2C898A6), 86 | SPH_C64(0x113F9804BEF90DAE), SPH_C64(0x1B710B35131C471B), 87 | SPH_C64(0x28DB77F523047D84), SPH_C64(0x32CAAB7B40C72493), 88 | SPH_C64(0x3C9EBE0A15C9BEBC), SPH_C64(0x431D67C49C100D4C), 89 | SPH_C64(0x4CC5D4BECB3E42B6), SPH_C64(0x597F299CFC657E2A), 90 | SPH_C64(0x5FCB6FAB3AD6FAEC), SPH_C64(0x6C44198C4A475817) 91 | }; 92 | 93 | static const sph_u64 H384[8] = { 94 | SPH_C64(0xCBBB9D5DC1059ED8), SPH_C64(0x629A292A367CD507), 95 | SPH_C64(0x9159015A3070DD17), SPH_C64(0x152FECD8F70E5939), 96 | SPH_C64(0x67332667FFC00B31), SPH_C64(0x8EB44A8768581511), 97 | SPH_C64(0xDB0C2E0D64F98FA7), SPH_C64(0x47B5481DBEFA4FA4) 98 | }; 99 | 100 | static const sph_u64 H512[8] = { 101 | SPH_C64(0x6A09E667F3BCC908), SPH_C64(0xBB67AE8584CAA73B), 102 | SPH_C64(0x3C6EF372FE94F82B), SPH_C64(0xA54FF53A5F1D36F1), 103 | SPH_C64(0x510E527FADE682D1), SPH_C64(0x9B05688C2B3E6C1F), 104 | SPH_C64(0x1F83D9ABFB41BD6B), SPH_C64(0x5BE0CD19137E2179) 105 | }; 106 | 107 | /* 108 | * This macro defines the body for a SHA-384 / SHA-512 compression function 109 | * implementation. The "in" parameter should evaluate, when applied to a 110 | * numerical input parameter from 0 to 15, to an expression which yields 111 | * the corresponding input block. The "r" parameter should evaluate to 112 | * an array or pointer expression designating the array of 8 words which 113 | * contains the input and output of the compression function. 114 | * 115 | * SHA-512 is hard for the compiler. If the loop is completely unrolled, 116 | * then the code will be quite huge (possibly more than 100 kB), and the 117 | * performance will be degraded due to cache misses on the code. We 118 | * unroll only eight steps, which avoids all needless copies when 119 | * 64-bit registers are swapped. 120 | */ 121 | 122 | #define SHA3_STEP(A, B, C, D, E, F, G, H, i) do { \ 123 | sph_u64 T1, T2; \ 124 | T1 = SPH_T64(H + BSG5_1(E) + CH(E, F, G) + K512[i] + W[i]); \ 125 | T2 = SPH_T64(BSG5_0(A) + MAJ(A, B, C)); \ 126 | D = SPH_T64(D + T1); \ 127 | H = SPH_T64(T1 + T2); \ 128 | } while (0) 129 | 130 | #define SHA3_ROUND_BODY(in, r) do { \ 131 | int i; \ 132 | sph_u64 A, B, C, D, E, F, G, H; \ 133 | sph_u64 W[80]; \ 134 | \ 135 | for (i = 0; i < 16; i ++) \ 136 | W[i] = in(i); \ 137 | for (i = 16; i < 80; i ++) \ 138 | W[i] = SPH_T64(SSG5_1(W[i - 2]) + W[i - 7] \ 139 | + SSG5_0(W[i - 15]) + W[i - 16]); \ 140 | A = (r)[0]; \ 141 | B = (r)[1]; \ 142 | C = (r)[2]; \ 143 | D = (r)[3]; \ 144 | E = (r)[4]; \ 145 | F = (r)[5]; \ 146 | G = (r)[6]; \ 147 | H = (r)[7]; \ 148 | for (i = 0; i < 80; i += 8) { \ 149 | SHA3_STEP(A, B, C, D, E, F, G, H, i + 0); \ 150 | SHA3_STEP(H, A, B, C, D, E, F, G, i + 1); \ 151 | SHA3_STEP(G, H, A, B, C, D, E, F, i + 2); \ 152 | SHA3_STEP(F, G, H, A, B, C, D, E, i + 3); \ 153 | SHA3_STEP(E, F, G, H, A, B, C, D, i + 4); \ 154 | SHA3_STEP(D, E, F, G, H, A, B, C, i + 5); \ 155 | SHA3_STEP(C, D, E, F, G, H, A, B, i + 6); \ 156 | SHA3_STEP(B, C, D, E, F, G, H, A, i + 7); \ 157 | } \ 158 | (r)[0] = SPH_T64((r)[0] + A); \ 159 | (r)[1] = SPH_T64((r)[1] + B); \ 160 | (r)[2] = SPH_T64((r)[2] + C); \ 161 | (r)[3] = SPH_T64((r)[3] + D); \ 162 | (r)[4] = SPH_T64((r)[4] + E); \ 163 | (r)[5] = SPH_T64((r)[5] + F); \ 164 | (r)[6] = SPH_T64((r)[6] + G); \ 165 | (r)[7] = SPH_T64((r)[7] + H); \ 166 | } while (0) 167 | 168 | /* 169 | * One round of SHA-384 / SHA-512. The data must be aligned for 64-bit access. 170 | */ 171 | static void 172 | sha3_round(const unsigned char *data, sph_u64 r[8]) 173 | { 174 | #define SHA3_IN(x) sph_dec64be_aligned(data + (8 * (x))) 175 | SHA3_ROUND_BODY(SHA3_IN, r); 176 | #undef SHA3_IN 177 | } 178 | 179 | /* see sph_sha3.h */ 180 | void 181 | sph_sha384_init(void *cc) 182 | { 183 | sph_sha384_context *sc; 184 | 185 | sc = cc; 186 | memcpy(sc->val, H384, sizeof H384); 187 | sc->count = 0; 188 | } 189 | 190 | /* see sph_sha3.h */ 191 | void 192 | sph_sha512_init(void *cc) 193 | { 194 | sph_sha512_context *sc; 195 | 196 | sc = cc; 197 | memcpy(sc->val, H512, sizeof H512); 198 | sc->count = 0; 199 | } 200 | 201 | #define RFUN sha3_round 202 | #define HASH sha384 203 | #define BE64 1 204 | #include "md_helper.c" 205 | 206 | /* see sph_sha3.h */ 207 | void 208 | sph_sha384_close(void *cc, void *dst) 209 | { 210 | sha384_close(cc, dst, 6); 211 | sph_sha384_init(cc); 212 | } 213 | 214 | /* see sph_sha3.h */ 215 | void 216 | sph_sha384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst) 217 | { 218 | sha384_addbits_and_close(cc, ub, n, dst, 6); 219 | sph_sha384_init(cc); 220 | } 221 | 222 | /* see sph_sha3.h */ 223 | void 224 | sph_sha512_close(void *cc, void *dst) 225 | { 226 | sha384_close(cc, dst, 8); 227 | sph_sha512_init(cc); 228 | } 229 | 230 | /* see sph_sha3.h */ 231 | void 232 | sph_sha512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst) 233 | { 234 | sha384_addbits_and_close(cc, ub, n, dst, 8); 235 | sph_sha512_init(cc); 236 | } 237 | 238 | /* see sph_sha3.h */ 239 | void 240 | sph_sha384_comp(const sph_u64 msg[16], sph_u64 val[8]) 241 | { 242 | #define SHA3_IN(x) msg[x] 243 | SHA3_ROUND_BODY(SHA3_IN, val); 244 | #undef SHA3_IN 245 | } 246 | 247 | #endif 248 | -------------------------------------------------------------------------------- /x17r/sph_simd.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_simd.h 154 2010-04-26 17:00:24Z tp $ */ 2 | /** 3 | * SIMD interface. SIMD is a family of functions which differ by 4 | * their output size; this implementation defines SIMD for output 5 | * sizes 224, 256, 384 and 512 bits. 6 | * 7 | * ==========================(LICENSE BEGIN)============================ 8 | * 9 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining 12 | * a copy of this software and associated documentation files (the 13 | * "Software"), to deal in the Software without restriction, including 14 | * without limitation the rights to use, copy, modify, merge, publish, 15 | * distribute, sublicense, and/or sell copies of the Software, and to 16 | * permit persons to whom the Software is furnished to do so, subject to 17 | * the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | * 30 | * ===========================(LICENSE END)============================= 31 | * 32 | * @file sph_simd.h 33 | * @author Thomas Pornin 34 | */ 35 | 36 | #ifndef SPH_SIMD_H__ 37 | #define SPH_SIMD_H__ 38 | 39 | #ifdef __cplusplus 40 | extern "C"{ 41 | #endif 42 | 43 | #include 44 | #include "sph_types.h" 45 | 46 | /** 47 | * Output size (in bits) for SIMD-224. 48 | */ 49 | #define SPH_SIZE_simd224 224 50 | 51 | /** 52 | * Output size (in bits) for SIMD-256. 53 | */ 54 | #define SPH_SIZE_simd256 256 55 | 56 | /** 57 | * Output size (in bits) for SIMD-384. 58 | */ 59 | #define SPH_SIZE_simd384 384 60 | 61 | /** 62 | * Output size (in bits) for SIMD-512. 63 | */ 64 | #define SPH_SIZE_simd512 512 65 | 66 | /** 67 | * This structure is a context for SIMD computations: it contains the 68 | * intermediate values and some data from the last entered block. Once 69 | * an SIMD computation has been performed, the context can be reused for 70 | * another computation. This specific structure is used for SIMD-224 71 | * and SIMD-256. 72 | * 73 | * The contents of this structure are private. A running SIMD computation 74 | * can be cloned by copying the context (e.g. with a simple 75 | * memcpy()). 76 | */ 77 | typedef struct { 78 | #ifndef DOXYGEN_IGNORE 79 | unsigned char buf[64]; /* first field, for alignment */ 80 | size_t ptr; 81 | sph_u32 state[16]; 82 | sph_u32 count_low, count_high; 83 | #endif 84 | } sph_simd_small_context; 85 | 86 | /** 87 | * This structure is a context for SIMD computations: it contains the 88 | * intermediate values and some data from the last entered block. Once 89 | * an SIMD computation has been performed, the context can be reused for 90 | * another computation. This specific structure is used for SIMD-384 91 | * and SIMD-512. 92 | * 93 | * The contents of this structure are private. A running SIMD computation 94 | * can be cloned by copying the context (e.g. with a simple 95 | * memcpy()). 96 | */ 97 | typedef struct { 98 | #ifndef DOXYGEN_IGNORE 99 | unsigned char buf[128]; /* first field, for alignment */ 100 | size_t ptr; 101 | sph_u32 state[32]; 102 | sph_u32 count_low, count_high; 103 | #endif 104 | } sph_simd_big_context; 105 | 106 | /** 107 | * Type for a SIMD-224 context (identical to the common "small" context). 108 | */ 109 | typedef sph_simd_small_context sph_simd224_context; 110 | 111 | /** 112 | * Type for a SIMD-256 context (identical to the common "small" context). 113 | */ 114 | typedef sph_simd_small_context sph_simd256_context; 115 | 116 | /** 117 | * Type for a SIMD-384 context (identical to the common "big" context). 118 | */ 119 | typedef sph_simd_big_context sph_simd384_context; 120 | 121 | /** 122 | * Type for a SIMD-512 context (identical to the common "big" context). 123 | */ 124 | typedef sph_simd_big_context sph_simd512_context; 125 | 126 | /** 127 | * Initialize an SIMD-224 context. This process performs no memory allocation. 128 | * 129 | * @param cc the SIMD-224 context (pointer to a 130 | * sph_simd224_context) 131 | */ 132 | void sph_simd224_init(void *cc); 133 | 134 | /** 135 | * Process some data bytes. It is acceptable that len is zero 136 | * (in which case this function does nothing). 137 | * 138 | * @param cc the SIMD-224 context 139 | * @param data the input data 140 | * @param len the input data length (in bytes) 141 | */ 142 | void sph_simd224(void *cc, const void *data, size_t len); 143 | 144 | /** 145 | * Terminate the current SIMD-224 computation and output the result into 146 | * the provided buffer. The destination buffer must be wide enough to 147 | * accomodate the result (28 bytes). The context is automatically 148 | * reinitialized. 149 | * 150 | * @param cc the SIMD-224 context 151 | * @param dst the destination buffer 152 | */ 153 | void sph_simd224_close(void *cc, void *dst); 154 | 155 | /** 156 | * Add a few additional bits (0 to 7) to the current computation, then 157 | * terminate it and output the result in the provided buffer, which must 158 | * be wide enough to accomodate the result (28 bytes). If bit number i 159 | * in ub has value 2^i, then the extra bits are those 160 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 161 | * level). The context is automatically reinitialized. 162 | * 163 | * @param cc the SIMD-224 context 164 | * @param ub the extra bits 165 | * @param n the number of extra bits (0 to 7) 166 | * @param dst the destination buffer 167 | */ 168 | void sph_simd224_addbits_and_close( 169 | void *cc, unsigned ub, unsigned n, void *dst); 170 | 171 | /** 172 | * Initialize an SIMD-256 context. This process performs no memory allocation. 173 | * 174 | * @param cc the SIMD-256 context (pointer to a 175 | * sph_simd256_context) 176 | */ 177 | void sph_simd256_init(void *cc); 178 | 179 | /** 180 | * Process some data bytes. It is acceptable that len is zero 181 | * (in which case this function does nothing). 182 | * 183 | * @param cc the SIMD-256 context 184 | * @param data the input data 185 | * @param len the input data length (in bytes) 186 | */ 187 | void sph_simd256(void *cc, const void *data, size_t len); 188 | 189 | /** 190 | * Terminate the current SIMD-256 computation and output the result into 191 | * the provided buffer. The destination buffer must be wide enough to 192 | * accomodate the result (32 bytes). The context is automatically 193 | * reinitialized. 194 | * 195 | * @param cc the SIMD-256 context 196 | * @param dst the destination buffer 197 | */ 198 | void sph_simd256_close(void *cc, void *dst); 199 | 200 | /** 201 | * Add a few additional bits (0 to 7) to the current computation, then 202 | * terminate it and output the result in the provided buffer, which must 203 | * be wide enough to accomodate the result (32 bytes). If bit number i 204 | * in ub has value 2^i, then the extra bits are those 205 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 206 | * level). The context is automatically reinitialized. 207 | * 208 | * @param cc the SIMD-256 context 209 | * @param ub the extra bits 210 | * @param n the number of extra bits (0 to 7) 211 | * @param dst the destination buffer 212 | */ 213 | void sph_simd256_addbits_and_close( 214 | void *cc, unsigned ub, unsigned n, void *dst); 215 | 216 | /** 217 | * Initialize an SIMD-384 context. This process performs no memory allocation. 218 | * 219 | * @param cc the SIMD-384 context (pointer to a 220 | * sph_simd384_context) 221 | */ 222 | void sph_simd384_init(void *cc); 223 | 224 | /** 225 | * Process some data bytes. It is acceptable that len is zero 226 | * (in which case this function does nothing). 227 | * 228 | * @param cc the SIMD-384 context 229 | * @param data the input data 230 | * @param len the input data length (in bytes) 231 | */ 232 | void sph_simd384(void *cc, const void *data, size_t len); 233 | 234 | /** 235 | * Terminate the current SIMD-384 computation and output the result into 236 | * the provided buffer. The destination buffer must be wide enough to 237 | * accomodate the result (48 bytes). The context is automatically 238 | * reinitialized. 239 | * 240 | * @param cc the SIMD-384 context 241 | * @param dst the destination buffer 242 | */ 243 | void sph_simd384_close(void *cc, void *dst); 244 | 245 | /** 246 | * Add a few additional bits (0 to 7) to the current computation, then 247 | * terminate it and output the result in the provided buffer, which must 248 | * be wide enough to accomodate the result (48 bytes). If bit number i 249 | * in ub has value 2^i, then the extra bits are those 250 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 251 | * level). The context is automatically reinitialized. 252 | * 253 | * @param cc the SIMD-384 context 254 | * @param ub the extra bits 255 | * @param n the number of extra bits (0 to 7) 256 | * @param dst the destination buffer 257 | */ 258 | void sph_simd384_addbits_and_close( 259 | void *cc, unsigned ub, unsigned n, void *dst); 260 | 261 | /** 262 | * Initialize an SIMD-512 context. This process performs no memory allocation. 263 | * 264 | * @param cc the SIMD-512 context (pointer to a 265 | * sph_simd512_context) 266 | */ 267 | void sph_simd512_init(void *cc); 268 | 269 | /** 270 | * Process some data bytes. It is acceptable that len is zero 271 | * (in which case this function does nothing). 272 | * 273 | * @param cc the SIMD-512 context 274 | * @param data the input data 275 | * @param len the input data length (in bytes) 276 | */ 277 | void sph_simd512(void *cc, const void *data, size_t len); 278 | 279 | /** 280 | * Terminate the current SIMD-512 computation and output the result into 281 | * the provided buffer. The destination buffer must be wide enough to 282 | * accomodate the result (64 bytes). The context is automatically 283 | * reinitialized. 284 | * 285 | * @param cc the SIMD-512 context 286 | * @param dst the destination buffer 287 | */ 288 | void sph_simd512_close(void *cc, void *dst); 289 | 290 | /** 291 | * Add a few additional bits (0 to 7) to the current computation, then 292 | * terminate it and output the result in the provided buffer, which must 293 | * be wide enough to accomodate the result (64 bytes). If bit number i 294 | * in ub has value 2^i, then the extra bits are those 295 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 296 | * level). The context is automatically reinitialized. 297 | * 298 | * @param cc the SIMD-512 context 299 | * @param ub the extra bits 300 | * @param n the number of extra bits (0 to 7) 301 | * @param dst the destination buffer 302 | */ 303 | void sph_simd512_addbits_and_close( 304 | void *cc, unsigned ub, unsigned n, void *dst); 305 | #ifdef __cplusplus 306 | } 307 | #endif 308 | 309 | #endif 310 | -------------------------------------------------------------------------------- /x17r/sph_skein.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_skein.h 253 2011-06-07 18:33:10Z tp $ */ 2 | /** 3 | * Skein interface. The Skein specification defines three main 4 | * functions, called Skein-256, Skein-512 and Skein-1024, which can be 5 | * further parameterized with an output length. For the SHA-3 6 | * competition, Skein-512 is used for output sizes of 224, 256, 384 and 7 | * 512 bits; this is what this code implements. Thus, we hereafter call 8 | * Skein-224, Skein-256, Skein-384 and Skein-512 what the Skein 9 | * specification defines as Skein-512-224, Skein-512-256, Skein-512-384 10 | * and Skein-512-512, respectively. 11 | * 12 | * ==========================(LICENSE BEGIN)============================ 13 | * 14 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining 17 | * a copy of this software and associated documentation files (the 18 | * "Software"), to deal in the Software without restriction, including 19 | * without limitation the rights to use, copy, modify, merge, publish, 20 | * distribute, sublicense, and/or sell copies of the Software, and to 21 | * permit persons to whom the Software is furnished to do so, subject to 22 | * the following conditions: 23 | * 24 | * The above copyright notice and this permission notice shall be 25 | * included in all copies or substantial portions of the Software. 26 | * 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 28 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 29 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 30 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 31 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 32 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 33 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 34 | * 35 | * ===========================(LICENSE END)============================= 36 | * 37 | * @file sph_skein.h 38 | * @author Thomas Pornin 39 | */ 40 | 41 | #ifndef SPH_SKEIN_H__ 42 | #define SPH_SKEIN_H__ 43 | 44 | #ifdef __cplusplus 45 | extern "C"{ 46 | #endif 47 | 48 | #include 49 | #include "sph_types.h" 50 | 51 | #if SPH_64 52 | 53 | /** 54 | * Output size (in bits) for Skein-224. 55 | */ 56 | #define SPH_SIZE_skein224 224 57 | 58 | /** 59 | * Output size (in bits) for Skein-256. 60 | */ 61 | #define SPH_SIZE_skein256 256 62 | 63 | /** 64 | * Output size (in bits) for Skein-384. 65 | */ 66 | #define SPH_SIZE_skein384 384 67 | 68 | /** 69 | * Output size (in bits) for Skein-512. 70 | */ 71 | #define SPH_SIZE_skein512 512 72 | 73 | /** 74 | * This structure is a context for Skein computations (with a 384- or 75 | * 512-bit output): it contains the intermediate values and some data 76 | * from the last entered block. Once a Skein computation has been 77 | * performed, the context can be reused for another computation. 78 | * 79 | * The contents of this structure are private. A running Skein computation 80 | * can be cloned by copying the context (e.g. with a simple 81 | * memcpy()). 82 | */ 83 | typedef struct { 84 | #ifndef DOXYGEN_IGNORE 85 | unsigned char buf[64]; /* first field, for alignment */ 86 | size_t ptr; 87 | sph_u64 h0, h1, h2, h3, h4, h5, h6, h7; 88 | sph_u64 bcount; 89 | #endif 90 | } sph_skein_big_context; 91 | 92 | /** 93 | * Type for a Skein-224 context (identical to the common "big" context). 94 | */ 95 | typedef sph_skein_big_context sph_skein224_context; 96 | 97 | /** 98 | * Type for a Skein-256 context (identical to the common "big" context). 99 | */ 100 | typedef sph_skein_big_context sph_skein256_context; 101 | 102 | /** 103 | * Type for a Skein-384 context (identical to the common "big" context). 104 | */ 105 | typedef sph_skein_big_context sph_skein384_context; 106 | 107 | /** 108 | * Type for a Skein-512 context (identical to the common "big" context). 109 | */ 110 | typedef sph_skein_big_context sph_skein512_context; 111 | 112 | /** 113 | * Initialize a Skein-224 context. This process performs no memory allocation. 114 | * 115 | * @param cc the Skein-224 context (pointer to a 116 | * sph_skein224_context) 117 | */ 118 | void sph_skein224_init(void *cc); 119 | 120 | /** 121 | * Process some data bytes. It is acceptable that len is zero 122 | * (in which case this function does nothing). 123 | * 124 | * @param cc the Skein-224 context 125 | * @param data the input data 126 | * @param len the input data length (in bytes) 127 | */ 128 | void sph_skein224(void *cc, const void *data, size_t len); 129 | 130 | /** 131 | * Terminate the current Skein-224 computation and output the result into 132 | * the provided buffer. The destination buffer must be wide enough to 133 | * accomodate the result (28 bytes). The context is automatically 134 | * reinitialized. 135 | * 136 | * @param cc the Skein-224 context 137 | * @param dst the destination buffer 138 | */ 139 | void sph_skein224_close(void *cc, void *dst); 140 | 141 | /** 142 | * Add a few additional bits (0 to 7) to the current computation, then 143 | * terminate it and output the result in the provided buffer, which must 144 | * be wide enough to accomodate the result (28 bytes). If bit number i 145 | * in ub has value 2^i, then the extra bits are those 146 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 147 | * level). The context is automatically reinitialized. 148 | * 149 | * @param cc the Skein-224 context 150 | * @param ub the extra bits 151 | * @param n the number of extra bits (0 to 7) 152 | * @param dst the destination buffer 153 | */ 154 | void sph_skein224_addbits_and_close( 155 | void *cc, unsigned ub, unsigned n, void *dst); 156 | 157 | /** 158 | * Initialize a Skein-256 context. This process performs no memory allocation. 159 | * 160 | * @param cc the Skein-256 context (pointer to a 161 | * sph_skein256_context) 162 | */ 163 | void sph_skein256_init(void *cc); 164 | 165 | /** 166 | * Process some data bytes. It is acceptable that len is zero 167 | * (in which case this function does nothing). 168 | * 169 | * @param cc the Skein-256 context 170 | * @param data the input data 171 | * @param len the input data length (in bytes) 172 | */ 173 | void sph_skein256(void *cc, const void *data, size_t len); 174 | 175 | /** 176 | * Terminate the current Skein-256 computation and output the result into 177 | * the provided buffer. The destination buffer must be wide enough to 178 | * accomodate the result (32 bytes). The context is automatically 179 | * reinitialized. 180 | * 181 | * @param cc the Skein-256 context 182 | * @param dst the destination buffer 183 | */ 184 | void sph_skein256_close(void *cc, void *dst); 185 | 186 | /** 187 | * Add a few additional bits (0 to 7) to the current computation, then 188 | * terminate it and output the result in the provided buffer, which must 189 | * be wide enough to accomodate the result (32 bytes). If bit number i 190 | * in ub has value 2^i, then the extra bits are those 191 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 192 | * level). The context is automatically reinitialized. 193 | * 194 | * @param cc the Skein-256 context 195 | * @param ub the extra bits 196 | * @param n the number of extra bits (0 to 7) 197 | * @param dst the destination buffer 198 | */ 199 | void sph_skein256_addbits_and_close( 200 | void *cc, unsigned ub, unsigned n, void *dst); 201 | 202 | /** 203 | * Initialize a Skein-384 context. This process performs no memory allocation. 204 | * 205 | * @param cc the Skein-384 context (pointer to a 206 | * sph_skein384_context) 207 | */ 208 | void sph_skein384_init(void *cc); 209 | 210 | /** 211 | * Process some data bytes. It is acceptable that len is zero 212 | * (in which case this function does nothing). 213 | * 214 | * @param cc the Skein-384 context 215 | * @param data the input data 216 | * @param len the input data length (in bytes) 217 | */ 218 | void sph_skein384(void *cc, const void *data, size_t len); 219 | 220 | /** 221 | * Terminate the current Skein-384 computation and output the result into 222 | * the provided buffer. The destination buffer must be wide enough to 223 | * accomodate the result (48 bytes). The context is automatically 224 | * reinitialized. 225 | * 226 | * @param cc the Skein-384 context 227 | * @param dst the destination buffer 228 | */ 229 | void sph_skein384_close(void *cc, void *dst); 230 | 231 | /** 232 | * Add a few additional bits (0 to 7) to the current computation, then 233 | * terminate it and output the result in the provided buffer, which must 234 | * be wide enough to accomodate the result (48 bytes). If bit number i 235 | * in ub has value 2^i, then the extra bits are those 236 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 237 | * level). The context is automatically reinitialized. 238 | * 239 | * @param cc the Skein-384 context 240 | * @param ub the extra bits 241 | * @param n the number of extra bits (0 to 7) 242 | * @param dst the destination buffer 243 | */ 244 | void sph_skein384_addbits_and_close( 245 | void *cc, unsigned ub, unsigned n, void *dst); 246 | 247 | /** 248 | * Initialize a Skein-512 context. This process performs no memory allocation. 249 | * 250 | * @param cc the Skein-512 context (pointer to a 251 | * sph_skein512_context) 252 | */ 253 | void sph_skein512_init(void *cc); 254 | 255 | /** 256 | * Process some data bytes. It is acceptable that len is zero 257 | * (in which case this function does nothing). 258 | * 259 | * @param cc the Skein-512 context 260 | * @param data the input data 261 | * @param len the input data length (in bytes) 262 | */ 263 | void sph_skein512(void *cc, const void *data, size_t len); 264 | 265 | /** 266 | * Terminate the current Skein-512 computation and output the result into 267 | * the provided buffer. The destination buffer must be wide enough to 268 | * accomodate the result (64 bytes). The context is automatically 269 | * reinitialized. 270 | * 271 | * @param cc the Skein-512 context 272 | * @param dst the destination buffer 273 | */ 274 | void sph_skein512_close(void *cc, void *dst); 275 | 276 | /** 277 | * Add a few additional bits (0 to 7) to the current computation, then 278 | * terminate it and output the result in the provided buffer, which must 279 | * be wide enough to accomodate the result (64 bytes). If bit number i 280 | * in ub has value 2^i, then the extra bits are those 281 | * numbered 7 downto 8-n (this is the big-endian convention at the byte 282 | * level). The context is automatically reinitialized. 283 | * 284 | * @param cc the Skein-512 context 285 | * @param ub the extra bits 286 | * @param n the number of extra bits (0 to 7) 287 | * @param dst the destination buffer 288 | */ 289 | void sph_skein512_addbits_and_close( 290 | void *cc, unsigned ub, unsigned n, void *dst); 291 | 292 | #endif 293 | 294 | #ifdef __cplusplus 295 | } 296 | #endif 297 | 298 | #endif 299 | -------------------------------------------------------------------------------- /x17r/sph_whirlpool.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_whirlpool.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * WHIRLPOOL interface. 4 | * 5 | * WHIRLPOOL knows three variants, dubbed "WHIRLPOOL-0" (original 6 | * version, published in 2000, studied by NESSIE), "WHIRLPOOL-1" 7 | * (first revision, 2001, with a new S-box) and "WHIRLPOOL" (current 8 | * version, 2003, with a new diffusion matrix, also described as "plain 9 | * WHIRLPOOL"). All three variants are implemented here. 10 | * 11 | * The original WHIRLPOOL (i.e. WHIRLPOOL-0) was published in: P. S. L. 12 | * M. Barreto, V. Rijmen, "The Whirlpool Hashing Function", First open 13 | * NESSIE Workshop, Leuven, Belgium, November 13--14, 2000. 14 | * 15 | * The current WHIRLPOOL specification and a reference implementation 16 | * can be found on the WHIRLPOOL web page: 17 | * http://paginas.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html 18 | * 19 | * ==========================(LICENSE BEGIN)============================ 20 | * 21 | * Copyright (c) 2007-2010 Projet RNRT SAPHIR 22 | * 23 | * Permission is hereby granted, free of charge, to any person obtaining 24 | * a copy of this software and associated documentation files (the 25 | * "Software"), to deal in the Software without restriction, including 26 | * without limitation the rights to use, copy, modify, merge, publish, 27 | * distribute, sublicense, and/or sell copies of the Software, and to 28 | * permit persons to whom the Software is furnished to do so, subject to 29 | * the following conditions: 30 | * 31 | * The above copyright notice and this permission notice shall be 32 | * included in all copies or substantial portions of the Software. 33 | * 34 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 35 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 36 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 37 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 38 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 39 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 40 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 41 | * 42 | * ===========================(LICENSE END)============================= 43 | * 44 | * @file sph_whirlpool.h 45 | * @author Thomas Pornin 46 | */ 47 | 48 | #ifndef SPH_WHIRLPOOL_H__ 49 | #define SPH_WHIRLPOOL_H__ 50 | 51 | #include 52 | #include "sph_types.h" 53 | 54 | #if SPH_64 55 | 56 | /** 57 | * Output size (in bits) for WHIRLPOOL. 58 | */ 59 | #define SPH_SIZE_whirlpool 512 60 | 61 | /** 62 | * Output size (in bits) for WHIRLPOOL-0. 63 | */ 64 | #define SPH_SIZE_whirlpool0 512 65 | 66 | /** 67 | * Output size (in bits) for WHIRLPOOL-1. 68 | */ 69 | #define SPH_SIZE_whirlpool1 512 70 | 71 | /** 72 | * This structure is a context for WHIRLPOOL computations: it contains the 73 | * intermediate values and some data from the last entered block. Once 74 | * a WHIRLPOOL computation has been performed, the context can be reused for 75 | * another computation. 76 | * 77 | * The contents of this structure are private. A running WHIRLPOOL computation 78 | * can be cloned by copying the context (e.g. with a simple 79 | * memcpy()). 80 | */ 81 | typedef struct { 82 | #ifndef DOXYGEN_IGNORE 83 | unsigned char buf[64]; /* first field, for alignment */ 84 | sph_u64 state[8]; 85 | #if SPH_64 86 | sph_u64 count; 87 | #else 88 | sph_u32 count_high, count_low; 89 | #endif 90 | #endif 91 | } sph_whirlpool_context; 92 | 93 | /** 94 | * Initialize a WHIRLPOOL context. This process performs no memory allocation. 95 | * 96 | * @param cc the WHIRLPOOL context (pointer to a 97 | * sph_whirlpool_context) 98 | */ 99 | void sph_whirlpool_init(void *cc); 100 | 101 | /** 102 | * Process some data bytes. It is acceptable that len is zero 103 | * (in which case this function does nothing). This function applies the 104 | * plain WHIRLPOOL algorithm. 105 | * 106 | * @param cc the WHIRLPOOL context 107 | * @param data the input data 108 | * @param len the input data length (in bytes) 109 | */ 110 | void sph_whirlpool(void *cc, const void *data, size_t len); 111 | 112 | /** 113 | * Terminate the current WHIRLPOOL computation and output the result into the 114 | * provided buffer. The destination buffer must be wide enough to 115 | * accomodate the result (64 bytes). The context is automatically 116 | * reinitialized. 117 | * 118 | * @param cc the WHIRLPOOL context 119 | * @param dst the destination buffer 120 | */ 121 | void sph_whirlpool_close(void *cc, void *dst); 122 | 123 | /** 124 | * WHIRLPOOL-0 uses the same structure than plain WHIRLPOOL. 125 | */ 126 | typedef sph_whirlpool_context sph_whirlpool0_context; 127 | 128 | #ifdef DOXYGEN_IGNORE 129 | /** 130 | * Initialize a WHIRLPOOL-0 context. This function is identical to 131 | * sph_whirlpool_init(). 132 | * 133 | * @param cc the WHIRLPOOL context (pointer to a 134 | * sph_whirlpool0_context) 135 | */ 136 | void sph_whirlpool0_init(void *cc); 137 | #endif 138 | 139 | #ifndef DOXYGEN_IGNORE 140 | #define sph_whirlpool0_init sph_whirlpool_init 141 | #endif 142 | 143 | /** 144 | * Process some data bytes. It is acceptable that len is zero 145 | * (in which case this function does nothing). This function applies the 146 | * WHIRLPOOL-0 algorithm. 147 | * 148 | * @param cc the WHIRLPOOL context 149 | * @param data the input data 150 | * @param len the input data length (in bytes) 151 | */ 152 | void sph_whirlpool0(void *cc, const void *data, size_t len); 153 | 154 | /** 155 | * Terminate the current WHIRLPOOL-0 computation and output the result into the 156 | * provided buffer. The destination buffer must be wide enough to 157 | * accomodate the result (64 bytes). The context is automatically 158 | * reinitialized. 159 | * 160 | * @param cc the WHIRLPOOL-0 context 161 | * @param dst the destination buffer 162 | */ 163 | void sph_whirlpool0_close(void *cc, void *dst); 164 | 165 | /** 166 | * WHIRLPOOL-1 uses the same structure than plain WHIRLPOOL. 167 | */ 168 | typedef sph_whirlpool_context sph_whirlpool1_context; 169 | 170 | #ifdef DOXYGEN_IGNORE 171 | /** 172 | * Initialize a WHIRLPOOL-1 context. This function is identical to 173 | * sph_whirlpool_init(). 174 | * 175 | * @param cc the WHIRLPOOL context (pointer to a 176 | * sph_whirlpool1_context) 177 | */ 178 | void sph_whirlpool1_init(void *cc); 179 | #endif 180 | 181 | #ifndef DOXYGEN_IGNORE 182 | #define sph_whirlpool1_init sph_whirlpool_init 183 | #endif 184 | 185 | /** 186 | * Process some data bytes. It is acceptable that len is zero 187 | * (in which case this function does nothing). This function applies the 188 | * WHIRLPOOL-1 algorithm. 189 | * 190 | * @param cc the WHIRLPOOL context 191 | * @param data the input data 192 | * @param len the input data length (in bytes) 193 | */ 194 | void sph_whirlpool1(void *cc, const void *data, size_t len); 195 | 196 | /** 197 | * Terminate the current WHIRLPOOL-1 computation and output the result into the 198 | * provided buffer. The destination buffer must be wide enough to 199 | * accomodate the result (64 bytes). The context is automatically 200 | * reinitialized. 201 | * 202 | * @param cc the WHIRLPOOL-1 context 203 | * @param dst the destination buffer 204 | */ 205 | void sph_whirlpool1_close(void *cc, void *dst); 206 | 207 | #endif 208 | 209 | #endif -------------------------------------------------------------------------------- /x17r/x17r.c: -------------------------------------------------------------------------------- 1 | /** 2 | * x17r algo implementation 3 | * 4 | * modifyed by wubei@fusionsilicon.com 2018 5 | */ 6 | 7 | #include 8 | #include 9 | #include "compat.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "sph_blake.h" 16 | #include "sph_bmw.h" 17 | #include "sph_groestl.h" 18 | #include "sph_jh.h" 19 | #include "sph_keccak.h" 20 | #include "sph_skein.h" 21 | #include "sph_luffa.h" 22 | #include "sph_cubehash.h" 23 | #include "sph_shavite.h" 24 | #include "sph_simd.h" 25 | #include "sph_echo.h" 26 | #include "sph_hamsi.h" 27 | #include "sph_fugue.h" 28 | #include "sph_shabal.h" 29 | #include "sph_whirlpool.h" 30 | #include "sph_sha2.h" 31 | #include "sph_haval.h" 32 | 33 | enum Algo { 34 | BLAKE = 0, 35 | BMW, 36 | GROESTL, 37 | JH, 38 | KECCAK, 39 | SKEIN, 40 | LUFFA, 41 | CUBEHASH, 42 | SHAVITE, 43 | SIMD, 44 | ECHO, 45 | HAMSI, 46 | FUGUE, 47 | SHABAL, 48 | WHIRLPOOL, 49 | SHA512, 50 | HAVAL,// 51 | HASH_FUNC_COUNT 52 | }; 53 | 54 | static void getAlgoString(const uint8_t* prevblock, char *output) 55 | { 56 | char *sptr = output; 57 | for (int j = 0; j < HASH_FUNC_COUNT; j++) { 58 | //uint8_t b = (16 - j) >> 1; // 16 first ascii hex chars (lsb in uint256) 59 | //printf ("the prevblock is %d\n",prevblock[j]); 60 | //uint8_t algoDigit = (j & 1) ? (prevblock[b] & 0xF) : prevblock[b] >> 4;// 61 | uint8_t algoDigit = prevblock[j] % HASH_FUNC_COUNT; 62 | 63 | //printf ("the algoDigit is %d\n",algoDigit); 64 | if (algoDigit >= 10) 65 | sprintf(sptr, "%c", 'A' + (algoDigit - 10)); 66 | else 67 | sprintf(sptr, "%u", (uint32_t) algoDigit); 68 | sptr++; 69 | } 70 | *sptr = '\0'; 71 | } 72 | 73 | 74 | //uint32_t s_ntime = UINT32_MAX; 75 | void x17r_hash(void* output, const void* input, const int in_len) 76 | { 77 | uint32_t s_ntime = UINT32_MAX; 78 | char hashOrder[HASH_FUNC_COUNT + 1] = { 0 }; 79 | 80 | char* bytes_swap = NULL; 81 | bytes_swap = (char *)malloc(in_len); 82 | if (in_len >= 180) { 83 | memcpy(bytes_swap, (char *)input, 140); 84 | memcpy(bytes_swap+140, (char *)input+144, in_len-144); 85 | memcpy(bytes_swap+in_len-4, (char *)input+140, 4); 86 | } else { 87 | memcpy(bytes_swap, (char *)input, in_len); 88 | } 89 | 90 | uint32_t _ALIGN(128) hash[64/4]; 91 | 92 | sph_blake512_context ctx_blake; 93 | sph_bmw512_context ctx_bmw; 94 | sph_groestl512_context ctx_groestl; 95 | sph_skein512_context ctx_skein; 96 | sph_jh512_context ctx_jh; 97 | sph_keccak512_context ctx_keccak; 98 | sph_luffa512_context ctx_luffa1; 99 | sph_cubehash512_context ctx_cubehash1; 100 | sph_shavite512_context ctx_shavite1; 101 | sph_simd512_context ctx_simd1; 102 | sph_echo512_context ctx_echo1; 103 | sph_hamsi512_context ctx_hamsi1; 104 | sph_fugue512_context ctx_fugue1; 105 | sph_shabal512_context ctx_shabal1; 106 | sph_whirlpool_context ctx_whirlpool1; 107 | sph_sha512_context ctx_sha512; 108 | sph_haval256_5_context ctx_haval;// 109 | 110 | memset((char*)&hash, 0, 64); 111 | void *in = (void*) bytes_swap; 112 | int size = in_len; 113 | 114 | if (s_ntime == UINT32_MAX) { 115 | const uint8_t* in8 = (uint8_t*) bytes_swap; 116 | getAlgoString(&in8[4], hashOrder); 117 | } 118 | 119 | for (int i = 0; i < 17; i++)// 120 | { 121 | const char elem = hashOrder[i]; 122 | const uint8_t algo = elem >= 'A' ? elem - 'A' + 10 : elem - '0'; 123 | //printf ("the algo is %d\n",algo); 124 | switch (algo) { 125 | case BLAKE: 126 | sph_blake512_init(&ctx_blake); 127 | sph_blake512(&ctx_blake, in, size); 128 | sph_blake512_close(&ctx_blake, hash); 129 | break; 130 | case BMW: 131 | sph_bmw512_init(&ctx_bmw); 132 | sph_bmw512(&ctx_bmw, in, size); 133 | sph_bmw512_close(&ctx_bmw, hash); 134 | break; 135 | case GROESTL: 136 | sph_groestl512_init(&ctx_groestl); 137 | sph_groestl512(&ctx_groestl, in, size); 138 | sph_groestl512_close(&ctx_groestl, hash); 139 | break; 140 | case SKEIN: 141 | sph_skein512_init(&ctx_skein); 142 | sph_skein512(&ctx_skein, in, size); 143 | sph_skein512_close(&ctx_skein, hash); 144 | break; 145 | case JH: 146 | sph_jh512_init(&ctx_jh); 147 | sph_jh512(&ctx_jh, in, size); 148 | sph_jh512_close(&ctx_jh, hash); 149 | break; 150 | case KECCAK: 151 | sph_keccak512_init(&ctx_keccak); 152 | sph_keccak512(&ctx_keccak, in, size); 153 | sph_keccak512_close(&ctx_keccak, hash); 154 | break; 155 | case LUFFA: 156 | sph_luffa512_init(&ctx_luffa1); 157 | sph_luffa512(&ctx_luffa1, in, size); 158 | sph_luffa512_close(&ctx_luffa1, hash); 159 | break; 160 | case CUBEHASH: 161 | sph_cubehash512_init(&ctx_cubehash1); 162 | sph_cubehash512(&ctx_cubehash1, in, size); 163 | sph_cubehash512_close(&ctx_cubehash1, hash); 164 | break; 165 | case SHAVITE: 166 | sph_shavite512_init(&ctx_shavite1); 167 | sph_shavite512(&ctx_shavite1, in, size); 168 | sph_shavite512_close(&ctx_shavite1, hash); 169 | break; 170 | case SIMD: 171 | sph_simd512_init(&ctx_simd1); 172 | sph_simd512(&ctx_simd1, in, size); 173 | sph_simd512_close(&ctx_simd1, hash); 174 | break; 175 | case ECHO: 176 | sph_echo512_init(&ctx_echo1); 177 | sph_echo512(&ctx_echo1, in, size); 178 | sph_echo512_close(&ctx_echo1, hash); 179 | break; 180 | case HAMSI: 181 | sph_hamsi512_init(&ctx_hamsi1); 182 | sph_hamsi512(&ctx_hamsi1, in, size); 183 | sph_hamsi512_close(&ctx_hamsi1, hash); 184 | break; 185 | case FUGUE: 186 | sph_fugue512_init(&ctx_fugue1); 187 | sph_fugue512(&ctx_fugue1, in, size); 188 | sph_fugue512_close(&ctx_fugue1, hash); 189 | break; 190 | case SHABAL: 191 | sph_shabal512_init(&ctx_shabal1); 192 | sph_shabal512(&ctx_shabal1, in, size); 193 | sph_shabal512_close(&ctx_shabal1, hash); 194 | break; 195 | case WHIRLPOOL: 196 | sph_whirlpool_init(&ctx_whirlpool1); 197 | sph_whirlpool(&ctx_whirlpool1, in, size); 198 | sph_whirlpool_close(&ctx_whirlpool1, hash); 199 | break; 200 | case SHA512: 201 | sph_sha512_init(&ctx_sha512); 202 | sph_sha512(&ctx_sha512,(const void*) in, size); 203 | sph_sha512_close(&ctx_sha512,(void*) hash); 204 | break; 205 | case HAVAL: 206 | sph_haval256_5_init(&ctx_haval); 207 | sph_haval256_5(&ctx_haval, (const void*)in, size); 208 | sph_haval256_5_close(&ctx_haval, hash); 209 | memset(hash+8,0x00000000,32); 210 | break; 211 | } 212 | in = (void*) hash; 213 | size = 64; 214 | } 215 | memcpy(output, hash, 32); 216 | free(bytes_swap); 217 | } 218 | /* 219 | int main(int argc, char** argv) { 220 | uint8_t md[32]; 221 | uint8_t in[80] = { 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41, 222 | 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41, 223 | 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41, 224 | 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41}; 225 | x17r_hash(md , in ); 226 | printf("the hash out is \n"); 227 | for (int i=0; i < 32; ++i) { 228 | printf("%02x",md[i]); 229 | } 230 | printf("\n"); 231 | return 0; 232 | } 233 | */ 234 | -------------------------------------------------------------------------------- /x17r/x17r.h: -------------------------------------------------------------------------------- 1 | #ifndef X17R_H__ 2 | #define X17R_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C"{ 6 | #endif 7 | 8 | #include 9 | 10 | #include 11 | #include "sph_types.h" 12 | 13 | static void getAlgoString(const uint8_t* prevblock, char *output); 14 | 15 | void x17r_hash(void* output, const void* input, const int in_len); 16 | 17 | #ifdef __cplusplus 18 | } 19 | #endif 20 | 21 | #endif 22 | --------------------------------------------------------------------------------