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