Ejemplo n.º 1
0
Array f_mcrypt_module_get_supported_key_sizes(const String& algorithm,
        const String& lib_dir /* = null_string */) {
    String dir = lib_dir.empty() ? String(MCG(algorithms_dir)) : lib_dir;

    int count = 0;
    int *key_sizes = mcrypt_module_get_algo_supported_key_sizes
                     ((char*)algorithm.data(), (char*)dir.data(), &count);

    Array ret = Array::Create();
    for (int i = 0; i < count; i++) {
        ret.append(key_sizes[i]);
    }
    mcrypt_free(key_sizes);
    return ret;
}
Ejemplo n.º 2
0
Array f_mcrypt_list_modes(CStrRef lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? MCG(modes_dir) : lib_dir;

  int count = 0;
  char **modules = mcrypt_list_modes((char*)dir.data(), &count);
  if (count == 0) {
    raise_warning("No modes found in module dir");
  }
  Array ret = Array::Create();
  for (int i = 0; i < count; i++) {
    ret.append(String(modules[i], CopyString));
  }
  mcrypt_free_p(modules, count);
  return ret;
}
Ejemplo n.º 3
0
Array HHVM_FUNCTION(mcrypt_list_algorithms,
                    const String& lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? String(MCG(algorithms_dir)) : lib_dir;

  int count = 0;
  char **modules = mcrypt_list_algorithms((char*)dir.data(), &count);
  if (count == 0) {
    raise_warning("No algorithms found in module dir");
  }
  Array ret = Array::Create();
  for (int i = 0; i < count; i++) {
    ret.append(String(modules[i], CopyString));
  }
  mcrypt_free_p(modules, count);
  return ret;
}
Ejemplo n.º 4
0
static Variant php_mcrypt_do_crypt(CStrRef cipher, CStrRef key, CStrRef data,
                                   CStrRef mode, CStrRef iv, bool dencrypt) {
  MCRYPT td = mcrypt_module_open((char*)cipher.data(),
                                 (char*)MCG(algorithms_dir).data(),
                                 (char*)mode.data(),
                                 (char*)MCG(modes_dir).data());
  if (td == MCRYPT_FAILED) {
    raise_warning(MCRYPT_OPEN_MODULE_FAILED);
    return false;
  }

  /* Checking for key-length */
  int max_key_length = mcrypt_enc_get_key_size(td);
  if (key.size() > max_key_length) {
    raise_warning("Size of key is too large for this algorithm");
  }
  int count;
  int *key_length_sizes = mcrypt_enc_get_supported_key_sizes(td, &count);
  int use_key_length;
  char *key_s = NULL;
  if (count == 0 && key_length_sizes == NULL) { // all lengths 1 - k_l_s = OK
    use_key_length = key.size();
    key_s = (char*)malloc(use_key_length);
    memcpy(key_s, key.data(), use_key_length);
  } else if (count == 1) {  /* only m_k_l = OK */
    key_s = (char*)malloc(key_length_sizes[0]);
    memset(key_s, 0, key_length_sizes[0]);
    memcpy(key_s, key.data(), MIN(key.size(), key_length_sizes[0]));
    use_key_length = key_length_sizes[0];
  } else { /* dertermine smallest supported key > length of requested key */
    use_key_length = max_key_length; /* start with max key length */
    for (int i = 0; i < count; i++) {
      if (key_length_sizes[i] >= key.size() &&
          key_length_sizes[i] < use_key_length) {
        use_key_length = key_length_sizes[i];
      }
    }
    key_s = (char*)malloc(use_key_length);
    memset(key_s, 0, use_key_length);
    memcpy(key_s, key.data(), MIN(key.size(), use_key_length));
  }
  mcrypt_free(key_length_sizes);

  /* Check IV */
  char *iv_s = NULL;
  int iv_size = mcrypt_enc_get_iv_size(td);

  /* IV is required */
  if (mcrypt_enc_mode_has_iv(td) == 1) {
    if (!iv.empty()) {
      if (iv_size != iv.size()) {
        raise_warning("The IV parameter must be as long as the blocksize");
      } else {
        iv_s = (char*)malloc(iv_size + 1);
        memcpy(iv_s, iv.data(), iv_size);
      }
    } else {
      raise_warning("Attempt to use an empty IV, which is NOT recommended");
      iv_s = (char*)malloc(iv_size + 1);
      memset(iv_s, 0, iv_size + 1);
    }
  }

  int block_size;
  unsigned long int data_size;
  String s;
  char *data_s;
  /* Check blocksize */
  if (mcrypt_enc_is_block_mode(td) == 1) { /* It's a block algorithm */
    block_size = mcrypt_enc_get_block_size(td);
    data_size = (((data.size() - 1) / block_size) + 1) * block_size;
    s = String(data_size, ReserveString);
    data_s = (char*)s.mutableSlice().ptr;
    memset(data_s, 0, data_size);
    memcpy(data_s, data.data(), data.size());
  } else { /* It's not a block algorithm */
    data_size = data.size();
    s = String(data_size, ReserveString);
    data_s = (char*)s.mutableSlice().ptr;
    memcpy(data_s, data.data(), data.size());
  }

  if (mcrypt_generic_init(td, key_s, use_key_length, iv_s) < 0) {
    raise_warning("Mcrypt initialisation failed");
    return false;
  }
  if (dencrypt) {
    mdecrypt_generic(td, data_s, data_size);
  } else {
    mcrypt_generic(td, data_s, data_size);
  }

  /* freeing vars */
  mcrypt_generic_end(td);
  if (key_s != NULL) {
    free(key_s);
  }
  if (iv_s != NULL) {
    free(iv_s);
  }
  return s.setSize(data_size);
}
Ejemplo n.º 5
0
bool f_mcrypt_module_self_test(CStrRef algorithm,
                               CStrRef lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? MCG(algorithms_dir) : lib_dir;
  return mcrypt_module_self_test((char*)algorithm.data(),
                                 (char*)dir.data()) == 0;
}
Ejemplo n.º 6
0
bool f_mcrypt_module_is_block_mode(CStrRef mode,
                                   CStrRef lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? MCG(modes_dir) : lib_dir;
  return mcrypt_module_is_block_mode((char*)mode.data(),
                                     (char*)dir.data()) == 1;
}
Ejemplo n.º 7
0
bool f_mcrypt_module_is_block_algorithm(CStrRef algorithm,
                                        CStrRef lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? MCG(algorithms_dir) : lib_dir;
  return mcrypt_module_is_block_algorithm((char*)algorithm.data(),
                                          (char*)dir.data()) == 1;
}
Ejemplo n.º 8
0
int64_t f_mcrypt_module_get_algo_key_size(CStrRef algorithm,
                                      CStrRef lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? MCG(algorithms_dir) : lib_dir;
  return mcrypt_module_get_algo_key_size((char*)algorithm.data(),
                                         (char*)dir.data());
}
Ejemplo n.º 9
0
bool f_mcrypt_module_self_test(const String& algorithm,
                               const String& lib_dir /* = null_string */) {
    String dir = lib_dir.empty() ? String(MCG(algorithms_dir)) : lib_dir;
    return mcrypt_module_self_test((char*)algorithm.data(),
                                   (char*)dir.data()) == 0;
}
Ejemplo n.º 10
0
bool f_mcrypt_module_is_block_mode(const String& mode,
                                   const String& lib_dir /* = null_string */) {
    String dir = lib_dir.empty() ? String(MCG(modes_dir)) : lib_dir;
    return mcrypt_module_is_block_mode((char*)mode.data(),
                                       (char*)dir.data()) == 1;
}
Ejemplo n.º 11
0
int64_t f_mcrypt_module_get_algo_key_size(const String& algorithm,
        const String& lib_dir /* = null_string */) {
    String dir = lib_dir.empty() ? String(MCG(algorithms_dir)) : lib_dir;
    return mcrypt_module_get_algo_key_size((char*)algorithm.data(),
                                           (char*)dir.data());
}
Ejemplo n.º 12
0
bool HHVM_FUNCTION(mcrypt_module_is_block_algorithm, const String& algorithm,
                                  const String& lib_dir /* = null_string */) {
  String dir = lib_dir.empty() ? String(MCG(algorithms_dir)) : lib_dir;
  return mcrypt_module_is_block_algorithm((char*)algorithm.data(),
                                          (char*)dir.data()) == 1;
}