1 2## tests for AES, taken from the examples in the manual page 3 4suppressMessages(library(digest)) 5 6# FIPS-197 examples 7 8hextextToRaw <- function(text) { 9 vals <- matrix(as.integer(as.hexmode(strsplit(text, "")[[1]])), ncol=2, byrow=TRUE) 10 vals <- vals %*% c(16, 1) 11 as.raw(vals) 12} 13 14plaintext <- hextextToRaw("00112233445566778899aabbccddeeff") 15 16aes128key <- hextextToRaw("000102030405060708090a0b0c0d0e0f") 17aes128output <- hextextToRaw("69c4e0d86a7b0430d8cdb78070b4c55a") 18aes <- AES(aes128key) 19#aes 20aes128 <- aes$encrypt(plaintext) 21#aes128 22expect_true(identical(aes128, aes128output)) 23expect_true(identical(plaintext, aes$decrypt(aes128, raw=TRUE))) 24 25aes192key <- hextextToRaw("000102030405060708090a0b0c0d0e0f1011121314151617") 26aes192output <- hextextToRaw("dda97ca4864cdfe06eaf70a0ec0d7191") 27aes <- AES(aes192key) 28#aes 29aes192 <- aes$encrypt(plaintext) 30#aes192 31expect_true(identical(aes192, aes192output)) 32expect_true(identical(plaintext, aes$decrypt(aes192, raw=TRUE))) 33 34aes256key <- hextextToRaw("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") 35aes256output <- hextextToRaw("8ea2b7ca516745bfeafc49904b496089") 36aes <- AES(aes256key) 37aes256 <- aes$encrypt(plaintext) 38#aes256 39expect_true(identical(aes256, aes256output)) 40expect_true(identical(plaintext, aes$decrypt(aes256, raw=TRUE))) 41 42# SP800-38a examples 43 44plaintext <- hextextToRaw(paste("6bc1bee22e409f96e93d7e117393172a", 45 "ae2d8a571e03ac9c9eb76fac45af8e51", 46 "30c81c46a35ce411e5fbc1191a0a52ef", 47 "f69f2445df4f9b17ad2b417be66c3710",sep="")) 48key <- hextextToRaw("2b7e151628aed2a6abf7158809cf4f3c") 49 50ecb128output <- hextextToRaw(paste("3ad77bb40d7a3660a89ecaf32466ef97", 51 "f5d3d58503b9699de785895a96fdbaaf", 52 "43b1cd7f598ece23881b00e3ed030688", 53 "7b0c785e27e8ad3f8223207104725dd4",sep="")) 54aes <- AES(key) 55ecb128 <- aes$encrypt(plaintext) 56#ecb128 57expect_true(identical(ecb128, ecb128output)) 58expect_true(identical(plaintext, aes$decrypt(ecb128, raw=TRUE))) 59 60cbc128output <- hextextToRaw(paste("7649abac8119b246cee98e9b12e9197d", 61 "5086cb9b507219ee95db113a917678b2", 62 "73bed6b8e3c1743b7116e69e22229516", 63 "3ff1caa1681fac09120eca307586e1a7",sep="")) 64iv <- hextextToRaw("000102030405060708090a0b0c0d0e0f") 65aes <- AES(key, mode="CBC", IV=iv) 66cbc128 <- aes$encrypt(plaintext) 67#cbc128 68expect_true(identical(cbc128, cbc128output)) 69aes <- AES(key, mode="CBC", IV=iv) 70expect_true(identical(plaintext, aes$decrypt(cbc128, raw=TRUE))) 71 72ctr128output <- hextextToRaw(paste("874d6191b620e3261bef6864990db6ce", 73 "9806f66b7970fdff8617187bb9fffdff", 74 "5ae4df3edbd5d35e5b4f09020db03eab", 75 "1e031dda2fbe03d1792170a0f3009cee",sep="")) 76iv <- hextextToRaw("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff") 77aes <- AES(key, mode="CTR", IV=iv) 78ctr128 <- aes$encrypt(plaintext) 79#ctr128 80expect_true(identical(ctr128, ctr128output)) 81aes <- AES(key, mode="CTR", IV=iv) 82expect_true(identical(plaintext, aes$decrypt(ctr128, raw=TRUE))) 83 84#cfb 85key <- hextextToRaw("2b7e151628aed2a6abf7158809cf4f3c") 86iv <- hextextToRaw("000102030405060708090a0b0c0d0e0f") 87#cfb not a multiplier of 16 88text <- "This is very secret string" 89key <- hextextToRaw("2b7e151628aed2a6abf7158809cf4f3c") 90iv <- hextextToRaw("000102030405060708090a0b0c0d0e0f") 91 92cfb128output <- hextextToRaw(paste("04960ebfb9044196ac6c4590bbdc8903", 93 "e1259a479e199af94518",sep="")) 94aes <- AES(key, mode="CFB", IV=iv) 95cfb128 <- aes$encrypt(text) 96expect_true(identical(cfb128, cfb128output)) 97aes <- AES(key, mode="CFB", IV=iv) 98expect_true(identical(text, aes$decrypt(cfb128, raw=FALSE))) 99 100#cfb128 101cfb128output <- hextextToRaw(paste("3b3fd92eb72dad20333449f8e83cfb4a", 102 "c8a64537a0b3a93fcde3cdad9f1ce58b", 103 "26751f67a3cbb140b1808cf187a4f4df", 104 "c04b05357c5d1c0eeac4c66f9ff7f2e6",sep="")) 105aes <- AES(key, mode="CFB", IV=iv) 106cfb128 <- aes$encrypt(plaintext) 107expect_true(identical(cfb128, cfb128output)) 108aes <- AES(key, mode="CFB", IV=iv) 109expect_true(identical(plaintext, aes$decrypt(cfb128, raw=TRUE))) 110 111# test throws exeception on IV null or not a multiplier of 16 bytes 112aes <- AES(key, mode="CFB", IV=NULL) 113expect_error(aes$encrypt(plaintext)) 114expect_error(aes$decrypt(plaintext)) 115 116aes <- AES(key, mode="CFB", IV=raw(15)) 117expect_error(aes$encrypt(plaintext)) 118expect_error(aes$decrypt(plaintext)) 119 120 121 122 123