Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(644)

Side by Side Diff: core/fpdfapi/fpdf_parser/cpdf_crypto_handler.cpp

Issue 2190123002: Fixup crypto key generation. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Out param last Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h" 7 #include "core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h"
8 8
9 #include <time.h> 9 #include <time.h>
10 10
(...skipping 10 matching lines...) Expand all
21 uint8_t* dest_buf, 21 uint8_t* dest_buf,
22 uint32_t& dest_size) { 22 uint32_t& dest_size) {
23 if (m_Cipher == FXCIPHER_NONE) { 23 if (m_Cipher == FXCIPHER_NONE) {
24 FXSYS_memcpy(dest_buf, src_buf, src_size); 24 FXSYS_memcpy(dest_buf, src_buf, src_size);
25 return; 25 return;
26 } 26 }
27 uint8_t realkey[16]; 27 uint8_t realkey[16];
28 int realkeylen = 16; 28 int realkeylen = 16;
29 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { 29 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) {
30 uint8_t key1[32]; 30 uint8_t key1[32];
31 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); 31 PopulateKey(objnum, gennum, key1);
32 key1[m_KeyLen + 0] = (uint8_t)objnum; 32
33 key1[m_KeyLen + 1] = (uint8_t)(objnum >> 8);
34 key1[m_KeyLen + 2] = (uint8_t)(objnum >> 16);
35 key1[m_KeyLen + 3] = (uint8_t)gennum;
36 key1[m_KeyLen + 4] = (uint8_t)(gennum >> 8);
37 FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3);
38 FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2);
39 if (m_Cipher == FXCIPHER_AES) { 33 if (m_Cipher == FXCIPHER_AES) {
40 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4); 34 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
41 } 35 }
42 CRYPT_MD5Generate( 36 CRYPT_MD5Generate(
43 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey); 37 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey);
44 realkeylen = m_KeyLen + 5; 38 realkeylen = m_KeyLen + 5;
45 if (realkeylen > 16) { 39 if (realkeylen > 16) {
46 realkeylen = 16; 40 realkeylen = 16;
47 } 41 }
48 } 42 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); 94 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt);
101 if (bEncrypt) { 95 if (bEncrypt) {
102 for (int i = 0; i < 16; i++) { 96 for (int i = 0; i < 16; i++) {
103 pContext->m_Block[i] = (uint8_t)rand(); 97 pContext->m_Block[i] = (uint8_t)rand();
104 } 98 }
105 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); 99 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block);
106 } 100 }
107 return pContext; 101 return pContext;
108 } 102 }
109 uint8_t key1[48]; 103 uint8_t key1[48];
110 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); 104 PopulateKey(objnum, gennum, key1);
111 FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3); 105
112 FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2);
113 if (m_Cipher == FXCIPHER_AES) { 106 if (m_Cipher == FXCIPHER_AES) {
114 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4); 107 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
115 } 108 }
116 uint8_t realkey[16]; 109 uint8_t realkey[16];
117 CRYPT_MD5Generate( 110 CRYPT_MD5Generate(
118 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey); 111 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey);
119 int realkeylen = m_KeyLen + 5; 112 int realkeylen = m_KeyLen + 5;
120 if (realkeylen > 16) { 113 if (realkeylen > 16) {
121 realkeylen = 16; 114 realkeylen = 16;
122 } 115 }
123 if (m_Cipher == FXCIPHER_AES) { 116 if (m_Cipher == FXCIPHER_AES) {
124 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); 117 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1);
125 pContext->m_bIV = TRUE; 118 pContext->m_bIV = TRUE;
126 pContext->m_BlockOffset = 0; 119 pContext->m_BlockOffset = 0;
127 CRYPT_AESSetKey(pContext->m_Context, 16, realkey, 16, bEncrypt); 120 CRYPT_AESSetKey(pContext->m_Context, 16, realkey, 16, bEncrypt);
128 if (bEncrypt) { 121 if (bEncrypt) {
129 for (int i = 0; i < 16; i++) { 122 for (int i = 0; i < 16; i++) {
130 pContext->m_Block[i] = (uint8_t)rand(); 123 pContext->m_Block[i] = (uint8_t)rand();
131 } 124 }
132 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); 125 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block);
133 } 126 }
134 return pContext; 127 return pContext;
135 } 128 }
136 void* pContext = FX_Alloc(uint8_t, 1040); 129 void* pContext = FX_Alloc(uint8_t, 1040);
137 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); 130 CRYPT_ArcFourSetup(pContext, realkey, realkeylen);
138 return pContext; 131 return pContext;
139 } 132 }
133
140 FX_BOOL CPDF_CryptoHandler::CryptStream(void* context, 134 FX_BOOL CPDF_CryptoHandler::CryptStream(void* context,
141 const uint8_t* src_buf, 135 const uint8_t* src_buf,
142 uint32_t src_size, 136 uint32_t src_size,
143 CFX_BinaryBuf& dest_buf, 137 CFX_BinaryBuf& dest_buf,
144 FX_BOOL bEncrypt) { 138 FX_BOOL bEncrypt) {
145 if (!context) { 139 if (!context) {
146 return FALSE; 140 return FALSE;
147 } 141 }
148 if (m_Cipher == FXCIPHER_NONE) { 142 if (m_Cipher == FXCIPHER_NONE) {
149 dest_buf.AppendBlock(src_buf, src_size); 143 dest_buf.AppendBlock(src_buf, src_size);
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 return TRUE; 322 return TRUE;
329 } 323 }
330 CPDF_CryptoHandler::CPDF_CryptoHandler() { 324 CPDF_CryptoHandler::CPDF_CryptoHandler() {
331 m_pAESContext = nullptr; 325 m_pAESContext = nullptr;
332 m_Cipher = FXCIPHER_NONE; 326 m_Cipher = FXCIPHER_NONE;
333 m_KeyLen = 0; 327 m_KeyLen = 0;
334 } 328 }
335 CPDF_CryptoHandler::~CPDF_CryptoHandler() { 329 CPDF_CryptoHandler::~CPDF_CryptoHandler() {
336 FX_Free(m_pAESContext); 330 FX_Free(m_pAESContext);
337 } 331 }
332
333 void CPDF_CryptoHandler::PopulateKey(uint32_t objnum,
334 uint32_t gennum,
335 uint8_t* key) {
336 FXSYS_memcpy(key, m_EncryptKey, m_KeyLen);
337 key[m_KeyLen + 0] = (uint8_t)objnum;
338 key[m_KeyLen + 1] = (uint8_t)(objnum >> 8);
339 key[m_KeyLen + 2] = (uint8_t)(objnum >> 16);
340 key[m_KeyLen + 3] = (uint8_t)gennum;
341 key[m_KeyLen + 4] = (uint8_t)(gennum >> 8);
342 }
OLDNEW
« no previous file with comments | « core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698