Coverage Report

Created: 2026-07-24 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
60
{
27
60
    c->num = 0;
28
60
    c->pad_type = 1;
29
60
    memset(&(c->h[0]), 0x52, MDC2_BLOCK);
30
60
    memset(&(c->hh[0]), 0x25, MDC2_BLOCK);
31
60
    return 1;
32
60
}
33
34
int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len)
35
60
{
36
60
    size_t i, j;
37
38
60
    i = c->num;
39
60
    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
60
    i = len & ~((size_t)MDC2_BLOCK - 1);
56
60
    if (i > 0)
57
60
        mdc2_body(c, in, i);
58
60
    j = len - i;
59
60
    if (j > 0) {
60
21
        memcpy(&(c->data[0]), &(in[i]), j);
61
21
        c->num = (int)j;
62
21
    }
63
60
    return 1;
64
60
}
65
66
static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len)
67
81
{
68
81
    register DES_LONG tin0, tin1;
69
81
    register DES_LONG ttin0, ttin1;
70
81
    DES_LONG d[2], dd[2];
71
81
    DES_key_schedule k;
72
81
    unsigned char *p;
73
81
    size_t i;
74
75
6.82M
    for (i = 0; i < len; i += 8) {
76
6.82M
        c2l(in, tin0);
77
6.82M
        d[0] = dd[0] = tin0;
78
6.82M
        c2l(in, tin1);
79
6.82M
        d[1] = dd[1] = tin1;
80
6.82M
        c->h[0] = (c->h[0] & 0x9f) | 0x40;
81
6.82M
        c->hh[0] = (c->hh[0] & 0x9f) | 0x20;
82
83
6.82M
        DES_set_odd_parity(&c->h);
84
6.82M
        DES_set_key_unchecked(&c->h, &k);
85
6.82M
        DES_encrypt1(d, &k, 1);
86
87
6.82M
        DES_set_odd_parity(&c->hh);
88
6.82M
        DES_set_key_unchecked(&c->hh, &k);
89
6.82M
        DES_encrypt1(dd, &k, 1);
90
91
6.82M
        ttin0 = tin0 ^ dd[0];
92
6.82M
        ttin1 = tin1 ^ dd[1];
93
6.82M
        tin0 ^= d[0];
94
6.82M
        tin1 ^= d[1];
95
96
6.82M
        p = c->h;
97
6.82M
        l2c(tin0, p);
98
6.82M
        l2c(ttin1, p);
99
6.82M
        p = c->hh;
100
6.82M
        l2c(ttin0, p);
101
6.82M
        l2c(tin1, p);
102
6.82M
    }
103
81
}
104
105
int MDC2_Final(unsigned char *md, MDC2_CTX *c)
106
30
{
107
30
    unsigned int i;
108
30
    int j;
109
110
30
    i = c->num;
111
30
    j = c->pad_type;
112
30
    if ((i > 0) || (j == 2)) {
113
21
        if (j == 2)
114
0
            c->data[i++] = 0x80;
115
21
        memset(&(c->data[i]), 0, MDC2_BLOCK - i);
116
21
        mdc2_body(c, c->data, MDC2_BLOCK);
117
21
    }
118
30
    memcpy(md, (char *)c->h, MDC2_BLOCK);
119
30
    memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK);
120
30
    return 1;
121
30
}