rc2test.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * RC2 low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/nelem.h"
  15. #include "testutil.h"
  16. #ifndef OPENSSL_NO_RC2
  17. # include <openssl/rc2.h>
  18. static unsigned char RC2key[4][16] = {
  19. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  21. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
  23. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  25. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  26. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
  27. };
  28. static unsigned char RC2plain[4][8] = {
  29. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  30. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  31. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  32. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  33. };
  34. static unsigned char RC2cipher[4][8] = {
  35. {0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7},
  36. {0x21, 0x82, 0x9C, 0x78, 0xA9, 0xF9, 0xC0, 0x74},
  37. {0x13, 0xDB, 0x35, 0x17, 0xD3, 0x21, 0x86, 0x9E},
  38. {0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31},
  39. };
  40. static int test_rc2(const int n)
  41. {
  42. int testresult = 1;
  43. RC2_KEY key;
  44. unsigned char buf[8], buf2[8];
  45. RC2_set_key(&key, 16, &(RC2key[n][0]), 0 /* or 1024 */ );
  46. RC2_ecb_encrypt(&RC2plain[n][0], buf, &key, RC2_ENCRYPT);
  47. if (!TEST_mem_eq(&RC2cipher[n][0], 8, buf, 8))
  48. testresult = 0;
  49. RC2_ecb_encrypt(buf, buf2, &key, RC2_DECRYPT);
  50. if (!TEST_mem_eq(&RC2plain[n][0], 8, buf2, 8))
  51. testresult = 0;
  52. return testresult;
  53. }
  54. #endif
  55. int setup_tests(void)
  56. {
  57. #ifndef OPENSSL_NO_RC2
  58. ADD_ALL_TESTS(test_rc2, OSSL_NELEM(RC2key));
  59. #endif
  60. return 1;
  61. }