250-eng_devcrypto-fix-ctr-mode.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. From 2887a5c8f9a385b3ebee12b98f68e7d1f9cc0ea0 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <[email protected]>
  3. Date: Wed, 28 Nov 2018 11:26:27 -0200
  4. Subject: [PATCH 6/7] eng_devcrypto: fix ctr mode
  5. Make CTR mode behave like a stream cipher.
  6. Signed-off-by: Eneas U de Queiroz <[email protected]>
  7. Reviewed-by: Matthias St. Pierre <[email protected]>
  8. Reviewed-by: Richard Levitte <[email protected]>
  9. (Merged from https://github.com/openssl/openssl/pull/7585)
  10. (cherry picked from commit b5015e834aa7d3f0a5d7585a8fae05cecbdbb848)
  11. --- a/crypto/engine/eng_devcrypto.c
  12. +++ b/crypto/engine/eng_devcrypto.c
  13. @@ -47,10 +47,12 @@ static int cfd;
  14. struct cipher_ctx {
  15. struct session_op sess;
  16. -
  17. - /* to pass from init to do_cipher */
  18. - const unsigned char *iv;
  19. int op; /* COP_ENCRYPT or COP_DECRYPT */
  20. + unsigned long mode; /* EVP_CIPH_*_MODE */
  21. +
  22. + /* to handle ctr mode being a stream cipher */
  23. + unsigned char partial[EVP_MAX_BLOCK_LENGTH];
  24. + unsigned int blocksize, num;
  25. };
  26. static const struct cipher_data_st {
  27. @@ -87,9 +89,9 @@ static const struct cipher_data_st {
  28. { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  29. #endif
  30. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
  31. - { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  32. - { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  33. - { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  34. + { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  35. + { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  36. + { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  37. #endif
  38. #if 0 /* Not yet supported */
  39. { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  40. @@ -146,6 +148,8 @@ static int cipher_init(EVP_CIPHER_CTX *c
  41. cipher_ctx->sess.keylen = cipher_d->keylen;
  42. cipher_ctx->sess.key = (void *)key;
  43. cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
  44. + cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
  45. + cipher_ctx->blocksize = cipher_d->blocksize;
  46. if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
  47. SYSerr(SYS_F_IOCTL, errno);
  48. return 0;
  49. @@ -160,8 +164,11 @@ static int cipher_do_cipher(EVP_CIPHER_C
  50. struct cipher_ctx *cipher_ctx =
  51. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  52. struct crypt_op cryp;
  53. + unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
  54. #if !defined(COP_FLAG_WRITE_IV)
  55. unsigned char saved_iv[EVP_MAX_IV_LENGTH];
  56. + const unsigned char *ivptr;
  57. + size_t nblocks, ivlen;
  58. #endif
  59. memset(&cryp, 0, sizeof(cryp));
  60. @@ -169,19 +176,28 @@ static int cipher_do_cipher(EVP_CIPHER_C
  61. cryp.len = inl;
  62. cryp.src = (void *)in;
  63. cryp.dst = (void *)out;
  64. - cryp.iv = (void *)EVP_CIPHER_CTX_iv_noconst(ctx);
  65. + cryp.iv = (void *)iv;
  66. cryp.op = cipher_ctx->op;
  67. #if !defined(COP_FLAG_WRITE_IV)
  68. cryp.flags = 0;
  69. - if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
  70. - assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
  71. - if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  72. - unsigned char *ivptr = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
  73. + ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  74. + if (ivlen > 0)
  75. + switch (cipher_ctx->mode) {
  76. + case EVP_CIPH_CBC_MODE:
  77. + assert(inl >= ivlen);
  78. + if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  79. + ivptr = in + inl - ivlen;
  80. + memcpy(saved_iv, ivptr, ivlen);
  81. + }
  82. + break;
  83. +
  84. + case EVP_CIPH_CTR_MODE:
  85. + break;
  86. - memcpy(saved_iv, ivptr, EVP_CIPHER_CTX_iv_length(ctx));
  87. + default: /* should not happen */
  88. + return 0;
  89. }
  90. - }
  91. #else
  92. cryp.flags = COP_FLAG_WRITE_IV;
  93. #endif
  94. @@ -192,17 +208,74 @@ static int cipher_do_cipher(EVP_CIPHER_C
  95. }
  96. #if !defined(COP_FLAG_WRITE_IV)
  97. - if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
  98. - unsigned char *ivptr = saved_iv;
  99. + if (ivlen > 0)
  100. + switch (cipher_ctx->mode) {
  101. + case EVP_CIPH_CBC_MODE:
  102. + assert(inl >= ivlen);
  103. + if (EVP_CIPHER_CTX_encrypting(ctx))
  104. + ivptr = out + inl - ivlen;
  105. + else
  106. + ivptr = saved_iv;
  107. +
  108. + memcpy(iv, ivptr, ivlen);
  109. + break;
  110. +
  111. + case EVP_CIPH_CTR_MODE:
  112. + nblocks = (inl + cipher_ctx->blocksize - 1)
  113. + / cipher_ctx->blocksize;
  114. + do {
  115. + ivlen--;
  116. + nblocks += iv[ivlen];
  117. + iv[ivlen] = (uint8_t) nblocks;
  118. + nblocks >>= 8;
  119. + } while (ivlen);
  120. + break;
  121. +
  122. + default: /* should not happen */
  123. + return 0;
  124. + }
  125. +#endif
  126. +
  127. + return 1;
  128. +}
  129. - assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
  130. - if (!EVP_CIPHER_CTX_encrypting(ctx))
  131. - ivptr = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
  132. +static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  133. + const unsigned char *in, size_t inl)
  134. +{
  135. + struct cipher_ctx *cipher_ctx =
  136. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  137. + size_t nblocks, len;
  138. - memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), ivptr,
  139. - EVP_CIPHER_CTX_iv_length(ctx));
  140. + /* initial partial block */
  141. + while (cipher_ctx->num && inl) {
  142. + (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
  143. + --inl;
  144. + cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
  145. + }
  146. +
  147. + /* full blocks */
  148. + if (inl > (unsigned int) cipher_ctx->blocksize) {
  149. + nblocks = inl/cipher_ctx->blocksize;
  150. + len = nblocks * cipher_ctx->blocksize;
  151. + if (cipher_do_cipher(ctx, out, in, len) < 1)
  152. + return 0;
  153. + inl -= len;
  154. + out += len;
  155. + in += len;
  156. + }
  157. +
  158. + /* final partial block */
  159. + if (inl) {
  160. + memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
  161. + if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
  162. + cipher_ctx->blocksize) < 1)
  163. + return 0;
  164. + while (inl--) {
  165. + out[cipher_ctx->num] = in[cipher_ctx->num]
  166. + ^ cipher_ctx->partial[cipher_ctx->num];
  167. + cipher_ctx->num++;
  168. + }
  169. }
  170. -#endif
  171. return 1;
  172. }
  173. @@ -249,6 +322,7 @@ static void prepare_cipher_methods(void)
  174. {
  175. size_t i;
  176. struct session_op sess;
  177. + unsigned long cipher_mode;
  178. memset(&sess, 0, sizeof(sess));
  179. sess.key = (void *)"01234567890123456789012345678901234567890123456789";
  180. @@ -266,9 +340,12 @@ static void prepare_cipher_methods(void)
  181. || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
  182. continue;
  183. + cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
  184. +
  185. if ((known_cipher_methods[i] =
  186. EVP_CIPHER_meth_new(cipher_data[i].nid,
  187. - cipher_data[i].blocksize,
  188. + cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
  189. + cipher_data[i].blocksize,
  190. cipher_data[i].keylen)) == NULL
  191. || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
  192. cipher_data[i].ivlen)
  193. @@ -278,6 +355,8 @@ static void prepare_cipher_methods(void)
  194. | EVP_CIPH_FLAG_DEFAULT_ASN1)
  195. || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
  196. || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
  197. + cipher_mode == EVP_CIPH_CTR_MODE ?
  198. + ctr_do_cipher :
  199. cipher_do_cipher)
  200. || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
  201. || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],