cipher_tdes.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2019-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. #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. /* TODO(3.0) Figure out what flags need to be here */
  15. #define TDES_FLAGS (EVP_CIPH_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. } PROV_TDES_CTX;
  27. #define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \
  28. kbits, blkbits, ivbits, block) \
  29. static OSSL_FUNC_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \
  30. static void *tdes_##type##_##lcmode##_newctx(void *provctx) \
  31. { \
  32. return tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \
  33. ivbits, flags, \
  34. ossl_prov_cipher_hw_tdes_##type##_##lcmode()); \
  35. } \
  36. static OSSL_FUNC_cipher_get_params_fn tdes_##type##_##lcmode##_get_params; \
  37. static int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[]) \
  38. { \
  39. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  40. flags, kbits, blkbits, ivbits); \
  41. } \
  42. const OSSL_DISPATCH ossl_tdes_##type##_##lcmode##_functions[] = { \
  43. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))tdes_einit }, \
  44. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))tdes_dinit }, \
  45. { OSSL_FUNC_CIPHER_UPDATE, \
  46. (void (*)(void))ossl_cipher_generic_##block##_update }, \
  47. { OSSL_FUNC_CIPHER_FINAL, (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))tdes_dupctx }, \
  52. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))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, (void (*)(void))tdes_get_ctx_params }, \
  58. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  59. (void (*)(void))tdes_gettable_ctx_params }, \
  60. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  61. (void (*)(void))ossl_cipher_generic_set_ctx_params }, \
  62. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  63. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  64. { 0, NULL } \
  65. }
  66. void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
  67. size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw);
  68. OSSL_FUNC_cipher_dupctx_fn tdes_dupctx;
  69. OSSL_FUNC_cipher_freectx_fn tdes_freectx;
  70. OSSL_FUNC_cipher_encrypt_init_fn tdes_einit;
  71. OSSL_FUNC_cipher_decrypt_init_fn tdes_dinit;
  72. OSSL_FUNC_cipher_get_ctx_params_fn tdes_get_ctx_params;
  73. OSSL_FUNC_cipher_gettable_ctx_params_fn tdes_gettable_ctx_params;
  74. #define PROV_CIPHER_HW_tdes_mode(type, mode) \
  75. static const PROV_CIPHER_HW type##_##mode = { \
  76. ossl_cipher_hw_tdes_##type##_initkey, \
  77. ossl_cipher_hw_tdes_##mode, \
  78. ossl_cipher_hw_tdes_copyctx \
  79. }; \
  80. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_##type##_##mode(void) \
  81. { \
  82. return &type##_##mode; \
  83. }
  84. int ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx,
  85. const unsigned char *key, size_t keylen);
  86. void ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst,
  87. const PROV_CIPHER_CTX *src);
  88. int ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  89. const unsigned char *in, size_t inl);
  90. int ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  91. const unsigned char *in, size_t len);
  92. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_cbc(void);
  93. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_ecb(void);