1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::{
    helper::{
        bit_distance, find_quartiles, l_capturing, mod_diff, pearson_hash, BUCKET_SIZE, WINDOW_SIZE,
    },
    TlshError,
};

const BUCKETS_A: [BucketKind; 2] = [BucketKind::Bucket128, BucketKind::Bucket256];
const CHECKSUM_A: [ChecksumKind; 2] = [ChecksumKind::OneByte, ChecksumKind::ThreeByte];
const VERSION_A: [Version; 2] = [Version::Original, Version::Version4];

/// A struct containing all required information from an input stream to generate a hash value.
///
/// An instance of this struct can be obtained by calling the function [`TlshBuilder::build`].
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Tlsh {
    bucket_kind: BucketKind,
    checksum_kind: ChecksumKind,
    ver: Version,
    checksum: Vec<u8>,
    len: usize,
    q1ratio: usize,
    q2ratio: usize,
    codes: Vec<u8>,
}

impl Tlsh {
    /// Try to convert a hash string. Returns an instance of [`Tlsh`] if the conversion is successful.
    pub fn from_str<T>(s: T) -> Result<Self, TlshError>
    where
        T: AsRef<str>,
    {
        let (mut bucket_kind, mut checksum_kind, mut ver) = (None, None, None);

        'outer: for bk in &BUCKETS_A {
            for ck in &CHECKSUM_A {
                for v in &VERSION_A {
                    if s.as_ref().len() == hash_len(*bk, *ck, *v) {
                        bucket_kind = Some(*bk);
                        checksum_kind = Some(*ck);
                        ver = Some(*v);
                        break 'outer;
                    }
                }
            }
        }

        if bucket_kind.is_none() {
            Err(TlshError::InvalidHashValue)?
        }

        let mut offset = ver.unwrap().ver().len();
        let mut checksum = vec![0; checksum_kind.unwrap().checksum_len()];
        let mut codes = vec![0; bucket_kind.unwrap().bucket_count() >> 2];

        for ii in 0..checksum.len() {
            checksum[ii] = u8::from_str_radix(
                &s.as_ref()[offset..(offset + 2)]
                    .chars()
                    .rev()
                    .collect::<String>(),
                16,
            )?;
            offset += 2;
        }

        let len = usize::from_str_radix(
            &s.as_ref()[offset..(offset + 2)]
                .chars()
                .rev()
                .collect::<String>(),
            16,
        )?;
        offset += 2;

        let qratio: usize = usize::from_str_radix(&s.as_ref()[offset..(offset + 2)], 16)?;
        offset += 2;

        let clen = codes.len();

        for ii in 0..clen {
            codes[clen - ii - 1] = u8::from_str_radix(&s.as_ref()[offset..(offset + 2)], 16)?;
            offset += 2;
        }

        Ok(Self {
            bucket_kind: bucket_kind.unwrap(),
            checksum_kind: checksum_kind.unwrap(),
            ver: ver.unwrap(),
            checksum: checksum,
            len,
            q1ratio: qratio >> 4,
            q2ratio: qratio & 0xF,
            codes,
        })
    }

    /// Computes and returns the hash value in hex-encoded string format.
    pub fn hash(&self) -> String {
        let cap = hash_len(self.bucket_kind, self.checksum_kind, self.ver);
        let mut result = String::with_capacity(cap);
        result.push_str(self.ver.ver());

        for ii in 0..self.checksum.len() {
            result.push_str(
                &format!("{:02X}", self.checksum[ii])
                    .chars()
                    .rev()
                    .collect::<String>(),
            );
        }
        result.push_str(
            &format!("{:02X}", self.len as u32)
                .chars()
                .rev()
                .collect::<String>(),
        );
        result.push_str(&format!("{:02X}", self.q1ratio << 4 | self.q2ratio));

        let len = self.codes.len();
        for ii in 0..len {
            result.push_str(&format!("{:02X}", self.codes[len - 1 - ii]));
        }

        result
    }

    /// Calculates the difference between two TLSH values.
    ///
    /// ```with_len``` controls whether the difference in length should be also considered in the calculation.
    pub fn diff(&self, other: &Tlsh, with_len: bool) -> usize {
        let mut result = 0;

        if with_len {
            match mod_diff(self.len, other.len, 256) {
                x @ 0..=1 => result = x,
                x @ _ => result = x * 12,
            };
        }

        match mod_diff(self.q1ratio, other.q1ratio, 16) {
            x @ 0..=1 => result += x,
            x @ _ => result += (x - 1) * 12,
        }

        match mod_diff(self.q2ratio, other.q2ratio, 16) {
            x @ 0..=1 => result += x,
            x @ _ => result += (x - 1) * 12,
        }

        for ii in 0..self.checksum.len() {
            if self.checksum[ii] != other.checksum[ii] {
                result += 1;
                break;
            }
        }

        result += bit_distance(&self.codes, &other.codes);

        result
    }
}

