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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <string.h>
  19. #include <assert.h>
  20. #include <gpxe/crypto.h>
  21. #include <gpxe/cbc.h>
  22. /** @file
  23. *
  24. * Cipher-block chaining
  25. *
  26. */
  27. /**
  28. * XOR data blocks
  29. *
  30. * @v src Input data
  31. * @v dst Second input data and output data buffer
  32. * @v len Length of data
  33. */
  34. static void cbc_xor ( const void *src, void *dst, size_t len ) {
  35. const uint32_t *srcl = src;
  36. uint32_t *dstl = dst;
  37. unsigned int i;
  38. /* Assume that block sizes will always be dword-aligned, for speed */
  39. assert ( ( len % sizeof ( *srcl ) ) == 0 );
  40. for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
  41. dstl[i] ^= srcl[i];
  42. }
  43. /**
  44. * Encrypt data
  45. *
  46. * @v ctx Context
  47. * @v src Data to encrypt
  48. * @v dst Buffer for encrypted data
  49. * @v len Length of data
  50. * @v cipher Underlying cipher algorithm
  51. * @v cbc_ctx CBC context
  52. */
  53. void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
  54. struct cipher_algorithm *cipher, void *cbc_ctx ) {
  55. size_t blocksize = cipher->blocksize;
  56. assert ( ( len % blocksize ) == 0 );
  57. while ( len ) {
  58. cbc_xor ( src, cbc_ctx, blocksize );
  59. cipher_encrypt ( cipher, ctx, cbc_ctx, dst, blocksize );
  60. memcpy ( cbc_ctx, dst, blocksize );
  61. dst += blocksize;
  62. src += blocksize;
  63. len -= blocksize;
  64. }
  65. }
  66. /**
  67. * Decrypt data
  68. *
  69. * @v ctx Context
  70. * @v src Data to decrypt
  71. * @v dst Buffer for decrypted data
  72. * @v len Length of data
  73. * @v cipher Underlying cipher algorithm
  74. * @v cbc_ctx CBC context
  75. */
  76. void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
  77. struct cipher_algorithm *cipher, void *cbc_ctx ) {
  78. size_t blocksize = cipher->blocksize;
  79. assert ( ( len % blocksize ) == 0 );
  80. while ( len ) {
  81. cipher_decrypt ( cipher, ctx, src, dst, blocksize );
  82. cbc_xor ( cbc_ctx, dst, blocksize );
  83. memcpy ( cbc_ctx, src, blocksize );
  84. dst += blocksize;
  85. src += blocksize;
  86. len -= blocksize;
  87. }
  88. }