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

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: 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 | « no previous file | 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
11 #include "core/fdrm/crypto/include/fx_crypt.h" 11 #include "core/fdrm/crypto/include/fx_crypt.h"
12 #include "core/fpdfapi/fpdf_parser/cpdf_security_handler.h" 12 #include "core/fpdfapi/fpdf_parser/cpdf_security_handler.h"
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h"
14 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" 14 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h"
15 15
16 void CPDF_CryptoHandler::CryptBlock(FX_BOOL bEncrypt, 16 void CPDF_CryptoHandler::CryptBlock(FX_BOOL bEncrypt,
17 uint32_t objnum, 17 uint32_t objnum,
18 uint32_t gennum, 18 uint32_t gennum,
19 const uint8_t* src_buf, 19 const uint8_t* src_buf,
20 uint32_t src_size, 20 uint32_t src_size,
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];
Tom Sepez 2016/07/28 20:46:22 can we pull this into a helper function?
dsinclair 2016/07/28 20:55:23 Done.
31 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); 31 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen);
32 key1[m_KeyLen + 0] = (uint8_t)objnum; 32 key1[m_KeyLen + 0] = (uint8_t)objnum;
33 key1[m_KeyLen + 1] = (uint8_t)(objnum >> 8); 33 key1[m_KeyLen + 1] = (uint8_t)(objnum >> 8);
34 key1[m_KeyLen + 2] = (uint8_t)(objnum >> 16); 34 key1[m_KeyLen + 2] = (uint8_t)(objnum >> 16);
35 key1[m_KeyLen + 3] = (uint8_t)gennum; 35 key1[m_KeyLen + 3] = (uint8_t)gennum;
36 key1[m_KeyLen + 4] = (uint8_t)(gennum >> 8); 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) { 37 if (m_Cipher == FXCIPHER_AES) {
40 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4); 38 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
41 } 39 }
42 CRYPT_MD5Generate( 40 CRYPT_MD5Generate(
43 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey); 41 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey);
44 realkeylen = m_KeyLen + 5; 42 realkeylen = m_KeyLen + 5;
45 if (realkeylen > 16) { 43 if (realkeylen > 16) {
46 realkeylen = 16; 44 realkeylen = 16;
47 } 45 }
48 } 46 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 if (bEncrypt) { 99 if (bEncrypt) {
102 for (int i = 0; i < 16; i++) { 100 for (int i = 0; i < 16; i++) {
103 pContext->m_Block[i] = (uint8_t)rand(); 101 pContext->m_Block[i] = (uint8_t)rand();
104 } 102 }
105 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); 103 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block);
106 } 104 }
107 return pContext; 105 return pContext;
108 } 106 }
109 uint8_t key1[48]; 107 uint8_t key1[48];
110 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); 108 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen);
111 FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3); 109 key1[m_KeyLen + 0] = (uint8_t)objnum;
112 FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2); 110 key1[m_KeyLen + 1] = (uint8_t)(objnum >> 8);
111 key1[m_KeyLen + 2] = (uint8_t)(objnum >> 16);
112 key1[m_KeyLen + 3] = (uint8_t)gennum;
113 key1[m_KeyLen + 4] = (uint8_t)(gennum >> 8);
113 if (m_Cipher == FXCIPHER_AES) { 114 if (m_Cipher == FXCIPHER_AES) {
114 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4); 115 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
115 } 116 }
116 uint8_t realkey[16]; 117 uint8_t realkey[16];
117 CRYPT_MD5Generate( 118 CRYPT_MD5Generate(
118 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey); 119 key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey);
119 int realkeylen = m_KeyLen + 5; 120 int realkeylen = m_KeyLen + 5;
120 if (realkeylen > 16) { 121 if (realkeylen > 16) {
121 realkeylen = 16; 122 realkeylen = 16;
122 } 123 }
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 return TRUE; 329 return TRUE;
329 } 330 }
330 CPDF_CryptoHandler::CPDF_CryptoHandler() { 331 CPDF_CryptoHandler::CPDF_CryptoHandler() {
331 m_pAESContext = nullptr; 332 m_pAESContext = nullptr;
332 m_Cipher = FXCIPHER_NONE; 333 m_Cipher = FXCIPHER_NONE;
333 m_KeyLen = 0; 334 m_KeyLen = 0;
334 } 335 }
335 CPDF_CryptoHandler::~CPDF_CryptoHandler() { 336 CPDF_CryptoHandler::~CPDF_CryptoHandler() {
336 FX_Free(m_pAESContext); 337 FX_Free(m_pAESContext);
337 } 338 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698