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

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package cfg
    18  
    19  import "crypto/rand"
    20  
    21  const (
    22  	// WrapHex is a Setting that enables the Hex Wrapper for the generated Profile.
    23  	WrapHex = cBit(0xD0)
    24  	// WrapZlib is a Setting that enables the ZLIB Wrapper for the generated Profile.
    25  	WrapZlib = cBit(0xD1)
    26  	// WrapGzip is a Setting that enables the GZIP Wrapper for the generated Profile.
    27  	WrapGzip = cBit(0xD2)
    28  	// WrapBase64 is a Setting that enables the Base64 Wrapper for the generated
    29  	// Profile.
    30  	WrapBase64 = cBit(0xD3)
    31  )
    32  
    33  const (
    34  	valXOR = cBit(0xD4)
    35  	valCBK = cBit(0xD5)
    36  	valAES = cBit(0xD6)
    37  )
    38  
    39  // WrapXOR returns a Setting that will apply the XOR Wrapper to the generated
    40  // Profile. The specified key will be the XOR key used.
    41  func WrapXOR(k []byte) Setting {
    42  	n := len(k)
    43  	if n > 0xFFFF {
    44  		n = 0xFFFF
    45  	}
    46  	c := append(cBytes{byte(valXOR), byte(n >> 8), byte(n)}, k[:n]...)
    47  	return &c
    48  }
    49  
    50  // WrapAES returns a Setting that will apply the AES Wrapper to the generated
    51  // Profile. The specified key and IV will be the AES Key and IV used.
    52  func WrapAES(k, iv []byte) Setting {
    53  	n, v := len(k), len(iv)
    54  	if n > 0xFF {
    55  		n = 0xFF
    56  	}
    57  	i := iv
    58  	if n > 0 && v == 0 {
    59  		i, v = make([]byte, 16), 16
    60  		rand.Read(i)
    61  	}
    62  	c := make(cBytes, 3+n+v)
    63  	c[0] = byte(valAES)
    64  	c[1], c[2] = byte(n), byte(v)
    65  	n = copy(c[3:], k) + 3
    66  	copy(c[n:], i)
    67  	return &c
    68  }
    69  
    70  // WrapCBK returns a Setting that will apply the CBK Wrapper to the generated
    71  // Profile. The specified ABC and Type values are the CBK letters used.
    72  //
    73  // To specify the CBK buffer size, use the 'WrapCBKSize' function instead.
    74  func WrapCBK(a, b, c, d byte) Setting {
    75  	return &cBytes{byte(valCBK), 128, a, b, c, d}
    76  }
    77  
    78  // WrapCBKSize returns a Setting that will apply the CBK Wrapper to the generated
    79  // Profile. The specified Size, ABC and Type values and the CBK size and letters
    80  // used.
    81  func WrapCBKSize(s, a, b, c, d byte) Setting {
    82  	return &cBytes{byte(valCBK), s, a, b, c, d}
    83  }