/// A builder struct for processing input stream(s).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TlshBuilder {
    bucket_kind: BucketKind,
    checksum_kind: ChecksumKind,
    buckets: [u32; BUCKET_SIZE],
    bucket_count: usize,
    checksum: u8,
    checksum_array: Vec<u8>,
    checksum_len: usize,
    code_size: usize,
    data_len: usize,
    slide_window: [u8; WINDOW_SIZE],
    ver: Version,
}

impl TlshBuilder {
    /// Constructs a new builder based on the number of buckets, checksum length and version.
    pub fn new(bucket: BucketKind, checksum: ChecksumKind, ver: Version) -> Self {
        let bucket_count = bucket.bucket_count();
        let checksum_len = checksum.checksum_len();

        Self {
            bucket_kind: bucket,
            checksum_kind: checksum,
            buckets: [0; BUCKET_SIZE],
            bucket_count,
            checksum: 0,
            checksum_array: vec![0; checksum_len],
            checksum_len,
            code_size: bucket_count >> 2,
            data_len: 0,
            slide_window: [0; WINDOW_SIZE],
            ver,
        }
    }

    /// Computes the quartiles and constructs the digest message and returns an instance of [`Tlsh`]
    /// that has all information needed to generate a hash value.
    pub fn build(&self) -> Result<Tlsh, TlshError> {
        if self.data_len < 50 {
            Err(TlshError::MinSizeNotReached)?
        }

        let (q1, q2, q3) = find_quartiles(&self.buckets, self.bucket_count);

        if q3 == 0 {
            panic!("q3 = 0")
        }

        let mut tmp = vec![0; self.code_size];
        for ii in 0..self.code_size {
            let mut h = 0;

            for jj in 0..4 {
                // Out of bound check?
                let kk = self.buckets[4 * ii + jj];
                if q3 < kk {
                    h += 3 << (jj * 2);
                } else if q2 < kk {
                    h += 2 << (jj * 2);
                } else if q1 < kk {
                    h += 1 << (jj * 2);
                }
            }

            tmp[ii] = h;
        }

        let len = l_capturing(self.data_len).unwrap();
        let q1ratio = (((q1 as f64 * 100.) / (q3 as f64)) as usize) % 16;
        let q2ratio = (((q2 as f64 * 100.) / (q3 as f64)) as usize) % 16;

        let checksum = if self.checksum_len == 1 {
            vec![self.checksum]
        } else {
            self.checksum_array.clone()
        };

        Ok(Tlsh {
            bucket_kind: self.bucket_kind,
            checksum_kind: self.checksum_kind,
            ver: self.ver,
            checksum: checksum,
            len,
            q1ratio,
            q2ratio,
            codes: tmp,
        })
    }

    /// Processes an input stream.
    pub fn update(&mut self, data: &[u8]) {
        self.update_from(data, 0, data.len());
    }

