/src/openssl/crypto/mdc2/mdc2dgst.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | /* |
11 | | * MD2 low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdio.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/des.h> |
21 | | #include <openssl/mdc2.h> |
22 | | #include "internal/common.h" |
23 | | |
24 | | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len); |
25 | | int MDC2_Init(MDC2_CTX *c) |
26 | 62 | { |
27 | 62 | c->num = 0; |
28 | 62 | c->pad_type = 1; |
29 | 62 | memset(&(c->h[0]), 0x52, MDC2_BLOCK); |
30 | 62 | memset(&(c->hh[0]), 0x25, MDC2_BLOCK); |
31 | 62 | return 1; |
32 | 62 | } |
33 | | |
34 | | int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len) |
35 | 62 | { |
36 | 62 | size_t i, j; |
37 | | |
38 | 62 | i = c->num; |
39 | 62 | if (i != 0) { |
40 | 0 | if (len < MDC2_BLOCK - i) { |
41 | | /* partial block */ |
42 | 0 | memcpy(&(c->data[i]), in, len); |
43 | 0 | c->num += (int)len; |
44 | 0 | return 1; |
45 | 0 | } else { |
46 | | /* filled one */ |
47 | 0 | j = MDC2_BLOCK - i; |
48 | 0 | memcpy(&(c->data[i]), in, j); |
49 | 0 | len -= j; |
50 | 0 | in += j; |
51 | 0 | c->num = 0; |
52 | 0 | mdc2_body(c, &(c->data[0]), MDC2_BLOCK); |
53 | 0 | } |
54 | 0 | } |
55 | 62 | i = len & ~((size_t)MDC2_BLOCK - 1); |
56 | 62 | if (i > 0) |
57 | 62 | mdc2_body(c, in, i); |
58 | 62 | j = len - i; |
59 | 62 | if (j > 0) { |
60 | 22 | memcpy(&(c->data[0]), &(in[i]), j); |
61 | 22 | c->num = (int)j; |
62 | 22 | } |
63 | 62 | return 1; |
64 | 62 | } |
65 | | |
66 | | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len) |
67 | 84 | { |
68 | 84 | register DES_LONG tin0, tin1; |
69 | 84 | register DES_LONG ttin0, ttin1; |
70 | 84 | DES_LONG d[2], dd[2]; |
71 | 84 | DES_key_schedule k; |
72 | 84 | unsigned char *p; |
73 | 84 | size_t i; |
74 | | |
75 | 8.41M | for (i = 0; i < len; i += 8) { |
76 | 8.41M | c2l(in, tin0); |
77 | 8.41M | d[0] = dd[0] = tin0; |
78 | 8.41M | c2l(in, tin1); |
79 | 8.41M | d[1] = dd[1] = tin1; |
80 | 8.41M | c->h[0] = (c->h[0] & 0x9f) | 0x40; |
81 | 8.41M | c->hh[0] = (c->hh[0] & 0x9f) | 0x20; |
82 | | |
83 | 8.41M | DES_set_odd_parity(&c->h); |
84 | 8.41M | DES_set_key_unchecked(&c->h, &k); |
85 | 8.41M | DES_encrypt1(d, &k, 1); |
86 | | |
87 | 8.41M | DES_set_odd_parity(&c->hh); |
88 | 8.41M | DES_set_key_unchecked(&c->hh, &k); |
89 | 8.41M | DES_encrypt1(dd, &k, 1); |
90 | | |
91 | 8.41M | ttin0 = tin0 ^ dd[0]; |
92 | 8.41M | ttin1 = tin1 ^ dd[1]; |
93 | 8.41M | tin0 ^= d[0]; |
94 | 8.41M | tin1 ^= d[1]; |
95 | | |
96 | 8.41M | p = c->h; |
97 | 8.41M | l2c(tin0, p); |
98 | 8.41M | l2c(ttin1, p); |
99 | 8.41M | p = c->hh; |
100 | 8.41M | l2c(ttin0, p); |
101 | 8.41M | l2c(tin1, p); |
102 | 8.41M | } |
103 | 84 | } |
104 | | |
105 | | int MDC2_Final(unsigned char *md, MDC2_CTX *c) |
106 | 31 | { |
107 | 31 | unsigned int i; |
108 | 31 | int j; |
109 | | |
110 | 31 | i = c->num; |
111 | 31 | j = c->pad_type; |
112 | 31 | if ((i > 0) || (j == 2)) { |
113 | 22 | if (j == 2) |
114 | 0 | c->data[i++] = 0x80; |
115 | 22 | memset(&(c->data[i]), 0, MDC2_BLOCK - i); |
116 | 22 | mdc2_body(c, c->data, MDC2_BLOCK); |
117 | 22 | } |
118 | 31 | memcpy(md, (char *)c->h, MDC2_BLOCK); |
119 | 31 | memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK); |
120 | 31 | return 1; |
121 | 31 | } |