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