os/ossrv/ssl/libcrypto/src/crypto/md5/md5_dgst.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/md5/md5_dgst.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include "md5_locl.h"
sl@0
    61
#include <openssl/opensslv.h>
sl@0
    62
sl@0
    63
const char MD5_version[]="MD5" OPENSSL_VERSION_PTEXT;
sl@0
    64
sl@0
    65
/* Implemented from RFC1321 The MD5 Message-Digest Algorithm
sl@0
    66
 */
sl@0
    67
sl@0
    68
#define INIT_DATA_A (unsigned long)0x67452301L
sl@0
    69
#define INIT_DATA_B (unsigned long)0xefcdab89L
sl@0
    70
#define INIT_DATA_C (unsigned long)0x98badcfeL
sl@0
    71
#define INIT_DATA_D (unsigned long)0x10325476L
sl@0
    72
sl@0
    73
EXPORT_C int MD5_Init(MD5_CTX *c)
sl@0
    74
	{
sl@0
    75
	c->A=INIT_DATA_A;
sl@0
    76
	c->B=INIT_DATA_B;
sl@0
    77
	c->C=INIT_DATA_C;
sl@0
    78
	c->D=INIT_DATA_D;
sl@0
    79
	c->Nl=0;
sl@0
    80
	c->Nh=0;
sl@0
    81
	c->num=0;
sl@0
    82
	return 1;
sl@0
    83
	}
sl@0
    84
sl@0
    85
#ifndef md5_block_host_order
sl@0
    86
void md5_block_host_order (MD5_CTX *c, const void *data, size_t num)
sl@0
    87
	{
sl@0
    88
	const MD5_LONG *X=data;
sl@0
    89
	register unsigned MD32_REG_T A,B,C,D;
sl@0
    90
sl@0
    91
	A=c->A;
sl@0
    92
	B=c->B;
sl@0
    93
	C=c->C;
sl@0
    94
	D=c->D;
sl@0
    95
sl@0
    96
	for (;num--;X+=HASH_LBLOCK)
sl@0
    97
		{
sl@0
    98
	/* Round 0 */
sl@0
    99
	R0(A,B,C,D,X[ 0], 7,0xd76aa478L);
sl@0
   100
	R0(D,A,B,C,X[ 1],12,0xe8c7b756L);
sl@0
   101
	R0(C,D,A,B,X[ 2],17,0x242070dbL);
sl@0
   102
	R0(B,C,D,A,X[ 3],22,0xc1bdceeeL);
sl@0
   103
	R0(A,B,C,D,X[ 4], 7,0xf57c0fafL);
sl@0
   104
	R0(D,A,B,C,X[ 5],12,0x4787c62aL);
sl@0
   105
	R0(C,D,A,B,X[ 6],17,0xa8304613L);
sl@0
   106
	R0(B,C,D,A,X[ 7],22,0xfd469501L);
sl@0
   107
	R0(A,B,C,D,X[ 8], 7,0x698098d8L);
sl@0
   108
	R0(D,A,B,C,X[ 9],12,0x8b44f7afL);
sl@0
   109
	R0(C,D,A,B,X[10],17,0xffff5bb1L);
sl@0
   110
	R0(B,C,D,A,X[11],22,0x895cd7beL);
sl@0
   111
	R0(A,B,C,D,X[12], 7,0x6b901122L);
sl@0
   112
	R0(D,A,B,C,X[13],12,0xfd987193L);
sl@0
   113
	R0(C,D,A,B,X[14],17,0xa679438eL);
sl@0
   114
	R0(B,C,D,A,X[15],22,0x49b40821L);
sl@0
   115
	/* Round 1 */
sl@0
   116
	R1(A,B,C,D,X[ 1], 5,0xf61e2562L);
sl@0
   117
	R1(D,A,B,C,X[ 6], 9,0xc040b340L);
sl@0
   118
	R1(C,D,A,B,X[11],14,0x265e5a51L);
sl@0
   119
	R1(B,C,D,A,X[ 0],20,0xe9b6c7aaL);
sl@0
   120
	R1(A,B,C,D,X[ 5], 5,0xd62f105dL);
sl@0
   121
	R1(D,A,B,C,X[10], 9,0x02441453L);
sl@0
   122
	R1(C,D,A,B,X[15],14,0xd8a1e681L);
sl@0
   123
	R1(B,C,D,A,X[ 4],20,0xe7d3fbc8L);
sl@0
   124
	R1(A,B,C,D,X[ 9], 5,0x21e1cde6L);
sl@0
   125
	R1(D,A,B,C,X[14], 9,0xc33707d6L);
sl@0
   126
	R1(C,D,A,B,X[ 3],14,0xf4d50d87L);
sl@0
   127
	R1(B,C,D,A,X[ 8],20,0x455a14edL);
sl@0
   128
	R1(A,B,C,D,X[13], 5,0xa9e3e905L);
sl@0
   129
	R1(D,A,B,C,X[ 2], 9,0xfcefa3f8L);
sl@0
   130
	R1(C,D,A,B,X[ 7],14,0x676f02d9L);
sl@0
   131
	R1(B,C,D,A,X[12],20,0x8d2a4c8aL);
sl@0
   132
	/* Round 2 */
sl@0
   133
	R2(A,B,C,D,X[ 5], 4,0xfffa3942L);
sl@0
   134
	R2(D,A,B,C,X[ 8],11,0x8771f681L);
sl@0
   135
	R2(C,D,A,B,X[11],16,0x6d9d6122L);
sl@0
   136
	R2(B,C,D,A,X[14],23,0xfde5380cL);
sl@0
   137
	R2(A,B,C,D,X[ 1], 4,0xa4beea44L);
sl@0
   138
	R2(D,A,B,C,X[ 4],11,0x4bdecfa9L);
sl@0
   139
	R2(C,D,A,B,X[ 7],16,0xf6bb4b60L);
sl@0
   140
	R2(B,C,D,A,X[10],23,0xbebfbc70L);
sl@0
   141
	R2(A,B,C,D,X[13], 4,0x289b7ec6L);
sl@0
   142
	R2(D,A,B,C,X[ 0],11,0xeaa127faL);
sl@0
   143
	R2(C,D,A,B,X[ 3],16,0xd4ef3085L);
sl@0
   144
	R2(B,C,D,A,X[ 6],23,0x04881d05L);
sl@0
   145
	R2(A,B,C,D,X[ 9], 4,0xd9d4d039L);
sl@0
   146
	R2(D,A,B,C,X[12],11,0xe6db99e5L);
sl@0
   147
	R2(C,D,A,B,X[15],16,0x1fa27cf8L);
sl@0
   148
	R2(B,C,D,A,X[ 2],23,0xc4ac5665L);
sl@0
   149
	/* Round 3 */
sl@0
   150
	R3(A,B,C,D,X[ 0], 6,0xf4292244L);
sl@0
   151
	R3(D,A,B,C,X[ 7],10,0x432aff97L);
sl@0
   152
	R3(C,D,A,B,X[14],15,0xab9423a7L);
sl@0
   153
	R3(B,C,D,A,X[ 5],21,0xfc93a039L);
sl@0
   154
	R3(A,B,C,D,X[12], 6,0x655b59c3L);
sl@0
   155
	R3(D,A,B,C,X[ 3],10,0x8f0ccc92L);
sl@0
   156
	R3(C,D,A,B,X[10],15,0xffeff47dL);
sl@0
   157
	R3(B,C,D,A,X[ 1],21,0x85845dd1L);
sl@0
   158
	R3(A,B,C,D,X[ 8], 6,0x6fa87e4fL);
sl@0
   159
	R3(D,A,B,C,X[15],10,0xfe2ce6e0L);
sl@0
   160
	R3(C,D,A,B,X[ 6],15,0xa3014314L);
sl@0
   161
	R3(B,C,D,A,X[13],21,0x4e0811a1L);
sl@0
   162
	R3(A,B,C,D,X[ 4], 6,0xf7537e82L);
sl@0
   163
	R3(D,A,B,C,X[11],10,0xbd3af235L);
sl@0
   164
	R3(C,D,A,B,X[ 2],15,0x2ad7d2bbL);
sl@0
   165
	R3(B,C,D,A,X[ 9],21,0xeb86d391L);
sl@0
   166
sl@0
   167
	A = c->A += A;
sl@0
   168
	B = c->B += B;
sl@0
   169
	C = c->C += C;
sl@0
   170
	D = c->D += D;
sl@0
   171
		}
sl@0
   172
	}
sl@0
   173
#endif
sl@0
   174
sl@0
   175
#ifndef md5_block_data_order
sl@0
   176
#ifdef X
sl@0
   177
#undef X
sl@0
   178
#endif
sl@0
   179
