2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/crypto.h>
14 #include <openssl/des.h>
15 #include <openssl/mdc2.h>
18 #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
19 l|=((DES_LONG)(*((c)++)))<< 8L, \
20 l|=((DES_LONG)(*((c)++)))<<16L, \
21 l|=((DES_LONG)(*((c)++)))<<24L)
24 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
25 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
26 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
27 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
29 static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len);
30 int MDC2_Init(MDC2_CTX *c)
34 memset(&(c->h[0]), 0x52, MDC2_BLOCK);
35 memset(&(c->hh[0]), 0x25, MDC2_BLOCK);
39 int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len)
45 if (len < MDC2_BLOCK - i) {
47 memcpy(&(c->data[i]), in, len);
53 memcpy(&(c->data[i]), in, j);
57 mdc2_body(c, &(c->data[0]), MDC2_BLOCK);
60 i = len & ~((size_t)MDC2_BLOCK - 1);
65 memcpy(&(c->data[0]), &(in[i]), j);
71 static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len)
73 register DES_LONG tin0, tin1;
74 register DES_LONG ttin0, ttin1;
80 for (i = 0; i < len; i += 8) {
85 c->h[0] = (c->h[0] & 0x9f) | 0x40;
86 c->hh[0] = (c->hh[0] & 0x9f) | 0x20;
88 DES_set_odd_parity(&c->h);
89 DES_set_key_unchecked(&c->h, &k);
90 DES_encrypt1(d, &k, 1);
92 DES_set_odd_parity(&c->hh);
93 DES_set_key_unchecked(&c->hh, &k);
94 DES_encrypt1(dd, &k, 1);
110 int MDC2_Final(unsigned char *md, MDC2_CTX *c)
117 if ((i > 0) || (j == 2)) {
120 memset(&(c->data[i]), 0, MDC2_BLOCK - i);
121 mdc2_body(c, c->data, MDC2_BLOCK);
123 memcpy(md, (char *)c->h, MDC2_BLOCK);
124 memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK);
133 unsigned char md[MDC2_DIGEST_LENGTH];
136 static char *text = "Now is the time for all ";
139 MDC2_Update(&c, text, strlen(text));
140 MDC2_Final(&(md[0]), &c);
142 for (i = 0; i < MDC2_DIGEST_LENGTH; i++)
143 printf("%02X", md[i]);