]>
Commit | Line | Data |
---|---|---|
d2e9e320 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
d2e9e320 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
a3654f05 | 13 | #include <openssl/crypto.h> |
ec577822 BM |
14 | #include <openssl/des.h> |
15 | #include <openssl/mdc2.h> | |
d02b48c6 RE |
16 | |
17 | #undef c2l | |
0f113f3e MC |
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) | |
d02b48c6 RE |
22 | |
23 | #undef l2c | |
0f113f3e MC |
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)) | |
d02b48c6 | 28 | |
9e0aad9f | 29 | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len); |
2dc769a1 | 30 | int MDC2_Init(MDC2_CTX *c) |
0f113f3e MC |
31 | { |
32 | c->num = 0; | |
33 | c->pad_type = 1; | |
34 | memset(&(c->h[0]), 0x52, MDC2_BLOCK); | |
35 | memset(&(c->hh[0]), 0x25, MDC2_BLOCK); | |
36 | return 1; | |
37 | } | |
d02b48c6 | 38 | |
9e0aad9f | 39 | int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len) |
0f113f3e MC |
40 | { |
41 | size_t i, j; | |
42 | ||
43 | i = c->num; | |
44 | if (i != 0) { | |
45 | if (i + len < MDC2_BLOCK) { | |
46 | /* partial block */ | |
47 | memcpy(&(c->data[i]), in, len); | |
48 | c->num += (int)len; | |
49 | return 1; | |
50 | } else { | |
51 | /* filled one */ | |
52 | j = MDC2_BLOCK - i; | |
53 | memcpy(&(c->data[i]), in, j); | |
54 | len -= j; | |
55 | in += j; | |
56 | c->num = 0; | |
57 | mdc2_body(c, &(c->data[0]), MDC2_BLOCK); | |
58 | } | |
59 | } | |
60 | i = len & ~((size_t)MDC2_BLOCK - 1); | |
61 | if (i > 0) | |
62 | mdc2_body(c, in, i); | |
63 | j = len - i; | |
64 | if (j > 0) { | |
65 | memcpy(&(c->data[0]), &(in[i]), j); | |
66 | c->num = (int)j; | |
67 | } | |
68 | return 1; | |
69 | } | |
d02b48c6 | 70 | |
9e0aad9f | 71 | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len) |
0f113f3e MC |
72 | { |
73 | register DES_LONG tin0, tin1; | |
74 | register DES_LONG ttin0, ttin1; | |
75 | DES_LONG d[2], dd[2]; | |
76 | DES_key_schedule k; | |
77 | unsigned char *p; | |
78 | size_t i; | |
79 | ||
80 | for (i = 0; i < len; i += 8) { | |
81 | c2l(in, tin0); | |
82 | d[0] = dd[0] = tin0; | |
83 | c2l(in, tin1); | |
84 | d[1] = dd[1] = tin1; | |
85 | c->h[0] = (c->h[0] & 0x9f) | 0x40; | |
86 | c->hh[0] = (c->hh[0] & 0x9f) | 0x20; | |
87 | ||
88 | DES_set_odd_parity(&c->h); | |
89 | DES_set_key_unchecked(&c->h, &k); | |
90 | DES_encrypt1(d, &k, 1); | |
91 | ||
92 | DES_set_odd_parity(&c->hh); | |
93 | DES_set_key_unchecked(&c->hh, &k); | |
94 | DES_encrypt1(dd, &k, 1); | |
95 | ||
96 | ttin0 = tin0 ^ dd[0]; | |
97 | ttin1 = tin1 ^ dd[1]; | |
98 | tin0 ^= d[0]; | |
99 | tin1 ^= d[1]; | |
100 | ||
101 | p = c->h; | |
102 | l2c(tin0, p); | |
103 | l2c(ttin1, p); | |
104 | p = c->hh; | |
105 | l2c(ttin0, p); | |
106 | l2c(tin1, p); | |
107 | } | |
108 | } | |
d02b48c6 | 109 | |
2dc769a1 | 110 | int MDC2_Final(unsigned char *md, MDC2_CTX *c) |
0f113f3e MC |
111 | { |
112 | unsigned int i; | |
113 | int j; | |
114 | ||
115 | i = c->num; | |
116 | j = c->pad_type; | |
117 | if ((i > 0) || (j == 2)) { | |
118 | if (j == 2) | |
119 | c->data[i++] = 0x80; | |
120 | memset(&(c->data[i]), 0, MDC2_BLOCK - i); | |
121 | mdc2_body(c, c->data, MDC2_BLOCK); | |
122 | } | |
123 | memcpy(md, (char *)c->h, MDC2_BLOCK); | |
124 | memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK); | |
125 | return 1; | |
126 | } | |
d02b48c6 RE |
127 | |
128 | #undef TEST | |
129 | ||
130 | #ifdef TEST | |
131 | main() | |
0f113f3e MC |
132 | { |
133 | unsigned char md[MDC2_DIGEST_LENGTH]; | |
134 | int i; | |
135 | MDC2_CTX c; | |
136 | static char *text = "Now is the time for all "; | |
137 | ||
138 | MDC2_Init(&c); | |
139 | MDC2_Update(&c, text, strlen(text)); | |
140 | MDC2_Final(&(md[0]), &c); | |
141 | ||
142 | for (i = 0; i < MDC2_DIGEST_LENGTH; i++) | |
143 | printf("%02X", md[i]); | |
144 | printf("\n"); | |
145 | } | |
d02b48c6 RE |
146 | |
147 | #endif |