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_test.c 2.7KB

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