Serpent implementation issues in python

0

I tried to implement the Serpent block cipher from the specification in Python 3.7. Unlike the reference implementation written for python 1.5, I didn't want to operate on strings of "1" and "0", but on integers instead. I don't know exactly how much of my implementation is wrong since it starts to fail during the calculation of the prekey. This is before any use of S-Boxes or bit-slices. The only test vectors I could find for this step are from this question on Cryptography SE.

This is the relevant code:

MASK32 = (1 << 32) -1
PHI = 0x9E3779B9


def rol(word, bits):

    bits &= 31
    return ((word << bits) | (word >> (32 - bits))) & MASK32


class Serpent:

    def __init__(self, key):

        # key consists of 4, 6 or 8 32-bit words (128, 192 or 256 bit key length)
        if len(key) not in (4, 6, 8):
            raise ValueError("invalid key length")
        self.key = tuple(key)
        self.rounds = 32
        # expand key
        if len(self.key) < 8:
            self.key = self.key + (1 << 31,)
        while len(self.key) < 8:
            self.key = self.key + (0,)
        # key schedule
        # w holds the words of the pre-key
        w = [0] * 140
        w[:8] = self.key
        for i in range(132):
            w[i+8] = rol(w[i] ^ w[i+3] ^ w[i+5] ^ w[i+7] ^ PHI ^ i, 11)
        w = w[8:]  # cutting off the key
        for i in range(self.rounds+1):
            i <<= 2
            print("%08X %08X %08X %08X" % (w[i], w[i|1], w[i|2], w[i|3])

passing the key [0x15fc0d48, 0xd7f8199c, 0xbe399183, 0x4d96f327, 0x10000000, 0x00000000, 0x00000000, 0x00000000] gave the following values for w (without the key):

EC3EB632 8EB0B5AF F2ECBD75 9C0ED66B
48D3CCE4 B5F0FE41 CF21D1CB 4ECF2759
C60780A9 928CCA8B 46BAA82B 2AB4826D
9ED77100 6EF3B9AD EAC9BC3E E1DFD712
40AE97ED 5F4F81C0 71F7F847 99429B78
96283939 ACB492BD 3A6CA0F2 DE798512
B314D9A8 45C01EF1 6C45D6C6 426A3712
67636E8E 8C57CC55 31E7197D 7AEC0BD4
928514CF B022A8FF 5E011DA7 F2F4E909
72E98E0B 69955560 9BA3A424 822E129C
EE1D58AF 151F794A 92D411F4 4BEF245D
05C0A980 5E617C75 2FE68828 5ADAB527
F66149FB 7B448ABB E15CB398 197CE768
A847CD18 619413F9 EC7C6D93 56FDD30C
18BF3A32 BB4C15CF 70F90F9A 8E36881B
BFD22AAF BBABD141 D2C40FE4 2C02DEA9
BE21C4F8 646C27B2 5AF1D8E8 AF10A930
D81BCE34 BBD4C8CA D98299E2 E9434F4A
8C9DCEEC FE3BC3BA 57290B43 8C647998
82154EE9 11D3C3E2 CCCEA921 36D1BB74
65B05DCE 3802A25B F5432EB0 76D16A73
0222C321 A85288E9 C4807DA3 3A9544D8
8C1368FC 206E2F64 EDA036CE A980026E
7698B17B 70F2210A 5842251D C4FCB33E
540BF87D 41D53E24 67182C32 345732E7
8BBADACE 62B2BAB3 2F55968A E554B4C9
6DE98BCC 2403E0B7 55D962D6 29A52DE8
FED46FEB 0A2CB1D3 449E8DB7 826CACA7
DE14CA91 558D96F3 1C84D0B1 E61DA98C
FC2FF288 2105BD55 E3B29241 132888FA
997B9CA0 E09AD26A 2275BA80 B3217904
5D46C914 D15D2ACB 1B511FFB ECEA9F4B
D148EC4E 948F9F4F D74BA0AB 22990182

The first word EC3EB632 appears in the comments of the other question, but I should have got 8EB0B0FF for the second word instead of 8EB0B5AF. Does anybody know where I made a mistake?

python
cryptography
asked on Stack Overflow Apr 22, 2019 by Aemyl

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0