/src/openssl31/crypto/modes/ctr128.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2008-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  |  | #include <string.h>  | 
11  |  | #include <openssl/crypto.h>  | 
12  |  | #include "internal/endian.h"  | 
13  |  | #include "crypto/modes.h"  | 
14  |  |  | 
15  |  | #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)  | 
16  |  | typedef size_t size_t_aX __attribute((__aligned__(1)));  | 
17  |  | #else  | 
18  |  | typedef size_t size_t_aX;  | 
19  |  | #endif  | 
20  |  |  | 
21  |  | /*  | 
22  |  |  * NOTE: the IV/counter CTR mode is big-endian.  The code itself is  | 
23  |  |  * endian-neutral.  | 
24  |  |  */  | 
25  |  |  | 
26  |  | /* increment counter (128-bit int) by 1 */  | 
27  |  | static void ctr128_inc(unsigned char *counter)  | 
28  | 0  | { | 
29  | 0  |     u32 n = 16, c = 1;  | 
30  |  | 
  | 
31  | 0  |     do { | 
32  | 0  |         --n;  | 
33  | 0  |         c += counter[n];  | 
34  | 0  |         counter[n] = (u8)c;  | 
35  | 0  |         c >>= 8;  | 
36  | 0  |     } while (n);  | 
37  | 0  | }  | 
38  |  |  | 
39  |  | #if !defined(OPENSSL_SMALL_FOOTPRINT)  | 
40  |  | static void ctr128_inc_aligned(unsigned char *counter)  | 
41  | 0  | { | 
42  | 0  |     size_t *data, c, d, n;  | 
43  | 0  |     DECLARE_IS_ENDIAN;  | 
44  |  | 
  | 
45  | 0  |     if (IS_LITTLE_ENDIAN || ((size_t)counter % sizeof(size_t)) != 0) { | 
46  | 0  |         ctr128_inc(counter);  | 
47  | 0  |         return;  | 
48  | 0  |     }  | 
49  |  |  | 
50  | 0  |     data = (size_t *)counter;  | 
51  | 0  |     c = 1;  | 
52  | 0  |     n = 16 / sizeof(size_t);  | 
53  | 0  |     do { | 
54  | 0  |         --n;  | 
55  | 0  |         d = data[n] += c;  | 
56  |  |         /* did addition carry? */  | 
57  | 0  |         c = ((d - c) & ~d) >> (sizeof(size_t) * 8 - 1);  | 
58  | 0  |     } while (n);  | 
59  | 0  | }  | 
60  |  | #endif  | 
61  |  |  | 
62  |  | /*  | 
63  |  |  * The input encrypted as though 128bit counter mode is being used.  The  | 
64  |  |  * extra state information to record how much of the 128bit block we have  | 
65  |  |  * used is contained in *num, and the encrypted counter is kept in  | 
66  |  |  * ecount_buf.  Both *num and ecount_buf must be initialised with zeros  | 
67  |  |  * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes  | 
68  |  |  * that the counter is in the x lower bits of the IV (ivec), and that the  | 
69  |  |  * application has full control over overflow and the rest of the IV.  This  | 
70  |  |  * implementation takes NO responsibility for checking that the counter  | 
71  |  |  * doesn't overflow into the rest of the IV when incremented.  | 
72  |  |  */  | 
73  |  | void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,  | 
74  |  |                            size_t len, const void *key,  | 
75  |  |                            unsigned char ivec[16],  | 
76  |  |                            unsigned char ecount_buf[16], unsigned int *num,  | 
77  |  |                            block128_f block)  | 
78  | 0  | { | 
79  | 0  |     unsigned int n;  | 
80  | 0  |     size_t l = 0;  | 
81  |  | 
  | 
82  | 0  |     n = *num;  | 
83  |  | 
  | 
84  | 0  | #if !defined(OPENSSL_SMALL_FOOTPRINT)  | 
85  | 0  |     if (16 % sizeof(size_t) == 0) { /* always true actually */ | 
86  | 0  |         do { | 
87  | 0  |             while (n && len) { | 
88  | 0  |                 *(out++) = *(in++) ^ ecount_buf[n];  | 
89  | 0  |                 --len;  | 
90  | 0  |                 n = (n + 1) % 16;  | 
91  | 0  |             }  | 
92  |  | 
  | 
93  | 0  | # if defined(STRICT_ALIGNMENT)  | 
94  | 0  |             if (((size_t)in | (size_t)out | (size_t)ecount_buf)  | 
95  | 0  |                 % sizeof(size_t) != 0)  | 
96  | 0  |                 break;  | 
97  | 0  | # endif  | 
98  | 0  |             while (len >= 16) { | 
99  | 0  |                 (*block) (ivec, ecount_buf, key);  | 
100  | 0  |                 ctr128_inc_aligned(ivec);  | 
101  | 0  |                 for (n = 0; n < 16; n += sizeof(size_t))  | 
102  | 0  |                     *(size_t_aX *)(out + n) =  | 
103  | 0  |                         *(size_t_aX *)(in + n)  | 
104  | 0  |                         ^ *(size_t_aX *)(ecount_buf + n);  | 
105  | 0  |                 len -= 16;  | 
106  | 0  |                 out += 16;  | 
107  | 0  |                 in += 16;  | 
108  | 0  |                 n = 0;  | 
109  | 0  |             }  | 
110  | 0  |             if (len) { | 
111  | 0  |                 (*block) (ivec, ecount_buf, key);  | 
112  | 0  |                 ctr128_inc_aligned(ivec);  | 
113  | 0  |                 while (len--) { | 
114  | 0  |                     out[n] = in[n] ^ ecount_buf[n];  | 
115  | 0  |                     ++n;  | 
116  | 0  |                 }  | 
117  | 0  |             }  | 
118  | 0  |             *num = n;  | 
119  | 0  |             return;  | 
120  | 0  |         } while (0);  | 
121  | 0  |     }  | 
122  |  |     /* the rest would be commonly eliminated by x86* compiler */  | 
123  | 0  | #endif  | 
124  | 0  |     while (l < len) { | 
125  | 0  |         if (n == 0) { | 
126  | 0  |             (*block) (ivec, ecount_buf, key);  | 
127  | 0  |             ctr128_inc(ivec);  | 
128  | 0  |         }  | 
129  | 0  |         out[l] = in[l] ^ ecount_buf[n];  | 
130  | 0  |         ++l;  | 
131  | 0  |         n = (n + 1) % 16;  | 
132  | 0  |     }  | 
133  |  | 
  | 
134  | 0  |     *num = n;  | 
135  | 0  | }  | 
136  |  |  | 
137  |  | /* increment upper 96 bits of 128-bit counter by 1 */  | 
138  |  | static void ctr96_inc(unsigned char *counter)  | 
139  | 0  | { | 
140  | 0  |     u32 n = 12, c = 1;  | 
141  |  | 
  | 
142  | 0  |     do { | 
143  | 0  |         --n;  | 
144  | 0  |         c += counter[n];  | 
145  | 0  |         counter[n] = (u8)c;  | 
146  | 0  |         c >>= 8;  | 
147  | 0  |     } while (n);  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,  | 
151  |  |                                  size_t len, const void *key,  | 
152  |  |                                  unsigned char ivec[16],  | 
153  |  |                                  unsigned char ecount_buf[16],  | 
154  |  |                                  unsigned int *num, ctr128_f func)  | 
155  | 81.6k  | { | 
156  | 81.6k  |     unsigned int n, ctr32;  | 
157  |  |  | 
158  | 81.6k  |    n = *num;  | 
159  |  |  | 
160  | 81.6k  |     while (n && len) { | 
161  | 0  |         *(out++) = *(in++) ^ ecount_buf[n];  | 
162  | 0  |         --len;  | 
163  | 0  |         n = (n + 1) % 16;  | 
164  | 0  |     }  | 
165  |  |  | 
166  | 81.6k  |     ctr32 = GETU32(ivec + 12);  | 
167  | 154k  |     while (len >= 16) { | 
168  | 73.0k  |         size_t blocks = len / 16;  | 
169  |  |         /*  | 
170  |  |          * 1<<28 is just a not-so-small yet not-so-large number...  | 
171  |  |          * Below condition is practically never met, but it has to  | 
172  |  |          * be checked for code correctness.  | 
173  |  |          */  | 
174  | 73.0k  |         if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))  | 
175  | 0  |             blocks = (1U << 28);  | 
176  |  |         /*  | 
177  |  |          * As (*func) operates on 32-bit counter, caller  | 
178  |  |          * has to handle overflow. 'if' below detects the  | 
179  |  |          * overflow, which is then handled by limiting the  | 
180  |  |          * amount of blocks to the exact overflow point...  | 
181  |  |          */  | 
182  | 73.0k  |         ctr32 += (u32)blocks;  | 
183  | 73.0k  |         if (ctr32 < blocks) { | 
184  | 0  |             blocks -= ctr32;  | 
185  | 0  |             ctr32 = 0;  | 
186  | 0  |         }  | 
187  | 73.0k  |         (*func) (in, out, blocks, key, ivec);  | 
188  |  |         /* (*ctr) does not update ivec, caller does: */  | 
189  | 73.0k  |         PUTU32(ivec + 12, ctr32);  | 
190  |  |         /* ... overflow was detected, propagate carry. */  | 
191  | 73.0k  |         if (ctr32 == 0)  | 
192  | 0  |             ctr96_inc(ivec);  | 
193  | 73.0k  |         blocks *= 16;  | 
194  | 73.0k  |         len -= blocks;  | 
195  | 73.0k  |         out += blocks;  | 
196  | 73.0k  |         in += blocks;  | 
197  | 73.0k  |     }  | 
198  | 81.6k  |     if (len) { | 
199  | 79.5k  |         memset(ecount_buf, 0, 16);  | 
200  | 79.5k  |         (*func) (ecount_buf, ecount_buf, 1, key, ivec);  | 
201  | 79.5k  |         ++ctr32;  | 
202  | 79.5k  |         PUTU32(ivec + 12, ctr32);  | 
203  | 79.5k  |         if (ctr32 == 0)  | 
204  | 0  |             ctr96_inc(ivec);  | 
205  | 831k  |         while (len--) { | 
206  | 752k  |             out[n] = in[n] ^ ecount_buf[n];  | 
207  | 752k  |             ++n;  | 
208  | 752k  |         }  | 
209  | 79.5k  |     }  | 
210  |  |  | 
211  | 81.6k  |     *num = n;  | 
212  | 81.6k  | }  |