summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-12-09 21:56:49 -0500
committerrsiddharth <s@ricketyspace.net>2020-12-09 21:56:49 -0500
commit620f53145792f038bfee9bd40a7b9a971ca0f287 (patch)
tree2719a6b1cb00902b02dd8518a58fd1f3fca08597 /lib
parentc2571480c1a35338e7b1b7d4b5ffeab018ef179e (diff)
lib/oracle.go: add OracleAESRandomEncrypt
Diffstat (limited to 'lib')
-rw-r--r--lib/oracle.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/oracle.go b/lib/oracle.go
new file mode 100644
index 0000000..a77793a
--- /dev/null
+++ b/lib/oracle.go
@@ -0,0 +1,68 @@
+// Copyright © 2020 rsiddharth <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package lib
+
+import (
+ "crypto/rand"
+ "math/big"
+)
+
+// 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 := make([]byte, 16)
+ _, err := rand.Read(key)
+ if err != nil {
+ panic(err)
+ }
+ // Generate random initialization vector; needed for AES CBC.
+ iv := make([]byte, 16)
+ _, err = rand.Read(iv)
+ 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, err := rand.Int(rand.Reader, big.NewInt(2))
+ if err != nil {
+ panic(err)
+ }
+ var out []byte
+ if m.Int64() == 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
+}
+
+// Randomly generates `min` to `max` bytes.
+func randomBytes(min, max int64) []byte {
+ var rn *big.Int
+ var err error
+ for {
+ rn, err = rand.Int(rand.Reader, big.NewInt(max+1))
+ if err != nil {
+ panic(err)
+ }
+ if rn.Int64() >= min {
+ break
+ }
+ }
+
+ bs := make([]byte, rn.Int64())
+ _, err = rand.Read(bs)
+ if err != nil {
+ panic(err)
+ }
+ return bs
+}