This file is indexed.

/usr/share/tcltk/tcllib1.18/ripemd/ripemd128.tcl is in tcllib 1.18-dfsg-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# ripemd128.tcl - Copyright (C) 2003 Pat Thoyts <patthoyts@users.sf.net>
#
# This is a Tcl-only implementation of the RIPEMD-128 hash algorithm as 
# described in [RIPE].
# Included is an implementation of keyed message authentication using 
# the RIPEMD-128 function [HMAC].
#
# See http://www.esat.kuleuven.ac.be/~cosicart/pdf/AB-9601/
#
# [RIPE] Dobbertin, H., Bosselaers A., and Preneel, B.
#        "RIPEMD-160: A Strengthened Version of RIPEMD" 
#        Fast Software Encryption, LNCS 1039, D. Gollmann, Ed., 
#        Springer-Verlag, 1996, pp. 71-82
# [HMAC] Krawczyk, H., Bellare, M., and R. Canetti, 
#       "HMAC: Keyed-Hashing for Message Authentication",
#        RFC 2104, February 1997.
#
# RFC 2286, ``Test cases for HMAC-RIPEMD160 and HMAC-RIPEMD128,''
# Internet Request for Comments 2286, J. Kapp, 
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------

package require Tcl 8.2;                # tcl minimum version

namespace eval ::ripemd {
    namespace eval ripemd128 {
        variable  accel
        array set accel {trf 0}

        variable uid
        if {![info exists uid]} {
            set uid 0
        }

        namespace export ripemd128 hmac128 Hex \
            RIPEMD128Init RIPEMD128Update RIPEMD128Final \
            RIPEHMAC128Init RIPEHMAC128Update RIPEHMAC128Final
    }
}

# -------------------------------------------------------------------------

# RIPEMD128Init - create and initialize an MD4 state variable. This will be
# cleaned up when we call MD4Final
#
proc ::ripemd::ripemd128::RIPEMD128Init {} {
    variable accel
    variable uid
    set token [namespace current]::[incr uid]
    upvar #0 $token state

    # Initialize RIPEMD-128 state structure (same as MD4).
    array set state \
        [list \
             A [expr {0x67452301}] \
             B [expr {0xefcdab89}] \
             C [expr {0x98badcfe}] \
             D [expr {0x10325476}] \
             n 0 i "" ]
    if {$accel(trf)} {
        set s {}
        switch -exact -- $::tcl_platform(platform) {
            windows { set s [open NUL w] }
            unix    { set s [open /dev/null w] }
        }
        if {$s != {}} {
            fconfigure $s -translation binary -buffering none
            ::ripemd128 -attach $s -mode write \
                -read-type variable \
                -read-destination [subst $token](trfread) \
                -write-type variable \
                -write-destination [subst $token](trfwrite)
            array set state [list trfread 0 trfwrite 0 trf $s]
        }
    }
    return $token
}

proc ::ripemd::ripemd128::RIPEMD128Update {token data} {
    upvar #0 $token state

    if {[info exists state(trf)]} {
        puts -nonewline $state(trf) $data
        return
    }

    # Update the state values
    incr state(n) [string length $data]
    append state(i) $data

    # Calculate the hash for any complete blocks
    set len [string length $state(i)]
    for {set n 0} {($n + 64) <= $len} {} {
        RIPEMD128Hash $token [string range $state(i) $n [incr n 64]]
    }

    # Adjust the state for the blocks completed.
    set state(i) [string range $state(i) $n end]
    return
}

proc ::ripemd::ripemd128::RIPEMD128Final {token} {
    upvar #0 $token state

    if {[info exists state(trf)]} {
        close $state(trf)
        set r $state(trfwrite)
        unset state
        return $r
    }

    # RFC1320:3.1 - Padding
    #
    set len [string length $state(i)]
    set pad [expr {56 - ($len % 64)}]
    if {$len % 64 > 56} {
        incr pad 64
    }
    if {$pad == 0} {
        incr pad 64
    }
    append state(i) [binary format a$pad \x80]

    # RFC1320:3.2 - Append length in bits as little-endian wide int.
    append state(i) [binary format ii [expr {8 * $state(n)}] 0]

    # Calculate the hash for the remaining block.
    set len [string length $state(i)]
    for {set n 0} {($n + 64) <= $len} {} {
        RIPEMD128Hash $token [string range $state(i) $n [incr n 64]]
    }

    # RFC1320:3.5 - Output
    set r [bytes $state(A)][bytes $state(B)][bytes $state(C)][bytes $state(D)]
    unset state
    return $r
}

