1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
//! # aes_with_operation_mode //! `aes_with_operation_mode` allows you to use AES with operation modes like CBC, CFB and so on. use super::aes_core; use std::mem; /// ECB (Electronic Codebook) Encryption /// /// This function encrypts a long plain from the first parameter and put the long cipher /// into the second parameter, using the scheduled keys in the third parameter. /// Finally, it returns the final block of the cipher (NOT the plain). /// ![ECB encryption](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/ECB_encryption.svg/1280px-ECB_encryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// # Examples /// ``` /// use aes_frast::{aes_core, aes_with_operation_mode, padding_128bit}; /// let length: usize = 64; /// let mut plain: Vec<u8> = vec![0x4E, 0xFD, 0x06, 0xB2, 0xCE, 0xEE, 0x59, 0x02, /// 0xCA, 0xE8, 0x4E, 0x58, 0xFC, 0x50, 0x92, 0x1E, /// 0xF0, 0x8D, 0xD2, 0x30, 0xB9, 0xC4, 0x1D, 0x1C, /// 0xD2, 0xEB, 0x88, 0x1D, 0xE7, 0x4A, 0xF7, 0x7B, /// 0x36, 0x75, 0xA9, 0x68, 0x03, 0xB9, 0x39, 0xDF, /// 0xFB, 0x9E, 0x80, 0x85, 0x1C, 0x73, 0x54, 0xBF, /// 0x2A, 0xE4, 0xAD, 0x0E, 0x43, 0x58, 0xDE, 0x88, /// 0xDF, 0xE9, 0xDF, 0xE1, 0x66, 0x3B, 0x1A, 0x7F]; /// let mut cipher = vec![0u8; length + 16]; /// let mut dec_cipher = vec![0u8; length + 16]; /// let mut o_key: Vec<u8> = vec![0xDE, 0xA6, 0xE1, 0x3A, 0x82, 0xFF, 0x95, 0x11, /// 0x86, 0x2D, 0xC1, 0x5A, 0x8E, 0xFA, 0xBE, 0xD4, /// 0x3B, 0x52, 0x28, 0x78, 0xE2, 0x49, 0x1F, 0x86, /// 0x0A, 0xB6, 0xE0, 0xCD, 0xBD, 0xDD, 0xCC, 0x2E]; /// let mut w_keys: Vec<u32> = vec![0u32; 60]; /// /// aes_core::setkey_enc_auto(&o_key, &mut w_keys); /// padding_128bit::pa_pkcs7(&mut plain); /// aes_with_operation_mode::ecb_enc(&plain, &mut cipher, &w_keys); /// /// let expected_encrypted = vec![0x48, 0x52, 0xC9, 0xB1, 0xD0, 0x45, 0xA4, 0xC1, /// 0x8C, 0xCF, 0x98, 0xEF, 0x57, 0x97, 0x62, 0xAC, /// 0x1D, 0xD5, 0x1A, 0xBC, 0xF6, 0x4F, 0xED, 0xD6, /// 0xAD, 0x18, 0x46, 0x4B, 0xED, 0x28, 0xC0, 0xC6, /// 0x72, 0xBD, 0x54, 0x49, 0x11, 0x93, 0xE3, 0xAE, /// 0xE8, 0x49, 0x9C, 0x84, 0x7C, 0xD5, 0x99, 0x41, /// 0x0B, 0x5D, 0x80, 0x07, 0x6A, 0x87, 0x6F, 0xFB, /// 0x45, 0x75, 0xE6, 0x38, 0x8A, 0x5C, 0x0D, 0x03, /// 0xFA, 0x60, 0x15, 0x86, 0x83, 0x02, 0x2E, 0xD9, /// 0x27, 0x56, 0x65, 0x6B, 0xFE, 0x68, 0x5F, 0xF3]; /// assert_eq!(length + 16, cipher.len()); /// for i in 0..(length + 16) { /// assert_eq!(expected_encrypted[i], cipher[i]); /// } /// /// aes_core::setkey_dec_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::ecb_dec(&cipher, &mut dec_cipher, &w_keys); /// padding_128bit::de_ansix923_pkcs7(&mut dec_cipher); /// /// assert_eq!(length, dec_cipher.len()); /// for i in 0..length { /// assert_eq!(plain[i], dec_cipher[i]); /// } /// ``` pub fn ecb_enc(plain: &[u8], cipher: &mut [u8], keys: &[u32]) -> Vec<u8> { let encryptor = match keys.len() { 44 => aes_core::block_enc_k128, 52 => aes_core::block_enc_k192, 60 => aes_core::block_enc_k256, _ => panic!("Invalid key length."), }; // `>> 4` is the same as `/ 16` and `<< 4` is the same as `* 4`. let block_number: usize = plain.len() >> 4; let mut start: usize = 0; let mut end: usize = 16; for i in 0usize..block_number { start = i << 4; end = start + 16; encryptor(&plain[start..end], &mut cipher[start..end], keys); } cipher[start..end].to_owned() } /// ECB (Electronic Codebook) Decryption /// /// This function decrypts a long cipher from the first parameter and put the long plain /// into the second parameter, using the scheduled keys in the third parameter. /// Finally, it returns the final block of the cipher (NOT the plain). /// ![ECB decryption](https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/ECB_decryption.svg/1280px-ECB_decryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// # Examples /// Please refer to the [`ecb_enc`] function, codes are included there. /// /// [`ecb_enc`]: ../aes_with_operation_mode/fn.ecb_enc.html pub fn ecb_dec(cipher: &[u8], plain: &mut [u8], keys: &[u32]) -> Vec<u8> { let decryptor = match keys.len() { 44 => aes_core::block_dec_k128, 52 => aes_core::block_dec_k192, 60 => aes_core::block_dec_k256, _ => panic!("Invalid key length."), }; let block_number: usize = cipher.len() >> 4; let mut start: usize = 0; let mut end: usize = 16; for i in 0usize..block_number { start = i << 4; end = start + 16; decryptor(&cipher[start..end], &mut plain[start..end], keys); } cipher[start..end].to_owned() } /// CBC (Cipher Block Chaining) Encryption /// /// This function encrypts a long plain from the first parameter and put the long cipher /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// Finally, it returns the final block of the cipher (NOT the plain). /// ![CBC encryption](https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/CBC_encryption.svg/1280px-CBC_encryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// # Examples /// ``` /// use aes_frast::{aes_core, aes_with_operation_mode, padding_128bit}; /// let length: usize = 64; /// let mut plain: Vec<u8> = vec![0x30, 0xA3, 0xBC, 0xA0, 0xBA, 0x57, 0xFB, 0x27, /// 0x2F, 0xE2, 0x31, 0xEC, 0xBB, 0x04, 0x5E, 0x93, /// 0x40, 0xA1, 0x19, 0xB6, 0xE2, 0x60, 0x4F, 0x74, /// 0x0E, 0xF2, 0xC0, 0x29, 0x1B, 0xF0, 0xF9, 0x9B, /// 0xFD, 0x90, 0xFD, 0x0D, 0x97, 0xF8, 0x9B, 0x91, /// 0x78, 0xF4, 0x90, 0x07, 0x66, 0x6C, 0xE8, 0x11, /// 0x5F, 0xFE, 0x6F, 0xE9, 0x4F, 0x75, 0xB0, 0xCE, /// 0x53, 0x48, 0x92, 0xC7, 0x96, 0xD2, 0x0B, 0x3E]; /// let mut cipher = vec![0u8; length + 16]; /// let mut dec_cipher = vec![0u8; length + 16]; /// let mut o_key: Vec<u8> = vec![0x0F, 0x57, 0x9F, 0x79, 0x50, 0x99, 0x0A, 0xCE, /// 0x66, 0x72, 0xA8, 0x17, 0x95, 0x1F, 0xF6, 0x06, /// 0x24, 0x40, 0xDE, 0xF5, 0x08, 0xF1, 0x64, 0x34, /// 0xD6, 0xEF, 0xEF, 0xFD, 0x26, 0x23, 0x04, 0x95]; /// let mut w_keys: Vec<u32> = vec![0u32; 60]; /// let mut iv: Vec<u8> = vec![0x04, 0x7C, 0xF3, 0xEA, 0xE1, 0x76, 0x45, 0x85, /// 0x72, 0x52, 0x7B, 0xAA, 0x26, 0x0D, 0x65, 0xBB]; /// /// aes_core::setkey_enc_auto(&o_key, &mut w_keys); /// padding_128bit::pa_pkcs7(&mut plain); /// aes_with_operation_mode::cbc_enc(&plain, &mut cipher, &w_keys, &iv); /// /// let expected_encrypted = vec![0xBE, 0xF7, 0x24, 0xED, 0xB0, 0x45, 0x0F, 0x20, /// 0xBC, 0x8B, 0x97, 0xA4, 0x17, 0xF3, 0xC0, 0x6F, /// 0x08, 0xF8, 0x3D, 0xF4, 0x78, 0x0E, 0xA3, 0x11, /// 0x5C, 0xE8, 0x59, 0x8F, 0xF7, 0xD6, 0x9C, 0x54, /// 0x83, 0x37, 0x9C, 0xE7, 0x95, 0x44, 0x78, 0x03, /// 0xDA, 0x86, 0x6B, 0x67, 0x7D, 0x66, 0xAD, 0x48, /// 0x7F, 0x8B, 0xF5, 0x57, 0x0C, 0xDC, 0xB0, 0x2C, /// 0x7C, 0xB7, 0xFA, 0x1A, 0x07, 0x57, 0x94, 0x14, /// 0x3A, 0xA4, 0xB7, 0x91, 0x76, 0x1C, 0x2F, 0xAD, /// 0x77, 0x8E, 0x50, 0xE7, 0x03, 0xEA, 0x70, 0x2A]; /// assert_eq!(length + 16, cipher.len()); /// for i in 0..(length + 16) { /// assert_eq!(expected_encrypted[i], cipher[i]); /// } /// /// aes_core::setkey_dec_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::cbc_dec(&cipher, &mut dec_cipher, &w_keys, &iv); /// padding_128bit::de_ansix923_pkcs7(&mut dec_cipher); /// /// assert_eq!(length, dec_cipher.len()); /// for i in 0..length { /// assert_eq!(plain[i], dec_cipher[i]); /// } /// ``` pub fn cbc_enc(plain: &[u8], cipher: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { let encryptor = match keys.len() { 44 => aes_core::block_enc_k128, 52 => aes_core::block_enc_k192, 60 => aes_core::block_enc_k256, _ => panic!("Invalid key length."), }; let block_number: usize = plain.len() >> 4; let mut buffer: [u8; 16] = [0; 16]; // The 1st (head) block for j in 0..16 { buffer[j] = iv[j] ^ plain[j]; } encryptor(&buffer, &mut cipher[0..16], keys); // The other blocks let mut start: usize = 0; for i in 1usize..block_number { start = i << 4; for j in 0..16 { buffer[j] = cipher[start + j - 16] ^ plain[start + j]; } encryptor(&buffer, &mut cipher[start..(start + 16)], keys); } cipher[start..(start + 16)].to_owned() } /// CBC (Cipher Block Chaining) Decryption /// /// This function decrypts a long cipher from the first parameter and put the long plain /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// Finally, it returns the final block of the cipher (NOT the plain). /// ![CBC decryption](https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/CBC_decryption.svg/1280px-CBC_decryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// # Examples /// Please refer to the [`cbc_enc`] function, codes are included there. /// /// [`cbc_enc`]: ../aes_with_operation_mode/fn.cbc_enc.html pub fn cbc_dec(cipher: &[u8], plain: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { let decryptor = match keys.len() { 44 => aes_core::block_dec_k128, 52 => aes_core::block_dec_k192, 60 => aes_core::block_dec_k256, _ => panic!("Invalid key length."), }; let block_number: usize = cipher.len() >> 4; let mut buffer: [u8; 16] = [0; 16]; // The 1st (head) block decryptor(&cipher[0..16], &mut buffer, keys); for j in 0..16 { plain[j] = iv[j] ^ buffer[j]; } // The other blocks let mut start: usize = 0; for i in 1usize..block_number { start = i << 4; decryptor(&cipher[start..(start + 16)], &mut buffer, keys); for j in 0..16 { plain[start + j] = cipher[start + j - 16] ^ buffer[j]; } } cipher[start..(start + 16)].to_owned() } /// CFB (Cipher Feedback) Encryption /// /// The feedback size is fixed to 128 bits, which is the same as block size. /// This mode doesn't require padding. /// /// This function encrypts a long plain from the first parameter and put the long cipher /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// Finally, it returns the final block of the cipher (NOT the plain). /// ![CFB encryption](https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/CFB_encryption.svg/1280px-CFB_encryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// # Examples /// ``` /// use aes_frast::{aes_core, aes_with_operation_mode, padding_128bit}; /// let length: usize = 64; /// let mut plain: Vec<u8> = vec![0x34, 0x63, 0xD0, 0x89, 0x1D, 0x71, 0x4A, 0xB0, /// 0x08, 0x5D, 0x22, 0xE1, 0x8B, 0xFA, 0x77, 0xF0, /// 0xEB, 0xE4, 0xB8, 0x9E, 0xF0, 0x05, 0x32, 0x7D, /// 0x4F, 0xBD, 0x87, 0x69, 0x75, 0x76, 0x78, 0xAA, /// 0x3D, 0x24, 0x06, 0x0C, 0xA4, 0xA5, 0x8C, 0xA0, /// 0x21, 0x58, 0xB8, 0xA1, 0x86, 0xAD, 0xBB, 0x6D, /// 0x9E, 0x09, 0x1C, 0x47, 0x06, 0x25, 0x4B, 0x2E, /// 0x30, 0x53, 0x3A, 0x5F, 0xE9, 0xDF, 0x3A, 0x90]; /// let mut cipher = vec![0u8; length]; /// let mut dec_cipher = vec![0u8; length]; /// let mut o_key: Vec<u8> = vec![0x0F, 0x57, 0x9F, 0x79, 0x50, 0x99, 0x0A, 0xCE, /// 0x66, 0x72, 0xA8, 0x17, 0x95, 0x1F, 0xF6, 0x06, /// 0x24, 0x40, 0xDE, 0xF5, 0x08, 0xF1, 0x64, 0x34, /// 0xD6, 0xEF, 0xEF, 0xFD, 0x26, 0x23, 0x04, 0x95]; /// let mut w_keys: Vec<u32> = vec![0u32; 60]; /// let mut iv: Vec<u8> = vec![0x04, 0x7C, 0xF3, 0xEA, 0xE1, 0x76, 0x45, 0x85, /// 0x72, 0x52, 0x7B, 0xAA, 0x26, 0x0D, 0x65, 0xBB]; /// /// aes_core::setkey_enc_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::cfb_enc(&plain, &mut cipher, &w_keys, &iv); /// /// let expected_encrypted = vec![0x9D, 0xF9, 0x3D, 0x71, 0xC1, 0x9E, 0x50, 0x22, /// 0x36, 0x35, 0xF1, 0xB6, 0xED, 0xA6, 0x86, 0x74, /// 0x5F, 0x34, 0xD6, 0x93, 0xA9, 0x0F, 0xFF, 0x50, /// 0x4C, 0xE6, 0x8E, 0xC0, 0x06, 0x3F, 0x3A, 0x62, /// 0x78, 0x9F, 0xAB, 0xB1, 0x06, 0x95, 0x64, 0xB6, /// 0xBB, 0x06, 0x92, 0x02, 0x34, 0x2D, 0x36, 0x35, /// 0xA4, 0x95, 0x30, 0x2E, 0x20, 0xD3, 0xE8, 0x53, /// 0x95, 0xA2, 0xDF, 0x83, 0x9B, 0x7B, 0x12, 0x3F]; /// /// assert_eq!(length, cipher.len(), "ERROR cipher.len()"); /// /// for i in 0..length { /// assert_eq!(expected_encrypted[i], cipher[i], "ERROR in encrypt {}", i); /// } /// /// // Notice: CFB only uses block-encryption, no matter we use it as encryption or decryption. /// // So, keep don't use functions which start with`setkey_dec_` and keep the next line commented out. /// //aes_core::setkey_dec_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::cfb_dec(&cipher, &mut dec_cipher, &w_keys, &iv); /// /// assert_eq!(length, dec_cipher.len(), "ERROR dec_cipher.len()"); /// for i in 0..length { /// assert_eq!(plain[i], dec_cipher[i], "ERROR in decrypt {}", i); /// } /// ``` pub fn cfb_enc(plain: &[u8], cipher: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { let encryptor = match keys.len() { 44 => aes_core::block_enc_k128, 52 => aes_core::block_enc_k192, 60 => aes_core::block_enc_k256, _ => panic!("Invalid key length."), }; let block_number: usize = plain.len() >> 4; let mut buffer: [u8; 16] = [0; 16]; // If input has only one block, consider it as the last block, not the 1st. // If input has only two blocks, consider it has no middle blocks. // The 1st (head) block encryptor(&iv, &mut buffer, keys); let mut start: usize = 0; if plain.len() >= 16 { for j in 0..16 { cipher[j] = buffer[j] ^ plain[j]; } // The middle blocks for i in 1usize..block_number { start = i << 4; encryptor(&cipher[(start - 16)..start], &mut buffer, keys); for j in 0..16 { cipher[start + j] = buffer[j] ^ plain[start + j]; } } } // The last (tail) block match plain.len() & 0b1111 { r if r != 0 => { if block_number != 0 { start = block_number << 4; encryptor(&cipher[(start - 16)..start], &mut buffer, keys); } for j in 0..r { cipher[start + j] = buffer[j] ^ plain[start + j]; } cipher[start..(start + r)].to_owned() }, _ => { cipher[start..(start + 16)].to_owned() }, } } /// CFB (Cipher Feedback) Decryption /// /// The feedback size is fixed to 128 bits, which is the same as block size. /// This mode doesn't require padding. /// /// This function decrypts a long cipher from the first parameter and put the long plain /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// Finally, it returns the final block of the cipher (NOT the plain). /// ![CFB decryption](https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/CFB_decryption.svg/1280px-CFB_decryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// # Examples /// Please refer to the [`cfb_enc`] function, codes are included there. /// /// [`cfb_enc`]: ../aes_with_operation_mode/fn.cfb_enc.html pub fn cfb_dec(cipher: &[u8], plain: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { // You may think this function is the same as the `cfb_enc` function, but in fact they differ // in the last line. Both functions return `&cipher[start..(start + 16)]`, which is the first // parameter in this function, while it's the second parameter in the `cfb_enc` function. let encryptor = match keys.len() { 44 => aes_core::block_enc_k128, 52 => aes_core::block_enc_k192, 60 => aes_core::block_enc_k256, _ => panic!("Invalid key length."), }; let block_number: usize = plain.len() >> 4; let mut buffer: [u8; 16] = [0; 16]; // The 1st (head) block encryptor(&iv, &mut buffer, keys); let mut start: usize = 0; if cipher.len() >= 16 { for j in 0..16 { plain[j] = buffer[j] ^ cipher[j] } // The middle blocks for i in 1usize..block_number { start = i << 4; encryptor(&cipher[(start - 16)..start], &mut buffer, keys); for j in 0..16 { plain[start + j] = buffer[j] ^ cipher[start + j]; } } } match cipher.len() & 0b1111 { // The last (tail) block r if r != 0 => { if block_number != 0 { start = block_number << 4; encryptor(&cipher[(start - 16)..start], &mut buffer, keys); } for j in 0..r { plain[start + j] = buffer[j] ^ cipher[start + j]; } cipher[start..(start + r)].to_owned() }, _ => { cipher[start..(start + 16)].to_owned() }, } } /// OFB (Output Feedback) Encryption and Decryption /// /// The feedback size is fixed to 128 bits, which is the same as block size. /// This mode doesn't require depadding if you didn't add padding when encrypting. /// /// This function encrypts a long plain from the first parameter and put the long cipher /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// However, if you use the corresponding [`setkey_dec_auto`], [`setkey_dec_k128`], [`setkey_dec_k192`] /// or [`setkey_dec_k256`] function to schedule the keys, and then use this function again, /// it will decrypt the first parameter into the second parameter. /// Finally, it returns the final block of the encryptor output (neither the plain nor cipher). /// ![OFB encryption](https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/OFB_encryption.svg/1280px-OFB_encryption.svg.png) /// ![OFB decryption](https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/OFB_decryption.svg/1280px-OFB_decryption.svg.png) /// (These pictures comes from the Wikimedia Commons) /// # Examples /// ``` /// use aes_frast::{aes_core, aes_with_operation_mode, padding_128bit}; /// let length: usize = 64; /// let mut plain: Vec<u8> = vec![0x34, 0x63, 0xD0, 0x89, 0x1D, 0x71, 0x4A, 0xB0, /// 0x08, 0x5D, 0x22, 0xE1, 0x8B, 0xFA, 0x77, 0xF0, /// 0xEB, 0xE4, 0xB8, 0x9E, 0xF0, 0x05, 0x32, 0x7D, /// 0x4F, 0xBD, 0x87, 0x69, 0x75, 0x76, 0x78, 0xAA, /// 0x3D, 0x24, 0x06, 0x0C, 0xA4, 0xA5, 0x8C, 0xA0, /// 0x21, 0x58, 0xB8, 0xA1, 0x86, 0xAD, 0xBB, 0x6D, /// 0x9E, 0x09, 0x1C, 0x47, 0x06, 0x25, 0x4B, 0x2E, /// 0x30, 0x53, 0x3A, 0x5F, 0xE9, 0xDF, 0x3A, 0x90]; /// let mut cipher = vec![0u8; length]; /// let mut dec_cipher = vec![0u8; length]; /// let mut o_key: Vec<u8> = vec![0x0F, 0x57, 0x9F, 0x79, 0x50, 0x99, 0x0A, 0xCE, /// 0x66, 0x72, 0xA8, 0x17, 0x95, 0x1F, 0xF6, 0x06, /// 0x24, 0x40, 0xDE, 0xF5, 0x08, 0xF1, 0x64, 0x34, /// 0xD6, 0xEF, 0xEF, 0xFD, 0x26, 0x23, 0x04, 0x95]; /// let mut w_keys: Vec<u32> = vec![0u32; 60]; /// let mut iv: Vec<u8> = vec![0x04, 0x7C, 0xF3, 0xEA, 0xE1, 0x76, 0x45, 0x85, /// 0x72, 0x52, 0x7B, 0xAA, 0x26, 0x0D, 0x65, 0xBB]; /// /// aes_core::setkey_enc_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::ofb_enc_dec(&plain, &mut cipher, &w_keys, &iv); /// /// let expected_encrypted = vec![0x9D, 0xF9, 0x3D, 0x71, 0xC1, 0x9E, 0x50, 0x22, /// 0x36, 0x35, 0xF1, 0xB6, 0xED, 0xA6, 0x86, 0x74, /// 0xE2, 0xB7, 0xA6, 0x41, 0x13, 0x87, 0xAB, 0x99, /// 0x74, 0xE0, 0xF9, 0x9B, 0xC9, 0x05, 0xCE, 0x63, /// 0x74, 0x54, 0x68, 0x8D, 0x7B, 0xDA, 0x6E, 0x9E, /// 0xF6, 0xA6, 0x12, 0xB3, 0xB3, 0x06, 0x79, 0x68, /// 0x62, 0x2E, 0x5A, 0xF9, 0x03, 0xE3, 0x5D, 0xEF, /// 0xA2, 0x4B, 0xA8, 0xC1, 0xD7, 0xEC, 0x3E, 0x1B]; /// /// assert_eq!(length, cipher.len(), "ERROR cipher.len()"); /// /// for i in 0..length { /// assert_eq!(expected_encrypted[i], cipher[i], "ERROR in encrypt {}", i); /// } /// /// // Notice: OFB only uses block-encryption, no matter we use it as encryption or decryption. /// // So, keep don't use functions which start with`setkey_dec_` and keep the next line commented out. /// //aes_core::setkey_dec_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::ofb_enc_dec(&cipher, &mut dec_cipher, &w_keys, &iv); /// /// assert_eq!(length, dec_cipher.len(), "ERROR dec_cipher.len()"); /// for i in 0..length { /// assert_eq!(plain[i], dec_cipher[i], "ERROR in decrypt {}", i); /// } /// ``` /// /// [`setkey_dec_auto`]: ../aes_core/fn.setkey_dec_auto.html /// [`setkey_dec_k128`]: ../aes_core/fn.setkey_dec_k128.html /// [`setkey_dec_k192`]: ../aes_core/fn.setkey_dec_k192.html /// [`setkey_dec_k256`]: ../aes_core/fn.setkey_dec_k256.html pub fn ofb_enc_dec(input: &[u8], output: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { let encryptor = match keys.len() { 44 => aes_core::block_enc_k128, 52 => aes_core::block_enc_k192, 60 => aes_core::block_enc_k256, _ => panic!("Invalid key length."), }; let block_number: usize = input.len() >> 4; let mut buffer_new = vec![0u8; 16]; let mut buffer_last = vec![0u8; 16]; // The 1st (head) block encryptor(&iv, &mut buffer_new, keys); let mut start: usize; if input.len() >= 16 { for j in 0..16 { output[j] = buffer_new[j] ^ input[j] } // The middle blocks for i in 1usize..block_number { start = i << 4; mem::swap(&mut buffer_new, &mut buffer_last); encryptor(&buffer_last, &mut buffer_new, keys); for j in 0..16 { output[start + j] = buffer_new[j] ^ input[start + j]; } } } else { buffer_new = Vec::from(iv); } match input.len() & 0b1111 { // The last (tail) block r if r != 0 => { start = block_number << 4; mem::swap(&mut buffer_new, &mut buffer_last); encryptor(&buffer_last, &mut buffer_new, keys); for j in 0..r { output[start + j] = buffer_new[j] ^ input[start + j]; } }, _ => {}, } buffer_new } /// PCBC (Propagating Cipher Block Chaining) Encryption (**Experimental**) /// /// This function encrypts a long plain from the first parameter and put the long cipher /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// Finally, it returns the XOR result of the final block of the cipher and the final block of the plain. /// ![PCBC encryption](https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PCBC_encryption.svg/1280px-PCBC_encryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// **[Attention!]** On a message encrypted in PCBC mode, if two adjacent ciphertext blocks /// are exchanged, this does not affect the decryption of subsequent blocks. /// # Examples /// ``` /// use aes_frast::{aes_core, aes_with_operation_mode, padding_128bit}; /// let length: usize = 64; /// let mut plain: Vec<u8> = vec![0x30, 0xA3, 0xBC, 0xA0, 0xBA, 0x57, 0xFB, 0x27, /// 0x2F, 0xE2, 0x31, 0xEC, 0xBB, 0x04, 0x5E, 0x93, /// 0x40, 0xA1, 0x19, 0xB6, 0xE2, 0x60, 0x4F, 0x74, /// 0x0E, 0xF2, 0xC0, 0x29, 0x1B, 0xF0, 0xF9, 0x9B, /// 0xFD, 0x90, 0xFD, 0x0D, 0x97, 0xF8, 0x9B, 0x91, /// 0x78, 0xF4, 0x90, 0x07, 0x66, 0x6C, 0xE8, 0x11, /// 0x5F, 0xFE, 0x6F, 0xE9, 0x4F, 0x75, 0xB0, 0xCE, /// 0x53, 0x48, 0x92, 0xC7, 0x96, 0xD2, 0x0B, 0x3E]; /// let mut cipher = vec![0u8; length + 16]; /// let mut dec_cipher = vec![0u8; length + 16]; /// let mut o_key: Vec<u8> = vec![0x0F, 0x57, 0x9F, 0x79, 0x50, 0x99, 0x0A, 0xCE, /// 0x66, 0x72, 0xA8, 0x17, 0x95, 0x1F, 0xF6, 0x06, /// 0x24, 0x40, 0xDE, 0xF5, 0x08, 0xF1, 0x64, 0x34, /// 0xD6, 0xEF, 0xEF, 0xFD, 0x26, 0x23, 0x04, 0x95]; /// let mut w_keys: Vec<u32> = vec![0u32; 60]; /// let mut iv: Vec<u8> = vec![0x04, 0x7C, 0xF3, 0xEA, 0xE1, 0x76, 0x45, 0x85, /// 0x72, 0x52, 0x7B, 0xAA, 0x26, 0x0D, 0x65, 0xBB]; /// /// aes_core::setkey_enc_auto(&o_key, &mut w_keys); /// padding_128bit::pa_pkcs7(&mut plain); /// aes_with_operation_mode::cbc_enc(&plain, &mut cipher, &w_keys, &iv); /// /// assert_eq!(length + 16, cipher.len()); /// /// aes_core::setkey_dec_auto(&o_key, &mut w_keys); /// aes_with_operation_mode::cbc_dec(&cipher, &mut dec_cipher, &w_keys, &iv); /// padding_128bit::de_ansix923_pkcs7(&mut dec_cipher); /// /// assert_eq!(length, dec_cipher.len()); /// for i in 0..length { /// assert_eq!(plain[i], dec_cipher[i]); /// } /// ``` pub fn pcbc_enc(plain: &[u8], cipher: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { let encryptor = match keys.len() { 44 => aes_core::block_enc_k128, 52 => aes_core::block_enc_k192, 60 => aes_core::block_enc_k256, _ => panic!("Invalid key length."), }; let mut buffer = vec![0u8; 16]; // The 1st (head) block for j in 0..16 { buffer[j] = iv[j] ^ plain[j]; } encryptor(&buffer, &mut cipher[0..16], keys); // The other blocks let block_number: usize = plain.len() >> 4; let mut start: usize = 0; for i in 1usize..block_number { start = i << 4; for j in 0..16 { buffer[j] = cipher[start + j - 16] ^ plain[start + j - 16] ^ plain[start + j]; } encryptor(&buffer, &mut cipher[start..(start + 16)], keys); } for j in 0..16 { buffer[j] = cipher[start + j] ^ plain[start + j]; } buffer } /// PCBC (Propagating Cipher Block Chaining) Decryption (**Experimental**) /// /// This function decrypts a long cipher from the first parameter and put the long plain /// into the second parameter, using the scheduled keys and the initialization vector (IV) /// in the third and fourth parameters. /// Finally, it returns the XOR result of the final block of the cipher and the final block of the plain. /// ![PCBC decryption](https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/PCBC_decryption.svg/1280px-PCBC_decryption.svg.png) /// (This picture comes from the Wikimedia Commons) /// **[Attention!]** On a message encrypted in PCBC mode, if two adjacent ciphertext blocks /// are exchanged, this does not affect the decryption of subsequent blocks. /// # Examples /// Please refer to the [`pcbc_enc`] function, codes are included there. /// /// [`pcbc_enc`]: ../aes_with_operation_mode/fn.pcbc_enc.html pub fn pcbc_dec(cipher: &[u8], plain: &mut [u8], keys: &[u32], iv: &[u8]) -> Vec<u8> { let decryptor = match keys.len() { 44 => aes_core::block_dec_k128, 52 => aes_core::block_dec_k192, 60 => aes_core::block_dec_k256, _ => panic!("Invalid key length."), }; let mut buffer = vec![0u8; 16]; // The 1st (head) block decryptor(&cipher[0..16], &mut buffer, keys); for j in 0..16 { plain[j] = iv[j] ^ buffer[j]; } // The other block let block_number: usize = cipher.len() >> 4; let mut start: usize = 0; for i in 1usize..block_number { start = i << 4; decryptor(&cipher[start..(start + 16)], &mut buffer, keys); for j in 0..16 { plain[start + j] = cipher[start + j - 16] ^ plain[start + j - 16] ^ buffer[j]; } } for j in 0..16 { buffer[j] = cipher[start + j] ^ plain[start + j]; } buffer }