def carryDigits(digits): bits = BitArray(bin='000') for digit in digits: bits = bits ^ digit bits.rol(1) return bits
def decode(data, padding_at_begin=True): """ base32 encoded string to bytes data """ if not isinstance(data, str): raise Exception('decode error: only string accepted.') bit_array = BitArray() for s in data: if s in ALPTHABET: i = ALPTHABET.find(s) bit_array.append(BitArray(uint=i, length=5)) else: raise Exception('decode error: char not in the ALPTHABET list: %s' % s) # remove padding 0 bits padding = len(bit_array) % 8 if padding > 0: if padding_at_begin: bit_array.rol(padding) pad_char = bit_array[-padding:] if(pad_char.int > 0): raise Exception('decode error: none zero padding') bit_array = bit_array[:len(bit_array)-padding] bytes_data = bit_array.bytes return bytes_data
def roundKeyGen(self, initialKey, operation): cKey = BitArray() dKey = BitArray() self.roundKeys = list() for roundCount in range(0, 16): #Split 56-bit key into C and D cKey = initialKey[0:28] dKey = initialKey[28:56] #Rotate to the left by the amount appropriate for each round cKey.rol(self.roundShift[roundCount]) dKey.rol(self.roundShift[roundCount]) #Put them back together for the 56-bit key cKey.append(dKey) initialKey = cKey #The 48-bit permutation is calculated and stored in the list of round keys #The 56-bit key is kept and saved to calculate the next round key self.roundKeys.append(self.permuteBits(initialKey, self.ROUND_P)) #If we're doing a decryption we need to run the round keys in reverse order. if (operation == 'DECRYPT'): roundKeyReversal = list() i = 0 for i in range(0, 16): roundKeyReversal.append(self.roundKeys[15 - i]) self.roundKeys = roundKeyReversal
def op(a, b, c, d, k, s, i, f): # a +F(b,c,d) + X[k] + T[i] inner_sum = (a.int + F(b, c, d).int + X[k] + T[i]) % 2 ^ 32 inner_array = BitArray(uint=inner_sum, length=32) # <<< s inner_array.rol(s) # + b result = (b.int + inner_array.int) % 2 ^ 32 # assign the result into A return BitArray(uint=result, length=32)
def t_iter(t, a, b, c, d, e, sched): f = hash_function(t) K = hash_constant(t) xa = BitArray(a) xa.rol(5) T = mod_add(xa, f(b,c,d)) T = mod_add(T, e) T = mod_add(T, K) T = mod_add(T, sched[t]) e = d d = c b.rol(30) c = b b = a a = T return a, b, c, d, e
def swizzle(deswizzled_bytes: bytes, checksum: int) -> BitArray: swizzled_bits = BitArray() checksum_ba = BitArray(uintbe=checksum, length=32) for i in range(10): b = BitArray(uint=deswizzled_bytes[i], length=8) offset = 32 - (i * 3) r = checksum_ba[offset - 3:offset] b.rol(r.uint) offset = 32 - (i * 2) v = checksum_ba[offset - 8:offset] c = v ^ b swizzled_bits.append(c) logging.debug(f'Swizzled bytes: {swizzled_bits.hex}') return swizzled_bits
def get_subkeys(seed: BitArray, debug=False): """ Get the set of 16 48-bit subkeys needed for the DES algorithm seed determines the value of these keys :param seed: seed key :param debug: set to true if debugging print statements are needed :return: an array of the 16 subkeys """ # initialize C and D arrays to all zeros C = [[False] * 28 for i in range(0, 17)] D = [[False] * 28 for i in range(0, 17)] # first, get initial values for C and D key_permuted = do_permutation(seed, vals.pc_1) if debug: print('Permuted key value: %s' % str(key_permuted)) C[0] = BitArray(flatten_list(key_permuted[0:4])) D[0] = BitArray(flatten_list(key_permuted[4:])) shift_amnts = vals.keygen_shift_table for i in range(1, 17): C_prev = BitArray(C[i - 1]) C_prev.rol(shift_amnts[i - 1]) C[i] = C_prev D_prev = BitArray(D[i - 1]) D_prev.rol(shift_amnts[i - 1]) D[i] = D_prev # print values of C and D if debug flag is set if debug: print('\nC values:') for i in range(0, len(C)): print('C_%s = %s' % (i, C[i].bin)) print('\nD values:') for i in range(0, len(D)): print('D_%s = %s' % (i, D[i].bin)) # create the subkeys based on the C and D values K = [] for i in range(0, 17): k_temp = BitArray(C[i] + D[i]) K.append(BitArray(flatten_list(do_permutation(k_temp, vals.pc_2)))) if debug: print('\nSubkey values:') for i in range(0, len(K)): print('K_%s = %s' % (i, K[i].bin)) return K
def testRol(self): s = BitArray("0b0001") s.rol(1) self.assertEqual(s, "0b0010")
def testRol(self): s = BitArray('0b0001') s.rol(1) self.assertEqual(s, '0b0010')
def circular_permutation_left(m, R): # Tested and works rol_m = BitArray(m) rol_m.rol(R) return (bytearray(rol_m.bytes))
def sha1(message): h0 = BitArray('0x67452301') h1 = BitArray('0xEFCDAB89') h2 = BitArray('0x98BADCFE') h3 = BitArray('0x10325476') h4 = BitArray('0xC3D2E1F0') m1 = len(message) * 8 hex_str = '0x' + ''.join([str(hex(ord(x)))[2:] for x in message]) ba = BitArray(hex_str) ba += BitArray('0b1') while len(ba) % 512 != 448: ba += BitArray('0b0') size = BitArray(bin(m1)) while len(size) < 64: size.prepend('0b0') ba.append(size) assert (len(ba) == 512) w = [BitArray('0x00000000') for x in range(80)] for i in range(16): w[i] = ba[32 * i:32 * (i + 1)] # pdb.set_trace() for i in range(16, 80): w[i] = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]) w[i].rol(1) # debugging # for x in range(len(w)): # if (x+1) % 4: # print(w[x].hex, end=' ') # else: # print(w[x].hex) # print('\n') a = BitArray(h0) b = BitArray(h1) c = BitArray(h2) d = BitArray(h3) e = BitArray(h4) for i in range(80): if 0 <= i <= 19: f = (b & c) | ((~b) & d) k = BitArray('0x5A827999') elif 20 <= i <= 39: f = b ^ c ^ d print(f.hex) k = BitArray('0x6ED9EBA1') elif 40 <= i <= 59: f = (b & c) | (b & d) | (c & d) k = BitArray('0x8F1BBCDC') elif 60 <= i <= 79: f = b ^ c ^ d k = BitArray('0xCA62C1D6') f = f[-32:] # cut it to 32 bits rotated_tmp = BitArray(a) rotated_tmp.rol(5) temp = reduce(bit_add32, [rotated_tmp, f, e, k, w[i]]) e = d d = c rotated_tmp = BitArray(b) rotated_tmp.rol(30) c = rotated_tmp b = a a = temp # print(a.hex + ' ' + b.hex + ' ' + c.hex + ' ' + d.hex + ' ' + e.hex) # print() h0 = bit_add32(h0, a) h1 = bit_add32(h1, b) h2 = bit_add32(h2, c) h3 = bit_add32(h3, d) h4 = bit_add32(h4, e) #pdb.set_trace() hh = str(h0)[2:] + str(h1)[2:] + str(h2)[2:] + str(h3)[2:] + str(h4)[2:] return hh