# -------------------------------------------------------------------------
# HMAC Hashed Message Authentication (RFC 2104)
#
# hmac = H(K xor opad, H(K xor ipad, text))
#
proc ::ripemd::ripemd128::RIPEHMAC128Init {K} {

    # Key K is adjusted to be 64 bytes long. If K is larger, then use
    # the RIPEMD-128 digest of K and pad this instead.
    set len [string length $K]
    if {$len > 64} {
        set tok [RIPEMD128Init]
        RIPEMD128Update $tok $K
        set K [RIPEMD128Final $tok]
        set len [string length $K]
    }
    set pad [expr {64 - $len}]
    append K [string repeat \0 $pad]

    # Cacluate the padding buffers.
    set Ki {}
    set Ko {}
    binary scan $K i16 Ks
    foreach k $Ks {
        append Ki [binary format i [expr {$k ^ 0x36363636}]]
        append Ko [binary format i [expr {$k ^ 0x5c5c5c5c}]]
    }

    set tok [RIPEMD128Init]
    RIPEMD128Update $tok $Ki;                 # initialize with the inner pad
    
    # preserve the Ko value for the final stage.
    # FRINK: nocheck
    set [subst $tok](Ko) $Ko

    return $tok
}

proc ::ripemd::ripemd128::RIPEHMAC128Update {token data} {
    RIPEMD128Update $token $data
    return
}

proc ::ripemd::ripemd128::RIPEHMAC128Final {token} {
    # FRINK: nocheck
    variable $token
    upvar 0 $token state

    set tok [RIPEMD128Init];            # init the outer hashing function
    RIPEMD128Update $tok $state(Ko);    # prepare with the outer pad.
    RIPEMD128Update $tok [RIPEMD128Final $token];  # hash the inner result
    return [RIPEMD128Final $tok]
}

# -------------------------------------------------------------------------

