Beispiel #1
0
    def test_CMAC_test_vectors(self):
        # test using official RFC AES_CMAC test vectors
        key = Fernet.generate_key()
        fernet = Fernet(key)

        K = '2b7e151628aed2a6abf7158809cf4f3c'.decode('hex')

        LENGTH = 0
        M = ''
        CMAC = 'bb1d6929e95937287fa37d129b756746'.decode('hex')
        assert fernet._AES_CMAC_generate(K, M, LENGTH) == CMAC

        LENGTH = 16
        M = '6bc1bee22e409f96e93d7e117393172a'.decode('hex')
        CMAC = '070a16b46b4d4144f79bdd9dd04a287c'.decode('hex')
        assert fernet._AES_CMAC_generate(K, M, LENGTH) == CMAC

        LENGTH = 40
        M = '6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411'.decode('hex')
        CMAC = 'dfa66747de9ae63030ca32611497c827'.decode('hex')
        assert fernet._AES_CMAC_generate(K, M, LENGTH) == CMAC

        LENGTH = 64
        M = '6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'.decode('hex')
        CMAC = '51f0bebf7e3b9d92fc49741779363cfe'.decode('hex')
        assert fernet._AES_CMAC_generate(K, M, LENGTH) == CMAC
Beispiel #2
0
    def test_CMAC_verify(self):
        # test CMAC verification function
        key = Fernet.generate_key()
        fernet = Fernet(key)

        # test against the cryptography.io library APIs
        for i in range(20):
            M = os.urandom(i)
            K = os.urandom(16)

            # use library API generation
            c = cmac.CMAC(algorithms.AES(K), backend=default_backend())
            c.update(M)
            lib_cmac = c.finalize()

            # verify a library generated cmac
            assert fernet._AES_CMAC_verify(K, M, len(M), lib_cmac)

            # verify against our own API
            cmac_ = fernet._AES_CMAC_generate(K, M, len(M))
            assert fernet._AES_CMAC_verify(K, M, len(M), cmac_)

            # # library verify our generated cmac
            c = cmac.CMAC(algorithms.AES(K), backend=default_backend())
            c.update(M)
            try:
                c.verify(cmac_)
            except Exception:
                raise Exception
Beispiel #3
0
    def test_CMAC_generate(self):
        # test AES_CMAC generation
        key = Fernet.generate_key()
        fernet = Fernet(key)

        # test against the cryptographi.io library APIs
        for i in range(20):
            M = os.urandom(i)
            K = os.urandom(16)

            # use library's API to generate CMAC
            c = cmac.CMAC(algorithms.AES(K), backend=default_backend())
            c.update(M)
            lib_cmac = c.finalize()

            # use our function to generate CMAC
            cmac_ = fernet._AES_CMAC_generate(K, M, len(M))

            # verify library CMAC vs our CMAC
            assert cmac_ == lib_cmac