/src/openssl/crypto/idea/i_skey.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  |  |  * IDEA low level APIs are deprecated for public use, but still ok for internal  | 
12  |  |  * use where we're using them to implement the higher level EVP interface, as is  | 
13  |  |  * the case here.  | 
14  |  |  */  | 
15  |  | #include "internal/deprecated.h"  | 
16  |  |  | 
17  |  | #include <openssl/idea.h>  | 
18  |  | #include "idea_local.h"  | 
19  |  |  | 
20  |  | static IDEA_INT inverse(unsigned int xin);  | 
21  |  | void IDEA_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks)  | 
22  | 0  | { | 
23  | 0  |     int i;  | 
24  | 0  |     register IDEA_INT *kt, *kf, r0, r1, r2;  | 
25  |  | 
  | 
26  | 0  |     kt = &(ks->data[0][0]);  | 
27  | 0  |     n2s(key, kt[0]);  | 
28  | 0  |     n2s(key, kt[1]);  | 
29  | 0  |     n2s(key, kt[2]);  | 
30  | 0  |     n2s(key, kt[3]);  | 
31  | 0  |     n2s(key, kt[4]);  | 
32  | 0  |     n2s(key, kt[5]);  | 
33  | 0  |     n2s(key, kt[6]);  | 
34  | 0  |     n2s(key, kt[7]);  | 
35  |  | 
  | 
36  | 0  |     kf = kt;  | 
37  | 0  |     kt += 8;  | 
38  | 0  |     for (i = 0; i < 6; i++) { | 
39  | 0  |         r2 = kf[1];  | 
40  | 0  |         r1 = kf[2];  | 
41  | 0  |         *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff;  | 
42  | 0  |         r0 = kf[3];  | 
43  | 0  |         *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;  | 
44  | 0  |         r1 = kf[4];  | 
45  | 0  |         *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;  | 
46  | 0  |         r0 = kf[5];  | 
47  | 0  |         *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;  | 
48  | 0  |         r1 = kf[6];  | 
49  | 0  |         *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;  | 
50  | 0  |         r0 = kf[7];  | 
51  | 0  |         *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;  | 
52  | 0  |         r1 = kf[0];  | 
53  | 0  |         if (i >= 5)  | 
54  | 0  |             break;  | 
55  | 0  |         *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;  | 
56  | 0  |         *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff;  | 
57  | 0  |         kf += 8;  | 
58  | 0  |     }  | 
59  | 0  | }  | 
60  |  |  | 
61  |  | void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)  | 
62  | 0  | { | 
63  | 0  |     int r;  | 
64  | 0  |     register IDEA_INT *fp, *tp, t;  | 
65  |  | 
  | 
66  | 0  |     tp = &(dk->data[0][0]);  | 
67  | 0  |     fp = &(ek->data[8][0]);  | 
68  | 0  |     for (r = 0; r < 9; r++) { | 
69  | 0  |         *(tp++) = inverse(fp[0]);  | 
70  | 0  |         *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);  | 
71  | 0  |         *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);  | 
72  | 0  |         *(tp++) = inverse(fp[3]);  | 
73  | 0  |         if (r == 8)  | 
74  | 0  |             break;  | 
75  | 0  |         fp -= 6;  | 
76  | 0  |         *(tp++) = fp[4];  | 
77  | 0  |         *(tp++) = fp[5];  | 
78  | 0  |     }  | 
79  |  | 
  | 
80  | 0  |     tp = &(dk->data[0][0]);  | 
81  | 0  |     t = tp[1];  | 
82  | 0  |     tp[1] = tp[2];  | 
83  | 0  |     tp[2] = t;  | 
84  |  | 
  | 
85  | 0  |     t = tp[49];  | 
86  | 0  |     tp[49] = tp[50];  | 
87  | 0  |     tp[50] = t;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | /* taken directly from the 'paper' I'll have a look at it later */  | 
91  |  | static IDEA_INT inverse(unsigned int xin)  | 
92  | 0  | { | 
93  | 0  |     long n1, n2, q, r, b1, b2, t;  | 
94  |  | 
  | 
95  | 0  |     if (xin == 0)  | 
96  | 0  |         b2 = 0;  | 
97  | 0  |     else { | 
98  | 0  |         n1 = 0x10001;  | 
99  | 0  |         n2 = xin;  | 
100  | 0  |         b2 = 1;  | 
101  | 0  |         b1 = 0;  | 
102  |  | 
  | 
103  | 0  |         do { | 
104  | 0  |             r = (n1 % n2);  | 
105  | 0  |             q = (n1 - r) / n2;  | 
106  | 0  |             if (r == 0) { | 
107  | 0  |                 if (b2 < 0)  | 
108  | 0  |                     b2 = 0x10001 + b2;  | 
109  | 0  |             } else { | 
110  | 0  |                 n1 = n2;  | 
111  | 0  |                 n2 = r;  | 
112  | 0  |                 t = b2;  | 
113  | 0  |                 b2 = b1 - q * b2;  | 
114  | 0  |                 b1 = t;  | 
115  | 0  |             }  | 
116  | 0  |         } while (r != 0);  | 
117  | 0  |     }  | 
118  | 0  |     return (IDEA_INT)b2;  | 
119  | 0  | }  |