set ::ripemd::ripemd128::RIPEMD128Hash_body {
    variable $token
    upvar 0 $token state

    # RFC1320:3.4 - Process Message in 16-Word Blocks
    binary scan $msg i* blocks
    foreach {X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15} $blocks {
        set A $state(A)   ;  set AA $state(A)
        set B $state(B)   ;  set BB $state(B)
        set C $state(C)   ;  set CC $state(C)
        set D $state(D)   ;  set DD $state(D)

        # Round 1 (track 1). 
        #    F(x,y,z) = x ^ y ^ z
        # Let [abcd x s] denote the operation
        #    a = (a + F(b,c,d) + X[x]) <<< s.
        # Do the following 16 operations.
        # [ABCD  0  11]  [DABC  1  14]  [CDAB  2 15]  [BCDA  3 12]
        set A [expr {($A + [F $B $C $D] + $X0)  <<< 11}]
        set D [expr {($D + [F $A $B $C] + $X1)  <<< 14}]
        set C [expr {($C + [F $D $A $B] + $X2)  <<< 15}]
        set B [expr {($B + [F $C $D $A] + $X3)  <<< 12}]
        # [ABCD  4  5]  [DABC  5  8]  [CDAB  6 7]  [BCDA  7 9]
        set A [expr {($A + [F $B $C $D] + $X4)  <<< 5}]
        set D [expr {($D + [F $A $B $C] + $X5)  <<< 8}]
        set C [expr {($C + [F $D $A $B] + $X6)  <<< 7}]
        set B [expr {($B + [F $C $D $A] + $X7)  <<< 9}]
        # [ABCD  8  11]  [DABC  9  13]  [CDAB 10 14]  [BCDA 11 15]
        set A [expr {($A + [F $B $C $D] + $X8)  <<< 11}]
        set D [expr {($D + [F $A $B $C] + $X9)  <<< 13}]
        set C [expr {($C + [F $D $A $B] + $X10) <<< 14}]
        set B [expr {($B + [F $C $D $A] + $X11) <<< 15}]
        # [ABCD 12  6]  [DABC 13  7]  [CDAB 14 9]  [BCDA 15 8]
        set A [expr {($A + [F $B $C $D] + $X12) <<< 6}]
        set D [expr {($D + [F $A $B $C] + $X13) <<< 7}]
        set C [expr {($C + [F $D $A $B] + $X14) <<< 9}]
        set B [expr {($B + [F $C $D $A] + $X15) <<< 8}]

        # Round 2 (track 1).
        #   G(x, y, z) = (x & y) | (~x & z)
        # Let [abcd k s] denote the operation
        #   a = (a + G(b,c,d) + X[k] + 5A827999) <<< s
        # Do the following 16 operations.
        # [ABCD  7  7]  [DABC  4  6]  [CDAB  13  8]  [BCDA 1 13]
        set A [expr {($A + [G $B $C $D] + $X7  + 0x5a827999) <<< 7}]
        set D [expr {($D + [G $A $B $C] + $X4  + 0x5a827999) <<< 6}]
        set C [expr {($C + [G $D $A $B] + $X13 + 0x5a827999) <<< 8}]
        set B [expr {($B + [G $C $D $A] + $X1  + 0x5a827999) <<< 13}]
        # [ABCD  10  11]  [DABC  6  9]  [CDAB  15  7]  [BCDA 3 15]
        set A [expr {($A + [G $B $C $D] + $X10 + 0x5a827999) <<< 11}]
        set D [expr {($D + [G $A $B $C] + $X6  + 0x5a827999) <<< 9}]
        set C [expr {($C + [G $D $A $B] + $X15 + 0x5a827999) <<< 7}]
        set B [expr {($B + [G $C $D $A] + $X3  + 0x5a827999) <<< 15}]
        # [ABCD 12  7]  [DABC  0  12]  [CDAB 9  15]  [BCDA 5 9]
        set A [expr {($A + [G $B $C $D] + $X12 + 0x5a827999) <<< 7}]
        set D [expr {($D + [G $A $B $C] + $X0  + 0x5a827999) <<< 12}]
        set C [expr {($C + [G $D $A $B] + $X9  + 0x5a827999) <<< 15}]
        set B [expr {($B + [G $C $D $A] + $X5  + 0x5a827999) <<< 9}]
        # [ABCD  2  11]  [DABC  14  7]  [CDAB 11  13]  [BCDA 8 12]
        set A [expr {($A + [G $B $C $D] + $X2  + 0x5a827999) <<< 11}]
        set D [expr {($D + [G $A $B $C] + $X14 + 0x5a827999) <<< 7}]
        set C [expr {($C + [G $D $A $B] + $X11 + 0x5a827999) <<< 13}]
        set B [expr {($B + [G $C $D $A] + $X8  + 0x5a827999) <<< 12}]
        
        # Round 3 (track 1).
        #   H(x,y,z) = (x | ~y) ^ z
        # Let [abcd k s] denote the operation
        #   a = (a + H(b,c,d) + X[k] + 6ED9EBA1) <<< s.
        # Do the following 16 operations.
        # [ABCD  3  11]  [DABC  10  13]  [CDAB  14 6]  [BCDA 4 7]
        set A [expr {($A + [H $B $C $D] + $X3  + 0x6ed9eba1) <<< 11}]
        set D [expr {($D + [H $A $B $C] + $X10 + 0x6ed9eba1) <<< 13}]
        set C [expr {($C + [H $D $A $B] + $X14 + 0x6ed9eba1) <<< 6}]
        set B [expr {($B + [H $C $D $A] + $X4  + 0x6ed9eba1) <<< 7}]
        # [ABCD  9  14]  [DABC 15  9]  [CDAB  8 13]  [BCDA 1 15]
        set A [expr {($A + [H $B $C $D] + $X9  + 0x6ed9eba1) <<< 14}]
        set D [expr {($D + [H $A $B $C] + $X15 + 0x6ed9eba1) <<< 9}]
        set C [expr {($C + [H $D $A $B] + $X8  + 0x6ed9eba1) <<< 13}]
        set B [expr {($B + [H $C $D $A] + $X1  + 0x6ed9eba1) <<< 15}]
        # [ABCD  2  14]  [DABC  7  8]  [CDAB  0 13]  [BCDA 6 6]
        set A [expr {($A + [H $B $C $D] + $X2  + 0x6ed9eba1) <<< 14}]
        set D [expr {($D + [H $A $B $C] + $X7  + 0x6ed9eba1) <<< 8}]
        set C [expr {($C + [H $D $A $B] + $X0  + 0x6ed9eba1) <<< 13}]
        set B [expr {($B + [H $C $D $A] + $X6  + 0x6ed9eba1) <<< 6}]
        # [ABCD  13  5]  [DABC 11  12]  [CDAB  5 7]  [BCDA 12  5]
        set A [expr {($A + [H $B $C $D] + $X13 + 0x6ed9eba1) <<< 5}]
        set D [expr {($D + [H $A $B $C] + $X11 + 0x6ed9eba1) <<< 12}]
        set C [expr {($C + [H $D $A $B] + $X5  + 0x6ed9eba1) <<< 7}]
        set B [expr {($B + [H $C $D $A] + $X12 + 0x6ed9eba1) <<< 5}]

        # Round 4 (track 1).
        #   I(x,y,z) = (x & z) | (y & ^ ~z)
        # Let [abcd k s] denote the operation
        #   a = (a + I(b,c,d) + X[k] + 8F1BBCDC) <<< s.
        # Do the following 16 operations.
        # [ABCD  1  11]  [DABC  9  12]  [CDAB  11 14]  [BCDA 10 15]
        set A [expr {($A + [I $B $C $D] + $X1  + 0x8f1bbcdc) <<< 11}]
        set D [expr {($D + [I $A $B $C] + $X9  + 0x8f1bbcdc) <<< 12}]
        set C [expr {($C + [I $D $A $B] + $X11 + 0x8f1bbcdc) <<< 14}]
        set B [expr {($B + [I $C $D $A] + $X10 + 0x8f1bbcdc) <<< 15}]
        # [ABCD  0  14]  [DABC 8  15]  [CDAB 12 9]  [BCDA 4 8]
        set A [expr {($A + [I $B $C $D] + $X0  + 0x8f1bbcdc) <<< 14}]
        set D [expr {($D + [I $A $B $C] + $X8  + 0x8f1bbcdc) <<< 15}]
        set C [expr {($C + [I $D $A $B] + $X12 + 0x8f1bbcdc) <<< 9}]
        set B [expr {($B + [I $C $D $A] + $X4  + 0x8f1bbcdc) <<< 8}]
        # [ABCD  13  9]  [DABC  3  14]  [CDAB  7 5]  [BCDA 15 6]
        set A [expr {($A + [I $B $C $D] + $X13 + 0x8f1bbcdc) <<< 9}]
        set D [expr {($D + [I $A $B $C] + $X3  + 0x8f1bbcdc) <<< 14}]
        set C [expr {($C + [I $D $A $B] + $X7  + 0x8f1bbcdc) <<< 5}]
        set B [expr {($B + [I $C $D $A] + $X15 + 0x8f1bbcdc) <<< 6}]
        # [ABCD  14  8]  [DABC 5  6]  [CDAB  6 5]  [BCDA 2 12]
        set A [expr {($A + [I $B $C $D] + $X14 + 0x8f1bbcdc) <<< 8}]
        set D [expr {($D + [I $A $B $C] + $X5  + 0x8f1bbcdc) <<< 6}]
        set C [expr {($C + [I $D $A $B] + $X6  + 0x8f1bbcdc) <<< 5}]
        set B [expr {($B + [I $C $D $A] + $X2  + 0x8f1bbcdc) <<< 12}]


        # Round 1 (track 2).
        #   I(x,y,z) = (x & z) | (y & ^ ~z)
        # Let [abcd k s] denote the operation
        #   a = (a + I(b,c,d) + X[k] + 50A28BE6) <<< s.
        # Do the following 16 operations.
        # [ABCD  5  8]  [DABC  14  9]  [CDAB  7 9]  [BCDA 0 11]
        set AA [expr {($AA + [I $BB $CC $DD] + $X5  + 0x50a28be6) <<< 8}]
        set DD [expr {($DD + [I $AA $BB $CC] + $X14 + 0x50a28be6) <<< 9}]
        set CC [expr {($CC + [I $DD $AA $BB] + $X7  + 0x50a28be6) <<< 9}]
        set BB [expr {($BB + [I $CC $DD $AA] + $X0  + 0x50a28be6) <<< 11}]
        # [ABCD  9  13]  [DABC 2  15]  [CDAB 11 15]  [BCDA 4 5]
        set AA [expr {($AA + [I $BB $CC $DD] + $X9  + 0x50a28be6) <<< 13}]
        set DD [expr {($DD + [I $AA $BB $CC] + $X2  + 0x50a28be6) <<< 15}]
        set CC [expr {($CC + [I $DD $AA $BB] + $X11 + 0x50a28be6) <<< 15}]
        set BB [expr {($BB + [I $CC $DD $AA] + $X4  + 0x50a28be6) <<< 5}]
        # [ABCD  13  7]  [DABC  6  7]  [CDAB  15 8]  [BCDA 8 11]
        set AA [expr {($AA + [I $BB $CC $DD] + $X13 + 0x50a28be6) <<< 7}]
        set DD [expr {($DD + [I $AA $BB $CC] + $X6  + 0x50a28be6) <<< 7}]
        set CC [expr {($CC + [I $DD $AA $BB] + $X15 + 0x50a28be6) <<< 8}]
        set BB [expr {($BB + [I $CC $DD $AA] + $X8  + 0x50a28be6) <<< 11}]
        # [ABCD  1  14]  [DABC 10  14]  [CDAB  3 12]  [BCDA 12 6]
        set AA [expr {($AA + [I $BB $CC $DD] + $X1  + 0x50a28be6) <<< 14}]
        set DD [expr {($DD + [I $AA $BB $CC] + $X10 + 0x50a28be6) <<< 14}]
        set CC [expr {($CC + [I $DD $AA $BB] + $X3  + 0x50a28be6) <<< 12}]
        set BB [expr {($BB + [I $CC $DD $AA] + $X12 + 0x50a28be6) <<< 6}]

        # Round 2 (track 2).
        #   H(x,y,z) = (x | ~y) ^ z
        # Let [abcd k s] denote the operation
        #   a = (a + H(b,c,d) + X[k] + 5C4DD124) <<< s.
        # Do the following 16 operations.
        # [ABCD  6  9]  [DABC  11  13]  [CDAB  3 15]  [BCDA 7 7]
        set AA [expr {($AA + [H $BB $CC $DD] + $X6  + 0x5c4dd124) <<< 9}]
        set DD [expr {($DD + [H $AA $BB $CC] + $X11 + 0x5c4dd124) <<< 13}]
        set CC [expr {($CC + [H $DD $AA $BB] + $X3  + 0x5c4dd124) <<< 15}]
        set BB [expr {($BB + [H $CC $DD $AA] + $X7  + 0x5c4dd124) <<< 7}]
        # [ABCD  0  12]  [DABC 13  8]  [CDAB 5 9]  [BCDA 10 11]
        set AA [expr {($AA + [H $BB $CC $DD] + $X0  + 0x5c4dd124) <<< 12}]
        set DD [expr {($DD + [H $AA $BB $CC] + $X13 + 0x5c4dd124) <<< 8}]
        set CC [expr {($CC + [H $DD $AA $BB] + $X5  + 0x5c4dd124) <<< 9}]
        set BB [expr {($BB + [H $CC $DD $AA] + $X10 + 0x5c4dd124) <<< 11}]
        # [ABCD  14  7]  [DABC  15  7]  [CDAB  8 12]  [BCDA 12 7]
        set AA [expr {($AA + [H $BB $CC $DD] + $X14 + 0x5c4dd124) <<< 7}]
        set DD [expr {($DD + [H $AA $BB $CC] + $X15 + 0x5c4dd124) <<< 7}]
        set CC [expr {($CC + [H $DD $AA $BB] + $X8  + 0x5c4dd124) <<< 12}]
        set BB [expr {($BB + [H $CC $DD $AA] + $X12 + 0x5c4dd124) <<< 7}]
        # [ABCD  4  6]  [DABC 9  15]  [CDAB  1 13]  [BCDA 2 11]
        set AA [expr {($AA + [H $BB $CC $DD] + $X4  + 0x5c4dd124) <<< 6}]
        set DD [expr {($DD + [H $AA $BB $CC] + $X9  + 0x5c4dd124) <<< 15}]
        set CC [expr {($CC + [H $DD $AA $BB] + $X1  + 0x5c4dd124) <<< 13}]
        set BB [expr {($BB + [H $CC $DD $AA] + $X2  + 0x5c4dd124) <<< 11}]

        # Round 3 (track 2).
        #   G(x, y, z) = (x & y) | (~x & z)
        # Let [abcd k s] denote the operation
        #   a = (a + G(b,c,d) + X[k] + 6D703EF3) <<< s.
        # Do the following 16 operations.
        # [ABCD  15  9]  [DABC  5 7]  [CDAB  1 15]  [BCDA 3 11]
        set AA [expr {($AA + [G $BB $CC $DD] + $X15 + 0x6d703ef3) <<< 9}]
        set DD [expr {($DD + [G $AA $BB $CC] + $X5  + 0x6d703ef3) <<< 7}]
        set CC [expr {($CC + [G $DD $AA $BB] + $X1  + 0x6d703ef3) <<< 15}]
        set BB [expr {($BB + [G $CC $DD $AA] + $X3  + 0x6d703ef3) <<< 11}]
        # [ABCD  7  8]  [DABC 14  6]  [CDAB 6 6]  [BCDA 9 14]
        set AA [expr {($AA + [G $BB $CC $DD] + $X7  + 0x6d703ef3) <<< 8}]
        set DD [expr {($DD + [G $AA $BB $CC] + $X14 + 0x6d703ef3) <<< 6}]
        set CC [expr {($CC + [G $DD $AA $BB] + $X6  + 0x6d703ef3) <<< 6}]
        set BB [expr {($BB + [G $CC $DD $AA] + $X9  + 0x6d703ef3) <<< 14}]
        # [ABCD  11  12]  [DABC  8  13]  [CDAB  12 5]  [BCDA 2 14]
        set AA [expr {($AA + [G $BB $CC $DD] + $X11 + 0x6d703ef3) <<< 12}]
        set DD [expr {($DD + [G $AA $BB $CC] + $X8  + 0x6d703ef3) <<< 13}]
        set CC [expr {($CC + [G $DD $AA $BB] + $X12 + 0x6d703ef3) <<< 5}]
        set BB [expr {($BB + [G $CC $DD $AA] + $X2  + 0x6d703ef3) <<< 14}]
        # [ABCD  10  13]  [DABC 0  13]  [CDAB  4 7]  [BCDA 13 5]
        set AA [expr {($AA + [G $BB $CC $DD] + $X10 + 0x6d703ef3) <<< 13}]
        set DD [expr {($DD + [G $AA $BB $CC] + $X0  + 0x6d703ef3) <<< 13}]
        set CC [expr {($CC + [G $DD $AA $BB] + $X4  + 0x6d703ef3) <<< 7}]
        set BB [expr {($BB + [G $CC $DD $AA] + $X13 + 0x6d703ef3) <<< 5}]

        # Round 4 (track 2).
        #   F(x,y,z) = x ^ y ^ z
        # Let [abcd k s] denote the operation
        #   a = (a + F(b,c,d) + X[k]) <<< s.
        # Do the following 16 operations.
        # [ABCD  8  15]  [DABC  6 5]  [CDAB  4 8]  [BCDA 1 11]
        set AA [expr {($AA + [F $BB $CC $DD] + $X8)  <<< 15}]
        set DD [expr {($DD + [F $AA $BB $CC] + $X6)  <<< 5}]
        set CC [expr {($CC + [F $DD $AA $BB] + $X4)  <<< 8}]
        set BB [expr {($BB + [F $CC $DD $AA] + $X1)  <<< 11}]
        # [ABCD  3  14]  [DABC 11 14]  [CDAB 15 6]  [BCDA 0 14]
        set AA [expr {($AA + [F $BB $CC $DD] + $X3)  <<< 14}]
        set DD [expr {($DD + [F $AA $BB $CC] + $X11) <<< 14}]
        set CC [expr {($CC + [F $DD $AA $BB] + $X15) <<< 6}]
        set BB [expr {($BB + [F $CC $DD $AA] + $X0)  <<< 14}]
        # [ABCD  5  6]  [DABC  12 9]  [CDAB  2 12]  [BCDA 13 9]
        set AA [expr {($AA + [F $BB $CC $DD] + $X5)  <<< 6}]
        set DD [expr {($DD + [F $AA $BB $CC] + $X12) <<< 9}]
        set CC [expr {($CC + [F $DD $AA $BB] + $X2)  <<< 12}]
        set BB [expr {($BB + [F $CC $DD $AA] + $X13) <<< 9}]
        # [ABCD  9  12]  [DABC 7  5]  [CDAB  10 15]  [BCDA 14 8]
        set AA [expr {($AA + [F $BB $CC $DD] + $X9)  <<< 12}]
        set DD [expr {($DD + [F $AA $BB $CC] + $X7)  <<< 5}]
        set CC [expr {($CC + [F $DD $AA $BB] + $X10) <<< 15}]
        set BB [expr {($BB + [F $CC $DD $AA] + $X14) <<< 8}]

        # Then perform the following additions to combine the results.
        set DD       [expr {$state(B) + $C + $DD}]
        set state(B) [expr {$state(C) + $D + $AA}]
        set state(C) [expr {$state(D) + $A + $BB}]
        set state(D) [expr {$state(A) + $B + $CC}]
        set state(A) $DD
    }

    return
}

