summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2021-02-11 23:00:49 -0500
committerrsiddharth <s@ricketyspace.net>2021-02-11 23:00:49 -0500
commitaaabeb704f200023aa544d70eaca9f363e8da975 (patch)
tree005886d6c8dc5b04160e5eec7fd561ad0d7bdd86
parent99a4f2f4e29e7963977ba16bf3aba3463a1c61d1 (diff)
lib: update Pkcs7PaddingUndo, AESDecryptCBC
Add error as the second return value to Pkcs7PaddingUndo and AESDecryptCBC.
-rw-r--r--challenge/c10.go4
-rw-r--r--challenge/c15.go28
-rw-r--r--challenge/c16.go2
-rw-r--r--lib/aes.go8
-rw-r--r--lib/blocks.go6
5 files changed, 29 insertions, 19 deletions
diff --git a/challenge/c10.go b/challenge/c10.go
index 18f2db2..0ed473b 100644
--- a/challenge/c10.go
+++ b/challenge/c10.go
@@ -97,11 +97,11 @@ func C10() {
fmt.Printf("NIST SP 800-38A F.2.1 (has padding):\n%v\n",
lib.PrettifyHexStr(lib.BytesToHexStr(cipher80038A)))
- o := lib.AESDecryptCBC(cipher80038A, key80038A, iv80038A)
+ o, _ := lib.AESDecryptCBC(cipher80038A, key80038A, iv80038A)
fmt.Printf("NIST SP 800-38A F.2.2:\n%v\n",
lib.PrettifyHexStr(lib.BytesToHexStr(o)))
- o = lib.AESDecryptCBC(cipher10, key10, iv10)
+ o, _ = lib.AESDecryptCBC(cipher10, key10, iv10)
fmt.Printf("Cryptopals Ch. 10:\n%v", lib.BytesToStr(o))
}
diff --git a/challenge/c15.go b/challenge/c15.go
index 40892ec..e974d64 100644
--- a/challenge/c15.go
+++ b/challenge/c15.go
@@ -15,7 +15,7 @@ func C15() {
padded_ice := lib.Pkcs7Padding(ice, 16)
fmt.Printf("ice padded: %v\n", padded_ice)
- unpadded_ice := lib.Pkcs7PaddingUndo(padded_ice)
+ unpadded_ice, _ := lib.Pkcs7PaddingUndo(padded_ice)
for i := 0; i < len(ice); i++ {
if ice[i] != unpadded_ice[i] {
panic("padding undo failed!")
@@ -23,18 +23,28 @@ func C15() {
}
fmt.Printf("unpadded ice: %v\n", unpadded_ice)
- // Will fail and panic
- // bad_ice := append(ice, []byte{5, 5, 5, 5}...)
- // fmt.Printf("bad ice: %v\n", bad_ice)
- // lib.Pkcs7PaddingUndo(bad_ice)
+ // Will fail.
+ bad_ice := append(ice, []byte{5, 5, 5, 5}...)
+ fmt.Printf("bad ice: %v\n", bad_ice)
+ _, err := lib.Pkcs7PaddingUndo(bad_ice)
+ if err != nil {
+ fmt.Printf("bad ice upadding failed: %s\n", err.Error())
+ }
- // Will fail and panic
- // evil_ice := append(ice, []byte{1, 2, 3, 4}...)
- // fmt.Printf("evil ice: %v\n", evil_ice)
- // lib.Pkcs7PaddingUndo(evil_ice)
+ // Will fail.
+ evil_ice := append(ice, []byte{1, 2, 3, 4}...)
+ fmt.Printf("evil ice: %v\n", evil_ice)
+ _, err = lib.Pkcs7PaddingUndo(evil_ice)
+ if err != nil {
+ fmt.Printf("evil ice upadding failed: %s\n", err.Error())
+ }
}
// Output:
// ice: [73 67 69 32 73 67 69 32 66 65 66 89]
// ice padded: [73 67 69 32 73 67 69 32 66 65 66 89 4 4 4 4]
// unpadded ice: [73 67 69 32 73 67 69 32 66 65 66 89]
+// bad ice: [73 67 69 32 73 67 69 32 66 65 66 89 5 5 5 5]
+// bad ice upadding failed: input is not pkcs#7 padded
+// evil ice: [73 67 69 32 73 67 69 32 66 65 66 89 1 2 3 4]
+// evil ice upadding failed: input is not pkcs#7 padded
diff --git a/challenge/c16.go b/challenge/c16.go
index 534d9fb..155fbaa 100644
--- a/challenge/c16.go
+++ b/challenge/c16.go
@@ -60,7 +60,7 @@ func cbcBitFlipEncrypt(s string) []byte {
}
func cbcBitFlipDecryptHasAdmin(c []byte) bool {
- b := lib.AESDecryptCBC(c, cbcBitFlipKey, cbcBitFlipIV)
+ b, _ := lib.AESDecryptCBC(c, cbcBitFlipKey, cbcBitFlipIV)
s := lib.BytesToStr(b)
// Convert to a map
diff --git a/lib/aes.go b/lib/aes.go
index 396ee5c..8c4dd26 100644
--- a/lib/aes.go
+++ b/lib/aes.go
@@ -23,7 +23,7 @@ func AESEncryptCBC(plain, key, iv []byte) []byte {
return output
}
-func AESDecryptCBC(cipher, key, iv []byte) []byte {
+func AESDecryptCBC(cipher, key, iv []byte) ([]byte, error) {
iter := len(cipher) / 16
lc := iv
@@ -38,9 +38,9 @@ func AESDecryptCBC(cipher, key, iv []byte) []byte {
}
// Undo padding
- output = Pkcs7PaddingUndo(output)
+ output, err := Pkcs7PaddingUndo(output)
- return output
+ return output, err
}
func AESEncryptECB(plain, key []byte) []byte {
@@ -72,7 +72,7 @@ func AESDecryptECB(cipher, key []byte) []byte {
}
// Undo padding
- output = Pkcs7PaddingUndo(output)
+ output, _ = Pkcs7PaddingUndo(output)
return output
}
diff --git a/lib/blocks.go b/lib/blocks.go
index 9b184d1..c4d8be1 100644
--- a/lib/blocks.go
+++ b/lib/blocks.go
@@ -112,15 +112,15 @@ func Pkcs7Padding(in []byte, k int) []byte {
}
// Removes PKCS#7 Padding from input `in`
-func Pkcs7PaddingUndo(in []byte) []byte {
+func Pkcs7PaddingUndo(in []byte) ([]byte, error) {
pd := in[len(in)-1] // padding character
pl := int(pd) // padding length
// Validate
for i := len(in) - 1; i >= len(in)-pl; i-- {
if in[i] != pd {
- panic("input is not pkcs#7 padded!")
+ return []byte{}, CPError{"input is not pkcs#7 padded"}
}
}
- return in[0:(len(in) - pl)]
+ return in[0:(len(in) - pl)], nil
}