cipher_tdes_wrap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 1995-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 and SHA-1 low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/sha.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/proverr.h>
  17. #include "cipher_tdes_default.h"
  18. #include "crypto/evp.h"
  19. #include "crypto/sha.h"
  20. #include "prov/implementations.h"
  21. #include "prov/providercommon.h"
  22. #define TDES_WRAP_FLAGS PROV_CIPHER_FLAG_CUSTOM_IV | PROV_CIPHER_FLAG_RAND_KEY
  23. static OSSL_FUNC_cipher_update_fn tdes_wrap_update;
  24. static OSSL_FUNC_cipher_cipher_fn tdes_wrap_cipher;
  25. static const unsigned char wrap_iv[8] =
  26. {
  27. 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
  28. };
  29. static int des_ede3_unwrap(PROV_CIPHER_CTX *ctx, unsigned char *out,
  30. const unsigned char *in, size_t inl)
  31. {
  32. unsigned char icv[8], iv[TDES_IVLEN], sha1tmp[SHA_DIGEST_LENGTH];
  33. int rv = -1;
  34. if (inl < 24)
  35. return -1;
  36. if (out == NULL)
  37. return inl - 16;
  38. memcpy(ctx->iv, wrap_iv, 8);
  39. /* Decrypt first block which will end up as icv */
  40. ctx->hw->cipher(ctx, icv, in, 8);
  41. /* Decrypt central blocks */
  42. /*
  43. * If decrypting in place move whole output along a block so the next
  44. * des_ede_cbc_cipher is in place.
  45. */
  46. if (out == in) {
  47. memmove(out, out + 8, inl - 8);
  48. in -= 8;
  49. }
  50. ctx->hw->cipher(ctx, out, in + 8, inl - 16);
  51. /* Decrypt final block which will be IV */
  52. ctx->hw->cipher(ctx, iv, in + inl - 8, 8);
  53. /* Reverse order of everything */
  54. BUF_reverse(icv, NULL, 8);
  55. BUF_reverse(out, NULL, inl - 16);
  56. BUF_reverse(ctx->iv, iv, 8);
  57. /* Decrypt again using new IV */
  58. ctx->hw->cipher(ctx, out, out, inl - 16);
  59. ctx->hw->cipher(ctx, icv, icv, 8);
  60. if (ossl_sha1(out, inl - 16, sha1tmp) /* Work out hash of first portion */
  61. && CRYPTO_memcmp(sha1tmp, icv, 8) == 0)
  62. rv = inl - 16;
  63. OPENSSL_cleanse(icv, 8);
  64. OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
  65. OPENSSL_cleanse(iv, 8);
  66. OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
  67. if (rv == -1)
  68. OPENSSL_cleanse(out, inl - 16);
  69. return rv;
  70. }
  71. static int des_ede3_wrap(PROV_CIPHER_CTX *ctx, unsigned char *out,
  72. const unsigned char *in, size_t inl)
  73. {
  74. unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  75. size_t ivlen = TDES_IVLEN;
  76. size_t icvlen = TDES_IVLEN;
  77. size_t len = inl + ivlen + icvlen;
  78. if (out == NULL)
  79. return len;
  80. /* Copy input to output buffer + 8 so we have space for IV */
  81. memmove(out + ivlen, in, inl);
  82. /* Work out ICV */
  83. if (!ossl_sha1(in, inl, sha1tmp))
  84. return 0;
  85. memcpy(out + inl + ivlen, sha1tmp, icvlen);
  86. OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
  87. /* Generate random IV */
  88. if (RAND_bytes_ex(ctx->libctx, ctx->iv, ivlen, 0) <= 0)
  89. return 0;
  90. memcpy(out, ctx->iv, ivlen);
  91. /* Encrypt everything after IV in place */
  92. ctx->hw->cipher(ctx, out + ivlen, out + ivlen, inl + ivlen);
  93. BUF_reverse(out, NULL, len);
  94. memcpy(ctx->iv, wrap_iv, ivlen);
  95. ctx->hw->cipher(ctx, out, out, len);
  96. return len;
  97. }
  98. static int tdes_wrap_cipher_internal(PROV_CIPHER_CTX *ctx, unsigned char *out,
  99. const unsigned char *in, size_t inl)
  100. {
  101. /*
  102. * Sanity check input length: we typically only wrap keys so EVP_MAXCHUNK
  103. * is more than will ever be needed. Also input length must be a multiple
  104. * of 8 bits.
  105. */
  106. if (inl >= EVP_MAXCHUNK || inl % 8)
  107. return -1;
  108. if (ctx->enc)
  109. return des_ede3_wrap(ctx, out, in, inl);
  110. else
  111. return des_ede3_unwrap(ctx, out, in, inl);
  112. }
  113. static int tdes_wrap_cipher(void *vctx,
  114. unsigned char *out, size_t *outl, size_t outsize,
  115. const unsigned char *in, size_t inl)
  116. {
  117. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  118. int ret;
  119. *outl = 0;
  120. if (!ossl_prov_is_running())
  121. return 0;
  122. if (outsize < inl) {
  123. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  124. return 0;
  125. }
  126. ret = tdes_wrap_cipher_internal(ctx, out, in, inl);
  127. if (ret <= 0)
  128. return 0;
  129. *outl = ret;
  130. return 1;
  131. }
  132. static int tdes_wrap_update(void *vctx, unsigned char *out, size_t *outl,
  133. size_t outsize, const unsigned char *in,
  134. size_t inl)
  135. {
  136. *outl = 0;
  137. if (inl == 0)
  138. return 1;
  139. if (outsize < inl) {
  140. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  141. return 0;
  142. }
  143. if (!tdes_wrap_cipher(vctx, out, outl, outsize, in, inl)) {
  144. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. # define IMPLEMENT_WRAP_CIPHER(flags, kbits, blkbits, ivbits) \
  150. static OSSL_FUNC_cipher_newctx_fn tdes_wrap_newctx; \
  151. static void *tdes_wrap_newctx(void *provctx) \
  152. { \
  153. return ossl_tdes_newctx(provctx, EVP_CIPH_WRAP_MODE, kbits, blkbits, \
  154. ivbits, flags, \
  155. ossl_prov_cipher_hw_tdes_wrap_cbc()); \
  156. } \
  157. static OSSL_FUNC_cipher_get_params_fn tdes_wrap_get_params; \
  158. static int tdes_wrap_get_params(OSSL_PARAM params[]) \
  159. { \
  160. return ossl_cipher_generic_get_params(params, EVP_CIPH_WRAP_MODE, flags, \
  161. kbits, blkbits, ivbits); \
  162. } \
  163. const OSSL_DISPATCH ossl_tdes_wrap_cbc_functions[] = \
  164. { \
  165. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) ossl_tdes_einit }, \
  166. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) ossl_tdes_dinit }, \
  167. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))tdes_wrap_cipher }, \
  168. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))tdes_wrap_newctx }, \
  169. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))ossl_tdes_freectx }, \
  170. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))tdes_wrap_update }, \
  171. { OSSL_FUNC_CIPHER_FINAL, \
  172. (void (*)(void))ossl_cipher_generic_stream_final }, \
  173. { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))tdes_wrap_get_params }, \
  174. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  175. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  176. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  177. (void (*)(void))ossl_tdes_get_ctx_params }, \
  178. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  179. (void (*)(void))ossl_tdes_gettable_ctx_params }, \
  180. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  181. (void (*)(void))ossl_cipher_generic_set_ctx_params }, \
  182. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  183. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  184. { 0, NULL } \
  185. }
  186. /* ossl_tdes_wrap_cbc_functions */
  187. IMPLEMENT_WRAP_CIPHER(TDES_WRAP_FLAGS, 64*3, 64, 0);