proc ::ripemd::ripemd128::byte {n v} {expr {((0xFF << (8 * $n)) & $v) >> (8 * $n)}}
proc ::ripemd::ripemd128::bytes {v} { 
    #format %c%c%c%c [byte 0 $v] [byte 1 $v] [byte 2 $v] [byte 3 $v]
    format %c%c%c%c \
        [expr {0xFF & $v}] \
        [expr {(0xFF00 & $v) >> 8}] \
        [expr {(0xFF0000 & $v) >> 16}] \
        [expr {((0xFF000000 & $v) >> 24) & 0xFF}]
}

# 32bit rotate-left
proc ::ripemd::ripemd128::<<< {v n} {
    return [expr {((($v << $n) \
                        | (($v >> (32 - $n)) \
                               & (0x7FFFFFFF >> (31 - $n))))) \
                      & 0xFFFFFFFF}]
}

# Convert our <<< pseudo-operator into a procedure call.
regsub -all -line \
    {\[expr {(.*) <<< (\d+)}\]} \
    $::ripemd::ripemd128::RIPEMD128Hash_body \
    {[<<< [expr {\1}] \2]} \
    ::ripemd::ripemd128::RIPEMD128Hash_body

#  F(x,y,z) = x ^ y ^ z
proc ::ripemd::ripemd128::F {X Y Z} {
    return [expr {$X ^ $Y ^ $Z}]
}
# Inline the F function F
regsub -all -line \
    {\[F (\$[ABCD]{1,2}) (\$[ABCD]{1,2}) (\$[ABCD]{1,2})\]} \
    $::ripemd::ripemd128::RIPEMD128Hash_body \
    {(\1 ^ \2 ^ \3)} \
    ::ripemd::ripemd128::RIPEMD128Hash_body
    
