cipher_des.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2019-2021 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. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  48. return NULL;
  49. }
  50. in->base.hw->copyctx(&ret->base, &in->base);
  51. return ret;
  52. }
  53. static void des_freectx(void *vctx)
  54. {
  55. PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
  56. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  57. OPENSSL_clear_free(ctx, sizeof(*ctx));
  58. }
  59. static int des_init(void *vctx, const unsigned char *key, size_t keylen,
  60. const unsigned char *iv, size_t ivlen,
  61. const OSSL_PARAM params[], int enc)
  62. {
  63. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  64. if (!ossl_prov_is_running())
  65. return 0;
  66. ctx->num = 0;
  67. ctx->bufsz = 0;
  68. ctx->enc = enc;
  69. if (iv != NULL) {
  70. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  71. return 0;
  72. } else if (ctx->iv_set) {
  73. /* reset IV to keep compatibility with 1.1.1 */
  74. memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
  75. }
  76. if (key != NULL) {
  77. if (keylen != ctx->keylen) {
  78. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  79. return 0;
  80. }
  81. if (!ctx->hw->init(ctx, key, keylen))
  82. return 0;
  83. }
  84. return ossl_cipher_generic_set_ctx_params(ctx, params);
  85. }
  86. static int des_einit(void *vctx, const unsigned char *key, size_t keylen,
  87. const unsigned char *iv, size_t ivlen,
  88. const OSSL_PARAM params[])
  89. {
  90. return des_init(vctx, key, keylen, iv, ivlen, params, 1);
  91. }
  92. static int des_dinit(void *vctx, const unsigned char *key, size_t keylen,
  93. const unsigned char *iv, size_t ivlen,
  94. const OSSL_PARAM params[])
  95. {
  96. return des_init(vctx, key, keylen, iv, ivlen, params, 0);
  97. }
  98. static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  99. {
  100. DES_cblock *deskey = ptr;
  101. size_t kl = ctx->keylen;
  102. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
  103. return 0;
  104. DES_set_odd_parity(deskey);
  105. return 1;
  106. }
  107. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(des)
  108. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  109. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(des)
  110. static int des_get_ctx_params(void *vctx, OSSL_PARAM params[])
  111. {
  112. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  113. OSSL_PARAM *p;
  114. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  115. return 0;
  116. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  117. if (p != NULL && !des_generatekey(ctx, p->data)) {
  118. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. #define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags, \
  124. kbits, blkbits, ivbits, block) \
  125. static OSSL_FUNC_cipher_newctx_fn type##_##lcmode##_newctx; \
  126. static void *des_##lcmode##_newctx(void *provctx) \
  127. { \
  128. return des_newctx(provctx, kbits, blkbits, ivbits, \
  129. EVP_CIPH_##UCMODE##_MODE, flags, \
  130. ossl_prov_cipher_hw_des_##lcmode()); \
  131. } \
  132. static OSSL_FUNC_cipher_get_params_fn des_##lcmode##_get_params; \
  133. static int des_##lcmode##_get_params(OSSL_PARAM params[]) \
  134. { \
  135. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  136. flags, kbits, blkbits, ivbits); \
  137. } \
  138. const OSSL_DISPATCH ossl_##des_##lcmode##_functions[] = { \
  139. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit }, \
  140. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit }, \
  141. { OSSL_FUNC_CIPHER_UPDATE, \
  142. (void (*)(void))ossl_cipher_generic_##block##_update }, \
  143. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##block##_final },\
  144. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  145. { OSSL_FUNC_CIPHER_NEWCTX, \
  146. (void (*)(void))des_##lcmode##_newctx }, \
  147. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx }, \
  148. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx }, \
  149. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  150. (void (*)(void))des_##lcmode##_get_params }, \
  151. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  152. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  153. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params }, \
  154. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  155. (void (*)(void))des_gettable_ctx_params }, \
  156. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  157. (void (*)(void))ossl_cipher_generic_set_ctx_params }, \
  158. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  159. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  160. { 0, NULL } \
  161. }
  162. /* ossl_des_ecb_functions */
  163. IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block);
  164. /* ossl_des_cbc_functions */
  165. IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block);
  166. /* ossl_des_ofb64_functions */
  167. IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream);
  168. /* ossl_des_cfb64_functions */
  169. IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream);
  170. /* ossl_des_cfb1_functions */
  171. IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream);
  172. /* ossl_des_cfb8_functions */
  173. IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream);