summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2021-02-24 17:13:22 -0500
committerrsiddharth <s@ricketyspace.net>2021-02-24 17:13:22 -0500
commit197b57adb377ea8b8fffaf076f6797abdf8c3fe5 (patch)
tree2579cae6ad292d73b0d8b635a7a43eac5f7a79ae
parenteec7f9ae3fcb4a9eecf1bd5aa3d58cb66544643e (diff)
challenge/c12.go: refactor
Keep everything inside C12
-rw-r--r--challenge/c12.go157
-rw-r--r--challenge/c14.go11
2 files changed, 87 insertions, 81 deletions
diff --git a/challenge/c12.go b/challenge/c12.go
index a19777b..b76f28a 100644
--- a/challenge/c12.go
+++ b/challenge/c12.go
@@ -5,16 +5,89 @@ package challenge
import (
"fmt"
+
"ricketyspace.net/cryptopals/lib"
)
-var sheep byte = 65
-
func C12() {
+ sheep := byte(65)
+ freshSheepBytes := func(n int) []byte {
+ in := make([]byte, n)
+ for i := 0; i < n; i++ {
+ in[i] = sheep
+ }
+ return in
+ }
+ findBlockSize := func() int {
+ in := make([]byte, 0)
+
+ in = append(in, sheep)
+ is := len(lib.OracleAESEncryptECB(in)) // initial size
+ bs := 0 // block size
+ for {
+ in = append(in, sheep)
+ bs = len(lib.OracleAESEncryptECB(in))
+ if bs != is {
+ return (bs - is)
+ }
+ }
+ }
+ 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
+
+ // Figure out ize of unknown string.
+ for {
+ in = append(in, sheep)
+ bs := len(lib.OracleAESEncryptECB(in))
+ if bs != c_sz {
+ return nblocks, (c_sz - len(in))
+ }
+ }
+
+ }
+ isOracleUsingECB := func() bool {
+ in := lib.StrToBytes("OliverMkTukudzi OliverMkTukudzi OliverMkTukudzi")
+ oo := lib.OracleAESEncryptECB(in)
+ if lib.CipherUsesECB(oo) != nil {
+ return true
+ }
+ return false
+ }
+ // `blocksize` is the size of a block
+ // `block` is the nth block that is being deciphered
+ // `n` is the nth byte of the block `block` that is going to be deciphered.
+ // `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)])
+
+ s := 16 * (block - 1)
+ e := s + 16
+ nbl := oo[s:e] // nth block of the cipher
+
+ // Shift `in` to the left by one place.
+ for i := 0; i < blocksize-1; i++ {
+ in[i] = in[i+1]
+ }
+
+ // Try all combinations.
+ for i := 0; i < 256; i++ {
+ in[15] = byte(i)
+ oo = lib.OracleAESEncryptECB(in)
+
+ if lib.BlocksEqual(nbl, oo[0:16]) {
+ ds = append(ds, in[15])
+ return in, ds
+ }
+ }
+ panic("not found!")
+ }
+
if !isOracleUsingECB() {
panic("oracle not using ecb mode")
}
-
blocksize := findBlockSize()
nbl, us_sz := findUnknownStringCharacteristics(blocksize)
in := freshSheepBytes(blocksize)
@@ -35,84 +108,6 @@ func C12() {
fmt.Printf("Unknown String:\n%v", lib.BytesToStr(ds))
}
-func freshSheepBytes(n int) []byte {
- in := make([]byte, n)
- for i := 0; i < n; i++ {
- in[i] = sheep
- }
- return in
-}
-
-func findBlockSize() int {
- in := make([]byte, 0)
-
- in = append(in, sheep)
- is := len(lib.OracleAESEncryptECB(in)) // initial size
- bs := 0 // block size
- for {
- in = append(in, sheep)
- bs = len(lib.OracleAESEncryptECB(in))
- if bs != is {
- return (bs - is)
- }
- }
-}
-
-func findUnknownStringCharacteristics(blocksize int) (int, int) {
- in := make([]byte, 0)
- c_sz := len(lib.OracleAESEncryptECB(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))
- if bs != c_sz {
- return nblocks, (c_sz - len(in))
- }
- }
-
-}
-
-func isOracleUsingECB() bool {
- in := lib.StrToBytes("OliverMkTukudzi OliverMkTukudzi OliverMkTukudzi")
- oo := lib.OracleAESEncryptECB(in)
- if lib.CipherUsesECB(oo) != nil {
- return true
- }
- return false
-}
-
-// `blocksize` is the size of a block
-// `block` is the nth block that is being deciphered
-// `n` is the nth byte of the block `block` that is going to be deciphered.
-// `in` (n-1)th block that is known
-// `ds` deciphered unknown string
-func decipherUnknownStringIter(blocksize, block, n int, in, ds []byte) ([]byte, []byte) {
- oo := lib.OracleAESEncryptECB(in[0:(blocksize - n)])
-
- s := 16 * (block - 1)
- e := s + 16
- nbl := oo[s:e] // nth block of the cipher
-
- // Shift `in` to the left by one place.
- for i := 0; i < blocksize-1; i++ {
- in[i] = in[i+1]
- }
-
- // Try all combinations.
- for i := 0; i < 256; i++ {
- in[15] = byte(i)
- oo = lib.OracleAESEncryptECB(in)
-
- if lib.BlocksEqual(nbl, oo[0:16]) {
- ds = append(ds, in[15])
- return in, ds
- }
- }
- panic("not found!")
-}
-
// Output:
// Unknown String:
// Rollin' in my 5.0
diff --git a/challenge/c14.go b/challenge/c14.go
index 3b5b11c..4471f44 100644
--- a/challenge/c14.go
+++ b/challenge/c14.go
@@ -5,9 +5,12 @@ package challenge
import (
"fmt"
+
"ricketyspace.net/cryptopals/lib"
)
+var sheep byte = 65
+
func C14() {
blocksize := findBlockSizeForVarEncryptECB()
rpl, nrpb, rpo := findRandPrefixLen(blocksize)
@@ -32,6 +35,14 @@ func C14() {
fmt.Printf("Unknown String:\n%v", lib.BytesToStr(ds))
}
+func freshSheepBytes(n int) []byte {
+ in := make([]byte, n)
+ for i := 0; i < n; i++ {
+ in[i] = sheep
+ }
+ return in
+}
+
func findBlockSizeForVarEncryptECB() int {
in := make([]byte, 0)