有没有安全高手给解释一下,这个MemCopy代码抵抗哪些攻击,原理是什么?
程序代码:
#include <stdio.h> #include <stdlib.h> typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; uint32_t GetRandom( uint32_t* x) { uint32_t ret; ret = rand(); *x = ~ret; return ret; } void MemCopy( uint8_t* pSrc, uint8_t* pDst, uint16_t len ) { uint32_t i, loop_i; uint32_t startIndex; uint32_t and_mask, xor_mask; uint32_t randVal, invRand; startIndex = GetRandom( &invRand ) >> 1; xor_mask = GetRandom( &invRand ); and_mask = 0x1ffff; while( ( len <= (and_mask >> 1 ) + 1 ) && ( and_mask > 1 ) ) { and_mask = and_mask >> 1; } xor_mask &= and_mask; for( loop_i = 0; loop_i <= and_mask; loop_i++ ) { i = ( startIndex + (loop_i ^ xor_mask) ) % len; pDst[i] = (uint8_t) GetRandom( &invRand ); randVal = GetRandom(&invRand); randVal = pSrc[i]; pDst[i] = randVal; randVal = invRand; } return; } void main() { unsigned char a1[256] = {1, 2, 3, 4, 5}; unsigned char a2[256]; int i; MemCopy(a1, a2, 5); for (i=0; i<5; i++) { printf("%02x ", a2[i]); } printf("\n"); }