cipher_tdes.h 6.2 KB

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