github.com/iDigitalFlame/xmt@v0.5.4/c2/x_ews.go (about)

     1  //go:build ews && implant
     2  // +build ews,implant
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package c2
    21  
    22  import (
    23  	"unsafe"
    24  
    25  	"github.com/iDigitalFlame/xmt/data/crypto/subtle"
    26  	"github.com/iDigitalFlame/xmt/util"
    27  )
    28  
    29  type header struct {
    30  	Data unsafe.Pointer
    31  	Len  int
    32  	Cap  int
    33  }
    34  type container struct {
    35  	// Alloc free in-memory crypt.
    36  	v []byte
    37  	k [16]byte
    38  }
    39  
    40  func (c *container) Wrap() {
    41  	for i := 0; i < 16; i++ {
    42  		c.k[i] = byte(util.FastRand())
    43  	}
    44  	if c.k[0] == 0 {
    45  		c.k[0] = 1
    46  	}
    47  	subtle.XorOp(c.v, c.k[:])
    48  }
    49  func (c *container) Unwrap() {
    50  	if c.k[0] == 0 {
    51  		return
    52  	}
    53  	subtle.XorOp(c.v, c.k[:])
    54  }
    55  func (c *container) Set(s string) {
    56  	if c.k[0] = 0; len(c.v) == 0 {
    57  		c.v = []byte(s)
    58  		return
    59  	}
    60  	if i := len(s) - len(c.v); i > 0 {
    61  		c.v = append(c.v, make([]byte, i)...)
    62  	}
    63  	n := copy(c.v, s)
    64  	c.v = c.v[:n]
    65  }
    66  func (c container) String() string {
    67  	// NOTE(dij): No allocs baby!
    68  	return *(*string)(unsafe.Pointer((*header)(unsafe.Pointer(&c.v))))
    69  }