cipher_tdes.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2019-2023 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 <openssl/des.h>
  10. #include <openssl/core_dispatch.h>
  11. #include "crypto/des_platform.h"
  12. #define DES_BLOCK_SIZE 8
  13. #define TDES_IVLEN 8
  14. #define TDES_FLAGS PROV_CIPHER_FLAG_RAND_KEY
  15. typedef struct prov_tdes_ctx_st {
  16. PROV_CIPHER_CTX base; /* Must be first */
  17. union {
  18. OSSL_UNION_ALIGN;
  19. DES_key_schedule ks[3];
  20. } tks;
  21. union {
  22. void (*cbc) (const void *, void *, size_t,
  23. const DES_key_schedule *, unsigned char *);
  24. } tstream;
  25. } PROV_TDES_CTX;
  26. #define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \
  27. kbits, blkbits, ivbits, block) \
  28. static OSSL_FUNC_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \
  29. static void *tdes_##type##_##lcmode##_newctx(void *provctx) \
  30. { \
  31. return ossl_tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \
  32. ivbits, flags, \
  33. ossl_prov_cipher_hw_tdes_##type##_##lcmode()); \
  34. } \
  35. static OSSL_FUNC_cipher_get_params_fn tdes_##type##_##lcmode##_get_params; \
  36. static int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[]) \
  37. { \
  38. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  39. flags, kbits, blkbits, ivbits); \
  40. } \
  41. const OSSL_DISPATCH ossl_tdes_##type##_##lcmode##_functions[] = { \
  42. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_tdes_einit }, \
  43. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_tdes_dinit }, \
  44. { OSSL_FUNC_CIPHER_UPDATE, \
  45. (void (*)(void))ossl_cipher_generic_##block##_update }, \
  46. { OSSL_FUNC_CIPHER_FINAL, \
  47. (void (*)(void))ossl_cipher_generic_##block##_final }, \
  48. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  49. { OSSL_FUNC_CIPHER_NEWCTX, \
  50. (void (*)(void))tdes_##type##_##lcmode##_newctx }, \
  51. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))ossl_tdes_dupctx }, \
  52. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))ossl_tdes_freectx }, \
  53. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  54. (void (*)(void))tdes_##type##_##lcmode##_get_params }, \
  55. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  56. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  57. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  58. (void (*)(void))ossl_tdes_get_ctx_params }, \
  59. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  60. (void (*)(void))ossl_tdes_gettable_ctx_params }, \
  61. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  62. (void (*)(void))ossl_cipher_generic_set_ctx_params }, \
  63. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  64. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  65. OSSL_DISPATCH_END \
  66. }
  67. void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
  68. size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw);
  69. OSSL_FUNC_cipher_dupctx_fn ossl_tdes_dupctx;
  70. OSSL_FUNC_cipher_freectx_fn ossl_tdes_freectx;
  71. OSSL_FUNC_cipher_encrypt_init_fn ossl_tdes_einit;
  72. OSSL_FUNC_cipher_decrypt_init_fn ossl_tdes_dinit;
  73. OSSL_FUNC_cipher_get_ctx_params_fn ossl_tdes_get_ctx_params;
  74. OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_tdes_gettable_ctx_params;
  75. #define PROV_CIPHER_HW_tdes_mode(type, mode) \
  76. static const PROV_CIPHER_HW type##_##mode = { \
  77. ossl_cipher_hw_tdes_##type##_initkey, \
  78. ossl_cipher_hw_tdes_##mode, \
  79. ossl_cipher_hw_tdes_copyctx \
  80. }; \
  81. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_##type##_##mode(void) \
  82. { \
  83. return &type##_##mode; \
  84. }
  85. int ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx,
  86. const unsigned char *key, size_t keylen);
  87. void ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst,
  88. const PROV_CIPHER_CTX *src);
  89. int ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  90. const unsigned char *in, size_t inl);
  91. int ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  92. const unsigned char *in, size_t len);
  93. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_cbc(void);
  94. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_ecb(void);