void md5_block_data_order (MD5_CTX *c, const void *data_, size_t num)
sl@0
   180
	{
sl@0
   181
	const unsigned char *data=data_;
sl@0
   182
	register unsigned MD32_REG_T A,B,C,D,l;
sl@0
   183
#ifndef MD32_XARRAY
sl@0
   184
	/* See comment in crypto/sha/sha_locl.h for details. */
sl@0
   185
	unsigned MD32_REG_T	XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7,
sl@0
   186
				XX8, XX9,XX10,XX11,XX12,XX13,XX14,XX15;
sl@0
   187
# define X(i)	XX##i
sl@0
   188
#else
sl@0
   189
	MD5_LONG XX[MD5_LBLOCK];
sl@0
   190
# define X(i)	XX[i]
sl@0
   191
#endif
sl@0
   192
sl@0
   193
	A=c->A;
sl@0
   194
	B=c->B;
sl@0
   195
	C=c->C;
sl@0
   196
	D=c->D;
sl@0
   197
sl@0
   198
	for (;num--;)
sl@0
   199
		{
sl@0
   200
	HOST_c2l(data,l); X( 0)=l;		HOST_c2l(data,l); X( 1)=l;
sl@0
   201
	/* Round 0 */
sl@0
   202
	R0(A,B,C,D,X( 0), 7,0xd76aa478L);	HOST_c2l(data,l); X( 2)=l;
sl@0
   203
	R0(D,A,B,C,X( 1),12,0xe8c7b756L);	HOST_c2l(data,l); X( 3)=l;
sl@0
   204
	R0(C,D,A,B,X( 2),17,0x242070dbL);	HOST_c2l(data,l); X( 4)=l;
sl@0
   205
	R0(B,C,D,A,X( 3),22,0xc1bdceeeL);	HOST_c2l(data,l); X( 5)=l;
sl@0
   206
	R0(A,B,C,D,X( 4), 7,0xf57c0fafL);	HOST_c2l(data,l); X( 6)=l;
sl@0
   207
	R0(D,A,B,C,X( 5),12,0x4787c62aL);	HOST_c2l(data,l); X( 7)=l;
sl@0
   208
	R0(C,D,A,B,X( 6),17,0xa8304613L);	HOST_c2l(data,l); X( 8)=l;
sl@0
   209
	R0(B,C,D,A,X( 7),22,0xfd469501L);	HOST_c2l(data,l); X( 9)=l;
sl@0
   210
	R0(A,B,C,D,X( 8), 7,0x698098d8L);	HOST_c2l(data,l); X(10)=l;
sl@0
   211
	R0(D,A,B,C,X( 9),12,0x8b44f7afL);	HOST_c2l(data,l); X(11)=l;
sl@0
   212
	R0(C,D,A,B,X(10),17,0xffff5bb1L);	HOST_c2l(data,l); X(12)=l;
sl@0
   213
	R0(B,C,D,A,X(11),22,0x895cd7beL);	HOST_c2l(data,l); X(13)=l;
sl@0
   214
	R0(A,B,C,D,X(12), 7,0x6b901122L);	HOST_c2l(data,l); X(14)=l;
sl@0
   215
	R0(D,A,B,C,X(13),12,0xfd987193L);	HOST_c2l(data,l); X(15)=l;
sl@0
   216
	R0(C,D,A,B,X(14),17,0xa679438eL);
sl@0
   217
	R0(B,C,D,A,X(15),22,0x49b40821L);
sl@0
   218
	/* Round 1 */
sl@0
   219
	R1(A,B,C,D,X( 1), 5,0xf61e2562L);
sl@0
   220
	R1(D,A,B,C,X( 6), 9,0xc040b340L);
sl@0
   221
	R1(C,D,A,B,X(11),14,0x265e5a51L);
sl@0
   222
	R1(B,C,D,A,X( 0),20,0xe9b6c7aaL);
sl@0
   223
	R1(A,B,C,D,X( 5), 5,0xd62f105dL);
sl@0
   224
	R1(D,A,B,C,X(10), 9,0x02441453L);
sl@0
   225
	R1(C,D,A,B,X(15),14,0xd8a1e681L);
sl@0
   226
	R1(B,C,D,A,X( 4),20,0xe7d3fbc8L);
sl@0
   227
	R1(A,B,C,D,X( 9), 5,0x21e1cde6L);
sl@0
   228
	R1(D,A,B,C,X(14), 9,0xc33707d6L);
sl@0
   229
	R1(C,D,A,B,X( 3),14,0xf4d50d87L);
sl@0
   230
	R1(B,C,D,A,X( 8),20,0x455a14edL);
sl@0
   231
	R1(A,B,C,D,X(13), 5,0xa9e3e905L);
sl@0
   232
	R1(D,A,B,C,X( 2), 9,0xfcefa3f8L);
sl@0
   233
	R1(C,D,A,B,X( 7),14,0x676f02d9L);
sl@0
   234
	R1(B,C,D,A,X(12),20,0x8d2a4c8aL);
sl@0
   235
	/* Round 2 */
sl@0
   236
	R2(A,B,C,D,X( 5), 4,0xfffa3942L);
sl@0
   237
	R2(D,A,B,C,X( 8),11,0x8771f681L);
sl@0
   238
	R2(C,D,A,B,X(11),16,0x6d9d6122L);
sl@0
   239
	R2(B,C,D,A,X(14),23,0xfde5380cL);
sl@0
   240
	R2(A,B,C,D,X( 1), 4,0xa4beea44L);
sl@0
   241
	R2(D,A,B,C,X( 4),11,0x4bdecfa9L);
sl@0
   242
	R2(C,D,A,B,X( 7),16,0xf6bb4b60L);
sl@0
   243
	R2(B,C,D,A,X(10),23,0xbebfbc70L);
sl@0
   244
	R2(A,B,C,D,X(13), 4,0x289b7ec6L);
sl@0
   245
	R2(D,A,B,C,X( 0),11,0xeaa127faL);
sl@0
   246
	R2(C,D,A,B,X( 3),16,0xd4ef3085L);
sl@0
   247
	R2(B,C,D,A,X( 6),23,0x04881d05L);
sl@0
   248
	R2(A,B,C,D,X( 9), 4,0xd9d4d039L);
sl@0
   249
	R2(D,A,B,C,X(12),11,0xe6db99e5L);
sl@0
   250
	R2(C,D,A,B,X(15),16,0x1fa27cf8L);
sl@0
   251
	R2(B,C,D,A,X( 2),23,0xc4ac5665L);
sl@0
   252
	/* Round 3 */
sl@0
   253
	R3(A,B,C,D,X( 0), 6,0xf4292244L);
sl@0
   254
	R3(D,A,B,C,X( 7),10,0x432aff97L);
sl@0
   255
	R3(C,D,A,B,X(14),15,0xab9423a7L);
sl@0
   256
	R3(B,C,D,A,X( 5),21,0xfc93a039L);
sl@0
   257
	R3(A,B,C,D,X(12), 6,0x655b59c3L);
sl@0
   258
	R3(D,A,B,C,X( 3),10,0x8f0ccc92L);
sl@0
   259
	R3(C,D,A,B,X(10),15,0xffeff47dL);
sl@0
   260
	R3(B,C,D,A,X( 1),21,0x85845dd1L);
sl@0
   261
	R3(A,B,C,D,X( 8), 6,0x6fa87e4fL);
sl@0
   262
	R3(D,A,B,C,X(15),10,0xfe2ce6e0L);
sl@0
   263
	R3(C,D,A,B,X( 6),15,0xa3014314L);
sl@0
   264
	R3(B,C,D,A,X(13),21,0x4e0811a1L);
sl@0
   265
	R3(A,B,C,D,X( 4), 6,0xf7537e82L);
sl@0
   266
	R3(D,A,B,C,X(11),10,0xbd3af235L);
sl@0
   267
	R3(C,D,A,B,X( 2),15,0x2ad7d2bbL);
sl@0
   268
	R3(B,C,D,A,X( 9),21,0xeb86d391L);
sl@0
   269
sl@0
   270
	A = c->A += A;
sl@0
   271
	B = c->B += B;
sl@0
   272
	C = c->C += C;
sl@0
   273
	D = c->D += D;
sl@0
   274
		}
sl@0
   275
	}
sl@0
   276
#endif
sl@0
   277
sl@0
   278
#ifdef undef
sl@0
   279
int printit(unsigned long *l)
sl@0
   280
	{
sl@0
   281
	int i,ii;
sl@0
   282
sl@0
   283
	for (i=0; i<2; i++)
sl@0
   284
		{
sl@0
   285
		for (ii=0; ii<8; ii++)
sl@0
   286
			{
sl@0
   287
			fprintf(stderr,"%08lx ",l[i*8+ii]);
sl@0
   288
			}
sl@0
   289
		fprintf(stderr,"\n");
sl@0
   290
		}
sl@0
   291
	}
sl@0
   292
#endif