コード例 #1
0
ファイル: tests.py プロジェクト: maojui/writeups
 def test_speck128_256(self):
     block_size = 128
     key_size = 256
     for x in range(self.test_cnt):
         key = randint(0, (2**key_size) - 1)
         plaintxt = randint(0, (2**block_size) - 1)
         c = SpeckCipher(key, key_size, block_size, 'ECB')
         assert c.decrypt(c.encrypt(plaintxt)) == plaintxt, 'Test %r Failed with Random Key %r and Random Plaintext %r' % (x, hex(key), hex(plaintxt))
コード例 #2
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_pcbc_mode_single(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'PCBC', init=self.iv)
        pcbc_out = c.encrypt(self.plaintxt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')
        pcbc_equivalent = c.encrypt(self.iv ^ self.plaintxt)
        assert pcbc_out == pcbc_equivalent
コード例 #3
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck64_128(self):
     key = 0x1b1a1918131211100b0a090803020100
     plaintxt = 0x3b7265747475432d
     ciphertxt = 0x8c6fa548454e028b
     block_size = 64
     key_size = 128
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #4
0
 def test_speck128_192(self):
     key = 0x17161514131211100f0e0d0c0b0a09080706050403020100
     plaintxt = 0x726148206665696843206f7420746e65
     ciphertxt = 0x1be4cf3a13135566f9bc185de03c1886
     block_size = 128
     key_size = 192
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #5
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck32_64(self):
     key = 0x1918111009080100
     plaintxt = 0x6574694c
     ciphertxt = 0xa86842f2
     block_size = 32
     key_size = 64
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #6
0
 def test_speck48_96(self):
     key = 0x1a19181211100a0908020100
     plaintxt = 0x6d2073696874
     ciphertxt = 0x735e10b6445d
     block_size = 48
     key_size = 96
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #7
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck128_256(self):
     key = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100
     plaintxt = 0x65736f6874206e49202e72656e6f6f70
     ciphertxt = 0x4109010405c0f53e4eeeb48d9c188f43
     block_size = 128
     key_size = 256
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #8
0
 def test_speck64_128(self):
     key = 0x1b1a1918131211100b0a090803020100
     plaintxt = 0x3b7265747475432d
     ciphertxt = 0x8c6fa548454e028b
     block_size = 64
     key_size = 128
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #9
0
 def test_speck64_96(self):
     key = 0x131211100b0a090803020100
     plaintxt = 0x74614620736e6165
     ciphertxt = 0x9f7952ec4175946c
     block_size = 64
     key_size = 96
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #10
0
 def test_speck96_96(self):
     key = 0x0d0c0b0a0908050403020100
     plaintxt = 0x65776f68202c656761737520
     ciphertxt = 0x9e4d09ab717862bdde8f79aa
     block_size = 96
     key_size = 96
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #11
0
 def test_speck96_144(self):
     key = 0x1514131211100d0c0b0a0908050403020100
     plaintxt = 0x656d6974206e69202c726576
     ciphertxt = 0x2bf31072228a7ae440252ee6
     block_size = 96
     key_size = 144
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #12
0
 def test_speck128_128(self):
     key = 0x0f0e0d0c0b0a09080706050403020100
     plaintxt = 0x6c617669757165207469206564616d20
     ciphertxt = 0xa65d9851797832657860fedf5c570d18
     block_size = 128
     key_size = 128
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #13
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck48_72(self):
     key = 0x1211100a0908020100
     plaintxt = 0x20796c6c6172
     ciphertxt = 0xc049a5385adc
     block_size = 48
     key_size = 72
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #14
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck128_256(self):
     block_size = 128
     key_size = 256
     for x in range(self.test_cnt):
         key = randint(0, (2**key_size) - 1)
         plaintxt = randint(0, (2**block_size) - 1)
         print(x, hex(key), hex(plaintxt))
         c = SpeckCipher(key, key_size, block_size, 'ECB')
         assert c.decrypt(c.encrypt(plaintxt)) == plaintxt
コード例 #15
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_ofb_mode_chain(self):
        plaintxts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        ciphertexts = [c.encrypt(x) for x in plaintxts]
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        decryptexts = [c.decrypt(x) for x in ciphertexts]

        assert plaintxts == decryptexts
コード例 #16
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck96_96(self):
     key = 0x0d0c0b0a0908050403020100
     plaintxt = 0x65776f68202c656761737520
     ciphertxt = 0x9e4d09ab717862bdde8f79aa
     block_size = 96
     key_size = 96
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #17
0
 def test_speck128_256(self):
     key = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100
     plaintxt = 0x65736f6874206e49202e72656e6f6f70
     ciphertxt = 0x4109010405c0f53e4eeeb48d9c188f43
     block_size = 128
     key_size = 256
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #18
0
 def test_speck128_256(self):
     block_size = 128
     key_size = 256
     for x in range(self.test_cnt):
         key = randint(0, (2**key_size) - 1)
         plaintxt = randint(0, (2**block_size) - 1)
         print(x, hex(key), hex(plaintxt))
         c = SpeckCipher(key, key_size, block_size, 'ECB')
         assert c.decrypt(c.encrypt(plaintxt)) == plaintxt
コード例 #19
0
 def test_speck48_72(self):
     key = 0x1211100a0908020100
     plaintxt = 0x20796c6c6172
     ciphertxt = 0xc049a5385adc
     block_size = 48
     key_size = 72
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #20
0
 def test_speck32_64(self):
     key = 0x1918111009080100
     plaintxt = 0x6574694c
     ciphertxt = 0xa86842f2
     block_size = 32
     key_size = 64
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #21
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck64_96(self):
     key = 0x131211100b0a090803020100
     plaintxt = 0x74614620736e6165
     ciphertxt = 0x9f7952ec4175946c
     block_size = 64
     key_size = 96
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #22
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck96_144(self):
     key = 0x1514131211100d0c0b0a0908050403020100
     plaintxt = 0x656d6974206e69202c726576
     ciphertxt = 0x2bf31072228a7ae440252ee6
     block_size = 96
     key_size = 144
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #23
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck128_128(self):
     key = 0x0f0e0d0c0b0a09080706050403020100
     plaintxt = 0x6c617669757165207469206564616d20
     ciphertxt = 0xa65d9851797832657860fedf5c570d18
     block_size = 128
     key_size = 128
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #24
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck128_192(self):
     key = 0x17161514131211100f0e0d0c0b0a09080706050403020100
     plaintxt = 0x726148206665696843206f7420746e65
     ciphertxt = 0x1be4cf3a13135566f9bc185de03c1886
     block_size = 128
     key_size = 192
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #25
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_ctr_mode_equivalent(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)
        ctr_out = c.encrypt(self.plaintxt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')
        ecb_out = c.encrypt(self.iv + self.counter)
        ctr_equivalent = ecb_out ^ self.plaintxt
        assert ctr_out == ctr_equivalent
コード例 #26
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
 def test_speck48_96(self):
     key = 0x1a19181211100a0908020100
     plaintxt = 0x6d2073696874
     ciphertxt = 0x735e10b6445d
     block_size = 48
     key_size = 96
     c = SpeckCipher(key, key_size, block_size, 'ECB')
     assert c.encrypt(plaintxt) == ciphertxt
     assert c.decrypt(ciphertxt) == plaintxt
コード例 #27
0
    def setUp(self):
        key = 0x123456789ABCDEF00FEDCBA987654321
        alphabet = string.ascii_letters + string.digits
        keysize = 64
        idsize = 32

        self.codec = IDCodec(key, alphabet, keysize, idsize)

        self.cipher = SpeckCipher(key, key_size=keysize, block_size=idsize)
        self.base_codec = BaseCodec(alphabet, bits=idsize)
コード例 #28
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_ctr_mode_single_cycle(self):

        self.counter = 0x01

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)
        ctr_out = c.encrypt(self.plaintxt)

        self.counter = 0x01

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)
        output_plaintext = c.decrypt(ctr_out)

        assert output_plaintext == self.plaintxt