# G(x,y,z) = (x & y) | (~x & z)
proc ::ripemd::ripemd128::G {X Y Z} {
    return [expr {($X & $Y) | (~$X & $Z)}]
}
# Inline the G function
regsub -all -line \
    {\[G (\$[ABCD]{1,2}) (\$[ABCD]{1,2}) (\$[ABCD]{1,2})\]} \
    $::ripemd::ripemd128::RIPEMD128Hash_body \
    {((\1 \& \2) | (~\1 \& \3))} \
    ::ripemd::ripemd128::RIPEMD128Hash_body

# H(x,y,z) = (x | ~y) ^ z
proc ::ripemd::ripemd128::H {X Y Z} {
    return [expr {($X | ~$Y) ^ $Z}]
}
# Inline the H function
regsub -all -line \
    {\[H (\$[ABCD]{1,2}) (\$[ABCD]{1,2}) (\$[ABCD]{1,2})\]} \
    $::ripemd::ripemd128::RIPEMD128Hash_body \
    {( (\1 | ~\2) ^ \3)} \
    ::ripemd::ripemd128::RIPEMD128Hash_body

# I(x,y,z) = (x & z) | (y & ~z)
proc ::ripemd::ripemd128::I {X Y Z} {
    return [expr {($X & $Z) | ($Y & ~$Z)}]
}
# Inline the I function
regsub -all -line \
    {\[I (\$[ABCD]{1,2}) (\$[ABCD]{1,2}) (\$[ABCD]{1,2})\]} \
    $::ripemd::ripemd128::RIPEMD128Hash_body \
    {( (\1 \& \3) | (\2 \& ~\3) )} \
    ::ripemd::ripemd128::RIPEMD128Hash_body

