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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <ipxe/crypto.h>
  27. #include <ipxe/cbc.h>
  28. /** @file
  29. *
  30. * Cipher-block chaining
  31. *
  32. */
  33. /**
  34. * XOR data blocks
  35. *
  36. * @v src Input data
  37. * @v dst Second input data and output data buffer
  38. * @v len Length of data
  39. */
  40. static void cbc_xor ( const void *src, void *dst, size_t len ) {
  41. const uint32_t *srcl = src;
  42. uint32_t *dstl = dst;
  43. unsigned int i;
  44. /* Assume that block sizes will always be dword-aligned, for speed */
  45. assert ( ( len % sizeof ( *srcl ) ) == 0 );
  46. for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
  47. dstl[i] ^= srcl[i];
  48. }
  49. /**
  50. * Encrypt data
  51. *
  52. * @v ctx Context
  53. * @v src Data to encrypt
  54. * @v dst Buffer for encrypted data
  55. * @v len Length of data
  56. * @v raw_cipher Underlying cipher algorithm
  57. * @v cbc_ctx CBC context
  58. */
  59. void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
  60. struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
  61. size_t blocksize = raw_cipher->blocksize;
  62. assert ( ( len % blocksize ) == 0 );
  63. while ( len ) {
  64. cbc_xor ( src, cbc_ctx, blocksize );
  65. cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
  66. memcpy ( cbc_ctx, dst, blocksize );
  67. dst += blocksize;
  68. src += blocksize;
  69. len -= blocksize;
  70. }
  71. }
  72. /**
  73. * Decrypt data
  74. *
  75. * @v ctx Context
  76. * @v src Data to decrypt
  77. * @v dst Buffer for decrypted data
  78. * @v len Length of data
  79. * @v raw_cipher Underlying cipher algorithm
  80. * @v cbc_ctx CBC context
  81. */
  82. void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
  83. struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
  84. size_t blocksize = raw_cipher->blocksize;
  85. uint8_t next_cbc_ctx[blocksize];
  86. assert ( ( len % blocksize ) == 0 );
  87. while ( len ) {
  88. memcpy ( next_cbc_ctx, src, blocksize );
  89. cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
  90. cbc_xor ( cbc_ctx, dst, blocksize );
  91. memcpy ( cbc_ctx, next_cbc_ctx, blocksize );
  92. dst += blocksize;
  93. src += blocksize;
  94. len -= blocksize;
  95. }
  96. }