155714Skris/* crypto/mdc2/mdc2dgst.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <stdlib.h>
6155714Skris#include <string.h>
6255714Skris#include <openssl/des.h>
6355714Skris#include <openssl/mdc2.h>
64194206Ssimon#include <openssl/err.h>
65194206Ssimon#ifdef OPENSSL_FIPS
66296465Sdelphij# include <openssl/fips.h>
67194206Ssimon#endif
6855714Skris
6955714Skris#undef c2l
70296465Sdelphij#define c2l(c,l)        (l =((DES_LONG)(*((c)++)))    , \
71296465Sdelphij                         l|=((DES_LONG)(*((c)++)))<< 8L, \
72296465Sdelphij                         l|=((DES_LONG)(*((c)++)))<<16L, \
73296465Sdelphij                         l|=((DES_LONG)(*((c)++)))<<24L)
7455714Skris
7555714Skris#undef l2c
76296465Sdelphij#define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
77296465Sdelphij                        *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
78296465Sdelphij                        *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
79296465Sdelphij                        *((c)++)=(unsigned char)(((l)>>24L)&0xff))
8055714Skris
81160814Ssimonstatic void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len);
82194206SsimonFIPS_NON_FIPS_MD_Init(MDC2)
83296465Sdelphij{
84296465Sdelphij    c->num = 0;
85296465Sdelphij    c->pad_type = 1;
86296465Sdelphij    memset(&(c->h[0]), 0x52, MDC2_BLOCK);
87296465Sdelphij    memset(&(c->hh[0]), 0x25, MDC2_BLOCK);
88296465Sdelphij    return 1;
89296465Sdelphij}
9055714Skris
91160814Ssimonint MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len)
92296465Sdelphij{
93296465Sdelphij    size_t i, j;
9455714Skris
95296465Sdelphij    i = c->num;
96296465Sdelphij    if (i != 0) {
97306230Sdelphij        if (len < MDC2_BLOCK - i) {
98296465Sdelphij            /* partial block */
99296465Sdelphij            memcpy(&(c->data[i]), in, len);
100296465Sdelphij            c->num += (int)len;
101296465Sdelphij            return 1;
102296465Sdelphij        } else {
103296465Sdelphij            /* filled one */
104296465Sdelphij            j = MDC2_BLOCK - i;
105296465Sdelphij            memcpy(&(c->data[i]), in, j);
106296465Sdelphij            len -= j;
107296465Sdelphij            in += j;
108296465Sdelphij            c->num = 0;
109296465Sdelphij            mdc2_body(c, &(c->data[0]), MDC2_BLOCK);
110296465Sdelphij        }
111296465Sdelphij    }
112296465Sdelphij    i = len & ~((size_t)MDC2_BLOCK - 1);
113296465Sdelphij    if (i > 0)
114296465Sdelphij        mdc2_body(c, in, i);
115296465Sdelphij    j = len - i;
116296465Sdelphij    if (j > 0) {
117296465Sdelphij        memcpy(&(c->data[0]), &(in[i]), j);
118296465Sdelphij        c->num = (int)j;
119296465Sdelphij    }
120296465Sdelphij    return 1;
121296465Sdelphij}
12255714Skris
123160814Ssimonstatic void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len)
124296465Sdelphij{
125296465Sdelphij    register DES_LONG tin0, tin1;
126296465Sdelphij    register DES_LONG ttin0, ttin1;
127296465Sdelphij    DES_LONG d[2], dd[2];
128296465Sdelphij    DES_key_schedule k;
129296465Sdelphij    unsigned char *p;
130296465Sdelphij    size_t i;
13155714Skris
132296465Sdelphij    for (i = 0; i < len; i += 8) {
133296465Sdelphij        c2l(in, tin0);
134296465Sdelphij        d[0] = dd[0] = tin0;
135296465Sdelphij        c2l(in, tin1);
136296465Sdelphij        d[1] = dd[1] = tin1;
137296465Sdelphij        c->h[0] = (c->h[0] & 0x9f) | 0x40;
138296465Sdelphij        c->hh[0] = (c->hh[0] & 0x9f) | 0x20;
13955714Skris
140296465Sdelphij        DES_set_odd_parity(&c->h);
141296465Sdelphij        DES_set_key_unchecked(&c->h, &k);
142296465Sdelphij        DES_encrypt1(d, &k, 1);
14355714Skris
144296465Sdelphij        DES_set_odd_parity(&c->hh);
145296465Sdelphij        DES_set_key_unchecked(&c->hh, &k);
146296465Sdelphij        DES_encrypt1(dd, &k, 1);
14755714Skris
148296465Sdelphij        ttin0 = tin0 ^ dd[0];
149296465Sdelphij        ttin1 = tin1 ^ dd[1];
150296465Sdelphij        tin0 ^= d[0];
151296465Sdelphij        tin1 ^= d[1];
15255714Skris
153296465Sdelphij        p = c->h;
154296465Sdelphij        l2c(tin0, p);
155296465Sdelphij        l2c(ttin1, p);
156296465Sdelphij        p = c->hh;
157296465Sdelphij        l2c(ttin0, p);
158296465Sdelphij        l2c(tin1, p);
159296465Sdelphij    }
160296465Sdelphij}
16155714Skris
162109998Smarkmint MDC2_Final(unsigned char *md, MDC2_CTX *c)
163296465Sdelphij{
164296465Sdelphij    unsigned int i;
165296465Sdelphij    int j;
16655714Skris
167296465Sdelphij    i = c->num;
168296465Sdelphij    j = c->pad_type;
169296465Sdelphij    if ((i > 0) || (j == 2)) {
170296465Sdelphij        if (j == 2)
171296465Sdelphij            c->data[i++] = 0x80;
172296465Sdelphij        memset(&(c->data[i]), 0, MDC2_BLOCK - i);
173296465Sdelphij        mdc2_body(c, c->data, MDC2_BLOCK);
174296465Sdelphij    }
175296465Sdelphij    memcpy(md, (char *)c->h, MDC2_BLOCK);
176296465Sdelphij    memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK);
177296465Sdelphij    return 1;
178296465Sdelphij}
17955714Skris
18055714Skris#undef TEST
18155714Skris
18255714Skris#ifdef TEST
18355714Skrismain()
184296465Sdelphij{
185296465Sdelphij    unsigned char md[MDC2_DIGEST_LENGTH];
186296465Sdelphij    int i;
187296465Sdelphij    MDC2_CTX c;
188296465Sdelphij    static char *text = "Now is the time for all ";
18955714Skris
190296465Sdelphij    MDC2_Init(&c);
191296465Sdelphij    MDC2_Update(&c, text, strlen(text));
192296465Sdelphij    MDC2_Final(&(md[0]), &c);
19355714Skris
194296465Sdelphij    for (i = 0; i < MDC2_DIGEST_LENGTH; i++)
195296465Sdelphij        printf("%02X", md[i]);
196296465Sdelphij    printf("\n");
197296465Sdelphij}
19855714Skris
19955714Skris#endif
200