@ -28,31 +28,31 @@ object XteaTest {
// two blocks
// two blocks
TestVector ( " 00000000000000000000000000000000 " , " 70e1225d6e4e76554141414141414141 " ,
TestVector ( " 00000000000000000000000000000000 " , " 70e1225d6e4e76554141414141414141 " ,
" 4141414141414141ed23375a821a8c2d " ) ,
" 4141414141414141ed23375a821a8c2d " )
// not a multiple of the block size
TestVector ( " 00000000000000000000000000000000 " , " 01 " , " 01 " ) ,
TestVector ( " 00000000000000000000000000000000 " , " 01020304050607 " , " 01020304050607 " ) ,
TestVector ( " 00000000000000000000000000000000 " , " 70e1225d6e4e765501 " , " 414141414141414101 " ) ,
TestVector ( " 00000000000000000000000000000000 " , " 70e1225d6e4e765501020304050607 " ,
" 414141414141414101020304050607 " )
)
)
@Test
@Test
fun testEncrypt ( ) {
fun testEncrypt ( ) {
for ( vector in TEST _VECTORS ) {
for ( vector in TEST _VECTORS ) {
val buffer = Unpooled . copiedBuffer ( vector . plaintext )
for ( i in 0. . 7 ) {
try {
for ( j in 0. . 7 ) {
buffer . xteaEncrypt ( 0 , buffer . readableBytes ( ) , vector . key )
val header = ByteArray ( i ) { it . toByte ( ) }
val trailer = ByteArray ( j ) { it . toByte ( ) }
val buffer = Unpooled . copiedBuffer ( header , vector . plaintext , trailer )
try {
buffer . xteaEncrypt ( i , vector . plaintext . size , vector . key )
val expected = Unpooled . wrappedBuffer ( vector . ciphertext )
val expected = Unpooled . wrappedBuffer ( header , vector . ciphertext , trailer )
try {
try {
assertEquals ( expected , buffer )
assertEquals ( expected , buffer )
} finally {
} finally {
expected . release ( )
expected . release ( )
}
} finally {
buffer . release ( )
}
}
}
} finally {
buffer . release ( )
}
}
}
}
}
}
@ -60,18 +60,25 @@ object XteaTest {
@Test
@Test
fun testDecrypt ( ) {
fun testDecrypt ( ) {
for ( vector in TEST _VECTORS ) {
for ( vector in TEST _VECTORS ) {
val buffer = Unpooled . copiedBuffer ( vector . ciphertext )
for ( i in 0. . 7 ) {
try {
for ( j in 0. . 7 ) {
buffer . xteaDecrypt ( 0 , buffer . readableBytes ( ) , vector . key )
val header = ByteArray ( i ) { it . toByte ( ) }
val trailer = ByteArray ( j ) { it . toByte ( ) }
val buffer = Unpooled . copiedBuffer ( header , vector . ciphertext , trailer )
try {
buffer . xteaDecrypt ( i , vector . ciphertext . size , vector . key )
val expected = Unpooled . wrappedBuffer ( vector . plaintext )
val expected = Unpooled . wrappedBuffer ( header , vector . plaintext , trailer )
try {
try {
assertEquals ( expected , buffer )
assertEquals ( expected , buffer )
} finally {
} finally {
expected . release ( )
expected . release ( )
}
} finally {
buffer . release ( )
}
}
}
} finally {
buffer . release ( )
}
}
}
}
}
}