blob: 0acd6b89b1ed116490edd2ee1308816a0d7ab830 [file] [log] [blame]
Stephen Hines6975a662012-05-03 12:26:44 -07001/*
Stephen Hines2f6a4932012-05-03 12:27:13 -07002 * copyright 2010, the android open source project
Stephen Hines6975a662012-05-03 12:26:44 -07003 *
4 * licensed under the apache license, version 2.0 (the "license");
5 * you may not use this file except in compliance with the license.
6 * you may obtain a copy of the license at
7 *
8 * http://www.apache.org/licenses/license-2.0
9 *
10 * unless required by applicable law or agreed to in writing, software
11 * distributed under the license is distributed on an "as is" basis,
12 * without warranties or conditions of any kind, either express or implied.
13 * see the license for the specific language governing permissions and
14 * limitations under the license.
15 */
16
Stephen Hines2f6a4932012-05-03 12:27:13 -070017#include "Sha1Helper.h"
Stephen Hines6975a662012-05-03 12:26:44 -070018
Stephen Hines2f6a4932012-05-03 12:27:13 -070019#include "Config.h"
20
21#include "DebugHelper.h"
Stephen Hines758d00c2012-05-03 12:30:15 -070022#include "FileHandle.h"
Stephen Hines2f6a4932012-05-03 12:27:13 -070023
24#include <string.h>
Stephen Hines6975a662012-05-03 12:26:44 -070025
26#include <utils/StopWatch.h>
27
Stephen Hines2f6a4932012-05-03 12:27:13 -070028#include <sha1.h>
Stephen Hines6975a662012-05-03 12:26:44 -070029
30namespace bcc {
31
32unsigned char sha1LibBCC_SHA1[20];
33char const *pathLibBCC_SHA1 = "/system/lib/libbcc.so.sha1";
34
35unsigned char sha1LibRS[20];
36char const *pathLibRS = "/system/lib/libRS.so";
37
38void calcSHA1(unsigned char *result, char const *data, size_t size) {
39 SHA1_CTX hashContext;
40
41 SHA1Init(&hashContext);
42 SHA1Update(&hashContext,
43 reinterpret_cast<const unsigned char *>(data),
44 static_cast<unsigned long>(size));
45
46 SHA1Final(result, &hashContext);
47}
48
49
50void calcFileSHA1(unsigned char *result, char const *filename) {
51 android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
52
Stephen Hines758d00c2012-05-03 12:30:15 -070053 FileHandle file;
Stephen Hines6975a662012-05-03 12:26:44 -070054
Stephen Hines758d00c2012-05-03 12:30:15 -070055 if (file.open(filename, OpenMode::Read) < 0) {
56 ALOGE("Unable to calculate the sha1 checksum of %s\n", filename);
Stephen Hines6975a662012-05-03 12:26:44 -070057 memset(result, '\0', 20);
58 return;
59 }
60
61 SHA1_CTX hashContext;
62 SHA1Init(&hashContext);
63
64 char buf[256];
65 while (true) {
66 ssize_t nread = file.read(buf, sizeof(buf));
67
68 if (nread < 0) {
69 break;
70 }
71
72 SHA1Update(&hashContext,
73 reinterpret_cast<unsigned char *>(buf),
74 static_cast<unsigned long>(nread));
75
76 if ((size_t)nread < sizeof(buf)) {
77 // finished.
78 break;
79 }
80 }
81
82 SHA1Final(result, &hashContext);
83}
84
85void readSHA1(unsigned char *result, int result_size, char const *filename) {
Stephen Hines758d00c2012-05-03 12:30:15 -070086 FileHandle file;
87 if (file.open(filename, OpenMode::Read) < 0) {
88 ALOGE("Unable to read binary sha1 file %s\n", filename);
Stephen Hines6975a662012-05-03 12:26:44 -070089 memset(result, '\0', result_size);
90 return;
91 }
92 file.read((char *)result, result_size);
93}
94
95} // namespace bcc