Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/crypto/idea/i_skey.c
Line
Count
Source
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 <openssl/idea.h>
11
#include "idea_local.h"
12
13
static IDEA_INT inverse(unsigned int xin);
14
void IDEA_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks)
15
1.43k
{
16
1.43k
    int i;
17
1.43k
    register IDEA_INT *kt, *kf, r0, r1, r2;
18
19
1.43k
    kt = &(ks->data[0][0]);
20
1.43k
    n2s(key, kt[0]);
21
1.43k
    n2s(key, kt[1]);
22
1.43k
    n2s(key, kt[2]);
23
1.43k
    n2s(key, kt[3]);
24
1.43k
    n2s(key, kt[4]);
25
1.43k
    n2s(key, kt[5]);
26
1.43k
    n2s(key, kt[6]);
27
1.43k
    n2s(key, kt[7]);
28
29
1.43k
    kf = kt;
30
1.43k
    kt += 8;
31
8.58k
    for (i = 0; i < 6; i++) {
32
8.58k
        r2 = kf[1];
33
8.58k
        r1 = kf[2];
34
8.58k
        *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff;
35
8.58k
        r0 = kf[3];
36
8.58k
        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
37
8.58k
        r1 = kf[4];
38
8.58k
        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
39
8.58k
        r0 = kf[5];
40
8.58k
        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
41
8.58k
        r1 = kf[6];
42
8.58k
        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
43
8.58k
        r0 = kf[7];
44
8.58k
        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
45
8.58k
        r1 = kf[0];
46
8.58k
        if (i >= 5)
47
1.43k
            break;
48
7.15k
        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
49
7.15k
        *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff;
50
7.15k
        kf += 8;
51
7.15k
    }
52
1.43k
}
53
54
void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)
55
891
{
56
891
    int r;
57
891
    register IDEA_INT *fp, *tp, t;
58
59
891
    tp = &(dk->data[0][0]);
60
891
    fp = &(ek->data[8][0]);
61
8.01k
    for (r = 0; r < 9; r++) {
62
8.01k
        *(tp++) = inverse(fp[0]);
63
8.01k
        *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);
64
8.01k
        *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);
65
8.01k
        *(tp++) = inverse(fp[3]);
66
8.01k
        if (r == 8)
67
891
            break;
68
7.12k
        fp -= 6;
69
7.12k
        *(tp++) = fp[4];
70
7.12k
        *(tp++) = fp[5];
71
7.12k
    }
72
73
891
    tp = &(dk->data[0][0]);
74
891
    t = tp[1];
75
891
    tp[1] = tp[2];
76
891
    tp[2] = t;
77
78
891
    t = tp[49];
79
891
    tp[49] = tp[50];
80
891
    tp[50] = t;
81
891
}
82
83
/* taken directly from the 'paper' I'll have a look at it later */
84
static IDEA_INT inverse(unsigned int xin)
85
16.0k
{
86
16.0k
    long n1, n2, q, r, b1, b2, t;
87
88
16.0k
    if (xin == 0)
89
209
        b2 = 0;
90
15.8k
    else {
91
15.8k
        n1 = 0x10001;
92
15.8k
        n2 = xin;
93
15.8k
        b2 = 1;
94
15.8k
        b1 = 0;
95
96
149k
        do {
97
149k
            r = (n1 % n2);
98
149k
            q = (n1 - r) / n2;
99
149k
            if (r == 0) {
100
15.8k
                if (b2 < 0)
101
7.62k
                    b2 = 0x10001 + b2;
102
134k
            } else {
103
134k
                n1 = n2;
104
134k
                n2 = r;
105
134k
                t = b2;
106
134k
                b2 = b1 - q * b2;
107
134k
                b1 = t;
108
134k
            }
109
149k
        } while (r != 0);
110
15.8k
    }
111
16.0k
    return (IDEA_INT)b2;
112
16.0k
}