1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/md2/md2_dgst.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,276 @@
1.4 +/* crypto/md2/md2_dgst.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +
1.62 +/*
1.63 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.64 + */
1.65 +
1.66 +#include <stdio.h>
1.67 +#include <stdlib.h>
1.68 +#include <string.h>
1.69 +#include <openssl/md2.h>
1.70 +#include <openssl/opensslv.h>
1.71 +#include <openssl/crypto.h>
1.72 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.73 +#include "libcrypto_wsd_macros.h"
1.74 +#include "libcrypto_wsd.h"
1.75 +#endif
1.76 +
1.77 +
1.78 +const char MD2_version[]="MD2" OPENSSL_VERSION_PTEXT;
1.79 +
1.80 +/* Implemented from RFC1319 The MD2 Message-Digest Algorithm
1.81 + */
1.82 +
1.83 +#define UCHAR unsigned char
1.84 +
1.85 +static void md2_block(MD2_CTX *c, const unsigned char *d);
1.86 +
1.87 +
1.88 +#ifndef EMULATOR
1.89 +/* The magic S table - I have converted it to hex since it is
1.90 + * basically just a random byte string. */
1.91 +static MD2_INT S[256]={
1.92 + 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01,
1.93 + 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13,
1.94 + 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C,
1.95 + 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA,
1.96 + 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
1.97 + 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12,
1.98 + 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49,
1.99 + 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A,
1.100 + 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F,
1.101 + 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
1.102 + 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27,
1.103 + 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03,
1.104 + 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1,
1.105 + 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6,
1.106 + 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
1.107 + 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1,
1.108 + 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20,
1.109 + 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02,
1.110 + 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6,
1.111 + 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
1.112 + 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A,
1.113 + 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26,
1.114 + 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09,
1.115 + 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52,
1.116 + 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
1.117 + 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A,
1.118 + 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D,
1.119 + 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39,
1.120 + 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4,
1.121 + 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
1.122 + 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A,
1.123 + 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14,
1.124 + };
1.125 +#else
1.126 +static const MD2_INT S[256]={
1.127 + 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01,
1.128 + 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13,
1.129 + 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C,
1.130 + 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA,
1.131 + 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
1.132 + 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12,
1.133 + 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49,
1.134 + 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A,
1.135 + 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F,
1.136 + 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
1.137 + 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27,
1.138 + 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03,
1.139 + 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1,
1.140 + 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6,
1.141 + 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
1.142 + 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1,
1.143 + 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20,
1.144 + 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02,
1.145 + 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6,
1.146 + 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
1.147 + 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A,
1.148 + 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26,
1.149 + 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09,
1.150 + 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52,
1.151 + 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
1.152 + 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A,
1.153 + 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D,
1.154 + 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39,
1.155 + 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4,
1.156 + 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
1.157 + 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A,
1.158 + 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14,
1.159 + };
1.160 +
1.161 +
1.162 +#endif
1.163 +EXPORT_C const char *MD2_options(void)
1.164 + {
1.165 + if (sizeof(MD2_INT) == 1)
1.166 + return("md2(char)");
1.167 + else
1.168 + return("md2(int)");
1.169 + }
1.170 +
1.171 +EXPORT_C int MD2_Init(MD2_CTX *c)
1.172 + {
1.173 + c->num=0;
1.174 + memset(c->state,0,sizeof c->state);
1.175 + memset(c->cksm,0,sizeof c->cksm);
1.176 + memset(c->data,0,sizeof c->data);
1.177 + return 1;
1.178 + }
1.179 +
1.180 +EXPORT_C int MD2_Update(MD2_CTX *c, const unsigned char *data, size_t len)
1.181 + {
1.182 + register UCHAR *p;
1.183 +
1.184 + if (len == 0) return 1;
1.185 +
1.186 + p=c->data;
1.187 + if (c->num != 0)
1.188 + {
1.189 + if ((c->num+len) >= MD2_BLOCK)
1.190 + {
1.191 + memcpy(&(p[c->num]),data,MD2_BLOCK-c->num);
1.192 + md2_block(c,c->data);
1.193 + data+=(MD2_BLOCK - c->num);
1.194 + len-=(MD2_BLOCK - c->num);
1.195 + c->num=0;
1.196 + /* drop through and do the rest */
1.197 + }
1.198 + else
1.199 + {
1.200 + memcpy(&(p[c->num]),data,len);
1.201 + /* data+=len; */
1.202 + c->num+=(int)len;
1.203 + return 1;
1.204 + }
1.205 + }
1.206 + /* we now can process the input data in blocks of MD2_BLOCK
1.207 + * chars and save the leftovers to c->data. */
1.208 + while (len >= MD2_BLOCK)
1.209 + {
1.210 + md2_block(c,data);
1.211 + data+=MD2_BLOCK;
1.212 + len-=MD2_BLOCK;
1.213 + }
1.214 + memcpy(p,data,len);
1.215 + c->num=(int)len;
1.216 + return 1;
1.217 + }
1.218 +
1.219 +static void md2_block(MD2_CTX *c, const unsigned char *d)
1.220 + {
1.221 + register MD2_INT t,*sp1,*sp2;
1.222 + register int i,j;
1.223 + MD2_INT state[48];
1.224 +
1.225 + sp1=c->state;
1.226 + sp2=c->cksm;
1.227 + j=sp2[MD2_BLOCK-1];
1.228 + for (i=0; i<16; i++)
1.229 + {
1.230 + state[i]=sp1[i];
1.231 + state[i+16]=t=d[i];
1.232 + state[i+32]=(t^sp1[i]);
1.233 + j=sp2[i]^=S[t^j];
1.234 + }
1.235 + t=0;
1.236 + for (i=0; i<18; i++)
1.237 + {
1.238 + for (j=0; j<48; j+=8)
1.239 + {
1.240 + t= state[j+ 0]^=S[t];
1.241 + t= state[j+ 1]^=S[t];
1.242 + t= state[j+ 2]^=S[t];
1.243 + t= state[j+ 3]^=S[t];
1.244 + t= state[j+ 4]^=S[t];
1.245 + t= state[j+ 5]^=S[t];
1.246 + t= state[j+ 6]^=S[t];
1.247 + t= state[j+ 7]^=S[t];
1.248 + }
1.249 + t=(t+i)&0xff;
1.250 + }
1.251 + memcpy(sp1,state,16*sizeof(MD2_INT));
1.252 + OPENSSL_cleanse(state,48*sizeof(MD2_INT));
1.253 + }
1.254 +
1.255 +EXPORT_C int MD2_Final(unsigned char *md, MD2_CTX *c)
1.256 + {
1.257 + int i,v;
1.258 + register UCHAR *cp;
1.259 + register MD2_INT *p1,*p2;
1.260 +
1.261 + cp=c->data;
1.262 + p1=c->state;
1.263 + p2=c->cksm;
1.264 + v=MD2_BLOCK-c->num;
1.265 + for (i=c->num; i<MD2_BLOCK; i++)
1.266 + cp[i]=(UCHAR)v;
1.267 +
1.268 + md2_block(c,cp);
1.269 +
1.270 + for (i=0; i<MD2_BLOCK; i++)
1.271 + cp[i]=(UCHAR)p2[i];
1.272 + md2_block(c,cp);
1.273 +
1.274 + for (i=0; i<16; i++)
1.275 + md[i]=(UCHAR)(p1[i]&0xff);
1.276 + memset((char *)&c,0,sizeof(c));
1.277 + return 1;
1.278 + }
1.279 +