From c35eb556136a35a5aa1f4e9371428a4c4cb7fdbd Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Wed, 24 Feb 2021 20:09:26 -0500 Subject: lib: remove oracle.go Move functions into C12 and C14 where they're used. --- challenge/c12.go | 30 +++++++++++++++++++++--------- challenge/c14.go | 31 ++++++++++++++++++++++++------- lib/oracle.go | 36 ------------------------------------ 3 files changed, 45 insertions(+), 52 deletions(-) delete mode 100644 lib/oracle.go diff --git a/challenge/c12.go b/challenge/c12.go index b76f28a..0308bd4 100644 --- a/challenge/c12.go +++ b/challenge/c12.go @@ -11,6 +11,18 @@ import ( func C12() { sheep := byte(65) + unknown := `Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg +aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq +dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg +YnkK` + key, err := lib.RandomBytes(16) + if err != nil { + fmt.Printf("key generatation error: %v", err) + } + encrypt := func(in []byte) []byte { + return lib.AESEncryptECB(append(in, lib.Base64ToBytes(unknown)...), key) + } + freshSheepBytes := func(n int) []byte { in := make([]byte, n) for i := 0; i < n; i++ { @@ -22,11 +34,11 @@ func C12() { in := make([]byte, 0) in = append(in, sheep) - is := len(lib.OracleAESEncryptECB(in)) // initial size - bs := 0 // block size + is := len(encrypt(in)) // initial size + bs := 0 // block size for { in = append(in, sheep) - bs = len(lib.OracleAESEncryptECB(in)) + bs = len(encrypt(in)) if bs != is { return (bs - is) } @@ -34,13 +46,13 @@ func C12() { } findUnknownStringCharacteristics := func(blocksize int) (int, int) { in := make([]byte, 0) - c_sz := len(lib.OracleAESEncryptECB(in)) // Cipher size - nblocks := c_sz / blocksize // number of blocks + c_sz := len(encrypt(in)) // Cipher size + nblocks := c_sz / blocksize // number of blocks // Figure out ize of unknown string. for { in = append(in, sheep) - bs := len(lib.OracleAESEncryptECB(in)) + bs := len(encrypt(in)) if bs != c_sz { return nblocks, (c_sz - len(in)) } @@ -49,7 +61,7 @@ func C12() { } isOracleUsingECB := func() bool { in := lib.StrToBytes("OliverMkTukudzi OliverMkTukudzi OliverMkTukudzi") - oo := lib.OracleAESEncryptECB(in) + oo := encrypt(in) if lib.CipherUsesECB(oo) != nil { return true } @@ -61,7 +73,7 @@ func C12() { // `in` (n-1)th block that is known // `ds` deciphered unknown string decipherUnknownStringIter := func(blocksize, block, n int, in, ds []byte) ([]byte, []byte) { - oo := lib.OracleAESEncryptECB(in[0:(blocksize - n)]) + oo := encrypt(in[0:(blocksize - n)]) s := 16 * (block - 1) e := s + 16 @@ -75,7 +87,7 @@ func C12() { // Try all combinations. for i := 0; i < 256; i++ { in[15] = byte(i) - oo = lib.OracleAESEncryptECB(in) + oo = encrypt(in) if lib.BlocksEqual(nbl, oo[0:16]) { ds = append(ds, in[15]) diff --git a/challenge/c14.go b/challenge/c14.go index 552c2df..b35f2cd 100644 --- a/challenge/c14.go +++ b/challenge/c14.go @@ -11,6 +11,23 @@ import ( func C14() { sheep := byte(65) + unknown := `Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg +aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq +dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg +YnkK` + key, err := lib.RandomBytes(16) + if err != nil { + fmt.Printf("key generatation error: %v", err) + } + oracleRandom, err := lib.RandomBytes(int(lib.RandomInt(1, 4096))) + if err != nil { + fmt.Printf("oracle random generation error: %v", err) + } + encrypt := func(in []byte) []byte { + in = append(oracleRandom, in...) + in = append(in, lib.Base64ToBytes(unknown)...) + return lib.AESEncryptECB(in, key) + } freshSheepBytes := func(n int) []byte { in := make([]byte, n) for i := 0; i < n; i++ { @@ -22,11 +39,11 @@ func C14() { in := make([]byte, 0) in = append(in, sheep) - is := len(lib.OracleAESVarEncryptECB(in)) // initial size + is := len(encrypt(in)) // initial size bs := 0 for { in = append(in, sheep) - bs = len(lib.OracleAESVarEncryptECB(in)) + bs = len(encrypt(in)) if bs != is { return (bs - is) } @@ -41,7 +58,7 @@ func C14() { found := false for { in := append(v, tsb...) - c := lib.OracleAESVarEncryptECB(in) + c := encrypt(in) index, found = lib.HasConsecutiveMatchingBlocks(c, blocksize) if found { break @@ -63,7 +80,7 @@ func C14() { findUnknownStringNumBlocksLength := func(rpl, blocksize int) (int, int) { padding := blocksize - (rpl % blocksize) in := make([]byte, padding) - c_sz := len(lib.OracleAESVarEncryptECB(in)) // Cipher size + c_sz := len(encrypt(in)) // Cipher size nblocks := c_sz / blocksize // total number of blocks rblocks := (rpl + padding) / blocksize // number of blocks of random prefix @@ -71,7 +88,7 @@ func C14() { // Figure out size of unknown string. for { in = append(in, sheep) - bs := len(lib.OracleAESVarEncryptECB(in)) + bs := len(encrypt(in)) if bs != c_sz { return ublocks, (c_sz - len(in) - rpl) } @@ -85,7 +102,7 @@ func C14() { // `in` (n-1)th block that is known // `ds` deciphered unknown string decipherOneByte := func(nrpb, rpo, blocksize, block, n int, in, ds []byte) ([]byte, []byte) { - oo := lib.OracleAESVarEncryptECB(in[0:(len(in) - n)]) + oo := encrypt(in[0:(len(in) - n)]) s := (nrpb * blocksize) + 16*(block-1) e := s + 16 @@ -99,7 +116,7 @@ func C14() { // Try all combinations. for i := 0; i < 256; i++ { in[len(in)-1] = byte(i) - oo = lib.OracleAESVarEncryptECB(in) + oo = encrypt(in) if lib.BlocksEqual(nbl, oo[(nrpb*blocksize):(nrpb*blocksize)+16]) { diff --git a/lib/oracle.go b/lib/oracle.go deleted file mode 100644 index 8a449a1..0000000 --- a/lib/oracle.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright © 2020 rsiddharth -// 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) - } -} - -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) -} -- cgit v1.2.3