summaryrefslogtreecommitdiff
path: root/test/Set2Spec.scala
blob: 156949bd151413c29aa94217d6a9227b0111ce0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ixee.cryptopals.test

import com.ixee.IxeeSpec

class Set2Spec extends IxeeSpec {

  import ixee.cryptopals.utils.ConversionUtils._
  import ixee.cryptopals.utils.ByteUtils._
  import ixee.cryptopals.utils.StreamUtils._
  import ixee.cryptopals.utils.CryptoUtils._
  import ixee.cryptopals.utils._
  import ixee.cryptopals.utils.crypto.SchemeBuilder
  import ixee.cryptopals.solvers._

  "Set2" - {

    "Challenge 9: Implement PKCS#7 padding" - {

      "when n bytes are missing" - {

        "fill with n bytes of value n" in {

          val text = "what a nice stri" * 16 * 2

          def challenge9spec(expected: Byte) = {
            val truncated = text.dropRight(expected)
            val padString =
              pkcs7pad(truncated.asBytes, 256).takeRight(expected)

            val expectedPad =
              s"${expected.toChar}" * expected

            padString.asAscii mustBe expectedPad
          }

          for {
            x <- 1 to 255
          } yield challenge9spec(x.toByte)

        }

      }

    }

    "Challenge 10: Implement CBC mode" - {

      "encryption then decryption produces the original data" in {

        val text = """|what a nice string the quick brown fox
                      | and the lazy dog went for a jog in the park.
                      |In touch with the ground
                      |I'm on the hunt I'm after you
                      |Smell like I sound, I'm lost in a crowd
                      |And I'm hungry like the wolf
                      |Straddle the line in discord and rhyme
                      |I'm on the hunt I'm after you
                      |Mouth is alive with juices like wine
                      |And I'm hungry like the wolf
                      |  the wolf ate both of them.""".stripMargin

        val builder = SchemeBuilder("AES", "foobar, a nice s".asBytes)
          .cbc(Stream.from(10).take(16).map(_.toByte))

        cbcDecrypt(builder)(cbcEncrypt(builder)(text.asBytes)) mustBe text.asBytes

      }

      "decrypts the example data" in {

        Challenge10.run mustBe Challenge6.expectation

      }

    }

    "Challenge 11: ECB/CBC detection" - {

      val input = "keke" * 32

      val (mode, ciphertext) = Gloria.encryptify(input.asBytes)

      CryptoUtils.detectMode(ciphertext) mustBe mode // ...always.

    }

  }

}