Advertisement
AnthonyCagliano

Untitled

Aug 2nd, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 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. blocks = ((in_len - bytes_offset) / AES_BLOCK_SIZE);
  11. }
  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); // if bytes_to_copy is less than blocksize, do nothing, since msg is truncated anyway
  18. aes_encrypt_block(iv, buf, ctx);
  19. xor_buf(buf, &out[idx*AES_BLOCK_SIZE], bytes_to_copy);
  20. increment_iv(iv, ctx->mode.ctr.counter_len); // increment iv for continued use
  21. if(idx==blocks){
  22. memcpy(ctx->mode.ctr.last_block, buf, AES_BLOCK_SIZE);
  23. ctx->mode.ctr.last_block_stop = bytes_to_copy;
  24. }
  25. }
  26. break;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement