threefish_api.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <linux/string.h>
  2. #include "threefish_api.h"
  3. void threefish_set_key(struct threefish_key *key_ctx,
  4. enum threefish_size state_size,
  5. u64 *key_data, u64 *tweak)
  6. {
  7. int key_words = state_size / 64;
  8. int i;
  9. u64 parity = KEY_SCHEDULE_CONST;
  10. key_ctx->tweak[0] = tweak[0];
  11. key_ctx->tweak[1] = tweak[1];
  12. key_ctx->tweak[2] = tweak[0] ^ tweak[1];
  13. for (i = 0; i < key_words; i++) {
  14. key_ctx->key[i] = key_data[i];
  15. parity ^= key_data[i];
  16. }
  17. key_ctx->key[i] = parity;
  18. key_ctx->state_size = state_size;
  19. }
  20. void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
  21. u8 *out)
  22. {
  23. u64 plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/
  24. u64 cipher[SKEIN_MAX_STATE_WORDS];
  25. skein_get64_lsb_first(plain, in, key_ctx->state_size / 64);
  26. threefish_encrypt_block_words(key_ctx, plain, cipher);
  27. skein_put64_lsb_first(out, cipher, key_ctx->state_size / 8);
  28. }
  29. void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in,
  30. u64 *out)
  31. {
  32. switch (key_ctx->state_size) {
  33. case THREEFISH_256:
  34. threefish_encrypt_256(key_ctx, in, out);
  35. break;
  36. case THREEFISH_512:
  37. threefish_encrypt_512(key_ctx, in, out);
  38. break;
  39. case THREEFISH_1024:
  40. threefish_encrypt_1024(key_ctx, in, out);
  41. break;
  42. }
  43. }
  44. void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
  45. u8 *out)
  46. {
  47. u64 plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/
  48. u64 cipher[SKEIN_MAX_STATE_WORDS];
  49. skein_get64_lsb_first(cipher, in, key_ctx->state_size / 64);
  50. threefish_decrypt_block_words(key_ctx, cipher, plain);
  51. skein_put64_lsb_first(out, plain, key_ctx->state_size / 8);
  52. }
  53. void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in,
  54. u64 *out)
  55. {
  56. switch (key_ctx->state_size) {
  57. case THREEFISH_256:
  58. threefish_decrypt_256(key_ctx, in, out);
  59. break;
  60. case THREEFISH_512:
  61. threefish_decrypt_512(key_ctx, in, out);
  62. break;
  63. case THREEFISH_1024:
  64. threefish_decrypt_1024(key_ctx, in, out);
  65. break;
  66. }
  67. }