# Define the MD4 hashing procedure with inline functions.
proc ::ripemd::ripemd128::RIPEMD128Hash {token msg} \
    $::ripemd::ripemd128::RIPEMD128Hash_body

unset ::ripemd::ripemd128::RIPEMD128Hash_body

# -------------------------------------------------------------------------

proc ::ripemd::ripemd128::Hex {data} {
    binary scan $data H* result
    return $result
}

# -------------------------------------------------------------------------

# LoadAccelerator --
#
#	This package can make use of a number of compiled extensions to
#	accelerate the digest computation. This procedure manages the
#	use of these extensions within the package. During normal usage
#	this should not be called, but the test package manipulates the
#	list of enabled accelerators.
#
proc ::ripemd::ripemd128::LoadAccelerator {name} {
    variable accel
    set r 0
    switch -exact -- $name {
        #critcl {
        #    if {![catch {package require tcllibc}]
        #        || ![catch {package require sha1c}]} {
        #        set r [expr {[info commands ::sha1::sha1c] != {}}]
        #    }
        #}
        #cryptkit {
        #    if {![catch {package require cryptkit}]} {
        #        set r [expr {![catch {cryptkit::cryptInit}]}]
        #    }
        #}
        trf {
            if {![catch {package require Trf}]} {
                set r [expr {![catch {::ripemd128 aa} msg]}]
            }
        }
        default {
            return -code error "invalid accelerator package:\
                must be one of [join [array names accel] {, }]"
        }
    }
    set accel($name) $r
}

