diff --git a/pdfio-aes.c b/pdfio-aes.c index c97f798..7abef74 100644 --- a/pdfio-aes.c +++ b/pdfio-aes.c @@ -126,6 +126,7 @@ _pdfioCryptoAESInit( { if ((i % nwords) == 0) { + // TODO: Optimize to shift and lookup S box in one step // Shifts word left once - [a0,a1,a2,a3] becomes [a1,a2,a3,a0] tempa[0] = rkptr[-3]; tempa[1] = rkptr[-2]; @@ -150,12 +151,14 @@ _pdfioCryptoAESInit( } else { + // Use unshifted values without S-box... tempa[0] = rkptr[-4]; tempa[1] = rkptr[-3]; tempa[2] = rkptr[-2]; tempa[3] = rkptr[-1]; } + // TODO: Optimize to incorporate this into previous steps *rkptr++ = *rkptr0++ ^ tempa[0]; *rkptr++ = *rkptr0++ ^ tempa[1]; *rkptr++ = *rkptr0++ ^ tempa[2]; @@ -171,7 +174,8 @@ _pdfioCryptoAESInit( // // '_pdfioCryptoAESDecrypt()' - Decrypt a block of bytes with AES. // -// "inbuffer" and "outbuffer" can point to the same memory. +// "inbuffer" and "outbuffer" can point to the same memory. Length must be a +// multiple of 16 bytes (excess is not decrypted). // void @@ -201,29 +205,14 @@ _pdfioCryptoAESDecrypt( outbuffer += 16; len -= 16; } - - if (len > 0) - { - // Pad the final buffer with (16 - len)... - uint8_t temp[16]; // Temporary buffer - - memset(temp, 16 - len, sizeof(temp)); - memcpy(temp, outbuffer, len); - - memcpy(next_iv, temp, 16); - InvCipher((state_t *)temp, ctx); - XorWithIv(temp, ctx->iv); - memcpy(ctx->iv, next_iv, 16); - - memcpy(outbuffer, temp, len); - } } // // '_pdfioCryptoAESEncrypt()' - Encrypt a block of bytes with AES. // -// "inbuffer" and "outbuffer" can point to the same memory. +// "inbuffer" and "outbuffer" can point to the same memory. "outbuffer" must +// be a multiple of 16 bytes. // void @@ -234,7 +223,6 @@ _pdfioCryptoAESEncrypt( size_t len) // I - Number of bytes to decrypt { uint8_t *iv = ctx->iv; // Current IV for CBC - uint8_t temp[16]; // Temporary buffer if (inbuffer != outbuffer) @@ -257,14 +245,11 @@ _pdfioCryptoAESEncrypt( if (len > 0) { // Pad the final buffer with (16 - len)... - memset(temp, 16 - len, sizeof(temp)); - memcpy(temp, outbuffer, len); + memset(outbuffer + len, 16 - len, 16 - len); - XorWithIv(temp, iv); - Cipher((state_t*)temp, ctx); - iv = temp; - - memcpy(outbuffer, temp, len); + XorWithIv(outbuffer, iv); + Cipher((state_t*)outbuffer, ctx); + iv = outbuffer; } /* store Iv in ctx for next call */ diff --git a/testpdfio.c b/testpdfio.c index 4608024..4799bc4 100644 --- a/testpdfio.c +++ b/testpdfio.c @@ -132,13 +132,13 @@ do_crypto_tests(void) const char *prefix, *suffix; // Prefix/suffix strings static const char *text = "Hello, World! Now is the time for all good men to come to the aid of their country.\n"; // Test text - static uint8_t aes128text[] = { 0xfb, 0x77, 0xac, 0xce, 0x3c, 0x95, 0x40, 0xcf, 0xca, 0xc8, 0x26, 0xbf, 0xc0, 0x69, 0x73, 0x3c, 0x01, 0xfd, 0x72, 0x01, 0xeb, 0x4d, 0x6f, 0xf7, 0xb4, 0x72, 0x6d, 0x84, 0x69, 0x9f, 0x89, 0xab, 0xe6, 0x2b, 0x9a, 0x9a, 0x6e, 0xc1, 0x61, 0xd7, 0x9d, 0x83, 0x2d, 0x58, 0x55, 0xa7, 0x58, 0x50, 0x00, 0xad, 0x19, 0x7b, 0xee, 0x6a, 0x36, 0x6f, 0xd1, 0xa7, 0xa4, 0x6b, 0xc5, 0x78, 0x9a, 0x18, 0x05, 0xf0, 0x2c, 0xd4, 0x60, 0x25, 0xe0, 0xa7, 0xb1, 0x36, 0xdb, 0x18, 0xd3, 0xf7, 0x59, 0x29, 0x8d, 0x26, 0x49, 0x63, 0xd9, 0xec, 0x08, 0x5a, 0xe1, 0x00, 0x20, 0x3c, 0x3e, 0x55, 0x2d, 0xe0 }; + static uint8_t aes128text[] = { 0xfb, 0x77, 0xac, 0xce, 0x3c, 0x95, 0x40, 0xcf, 0xca, 0xc8, 0x26, 0xbf, 0xc0, 0x69, 0x73, 0x3c, 0x01, 0xfd, 0x72, 0x01, 0xeb, 0x4d, 0x6f, 0xf7, 0xb4, 0x72, 0x6d, 0x84, 0x69, 0x9f, 0x89, 0xab, 0xe6, 0x2b, 0x9a, 0x9a, 0x6e, 0xc1, 0x61, 0xd7, 0x9d, 0x83, 0x2d, 0x58, 0x55, 0xa7, 0x58, 0x50, 0x00, 0xad, 0x19, 0x7b, 0xee, 0x6a, 0x36, 0x6f, 0xd1, 0xa7, 0xa4, 0x6b, 0xc5, 0x78, 0x9a, 0x18, 0x05, 0xf0, 0x2c, 0xd4, 0x60, 0x25, 0xe0, 0xa7, 0xb1, 0x36, 0xdb, 0x18, 0xd3, 0xf7, 0x59, 0x29, 0x22, 0xec, 0x25, 0x77, 0x0d, 0x9e, 0x5a, 0x01, 0xcc, 0xf6, 0x29, 0xc2, 0x08, 0xc2, 0xfc, 0x4f }; // Expected AES-128 CBC result - static uint8_t aes256text[] = { 0xc6, 0xec, 0x4d, 0x14, 0x96, 0xd4, 0xc2, 0xda, 0xfa, 0xe5, 0x86, 0xfb, 0xb9, 0xde, 0xde, 0x63, 0x66, 0xf7, 0xf0, 0x73, 0x18, 0x48, 0xee, 0x66, 0x8a, 0xfc, 0xa5, 0xcd, 0x89, 0xcc, 0xd4, 0x03, 0x7a, 0xd9, 0x19, 0xfc, 0x32, 0xeb, 0x15, 0xaf, 0x6b, 0x65, 0xe1, 0xe2, 0x77, 0xd1, 0xdf, 0x3e, 0x85, 0x41, 0x38, 0xa1, 0x1e, 0x90, 0x29, 0xfc, 0xa8, 0x30, 0xa5, 0xf0, 0x2c, 0x3f, 0x23, 0x71, 0x27, 0x9e, 0xc0, 0x75, 0x54, 0x16, 0xfd, 0x75, 0x24, 0x5e, 0x13, 0x97, 0xba, 0x95, 0x2e, 0xe8, 0x03, 0x1d, 0xf7, 0xee, 0xa5, 0xf9, 0x2c, 0x62, 0xdd, 0x02, 0xf2, 0x40, 0x3f, 0x22, 0xa3, 0x51 }; + static uint8_t aes256text[] = { 0x2b, 0x94, 0x45, 0x9e, 0xed, 0xa0, 0x89, 0x7b, 0x35, 0x4e, 0xde, 0x06, 0x00, 0x4d, 0xda, 0x6b, 0x61, 0x2f, 0xb9, 0x06, 0xd5, 0x0f, 0x22, 0xed, 0xd2, 0xe3, 0x6b, 0x39, 0x5a, 0xa1, 0xe3, 0x7d, 0xa1, 0xcc, 0xd4, 0x0b, 0x6b, 0xa4, 0xff, 0xe9, 0x9c, 0x89, 0x0c, 0xc7, 0x95, 0x47, 0x19, 0x9b, 0x06, 0xdc, 0xc8, 0x7c, 0x5c, 0x5d, 0x56, 0x99, 0x1e, 0x90, 0x7d, 0x99, 0xc5, 0x7b, 0xc4, 0xe4, 0xfb, 0x02, 0x15, 0x50, 0x23, 0x2a, 0xe4, 0xc1, 0x20, 0xfd, 0xf4, 0x03, 0xfe, 0x6f, 0x15, 0x48, 0xd8, 0x62, 0x36, 0x98, 0x2a, 0x62, 0xf5, 0x2c, 0xa6, 0xfa, 0x7a, 0x43, 0x53, 0xcd, 0xad, 0x18 }; // Expected AES-256 CBC result static uint8_t md5text[16] = { 0x74, 0x0c, 0x2c, 0xea, 0xe1, 0xab, 0x06, 0x7c, 0xdb, 0x1d, 0x49, 0x1d, 0x2d, 0x66, 0xf2, 0x93 }; // Expected MD5 hash result - static uint8_t rc4text[] = { 0xd2, 0xa2, 0xa0, 0xf6, 0x0f, 0xb1, 0x3e, 0xa0, 0xdd, 0xe1, 0x44, 0xfd, 0xec, 0xc4, 0x55, 0xf8, 0x25, 0x68, 0xad, 0xe6, 0xb0, 0x60, 0x7a, 0x0f, 0x4e, 0xfe, 0xed, 0x9c, 0x78, 0x3a, 0xf8, 0x73, 0x79, 0xbd, 0x82, 0x88, 0x39, 0x01, 0xc7, 0xd0, 0x34, 0xfe, 0x40, 0x16, 0x93, 0x5a, 0xec, 0x81, 0xda, 0x34, 0xdf, 0x5b, 0xd1, 0x47, 0x2c, 0xfa, 0xe0, 0x13, 0xc5, 0xe2, 0xb0, 0x57, 0x5c, 0x17, 0x62, 0xaa, 0x83, 0x1c, 0x4f, 0xa0, 0x0a, 0xed, 0x6c, 0x42, 0x41, 0x8a, 0x45, 0x03, 0xb8, 0x72, 0xa8, 0x99, 0xd7, 0x01, 0x84 }; + static uint8_t rc4text[] = { 0xd2, 0xa2, 0xa0, 0xf6, 0x0f, 0xb1, 0x3e, 0xa0, 0xdd, 0xe1, 0x44, 0xfd, 0xec, 0xc4, 0x55, 0xf8, 0x25, 0x68, 0xad, 0xe6, 0xb0, 0x60, 0x7a, 0x0f, 0x4e, 0xfe, 0xed, 0x9c, 0x78, 0x3a, 0xf8, 0x73, 0x79, 0xbd, 0x82, 0x88, 0x39, 0x01, 0xc7, 0xd0, 0x34, 0xfe, 0x40, 0x16, 0x93, 0x5a, 0xec, 0x81, 0xda, 0x34, 0xdf, 0x5b, 0xd1, 0x47, 0x2c, 0xfa, 0xe0, 0x13, 0xc5, 0xe2, 0xb0, 0x57, 0x5c, 0x17, 0x62, 0xaa, 0x83, 0x1c, 0x4f, 0xa0, 0x0a, 0xed, 0x6c, 0x42, 0x41, 0x8a, 0x45, 0x03, 0xb8, 0x72, 0xa8, 0x99, 0xd7, 0x06 }; // Expected RC4 result static uint8_t sha256text[32] = { 0x19, 0x71, 0x9b, 0xf0, 0xc6, 0xd8, 0x34, 0xc9, 0x6e, 0x8a, 0x56, 0xcc, 0x34, 0x45, 0xb7, 0x1d, 0x5b, 0x74, 0x9c, 0x52, 0x40, 0xcd, 0x30, 0xa2, 0xc2, 0x84, 0x53, 0x83, 0x16, 0xf8, 0x1a, 0xbb }; // Expected SHA-256 hash result