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-13 18:01:27 +00:00
|
|
|
established with FA exchange and support channel operations
|
|
|
|
(echo, file-copy, remote-cmd, ...) */
|
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
|
|
|
|
2018-01-12 07:01:39 +00:00
|
|
|
"golang.org/x/crypto/blowfish"
|
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-13 06:13:01 +00:00
|
|
|
CAlgAES256 = iota
|
|
|
|
CAlgTwofish128 // golang.org/x/crypto/twofish
|
|
|
|
CAlgBlowfish64 // golang.org/x/crypto/blowfish
|
|
|
|
CAlgNoneDisallowed
|
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-13 06:13:01 +00:00
|
|
|
HmacSHA256 = iota
|
|
|
|
HmacNoneDisallowed
|
2018-01-09 04:23:19 +00:00
|
|
|
)
|
|
|
|
|
2018-01-13 18:01:27 +00:00
|
|
|
type ChanOp uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChanOpNop = '.'
|
|
|
|
ChanOpEcho = 'e' // For testing - echo client data to stderr
|
|
|
|
//ChanOpFileWrite = "w"
|
|
|
|
//ChanOpFileRead = "r"
|
|
|
|
//ChanOpRemoteCmd = "x"
|
|
|
|
)
|
|
|
|
|
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-12 07:01:39 +00:00
|
|
|
var ivlen int
|
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
|
2018-01-12 07:01:39 +00:00
|
|
|
// TODO: each cipher alg case should ensure len(keymat.Bytes())
|
|
|
|
// is >= 2*cipher.BlockSize (enough for both key and iv)
|
2018-01-12 03:42:42 +00:00
|
|
|
switch copts {
|
2018-01-13 06:13:01 +00:00
|
|
|
case CAlgAES256:
|
2018-01-09 04:23:19 +00:00
|
|
|
key = keymat.Bytes()[0:aes.BlockSize]
|
2018-01-09 07:08:58 +00:00
|
|
|
block, err = aes.NewCipher(key)
|
2018-01-12 07:01:39 +00:00
|
|
|
ivlen = aes.BlockSize
|
2018-01-13 06:13:01 +00:00
|
|
|
iv := keymat.Bytes()[aes.BlockSize : aes.BlockSize+ivlen]
|
2018-01-11 06:50:13 +00:00
|
|
|
ret = cipher.NewOFB(block, iv)
|
2018-01-12 03:42:42 +00:00
|
|
|
fmt.Printf("[cipher AES_256 (%d)]\n", copts)
|
|
|
|
break
|
2018-01-13 06:13:01 +00:00
|
|
|
case CAlgTwofish128:
|
2018-01-12 03:42:42 +00:00
|
|
|
key = keymat.Bytes()[0:twofish.BlockSize]
|
|
|
|
block, err = twofish.NewCipher(key)
|
2018-01-12 07:01:39 +00:00
|
|
|
ivlen = twofish.BlockSize
|
2018-01-13 06:13:01 +00:00
|
|
|
iv := keymat.Bytes()[twofish.BlockSize : twofish.BlockSize+ivlen]
|
2018-01-12 03:42:42 +00:00
|
|
|
ret = cipher.NewOFB(block, iv)
|
2018-01-12 07:01:39 +00:00
|
|
|
fmt.Printf("[cipher TWOFISH_128 (%d)]\n", copts)
|
|
|
|
break
|
2018-01-13 06:13:01 +00:00
|
|
|
case CAlgBlowfish64:
|
2018-01-12 07:01:39 +00:00
|
|
|
key = keymat.Bytes()[0:blowfish.BlockSize]
|
|
|
|
block, err = blowfish.NewCipher(key)
|
|
|
|
ivlen = blowfish.BlockSize
|
|
|
|
// N.b. Bounds enforcement of differing cipher algorithms
|
|
|
|
// ------------------------------------------------------
|
|
|
|
// cipher/aes and x/cipher/twofish appear to allow one to
|
|
|
|
// pass an iv larger than the blockSize harmlessly to
|
|
|
|
// cipher.NewOFB(); x/cipher/blowfish implementation will
|
|
|
|
// segfault here if len(iv) is not exactly blowfish.BlockSize.
|
|
|
|
//
|
|
|
|
// I assume the other two check bounds and only
|
|
|
|
// copy what's needed whereas blowfish does no such check.
|
2018-01-13 06:13:01 +00:00
|
|
|
iv := keymat.Bytes()[blowfish.BlockSize : blowfish.BlockSize+ivlen]
|
2018-01-12 07:01:39 +00:00
|
|
|
ret = cipher.NewOFB(block, iv)
|
|
|
|
fmt.Printf("[cipher BLOWFISH_64 (%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-09 07:08:58 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-01-12 03:42:42 +00:00
|
|
|
hopts := (hc.cipheropts >> 8) & 0xFF
|
|
|
|
switch hopts {
|
2018-01-13 06:13:01 +00:00
|
|
|
case HmacSHA256:
|
|
|
|
fmt.Printf("[nop HmacSHA256 (%d)]\n", hopts)
|
2018-01-12 03:42:42 +00:00
|
|
|
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
|
|
|
|
}
|