# -------------------------------------------------------------------------

# Description:
#  Pop the nth element off a list. Used in options processing.
#
proc ::ripemd::ripemd128::Pop {varname {nth 0}} {
    upvar $varname args
    set r [lindex $args $nth]
    set args [lreplace $args $nth $nth]
    return $r
}

# -------------------------------------------------------------------------

# fileevent handler for chunked file hashing.
#
proc ::ripemd::ripemd128::Chunk {token channel {chunksize 4096}} {
    # FRINK: nocheck
    variable $token
    upvar 0 $token state
    
    if {[eof $channel]} {
        fileevent $channel readable {}
        set state(reading) 0
    }
        
    RIPEMD128Update $token [read $channel $chunksize]
}

# -------------------------------------------------------------------------

proc ::ripemd::ripemd128::ripemd128 {args} {
    array set opts {-hex 0 -filename {} -channel {} -chunksize 4096}
    while {[string match -* [set option [lindex $args 0]]]} {
        switch -glob -- $option {
            -hex       { set opts(-hex) 1 }
            -file*     { set opts(-filename) [Pop args 1] }
            -channel   { set opts(-channel) [Pop args 1] }
            -chunksize { set opts(-chunksize) [Pop args 1] }
            default {
                if {[llength $args] == 1} { break }
                if {[string compare $option "--"] == 0} { Pop args; break }
                set err [join [lsort [array names opts]] ", "]
                return -code error "bad option $option:\
                    must be one of $err"
            }
        }
        Pop args
    }
    
    if {$opts(-filename) != {}} {
        set opts(-channel) [open $opts(-filename) r]
        fconfigure $opts(-channel) -translation binary
    }

    if {$opts(-channel) == {}} {

        if {[llength $args] != 1} {
            return -code error "wrong # args:\
                should be \"ripemd128 ?-hex? -filename file | string\""
        }
        set tok [RIPEMD128Init]
        RIPEMD128Update $tok [lindex $args 0]
        set r [RIPEMD128Final $tok]

    } else {

        set tok [RIPEMD128Init]
        # FRINK: nocheck
        set [subst $tok](reading) 1
        fileevent $opts(-channel) readable \
            [list [namespace origin Chunk] \
                 $tok $opts(-channel) $opts(-chunksize)]
        vwait [subst $tok](reading)
        set r [RIPEMD128Final $tok]

        # If we opened the channel - we should close it too.
        if {$opts(-filename) != {}} {
            close $opts(-channel)
        }
    }
    
    if {$opts(-hex)} {
        set r [Hex $r]
    }
    return $r
}

# -------------------------------------------------------------------------

proc ::ripemd::ripemd128::hmac128 {args} {
    array set opts {-hex 0 -filename {} -channel {} -chunksize 4096}
    while {[string match -* [set option [lindex $args 0]]]} {
        switch -glob -- $option {
            -key       { set opts(-key) [Pop args 1] }
            -hex       { set opts(-hex) 1 }
            -file*     { set opts(-filename) [Pop args 1] }
            -channel   { set opts(-channel) [Pop args 1] }
            -chunksize { set opts(-chunksize) [Pop args 1] }
            default {
                if {[llength $args] == 1} { break }
                if {[string compare $option "--"] == 0} { Pop args; break }
                set err [join [lsort [array names opts]] ", "]
                return -code error "bad option $option:\
                    must be one of $err"
            }
        }
        Pop args
    }

    if {![info exists opts(-key)]} {
        return -code error "wrong # args:\
            should be \"hmac128 ?-hex? -key key -filename file | string\""
    }

    if {$opts(-filename) != {}} {
        set opts(-channel) [open $opts(-filename) r]
        fconfigure $opts(-channel) -translation binary
    }

    if {$opts(-channel) == {}} {

        if {[llength $args] != 1} {
            return -code error "wrong # args:\
                should be \"hmac128 ?-hex? -key key -filename file | string\""
        }
        set tok [RIPEHMAC128Init $opts(-key)]
        RIPEHMAC128Update $tok [lindex $args 0]
        set r [RIPEHMAC128Final $tok]

    } else {

        set tok [RIPEHMAC128Init $opts(-key)]
        # FRINK: nocheck
        set [subst $tok](reading) 1
        fileevent $opts(-channel) readable \
            [list [namespace origin Chunk] \
                 $tok $opts(-channel) $opts(-chunksize)]
        vwait [subst $tok](reading)
        set r [RIPEHMAC128Final $tok]

        # If we opened the channel - we should close it too.
        if {$opts(-filename) != {}} {
            close $opts(-channel)
        }
    }
    
    if {$opts(-hex)} {
        set r [Hex $r]
    }
    return $r
}

# -------------------------------------------------------------------------

namespace eval ::ripemd {

    namespace import -force [namespace current]::ripemd128::*

    namespace export ripemd128 hmac128 \
        RIPEMD128Init RIPEMD128Update RIPEMD128Final \
        RIPEHMAC128Init RIPEHMAC128Update RIPEHMAC128Final
}

# -------------------------------------------------------------------------

# Try and load a compiled extension to help.
namespace eval ::ripemd::ripemd128 {
    variable e {}
    foreach e {trf} {
        if {[LoadAccelerator $e]} break
    }
    unset e
}

package provide ripemd128 1.0.5

# -------------------------------------------------------------------------
# Local Variables:
#   mode: tcl
#   indent-tabs-mode: nil
# End: