cipher_des.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /*
  10. * DES low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/rand.h>
  15. #include <openssl/proverr.h>
  16. #include "prov/ciphercommon.h"
  17. #include "cipher_des.h"
  18. #include "prov/implementations.h"
  19. #include "prov/providercommon.h"
  20. #define DES_FLAGS PROV_CIPHER_FLAG_RAND_KEY
  21. static OSSL_FUNC_cipher_freectx_fn des_freectx;
  22. static OSSL_FUNC_cipher_encrypt_init_fn des_einit;
  23. static OSSL_FUNC_cipher_decrypt_init_fn des_dinit;
  24. static OSSL_FUNC_cipher_get_ctx_params_fn des_get_ctx_params;
  25. static OSSL_FUNC_cipher_gettable_ctx_params_fn des_gettable_ctx_params;
  26. static void *des_newctx(void *provctx, size_t kbits, size_t blkbits,
  27. size_t ivbits, unsigned int mode, uint64_t flags,
  28. const PROV_CIPHER_HW *hw)
  29. {
  30. PROV_DES_CTX *ctx;
  31. if (!ossl_prov_is_running())
  32. return NULL;
  33. ctx = OPENSSL_zalloc(sizeof(*ctx));
  34. if (ctx != NULL)
  35. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
  36. hw, provctx);
  37. return ctx;
  38. }
  39. static void *des_dupctx(void *ctx)
  40. {
  41. PROV_DES_CTX *in = (PROV_DES_CTX *)ctx;
  42. PROV_DES_CTX *ret;
  43. if (!ossl_prov_is_running())
  44. return NULL;
  45. ret = OPENSSL_malloc(sizeof(*ret));
  46. if (ret == NULL)
  47. return NULL;
  48. in->base.hw->copyctx(&ret->base, &in->base);
  49. return ret;
  50. }
  51. static void des_freectx(void *vctx)
  52. {
  53. PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
  54. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  55. OPENSSL_clear_free(ctx, sizeof(*ctx));
  56. }
  57. static int des_init(void *vctx, const unsigned char *key, size_t keylen,
  58. const unsigned char *iv, size_t ivlen,
  59. const OSSL_PARAM params[], int enc)
  60. {
  61. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  62. if (!ossl_prov_is_running())
  63. return 0;
  64. ctx->num = 0;
  65. ctx->bufsz = 0;
  66. ctx->enc = enc;
  67. if (iv != NULL) {
  68. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  69. return 0;
  70. } else if (ctx->iv_set) {
  71. /* reset IV to keep compatibility with 1.1.1 */
  72. memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
  73. }
  74. if (key != NULL) {
  75. if (keylen != ctx->keylen) {
  76. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  77. return 0;
  78. }
  79. if (!ctx->hw->init(ctx, key, keylen))
  80. return 0;
  81. ctx->key_set = 1;
  82. }
  83. return ossl_cipher_generic_set_ctx_params(ctx, params);
  84. }
  85. static int des_einit(void *vctx, const unsigned char *key, size_t keylen,
  86. const unsigned char *iv, size_t ivlen,
  87. const OSSL_PARAM params[])
  88. {
  89. return des_init(vctx, key, keylen, iv, ivlen, params, 1);
  90. }
  91. static int des_dinit(void *vctx, const unsigned char *key, size_t keylen,
  92. const unsigned char *iv, size_t ivlen,
  93. const OSSL_PARAM params[])
  94. {
  95. return des_init(vctx, key, keylen, iv, ivlen, params, 0);
  96. }
  97. static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  98. {
  99. DES_cblock *deskey = ptr;
  100. size_t kl = ctx->keylen;
  101. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
  102. return 0;
  103. DES_set_odd_parity(deskey);
  104. return 1;
  105. }
  106. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(des)
  107. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  108. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(des)
  109. static int des_get_ctx_params(void *vctx, OSSL_PARAM params[])
  110. {
  111. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  112. OSSL_PARAM *p;
  113. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  114. return 0;
  115. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  116. if (p != NULL && !des_generatekey(ctx, p->data)) {
  117. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. #define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags, \
  123. kbits, blkbits, ivbits, block) \
  124. static OSSL_FUNC_cipher_newctx_fn type##_##lcmode##_newctx; \
  125. static void *des_##lcmode##_newctx(void *provctx) \
  126. { \
  127. return des_newctx(provctx, kbits, blkbits, ivbits, \
  128. EVP_CIPH_##UCMODE##_MODE, flags, \
  129. ossl_prov_cipher_hw_des_##lcmode()); \
  130. } \
  131. static OSSL_FUNC_cipher_get_params_fn des_##lcmode##_get_params; \
  132. static int des_##lcmode##_get_params(OSSL_PARAM params[]) \
  133. { \
  134. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  135. flags, kbits, blkbits, ivbits); \
  136. } \
  137. const OSSL_DISPATCH ossl_##des_##lcmode##_functions[] = { \
  138. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit }, \
  139. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit }, \
  140. { OSSL_FUNC_CIPHER_UPDATE, \
  141. (void (*)(void))ossl_cipher_generic_##block##_update }, \
  142. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##block##_final },\
  143. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  144. { OSSL_FUNC_CIPHER_NEWCTX, \
  145. (void (*)(void))des_##lcmode##_newctx }, \
  146. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx }, \
  147. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx }, \
  148. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  149. (void (*)(void))des_##lcmode##_get_params }, \
  150. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  151. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  152. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params }, \
  153. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  154. (void (*)(void))des_gettable_ctx_params }, \
  155. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  156. (void (*)(void))ossl_cipher_generic_set_ctx_params }, \
  157. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  158. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  159. OSSL_DISPATCH_END \
  160. }
  161. /* ossl_des_ecb_functions */
  162. IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block);
  163. /* ossl_des_cbc_functions */
  164. IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block);
  165. /* ossl_des_ofb64_functions */
  166. IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream);
  167. /* ossl_des_cfb64_functions */
  168. IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream);
  169. /* ossl_des_cfb1_functions */
  170. IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream);
  171. /* ossl_des_cfb8_functions */
  172. IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream);