Algorytmy kryptograficzne | Laboratorium 6

  1. Uzupełnnij implementację kryptosystemu AES. Wykorzystaj wyłącznie oficjalny standard FIPS-197.

      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
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    
    from typing import List, Sequence
    
    
    # S-BOX (Table 4)
    SBOX: List[int] = [
    
    ]
    
    # Inverse S-BOX (Table 6)
    INV_SBOX: List[int] = [
    
    ]
    
    
    # Figure 1
    def _bytes_to_state(block: bytes) -> List[List[int]]:
        pass
    
    
    # Figure 1
    def _state_to_bytes(state: Sequence[Sequence[int]]) -> bytes:
        pass
    
    
    # Formula (5.9)
    def _add_round_key(state: List[List[int]], round_key: bytes) -> None:
        pass
    
    
    # S-BOX
    def _sub_bytes(state: List[List[int]]) -> None:
        pass
    
    
    # Inverse S-BOX
    def _inv_sub_bytes(state: List[List[int]]) -> None:
        pass
    
    
    # Formula (5.5)
    def _shift_rows(state: List[List[int]]) -> None:
        pass
    
    
    # Formula (5.12)
    def _inv_shift_rows(state: List[List[int]]) -> None:
        pass
    
    
    # Multiplication by x mod x^8+x^4+x^3+x+1 (Formula (4.5))
    def _xtime(x: int) -> int:
        pass
    
    
    # Formula (5.7)
    def _mix_columns(state: List[List[int]]) -> None:
        pass
    
    
    # Formula (5.14)
    def _inv_mix_columns(state: List[List[int]]) -> None:
        pass
    
    
    # Rcon (Table 5)
    RCON: List[int] = []
    
    
    # Formula (5.10)
    def _rot_word(word: List[int]) -> List[int]:
        pass
    
    
    # Formula (5.11)
    def _sub_word(word: List[int]) -> List[int]:
        pass
    
    
    # Algorithm 2
    def key_expand_128(key: bytes) -> List[bytes]:
        pass
    
    
    # Algorithm 1
    def aes128_encrypt_block(pt: bytes, key: bytes) -> bytes:
        pass
    
    
    # Algorithm 3
    def aes128_decrypt_block(ct: bytes, key: bytes) -> bytes:
        pass
    
    
    def test() -> None:
        key = bytes.fromhex("000102030405060708090a0b0c0d0e0f")
        pt = bytes.fromhex("00112233445566778899aabbccddeeff")
        expected = bytes.fromhex("69c4e0d86a7b0430d8cdb78070b4c55a")
    
        ct = aes128_encrypt_block(pt, key)
        assert ct == exp
    
        rt = aes128_decrypt_block(ct, key)
        assert rt == pt
    
        print("OK")
    
    
    if __name__ == "__main__":
        test()
    
  2. Dla ustalonego bloku tekstu jawnego oraz klucza zbadaj, jaka jest średnia odległość Hamminga szyfrogramów przy zmianie pojedynczych bitów tekstu jawnego lub klucza.