summaryrefslogblamecommitdiffstats
path: root/lib/oracle.go
blob: 4def7e260f98c3d4e20fbc4417a24fd9f71eb6d8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10




                                                    




                                                                                        
                    
                       

             

                     
                                        



                          
                                                                


                          

 




                                                                      
                                   



                                                                     
                                  








                                                             
                            
                      
                   








                                                
                                            
                                                                                    

 


                                                        
                                           
 
// Copyright © 2020 rsiddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package lib

var oracleUnknown string = `Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg
aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq
dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg
YnkK`

var oracleKey []byte
var oracleRandom []byte

func init() {
	var err error

	oracleKey, err = RandomBytes(16)
	if err != nil {
		panic(err)
	}

	oracleRandom, err = RandomBytes(int(RandomInt(1, 4096)))
	if err != nil {
		panic(err)
	}
}

// Given an input `in`, this function AES encrypts `in` using a
// randomly generate 16-byte key using ECB or CBC mode and returns the
// cipher.
func OracleAESRandomEncrypt(in []byte) []byte {
	// Generate random key.
	key, err := RandomBytes(16)
	if err != nil {
		panic(err)
	}
	// Generate random initialization vector; needed for AES CBC.
	iv, err := RandomBytes(16)
	if err != nil {
		panic(err)
	}

	// Add 5-10 bytes at the beginning and end of `in`
	in = append(randomBytes(5, 10), in...)
	in = append(in, randomBytes(5, 10)...)

	// Randomly encrypt `in` with AES in ECB or CBC mode.
	m := RandomInt(0, 1)
	var out []byte
	if m == 0 {
		// Encrypt with AES in ECB mode.
		out = AESEncryptECB(in, key)
	} else {
		// Encrypt with AES in CBC mode.
		out = AESEncryptCBC(in, key, iv)
	}
	return out
}

func OracleAESEncryptECB(in []byte) []byte {
	return AESEncryptECB(append(in, Base64ToBytes(oracleUnknown)...), oracleKey)
}

func OracleAESVarEncryptECB(in []byte) []byte {
	in = append(oracleRandom, in...)
	in = append(in, Base64ToBytes(oracleUnknown)...)
	return AESEncryptECB(in, oracleKey)
}