2018-01-09 04:23:19 +00:00
|
|
|
/* Herradura - a Key exchange scheme in the style of Diffie-Hellman Key Exchange.
|
|
|
|
Copyright (C) 2017 Omar Alejandro Herrera Reyna
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
golang implementation by Russ Magee (rmagee_at_gmail.com) */
|
2018-01-11 21:44:11 +00:00
|
|
|
|
2018-01-09 04:23:19 +00:00
|
|
|
package herradurakex
|
|
|
|
|
2018-01-09 07:08:58 +00:00
|
|
|
/* Support functions to set up encryption once an HKEx Conn has been
|
2018-01-11 06:50:13 +00:00
|
|
|
established with FA exchange */
|
2018-01-09 07:08:58 +00:00
|
|
|
|
2018-01-09 04:23:19 +00:00
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2018-01-12 03:42:42 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/twofish"
|
2018-01-09 04:23:19 +00:00
|
|
|
)
|
|
|
|
|
2018-01-11 21:44:11 +00:00
|
|
|
// Available ciphers for hkex.Conn
|
2018-01-09 04:23:19 +00:00
|
|
|
const (
|
2018-01-12 03:42:42 +00:00
|
|
|
C_AES_256 = iota
|
|
|
|
C_TWOFISH_128 // golang.org/x/crypto/twofish
|
|
|
|
C_NONE_DISALLOWED
|
2018-01-09 04:23:19 +00:00
|
|
|
)
|
|
|
|
|
2018-01-11 21:44:11 +00:00
|
|
|
// Available HMACs for hkex.Conn (TODO: not currently used)
|
2018-01-09 04:23:19 +00:00
|
|
|
const (
|
2018-01-12 03:42:42 +00:00
|
|
|
H_BOGUS = iota
|
|
|
|
H_SHA256
|
|
|
|
H_NONE_DISALLOWED
|
2018-01-09 04:23:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*TODO: HMAC derived from HKEx FA.*/
|
2018-01-09 07:08:58 +00:00
|
|
|
/* Support functionality to set up encryption after a channel has
|
|
|
|
been negotiated via hkexnet.go
|
2018-01-09 04:23:19 +00:00
|
|
|
*/
|
2018-01-12 03:42:42 +00:00
|
|
|
func (hc Conn) getStream(keymat *big.Int) (ret cipher.Stream) {
|
2018-01-09 04:23:19 +00:00
|
|
|
var key []byte
|
|
|
|
var block cipher.Block
|
2018-01-09 07:08:58 +00:00
|
|
|
var err error
|
2018-01-09 04:23:19 +00:00
|
|
|
|
2018-01-12 03:42:42 +00:00
|
|
|
copts := hc.cipheropts & 0xFF
|
|
|
|
switch copts {
|
2018-01-09 04:23:19 +00:00
|
|
|
case C_AES_256:
|
|
|
|
key = keymat.Bytes()[0:aes.BlockSize]
|
2018-01-09 07:08:58 +00:00
|
|
|
block, err = aes.NewCipher(key)
|
2018-01-11 06:50:13 +00:00
|
|
|
iv := make([]byte, aes.BlockSize)
|
|
|
|
//if _, err = io.ReadFull(crand.Reader, iv); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
//}
|
|
|
|
iv = keymat.Bytes()[aes.BlockSize:]
|
|
|
|
ret = cipher.NewOFB(block, iv)
|
2018-01-12 03:42:42 +00:00
|
|
|
fmt.Printf("[cipher AES_256 (%d)]\n", copts)
|
|
|
|
break
|
|
|
|
case C_TWOFISH_128:
|
|
|
|
key = keymat.Bytes()[0:twofish.BlockSize]
|
|
|
|
block, err = twofish.NewCipher(key)
|
|
|
|
iv := make([]byte, twofish.BlockSize)
|
|
|
|
//if _, err = io.ReadFull(crand.Reader, iv); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
//}
|
|
|
|
iv = keymat.Bytes()[twofish.BlockSize:]
|
|
|
|
ret = cipher.NewOFB(block, iv)
|
|
|
|
fmt.Printf("[cipher TWOFISH_256 (%d)]\n", copts)
|
2018-01-09 04:23:19 +00:00
|
|
|
break
|
|
|
|
default:
|
2018-01-12 03:42:42 +00:00
|
|
|
fmt.Printf("DOOFUS SET A VALID CIPHER ALG (%d)\n", copts)
|
2018-01-11 06:50:13 +00:00
|
|
|
block, err = nil, nil
|
2018-01-09 07:08:58 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-01-12 03:42:42 +00:00
|
|
|
hopts := (hc.cipheropts >> 8) & 0xFF
|
|
|
|
switch hopts {
|
|
|
|
case H_BOGUS:
|
|
|
|
fmt.Printf("[nop H_BOGUS (%d)]\n", hopts)
|
|
|
|
break
|
|
|
|
case H_SHA256:
|
|
|
|
fmt.Printf("[nop H_SHA256 (%d)]\n", hopts)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Printf("DOOFUS SET A VALID HMAC ALG (%d)\n", hopts)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-01-09 07:08:58 +00:00
|
|
|
if err != nil {
|
2018-01-09 04:23:19 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|