Exemple #1
0
static void
runtime_test(void)
{
    /* size_t should be able to represent the length of any size buffer */
    IMP_ASSERT(sizeof(size_t) == sizeof(void *));

    /* we must be able to perform the assignment (Py_ssize_t) -> (size_t)
     * as long as the value is non-negative. */
    IMP_ASSERT(sizeof(size_t) >= sizeof(Py_ssize_t));

    /* char must be one octet */
    IMP_ASSERT(sizeof(char) == 1);

    /* Perform a basic test of the xor_strings function, including a test for
     * an off-by-one bug. */
    {
        char x[7] = "\x00hello";    /* NUL + "hello" + NUL */
        char y[7] = "\xffworld";    /* 0xff + "world" + NUL */
        char z[9] = "[ABCDEFG]";    /* "[ABCDEFG]" + NUL */

        xor_strings(z+1, x, y, 7);
        IMP_ASSERT(!memcmp(z, "[\xff\x1f\x0a\x1e\x00\x0b\x00]", 9));
    }

    /* Perform a basic test of the xor_string_with_char function, including a test for
     * an off-by-one bug. */
    {
        char x[7] = "\x00hello";    /* NUL + "hello" + NUL */
        char y = 170;               /* 0xaa */
        char z[9] = "[ABCDEFG]";    /* "[ABCDEFG]" + NUL */

        xor_string_with_char(z+1, x, y, 7);
        IMP_ASSERT(!memcmp(z, "[\xaa\xc2\xcf\xc6\xc6\xc5\xaa]", 9));
    }
}
Exemple #2
0
static PyObject *
strxor_function(PyObject *self, PyObject *args)
{
    PyObject *a, *b, *retval;
    Py_ssize_t len_a, len_b;

    if (!PyArg_ParseTuple(args, "SS", &a, &b))
        return NULL;

    len_a = PyBytes_GET_SIZE(a);
    len_b = PyBytes_GET_SIZE(b);

    assert(len_a >= 0);
    assert(len_b >= 0);

    if (len_a != len_b) {
        PyErr_SetString(PyExc_ValueError, "length of both strings must be equal");
        return NULL;
    }

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, len_a);
    if (!retval) {
        return NULL;
    }

    /* retval := a ^ b */
    xor_strings(PyBytes_AS_STRING(retval), PyBytes_AS_STRING(a), PyBytes_AS_STRING(b), len_a);

    return retval;
}
Exemple #3
0
std::string normal_CFB_decrypt(const SymAlg::Ptr & crypt, const std::string & data, std::string IV){
    std::string out = "";
    const std::size_t BS = crypt -> blocksize() >> 3;
    std::string::size_type x = 0;
    while (x < data.size()){
        out += xor_strings(crypt -> encrypt(IV), data.substr(x, BS));
        IV = data.substr(x, BS);
        x += BS;
    }
    return out;
}
Exemple #4
0
static PyObject *
strxor_function(PyObject *self, PyObject *args)
{
    char *a, *b;
    PyObject *retval;
    int len_a, len_b;
#ifdef HAS_NEW_BUFFER
    Py_buffer a_view, b_view;
    if (!PyArg_ParseTuple(args, "s*s*", &a_view, &b_view))
        return NULL;
    a = (char*)a_view.buf;
    len_a = a_view.len;
    b = (char*)b_view.buf;
    len_b = b_view.len;
#else
    if (!PyArg_ParseTuple(args, "s#s#", &a, &len_a, &b, &len_b))
        return NULL;
#endif
    assert(len_a >= 0);
    assert(len_b >= 0);

    if (len_a != len_b) {
        PyErr_SetString(PyExc_ValueError, "length of both strings must be equal");
#ifdef HAS_NEW_BUFFER
        PyBuffer_Release(&a_view);
        PyBuffer_Release(&b_view);
#endif
        return NULL;
    }

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, len_a);
    if (!retval) {
#ifdef HAS_NEW_BUFFER
        PyBuffer_Release(&a_view);
        PyBuffer_Release(&b_view);
#endif
        return NULL;
    }

    /* retval := a ^ b */
    xor_strings(PyBytes_AS_STRING(retval), a, b, len_a);

#ifdef HAS_NEW_BUFFER
    PyBuffer_Release(&a_view);
    PyBuffer_Release(&b_view);
#endif
    return retval;
}
Exemple #5
0
int main (int argc, char **argv)
{
  char *result;
  char *bob = "Bob";
  char *eve = "Eve";
  puts ("binary calculations:");
  printf ("bob:\t\t");
  binary_print_string(bob, strlen(bob));
  printf ("eve:\t\t");
  binary_print_string(eve, strlen(eve));
  
  result = xor_strings(bob, eve, strlen(bob));
  printf ("xor result:\t");
  binary_print_string(result, strlen(result));
  
  puts ("hex values:");  
  printf ("bob:\t\t");
  hex_print_string(bob, strlen(bob));
  printf ("eve:\t\t");
  hex_print_string(eve, strlen(eve));
  printf ("xor result:\t");
  hex_print_string(result, strlen(result));
  free (result); // good habits save lives
}