1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
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 
17 #include "ChecksumCalculator.h"
18 
19 #include "aemu/base/files/Stream.h"
20 
21 #include <string>
22 #include <vector>
23 
24 #include <assert.h>
25 #include <string.h>
26 
27 // Checklist when implementing new protocol:
28 // 1. update CHECKSUMHELPER_MAX_VERSION
29 // 2. update checksumByteSize()
30 // 3. update addBuffer, writeChecksum, resetChecksum, validate
31 
32 // change CHECKSUMHELPER_MAX_VERSION when you want to update the protocol version
33 #define CHECKSUMHELPER_MAX_VERSION 1
34 
35 // utility macros to create checksum string at compilation time
36 #define CHECKSUMHELPER_VERSION_STR_PREFIX "ANDROID_EMU_CHECKSUM_HELPER_v"
37 #define CHECKSUMHELPER_MACRO_TO_STR(x) #x
38 #define CHECKSUMHELPER_MACRO_VAL_TO_STR(x) CHECKSUMHELPER_MACRO_TO_STR(x)
39 
40 static const uint32_t kMaxVersion = CHECKSUMHELPER_MAX_VERSION;
41 static const char* kMaxVersionStrPrefix = CHECKSUMHELPER_VERSION_STR_PREFIX;
42 static const char* kMaxVersionStr = CHECKSUMHELPER_VERSION_STR_PREFIX CHECKSUMHELPER_MACRO_VAL_TO_STR(CHECKSUMHELPER_MAX_VERSION);
43 
44 #undef CHECKSUMHELPER_MAX_VERSION
45 #undef CHECKSUMHELPER_VERSION_STR_PREFIX
46 #undef CHECKSUMHELPER_MACRO_TO_STR
47 #undef CHECKSUMHELPER_MACRO_VAL_TO_STR
48 
getMaxVersion()49 uint32_t ChecksumCalculator::getMaxVersion() {return kMaxVersion;}
getMaxVersionStr()50 const char* ChecksumCalculator::getMaxVersionStr() {return kMaxVersionStr;}
getMaxVersionStrPrefix()51 const char* ChecksumCalculator::getMaxVersionStrPrefix() {return kMaxVersionStrPrefix;}
52 
setVersion(uint32_t version)53 bool ChecksumCalculator::setVersion(uint32_t version) {
54     if (version > kMaxVersion) {  // unsupported version
55         LOG_CHECKSUMHELPER("%s: ChecksumCalculator Set Unsupported version Version %d\n",
56                 __FUNCTION__, m_version);
57         return false;
58     }
59     if (m_isEncodingChecksum) { // setVersion is called in the middle of encoding checksums
60         LOG_CHECKSUMHELPER("%s: called between addBuffer and writeChecksum\n",
61                 __FUNCTION__);
62         return false;
63     }
64     m_version = version;
65     m_checksumSize = checksumByteSize(version);
66     LOG_CHECKSUMHELPER("%s: ChecksumCalculator Set Version %d\n", __FUNCTION__,
67                 m_version);
68     return true;
69 }
70 
addBuffer(const void * buf,size_t packetLen)71 void ChecksumCalculator::addBuffer(const void* buf, size_t packetLen) {
72     m_isEncodingChecksum = true;
73     switch (m_version) {
74         case 1:
75             m_v1BufferTotalLength += packetLen;
76             break;
77     }
78 }
79 
writeChecksum(void * outputChecksum,size_t outputChecksumLen)80 bool ChecksumCalculator::writeChecksum(void* outputChecksum, size_t outputChecksumLen) {
81     if (outputChecksumLen < checksumByteSize()) return false;
82     char *checksumPtr = (char *)outputChecksum;
83     switch (m_version) {
84         case 1: { // protocol v1 is to reverse the packetLen and write it at the end
85             uint32_t val = computeV1Checksum();
86             memcpy(checksumPtr, &val, sizeof(val));
87             memcpy(checksumPtr+sizeof(val), &m_numWrite, sizeof(m_numWrite));
88             break;
89         }
90     }
91     resetChecksum();
92     m_numWrite++;
93     return true;
94 }
95 
resetChecksum()96 void ChecksumCalculator::resetChecksum() {
97     switch (m_version) {
98         case 1:
99             m_v1BufferTotalLength = 0;
100             break;
101     }
102     m_isEncodingChecksum = false;
103 }
104 
validate(const void * expectedChecksum,size_t expectedChecksumLen)105 bool ChecksumCalculator::validate(const void* expectedChecksum,
106                                   size_t expectedChecksumLen) {
107     const size_t checksumSize = checksumByteSize();
108     if (expectedChecksumLen != checksumSize) {
109         m_numRead++;
110         resetChecksum();
111         return false;
112     }
113     bool isValid;
114     switch (m_version) {
115         case 1: {
116             const uint32_t val = computeV1Checksum();
117             assert(checksumSize == sizeof(val) + sizeof(m_numRead));
118             isValid = 0 == memcmp(&val, expectedChecksum, sizeof(val)) &&
119                       0 == memcmp(&m_numRead,
120                                   static_cast<const char*>(expectedChecksum) +
121                                           sizeof(val),
122                                   sizeof(m_numRead));
123             break;
124         }
125         default:
126             isValid = true;  // No checksum is a valid checksum.
127             break;
128     }
129     m_numRead++;
130     resetChecksum();
131     return isValid;
132 }
133 
computeV1Checksum() const134 uint32_t ChecksumCalculator::computeV1Checksum() const {
135     uint32_t revLen = m_v1BufferTotalLength;
136     revLen = (revLen & 0xffff0000) >> 16 | (revLen & 0x0000ffff) << 16;
137     revLen = (revLen & 0xff00ff00) >> 8 | (revLen & 0x00ff00ff) << 8;
138     revLen = (revLen & 0xf0f0f0f0) >> 4 | (revLen & 0x0f0f0f0f) << 4;
139     revLen = (revLen & 0xcccccccc) >> 2 | (revLen & 0x33333333) << 2;
140     revLen = (revLen & 0xaaaaaaaa) >> 1 | (revLen & 0x55555555) << 1;
141     return revLen;
142 }
143 
save(android::base::Stream * stream)144 void ChecksumCalculator::save(android::base::Stream* stream) {
145     assert(!m_isEncodingChecksum);
146     switch (m_version) {
147     case 1:
148         assert(m_v1BufferTotalLength == 0);
149         break;
150     }
151 
152     // Our checksum should never become > 255 bytes. Ever.
153     assert((uint8_t)m_checksumSize == m_checksumSize);
154     stream->putByte(m_checksumSize);
155     stream->putBe32(m_version);
156     stream->putBe32(m_numRead);
157     stream->putBe32(m_numWrite);
158 }
159 
load(android::base::Stream * stream)160 void ChecksumCalculator::load(android::base::Stream* stream) {
161     assert(!m_isEncodingChecksum);
162     switch (m_version) {
163     case 1:
164         assert(m_v1BufferTotalLength == 0);
165         break;
166     }
167 
168     m_checksumSize = stream->getByte();
169     m_version = stream->getBe32();
170     m_numRead = stream->getBe32();
171     m_numWrite = stream->getBe32();
172 }
173