コード例 #1
0
    def test_encdec_one(self):
        """Test using values from RFC8439 section 2.3.2."""

        key = bytes(range(32))
        nonce = b"\x00\x00\x00\x00\x00\x00\x00\x4a\x00\x00\x00\x00"
        ##nonce = b"\x00\x00\x00\x09\x00\x00\x00\x4a\x00\x00\x00\x00"
        counter = 1
        plain_text = (b"Ladies and Gentlemen of the class of '99: If I could"
                      b" offer you only one tip for the future,"
                      b" sunscreen would be it.")

        cipher_text = encrypt(plain_text,
                              key,
                              self.algo_name,
                              nonce=nonce,
                              counter=counter)

        expected_cipher_text = (
            b"\x6e\x2e\x35\x9a\x25\x68\xf9\x80\x41\xba\x07\x28\xdd\x0d\x69\x81"
            b"\xe9\x7e\x7a\xec\x1d\x43\x60\xc2\x0a\x27\xaf\xcc\xfd\x9f\xae\x0b"
            b"\xf9\x1b\x65\xc5\x52\x47\x33\xab\x8f\x59\x3d\xab\xcd\x62\xb3\x57"
            b"\x16\x39\xd6\x24\xe6\x51\x52\xab\x8f\x53\x0c\x35\x9f\x08\x61\xd8"
            b"\x07\xca\x0d\xbf\x50\x0d\x6a\x61\x56\xa3\x8e\x08\x8a\x22\xb6\x5e"
            b"\x52\xbc\x51\x4d\x16\xcc\xf8\x06\x81\x8c\xe9\x1a\xb7\x79\x37\x36"
            b"\x5a\xf9\x0b\xbf\x74\xa3\x5b\xe6\xb4\x0b\x8e\xed\xf2\x78\x5e\x42"
            b"\x87\x4d")

        self.assertEqual(cipher_text,
                         expected_cipher_text,
                         msg="Checking cipher text matches expected value")

        decrypted_text = decrypt(cipher_text,
                                 key,
                                 self.algo_name,
                                 nonce=nonce,
                                 counter=counter)

        self.assertEqual(
            plain_text,
            decrypted_text,
            msg=
            "Checking decryption of encrypted text gives original plain text")
コード例 #2
0
    def test_encdec_two(self):
        """Test using values approximating that from RPS game."""

        key = b"TPSecret" * 4
        nonce = bytearray(range(12, 0, -1))
        plain_text = b"rock"

        cipher_text = encrypt(plain_text, key, self.algo_name, nonce=nonce)

        decrypted_text = decrypt(cipher_text, key, self.algo_name, nonce=nonce)

        ### It is possible for these to match but very unlikely in the general case
        self.assertNotEqual(plain_text,
                            cipher_text,
                            msg="Check cipher_text is not plain_text")
        self.assertEqual(
            plain_text,
            decrypted_text,
            msg=
            "Checking decryption of encrypted text gives original plain text")
コード例 #3
0
                if (cipher_ad is None and isinstance(opponent_msgs[msg_idx][0],
                                                     RpsEncDataAdvertisement)):
                    cipher_ad = opponent_msgs[msg_idx][0]
                    cipher_bytes = cipher_ad.enc_data
                    cipher_round = cipher_ad.round_no
                elif (key_ad is None and isinstance(opponent_msgs[msg_idx][0],
                                                    RpsKeyDataAdvertisement)):
                    key_ad = opponent_msgs[msg_idx][0]
                    key_bytes = key_ad.key_data
                    key_round = key_ad.round_no

            if cipher_ad and key_ad:
                if round_no == cipher_round == key_round:
                    key = enlargeKey(key_bytes, KEY_ENLARGE)
                    plain_bytes = decrypt(cipher_bytes,
                                          key,
                                          CRYPTO_ALGO,
                                          nonce=static_nonce)
                    opponent_choice = strUnpad(plain_bytes)
                else:
                    print("Received wrong round for {:d} {:d}: {:d} {:d}",
                          opponent_name, round_no, cipher_round, key_round)
            else:
                print(
                    "Missing packets: RpsEncDataAdvertisement "
                    "and RpsKeyDataAdvertisement:", cipher_ad, key_ad)
            player_choices.append(opponent_choice)

        ### Free up some memory by deleting any data that's no longer needed
        del allmsg_by_addr
        gc.collect()  ### TODO - try removing this
        d_print(2, "GC after comms", gc.mem_free())