cipher_idea_hw.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2019 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. #include "cipher_idea.h"
  10. static int cipher_hw_idea_initkey(PROV_CIPHER_CTX *ctx,
  11. const unsigned char *key, size_t keylen)
  12. {
  13. PROV_IDEA_CTX *ictx = (PROV_IDEA_CTX *)ctx;
  14. IDEA_KEY_SCHEDULE *ks = &(ictx->ks.ks);
  15. if (ctx->enc
  16. || ctx->mode == EVP_CIPH_OFB_MODE
  17. || ctx->mode == EVP_CIPH_CFB_MODE) {
  18. IDEA_set_encrypt_key(key, ks);
  19. } else {
  20. IDEA_KEY_SCHEDULE tmp;
  21. IDEA_set_encrypt_key(key, &tmp);
  22. IDEA_set_decrypt_key(&tmp, ks);
  23. OPENSSL_cleanse((unsigned char *)&tmp, sizeof(IDEA_KEY_SCHEDULE));
  24. }
  25. return 1;
  26. }
  27. # define PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, fname) \
  28. IMPLEMENT_CIPHER_HW_##UCMODE(mode, idea, PROV_IDEA_CTX, IDEA_KEY_SCHEDULE, \
  29. fname) \
  30. static const PROV_CIPHER_HW idea_##mode = { \
  31. cipher_hw_idea_initkey, \
  32. cipher_hw_idea_##mode##_cipher \
  33. }; \
  34. const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_##mode(size_t keybits) \
  35. { \
  36. return &idea_##mode; \
  37. }
  38. # define PROV_CIPHER_HW_idea_mode(mode, UCMODE) \
  39. PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, IDEA_##mode)
  40. PROV_CIPHER_HW_idea_mode(cbc, CBC)
  41. PROV_CIPHER_HW_idea_mode(ofb64, OFB)
  42. PROV_CIPHER_HW_idea_mode(cfb64, CFB)
  43. /*
  44. * IDEA_ecb_encrypt() does not have a enc parameter - so we create a macro
  45. * that ignores this parameter when IMPLEMENT_CIPHER_HW_ecb() is called.
  46. */
  47. #define IDEA2_ecb_encrypt(in, out, ks, enc) IDEA_ecb_encrypt(in, out, ks)
  48. PROV_CIPHER_HW_idea_mode_ex(ecb, ECB, IDEA2_ecb)