def load_tests(self, filename):
        comps = "Cryptodome.SelfTest.Cipher.test_vectors.wycheproof".split(".")
        with open(pycryptodome_filename(comps, filename), "rt") as file_in:
            tv_tree = json.load(file_in)

        class TestVector(object):
            pass

        result = []

        for group in tv_tree['testGroups']:

            rsa_key = RSA.import_key(group['privateKeyPem'])
            if group['sha'] == "SHA-1":
                hash_mod = SHA1
            elif group['sha'] == "SHA-224":
                hash_mod = SHA224
            elif group['sha'] == "SHA-256":
                hash_mod = SHA256
            elif group['sha'] == "SHA-384":
                hash_mod = SHA384
            elif group['sha'] == "SHA-512":
                hash_mod = SHA512
            else:
                raise ValueError("Unknown sha " + group['sha'])

            if group['mgfSha'] == "SHA-1":
                mgf = lambda x, y: MGF1(x, y, SHA1)
            elif group['mgfSha'] == "SHA-224":
                mgf = lambda x, y: MGF1(x, y, SHA224)
            elif group['mgfSha'] == "SHA-256":
                mgf = lambda x, y: MGF1(x, y, SHA256)
            elif group['mgfSha'] == "SHA-384":
                mgf = lambda x, y: MGF1(x, y, SHA384)
            elif group['mgfSha'] == "SHA-512":
                mgf = lambda x, y: MGF1(x, y, SHA512)
            else:
                raise ValueError("Unknown mgf/sha " + group['mgfSha'])

            for test in group['tests']:
                tv = TestVector()

                tv.rsa_key = rsa_key
                tv.hash_mod = hash_mod
                tv.mgf = mgf
                tv.algo = "%s with MGF1/%s" % (group['sha'], group['mgfSha'])

                tv.id = test['tcId']
                tv.comment = test['comment']
                for attr in 'msg', 'ct', 'label':
                    setattr(tv, attr, unhexlify(test[attr]))
                tv.valid = test['result'] != "invalid"
                tv.warning = test['result'] == "acceptable"

                result.append(tv)
        return result
Ejemplo n.º 2
0
 def filter_mgf(group):
     if group['mgfSha'] == "SHA-1":
         return lambda x, y: MGF1(x, y, SHA1)
     elif group['mgfSha'] == "SHA-224":
         return lambda x, y: MGF1(x, y, SHA224)
     elif group['mgfSha'] == "SHA-256":
         return lambda x, y: MGF1(x, y, SHA256)
     elif group['mgfSha'] == "SHA-384":
         return lambda x, y: MGF1(x, y, SHA384)
     elif group['mgfSha'] == "SHA-512":
         return lambda x, y: MGF1(x, y, SHA512)
     else:
         raise ValueError("Unknown mgf/sha " + group['mgfSha'])
Ejemplo n.º 3
0
    def __init__(self, key, hashAlgo, mgfunc, label, randfunc):
        """Initialize this PKCS#1 OAEP cipher object.

        :Parameters:
         key : an RSA key object
                If a private half is given, both encryption and decryption are possible.
                If a public half is given, only encryption is possible.
         hashAlgo : hash object
                The hash function to use. This can be a module under `Cryptodome.Hash`
                or an existing hash object created from any of such modules. If not specified,
                `Cryptodome.Hash.SHA1` is used.
         mgfunc : callable
                A mask generation function that accepts two parameters: a string to
                use as seed, and the lenth of the mask to generate, in bytes.
                If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice).
         label : bytes/bytearray/memoryview
                A label to apply to this particular encryption. If not specified,
                an empty string is used. Specifying a label does not improve
                security.
         randfunc : callable
                A function that returns random bytes.

        :attention: Modify the mask generation function only if you know what you are doing.
                    Sender and receiver must use the same one.
        """
        self._key = key

        if hashAlgo:
            self._hashObj = hashAlgo
        else:
            self._hashObj = Cryptodome.Hash.SHA1

        if mgfunc:
            self._mgf = mgfunc
        else:
            self._mgf = lambda x,y: MGF1(x,y,self._hashObj)

        self._label = _copy_bytes(None, None, label)
        self._randfunc = randfunc
Ejemplo n.º 4
0
 def mgf(x, y, mh=mgf1_hash):
     return MGF1(x, y, mh)
Ejemplo n.º 5
0
 def _mgf1_fun(x, y):
     return MGF1(x, y, SHA)