| 180 | } |
| 181 | |
| 182 | void ChaCha20::Crypt(const unsigned char* m, unsigned char* c, size_t bytes) |
| 183 | { |
| 184 | uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; |
| 185 | uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; |
| 186 | unsigned char *ctarget = nullptr; |
| 187 | unsigned char tmp[64]; |
| 188 | unsigned int i; |
| 189 | |
| 190 | if (!bytes) return; |
| 191 | |
| 192 | j0 = input[0]; |
| 193 | j1 = input[1]; |
| 194 | j2 = input[2]; |
| 195 | j3 = input[3]; |
| 196 | j4 = input[4]; |
| 197 | j5 = input[5]; |
| 198 | j6 = input[6]; |
| 199 | j7 = input[7]; |
| 200 | j8 = input[8]; |
| 201 | j9 = input[9]; |
| 202 | j10 = input[10]; |
| 203 | j11 = input[11]; |
| 204 | j12 = input[12]; |
| 205 | j13 = input[13]; |
| 206 | j14 = input[14]; |
| 207 | j15 = input[15]; |
| 208 | |
| 209 | for (;;) { |
| 210 | if (bytes < 64) { |
| 211 | // if m has fewer than 64 bytes available, copy m to tmp and |
| 212 | // read from tmp instead |
| 213 | for (i = 0;i < bytes;++i) tmp[i] = m[i]; |
| 214 | m = tmp; |
| 215 | ctarget = c; |
| 216 | c = tmp; |
| 217 | } |
| 218 | x0 = j0; |
| 219 | x1 = j1; |
| 220 | x2 = j2; |
| 221 | x3 = j3; |
| 222 | x4 = j4; |
| 223 | x5 = j5; |
| 224 | x6 = j6; |
| 225 | x7 = j7; |
| 226 | x8 = j8; |
| 227 | x9 = j9; |
| 228 | x10 = j10; |
| 229 | x11 = j11; |
| 230 | x12 = j12; |
| 231 | x13 = j13; |
| 232 | x14 = j14; |
| 233 | x15 = j15; |
| 234 | for (i = 20;i > 0;i -= 2) { |
| 235 | QUARTERROUND( x0, x4, x8,x12) |
| 236 | QUARTERROUND( x1, x5, x9,x13) |
| 237 | QUARTERROUND( x2, x6,x10,x14) |
| 238 | QUARTERROUND( x3, x7,x11,x15) |
| 239 | QUARTERROUND( x0, x5,x10,x15) |