mirror of https://gogs.blitter.com/RLabs/xs
				
				
				
			Experimental cipher/hmac alg change on rekey
This commit is contained in:
		
							parent
							
								
									e82d968381
								
							
						
					
					
						commit
						8698c7e827
					
				
							
								
								
									
										2
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										2
									
								
								Makefile
								
								
								
								
							| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
VERSION := 0.9.6
 | 
					VERSION := 0.9.7-rc
 | 
				
			||||||
.PHONY: lint vis clean common client server passwd\
 | 
					.PHONY: lint vis clean common client server passwd\
 | 
				
			||||||
 subpkgs install uninstall reinstall scc
 | 
					 subpkgs install uninstall reinstall scc
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										5
									
								
								xs/xs.go
								
								
								
								
							
							
						
						
									
										5
									
								
								xs/xs.go
								
								
								
								
							| 
						 | 
					@ -702,6 +702,7 @@ func main() { //nolint: funlen, gocyclo
 | 
				
			||||||
		cmdStr        string
 | 
							cmdStr        string
 | 
				
			||||||
		tunSpecStr    string // lport1:rport1[,lport2:rport2,...]
 | 
							tunSpecStr    string // lport1:rport1[,lport2:rport2,...]
 | 
				
			||||||
		rekeySecs     uint
 | 
							rekeySecs     uint
 | 
				
			||||||
 | 
							remodulate    bool // true: when rekeying, switch to random cipher/hmac alg
 | 
				
			||||||
		copySrc       []byte
 | 
							copySrc       []byte
 | 
				
			||||||
		copyDst       string
 | 
							copyDst       string
 | 
				
			||||||
		copyQuiet     bool
 | 
							copyQuiet     bool
 | 
				
			||||||
| 
						 | 
					@ -747,6 +748,7 @@ func main() { //nolint: funlen, gocyclo
 | 
				
			||||||
	flag.StringVar(&kcpMode, "K", "unused", "KCP `alg`, one of [KCP_NONE | KCP_AES | KCP_BLOWFISH | KCP_CAST5 | KCP_SM4 | KCP_SALSA20 | KCP_SIMPLEXOR | KCP_TEA | KCP_3DES | KCP_TWOFISH | KCP_XTEA] to use KCP (github.com/xtaci/kcp-go) reliable UDP instead of TCP") //nolint:lll
 | 
						flag.StringVar(&kcpMode, "K", "unused", "KCP `alg`, one of [KCP_NONE | KCP_AES | KCP_BLOWFISH | KCP_CAST5 | KCP_SM4 | KCP_SALSA20 | KCP_SIMPLEXOR | KCP_TEA | KCP_3DES | KCP_TWOFISH | KCP_XTEA] to use KCP (github.com/xtaci/kcp-go) reliable UDP instead of TCP") //nolint:lll
 | 
				
			||||||
	flag.UintVar(&port, "p", 2000, "``port")                                                                                                                                                                                                                            //nolint:gomnd,lll
 | 
						flag.UintVar(&port, "p", 2000, "``port")                                                                                                                                                                                                                            //nolint:gomnd,lll
 | 
				
			||||||
	flag.UintVar(&rekeySecs, "r", 300, "rekey interval in `secs`")
 | 
						flag.UintVar(&rekeySecs, "r", 300, "rekey interval in `secs`")
 | 
				
			||||||
 | 
						flag.BoolVar(&remodulate, "R", false, "Borg Countermeasures (remodulate cipher/hmac alg on each rekey)")
 | 
				
			||||||
	//nolint:gocritic,nolintlint // flag.StringVar(&authCookie, "a", "", "auth cookie")
 | 
						//nolint:gocritic,nolintlint // flag.StringVar(&authCookie, "a", "", "auth cookie")
 | 
				
			||||||
	flag.BoolVar(&chaffEnabled, "e", true, "enable chaff pkts")
 | 
						flag.BoolVar(&chaffEnabled, "e", true, "enable chaff pkts")
 | 
				
			||||||
	flag.UintVar(&chaffFreqMin, "f", 100, "chaff pkt freq min `msecs`")  //nolint:gomnd
 | 
						flag.UintVar(&chaffFreqMin, "f", 100, "chaff pkt freq min `msecs`")  //nolint:gomnd
 | 
				
			||||||
| 
						 | 
					@ -973,6 +975,9 @@ func main() { //nolint: funlen, gocyclo
 | 
				
			||||||
		exitWithStatus(XSNetDialFailed)
 | 
							exitWithStatus(XSNetDialFailed)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if remodulate {
 | 
				
			||||||
 | 
							conn.SetOpts(xsnet.CORemodulateShields)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	conn.RekeyHelper(rekeySecs)
 | 
						conn.RekeyHelper(rekeySecs)
 | 
				
			||||||
	defer conn.ShutdownRekey()
 | 
						defer conn.ShutdownRekey()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -530,11 +530,13 @@ func main() { //nolint:funlen,gocyclo
 | 
				
			||||||
	var dbg bool
 | 
						var dbg bool
 | 
				
			||||||
	var laddr string
 | 
						var laddr string
 | 
				
			||||||
	var rekeySecs uint
 | 
						var rekeySecs uint
 | 
				
			||||||
 | 
						var remodulate bool // true: when rekeying, switch to random cipher/hmac alg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var useSystemPasswd bool
 | 
						var useSystemPasswd bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flag.BoolVar(&vopt, "v", false, "show version")
 | 
						flag.BoolVar(&vopt, "v", false, "show version")
 | 
				
			||||||
	flag.UintVar(&rekeySecs, "r", 300, "rekey interval in `secs`")
 | 
						flag.UintVar(&rekeySecs, "r", 300, "rekey interval in `secs`")
 | 
				
			||||||
 | 
						flag.BoolVar(&remodulate, "R", false, "Borg Countermeasures (remodulate cipher/hmac alg on each rekey)")
 | 
				
			||||||
	flag.StringVar(&laddr, "l", ":2000", "interface[:port] to listen")                                                                                                                                                                                                         //nolint:gomnd,lll
 | 
						flag.StringVar(&laddr, "l", ":2000", "interface[:port] to listen")                                                                                                                                                                                                         //nolint:gomnd,lll
 | 
				
			||||||
	flag.StringVar(&kcpMode, "K", "unused", `set to one of ["KCP_NONE","KCP_AES", "KCP_BLOWFISH", "KCP_CAST5", "KCP_SM4", "KCP_SALSA20", "KCP_SIMPLEXOR", "KCP_TEA", "KCP_3DES", "KCP_TWOFISH", "KCP_XTEA"] to use KCP (github.com/xtaci/kcp-go) reliable UDP instead of TCP`) //nolint:lll
 | 
						flag.StringVar(&kcpMode, "K", "unused", `set to one of ["KCP_NONE","KCP_AES", "KCP_BLOWFISH", "KCP_CAST5", "KCP_SM4", "KCP_SALSA20", "KCP_SIMPLEXOR", "KCP_TEA", "KCP_3DES", "KCP_TWOFISH", "KCP_XTEA"] to use KCP (github.com/xtaci/kcp-go) reliable UDP instead of TCP`) //nolint:lll
 | 
				
			||||||
	flag.BoolVar(&useSysLogin, "L", false, "use system login")
 | 
						flag.BoolVar(&useSysLogin, "L", false, "use system login")
 | 
				
			||||||
| 
						 | 
					@ -702,6 +704,9 @@ func main() { //nolint:funlen,gocyclo
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				log.Println("Accepted client")
 | 
									log.Println("Accepted client")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if remodulate {
 | 
				
			||||||
 | 
										conn.SetOpts(xsnet.CORemodulateShields)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				conn.RekeyHelper(rekeySecs)
 | 
									conn.RekeyHelper(rekeySecs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Set up chaffing to client
 | 
									// Set up chaffing to client
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,6 +22,7 @@ import (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"blitter.com/go/cryptmt"
 | 
						"blitter.com/go/cryptmt"
 | 
				
			||||||
	"blitter.com/go/hopscotch"
 | 
						"blitter.com/go/hopscotch"
 | 
				
			||||||
 | 
						"blitter.com/go/xs/logger"
 | 
				
			||||||
	"github.com/aead/chacha20/chacha"
 | 
						"github.com/aead/chacha20/chacha"
 | 
				
			||||||
	"golang.org/x/crypto/blowfish"
 | 
						"golang.org/x/crypto/blowfish"
 | 
				
			||||||
	"golang.org/x/crypto/twofish"
 | 
						"golang.org/x/crypto/twofish"
 | 
				
			||||||
| 
						 | 
					@ -57,9 +58,19 @@ func expandKeyMat(keymat []byte, blocksize int) []byte {
 | 
				
			||||||
	return keymat
 | 
						return keymat
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* (Re-)initialize the keystream and hmac state for an xsnet.Conn, returning
 | 
					// Choose a cipher and hmac alg from supported sets, given two uint8 values
 | 
				
			||||||
   a cipherStream and hash
 | 
					func getNewStreamAlgs(cb uint8, hb uint8) (config uint32) {
 | 
				
			||||||
 */
 | 
						// Get new cipher and hash algs (clamped to valid values) based on
 | 
				
			||||||
 | 
						// the input rekeying data
 | 
				
			||||||
 | 
						c := (cb % CAlgNoneDisallowed)
 | 
				
			||||||
 | 
						h := (hb % HmacNoneDisallowed)
 | 
				
			||||||
 | 
						config = uint32(h<<8) | uint32(c)
 | 
				
			||||||
 | 
						logger.LogDebug(fmt.Sprintf("[Chose new algs [%d:%d]", h, c))
 | 
				
			||||||
 | 
						return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// (Re-)initialize the keystream and hmac state for an xsnet.Conn, returning
 | 
				
			||||||
 | 
					// a cipherStream and hash
 | 
				
			||||||
func (hc *Conn) getStream(keymat []byte) (rc cipher.Stream, mc hash.Hash, err error) {
 | 
					func (hc *Conn) getStream(keymat []byte) (rc cipher.Stream, mc hash.Hash, err error) {
 | 
				
			||||||
	var key []byte
 | 
						var key []byte
 | 
				
			||||||
	var block cipher.Block
 | 
						var block cipher.Block
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -122,5 +122,12 @@ const (
 | 
				
			||||||
	HmacNoneDisallowed
 | 
						HmacNoneDisallowed
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Conn opts outside of basic kex/cipher/hmac connect config
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						CONone = iota
 | 
				
			||||||
 | 
						CORemodulateShields  // if set, rekeying also reselects random cipher/hmac alg
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					type COValue uint32
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Available HMACs for hkex.Conn
 | 
					// Available HMACs for hkex.Conn
 | 
				
			||||||
type CSHmacAlg uint32
 | 
					type CSHmacAlg uint32
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1351,6 +1351,9 @@ func (hc *Conn) Read(b []byte) (n int, err error) {
 | 
				
			||||||
				//logger.LogDebug(fmt.Sprintf("[Got rekey [%02x %02x %02x ...]\n",
 | 
									//logger.LogDebug(fmt.Sprintf("[Got rekey [%02x %02x %02x ...]\n",
 | 
				
			||||||
				//	payloadBytes[0], payloadBytes[1], payloadBytes[2]))
 | 
									//	payloadBytes[0], payloadBytes[1], payloadBytes[2]))
 | 
				
			||||||
				rekeyData := payloadBytes
 | 
									rekeyData := payloadBytes
 | 
				
			||||||
 | 
									if hc.opts&CORemodulateShields != 0 {
 | 
				
			||||||
 | 
										hc.cipheropts = getNewStreamAlgs(rekeyData[0], rekeyData[1])
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				hc.r, hc.rm, err = hc.getStream(rekeyData)
 | 
									hc.r, hc.rm, err = hc.getStream(rekeyData)
 | 
				
			||||||
			case CSOTermSize:
 | 
								case CSOTermSize:
 | 
				
			||||||
				fmt.Sscanf(string(payloadBytes), "%d %d", &hc.Rows, &hc.Cols)
 | 
									fmt.Sscanf(string(payloadBytes), "%d %d", &hc.Rows, &hc.Cols)
 | 
				
			||||||
| 
						 | 
					@ -1615,6 +1618,9 @@ func (hc *Conn) RekeyHelper(intervalSecs uint) {
 | 
				
			||||||
				//logger.LogDebug("[+rekeyHelper]")
 | 
									//logger.LogDebug("[+rekeyHelper]")
 | 
				
			||||||
				_, err = hc.WritePacket(rekeyData, CSORekey)
 | 
									_, err = hc.WritePacket(rekeyData, CSORekey)
 | 
				
			||||||
				hc.Lock()
 | 
									hc.Lock()
 | 
				
			||||||
 | 
									if hc.opts&CORemodulateShields != 0 {
 | 
				
			||||||
 | 
										hc.cipheropts = getNewStreamAlgs(rekeyData[0], rekeyData[1])
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				hc.w, hc.wm, err = hc.getStream(rekeyData)
 | 
									hc.w, hc.wm, err = hc.getStream(rekeyData)
 | 
				
			||||||
				//logger.LogDebug("[-rekeyHelper]")
 | 
									//logger.LogDebug("[-rekeyHelper]")
 | 
				
			||||||
				hc.Unlock()
 | 
									hc.Unlock()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue