Coverage Report

Created: 2023-06-08 06:40

/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
824
{
16
824
    int i;
17
824
    register IDEA_INT *kt, *kf, r0, r1, r2;
18
19
824
    kt = &(ks->data[0][0]);
20
824
    n2s(key, kt[0]);
21
824
    n2s(key, kt[1]);
22
824
    n2s(key, kt[2]);
23
824
    n2s(key, kt[3]);
24
824
    n2s(key, kt[4]);
25
824
    n2s(key, kt[5]);
26
824
    n2s(key, kt[6]);
27
824
    n2s(key, kt[7]);
28
29
824
    kf = kt;
30
824
    kt += 8;
31
4.94k
    for (i = 0; i < 6; i++) {
32
4.94k
        r2 = kf[1];
33
4.94k
        r1 = kf[2];
34
4.94k
        *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff;
35
4.94k
        r0 = kf[3];
36
4.94k
        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
37
4.94k
        r1 = kf[4];
38
4.94k
        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
39
4.94k
        r0 = kf[5];
40
4.94k
        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
41
4.94k
        r1 = kf[6];
42
4.94k
        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
43
4.94k
        r0 = kf[7];
44
4.94k
        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
45
4.94k
        r1 = kf[0];
46
4.94k
        if (i >= 5)
47
824
            break;
48
4.12k
        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
49
4.12k
        *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff;
50
4.12k
        kf += 8;
51
4.12k
    }
52
824
}
53
54
void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)
55
249
{
56
249
    int r;
57
249
    register IDEA_INT *fp, *tp, t;
58
59
249
    tp = &(dk->data[0][0]);
60
249
    fp = &(ek->data[8][0]);
61
2.24k
    for (r = 0; r < 9; r++) {
62
2.24k
        *(tp++) = inverse(fp[0]);
63
2.24k
        *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);
64
2.24k
        *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);
65
2.24k
        *(tp++) = inverse(fp[3]);
66
2.24k
        if (r == 8)
67
249
            break;
68
1.99k
        fp -= 6;
69
1.99k
        *(tp++) = fp[4];
70
1.99k
        *(tp++) = fp[5];
71
1.99k
    }
72
73
249
    tp = &(dk->data[0][0]);
74
249
    t = tp[1];
75
249
    tp[1] = tp[2];
76
249
    tp[2] = t;
77
78
249
    t = tp[49];
79
249
    tp[49] = tp[50];
80
249
    tp[50] = t;
81
249
}
82
83
/* taken directly from the 'paper' I'll have a look at it later */
84
static IDEA_INT inverse(unsigned int xin)
85
4.48k
{
86
4.48k
    long n1, n2, q, r, b1, b2, t;
87
88
4.48k
    if (xin == 0)
89
84
        b2 = 0;
90
4.39k
    else {
91
4.39k
        n1 = 0x10001;
92
4.39k
        n2 = xin;
93
4.39k
        b2 = 1;
94
4.39k
        b1 = 0;
95
96
41.5k
        do {
97
41.5k
            r = (n1 % n2);
98
41.5k
            q = (n1 - r) / n2;
99
41.5k
            if (r == 0) {
100
4.39k
                if (b2 < 0)
101
2.21k
                    b2 = 0x10001 + b2;
102
37.1k
            } else {
103
37.1k
                n1 = n2;
104
37.1k
                n2 = r;
105
37.1k
                t = b2;
106
37.1k
                b2 = b1 - q * b2;
107
37.1k
                b1 = t;
108
37.1k
            }
109
41.5k
        } while (r != 0);
110
4.39k
    }
111
4.48k
    return (IDEA_INT)b2;
112
4.48k
}