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

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