int test_xor_buffer(void) { /* We will test the function on small integers only. If it works there, * it probably always works, given it is a very generic function. */ uint32_t a, b, out, t; for (t = 0; t < 1024; ++t) { a = (uint32_t)t * 7919; b = (uint32_t)t * 3637; out = a ^ b; /* Expected output. */ xor_buffer(&a, &b, sizeof(uint32_t)); ASSERT_EQ(a, out); } /* Finally, check the function does nothing on zero-length inputs. */ a = 0x5a; xor_buffer(&a, &b, 0); ASSERT_EQ(a, 0x5a); return 1; }
static void cfb_decrypt_update(struct CFB_STATE *state, struct BLOCK_STATE *cipher_state, const void *in, size_t inlen, void *out, size_t *outlen) { if (outlen) *outlen = 0; while (inlen != 0) { size_t block_size = state->block_size; size_t process = 0; if (state->remaining == 0) { block_forward(cipher_state, state->iv); state->remaining = block_size; } process = (inlen < state->remaining) ? inlen : state->remaining; if (out != in) memcpy(out, in, process); memcpy(state->tmp, in, process); xor_buffer(out, offset(state->iv, block_size - state->remaining), process); memcpy(offset(state->iv, block_size - state->remaining), state->tmp, process); if (outlen) (*outlen) += process; state->remaining -= process; out = offset(out, process); in = offset(in, process); inlen -= process; } }
static void genuid_glib_pseudo (guchar *buffer, int length) { static guint32 inc = 0; int i; INC_LOCK (); inc++; for (i = 0; i < length; i++) { buffer [i] = g_rand_int_range (glib_prng, 0, 255); if (i < sizeof (guint32)) buffer [i] ^= ((guchar *) &inc) [i]; } xor_buffer (buffer, length); INC_UNLOCK (); }
DWORD queue_func(BYTE* pParam, DWORD param_size) { queue_func_params_t * params = (queue_func_params_t*)pParam; unsigned char checksum = 0; unsigned char buffer[16 * 1024]; params->succ = false; DWORD ret = -1; // we must open the file each time, because we need separate file pointers HANDLE hfile = CreateFile(params->filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hfile == INVALID_HANDLE_VALUE) { print_last_error(_T("CreateFile")); return -1; } // seek to offset if (SetFilePointer(hfile, params->offset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { print_last_error(_T("SetFilePointer")); goto cleanup; } // read the file in chunks and compute the checksum for (int i = 0; i < params->size; ) { DWORD readcount; if (!ReadFile(hfile, buffer, sizeof(buffer), &readcount, NULL)) { print_last_error(_T("ReadFile")); goto cleanup; } if (readcount <= 0) { _tprintf(_T("read count is not > 0\n")); goto cleanup; } i += readcount; checksum ^= xor_buffer(buffer, readcount); } params->succ = true; ret = checksum; cleanup: CloseHandle(hfile); return ret; }
static void genuid_simple (guchar *buffer, int length) { static guint32 inc = 0; g_assert (length >= 4); p_memzero (buffer, length); if (length > 4) memcpy (buffer + 4, &genuid_pid, 4); if (length > 8) memcpy (buffer + 8, &genuid_uid, 4); INC_LOCK (); inc++; memcpy (buffer, &inc, 4); xor_buffer (buffer, length); INC_UNLOCK (); }
/** * Internal function to decrypt keys * * @param ctx AES context * @param iv Initializing Vector * @param iv_length Length of the Initializing Vector * @param input Crypted input buffer * @param input_length Input buffer length * @param output Decrypted result * @return TRUE if result can be trusted, FALSE otherwise */ static int aes_ccm_encrypt_decrypt( AES_CONTEXT* ctx, unsigned char* nonce, unsigned char nonce_length, unsigned char* input, unsigned int input_length, unsigned char* mac, unsigned int mac_length, unsigned char* output) { // Check parameters if(!ctx || !input || !mac || !output) return FALSE; dis_printf(L_DEBUG, "Entering aes_ccm_encrypt_decrypt...\n"); unsigned char iv[16]; unsigned int loop = 0; unsigned char tmp_buf[16] = {0,}; unsigned char* failsafe = NULL; /* * Here is how the counter works in microsoft compatible ccm implementation: * * - User supplies a less than 16 bytes and more than 12 bytes iv * * - Copy it in order to form this format: * 15-iv_length-1 (1 byte) | iv (max 14 bytes) | counter counting from zero (from 1 to 3 byte) * * - Apply counter mode of aes * * (thanks to Kumar and Kumar for these explanations) */ memset(iv, 0, sizeof(iv)); memcpy(iv + 1, nonce, (nonce_length % sizeof(iv))); if(15 - nonce_length - 1 < 0) return FALSE; *iv = (unsigned char)(15 - nonce_length - 1); AES_ECB_ENC(ctx, AES_ENCRYPT, iv, tmp_buf); dis_printf(L_DEBUG, "\tTmp buffer:\n"); hexdump(L_DEBUG, tmp_buf, 16); dis_printf(L_DEBUG, "\tInput:\n"); hexdump(L_DEBUG, mac, mac_length); xor_buffer(mac, tmp_buf, NULL, mac_length); dis_printf(L_DEBUG, "\tOutput:\n"); hexdump(L_DEBUG, mac, mac_length); /* Increment the internal iv counter */ iv[15] = 1; if(input_length > sizeof(iv)) { loop = input_length >> 4; dis_printf(L_DEBUG, "Input length: %d, loop: %d\n", input_length, loop); do { AES_ECB_ENC(ctx, AES_ENCRYPT, iv, tmp_buf); xor_buffer(input, tmp_buf, output, sizeof(iv)); iv[15]++; /* A failsafe to not have the same iv twice */ if(!iv[15]) { failsafe = &iv[15]; do { failsafe--; (*failsafe)++; } while(*failsafe == 0 && failsafe >= &iv[0]); } input += sizeof(iv); output += sizeof(iv); input_length = (unsigned int)(input_length - sizeof(iv)); } while(--loop); }
static void AddRoundKey (uint8_t *state, const uint8_t *key) { xor_buffer(state, key, 16); }