예제 #1
0
    def test_paillier_key_generation(self):
        key = paillier.generate_keys(bits = 128)
        public = key.public()

        ptxt_original = 521
        ctxt = paillier.encrypt(public, ptxt_original)
        ptxt = paillier.decrypt(key, ctxt)
        self.assertEqual(ptxt_original, ptxt)

        # test homomorphism
        ptxt1 = 14
        ptxt2 = 19
        ctxt1 = paillier.encrypt(public, ptxt1)
        ctxt2 = paillier.encrypt(public, ptxt2)
        final_ptxt = paillier.decrypt(key, ctxt1 * ctxt2)
        self.assertEqual(final_ptxt, ptxt1 + ptxt2)

        # test average
        ptxt3 = 12
        ctxt3 = paillier.encrypt(public, ptxt3)
        ciphertext = [ctxt1, ctxt2, ctxt3]
        numerator, denominator = paillier.average(public, ciphertext)
        numerator = paillier.decrypt(key, numerator)
        denominator = paillier.decrypt(key, denominator)
        average = numerator/denominator
        self.assertAlmostEqual(average, 15)
예제 #2
0
파일: crypto.py 프로젝트: Pringley/edb
def generate_keyinfo(keyschema):
    """Return a dict of secure randomly-generated keys.

    The provided parameter `keyschema` should be a dict mapping key names to
    descriptions. For example:

        {
            'encrypt': {'type': 'block', 'bits': 256},
            'hmac': {'type': 'block', 'bits': 256},
            'homomorphic': {'type': 'paillier', 'bits': 512}
        }

    Supported types include "block" (random string of bit size) and "paillier"
    (key for the paillier cryptosystem). The default type is "block" if
    unspecified. The default number of bits is 256.

    Example return value:

        {
            'encrypt': b'\xb5c\x1d...',
            'hmac': b'\x7f\xa7\xcd...',
            'homomorphic': paillier.Key(modulus=..., ...)
        }

    """
    keyinfo = {}
    for name, attrs in keyschema.items():
        bad_attrs = set(attrs.keys()) - set(['type', 'bits'])
        if bad_attrs:
            raise EDBError("invalid schema: unexpected attrs: "
                           "{}".format(bad_attrs))
        keytype = attrs.get('type', 'block')
        try:
            keybits = int(attrs.get('bits', 256))
        except ValueError:
            raise EDBError("invalid schema: {}/bits is not int".format(name))
        if keytype == 'block':
            key = get_random_bytes(keybits // 8)
        elif keytype == 'paillier':
            key = paillier.generate_keys(keybits)
        else:
            raise EDBError("invalid schema: bad type: ".format(keytype))
        keyinfo[name] = key
    return keyinfo
예제 #3
0
def generate_keyinfo(keyschema):
    """Return a dict of secure randomly-generated keys.

    The provided parameter `keyschema` should be a dict mapping key names to
    descriptions. For example:

        {
            'encrypt': {'type': 'block', 'bits': 256},
            'hmac': {'type': 'block', 'bits': 256},
            'homomorphic': {'type': 'paillier', 'bits': 512}
        }

    Supported types include "block" (random string of bit size) and "paillier"
    (key for the paillier cryptosystem). The default type is "block" if
    unspecified. The default number of bits is 256.

    Example return value:

        {
            'encrypt': b'\xb5c\x1d...',
            'hmac': b'\x7f\xa7\xcd...',
            'homomorphic': paillier.Key(modulus=..., ...)
        }

    """
    keyinfo = {}
    for name, attrs in keyschema.items():
        bad_attrs = set(attrs.keys()) - set(['type', 'bits'])
        if bad_attrs:
            raise EDBError("invalid schema: unexpected attrs: "
                           "{}".format(bad_attrs))
        keytype = attrs.get('type', 'block')
        try:
            keybits = int(attrs.get('bits', 256))
        except ValueError:
            raise EDBError("invalid schema: {}/bits is not int".format(name))
        if keytype == 'block':
            key = get_random_bytes(keybits // 8)
        elif keytype == 'paillier':
            key = paillier.generate_keys(keybits)
        else:
            raise EDBError("invalid schema: bad type: ".format(keytype))
        keyinfo[name] = key
    return keyinfo