2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * MD2 low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
19 #include <openssl/crypto.h>
20 #include <openssl/des.h>
21 #include <openssl/mdc2.h>
24 #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
25 l|=((DES_LONG)(*((c)++)))<< 8L, \
26 l|=((DES_LONG)(*((c)++)))<<16L, \
27 l|=((DES_LONG)(*((c)++)))<<24L)
30 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
31 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
32 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
33 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
35 static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len);
36 int MDC2_Init(MDC2_CTX *c)
40 memset(&(c->h[0]), 0x52, MDC2_BLOCK);
41 memset(&(c->hh[0]), 0x25, MDC2_BLOCK);
45 int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len)
51 if (len < MDC2_BLOCK - i) {
53 memcpy(&(c->data[i]), in, len);
59 memcpy(&(c->data[i]), in, j);
63 mdc2_body(c, &(c->data[0]), MDC2_BLOCK);
66 i = len & ~((size_t)MDC2_BLOCK - 1);
71 memcpy(&(c->data[0]), &(in[i]), j);
77 static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len)
79 register DES_LONG tin0, tin1;
80 register DES_LONG ttin0, ttin1;
86 for (i = 0; i < len; i += 8) {
91 c->h[0] = (c->h[0] & 0x9f) | 0x40;
92 c->hh[0] = (c->hh[0] & 0x9f) | 0x20;
94 DES_set_odd_parity(&c->h);
95 DES_set_key_unchecked(&c->h, &k);
96 DES_encrypt1(d, &k, 1);
98 DES_set_odd_parity(&c->hh);
99 DES_set_key_unchecked(&c->hh, &k);
100 DES_encrypt1(dd, &k, 1);
102 ttin0 = tin0 ^ dd[0];
103 ttin1 = tin1 ^ dd[1];
116 int MDC2_Final(unsigned char *md, MDC2_CTX *c)
123 if ((i > 0) || (j == 2)) {
126 memset(&(c->data[i]), 0, MDC2_BLOCK - i);
127 mdc2_body(c, c->data, MDC2_BLOCK);
129 memcpy(md, (char *)c->h, MDC2_BLOCK);
130 memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK);