Line | Count | Source (jump to first uncovered line) |
1 | | /* ctr16.c |
2 | | |
3 | | Cipher counter mode, optimized for 16-byte blocks. |
4 | | |
5 | | Copyright (C) 2005-2018 Niels Möller |
6 | | Copyright (C) 2018 Red Hat, Inc. |
7 | | |
8 | | This file is part of GNU Nettle. |
9 | | |
10 | | GNU Nettle is free software: you can redistribute it and/or |
11 | | modify it under the terms of either: |
12 | | |
13 | | * the GNU Lesser General Public License as published by the Free |
14 | | Software Foundation; either version 3 of the License, or (at your |
15 | | option) any later version. |
16 | | |
17 | | or |
18 | | |
19 | | * the GNU General Public License as published by the Free |
20 | | Software Foundation; either version 2 of the License, or (at your |
21 | | option) any later version. |
22 | | |
23 | | or both in parallel, as here. |
24 | | |
25 | | GNU Nettle is distributed in the hope that it will be useful, |
26 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
27 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
28 | | General Public License for more details. |
29 | | |
30 | | You should have received copies of the GNU General Public License and |
31 | | the GNU Lesser General Public License along with this program. If |
32 | | not, see http://www.gnu.org/licenses/. |
33 | | */ |
34 | | |
35 | | #if HAVE_CONFIG_H |
36 | | # include "config.h" |
37 | | #endif |
38 | | |
39 | | #include <assert.h> |
40 | | |
41 | | #include "ctr.h" |
42 | | |
43 | | #include "ctr-internal.h" |
44 | | #include "memxor.h" |
45 | | #include "nettle-internal.h" |
46 | | |
47 | | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
48 | | |
49 | | void |
50 | | _nettle_ctr_crypt16(const void *ctx, nettle_cipher_func *f, |
51 | | nettle_fill16_func *fill, uint8_t *ctr, |
52 | | size_t length, uint8_t *dst, |
53 | | const uint8_t *src) |
54 | 0 | { |
55 | 0 | if (dst != src && !((uintptr_t) dst % sizeof(uint64_t))) |
56 | 0 | { |
57 | 0 | size_t blocks = length / 16u; |
58 | 0 | size_t done; |
59 | 0 | fill (ctr, blocks, (union nettle_block16 *) dst); |
60 | |
|
61 | 0 | done = blocks * 16; |
62 | 0 | f(ctx, done, dst, dst); |
63 | 0 | memxor (dst, src, done); |
64 | |
|
65 | 0 | length -= done; |
66 | 0 | if (length > 0) |
67 | 0 | { /* Left-over partial block */ |
68 | 0 | union nettle_block16 block; |
69 | 0 | dst += done; |
70 | 0 | src += done; |
71 | 0 | assert (length < 16); |
72 | | /* Use fill, to update ctr value in the same way in all cases. */ |
73 | 0 | fill (ctr, 1, &block); |
74 | 0 | f (ctx, 16, block.b, block.b); |
75 | 0 | memxor3 (dst, src, block.b, length); |
76 | 0 | } |
77 | 0 | } |
78 | 0 | else |
79 | 0 | { |
80 | | /* Construct an aligned buffer of consecutive counter values, of |
81 | | size at most CTR_BUFFER_LIMIT. */ |
82 | 0 | TMP_DECL(buffer, union nettle_block16, CTR_BUFFER_LIMIT / 16); |
83 | 0 | size_t blocks = (length + 15) / 16u; |
84 | 0 | size_t i; |
85 | 0 | TMP_ALLOC(buffer, MIN(blocks, CTR_BUFFER_LIMIT / 16)); |
86 | |
|
87 | 0 | for (i = 0; blocks >= CTR_BUFFER_LIMIT / 16; |
88 | 0 | i += CTR_BUFFER_LIMIT, blocks -= CTR_BUFFER_LIMIT / 16) |
89 | 0 | { |
90 | 0 | fill (ctr, CTR_BUFFER_LIMIT / 16, buffer); |
91 | 0 | f(ctx, CTR_BUFFER_LIMIT, buffer->b, buffer->b); |
92 | 0 | if (length - i < CTR_BUFFER_LIMIT) |
93 | 0 | goto done; |
94 | 0 | memxor3 (dst + i, src + i, buffer->b, CTR_BUFFER_LIMIT); |
95 | 0 | } |
96 | | |
97 | 0 | if (blocks > 0) |
98 | 0 | { |
99 | 0 | assert (length - i < CTR_BUFFER_LIMIT); |
100 | 0 | fill (ctr, blocks, buffer); |
101 | 0 | f(ctx, blocks * 16, buffer->b, buffer->b); |
102 | 0 | done: |
103 | 0 | memxor3 (dst + i, src + i, buffer->b, length - i); |
104 | 0 | } |
105 | 0 | } |
106 | 0 | } |