You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cbc.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <ipxe/crypto.h>
  22. #include <ipxe/cbc.h>
  23. /** @file
  24. *
  25. * Cipher-block chaining
  26. *
  27. */
  28. /**
  29. * XOR data blocks
  30. *
  31. * @v src Input data
  32. * @v dst Second input data and output data buffer
  33. * @v len Length of data
  34. */
  35. static void cbc_xor ( const void *src, void *dst, size_t len ) {
  36. const uint32_t *srcl = src;
  37. uint32_t *dstl = dst;
  38. unsigned int i;
  39. /* Assume that block sizes will always be dword-aligned, for speed */
  40. assert ( ( len % sizeof ( *srcl ) ) == 0 );
  41. for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
  42. dstl[i] ^= srcl[i];
  43. }
  44. /**
  45. * Encrypt data
  46. *
  47. * @v ctx Context
  48. * @v src Data to encrypt
  49. * @v dst Buffer for encrypted data
  50. * @v len Length of data
  51. * @v raw_cipher Underlying cipher algorithm
  52. * @v cbc_ctx CBC context
  53. */
  54. void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
  55. struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
  56. size_t blocksize = raw_cipher->blocksize;
  57. assert ( ( len % blocksize ) == 0 );
  58. while ( len ) {
  59. cbc_xor ( src, cbc_ctx, blocksize );
  60. cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
  61. memcpy ( cbc_ctx, dst, blocksize );
  62. dst += blocksize;
  63. src += blocksize;
  64. len -= blocksize;
  65. }
  66. }
  67. /**
  68. * Decrypt data
  69. *
  70. * @v ctx Context
  71. * @v src Data to decrypt
  72. * @v dst Buffer for decrypted data
  73. * @v len Length of data
  74. * @v raw_cipher Underlying cipher algorithm
  75. * @v cbc_ctx CBC context
  76. */
  77. void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
  78. struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
  79. size_t blocksize = raw_cipher->blocksize;
  80. assert ( ( len % blocksize ) == 0 );
  81. while ( len ) {
  82. cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
  83. cbc_xor ( cbc_ctx, dst, blocksize );
  84. memcpy ( cbc_ctx, src, blocksize );
  85. dst += blocksize;
  86. src += blocksize;
  87. len -= blocksize;
  88. }
  89. }