LCOV - code coverage report
Current view: top level - common/utils - rmd160.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 191 193 99.0 %
Date: 2024-11-12 21:42:17 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /********************************************************************\
       2             :  *
       3             :  *      FILE:     rmd160.c
       4             :  *
       5             :  *      CONTENTS: A sample C-implementation of the RIPEMD-160
       6             :  *                hash-function.
       7             :  *      TARGET:   any computer with an ANSI C compiler
       8             :  *
       9             :  *      AUTHOR:   Antoon Bosselaers, ESAT-COSIC
      10             :  *      DATE:     1 March 1996
      11             :  *      VERSION:  1.0
      12             :  *
      13             :  *      Copyright (c) 1996 Katholieke Universiteit Leuven
      14             :  *
      15             :  *      Permission is hereby granted, free of charge, to any person
      16             :  *      obtaining a copy of this software and associated documentation
      17             :  *      files (the "Software"), to deal in the Software without restriction,
      18             :  *      including without limitation the rights to use, copy, modify, merge,
      19             :  *      publish, distribute, sublicense, and/or sell copies of the Software,
      20             :  *      and to permit persons to whom the Software is furnished to do so,
      21             :  *      subject to the following conditions:
      22             :  *
      23             :  *      The above copyright notice and this permission notice shall be
      24             :  *      included in all copies or substantial portions of the Software.
      25             :  *
      26             :  *      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      27             :  *      EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      28             :  *      MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      29             :  *      IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      30             :  *      CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      31             :  *      TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      32             :  *      SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      33             :  *
      34             : \********************************************************************/
      35             : 
      36             : /*  header files */
      37             : #include "monetdb_config.h"
      38             : #include "rmd160.h"
      39             : 
      40             : /********************************************************************/
      41             : 
      42       38291 : void MDinit(dword *MDbuf)
      43             : {
      44       38291 :    MDbuf[0] = 0x67452301UL;
      45       38291 :    MDbuf[1] = 0xefcdab89UL;
      46       38291 :    MDbuf[2] = 0x98badcfeUL;
      47       38291 :    MDbuf[3] = 0x10325476UL;
      48       38291 :    MDbuf[4] = 0xc3d2e1f0UL;
      49             : 
      50       38291 :    return;
      51             : }
      52             : 
      53             : /********************************************************************/
      54             : 
      55      114870 : void MDcompress(dword *MDbuf, dword *X)
      56             : {
      57      114870 :    dword aa = MDbuf[0],  bb = MDbuf[1],  cc = MDbuf[2],
      58      114870 :          dd = MDbuf[3],  ee = MDbuf[4];
      59      114870 :    dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
      60      114870 :          ddd = MDbuf[3], eee = MDbuf[4];
      61             : 
      62             :    /* round 1 */
      63      114870 :    FF(aa, bb, cc, dd, ee, X[ 0], 11);
      64      114870 :    FF(ee, aa, bb, cc, dd, X[ 1], 14);
      65      114870 :    FF(dd, ee, aa, bb, cc, X[ 2], 15);
      66      114870 :    FF(cc, dd, ee, aa, bb, X[ 3], 12);
      67      114870 :    FF(bb, cc, dd, ee, aa, X[ 4],  5);
      68      114870 :    FF(aa, bb, cc, dd, ee, X[ 5],  8);
      69      114870 :    FF(ee, aa, bb, cc, dd, X[ 6],  7);
      70      114870 :    FF(dd, ee, aa, bb, cc, X[ 7],  9);
      71      114870 :    FF(cc, dd, ee, aa, bb, X[ 8], 11);
      72      114870 :    FF(bb, cc, dd, ee, aa, X[ 9], 13);
      73      114870 :    FF(aa, bb, cc, dd, ee, X[10], 14);
      74      114870 :    FF(ee, aa, bb, cc, dd, X[11], 15);
      75      114870 :    FF(dd, ee, aa, bb, cc, X[12],  6);
      76      114870 :    FF(cc, dd, ee, aa, bb, X[13],  7);
      77      114870 :    FF(bb, cc, dd, ee, aa, X[14],  9);
      78      114870 :    FF(aa, bb, cc, dd, ee, X[15],  8);
      79             : 
      80             :    /* round 2 */
      81      114870 :    GG(ee, aa, bb, cc, dd, X[ 7],  7);
      82      114870 :    GG(dd, ee, aa, bb, cc, X[ 4],  6);
      83      114870 :    GG(cc, dd, ee, aa, bb, X[13],  8);
      84      114870 :    GG(bb, cc, dd, ee, aa, X[ 1], 13);
      85      114870 :    GG(aa, bb, cc, dd, ee, X[10], 11);
      86      114870 :    GG(ee, aa, bb, cc, dd, X[ 6],  9);
      87      114870 :    GG(dd, ee, aa, bb, cc, X[15],  7);
      88      114870 :    GG(cc, dd, ee, aa, bb, X[ 3], 15);
      89      114870 :    GG(bb, cc, dd, ee, aa, X[12],  7);
      90      114870 :    GG(aa, bb, cc, dd, ee, X[ 0], 12);
      91      114870 :    GG(ee, aa, bb, cc, dd, X[ 9], 15);
      92      114870 :    GG(dd, ee, aa, bb, cc, X[ 5],  9);
      93      114870 :    GG(cc, dd, ee, aa, bb, X[ 2], 11);
      94      114870 :    GG(bb, cc, dd, ee, aa, X[14],  7);
      95      114870 :    GG(aa, bb, cc, dd, ee, X[11], 13);
      96      114870 :    GG(ee, aa, bb, cc, dd, X[ 8], 12);
      97             : 
      98             :    /* round 3 */
      99      114870 :    HH(dd, ee, aa, bb, cc, X[ 3], 11);
     100      114870 :    HH(cc, dd, ee, aa, bb, X[10], 13);
     101      114870 :    HH(bb, cc, dd, ee, aa, X[14],  6);
     102      114870 :    HH(aa, bb, cc, dd, ee, X[ 4],  7);
     103      114870 :    HH(ee, aa, bb, cc, dd, X[ 9], 14);
     104      114870 :    HH(dd, ee, aa, bb, cc, X[15],  9);
     105      114870 :    HH(cc, dd, ee, aa, bb, X[ 8], 13);
     106      114870 :    HH(bb, cc, dd, ee, aa, X[ 1], 15);
     107      114870 :    HH(aa, bb, cc, dd, ee, X[ 2], 14);
     108      114870 :    HH(ee, aa, bb, cc, dd, X[ 7],  8);
     109      114870 :    HH(dd, ee, aa, bb, cc, X[ 0], 13);
     110      114870 :    HH(cc, dd, ee, aa, bb, X[ 6],  6);
     111      114870 :    HH(bb, cc, dd, ee, aa, X[13],  5);
     112      114870 :    HH(aa, bb, cc, dd, ee, X[11], 12);
     113      114870 :    HH(ee, aa, bb, cc, dd, X[ 5],  7);
     114      114870 :    HH(dd, ee, aa, bb, cc, X[12],  5);
     115             : 
     116             :    /* round 4 */
     117      114870 :    II(cc, dd, ee, aa, bb, X[ 1], 11);
     118      114870 :    II(bb, cc, dd, ee, aa, X[ 9], 12);
     119      114870 :    II(aa, bb, cc, dd, ee, X[11], 14);
     120      114870 :    II(ee, aa, bb, cc, dd, X[10], 15);
     121      114870 :    II(dd, ee, aa, bb, cc, X[ 0], 14);
     122      114870 :    II(cc, dd, ee, aa, bb, X[ 8], 15);
     123      114870 :    II(bb, cc, dd, ee, aa, X[12],  9);
     124      114870 :    II(aa, bb, cc, dd, ee, X[ 4],  8);
     125      114870 :    II(ee, aa, bb, cc, dd, X[13],  9);
     126      114870 :    II(dd, ee, aa, bb, cc, X[ 3], 14);
     127      114870 :    II(cc, dd, ee, aa, bb, X[ 7],  5);
     128      114870 :    II(bb, cc, dd, ee, aa, X[15],  6);
     129      114870 :    II(aa, bb, cc, dd, ee, X[14],  8);
     130      114870 :    II(ee, aa, bb, cc, dd, X[ 5],  6);
     131      114870 :    II(dd, ee, aa, bb, cc, X[ 6],  5);
     132      114870 :    II(cc, dd, ee, aa, bb, X[ 2], 12);
     133             : 
     134             :    /* round 5 */
     135      114870 :    JJ(bb, cc, dd, ee, aa, X[ 4],  9);
     136      114870 :    JJ(aa, bb, cc, dd, ee, X[ 0], 15);
     137      114870 :    JJ(ee, aa, bb, cc, dd, X[ 5],  5);
     138      114870 :    JJ(dd, ee, aa, bb, cc, X[ 9], 11);
     139      114870 :    JJ(cc, dd, ee, aa, bb, X[ 7],  6);
     140      114870 :    JJ(bb, cc, dd, ee, aa, X[12],  8);
     141      114870 :    JJ(aa, bb, cc, dd, ee, X[ 2], 13);
     142      114870 :    JJ(ee, aa, bb, cc, dd, X[10], 12);
     143      114870 :    JJ(dd, ee, aa, bb, cc, X[14],  5);
     144      114870 :    JJ(cc, dd, ee, aa, bb, X[ 1], 12);
     145      114870 :    JJ(bb, cc, dd, ee, aa, X[ 3], 13);
     146      114870 :    JJ(aa, bb, cc, dd, ee, X[ 8], 14);
     147      114870 :    JJ(ee, aa, bb, cc, dd, X[11], 11);
     148      114870 :    JJ(dd, ee, aa, bb, cc, X[ 6],  8);
     149      114870 :    JJ(cc, dd, ee, aa, bb, X[15],  5);
     150      114870 :    JJ(bb, cc, dd, ee, aa, X[13],  6);
     151             : 
     152             :    /* parallel round 1 */
     153      114870 :    JJJ(aaa, bbb, ccc, ddd, eee, X[ 5],  8);
     154      114870 :    JJJ(eee, aaa, bbb, ccc, ddd, X[14],  9);
     155      114870 :    JJJ(ddd, eee, aaa, bbb, ccc, X[ 7],  9);
     156      114870 :    JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
     157      114870 :    JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
     158      114870 :    JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
     159      114870 :    JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
     160      114870 :    JJJ(ddd, eee, aaa, bbb, ccc, X[ 4],  5);
     161      114870 :    JJJ(ccc, ddd, eee, aaa, bbb, X[13],  7);
     162      114870 :    JJJ(bbb, ccc, ddd, eee, aaa, X[ 6],  7);
     163      114870 :    JJJ(aaa, bbb, ccc, ddd, eee, X[15],  8);
     164      114870 :    JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
     165      114870 :    JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
     166      114870 :    JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
     167      114870 :    JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
     168      114870 :    JJJ(aaa, bbb, ccc, ddd, eee, X[12],  6);
     169             : 
     170             :    /* parallel round 2 */
     171      114870 :    III(eee, aaa, bbb, ccc, ddd, X[ 6],  9);
     172      114870 :    III(ddd, eee, aaa, bbb, ccc, X[11], 13);
     173      114870 :    III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
     174      114870 :    III(bbb, ccc, ddd, eee, aaa, X[ 7],  7);
     175      114870 :    III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
     176      114870 :    III(eee, aaa, bbb, ccc, ddd, X[13],  8);
     177      114870 :    III(ddd, eee, aaa, bbb, ccc, X[ 5],  9);
     178      114870 :    III(ccc, ddd, eee, aaa, bbb, X[10], 11);
     179      114870 :    III(bbb, ccc, ddd, eee, aaa, X[14],  7);
     180      114870 :    III(aaa, bbb, ccc, ddd, eee, X[15],  7);
     181      114870 :    III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
     182      114870 :    III(ddd, eee, aaa, bbb, ccc, X[12],  7);
     183      114870 :    III(ccc, ddd, eee, aaa, bbb, X[ 4],  6);
     184      114870 :    III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
     185      114870 :    III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
     186      114870 :    III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
     187             : 
     188             :    /* parallel round 3 */
     189      114870 :    HHH(ddd, eee, aaa, bbb, ccc, X[15],  9);
     190      114870 :    HHH(ccc, ddd, eee, aaa, bbb, X[ 5],  7);
     191      114870 :    HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
     192      114870 :    HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
     193      114870 :    HHH(eee, aaa, bbb, ccc, ddd, X[ 7],  8);
     194      114870 :    HHH(ddd, eee, aaa, bbb, ccc, X[14],  6);
     195      114870 :    HHH(ccc, ddd, eee, aaa, bbb, X[ 6],  6);
     196      114870 :    HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
     197      114870 :    HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
     198      114870 :    HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
     199      114870 :    HHH(ddd, eee, aaa, bbb, ccc, X[12],  5);
     200      114870 :    HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
     201      114870 :    HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
     202      114870 :    HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
     203      114870 :    HHH(eee, aaa, bbb, ccc, ddd, X[ 4],  7);
     204      114870 :    HHH(ddd, eee, aaa, bbb, ccc, X[13],  5);
     205             : 
     206             :    /* parallel round 4 */
     207      114870 :    GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
     208      114870 :    GGG(bbb, ccc, ddd, eee, aaa, X[ 6],  5);
     209      114870 :    GGG(aaa, bbb, ccc, ddd, eee, X[ 4],  8);
     210      114870 :    GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
     211      114870 :    GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
     212      114870 :    GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
     213      114870 :    GGG(bbb, ccc, ddd, eee, aaa, X[15],  6);
     214      114870 :    GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
     215      114870 :    GGG(eee, aaa, bbb, ccc, ddd, X[ 5],  6);
     216      114870 :    GGG(ddd, eee, aaa, bbb, ccc, X[12],  9);
     217      114870 :    GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
     218      114870 :    GGG(bbb, ccc, ddd, eee, aaa, X[13],  9);
     219      114870 :    GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
     220      114870 :    GGG(eee, aaa, bbb, ccc, ddd, X[ 7],  5);
     221      114870 :    GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
     222      114870 :    GGG(ccc, ddd, eee, aaa, bbb, X[14],  8);
     223             : 
     224             :    /* parallel round 5 */
     225      114870 :    FFF(bbb, ccc, ddd, eee, aaa, X[12] ,  8);
     226      114870 :    FFF(aaa, bbb, ccc, ddd, eee, X[15] ,  5);
     227      114870 :    FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
     228      114870 :    FFF(ddd, eee, aaa, bbb, ccc, X[ 4] ,  9);
     229      114870 :    FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
     230      114870 :    FFF(bbb, ccc, ddd, eee, aaa, X[ 5] ,  5);
     231      114870 :    FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
     232      114870 :    FFF(eee, aaa, bbb, ccc, ddd, X[ 7] ,  6);
     233      114870 :    FFF(ddd, eee, aaa, bbb, ccc, X[ 6] ,  8);
     234      114870 :    FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
     235      114870 :    FFF(bbb, ccc, ddd, eee, aaa, X[13] ,  6);
     236      114870 :    FFF(aaa, bbb, ccc, ddd, eee, X[14] ,  5);
     237      114870 :    FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
     238      114870 :    FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
     239      114870 :    FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
     240      114870 :    FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
     241             : 
     242             :    /* combine results */
     243      114870 :    ddd += cc + MDbuf[1];               /* final result for MDbuf[0] */
     244      114870 :    MDbuf[1] = MDbuf[2] + dd + eee;
     245      114870 :    MDbuf[2] = MDbuf[3] + ee + aaa;
     246      114870 :    MDbuf[3] = MDbuf[4] + aa + bbb;
     247      114870 :    MDbuf[4] = MDbuf[0] + bb + ccc;
     248      114870 :    MDbuf[0] = ddd;
     249             : 
     250      114870 :    return;
     251             : }
     252             : 
     253             : /********************************************************************/
     254             : 
     255       38291 : void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen)
     256             : {
     257       38291 :    unsigned int i;                                 /* counter       */
     258       38291 :    dword        X[16];                             /* message words */
     259             : 
     260       38291 :    memset(X, 0, 16*sizeof(dword));
     261             : 
     262             :    /* put bytes from strptr into X */
     263      402335 :    for (i=0; i<(lswlen&63); i++) {
     264             :       /* byte i goes into word X[i div 4] at pos.  8*(i mod 4)  */
     265      364044 :       X[i>>2] ^= (dword) *strptr++ << (8 * (i&3));
     266             :    }
     267             : 
     268             :    /* append the bit m_n == 1 */
     269       38291 :    X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7);
     270             : 
     271       38291 :    if ((lswlen & 63) > 55) {
     272             :       /* length goes to next block */
     273           0 :       MDcompress(MDbuf, X);
     274           0 :       memset(X, 0, 16*sizeof(dword));
     275             :    }
     276             : 
     277             :    /* append length in bits*/
     278       38291 :    X[14] = lswlen << 3;
     279       38291 :    X[15] = (lswlen >> 29) | (mswlen << 3);
     280       38291 :    MDcompress(MDbuf, X);
     281             : 
     282       38291 :    return;
     283             : }
     284             : 
     285             : /************************ end of file rmd160.c **********************/

Generated by: LCOV version 1.14