cipher_des.c 7.6 KB

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