Coverage Report

Created: 2018-08-29 13:53

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