コード例 #29
0
ファイル: crypto.py プロジェクト: AdnanSlef/DACC-ectf
 def encrypt(self, pt):
     """encrypt data
     Args:
         pt (bytes): the plaintext message to encrypt
     """
     speck = SpeckCipher(self.key, 256, 128, 'CTR', self.iv)
     extra = len(pt) % 16
     return b''.join(
         speck.encrypt(int.from_bytes(pt[i:i +
                                         16], 'big')).to_bytes(16, 'big')
         for i in range(0,
                        len(pt) - extra, 16)) + speck.encrypt(
                            int.from_bytes(
                                pt[len(pt) - extra:], 'big')).to_bytes(
                                    16, 'big')[16 - extra:]
コード例 #30
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_ctr_mode_chain(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)

        ctr_out = 0
        for x in range(1000):
            ctr_out = c.encrypt(self.plaintxt)
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')

        ctr_equivalent = 0
        for x in range(1000):
            ecb_out = c.encrypt(self.iv + self.counter)
            self.counter += 1
            ctr_equivalent = ecb_out ^ self.plaintxt

        assert ctr_out == ctr_equivalent
コード例 #31
0
ファイル: crypto.py プロジェクト: AdnanSlef/DACC-ectf
 def decrypt(self, c, extra=0):
     """decrypt data
     Args:
         c (bytes): the ciphertest to decrypt
         extra (int): the length (in bytes) of the final block of plaintext
                         alternatively, the length of the plaintext in bytes (mod 16)
     """
     speck = SpeckCipher(self.key, 256, 128, 'CTR', self.iv)
     return b''.join(
         speck.decrypt(int.from_bytes(c[i:i +
                                        16], 'big')).to_bytes(16, 'big')
         for i in range(0,
                        len(c) - extra, 16)) + speck.decrypt(
                            int.from_bytes(
                                c[len(c) - extra:], 'big')).to_bytes(
                                    16, 'big')[16 - extra:]
