diff options
Diffstat (limited to 'src/block/mars/mars.cpp')
-rw-r--r-- | src/block/mars/mars.cpp | 89 |
1 files changed, 48 insertions, 41 deletions
diff --git a/src/block/mars/mars.cpp b/src/block/mars/mars.cpp index 57a224fac..5864ac49b 100644 --- a/src/block/mars/mars.cpp +++ b/src/block/mars/mars.cpp @@ -149,7 +149,7 @@ inline void decrypt_round(u32bit& A, u32bit& B, u32bit& C, u32bit& D, */ void forward_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) { - for(u32bit j = 0; j != 2; ++j) + for(size_t j = 0; j != 2; ++j) { B ^= SBOX[get_byte(3, A)]; B += SBOX[get_byte(2, A) + 256]; C += SBOX[get_byte(1, A)]; D ^= SBOX[get_byte(0, A) + 256]; @@ -174,7 +174,7 @@ void forward_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) */ void reverse_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) { - for(u32bit j = 0; j != 2; ++j) + for(size_t j = 0; j != 2; ++j) { B ^= SBOX[get_byte(3, A) + 256]; C -= SBOX[get_byte(0, A)]; D -= SBOX[get_byte(1, A) + 256]; D ^= SBOX[get_byte(2, A)]; @@ -202,7 +202,7 @@ u32bit gen_mask(u32bit input) { u32bit mask = 0; - for(u32bit j = 2; j != 31; ++j) + for(size_t j = 2; j != 31; ++j) { u32bit region = (input >> (j-1)) & 0x07; @@ -232,9 +232,9 @@ u32bit gen_mask(u32bit input) /* * MARS Encryption */ -void MARS::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void MARS::encrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0) + EK[0]; u32bit B = load_le<u32bit>(in, 1) + EK[1]; @@ -267,17 +267,17 @@ void MARS::encrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, A, B, C, D); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * MARS Decryption */ -void MARS::decrypt_n(const byte in[], byte out[], u32bit blocks) const +void MARS::decrypt_n(const byte in[], byte out[], size_t blocks) const { - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 3) + EK[39]; u32bit B = load_le<u32bit>(in, 2) + EK[38]; @@ -310,40 +310,41 @@ void MARS::decrypt_n(const byte in[], byte out[], u32bit blocks) const store_le(out, D, C, B, A); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += block_size(); + out += block_size(); } } /* * MARS Key Schedule */ -void MARS::key_schedule(const byte key[], u32bit length) +void MARS::key_schedule(const byte key[], size_t length) { - SecureVector<u32bit, 15> T; - for(u32bit j = 0; j != length / 4; ++j) - T[j] = load_le<u32bit>(key, j); + SecureVector<u32bit> T(15); + for(size_t i = 0; i != length / 4; ++i) + T[i] = load_le<u32bit>(key, i); + T[length / 4] = length / 4; - for(u32bit j = 0; j != 4; ++j) + for(u32bit i = 0; i != 4; ++i) { - T[ 0] ^= rotate_left(T[ 8] ^ T[13], 3) ^ (j ); - T[ 1] ^= rotate_left(T[ 9] ^ T[14], 3) ^ (j + 4); - T[ 2] ^= rotate_left(T[10] ^ T[ 0], 3) ^ (j + 8); - T[ 3] ^= rotate_left(T[11] ^ T[ 1], 3) ^ (j + 12); - T[ 4] ^= rotate_left(T[12] ^ T[ 2], 3) ^ (j + 16); - T[ 5] ^= rotate_left(T[13] ^ T[ 3], 3) ^ (j + 20); - T[ 6] ^= rotate_left(T[14] ^ T[ 4], 3) ^ (j + 24); - T[ 7] ^= rotate_left(T[ 0] ^ T[ 5], 3) ^ (j + 28); - T[ 8] ^= rotate_left(T[ 1] ^ T[ 6], 3) ^ (j + 32); - T[ 9] ^= rotate_left(T[ 2] ^ T[ 7], 3) ^ (j + 36); - T[10] ^= rotate_left(T[ 3] ^ T[ 8], 3) ^ (j + 40); - T[11] ^= rotate_left(T[ 4] ^ T[ 9], 3) ^ (j + 44); - T[12] ^= rotate_left(T[ 5] ^ T[10], 3) ^ (j + 48); - T[13] ^= rotate_left(T[ 6] ^ T[11], 3) ^ (j + 52); - T[14] ^= rotate_left(T[ 7] ^ T[12], 3) ^ (j + 56); - - for(u32bit k = 0; k != 4; ++k) + T[ 0] ^= rotate_left(T[ 8] ^ T[13], 3) ^ (i ); + T[ 1] ^= rotate_left(T[ 9] ^ T[14], 3) ^ (i + 4); + T[ 2] ^= rotate_left(T[10] ^ T[ 0], 3) ^ (i + 8); + T[ 3] ^= rotate_left(T[11] ^ T[ 1], 3) ^ (i + 12); + T[ 4] ^= rotate_left(T[12] ^ T[ 2], 3) ^ (i + 16); + T[ 5] ^= rotate_left(T[13] ^ T[ 3], 3) ^ (i + 20); + T[ 6] ^= rotate_left(T[14] ^ T[ 4], 3) ^ (i + 24); + T[ 7] ^= rotate_left(T[ 0] ^ T[ 5], 3) ^ (i + 28); + T[ 8] ^= rotate_left(T[ 1] ^ T[ 6], 3) ^ (i + 32); + T[ 9] ^= rotate_left(T[ 2] ^ T[ 7], 3) ^ (i + 36); + T[10] ^= rotate_left(T[ 3] ^ T[ 8], 3) ^ (i + 40); + T[11] ^= rotate_left(T[ 4] ^ T[ 9], 3) ^ (i + 44); + T[12] ^= rotate_left(T[ 5] ^ T[10], 3) ^ (i + 48); + T[13] ^= rotate_left(T[ 6] ^ T[11], 3) ^ (i + 52); + T[14] ^= rotate_left(T[ 7] ^ T[12], 3) ^ (i + 56); + + for(size_t j = 0; j != 4; ++j) { T[ 0] = rotate_left(T[ 0] + SBOX[T[14] % 512], 9); T[ 1] = rotate_left(T[ 1] + SBOX[T[ 0] % 512], 9); @@ -362,17 +363,23 @@ void MARS::key_schedule(const byte key[], u32bit length) T[14] = rotate_left(T[14] + SBOX[T[13] % 512], 9); } - EK[10*j + 0] = T[ 0]; EK[10*j + 1] = T[ 4]; EK[10*j + 2] = T[ 8]; - EK[10*j + 3] = T[12]; EK[10*j + 4] = T[ 1]; EK[10*j + 5] = T[ 5]; - EK[10*j + 6] = T[ 9]; EK[10*j + 7] = T[13]; EK[10*j + 8] = T[ 2]; - EK[10*j + 9] = T[ 6]; + EK[10*i + 0] = T[ 0]; + EK[10*i + 1] = T[ 4]; + EK[10*i + 2] = T[ 8]; + EK[10*i + 3] = T[12]; + EK[10*i + 4] = T[ 1]; + EK[10*i + 5] = T[ 5]; + EK[10*i + 6] = T[ 9]; + EK[10*i + 7] = T[13]; + EK[10*i + 8] = T[ 2]; + EK[10*i + 9] = T[ 6]; } - for(u32bit j = 5; j != 37; j += 2) + for(size_t i = 5; i != 37; i += 2) { - u32bit key3 = EK[j] & 3; - EK[j] |= 3; - EK[j] ^= rotate_left(SBOX[265 + key3], EK[j-1] % 32) & gen_mask(EK[j]); + u32bit key3 = EK[i] & 3; + EK[i] |= 3; + EK[i] ^= rotate_left(SBOX[265 + key3], EK[i-1] % 32) & gen_mask(EK[i]); } } |