Home | History | Annotate | Download | only in cups
      1 /*
      2  * Private MD5 implementation for CUPS.
      3  *
      4  * Copyright 2007-2014 by Apple Inc.
      5  * Copyright 2005 by Easy Software Products
      6  * Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
      7  *
      8  * This software is provided 'as-is', without any express or implied
      9  * warranty.  In no event will the authors be held liable for any damages
     10  * arising from the use of this software.
     11  *
     12  * Permission is granted to anyone to use this software for any purpose,
     13  * including commercial applications, and to alter it and redistribute it
     14  * freely, subject to the following restrictions:
     15  *
     16  * 1. The origin of this software must not be misrepresented; you must not
     17  *    claim that you wrote the original software. If you use this software
     18  *    in a product, an acknowledgment in the product documentation would be
     19  *    appreciated but is not required.
     20  * 2. Altered source versions must be plainly marked as such, and must not be
     21  *    misrepresented as being the original software.
     22  * 3. This notice may not be removed or altered from any source distribution.
     23  *
     24  * L. Peter Deutsch
     25  * ghost (at) aladdin.com
     26  */
     27 /*
     28   Independent implementation of MD5 (RFC 1321).
     29 
     30   This code implements the MD5 Algorithm defined in RFC 1321.
     31   It is derived directly from the text of the RFC and not from the
     32   reference implementation.
     33 
     34   The original and principal author of md5.c is L. Peter Deutsch
     35   <ghost (at) aladdin.com>.  Other authors are noted in the change history
     36   that follows (in reverse chronological order):
     37 
     38   1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
     39   1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
     40   1999-05-03 lpd Original version.
     41  */
     42 
     43 #include "md5-private.h"
     44 #include "string-private.h"
     45 
     46 #define T1 0xd76aa478
     47 #define T2 0xe8c7b756
     48 #define T3 0x242070db
     49 #define T4 0xc1bdceee
     50 #define T5 0xf57c0faf
     51 #define T6 0x4787c62a
     52 #define T7 0xa8304613
     53 #define T8 0xfd469501
     54 #define T9 0x698098d8
     55 #define T10 0x8b44f7af
     56 #define T11 0xffff5bb1
     57 #define T12 0x895cd7be
     58 #define T13 0x6b901122
     59 #define T14 0xfd987193
     60 #define T15 0xa679438e
     61 #define T16 0x49b40821
     62 #define T17 0xf61e2562
     63 #define T18 0xc040b340
     64 #define T19 0x265e5a51
     65 #define T20 0xe9b6c7aa
     66 #define T21 0xd62f105d
     67 #define T22 0x02441453
     68 #define T23 0xd8a1e681
     69 #define T24 0xe7d3fbc8
     70 #define T25 0x21e1cde6
     71 #define T26 0xc33707d6
     72 #define T27 0xf4d50d87
     73 #define T28 0x455a14ed
     74 #define T29 0xa9e3e905
     75 #define T30 0xfcefa3f8
     76 #define T31 0x676f02d9
     77 #define T32 0x8d2a4c8a
     78 #define T33 0xfffa3942
     79 #define T34 0x8771f681
     80 #define T35 0x6d9d6122
     81 #define T36 0xfde5380c
     82 #define T37 0xa4beea44
     83 #define T38 0x4bdecfa9
     84 #define T39 0xf6bb4b60
     85 #define T40 0xbebfbc70
     86 #define T41 0x289b7ec6
     87 #define T42 0xeaa127fa
     88 #define T43 0xd4ef3085
     89 #define T44 0x04881d05
     90 #define T45 0xd9d4d039
     91 #define T46 0xe6db99e5
     92 #define T47 0x1fa27cf8
     93 #define T48 0xc4ac5665
     94 #define T49 0xf4292244
     95 #define T50 0x432aff97
     96 #define T51 0xab9423a7
     97 #define T52 0xfc93a039
     98 #define T53 0x655b59c3
     99 #define T54 0x8f0ccc92
    100 #define T55 0xffeff47d
    101 #define T56 0x85845dd1
    102 #define T57 0x6fa87e4f
    103 #define T58 0xfe2ce6e0
    104 #define T59 0xa3014314
    105 #define T60 0x4e0811a1
    106 #define T61 0xf7537e82
    107 #define T62 0xbd3af235
    108 #define T63 0x2ad7d2bb
    109 #define T64 0xeb86d391
    110 
    111 static void
    112 _cups_md5_process(_cups_md5_state_t *pms, const unsigned char *data /*[64]*/)
    113 {
    114     unsigned int
    115 	a = pms->abcd[0], b = pms->abcd[1],
    116 	c = pms->abcd[2], d = pms->abcd[3];
    117     unsigned int t;
    118 
    119 #ifndef ARCH_IS_BIG_ENDIAN
    120 # define ARCH_IS_BIG_ENDIAN 1	/* slower, default implementation */
    121 #endif
    122 #if ARCH_IS_BIG_ENDIAN
    123 
    124     /*
    125      * On big-endian machines, we must arrange the bytes in the right
    126      * order.  (This also works on machines of unknown byte order.)
    127      */
    128     unsigned int X[16];
    129     const unsigned char *xp = data;
    130     int i;
    131 
    132     for (i = 0; i < 16; ++i, xp += 4)
    133 	X[i] = (unsigned)xp[0] + ((unsigned)xp[1] << 8) +
    134 	       ((unsigned)xp[2] << 16) + ((unsigned)xp[3] << 24);
    135 
    136 #else  /* !ARCH_IS_BIG_ENDIAN */
    137 
    138     /*
    139      * On little-endian machines, we can process properly aligned data
    140      * without copying it.
    141      */
    142     unsigned int xbuf[16];
    143     const unsigned int *X;
    144 
    145     if (!((data - (const unsigned char *)0) & 3)) {
    146 	/* data are properly aligned */
    147 	X = (const unsigned int *)data;
    148     } else {
    149 	/* not aligned */
    150 	memcpy(xbuf, data, 64);
    151 	X = xbuf;
    152     }
    153 #endif
    154 
    155 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
    156 
    157     /* Round 1. */
    158     /* Let [abcd k s i] denote the operation
    159        a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
    160 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
    161 #define SET(a, b, c, d, k, s, Ti)\
    162   t = a + F(b,c,d) + X[k] + Ti;\
    163   a = ROTATE_LEFT(t, s) + b
    164     /* Do the following 16 operations. */
    165     SET(a, b, c, d,  0,  7,  T1);
    166     SET(d, a, b, c,  1, 12,  T2);
    167     SET(c, d, a, b,  2, 17,  T3);
    168     SET(b, c, d, a,  3, 22,  T4);
    169     SET(a, b, c, d,  4,  7,  T5);
    170     SET(d, a, b, c,  5, 12,  T6);
    171     SET(c, d, a, b,  6, 17,  T7);
    172     SET(b, c, d, a,  7, 22,  T8);
    173     SET(a, b, c, d,  8,  7,  T9);
    174     SET(d, a, b, c,  9, 12, T10);
    175     SET(c, d, a, b, 10, 17, T11);
    176     SET(b, c, d, a, 11, 22, T12);
    177     SET(a, b, c, d, 12,  7, T13);
    178     SET(d, a, b, c, 13, 12, T14);
    179     SET(c, d, a, b, 14, 17, T15);
    180     SET(b, c, d, a, 15, 22, T16);
    181 #undef SET
    182 
    183      /* Round 2. */
    184      /* Let [abcd k s i] denote the operation
    185           a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
    186 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
    187 #define SET(a, b, c, d, k, s, Ti)\
    188   t = a + G(b,c,d) + X[k] + Ti;\
    189   a = ROTATE_LEFT(t, s) + b
    190      /* Do the following 16 operations. */
    191     SET(a, b, c, d,  1,  5, T17);
    192     SET(d, a, b, c,  6,  9, T18);
    193     SET(c, d, a, b, 11, 14, T19);
    194     SET(b, c, d, a,  0, 20, T20);
    195     SET(a, b, c, d,  5,  5, T21);
    196     SET(d, a, b, c, 10,  9, T22);
    197     SET(c, d, a, b, 15, 14, T23);
    198     SET(b, c, d, a,  4, 20, T24);
    199     SET(a, b, c, d,  9,  5, T25);
    200     SET(d, a, b, c, 14,  9, T26);
    201     SET(c, d, a, b,  3, 14, T27);
    202     SET(b, c, d, a,  8, 20, T28);
    203     SET(a, b, c, d, 13,  5, T29);
    204     SET(d, a, b, c,  2,  9, T30);
    205     SET(c, d, a, b,  7, 14, T31);
    206     SET(b, c, d, a, 12, 20, T32);
    207 #undef SET
    208 
    209      /* Round 3. */
    210      /* Let [abcd k s t] denote the operation
    211           a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
    212 #define H(x, y, z) ((x) ^ (y) ^ (z))
    213 #define SET(a, b, c, d, k, s, Ti)\
    214   t = a + H(b,c,d) + X[k] + Ti;\
    215   a = ROTATE_LEFT(t, s) + b
    216      /* Do the following 16 operations. */
    217     SET(a, b, c, d,  5,  4, T33);
    218     SET(d, a, b, c,  8, 11, T34);
    219     SET(c, d, a, b, 11, 16, T35);
    220     SET(b, c, d, a, 14, 23, T36);
    221     SET(a, b, c, d,  1,  4, T37);
    222     SET(d, a, b, c,  4, 11, T38);
    223     SET(c, d, a, b,  7, 16, T39);
    224     SET(b, c, d, a, 10, 23, T40);
    225     SET(a, b, c, d, 13,  4, T41);
    226     SET(d, a, b, c,  0, 11, T42);
    227     SET(c, d, a, b,  3, 16, T43);
    228     SET(b, c, d, a,  6, 23, T44);
    229     SET(a, b, c, d,  9,  4, T45);
    230     SET(d, a, b, c, 12, 11, T46);
    231     SET(c, d, a, b, 15, 16, T47);
    232     SET(b, c, d, a,  2, 23, T48);
    233 #undef SET
    234 
    235      /* Round 4. */
    236      /* Let [abcd k s t] denote the operation
    237           a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
    238 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
    239 #define SET(a, b, c, d, k, s, Ti)\
    240   t = a + I(b,c,d) + X[k] + Ti;\
    241   a = ROTATE_LEFT(t, s) + b
    242      /* Do the following 16 operations. */
    243     SET(a, b, c, d,  0,  6, T49);
    244     SET(d, a, b, c,  7, 10, T50);
    245     SET(c, d, a, b, 14, 15, T51);
    246     SET(b, c, d, a,  5, 21, T52);
    247     SET(a, b, c, d, 12,  6, T53);
    248     SET(d, a, b, c,  3, 10, T54);
    249     SET(c, d, a, b, 10, 15, T55);
    250     SET(b, c, d, a,  1, 21, T56);
    251     SET(a, b, c, d,  8,  6, T57);
    252     SET(d, a, b, c, 15, 10, T58);
    253     SET(c, d, a, b,  6, 15, T59);
    254     SET(b, c, d, a, 13, 21, T60);
    255     SET(a, b, c, d,  4,  6, T61);
    256     SET(d, a, b, c, 11, 10, T62);
    257     SET(c, d, a, b,  2, 15, T63);
    258     SET(b, c, d, a,  9, 21, T64);
    259 #undef SET
    260 
    261      /* Then perform the following additions. (That is increment each
    262         of the four registers by the value it had before this block
    263         was started.) */
    264     pms->abcd[0] += a;
    265     pms->abcd[1] += b;
    266     pms->abcd[2] += c;
    267     pms->abcd[3] += d;
    268 }
    269 
    270 void
    271 _cupsMD5Init(_cups_md5_state_t *pms)
    272 {
    273     pms->count[0] = pms->count[1] = 0;
    274     pms->abcd[0] = 0x67452301;
    275     pms->abcd[1] = 0xefcdab89;
    276     pms->abcd[2] = 0x98badcfe;
    277     pms->abcd[3] = 0x10325476;
    278 }
    279 
    280 void
    281 _cupsMD5Append(_cups_md5_state_t *pms, const unsigned char *data, int nbytes)
    282 {
    283     const unsigned char *p = data;
    284     int left = nbytes;
    285     int offset = (pms->count[0] >> 3) & 63;
    286     unsigned int nbits = (unsigned int)(nbytes << 3);
    287 
    288     if (nbytes <= 0)
    289 	return;
    290 
    291     /* Update the message length. */
    292     pms->count[1] += (unsigned)nbytes >> 29;
    293     pms->count[0] += nbits;
    294     if (pms->count[0] < nbits)
    295 	pms->count[1]++;
    296 
    297     /* Process an initial partial block. */
    298     if (offset) {
    299 	int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
    300 
    301 	memcpy(pms->buf + offset, p, (size_t)copy);
    302 	if (offset + copy < 64)
    303 	    return;
    304 	p += copy;
    305 	left -= copy;
    306 	_cups_md5_process(pms, pms->buf);
    307     }
    308 
    309     /* Process full blocks. */
    310     for (; left >= 64; p += 64, left -= 64)
    311 	_cups_md5_process(pms, p);
    312 
    313     /* Process a final partial block. */
    314     if (left)
    315 	memcpy(pms->buf, p, (size_t)left);
    316 }
    317 
    318 void
    319 _cupsMD5Finish(_cups_md5_state_t *pms, unsigned char digest[16])
    320 {
    321     static const unsigned char pad[64] = {
    322 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    323 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    324 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    325 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    326     };
    327     unsigned char data[8];
    328     int i;
    329 
    330     /* Save the length before padding. */
    331     for (i = 0; i < 8; ++i)
    332 	data[i] = (unsigned char)(pms->count[i >> 2] >> ((i & 3) << 3));
    333     /* Pad to 56 bytes mod 64. */
    334     _cupsMD5Append(pms, pad, (int)((55 - (pms->count[0] >> 3)) & 63) + 1);
    335     /* Append the length. */
    336     _cupsMD5Append(pms, data, 8);
    337     for (i = 0; i < 16; ++i)
    338 	digest[i] = (unsigned char)(pms->abcd[i >> 2] >> ((i & 3) << 3));
    339 }
    340