TAY
Fork of Crypto_light by
cipher/DES.cpp@7:2dbbdfb08123, 2013-09-14 (annotated)
- Committer:
- feb11
- Date:
- Sat Sep 14 18:21:32 2013 +0000
- Revision:
- 7:2dbbdfb08123
- Parent:
- DES.cpp@5:06cd9c8afa0b
- Child:
- 8:a090264e9b2d
added DES (not tested yet)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 5:06cd9c8afa0b | 1 | #include "DES.h" |
feb11 | 7:2dbbdfb08123 | 2 | #include <stdio.h> |
feb11 | 7:2dbbdfb08123 | 3 | #include <stdlib.h> |
feb11 | 7:2dbbdfb08123 | 4 | #include <string.h> |
feb11 | 7:2dbbdfb08123 | 5 | |
feb11 | 7:2dbbdfb08123 | 6 | |
feb11 | 7:2dbbdfb08123 | 7 | static const uint8_t S1[] = |
feb11 | 7:2dbbdfb08123 | 8 | { |
feb11 | 7:2dbbdfb08123 | 9 | 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
feb11 | 7:2dbbdfb08123 | 10 | 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
feb11 | 7:2dbbdfb08123 | 11 | 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
feb11 | 7:2dbbdfb08123 | 12 | 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
feb11 | 7:2dbbdfb08123 | 13 | }; |
feb11 | 7:2dbbdfb08123 | 14 | |
feb11 | 7:2dbbdfb08123 | 15 | static const uint8_t S2[] = |
feb11 | 7:2dbbdfb08123 | 16 | { |
feb11 | 7:2dbbdfb08123 | 17 | 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
feb11 | 7:2dbbdfb08123 | 18 | 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
feb11 | 7:2dbbdfb08123 | 19 | 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
feb11 | 7:2dbbdfb08123 | 20 | 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
feb11 | 7:2dbbdfb08123 | 21 | }; |
feb11 | 7:2dbbdfb08123 | 22 | |
feb11 | 7:2dbbdfb08123 | 23 | static const uint8_t S3[] = |
feb11 | 7:2dbbdfb08123 | 24 | { |
feb11 | 7:2dbbdfb08123 | 25 | 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
feb11 | 7:2dbbdfb08123 | 26 | 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
feb11 | 7:2dbbdfb08123 | 27 | 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
feb11 | 7:2dbbdfb08123 | 28 | 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
feb11 | 7:2dbbdfb08123 | 29 | }; |
feb11 | 7:2dbbdfb08123 | 30 | |
feb11 | 7:2dbbdfb08123 | 31 | static const uint8_t S4[] = |
feb11 | 7:2dbbdfb08123 | 32 | { |
feb11 | 7:2dbbdfb08123 | 33 | 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
feb11 | 7:2dbbdfb08123 | 34 | 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
feb11 | 7:2dbbdfb08123 | 35 | 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
feb11 | 7:2dbbdfb08123 | 36 | 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
feb11 | 7:2dbbdfb08123 | 37 | }; |
feb11 | 7:2dbbdfb08123 | 38 | |
feb11 | 7:2dbbdfb08123 | 39 | static const uint8_t S5[] = |
feb11 | 7:2dbbdfb08123 | 40 | { |
feb11 | 7:2dbbdfb08123 | 41 | 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
feb11 | 7:2dbbdfb08123 | 42 | 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
feb11 | 7:2dbbdfb08123 | 43 | 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
feb11 | 7:2dbbdfb08123 | 44 | 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
feb11 | 7:2dbbdfb08123 | 45 | }; |
feb11 | 7:2dbbdfb08123 | 46 | |
feb11 | 7:2dbbdfb08123 | 47 | static const uint8_t S6[] = |
feb11 | 7:2dbbdfb08123 | 48 | { |
feb11 | 7:2dbbdfb08123 | 49 | 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
feb11 | 7:2dbbdfb08123 | 50 | 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
feb11 | 7:2dbbdfb08123 | 51 | 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
feb11 | 7:2dbbdfb08123 | 52 | 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
feb11 | 7:2dbbdfb08123 | 53 | }; |
feb11 | 7:2dbbdfb08123 | 54 | |
feb11 | 7:2dbbdfb08123 | 55 | static const uint8_t S7[] = |
feb11 | 7:2dbbdfb08123 | 56 | { |
feb11 | 7:2dbbdfb08123 | 57 | 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
feb11 | 7:2dbbdfb08123 | 58 | 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
feb11 | 7:2dbbdfb08123 | 59 | 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
feb11 | 7:2dbbdfb08123 | 60 | 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
feb11 | 7:2dbbdfb08123 | 61 | }; |
feb11 | 7:2dbbdfb08123 | 62 | |
feb11 | 7:2dbbdfb08123 | 63 | static const uint8_t S8[] = |
feb11 | 7:2dbbdfb08123 | 64 | { |
feb11 | 7:2dbbdfb08123 | 65 | 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
feb11 | 7:2dbbdfb08123 | 66 | 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
feb11 | 7:2dbbdfb08123 | 67 | 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
feb11 | 7:2dbbdfb08123 | 68 | 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
feb11 | 7:2dbbdfb08123 | 69 | }; |
feb11 | 7:2dbbdfb08123 | 70 | |
feb11 | 7:2dbbdfb08123 | 71 | static void pc1(uint8_t *k, uint8_t *key) |
feb11 | 7:2dbbdfb08123 | 72 | { |
feb11 | 7:2dbbdfb08123 | 73 | memset(k, 0, 7); |
feb11 | 7:2dbbdfb08123 | 74 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 75 | { |
feb11 | 7:2dbbdfb08123 | 76 | k[0] = (k[0] << 1) | (key[i] & 0x01); |
feb11 | 7:2dbbdfb08123 | 77 | k[1] = (k[1] << 1) | ((key[i] & 0x02) >> 1); |
feb11 | 7:2dbbdfb08123 | 78 | k[2] = (k[2] << 1) | ((key[i] & 0x04) >> 2); |
feb11 | 7:2dbbdfb08123 | 79 | } |
feb11 | 7:2dbbdfb08123 | 80 | for(int i = 0 ; i < 4; ++i) |
feb11 | 7:2dbbdfb08123 | 81 | { |
feb11 | 7:2dbbdfb08123 | 82 | k[3] = (k[3] << 1) | ((key[4+i] & 0x40) >> 6); |
feb11 | 7:2dbbdfb08123 | 83 | k[4] = (k[4] << 1) | ((key[4+i] & 0x20) >> 5); |
feb11 | 7:2dbbdfb08123 | 84 | k[5] = (k[5] << 1) | ((key[4+i] & 0x10) >> 4); |
feb11 | 7:2dbbdfb08123 | 85 | k[6] = (k[6] << 1) | ((key[i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 86 | } |
feb11 | 7:2dbbdfb08123 | 87 | for(int i = 0 ; i < 4; ++i) |
feb11 | 7:2dbbdfb08123 | 88 | { |
feb11 | 7:2dbbdfb08123 | 89 | k[3] = (k[3] << 1) | ((key[4+i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 90 | k[4] = (k[4] << 1) | ((key[i] & 0x40) >> 6); |
feb11 | 7:2dbbdfb08123 | 91 | k[5] = (k[5] << 1) | ((key[i] & 0x20) >> 5); |
feb11 | 7:2dbbdfb08123 | 92 | k[6] = (k[6] << 1) | ((key[i] & 0x10) >> 4); |
feb11 | 7:2dbbdfb08123 | 93 | } |
feb11 | 7:2dbbdfb08123 | 94 | } |
feb11 | 7:2dbbdfb08123 | 95 | |
feb11 | 7:2dbbdfb08123 | 96 | static void leftShift(uint8_t *k) |
feb11 | 7:2dbbdfb08123 | 97 | { |
feb11 | 7:2dbbdfb08123 | 98 | uint8_t tmp = k[0] & 0x01, tmp2 = k[3] & 0x10; |
feb11 | 7:2dbbdfb08123 | 99 | k[0] = (k[0] >> 1) | ((k[1] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 100 | k[1] = (k[1] >> 1) | ((k[2] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 101 | k[2] = (k[2] >> 1) | ((k[3] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 102 | |
feb11 | 7:2dbbdfb08123 | 103 | k[3] = ((k[3] & 0x0E) >> 1) | (tmp << 3) | ((k[3] & 0xE0) >> 1) | ((k[4] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 104 | |
feb11 | 7:2dbbdfb08123 | 105 | k[4] = (k[4] >> 1) | ((k[5] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 106 | k[5] = (k[5] >> 1) | ((k[6] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 107 | k[6] = (k[6] >> 1) | (tmp2 << 3); |
feb11 | 7:2dbbdfb08123 | 108 | |
feb11 | 7:2dbbdfb08123 | 109 | } |
feb11 | 7:2dbbdfb08123 | 110 | |
feb11 | 7:2dbbdfb08123 | 111 | void pc2(uint8_t *subKey, uint8_t *k) |
feb11 | 7:2dbbdfb08123 | 112 | { |
feb11 | 7:2dbbdfb08123 | 113 | subKey[0] = ((k[1] & 0x20) >> 5) | ((k[2] & 0x01) << 1) | (k[1] & 0x04) | ((k[2] & 0x80) >> 4) | ((k[0] & 0x01) << 4) | ((k[0] & 0x10) << 1) | ((k[0] & 0x04) << 4) | ((k[3] & 0x08) << 4); |
feb11 | 7:2dbbdfb08123 | 114 | subKey[1] = ((k[1] & 0x40) >> 6) | ((k[0] & 0x20) >> 4) | ((k[2] & 0x10) >> 2) | ((k[1] & 0x02) << 2) | ((k[2] & 0x40) >> 2) | ((k[2] & 0x04) << 3) | ((k[1] & 0x08) << 3) | ((k[0] & 0x08) << 4); |
feb11 | 7:2dbbdfb08123 | 115 | subKey[2] = ((k[3] & 0x02) >> 1) | ((k[0] & 0x80) >> 6) | ((k[1] & 0x80) >> 5) | ((k[0] & 0x40) >> 3) | ((k[3] & 0x04) << 2) | ((k[2] & 0x08) << 2) | ((k[1] & 0x10) << 2) | ((k[0] & 0x02) << 6); |
feb11 | 7:2dbbdfb08123 | 116 | subKey[3] = (k[5] & 0x01) | ((k[6] & 0x08) >> 2) | ((k[3] & 0x40) >> 4) | ((k[4] & 0x10) >> 1) | ((k[5] & 0x40) >> 2) | ((k[6] & 0x40) >> 1) | ((k[3] & 0x20) << 1) | (k[4] & 0x80); |
feb11 | 7:2dbbdfb08123 | 117 | subKey[4] = ((k[6] & 0x04) >> 2) | ((k[5] & 0x10) >> 3) | ((k[4] & 0x01) << 2) | ((k[5] & 0x80) >> 4) | ((k[5] & 0x08) << 1) | ((k[6] & 0x01) << 5) | (k[4] & 0x40) | (k[6] & 0x80); |
feb11 | 7:2dbbdfb08123 | 118 | subKey[5] = ((k[4] & 0x02) >> 1) | ((k[6] & 0x10) >> 3) | ((k[5] & 0x20) >> 3) | ((k[5] & 0x02) << 2) | ((k[6] & 0x02) << 3) | ((k[4] & 0x08) << 2) | ((k[3] & 0x10) << 2) | (k[3] & 0x80); |
feb11 | 7:2dbbdfb08123 | 119 | } |
feb11 | 7:2dbbdfb08123 | 120 | |
feb11 | 7:2dbbdfb08123 | 121 | |
feb11 | 7:2dbbdfb08123 | 122 | static void initialPermutation(uint8_t *in) |
feb11 | 7:2dbbdfb08123 | 123 | { |
feb11 | 7:2dbbdfb08123 | 124 | uint8_t tmp[8]; |
feb11 | 7:2dbbdfb08123 | 125 | memcpy(tmp, in, 8); |
feb11 | 7:2dbbdfb08123 | 126 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 127 | { |
feb11 | 7:2dbbdfb08123 | 128 | tmp[4] = (tmp[4] << 1) | (in[i] & 0x01); |
feb11 | 7:2dbbdfb08123 | 129 | tmp[5] = (tmp[5] << 1) | ((in[i] & 0x04) >> 2); |
feb11 | 7:2dbbdfb08123 | 130 | tmp[6] = (tmp[6] << 1) | ((in[i] & 0x10) >> 4); |
feb11 | 7:2dbbdfb08123 | 131 | tmp[7] = (tmp[7] << 1) | ((in[i] & 0x40) >> 6); |
feb11 | 7:2dbbdfb08123 | 132 | |
feb11 | 7:2dbbdfb08123 | 133 | tmp[0] = (tmp[0] << 1) | ((in[i] & 0x02) >> 1); |
feb11 | 7:2dbbdfb08123 | 134 | tmp[1] = (tmp[1] << 1) | ((in[i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 135 | tmp[2] = (tmp[2] << 1) | ((in[i] & 0x20) >> 5); |
feb11 | 7:2dbbdfb08123 | 136 | tmp[3] = (tmp[3] << 1) | ((in[i] & 0x80) >> 7); |
feb11 | 7:2dbbdfb08123 | 137 | } |
feb11 | 7:2dbbdfb08123 | 138 | |
feb11 | 7:2dbbdfb08123 | 139 | memcpy(in, tmp, 8); |
feb11 | 7:2dbbdfb08123 | 140 | } |
feb11 | 7:2dbbdfb08123 | 141 | |
feb11 | 7:2dbbdfb08123 | 142 | static void invInitialPermutation(uint8_t *out) |
feb11 | 7:2dbbdfb08123 | 143 | { |
feb11 | 7:2dbbdfb08123 | 144 | uint8_t tmp[8]; |
feb11 | 7:2dbbdfb08123 | 145 | memcpy(tmp, out , 8); |
feb11 | 7:2dbbdfb08123 | 146 | for(int i = 3; i >= 0; --i) |
feb11 | 7:2dbbdfb08123 | 147 | { |
feb11 | 7:2dbbdfb08123 | 148 | out[0] = (out[0] << 2) | ((tmp[4+i] & 0x80) >> 7) | ((tmp[i] & 0x80) >> 6); |
feb11 | 7:2dbbdfb08123 | 149 | out[1] = (out[1] << 2) | ((tmp[4+i] & 0x40) >> 6) | ((tmp[i] & 0x40) >> 5); |
feb11 | 7:2dbbdfb08123 | 150 | out[2] = (out[2] << 2) | ((tmp[4+i] & 0x20) >> 5) | ((tmp[i] & 0x20) >> 4); |
feb11 | 7:2dbbdfb08123 | 151 | out[3] = (out[3] << 2) | ((tmp[4+i] & 0x10) >> 4) | ((tmp[i] & 0x10) >> 3); |
feb11 | 7:2dbbdfb08123 | 152 | out[4] = (out[4] << 2) | ((tmp[4+i] & 0x08) >> 3) | ((tmp[i] & 0x08) >> 2); |
feb11 | 7:2dbbdfb08123 | 153 | out[5] = (out[5] << 2) | ((tmp[4+i] & 0x04) >> 2) | ((tmp[i] & 0x04) >> 1); |
feb11 | 7:2dbbdfb08123 | 154 | out[6] = (out[6] << 2) | ((tmp[4+i] & 0x02) >> 1) | (tmp[i] & 0x02); |
feb11 | 7:2dbbdfb08123 | 155 | out[7] = (out[7] << 2) | (tmp[4+i] & 0x01) | ((tmp[i] & 0x01) << 1); |
feb11 | 7:2dbbdfb08123 | 156 | } |
feb11 | 7:2dbbdfb08123 | 157 | } |
feb11 | 7:2dbbdfb08123 | 158 | |
feb11 | 7:2dbbdfb08123 | 159 | static void expand(uint8_t *e, uint8_t *r) |
feb11 | 7:2dbbdfb08123 | 160 | { |
feb11 | 7:2dbbdfb08123 | 161 | |
feb11 | 7:2dbbdfb08123 | 162 | e[0] = ((r[3] & 0x80) >> 7) | ((r[0] & 0x1F) << 1) | ((r[0] & 0x18) << 3); |
feb11 | 7:2dbbdfb08123 | 163 | e[1] = ((r[0] & 0xE0) >> 5) | ((r[1] & 0x01) << 3) | ((r[0] & 0x80) >> 3) | ((r[1] & 0x07) << 5); |
feb11 | 7:2dbbdfb08123 | 164 | e[2] = ((r[1] & 0x18) >> 3) | ((r[1] & 0xF8) >> 1) | ((r[2] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 165 | e[3] = ((r[1] & 0x80) >> 7) | ((r[2] & 0x1F) << 1) | ((r[2] & 0x18) << 3); |
feb11 | 7:2dbbdfb08123 | 166 | e[4] = ((r[2] & 0xE0) >> 5) | ((r[3] & 0x01) << 3) | ((r[2] & 0x80) >> 3) | ((r[3] & 0x07) << 5); |
feb11 | 7:2dbbdfb08123 | 167 | e[5] = ((r[3] & 0x18) >> 3) | ((r[3] & 0xF8) >> 1) | ((r[0] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 168 | |
feb11 | 7:2dbbdfb08123 | 169 | } |
feb11 | 7:2dbbdfb08123 | 170 | |
feb11 | 7:2dbbdfb08123 | 171 | static void permutation(uint8_t *r) |
feb11 | 7:2dbbdfb08123 | 172 | { |
feb11 | 7:2dbbdfb08123 | 173 | uint8_t buffer[4]; |
feb11 | 7:2dbbdfb08123 | 174 | |
feb11 | 7:2dbbdfb08123 | 175 | buffer[0] = ((r[1] & 0x80) >> 7) | ((r[0] & 0x40) >> 5) | ((r[2] & 0x08) >> 1) | ((r[2] & 0x10) >> 1) | (r[3] & 0x10) | ((r[1] & 0x08) << 2) | ((r[3] & 0x08) << 3) | ((r[2] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 176 | buffer[1] = (r[0] & 0x01) | ((r[1] & 0x40) >> 5) | ((r[2] & 0x40) >> 4) | ((r[3] & 0x02) << 2) | (r[0] & 0x10) | ((r[2] & 0x02) << 4) | (r[3] & 0x40) | ((r[1] & 0x02) << 6); |
feb11 | 7:2dbbdfb08123 | 177 | buffer[2] = ((r[0] & 0x02) >> 1) | ((r[0] & 0x80) >> 6) | ((r[2] & 0x80) >> 5) | ((r[1] & 0x20) >> 2) | ((r[3] & 0x80) >> 3) | ((r[3] & 0x04) << 3) | ((r[0] & 0x04) << 4) | ((r[1] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 178 | buffer[3] = ((r[2] & 0x04) >> 2) | ((r[1] & 0x10) >> 3) | ((r[3] & 0x20) >> 3) | ((r[0] & 0x20) >> 2) | ((r[2] & 0x20) >> 1) | ((r[1] & 0x04) << 3) | ((r[0] & 0x08) << 3) | ((r[3] & 0x01) << 7); |
feb11 | 7:2dbbdfb08123 | 179 | |
feb11 | 7:2dbbdfb08123 | 180 | memcpy(r, buffer,4); |
feb11 | 7:2dbbdfb08123 | 181 | } |
feb11 | 7:2dbbdfb08123 | 182 | |
feb11 | 7:2dbbdfb08123 | 183 | static void substitute(uint8_t *r, uint8_t *e) |
feb11 | 7:2dbbdfb08123 | 184 | { |
feb11 | 7:2dbbdfb08123 | 185 | int index = ((e[0] & 0x01) << 5) | ((e[0] & 0x02) << 2) | (e[0] & 0x04) | ((e[0] & 0x08) >> 2) | ((e[0] & 0x10) >> 4) | ((e[0] & 0x20) >> 1); |
feb11 | 7:2dbbdfb08123 | 186 | int index2 = ((e[0] & 0x40) >> 1) | ((e[0] & 0x80) >> 4) | ((e[1] & 0x01) << 2) | (e[1] & 0x02) | ((e[1] & 0x04) >> 2) | ((e[1] & 0x08) << 1); |
feb11 | 7:2dbbdfb08123 | 187 | r[0] = ((S2[index2] & 0x08) >> 3) | ((S2[index2] & 0x04) >> 1) | ((S2[index2] & 0x02) << 1) | ((S2[index2] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 188 | r[0] <<= 4; |
feb11 | 7:2dbbdfb08123 | 189 | r[0] |= ((S1[index] & 0x08) >> 3) | ((S1[index] & 0x04) >> 1) | ((S1[index] & 0x02) << 1) | ((S1[index] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 190 | |
feb11 | 7:2dbbdfb08123 | 191 | |
feb11 | 7:2dbbdfb08123 | 192 | index = ((e[1] & 0x10) << 1) | ((e[1] & 0x20) >> 2) | ((e[1] & 0x40) >> 4) | ((e[1] & 0x80) >> 6) | (e[2] & 0x01) | ((e[2] & 0x02) << 3); |
feb11 | 7:2dbbdfb08123 | 193 | index2 = ((e[2] & 0x04) << 3) | (e[2] & 0x08) | ((e[2] & 0x10) >> 2) | ((e[2] & 0x20) >> 4) | ((e[2] & 0x40) >> 6) | ((e[2] & 0x80) >> 3); |
feb11 | 7:2dbbdfb08123 | 194 | |
feb11 | 7:2dbbdfb08123 | 195 | |
feb11 | 7:2dbbdfb08123 | 196 | r[1] = ((S4[index2] & 0x08) >> 3) | ((S4[index2] & 0x04) >> 1) | ((S4[index2] & 0x02) << 1) | ((S4[index2] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 197 | r[1] <<= 4; |
feb11 | 7:2dbbdfb08123 | 198 | r[1] |= ((S3[index] & 0x08) >> 3) | ((S3[index] & 0x04) >> 1) | ((S3[index] & 0x02) << 1) | ((S3[index] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 199 | |
feb11 | 7:2dbbdfb08123 | 200 | |
feb11 | 7:2dbbdfb08123 | 201 | index = ((e[3] & 0x01) << 5) | ((e[3] & 0x02) << 2) | (e[3] & 0x04) | ((e[3] & 0x08) >> 2) | ((e[3] & 0x10) >> 4) | ((e[3] & 0x20) >> 1); |
feb11 | 7:2dbbdfb08123 | 202 | index2 = ((e[3] & 0x40) >> 1) | ((e[3] & 0x80) >> 4) | ((e[4] & 0x01) << 2) | (e[4] & 0x02) | ((e[4] & 0x04) >> 2) | ((e[4] & 0x08) << 1); |
feb11 | 7:2dbbdfb08123 | 203 | r[2] = ((S6[index2] & 0x08) >> 3) | ((S6[index2] & 0x04) >> 1) | ((S6[index2] & 0x02) << 1) | ((S6[index2] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 204 | r[2] <<= 4; |
feb11 | 7:2dbbdfb08123 | 205 | r[2] |= ((S5[index] & 0x08) >> 3) | ((S5[index] & 0x04) >> 1) | ((S5[index] & 0x02) << 1) | ((S5[index] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 206 | |
feb11 | 7:2dbbdfb08123 | 207 | |
feb11 | 7:2dbbdfb08123 | 208 | index = ((e[4] & 0x10) << 1) | ((e[4] & 0x20) >> 2) | ((e[4] & 0x40) >> 4) | ((e[4] & 0x80) >> 6) | (e[5] & 0x01) | ((e[5] & 0x02) << 3); |
feb11 | 7:2dbbdfb08123 | 209 | index2 = ((e[5] & 0x04) << 3) | (e[5] & 0x08) | ((e[5] & 0x10) >> 2) | ((e[5] & 0x20) >> 4) | ((e[5] & 0x40) >> 6) | ((e[5] & 0x80) >> 3); |
feb11 | 7:2dbbdfb08123 | 210 | |
feb11 | 7:2dbbdfb08123 | 211 | r[3] = ((S8[index2] & 0x08) >> 3) | ((S8[index2] & 0x04) >> 1) | ((S8[index2] & 0x02) << 1) | ((S8[index2] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 212 | r[3] <<= 4; |
feb11 | 7:2dbbdfb08123 | 213 | r[3] |= ((S7[index] & 0x08) >> 3) | ((S7[index] & 0x04) >> 1) | ((S7[index] & 0x02) << 1) | ((S7[index] & 0x01) << 3); |
feb11 | 7:2dbbdfb08123 | 214 | } |
feb11 | 5:06cd9c8afa0b | 215 | |
feb11 | 5:06cd9c8afa0b | 216 | |
feb11 | 5:06cd9c8afa0b | 217 | DES::DES(uint8_t *key): |
feb11 | 5:06cd9c8afa0b | 218 | Cipher() |
feb11 | 5:06cd9c8afa0b | 219 | { |
feb11 | 7:2dbbdfb08123 | 220 | generateSubKeys(key); |
feb11 | 5:06cd9c8afa0b | 221 | } |
feb11 | 5:06cd9c8afa0b | 222 | |
feb11 | 5:06cd9c8afa0b | 223 | uint32_t DES::getBlockSize() const |
feb11 | 5:06cd9c8afa0b | 224 | { |
feb11 | 5:06cd9c8afa0b | 225 | return 8; |
feb11 | 5:06cd9c8afa0b | 226 | } |
feb11 | 5:06cd9c8afa0b | 227 | |
feb11 | 5:06cd9c8afa0b | 228 | CIPHER_TYPE DES::getType() const |
feb11 | 5:06cd9c8afa0b | 229 | { |
feb11 | 7:2dbbdfb08123 | 230 | return BLOCK_CIPHER; |
feb11 | 7:2dbbdfb08123 | 231 | }; |
feb11 | 7:2dbbdfb08123 | 232 | |
feb11 | 7:2dbbdfb08123 | 233 | void DES::generateSubKeys(uint8_t *key) |
feb11 | 7:2dbbdfb08123 | 234 | { |
feb11 | 7:2dbbdfb08123 | 235 | |
feb11 | 7:2dbbdfb08123 | 236 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 237 | key[i] = ((key[i] & 0x01) << 7) | ((key[i] & 0x02) << 5) | ((key[i] & 0x04) << 3) | ((key[i] & 0x08) << 1) | ((key[i] & 0x10) >> 1) | ((key[i] & 0x20) >> 3) | ((key[i] & 0x40) >> 5) | ((key[i] & 0x80) >> 7); |
feb11 | 7:2dbbdfb08123 | 238 | |
feb11 | 7:2dbbdfb08123 | 239 | uint8_t workingKey[7]; |
feb11 | 7:2dbbdfb08123 | 240 | pc1(workingKey, key); |
feb11 | 7:2dbbdfb08123 | 241 | |
feb11 | 7:2dbbdfb08123 | 242 | for(int i = 1; i <= 16; ++i) |
feb11 | 7:2dbbdfb08123 | 243 | { |
feb11 | 7:2dbbdfb08123 | 244 | leftShift(workingKey); |
feb11 | 7:2dbbdfb08123 | 245 | if(i != 9 && i >= 3 && i <=15) |
feb11 | 7:2dbbdfb08123 | 246 | leftShift(workingKey); |
feb11 | 7:2dbbdfb08123 | 247 | pc2(subKeys[i-1], workingKey); |
feb11 | 7:2dbbdfb08123 | 248 | } |
feb11 | 5:06cd9c8afa0b | 249 | } |
feb11 | 7:2dbbdfb08123 | 250 | |
feb11 | 7:2dbbdfb08123 | 251 | void DES::encrypt(uint8_t *out, uint8_t *in, uint32_t length) |
feb11 | 7:2dbbdfb08123 | 252 | { |
feb11 | 7:2dbbdfb08123 | 253 | uint8_t tmp[8]; |
feb11 | 7:2dbbdfb08123 | 254 | memcpy(tmp, in, 8); |
feb11 | 7:2dbbdfb08123 | 255 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 256 | tmp[i] = ((tmp[i] & 0x01) << 7) | ((tmp[i] & 0x02) << 5) | ((tmp[i] & 0x04) << 3) | ((tmp[i] & 0x08) << 1) | ((tmp[i] & 0x10) >> 1) | ((tmp[i] & 0x20) >> 3) | ((tmp[i] & 0x40) >> 5) | ((tmp[i] & 0x80) >> 7); |
feb11 | 7:2dbbdfb08123 | 257 | |
feb11 | 7:2dbbdfb08123 | 258 | |
feb11 | 7:2dbbdfb08123 | 259 | uint8_t l[4], r[4], tmpR[4], e[6]; |
feb11 | 7:2dbbdfb08123 | 260 | initialPermutation(tmp); |
feb11 | 7:2dbbdfb08123 | 261 | memcpy(l, tmp, 4); |
feb11 | 7:2dbbdfb08123 | 262 | memcpy(r, &tmp[4], 4); |
feb11 | 7:2dbbdfb08123 | 263 | for(int i = 0; i < 16; ++i) |
feb11 | 7:2dbbdfb08123 | 264 | { |
feb11 | 7:2dbbdfb08123 | 265 | memcpy(tmpR, r, 4); |
feb11 | 7:2dbbdfb08123 | 266 | expand(e, r); |
feb11 | 7:2dbbdfb08123 | 267 | for(int j = 0; j < 6; ++j) |
feb11 | 7:2dbbdfb08123 | 268 | e[j] ^= subKeys[i][j]; |
feb11 | 7:2dbbdfb08123 | 269 | substitute(r,e); |
feb11 | 7:2dbbdfb08123 | 270 | permutation(r); |
feb11 | 7:2dbbdfb08123 | 271 | for(int j = 0; j < 4; ++j) |
feb11 | 7:2dbbdfb08123 | 272 | r[j] ^= l[j]; |
feb11 | 7:2dbbdfb08123 | 273 | |
feb11 | 7:2dbbdfb08123 | 274 | memcpy(l, tmpR, 4); |
feb11 | 7:2dbbdfb08123 | 275 | |
feb11 | 7:2dbbdfb08123 | 276 | } |
feb11 | 7:2dbbdfb08123 | 277 | memcpy(tmp, r, 4); |
feb11 | 7:2dbbdfb08123 | 278 | memcpy(&tmp[4], l, 4); |
feb11 | 7:2dbbdfb08123 | 279 | |
feb11 | 7:2dbbdfb08123 | 280 | invInitialPermutation(tmp); |
feb11 | 7:2dbbdfb08123 | 281 | |
feb11 | 7:2dbbdfb08123 | 282 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 283 | { |
feb11 | 7:2dbbdfb08123 | 284 | out[i] = ((tmp[i] & 0x01) << 3) | ((tmp[i] & 0x02) << 1) | ((tmp[i] & 0x04) >> 1) | ((tmp[i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 285 | out[i] <<= 4; |
feb11 | 7:2dbbdfb08123 | 286 | tmp[i] >>= 4; |
feb11 | 7:2dbbdfb08123 | 287 | out[i] |= ((tmp[i] & 0x01) << 3) | ((tmp[i] & 0x02) << 1) | ((tmp[i] & 0x04) >> 1) | ((tmp[i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 288 | } |
feb11 | 7:2dbbdfb08123 | 289 | } |
feb11 | 7:2dbbdfb08123 | 290 | |
feb11 | 7:2dbbdfb08123 | 291 | |
feb11 | 7:2dbbdfb08123 | 292 | void DES::decrypt(uint8_t *out, uint8_t *in, uint32_t length) |
feb11 | 7:2dbbdfb08123 | 293 | { |
feb11 | 7:2dbbdfb08123 | 294 | uint8_t tmp[8]; |
feb11 | 7:2dbbdfb08123 | 295 | memcpy(tmp, in, 8); |
feb11 | 7:2dbbdfb08123 | 296 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 297 | tmp[i] = ((tmp[i] & 0x01) << 7) | ((tmp[i] & 0x02) << 5) | ((tmp[i] & 0x04) << 3) | ((tmp[i] & 0x08) << 1) | ((tmp[i] & 0x10) >> 1) | ((tmp[i] & 0x20) >> 3) | ((tmp[i] & 0x40) >> 5) | ((tmp[i] & 0x80) >> 7); |
feb11 | 7:2dbbdfb08123 | 298 | |
feb11 | 7:2dbbdfb08123 | 299 | uint8_t l[4], r[4], tmpL[4], e[6]; |
feb11 | 7:2dbbdfb08123 | 300 | initialPermutation(tmp); |
feb11 | 7:2dbbdfb08123 | 301 | memcpy(l, tmp, 4); |
feb11 | 7:2dbbdfb08123 | 302 | memcpy(r, &tmp[4], 4); |
feb11 | 7:2dbbdfb08123 | 303 | |
feb11 | 7:2dbbdfb08123 | 304 | for(int i = 15; i >= 0; --i) |
feb11 | 7:2dbbdfb08123 | 305 | { |
feb11 | 7:2dbbdfb08123 | 306 | memcpy(tmpL, r, 4); |
feb11 | 7:2dbbdfb08123 | 307 | expand(e, r); |
feb11 | 7:2dbbdfb08123 | 308 | for(int j = 0; j < 6; ++j) |
feb11 | 7:2dbbdfb08123 | 309 | e[j] ^= subKeys[i][j]; |
feb11 | 7:2dbbdfb08123 | 310 | substitute(r,e); |
feb11 | 7:2dbbdfb08123 | 311 | permutation(r); |
feb11 | 7:2dbbdfb08123 | 312 | for(int j = 0; j < 4; ++j) |
feb11 | 7:2dbbdfb08123 | 313 | r[j] ^= l[j]; |
feb11 | 7:2dbbdfb08123 | 314 | |
feb11 | 7:2dbbdfb08123 | 315 | memcpy(l, tmpL, 4); |
feb11 | 7:2dbbdfb08123 | 316 | } |
feb11 | 7:2dbbdfb08123 | 317 | |
feb11 | 7:2dbbdfb08123 | 318 | memcpy(&tmp[4], l, 4); |
feb11 | 7:2dbbdfb08123 | 319 | memcpy(tmp, r, 4); |
feb11 | 7:2dbbdfb08123 | 320 | invInitialPermutation(tmp); |
feb11 | 7:2dbbdfb08123 | 321 | |
feb11 | 7:2dbbdfb08123 | 322 | |
feb11 | 7:2dbbdfb08123 | 323 | for(int i = 0; i < 8; ++i) |
feb11 | 7:2dbbdfb08123 | 324 | { |
feb11 | 7:2dbbdfb08123 | 325 | out[i] = ((tmp[i] & 0x01) << 3) | ((tmp[i] & 0x02) << 1) | ((tmp[i] & 0x04) >> 1) | ((tmp[i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 326 | out[i] <<= 4; |
feb11 | 7:2dbbdfb08123 | 327 | tmp[i] >>= 4; |
feb11 | 7:2dbbdfb08123 | 328 | out[i] |= ((tmp[i] & 0x01) << 3) | ((tmp[i] & 0x02) << 1) | ((tmp[i] & 0x04) >> 1) | ((tmp[i] & 0x08) >> 3); |
feb11 | 7:2dbbdfb08123 | 329 | } |
feb11 | 7:2dbbdfb08123 | 330 | } |