def ModExp(a, b, c):
    """Uses openssl, if available, to do a^b mod c where a,b,c are longs."""
    if not _FOUND_SSL:
        return pow(a, b, c)
    # convert arbitrary long args to bytes
    bytes_a = number.LongToBytes(a)
    bytes_b = number.LongToBytes(b)
    bytes_c = number.LongToBytes(c)

    # convert bytes to (pointer to) Bignums.
    bn_a = ssl.BN_bin2bn(bytes_a, len(bytes_a), 0)
    bn_b = ssl.BN_bin2bn(bytes_b, len(bytes_b), 0)
    bn_c = ssl.BN_bin2bn(bytes_c, len(bytes_c), 0)
    bn_result = ssl.BN_new()
    ctx = ssl.BN_CTX_new()

    # exponentiate and convert result to long
    ssl.BN_mod_exp(bn_result, bn_a, bn_b, bn_c, ctx)
    num_bytes_in_result = _NumBytesBn(bn_result)
    bytes_result = ctypes.create_string_buffer(num_bytes_in_result)
    ssl.BN_bn2bin(bn_result, bytes_result)
    long_result = number.BytesToLong(bytes_result.raw)

    # clean up
    ssl.BN_CTX_free(ctx)
    ssl.BN_free(bn_a)
    ssl.BN_free(bn_b)
    ssl.BN_free(bn_c)
    ssl.BN_free(bn_result)

    return long_result
Exemple #2
0
 def testLongToBytes(self):
     self.assertEqual(struct.pack('>I', 3), number.LongToBytes(3))
     self.assertEqual(struct.pack('>2I', 1, 7),
                      number.LongToBytes(4294967303))
     self.assertEqual(struct.pack('>3I', 1, 0, 0),
                      number.LongToBytes(2**64))
     self.assertEqual(
         struct.pack('>6I', 1, 1, 1, 1, 1, 7),
         number.LongToBytes(2**160 + 2**128 + 2**96 + 2**64 + 2**32 + 7))
 def __init__(self, key):
   super(HomomorphicFloatCipher, self).__init__()
   self._paillier = pcrypto.Paillier(key)
   # Store raw binary nsquare as a string using '\x' syntax which can be passed
   # in a query.
   nsquare_bytes = number.LongToBytes(self._paillier.nsquare)
   self.nsquare = '\\x' + '\\x'.join(x.encode('hex') for x in nsquare_bytes)
    def Encrypt(self, plaintext):
        """Encrypts float and returns a base64 encoding of ciphertext as bytes.

    Args:
      plaintext: float

    Returns:
      encrypted plaintext, converted to bytes, and base64 encoded.

    Raises:
      ValueError: when plaintext is not a float.
    """
        if not isinstance(plaintext, float):
            raise ValueError('Expected float type data but got: %s' %
                             type(plaintext))
        return base64.b64encode(
            number.LongToBytes(self._paillier.EncryptFloat(plaintext)))
    def Encrypt(self, plaintext):
        """Encrypts int64 and returns a base64 encoding of ciphertext as bytes.

    Args:
      plaintext: either a int or long to be encrypted

    Returns:
      encrypted plaintext, converted to bytes, and base64 encoded.

    Raises:
      ValueError: when plaintext is neither an int, nor a long.
    """
        if not isinstance(plaintext, int) and not isinstance(plaintext, long):
            raise ValueError('Expected int or long type data but got: %s' %
                             type(plaintext))
        return base64.b64encode(
            number.LongToBytes(self._paillier.EncryptInt64(plaintext)))