Advertisement
AnthonyCagliano

Untitled

Aug 2nd, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. case AES_CTR:
  2. {
  3. size_t bytes_to_copy = ctx->mode.ctr.last_block_stop;
  4. size_t bytes_offset = AES_BLOCK_SIZE - bytes_to_copy;
  5.  
  6. // xor remaining bytes from last encrypt operation into new plaintext
  7. if(bytes_to_copy){
  8. memcpy(out, in, bytes_offset);
  9. xor_buf(&ctx->mode.ctr.last_block[bytes_to_copy], out, bytes_offset);
  10. }
  11. blocks = ((in_len - bytes_offset) / AES_BLOCK_SIZE);
  12. // encrypt message in CTR mode
  13. for(idx = 0; idx <= blocks; idx++){
  14. bytes_to_copy = MIN(AES_BLOCK_SIZE, in_len - (idx * AES_BLOCK_SIZE));
  15. //bytes_to_pad = AES_BLOCK_SIZE-bytes_to_copy;
  16. memcpy(&out[idx*AES_BLOCK_SIZE], &in[idx*AES_BLOCK_SIZE], bytes_to_copy);
  17. // memset(&buf[bytes_to_copy], 0, bytes_to_pad);
  18. // if bytes_to_copy is less than blocksize, do nothing, since msg is truncated anyway
  19. aes_encrypt_block(iv, buf, ctx);
  20. xor_buf(buf, &out[idx*AES_BLOCK_SIZE], bytes_to_copy);
  21. increment_iv(iv, ctx->mode.ctr.counter_len); // increment iv for continued use
  22. if(idx==blocks){
  23. memcpy(ctx->mode.ctr.last_block, buf, AES_BLOCK_SIZE);
  24. ctx->mode.ctr.last_block_stop = bytes_to_copy;
  25. }
  26. }
  27. break;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement