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