/src/openssl/crypto/mdc2/mdc2dgst.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
23 | | #undef c2l |
24 | 14.9M | #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \ |
25 | 14.9M | l|=((DES_LONG)(*((c)++)))<< 8L, \ |
26 | 14.9M | l|=((DES_LONG)(*((c)++)))<<16L, \ |
27 | 14.9M | l|=((DES_LONG)(*((c)++)))<<24L) |
28 | | |
29 | | #undef l2c |
30 | 29.9M | #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ |
31 | 29.9M | *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ |
32 | 29.9M | *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ |
33 | 29.9M | *((c)++)=(unsigned char)(((l)>>24L)&0xff)) |
34 | | |
35 | | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len); |
36 | | int MDC2_Init(MDC2_CTX *c) |
37 | 54 | { |
38 | 54 | c->num = 0; |
39 | 54 | c->pad_type = 1; |
40 | 54 | memset(&(c->h[0]), 0x52, MDC2_BLOCK); |
41 | 54 | memset(&(c->hh[0]), 0x25, MDC2_BLOCK); |
42 | 54 | return 1; |
43 | 54 | } |
44 | | |
45 | | int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len) |
46 | 54 | { |
47 | 54 | size_t i, j; |
48 | | |
49 | 54 | i = c->num; |
50 | 54 | if (i != 0) { |
51 | 0 | if (len < MDC2_BLOCK - i) { |
52 | | /* partial block */ |
53 | 0 | memcpy(&(c->data[i]), in, len); |
54 | 0 | c->num += (int)len; |
55 | 0 | return 1; |
56 | 0 | } else { |
57 | | /* filled one */ |
58 | 0 | j = MDC2_BLOCK - i; |
59 | 0 | memcpy(&(c->data[i]), in, j); |
60 | 0 | len -= j; |
61 | 0 | in += j; |
62 | 0 | c->num = 0; |
63 | 0 | mdc2_body(c, &(c->data[0]), MDC2_BLOCK); |
64 | 0 | } |
65 | 0 | } |
66 | 54 | i = len & ~((size_t)MDC2_BLOCK - 1); |
67 | 54 | if (i > 0) |
68 | 54 | mdc2_body(c, in, i); |
69 | 54 | j = len - i; |
70 | 54 | if (j > 0) { |
71 | 22 | memcpy(&(c->data[0]), &(in[i]), j); |
72 | 22 | c->num = (int)j; |
73 | 22 | } |
74 | 54 | return 1; |
75 | 54 | } |
76 | | |
77 | | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len) |
78 | 76 | { |
79 | 76 | register DES_LONG tin0, tin1; |
80 | 76 | register DES_LONG ttin0, ttin1; |
81 | 76 | DES_LONG d[2], dd[2]; |
82 | 76 | DES_key_schedule k; |
83 | 76 | unsigned char *p; |
84 | 76 | size_t i; |
85 | | |
86 | 7.48M | for (i = 0; i < len; i += 8) { |
87 | 7.48M | c2l(in, tin0); |
88 | 7.48M | d[0] = dd[0] = tin0; |
89 | 7.48M | c2l(in, tin1); |
90 | 7.48M | d[1] = dd[1] = tin1; |
91 | 7.48M | c->h[0] = (c->h[0] & 0x9f) | 0x40; |
92 | 7.48M | c->hh[0] = (c->hh[0] & 0x9f) | 0x20; |
93 | | |
94 | 7.48M | DES_set_odd_parity(&c->h); |
95 | 7.48M | DES_set_key_unchecked(&c->h, &k); |
96 | 7.48M | DES_encrypt1(d, &k, 1); |
97 | | |
98 | 7.48M | DES_set_odd_parity(&c->hh); |
99 | 7.48M | DES_set_key_unchecked(&c->hh, &k); |
100 | 7.48M | DES_encrypt1(dd, &k, 1); |
101 | | |
102 | 7.48M | ttin0 = tin0 ^ dd[0]; |
103 | 7.48M | ttin1 = tin1 ^ dd[1]; |
104 | 7.48M | tin0 ^= d[0]; |
105 | 7.48M | tin1 ^= d[1]; |
106 | | |
107 | 7.48M | p = c->h; |
108 | 7.48M | l2c(tin0, p); |
109 | 7.48M | l2c(ttin1, p); |
110 | 7.48M | p = c->hh; |
111 | 7.48M | l2c(ttin0, p); |
112 | 7.48M | l2c(tin1, p); |
113 | 7.48M | } |
114 | 76 | } |
115 | | |
116 | | int MDC2_Final(unsigned char *md, MDC2_CTX *c) |
117 | 27 | { |
118 | 27 | unsigned int i; |
119 | 27 | int j; |
120 | | |
121 | 27 | i = c->num; |
122 | 27 | j = c->pad_type; |
123 | 27 | if ((i > 0) || (j == 2)) { |
124 | 22 | if (j == 2) |
125 | 0 | c->data[i++] = 0x80; |
126 | 22 | memset(&(c->data[i]), 0, MDC2_BLOCK - i); |
127 | 22 | mdc2_body(c, c->data, MDC2_BLOCK); |
128 | 22 | } |
129 | 27 | memcpy(md, (char *)c->h, MDC2_BLOCK); |
130 | 27 | memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK); |
131 | 27 | return 1; |
132 | 27 | } |