    /// Reads an input stream from an offset an processes it.
    ///
    /// # Parameters
    /// * data: input data to be added
    /// * offset: index in array from which data will be read
    /// * len: number of bytes to be read
    pub fn update_from(&mut self, data: &[u8], offset: usize, len: usize) {
        let mut j0 = self.data_len % WINDOW_SIZE;
        let (mut j1, mut j2, mut j3, mut j4) = (
            (j0 + WINDOW_SIZE - 1) % WINDOW_SIZE,
            (j0 + WINDOW_SIZE - 2) % WINDOW_SIZE,
            (j0 + WINDOW_SIZE - 3) % WINDOW_SIZE,
            (j0 + WINDOW_SIZE - 4) % WINDOW_SIZE,
        );

        let mut fed_len = self.data_len;

        for ii in offset..(offset + len) {
            self.slide_window[j0] = data[ii];

            if fed_len >= 4 {
                self.checksum = pearson_hash(
                    0,
                    self.slide_window[j0],
                    self.slide_window[j1],
                    self.checksum,
                );

                if self.checksum_len > 1 {
                    self.checksum_array[0] = self.checksum;

                    for kk in 1..self.checksum_len {
                        self.checksum_array[kk] = pearson_hash(
                            self.checksum_array[kk - 1],
                            self.slide_window[j0],
                            self.slide_window[j1],
                            self.checksum_array[kk],
                        )
                    }
                }

                // Select 6 triplets out of 10. The last four are processed in the next iteration.
                // A  - B   - C  - D  - E
                // j0   j1    j2   j3   j4

                let mut r = pearson_hash(
                    2,
                    self.slide_window[j0],
                    self.slide_window[j1],
                    self.slide_window[j2],
                );
                self.buckets[r as usize] += 1;

                r = pearson_hash(
                    3,
                    self.slide_window[j0],
                    self.slide_window[j1],
                    self.slide_window[j3],
                );
                self.buckets[r as usize] += 1;

                r = pearson_hash(
                    5,
                    self.slide_window[j0],
                    self.slide_window[j2],
                    self.slide_window[j3],
                );
                self.buckets[r as usize] += 1;

                r = pearson_hash(
                    7,
                    self.slide_window[j0],
                    self.slide_window[j2],
                    self.slide_window[j4],
                );
                self.buckets[r as usize] += 1;

                r = pearson_hash(
                    11,
                    self.slide_window[j0],
                    self.slide_window[j1],
                    self.slide_window[j4],
                );
                self.buckets[r as usize] += 1;

                r = pearson_hash(
                    13,
                    self.slide_window[j0],
                    self.slide_window[j3],
                    self.slide_window[j4],
                );
                self.buckets[r as usize] += 1;
            }

            fed_len += 1;

            let tmp = j4;
            j4 = j3;
            j3 = j2;
            j2 = j1;
            j1 = j0;
            j0 = tmp;
        }

        self.data_len += len;
    }

    /// Clears the state of a builder, removing all data.
    pub fn reset(&mut self) {
        self.buckets.fill(0);
        self.checksum = 0;
        self.data_len = 0;
        self.slide_window.fill(0);
    }
}

/// An enum determining the number of buckets for hashing.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum BucketKind {
    /// Hashing with 128 buckets.
    Bucket128,
    /// Hashing with 256 buckets.
    Bucket256,
}

impl BucketKind {
    /// Returns the number of buckets.
    pub fn bucket_count(&self) -> usize {
        match self {
            BucketKind::Bucket128 => 128,
            BucketKind::Bucket256 => 256,
        }
    }
}

/// An enum determining the length of checksum.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum ChecksumKind {
    /// TLSH uses one byte for checksum. The collision rate is 1/24.
    OneByte,
    /// TLSH uses three bytes for checksum. The collision rate is 1/5800.
    ThreeByte,
}

impl ChecksumKind {
    pub fn checksum_len(&self) -> usize {
        match self {
            ChecksumKind::OneByte => 1,
            ChecksumKind::ThreeByte => 3,
        }
    }
}

/// An enum representing the version of TLSH.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum Version {
    /// Original version, mapping to an empty string ```""```.
    Original,
    /// Current version, mapping to an string ```"T1"```.
    Version4,
}

impl Version {
    pub fn ver(&self) -> &str {
        match self {
            Version::Original => "",
            Version::Version4 => "T1",
        }
    }
}

fn hash_len(bucket: BucketKind, checksum: ChecksumKind, ver: Version) -> usize {
    (bucket.bucket_count() >> 1) + (checksum.checksum_len() << 1) + ver.ver().len() + 4
}