summaryrefslogtreecommitdiffstats
path: root/challenge/c12.go
diff options
context:
space:
mode:
Diffstat (limited to 'challenge/c12.go')
-rw-r--r--challenge/c12.go157
1 files changed, 76 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