コード例 #32
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_cbc_mode_chain(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CBC', init=self.iv)

        cbc_out = 0
        for x in range(1000):
            cbc_out = c.encrypt(self.plaintxt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')

        cbc_equivalent = self.iv
        for x in range(1000):
            cbc_input = self.plaintxt ^ cbc_equivalent
            cbc_equivalent = c.encrypt(cbc_input)

        assert cbc_out == cbc_equivalent
コード例 #33
0
class IDCodec(object):
    def __init__(self, key, alphabet, key_size, block_size):
        self.cipher = SpeckCipher(key,
                                  key_size=key_size,
                                  block_size=block_size)
        self.codec = BaseCodec(alphabet, bits=block_size)
        self.max_id = 2**block_size

    def decode(self, hash: str) -> int:
        return self.cipher.decrypt(self.codec.decode(hash)) + 1

    def encode(self, id: int) -> str:
        if id < 1 or id > self.max_id:
            raise Exception('id must be between 1 and %d' % (self.max_id))

        return self.codec.encode(self.cipher.encrypt(id - 1))
コード例 #34
0
ファイル: tests.py プロジェクト: maojui/writeups
    def test_ofb_mode_equivalent(self):
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        ofb_encrypt = c.encrypt(self.plaintxt)
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        ofb_decrypt = c.decrypt(ofb_encrypt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')
        ecb_out = c.encrypt(self.iv)
        ofb_equivalent_encrypt = ecb_out ^ self.plaintxt
        ofb_equivalent_decrypt = ecb_out ^ ofb_equivalent_encrypt

        assert ofb_encrypt == ofb_equivalent_encrypt
        assert ofb_decrypt == ofb_equivalent_decrypt
コード例 #35
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_pcbc_mode_chain(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'PCBC', init=self.iv)

        pcbc_out = 0
        for x in range(1000):
            pcbc_out = c.encrypt(self.plaintxt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')

        pcbc_equivalent = 0
        for x in range(1000):
            pcbc_input = self.plaintxt ^ self.iv
            pcbc_equivalent = c.encrypt(pcbc_input)
            self.iv = pcbc_equivalent ^ self.plaintxt

        assert pcbc_out == pcbc_equivalent
コード例 #36
0
ファイル: crypt.py プロジェクト: gyani-kto-mah/duifa
    def encrypt(self, plaintext):
        out = []
        number_str = str(self.encode(str(plaintext)))
        chunked_number_str = self.chunks(number_str, 19)
        for nstr in chunked_number_str:
            encrypted_str = SpeckCipher(self.key).encrypt(int(nstr))
            out.append(encrypted_str)

        return ".".join([str(x) for x in out])
コード例 #37
0
 def __init__(self):
     key_bytes = array.array('B', [])
     for k in self.gs_encryption_key:
         key_bytes.append(k)
     self.key_int = int.from_bytes(key_bytes,
                                   byteorder='little',
                                   signed=False)
     iv_bytes = array.array('B', [])
     for i in self.gs_iv:
         iv_bytes.append(i)
     self.iv_int = int.from_bytes(iv_bytes,
                                  byteorder='little',
                                  signed=False)
     self.gs_speck = SpeckCipher(self.key_int,
                                 key_size=128,
                                 block_size=64,
                                 mode=self.mode,
                                 init=self.iv_int)
コード例 #38
0
def argon2_test():
    start = time.time()
    ph = PasswordHasher()
    hash = ph.hash(password)
    hashed = hashlib.sha256(hash).digest()
    end = time.time()
    print "argon2 : ",(end-start)
    start = time.time()
    obj =AES.new(hashed,AES.MODE_CBC, 'This is an IV456')
    ciphertext = obj.encrypt("The answer is no")
    end = time.time()
    print "AES Encrypt: ",(end-start)*1000
    start = time.time()
    obj =AES.new(hashed,AES.MODE_CBC, 'This is an IV456')
    plaintext = obj.decrypt(ciphertext)
    end = time.time()
    print "AES Decrypt: ",(end-start)*1000
    my_plaintext = 0xCCCCAAAA55553333
    start = time.time()
    big_cipher = SimonCipher(0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF0000, key_size=256, block_size=128)
    simon = big_cipher.encrypt(my_plaintext)
    end = time.time()
    print "Simon Encrypt: ",(end-start)*1000
    start = time.time()
    big_cipher1 = SpeckCipher(0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF0000, key_size=256, block_size=128)
    speck = big_cipher1.encrypt(my_plaintext)
    end = time.time()
    print "Speck Encrypt: ",(end-start)*1000
    start = time.time()
    big_cipher = SimonCipher(0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF0000, key_size=256, block_size=128)
    plain = big_cipher.decrypt(simon)
    end = time.time()
    print plain
    print "Simon Decrypt: ",(end-start)*1000
    start = time.time()
    big_cipher1 = SpeckCipher(0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF0000, key_size=256, block_size=128)
    plain = big_cipher1.decrypt(speck)
    end = time.time()
    print plain
    print "Speck Decrypt: ",(end-start)*1000
コード例 #39
0
ファイル: main.py プロジェクト: jnbarnesUdel/Applied-Crypto
def MtE(ptxt, key1, key2):
    ctxt = 0
    T = hmac.new(
        key1.encode(), msg=ptxt.encode(),
        digestmod=SHA256).digest()  #was digest before see if this works
    E = SpeckCipher(key2)  #encryption block with hmack output as key
    Prev = random.getrandbits(128)  #create random IV for first value
    thing = ptxt.encode() + T
    thing = str(thing)
    block = breakup(thing, 16)
    for i in range(len(block)):
        temp2 = 0
        for j in block[i]:  #make string into int
            temp2 = (temp2 << 8) | ord(j)

        block[i] = temp2 ^ Prev
        temp = E.encrypt(block[i])
        #print(temp2)
        #print(temp)
        ctxt = (ctxt << 128) | (temp ^ temp2)  # accumulate onto the ctxt
    print("CTXT: ")
    print(hex(ctxt))
コード例 #40
0
ファイル: tests.py プロジェクト: maojui/writeups
    def test_pcbc_mode_single(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'PCBC', init=self.iv)
        pcbc_out = c.encrypt(self.plaintxt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')
        pcbc_equivalent = c.encrypt(self.iv ^ self.plaintxt)
        assert pcbc_out == pcbc_equivalent
コード例 #41
0
class GsCipher:
    gs_encryption_key = None
    gs_iv = None
    gs_speck = None
    mode = None
    logger = None
    key_int = None
    iv_int = None

    def __init__(self):
        key_bytes = array.array('B', [])
        for k in self.gs_encryption_key:
            key_bytes.append(k)
        self.key_int = int.from_bytes(key_bytes,
                                      byteorder='little',
                                      signed=False)
        iv_bytes = array.array('B', [])
        for i in self.gs_iv:
            iv_bytes.append(i)
        self.iv_int = int.from_bytes(iv_bytes,
                                     byteorder='little',
                                     signed=False)
        self.gs_speck = SpeckCipher(self.key_int,
                                    key_size=128,
                                    block_size=64,
                                    mode=self.mode,
                                    init=self.iv_int)

    def encrypt(self, ax25_packet):
        if self.gs_speck.mode == 'CBC':
            self.gs_speck.update_iv(self.iv_int)
        ax25_packet_encrypted = array.array('B', ax25_packet[:16])
        ax25_packet_temp = array.array('B', ax25_packet[16:])
        for i in range(0, 232, 8):
            plaintext_int = int.from_bytes(ax25_packet_temp[i:(i + 8)],
                                           byteorder='big',
                                           signed=False)
            ciphertext = self.gs_speck.encrypt(plaintext_int)
            ciphertext_bytes = bytearray.fromhex('{:032x}'.format(ciphertext))
            for c in ciphertext_bytes[8:]:
                ax25_packet_encrypted.append(c)
        ax25_packet_encrypted.extend([0x00] * 5)
        return ax25_packet_encrypted

    def decrypt(self, ax25_packet_encrypted):
        if self.gs_speck.mode == 'CBC':
            self.gs_speck.update_iv(self.iv_int)
        ax25_packet = array.array('B', ax25_packet_encrypted[:16])
        ax25_packet_temp = ax25_packet_encrypted[16:]
        for i in range(0, 232, 8):
            ciphertext_int = int.from_bytes(ax25_packet_temp[i:(i + 8)],
                                            byteorder='big',
                                            signed=False)
            plaintext = self.gs_speck.decrypt(ciphertext_int)
            plaintext_bytes = bytearray.fromhex('{:032x}'.format(plaintext))
            for p in plaintext_bytes[8:]:
                ax25_packet.append(p)
        return ax25_packet
コード例 #42
0
def decrypt_matrix(K0, K1, X):
    # Step 1: Create the cipher object

    # Discovered it's speck-ecb-128-128 through reversing
    speck_cipher = SpeckCipher(K0, K1)

    # TODO(eugenek): These steps are less complicated than they look, my SpeckCipher class just blows,
    # and expects data to come as QWORDS

    # Step 2: Format the matrix into cipher text
    # Flatten the matrix to the cipher blocks, size 128
    # First get 64-bit QWORDs, 2 cipher blocks are formed out of them
    ct_64 = [0, 0, 0, 0]
    for i in xrange(4):
        ct_64[i] = (X[i][0] << 48) | (X[i][1] << 32) | (X[i][2] << 16) | (
            X[i][3] << 0)
    ct0 = (ct_64[0] << 64) | (ct_64[1])
    ct1 = (ct_64[2] << 64) | (ct_64[3])

    # Step 3: Decrypt the cipher text
    pt0_0, pt0_1 = speck_cipher.decrypt(ct_64[0], ct_64[1])
    pt1_0, pt1_1 = speck_cipher.decrypt(ct_64[2], ct_64[3])
    pt0 = (pt0_0 << 64) | (pt0_1)
    pt1 = (pt1_0 << 64) | (pt1_1)

    # Step 4: Parse the data into a list of shorts
    ptshorts = []
    pt0_str = "%0X" % pt0
    for i in xrange(0, len(pt0_str), 4):
        short = pt0_str[i] + pt0_str[i + 1] + pt0_str[i + 2] + pt0_str[i + 3]
        ptshorts.append(int(short, 16))

    pt1_str = "%0X" % pt1
    for i in xrange(0, len(pt1_str), 4):
        short = pt1_str[i] + pt1_str[i + 1] + pt1_str[i + 2] + pt1_str[i + 3]
        ptshorts.append(int(short, 16))

    return ptshorts
コード例 #43
0
ファイル: tests.py プロジェクト: maojui/writeups
    def test_ofb_mode_chain(self):
        plaintxts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        ciphertexts = [c.encrypt(x) for x in plaintxts]
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        decryptexts = [c.decrypt(x) for x in ciphertexts]

        assert plaintxts == decryptexts
コード例 #44
0
ファイル: tests.py プロジェクト: maojui/writeups
    def test_ctr_mode_equivalent(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)
        ctr_out = c.encrypt(self.plaintxt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')
        ecb_out = c.encrypt(self.iv + self.counter)
        ctr_equivalent = ecb_out ^ self.plaintxt
        assert ctr_out == ctr_equivalent
コード例 #45
0
class TestIDCodec(unittest.TestCase):
    def setUp(self):
        key = 0x123456789ABCDEF00FEDCBA987654321
        alphabet = string.ascii_letters + string.digits
        keysize = 64
        idsize = 32

        self.codec = IDCodec(key, alphabet, keysize, idsize)

        self.cipher = SpeckCipher(key, key_size=keysize, block_size=idsize)
        self.base_codec = BaseCodec(alphabet, bits=idsize)

    def test_exception(self):
        with self.assertRaises(Exception) as e:
            self.codec.encode(0)

        with self.assertRaises(Exception) as e:
            self.codec.encode(-1)

        with self.assertRaises(Exception) as e:
            self.codec.encode(2**32 + 2)

        with self.assertRaises(Exception) as e:
            self.codec.encode(2**64)

    def test_codec(self):
        vals = [0, 2**32 - 1]
        for val in vals:
            encoded_1 = self.codec.encode(val + 1)
            encoded_2 = self.base_codec.encode(self.cipher.encrypt(val))
            self.assertEqual(encoded_1, encoded_2)

            decoded_1 = self.codec.decode(encoded_1)
            decoded_2 = self.cipher.decrypt(
                self.base_codec.decode(encoded_2)) + 1
            self.assertEqual(decoded_1, decoded_2)
コード例 #46
0
ファイル: tests.py プロジェクト: maojui/writeups
    def test_ctr_mode_single_cycle(self):

        self.counter = 0x01

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)
        ctr_out = c.encrypt(self.plaintxt)

        self.counter = 0x01

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)
        output_plaintext = c.decrypt(ctr_out)

        assert output_plaintext == self.plaintxt
コード例 #47
0
ファイル: tests.py プロジェクト: yuehann/IOT_Gateway
    def test_ofb_mode_equivalent(self):
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        ofb_encrypt = c.encrypt(self.plaintxt)
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'OFB', init=self.iv)
        ofb_decrypt = c.decrypt(ofb_encrypt)

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')
        ecb_out = c.encrypt(self.iv)
        ofb_equivalent_encrypt = ecb_out ^ self.plaintxt
        ofb_equivalent_decrypt = ecb_out ^ ofb_equivalent_encrypt

        assert ofb_encrypt == ofb_equivalent_encrypt
        assert ofb_decrypt == ofb_equivalent_decrypt
コード例 #48
0
ファイル: usage_speck.py プロジェクト: TUM-cm/iPresence
def client_server_test():
    block_size = 128
    key_token = 123456 # shared via VLC
        
    speck_server = SpeckCipher(key_token, block_size=block_size)
    
    counter = 0
    cipher_server = speck_server.encrypt(counter)
    print cipher_server
    
    speck_client = SpeckCipher(key_token, block_size=block_size)
    plain_client = speck_client.decrypt(cipher_server)
    print plain_client
    
    plain_client += 1
    cipher_client = speck_client.encrypt(plain_client)
    print cipher_client
    
    plain_server = speck_server.decrypt(cipher_client)
    print plain_server
    
    assert plain_server > counter
コード例 #49
0
    def test_cbc_mode_chain(self):

        c1 = SpeckCipher(self.key,
                         self.key_size,
                         self.block_size,
                         'CBC',
                         init=self.iv)
        c2 = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')

        cbc_iv_equivalent = self.iv
        for x in range(1000):
            cbc_input = self.plaintxt ^ cbc_iv_equivalent
            cbc_iv_equivalent = c2.encrypt(cbc_input)
            cbc_out = c1.encrypt(self.plaintxt)
            assert cbc_out == cbc_iv_equivalent
コード例 #50
0
ファイル: tests.py プロジェクト: maojui/writeups
    def test_ctr_mode_chain(self):

        c = SpeckCipher(self.key, self.key_size, self.block_size, 'CTR', init=self.iv, counter=self.counter)

        ctr_out = 0
        for x in range(1000):
            ctr_out = c.encrypt(self.plaintxt)
        c = SpeckCipher(self.key, self.key_size, self.block_size, 'ECB')

        ctr_equivalent = 0
        for x in range(1000):
            ecb_out = c.encrypt(self.iv + self.counter)
            self.counter += 1
            ctr_equivalent = ecb_out ^ self.plaintxt

        assert ctr_out == ctr_equivalent