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

Side by Side Diff: core/fdrm/crypto/fx_crypt.cpp

Issue 2517153003: Add unit test for fdrm's MD5 (Closed)
Patch Set: year Created 4 years 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/fdrm/crypto/fx_crypt.h ('k') | core/fdrm/crypto/fx_crypt_unittest.cpp » ('j') | 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/fdrm/crypto/fx_crypt.h" 7 #include "core/fdrm/crypto/fx_crypt.h"
8 8
9 #ifdef __cplusplus 9 #ifdef __cplusplus
10 extern "C" { 10 extern "C" {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 s->y = y; 50 s->y = y;
51 } 51 }
52 void CRYPT_ArcFourCryptBlock(uint8_t* pData, 52 void CRYPT_ArcFourCryptBlock(uint8_t* pData,
53 uint32_t size, 53 uint32_t size,
54 const uint8_t* key, 54 const uint8_t* key,
55 uint32_t keylen) { 55 uint32_t keylen) {
56 rc4_state s; 56 rc4_state s;
57 CRYPT_ArcFourSetup(&s, key, keylen); 57 CRYPT_ArcFourSetup(&s, key, keylen);
58 CRYPT_ArcFourCrypt(&s, pData, size); 58 CRYPT_ArcFourCrypt(&s, pData, size);
59 } 59 }
60 struct md5_context {
61 uint32_t total[2];
62 uint32_t state[4];
63 uint8_t buffer[64];
64 };
65 #define GET_UINT32(n, b, i) \ 60 #define GET_UINT32(n, b, i) \
66 { \ 61 { \
67 (n) = (uint32_t)((uint8_t*)b)[(i)] | \ 62 (n) = (uint32_t)((uint8_t*)b)[(i)] | \
68 (((uint32_t)((uint8_t*)b)[(i) + 1]) << 8) | \ 63 (((uint32_t)((uint8_t*)b)[(i) + 1]) << 8) | \
69 (((uint32_t)((uint8_t*)b)[(i) + 2]) << 16) | \ 64 (((uint32_t)((uint8_t*)b)[(i) + 2]) << 16) | \
70 (((uint32_t)((uint8_t*)b)[(i) + 3]) << 24); \ 65 (((uint32_t)((uint8_t*)b)[(i) + 3]) << 24); \
71 } 66 }
72 #define PUT_UINT32(n, b, i) \ 67 #define PUT_UINT32(n, b, i) \
73 { \ 68 { \
74 (((uint8_t*)b)[(i)]) = (uint8_t)(((n)) & 0xFF); \ 69 (((uint8_t*)b)[(i)]) = (uint8_t)(((n)) & 0xFF); \
75 (((uint8_t*)b)[(i) + 1]) = (uint8_t)(((n) >> 8) & 0xFF); \ 70 (((uint8_t*)b)[(i) + 1]) = (uint8_t)(((n) >> 8) & 0xFF); \
76 (((uint8_t*)b)[(i) + 2]) = (uint8_t)(((n) >> 16) & 0xFF); \ 71 (((uint8_t*)b)[(i) + 2]) = (uint8_t)(((n) >> 16) & 0xFF); \
77 (((uint8_t*)b)[(i) + 3]) = (uint8_t)(((n) >> 24) & 0xFF); \ 72 (((uint8_t*)b)[(i) + 3]) = (uint8_t)(((n) >> 24) & 0xFF); \
78 } 73 }
79 void md5_process(struct md5_context* ctx, const uint8_t data[64]) { 74 void md5_process(struct CRYPT_md5_context* ctx, const uint8_t data[64]) {
80 uint32_t A, B, C, D, X[16]; 75 uint32_t A, B, C, D, X[16];
81 GET_UINT32(X[0], data, 0); 76 GET_UINT32(X[0], data, 0);
82 GET_UINT32(X[1], data, 4); 77 GET_UINT32(X[1], data, 4);
83 GET_UINT32(X[2], data, 8); 78 GET_UINT32(X[2], data, 8);
84 GET_UINT32(X[3], data, 12); 79 GET_UINT32(X[3], data, 12);
85 GET_UINT32(X[4], data, 16); 80 GET_UINT32(X[4], data, 16);
86 GET_UINT32(X[5], data, 20); 81 GET_UINT32(X[5], data, 20);
87 GET_UINT32(X[6], data, 24); 82 GET_UINT32(X[6], data, 24);
88 GET_UINT32(X[7], data, 28); 83 GET_UINT32(X[7], data, 28);
89 GET_UINT32(X[8], data, 32); 84 GET_UINT32(X[8], data, 32);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 P(D, A, B, C, 11, 10, 0xBD3AF235); 170 P(D, A, B, C, 11, 10, 0xBD3AF235);
176 P(C, D, A, B, 2, 15, 0x2AD7D2BB); 171 P(C, D, A, B, 2, 15, 0x2AD7D2BB);
177 P(B, C, D, A, 9, 21, 0xEB86D391); 172 P(B, C, D, A, 9, 21, 0xEB86D391);
178 #undef F 173 #undef F
179 ctx->state[0] += A; 174 ctx->state[0] += A;
180 ctx->state[1] += B; 175 ctx->state[1] += B;
181 ctx->state[2] += C; 176 ctx->state[2] += C;
182 ctx->state[3] += D; 177 ctx->state[3] += D;
183 } 178 }
184 void CRYPT_MD5Start(void* context) { 179 void CRYPT_MD5Start(void* context) {
185 struct md5_context* ctx = (struct md5_context*)context; 180 struct CRYPT_md5_context* ctx = (struct CRYPT_md5_context*)context;
186 ctx->total[0] = 0; 181 ctx->total[0] = 0;
187 ctx->total[1] = 0; 182 ctx->total[1] = 0;
188 ctx->state[0] = 0x67452301; 183 ctx->state[0] = 0x67452301;
189 ctx->state[1] = 0xEFCDAB89; 184 ctx->state[1] = 0xEFCDAB89;
190 ctx->state[2] = 0x98BADCFE; 185 ctx->state[2] = 0x98BADCFE;
191 ctx->state[3] = 0x10325476; 186 ctx->state[3] = 0x10325476;
192 } 187 }
193 void CRYPT_MD5Update(void* pctx, const uint8_t* input, uint32_t length) { 188 void CRYPT_MD5Update(void* pctx, const uint8_t* input, uint32_t length) {
194 struct md5_context* ctx = (struct md5_context*)pctx; 189 struct CRYPT_md5_context* ctx = (struct CRYPT_md5_context*)pctx;
195 uint32_t left, fill; 190 uint32_t left, fill;
196 if (!length) { 191 if (!length) {
197 return; 192 return;
198 } 193 }
199 left = (ctx->total[0] >> 3) & 0x3F; 194 left = (ctx->total[0] >> 3) & 0x3F;
200 fill = 64 - left; 195 fill = 64 - left;
201 ctx->total[0] += length << 3; 196 ctx->total[0] += length << 3;
202 ctx->total[1] += length >> 29; 197 ctx->total[1] += length >> 29;
203 ctx->total[0] &= 0xFFFFFFFF; 198 ctx->total[0] &= 0xFFFFFFFF;
204 ctx->total[1] += ctx->total[0] < length << 3; 199 ctx->total[1] += ctx->total[0] < length << 3;
(...skipping 11 matching lines...) Expand all
216 } 211 }
217 if (length) { 212 if (length) {
218 FXSYS_memcpy((void*)(ctx->buffer + left), (void*)input, length); 213 FXSYS_memcpy((void*)(ctx->buffer + left), (void*)input, length);
219 } 214 }
220 } 215 }
221 const uint8_t md5_padding[64] = { 216 const uint8_t md5_padding[64] = {
222 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 217 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
225 void CRYPT_MD5Finish(void* pctx, uint8_t digest[16]) { 220 void CRYPT_MD5Finish(void* pctx, uint8_t digest[16]) {
226 struct md5_context* ctx = (struct md5_context*)pctx; 221 struct CRYPT_md5_context* ctx = (struct CRYPT_md5_context*)pctx;
227 uint32_t last, padn; 222 uint32_t last, padn;
228 uint8_t msglen[8]; 223 uint8_t msglen[8];
229 PUT_UINT32(ctx->total[0], msglen, 0); 224 PUT_UINT32(ctx->total[0], msglen, 0);
230 PUT_UINT32(ctx->total[1], msglen, 4); 225 PUT_UINT32(ctx->total[1], msglen, 4);
231 last = (ctx->total[0] >> 3) & 0x3F; 226 last = (ctx->total[0] >> 3) & 0x3F;
232 padn = (last < 56) ? (56 - last) : (120 - last); 227 padn = (last < 56) ? (56 - last) : (120 - last);
233 CRYPT_MD5Update(ctx, md5_padding, padn); 228 CRYPT_MD5Update(ctx, md5_padding, padn);
234 CRYPT_MD5Update(ctx, msglen, 8); 229 CRYPT_MD5Update(ctx, msglen, 8);
235 PUT_UINT32(ctx->state[0], digest, 0); 230 PUT_UINT32(ctx->state[0], digest, 0);
236 PUT_UINT32(ctx->state[1], digest, 4); 231 PUT_UINT32(ctx->state[1], digest, 4);
237 PUT_UINT32(ctx->state[2], digest, 8); 232 PUT_UINT32(ctx->state[2], digest, 8);
238 PUT_UINT32(ctx->state[3], digest, 12); 233 PUT_UINT32(ctx->state[3], digest, 12);
239 } 234 }
240 void CRYPT_MD5Generate(const uint8_t* input, 235 void CRYPT_MD5Generate(const uint8_t* input,
241 uint32_t length, 236 uint32_t length,
242 uint8_t digest[16]) { 237 uint8_t digest[16]) {
243 md5_context ctx; 238 CRYPT_md5_context ctx;
244 CRYPT_MD5Start(&ctx); 239 CRYPT_MD5Start(&ctx);
245 CRYPT_MD5Update(&ctx, input, length); 240 CRYPT_MD5Update(&ctx, input, length);
246 CRYPT_MD5Finish(&ctx, digest); 241 CRYPT_MD5Finish(&ctx, digest);
247 } 242 }
248 static bool (*g_PubKeyDecryptor)(const uint8_t* pData, 243 static bool (*g_PubKeyDecryptor)(const uint8_t* pData,
249 uint32_t size, 244 uint32_t size,
250 uint8_t* data_buf, 245 uint8_t* data_buf,
251 uint32_t& data_len) = nullptr; 246 uint32_t& data_len) = nullptr;
252 void CRYPT_SetPubKeyDecryptor(bool (*func)(const uint8_t* pData, 247 void CRYPT_SetPubKeyDecryptor(bool (*func)(const uint8_t* pData,
253 uint32_t size, 248 uint32_t size,
254 uint8_t* data_buf, 249 uint8_t* data_buf,
255 uint32_t& data_len)) { 250 uint32_t& data_len)) {
256 g_PubKeyDecryptor = func; 251 g_PubKeyDecryptor = func;
257 } 252 }
258 #ifdef __cplusplus 253 #ifdef __cplusplus
259 }; 254 };
260 #endif 255 #endif
OLDNEW
« no previous file with comments | « core/fdrm/crypto/fx_crypt.h ('k') | core/fdrm/crypto/fx_crypt_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698