/src/nettle-with-libgmp/salsa20-crypt-internal.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* salsa20-crypt-internal.c  | 
2  |  |  | 
3  |  |    The Salsa20 stream cipher.  | 
4  |  |  | 
5  |  |    Copyright (C) 2012 Simon Josefsson  | 
6  |  |    Copyright (C) 2020 Niels Möller  | 
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 <string.h>  | 
40  |  |  | 
41  |  | #include "salsa20.h"  | 
42  |  | #include "salsa20-internal.h"  | 
43  |  |  | 
44  |  | #include "macros.h"  | 
45  |  | #include "memxor.h"  | 
46  |  |  | 
47  |  | #if HAVE_NATIVE_salsa20_2core  | 
48  |  | #define _nettle_salsa20_crypt_2core _nettle_salsa20_crypt  | 
49  |  | #elif !HAVE_NATIVE_fat_salsa20_2core  | 
50  |  | #define _nettle_salsa20_crypt_1core _nettle_salsa20_crypt  | 
51  |  | #endif  | 
52  |  |  | 
53  |  | #if HAVE_NATIVE_salsa20_2core || HAVE_NATIVE_fat_salsa20_2core  | 
54  |  | void  | 
55  |  | _nettle_salsa20_crypt_2core(struct salsa20_ctx *ctx, unsigned rounds,  | 
56  |  |           size_t length, uint8_t *dst,  | 
57  |  |           const uint8_t *src)  | 
58  | 776  | { | 
59  | 776  |   uint32_t x[2*_SALSA20_INPUT_LENGTH];  | 
60  | 776  |   while (length > SALSA20_BLOCK_SIZE)  | 
61  | 0  |     { | 
62  | 0  |       _nettle_salsa20_2core (x, ctx->input, rounds);  | 
63  | 0  |       ctx->input[8] += 2;  | 
64  | 0  |       ctx->input[9] += (ctx->input[8] < 2);  | 
65  | 0  |       if (length <= 2 * SALSA20_BLOCK_SIZE)  | 
66  | 0  |   { | 
67  | 0  |     memxor3 (dst, src, x, length);  | 
68  | 0  |     return;  | 
69  | 0  |   }  | 
70  | 0  |       memxor3 (dst, src, x, 2*SALSA20_BLOCK_SIZE);  | 
71  |  | 
  | 
72  | 0  |       length -= 2*SALSA20_BLOCK_SIZE;  | 
73  | 0  |       dst += 2*SALSA20_BLOCK_SIZE;  | 
74  | 0  |       src += 2*SALSA20_BLOCK_SIZE;  | 
75  | 0  |     }  | 
76  | 776  |   _nettle_salsa20_core (x, ctx->input, rounds);  | 
77  | 776  |   ctx->input[9] += (++ctx->input[8] == 0);  | 
78  | 776  |   memxor3 (dst, src, x, length);  | 
79  | 776  | }  | 
80  |  | #endif  | 
81  |  |  | 
82  |  | #if !HAVE_NATIVE_salsa20_2core  | 
83  |  | void  | 
84  |  | _nettle_salsa20_crypt_1core(struct salsa20_ctx *ctx, unsigned rounds,  | 
85  |  |           size_t length,  | 
86  |  |           uint8_t *dst,  | 
87  |  |           const uint8_t *src)  | 
88  |  | { | 
89  |  |   for (;;)  | 
90  |  |     { | 
91  |  |       uint32_t x[_SALSA20_INPUT_LENGTH];  | 
92  |  |  | 
93  |  |       _nettle_salsa20_core (x, ctx->input, rounds);  | 
94  |  |  | 
95  |  |       ctx->input[9] += (++ctx->input[8] == 0);  | 
96  |  |  | 
97  |  |       /* stopping at 2^70 length per nonce is user's responsibility */  | 
98  |  |  | 
99  |  |       if (length <= SALSA20_BLOCK_SIZE)  | 
100  |  |   { | 
101  |  |     memxor3 (dst, src, x, length);  | 
102  |  |     return;  | 
103  |  |   }  | 
104  |  |       memxor3 (dst, src, x, SALSA20_BLOCK_SIZE);  | 
105  |  |  | 
106  |  |       length -= SALSA20_BLOCK_SIZE;  | 
107  |  |       dst += SALSA20_BLOCK_SIZE;  | 
108  |  |       src += SALSA20_BLOCK_SIZE;  | 
109  |  |     }  | 
110  |  | }  | 
111  |  | #endif  |