The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/crypto/ripemd160/rmd160.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: rmd160.c,v 1.3 2003/08/26 20:15:13 thorpej Exp $       */
    2 /*      $KAME: rmd160.c,v 1.2 2003/07/25 09:37:55 itojun Exp $  */
    3 /*      $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $       */
    4 /*
    5  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   26  */
   27 /*
   28  * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160",
   29  * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997,
   30  * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __KERNEL_RCSID(0, "$NetBSD: rmd160.c,v 1.3 2003/08/26 20:15:13 thorpej Exp $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/endian.h>
   39 #include <crypto/ripemd160/rmd160.h>
   40 
   41 #define PUT_64BIT_LE(cp, value) do { \
   42         (cp)[7] = (value) >> 56; \
   43         (cp)[6] = (value) >> 48; \
   44         (cp)[5] = (value) >> 40; \
   45         (cp)[4] = (value) >> 32; \
   46         (cp)[3] = (value) >> 24; \
   47         (cp)[2] = (value) >> 16; \
   48         (cp)[1] = (value) >> 8; \
   49         (cp)[0] = (value); } while (0)
   50 
   51 #define PUT_32BIT_LE(cp, value) do { \
   52         (cp)[3] = (value) >> 24; \
   53         (cp)[2] = (value) >> 16; \
   54         (cp)[1] = (value) >> 8; \
   55         (cp)[0] = (value); } while (0)
   56 
   57 #define H0      0x67452301U
   58 #define H1      0xEFCDAB89U
   59 #define H2      0x98BADCFEU
   60 #define H3      0x10325476U
   61 #define H4      0xC3D2E1F0U
   62 
   63 #define K0      0x00000000U
   64 #define K1      0x5A827999U
   65 #define K2      0x6ED9EBA1U
   66 #define K3      0x8F1BBCDCU
   67 #define K4      0xA953FD4EU
   68 
   69 #define KK0     0x50A28BE6U
   70 #define KK1     0x5C4DD124U
   71 #define KK2     0x6D703EF3U
   72 #define KK3     0x7A6D76E9U
   73 #define KK4     0x00000000U
   74 
   75 /* rotate x left n bits.  */
   76 #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n))))
   77 
   78 #define F0(x, y, z) ((x) ^ (y) ^ (z))
   79 #define F1(x, y, z) (((x) & (y)) | ((~x) & (z)))
   80 #define F2(x, y, z) (((x) | (~y)) ^ (z))
   81 #define F3(x, y, z) (((x) & (z)) | ((y) & (~z)))
   82 #define F4(x, y, z) ((x) ^ ((y) | (~z)))
   83 
   84 #define R(a, b, c, d, e, Fj, Kj, sj, rj) \
   85         do { \
   86                 a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \
   87                 c = ROL(10, c); \
   88         } while(0)
   89 
   90 #define X(i)    x[i]
   91 
   92 static const u_char PADDING[64] = {
   93         0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   94         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   95         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   96 };
   97 
   98 void
   99 RMD160Init(RMD160_CTX *ctx)
  100 {
  101         ctx->count = 0;
  102         ctx->state[0] = H0;
  103         ctx->state[1] = H1;
  104         ctx->state[2] = H2;
  105         ctx->state[3] = H3;
  106         ctx->state[4] = H4;
  107 }
  108 
  109 void
  110 RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
  111 {
  112         u_int32_t have, off, need;
  113 
  114         have = (ctx->count/8) % 64;
  115         need = 64 - have;
  116         ctx->count += 8 * len;
  117         off = 0;
  118 
  119         if (len >= need) {
  120                 if (have) {
  121                         memcpy(ctx->buffer + have, input, need);
  122                         RMD160Transform(ctx->state, ctx->buffer);
  123                         off = need;
  124                         have = 0;
  125                 }
  126                 /* now the buffer is empty */
  127                 while (off + 64 <= len) {
  128                         RMD160Transform(ctx->state, input+off);
  129                         off += 64;
  130                 }
  131         }
  132         if (off < len)
  133                 memcpy(ctx->buffer + have, input+off, len-off);
  134 }
  135 
  136 void
  137 RMD160Final(u_char digest[20], RMD160_CTX *ctx)
  138 {
  139         int i;
  140         u_char size[8];
  141         u_int32_t padlen;
  142 
  143         PUT_64BIT_LE(size, ctx->count);
  144 
  145         /*
  146          * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes
  147          * for the size
  148          */
  149         padlen = 64 - ((ctx->count/8) % 64);
  150         if (padlen < 1 + 8)
  151                 padlen += 64;
  152         RMD160Update(ctx, PADDING, padlen - 8);         /* padlen - 8 <= 64 */
  153         RMD160Update(ctx, size, 8);
  154 
  155         if (digest != NULL)
  156                 for (i = 0; i < 5; i++)
  157                         PUT_32BIT_LE(digest + i*4, ctx->state[i]);
  158 
  159         memset(ctx, 0, sizeof (*ctx));
  160 }
  161 
  162 void
  163 RMD160Transform(u_int32_t state[5], const u_char block[64])
  164 {
  165         u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
  166 
  167 #if BYTE_ORDER == LITTLE_ENDIAN
  168         memcpy(x, block, 64);
  169 #else
  170         int i;
  171 
  172         for (i = 0; i < 16; i++)
  173                 x[i] = le32toh(*(u_int32_t*)(block+i*4));
  174 #endif
  175 
  176         a = state[0];
  177         b = state[1];
  178         c = state[2];
  179         d = state[3];
  180         e = state[4];
  181 
  182         /* Round 1 */
  183         R(a, b, c, d, e, F0, K0, 11,  0);
  184         R(e, a, b, c, d, F0, K0, 14,  1);
  185         R(d, e, a, b, c, F0, K0, 15,  2);
  186         R(c, d, e, a, b, F0, K0, 12,  3);
  187         R(b, c, d, e, a, F0, K0,  5,  4);
  188         R(a, b, c, d, e, F0, K0,  8,  5);
  189         R(e, a, b, c, d, F0, K0,  7,  6);
  190         R(d, e, a, b, c, F0, K0,  9,  7);
  191         R(c, d, e, a, b, F0, K0, 11,  8);
  192         R(b, c, d, e, a, F0, K0, 13,  9);
  193         R(a, b, c, d, e, F0, K0, 14, 10);
  194         R(e, a, b, c, d, F0, K0, 15, 11);
  195         R(d, e, a, b, c, F0, K0,  6, 12);
  196         R(c, d, e, a, b, F0, K0,  7, 13);
  197         R(b, c, d, e, a, F0, K0,  9, 14);
  198         R(a, b, c, d, e, F0, K0,  8, 15); /* #15 */
  199         /* Round 2 */
  200         R(e, a, b, c, d, F1, K1,  7,  7);
  201         R(d, e, a, b, c, F1, K1,  6,  4);
  202         R(c, d, e, a, b, F1, K1,  8, 13);
  203         R(b, c, d, e, a, F1, K1, 13,  1);
  204         R(a, b, c, d, e, F1, K1, 11, 10);
  205         R(e, a, b, c, d, F1, K1,  9,  6);
  206         R(d, e, a, b, c, F1, K1,  7, 15);
  207         R(c, d, e, a, b, F1, K1, 15,  3);
  208         R(b, c, d, e, a, F1, K1,  7, 12);
  209         R(a, b, c, d, e, F1, K1, 12,  0);
  210         R(e, a, b, c, d, F1, K1, 15,  9);
  211         R(d, e, a, b, c, F1, K1,  9,  5);
  212         R(c, d, e, a, b, F1, K1, 11,  2);
  213         R(b, c, d, e, a, F1, K1,  7, 14);
  214         R(a, b, c, d, e, F1, K1, 13, 11);
  215         R(e, a, b, c, d, F1, K1, 12,  8); /* #31 */
  216         /* Round 3 */
  217         R(d, e, a, b, c, F2, K2, 11,  3);
  218         R(c, d, e, a, b, F2, K2, 13, 10);
  219         R(b, c, d, e, a, F2, K2,  6, 14);
  220         R(a, b, c, d, e, F2, K2,  7,  4);
  221         R(e, a, b, c, d, F2, K2, 14,  9);
  222         R(d, e, a, b, c, F2, K2,  9, 15);
  223         R(c, d, e, a, b, F2, K2, 13,  8);
  224         R(b, c, d, e, a, F2, K2, 15,  1);
  225         R(a, b, c, d, e, F2, K2, 14,  2);
  226         R(e, a, b, c, d, F2, K2,  8,  7);
  227         R(d, e, a, b, c, F2, K2, 13,  0);
  228         R(c, d, e, a, b, F2, K2,  6,  6);
  229         R(b, c, d, e, a, F2, K2,  5, 13);
  230         R(a, b, c, d, e, F2, K2, 12, 11);
  231         R(e, a, b, c, d, F2, K2,  7,  5);
  232         R(d, e, a, b, c, F2, K2,  5, 12); /* #47 */
  233         /* Round 4 */
  234         R(c, d, e, a, b, F3, K3, 11,  1);
  235         R(b, c, d, e, a, F3, K3, 12,  9);
  236         R(a, b, c, d, e, F3, K3, 14, 11);
  237         R(e, a, b, c, d, F3, K3, 15, 10);
  238         R(d, e, a, b, c, F3, K3, 14,  0);
  239         R(c, d, e, a, b, F3, K3, 15,  8);
  240         R(b, c, d, e, a, F3, K3,  9, 12);
  241         R(a, b, c, d, e, F3, K3,  8,  4);
  242         R(e, a, b, c, d, F3, K3,  9, 13);
  243         R(d, e, a, b, c, F3, K3, 14,  3);
  244         R(c, d, e, a, b, F3, K3,  5,  7);
  245         R(b, c, d, e, a, F3, K3,  6, 15);
  246         R(a, b, c, d, e, F3, K3,  8, 14);
  247         R(e, a, b, c, d, F3, K3,  6,  5);
  248         R(d, e, a, b, c, F3, K3,  5,  6);
  249         R(c, d, e, a, b, F3, K3, 12,  2); /* #63 */
  250         /* Round 5 */
  251         R(b, c, d, e, a, F4, K4,  9,  4);
  252         R(a, b, c, d, e, F4, K4, 15,  0);
  253         R(e, a, b, c, d, F4, K4,  5,  5);
  254         R(d, e, a, b, c, F4, K4, 11,  9);
  255         R(c, d, e, a, b, F4, K4,  6,  7);
  256         R(b, c, d, e, a, F4, K4,  8, 12);
  257         R(a, b, c, d, e, F4, K4, 13,  2);
  258         R(e, a, b, c, d, F4, K4, 12, 10);
  259         R(d, e, a, b, c, F4, K4,  5, 14);
  260         R(c, d, e, a, b, F4, K4, 12,  1);
  261         R(b, c, d, e, a, F4, K4, 13,  3);
  262         R(a, b, c, d, e, F4, K4, 14,  8);
  263         R(e, a, b, c, d, F4, K4, 11, 11);
  264         R(d, e, a, b, c, F4, K4,  8,  6);
  265         R(c, d, e, a, b, F4, K4,  5, 15);
  266         R(b, c, d, e, a, F4, K4,  6, 13); /* #79 */
  267 
  268         aa = a ; bb = b; cc = c; dd = d; ee = e;
  269 
  270         a = state[0];
  271         b = state[1];
  272         c = state[2];
  273         d = state[3];
  274         e = state[4];
  275 
  276         /* Parallel round 1 */
  277         R(a, b, c, d, e, F4, KK0,  8,  5);
  278         R(e, a, b, c, d, F4, KK0,  9, 14);
  279         R(d, e, a, b, c, F4, KK0,  9,  7);
  280         R(c, d, e, a, b, F4, KK0, 11,  0);
  281         R(b, c, d, e, a, F4, KK0, 13,  9);
  282         R(a, b, c, d, e, F4, KK0, 15,  2);
  283         R(e, a, b, c, d, F4, KK0, 15, 11);
  284         R(d, e, a, b, c, F4, KK0,  5,  4);
  285         R(c, d, e, a, b, F4, KK0,  7, 13);
  286         R(b, c, d, e, a, F4, KK0,  7,  6);
  287         R(a, b, c, d, e, F4, KK0,  8, 15);
  288         R(e, a, b, c, d, F4, KK0, 11,  8);
  289         R(d, e, a, b, c, F4, KK0, 14,  1);
  290         R(c, d, e, a, b, F4, KK0, 14, 10);
  291         R(b, c, d, e, a, F4, KK0, 12,  3);
  292         R(a, b, c, d, e, F4, KK0,  6, 12); /* #15 */
  293         /* Parallel round 2 */
  294         R(e, a, b, c, d, F3, KK1,  9,  6);
  295         R(d, e, a, b, c, F3, KK1, 13, 11);
  296         R(c, d, e, a, b, F3, KK1, 15,  3);
  297         R(b, c, d, e, a, F3, KK1,  7,  7);
  298         R(a, b, c, d, e, F3, KK1, 12,  0);
  299         R(e, a, b, c, d, F3, KK1,  8, 13);
  300         R(d, e, a, b, c, F3, KK1,  9,  5);
  301         R(c, d, e, a, b, F3, KK1, 11, 10);
  302         R(b, c, d, e, a, F3, KK1,  7, 14);
  303         R(a, b, c, d, e, F3, KK1,  7, 15);
  304         R(e, a, b, c, d, F3, KK1, 12,  8);
  305         R(d, e, a, b, c, F3, KK1,  7, 12);
  306         R(c, d, e, a, b, F3, KK1,  6,  4);
  307         R(b, c, d, e, a, F3, KK1, 15,  9);
  308         R(a, b, c, d, e, F3, KK1, 13,  1);
  309         R(e, a, b, c, d, F3, KK1, 11,  2); /* #31 */
  310         /* Parallel round 3 */
  311         R(d, e, a, b, c, F2, KK2,  9, 15);
  312         R(c, d, e, a, b, F2, KK2,  7,  5);
  313         R(b, c, d, e, a, F2, KK2, 15,  1);
  314         R(a, b, c, d, e, F2, KK2, 11,  3);
  315         R(e, a, b, c, d, F2, KK2,  8,  7);
  316         R(d, e, a, b, c, F2, KK2,  6, 14);
  317         R(c, d, e, a, b, F2, KK2,  6,  6);
  318         R(b, c, d, e, a, F2, KK2, 14,  9);
  319         R(a, b, c, d, e, F2, KK2, 12, 11);
  320         R(e, a, b, c, d, F2, KK2, 13,  8);
  321         R(d, e, a, b, c, F2, KK2,  5, 12);
  322         R(c, d, e, a, b, F2, KK2, 14,  2);
  323         R(b, c, d, e, a, F2, KK2, 13, 10);
  324         R(a, b, c, d, e, F2, KK2, 13,  0);
  325         R(e, a, b, c, d, F2, KK2,  7,  4);
  326         R(d, e, a, b, c, F2, KK2,  5, 13); /* #47 */
  327         /* Parallel round 4 */
  328         R(c, d, e, a, b, F1, KK3, 15,  8);
  329         R(b, c, d, e, a, F1, KK3,  5,  6);
  330         R(a, b, c, d, e, F1, KK3,  8,  4);
  331         R(e, a, b, c, d, F1, KK3, 11,  1);
  332         R(d, e, a, b, c, F1, KK3, 14,  3);
  333         R(c, d, e, a, b, F1, KK3, 14, 11);
  334         R(b, c, d, e, a, F1, KK3,  6, 15);
  335         R(a, b, c, d, e, F1, KK3, 14,  0);
  336         R(e, a, b, c, d, F1, KK3,  6,  5);
  337         R(d, e, a, b, c, F1, KK3,  9, 12);
  338         R(c, d, e, a, b, F1, KK3, 12,  2);
  339         R(b, c, d, e, a, F1, KK3,  9, 13);
  340         R(a, b, c, d, e, F1, KK3, 12,  9);
  341         R(e, a, b, c, d, F1, KK3,  5,  7);
  342         R(d, e, a, b, c, F1, KK3, 15, 10);
  343         R(c, d, e, a, b, F1, KK3,  8, 14); /* #63 */
  344         /* Parallel round 5 */
  345         R(b, c, d, e, a, F0, KK4,  8, 12);
  346         R(a, b, c, d, e, F0, KK4,  5, 15);
  347         R(e, a, b, c, d, F0, KK4, 12, 10);
  348         R(d, e, a, b, c, F0, KK4,  9,  4);
  349         R(c, d, e, a, b, F0, KK4, 12,  1);
  350         R(b, c, d, e, a, F0, KK4,  5,  5);
  351         R(a, b, c, d, e, F0, KK4, 14,  8);
  352         R(e, a, b, c, d, F0, KK4,  6,  7);
  353         R(d, e, a, b, c, F0, KK4,  8,  6);
  354         R(c, d, e, a, b, F0, KK4, 13,  2);
  355         R(b, c, d, e, a, F0, KK4,  6, 13);
  356         R(a, b, c, d, e, F0, KK4,  5, 14);
  357         R(e, a, b, c, d, F0, KK4, 15,  0);
  358         R(d, e, a, b, c, F0, KK4, 13,  3);
  359         R(c, d, e, a, b, F0, KK4, 11,  9);
  360         R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
  361 
  362         t =        state[1] + cc + d;
  363         state[1] = state[2] + dd + e;
  364         state[2] = state[3] + ee + a;
  365         state[3] = state[4] + aa + b;
  366         state[4] = state[0] + bb + c;
  367         state[0] = t;
  368 }

Cache object: c0c5ad593b7fee1a4f39ddf6480951c2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.