Esempio n. 1
0
def sm3_CF(v_i, b_i):
    # 消息扩展
    w = []
    for i in range(16):
        # 消息按512bit进行分组
        weight = 0x1000000
        data = 0
        for k in range(i * 4, (i + 1) * 4):
            data = data + b_i[k] * weight
            weight = int(weight / 0x100)
        w.append(data)

    for j in range(16, 68):
        # 消息扩展 16 TO 67, 为下面的循环做准备
        w.append(0)
        w[j] = sm3_P_1(w[j - 16] ^ w[j - 9] ^ (rotl(w[j - 3], 15 % 32))) ^ (
            rotl(w[j - 13], 7 % 32)) ^ w[j - 6]
        str1 = "%08x" % w[j]
    w_1 = []
    for j in range(0, 64):
        # 消息扩展 0 TO 63
        w_1.append(0)
        w_1[j] = w[j] ^ w[j + 4]
        str1 = "%08x" % w_1[j]

    a, b, c, d, e, f, g, h = v_i

    for j in range(0, 64):
        # 压缩函数
        ss_1 = rotl(
            ((rotl(a, 12 % 32)) + e + (rotl(T_j[j], j % 32))) & 0xffffffff,
            7 % 32)
        ss_2 = ss_1 ^ (rotl(a, 12 % 32))
        tt_1 = (sm3_FF_j(a, b, c, j) + d + ss_2 + w_1[j]) & 0xffffffff
        tt_2 = (sm3_GG_j(e, f, g, j) + h + ss_1 + w[j]) & 0xffffffff
        d = c
        c = rotl(b, 9 % 32)
        b = a
        a = tt_1
        h = g
        g = rotl(f, 19 % 32)
        f = e
        e = sm3_P_0(tt_2)

        a, b, c, d, e, f, g, h = map(lambda x: x & 0xFFFFFFFF,
                                     [a, b, c, d, e, f, g, h])

    v_j = [a, b, c, d, e, f, g, h]
    return [v_j[i] ^ v_i[i] for i in range(8)]
Esempio n. 2
0
    def sm3_cf(self, v_i, b_i):
        w = []
        for i in range(16):
            weight = 0x1000000
            data = 0
            for k in range(i * 4, (i + 1) * 4):
                data = data + b_i[k] * weight
                weight = int(weight / 0x100)
            w.append(data)

        for j in range(16, 68):
            w.append(0)
            w[j] = self.sm3_p_1(w[j - 16] ^ w[j - 9]
                                ^ (rotl(w[j - 3], 15 % 32))) ^ (rotl(
                                    w[j - 13], 7 % 32)) ^ w[j - 6]
            str1 = "%08x" % w[j]
        w_1 = []
        for j in range(0, 64):
            w_1.append(0)
            w_1[j] = w[j] ^ w[j + 4]
            str1 = "%08x" % w_1[j]

        a, b, c, d, e, f, g, h = v_i

        for j in range(0, 64):
            ss_1 = rotl(((rotl(a, 12 % 32)) + e + (rotl(self.T_j[j], j % 32)))
                        & 0xffffffff, 7 % 32)
            ss_2 = ss_1 ^ (rotl(a, 12 % 32))
            tt_1 = (self.sm3_ff_j(a, b, c, j) + d + ss_2 + w_1[j]) & 0xffffffff
            tt_2 = (self.sm3_gg_j(e, f, g, j) + h + ss_1 + w[j]) & 0xffffffff
            d = c
            c = rotl(b, 9 % 32)
            b = a
            a = tt_1
            h = g
            g = rotl(f, 19 % 32)
            f = e
            e = self.sm3_p_0(tt_2)

            a, b, c, d, e, f, g, h = map(lambda x: x & 0xFFFFFFFF,
                                         [a, b, c, d, e, f, g, h])

        v_j = [a, b, c, d, e, f, g, h]
        return [v_j[i] ^ v_i[i] for i in range(8)]
Esempio n. 3
0
def sm3_p_1(x):
    return x ^ (rotl(x, 15 % 32)) ^ (rotl(x, 23 % 32))
Esempio n. 4
0
def sm3_p_0(x):
    return x ^ (rotl(x, 9 % 32)) ^ (rotl(x, 17 % 32))
Esempio n. 5
0
def sm3_P_1(x):
    # 置换函数 P1; 右移15位, 右移23位
    return x ^ (rotl(x, 15 % 32)) ^ (rotl(x, 23 % 32))
Esempio n. 6
0
def sm3_P_0(x):
    # 置换函数P0; 右移9位, 右移17位
    return x ^ (rotl(x, 9 % 32)) ^ (rotl(x, 17 % 32))