Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cbc_test.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2012 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. /** @file
  20. *
  21. * CBC self-tests
  22. *
  23. */
  24. /* Forcibly enable assertions */
  25. #undef NDEBUG
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <ipxe/crypto.h>
  30. #include "cbc_test.h"
  31. /**
  32. * Test CBC encryption
  33. *
  34. * @v cipher Cipher algorithm
  35. * @v key Key
  36. * @v key_len Length of key
  37. * @v iv Initialisation vector
  38. * @v plaintext Plaintext data
  39. * @v expected_ciphertext Expected ciphertext data
  40. * @v len Length of data
  41. * @ret ok Ciphertext is as expected
  42. */
  43. int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
  44. size_t key_len, const void *iv, const void *plaintext,
  45. const void *expected_ciphertext, size_t len ) {
  46. uint8_t ctx[ cipher->ctxsize ];
  47. uint8_t ciphertext[ len ];
  48. int rc;
  49. /* Initialise cipher */
  50. rc = cipher_setkey ( cipher, ctx, key, key_len );
  51. assert ( rc == 0 );
  52. cipher_setiv ( cipher, ctx, iv );
  53. /* Perform encryption */
  54. cipher_encrypt ( cipher, ctx, plaintext, ciphertext, len );
  55. /* Verify result */
  56. return ( memcmp ( ciphertext, expected_ciphertext, len ) == 0 );
  57. }
  58. /**
  59. * Test CBC decryption
  60. *
  61. * @v cipher Cipher algorithm
  62. * @v key Key
  63. * @v key_len Length of key
  64. * @v iv Initialisation vector
  65. * @v ciphertext Ciphertext data
  66. * @v expected_plaintext Expected plaintext data
  67. * @v len Length of data
  68. * @ret ok Plaintext is as expected
  69. */
  70. int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
  71. size_t key_len, const void *iv, const void *ciphertext,
  72. const void *expected_plaintext, size_t len ) {
  73. uint8_t ctx[ cipher->ctxsize ];
  74. uint8_t plaintext[ len ];
  75. int rc;
  76. /* Initialise cipher */
  77. rc = cipher_setkey ( cipher, ctx, key, key_len );
  78. assert ( rc == 0 );
  79. cipher_setiv ( cipher, ctx, iv );
  80. /* Perform encryption */
  81. cipher_decrypt ( cipher, ctx, ciphertext, plaintext, len );
  82. /* Verify result */
  83. return ( memcmp ( plaintext, expected_plaintext, len ) == 0 );
  84. }