Line | Count | Source (jump to first uncovered line) |
1 | | /* des3.c |
2 | | |
3 | | Triple DES cipher. Three key encrypt-decrypt-encrypt. |
4 | | |
5 | | Copyright (C) 2001, 2010 Niels Möller |
6 | | |
7 | | This file is part of GNU Nettle. |
8 | | |
9 | | GNU Nettle is free software: you can redistribute it and/or |
10 | | modify it under the terms of either: |
11 | | |
12 | | * the GNU Lesser General Public License as published by the Free |
13 | | Software Foundation; either version 3 of the License, or (at your |
14 | | option) any later version. |
15 | | |
16 | | or |
17 | | |
18 | | * the GNU General Public License as published by the Free |
19 | | Software Foundation; either version 2 of the License, or (at your |
20 | | option) any later version. |
21 | | |
22 | | or both in parallel, as here. |
23 | | |
24 | | GNU Nettle is distributed in the hope that it will be useful, |
25 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
27 | | General Public License for more details. |
28 | | |
29 | | You should have received copies of the GNU General Public License and |
30 | | the GNU Lesser General Public License along with this program. If |
31 | | not, see http://www.gnu.org/licenses/. |
32 | | */ |
33 | | |
34 | | #if HAVE_CONFIG_H |
35 | | # include "config.h" |
36 | | #endif |
37 | | |
38 | | #include "des.h" |
39 | | |
40 | | /* It's possible to make some more general pipe construction, like the |
41 | | * lsh/src/cascade.c, but as in practice it's never used for anything |
42 | | * like triple DES, it's not worth the effort. */ |
43 | | |
44 | | /* Returns 1 for good keys and 0 for weak keys. */ |
45 | | int |
46 | | des3_set_key(struct des3_ctx *ctx, const uint8_t *key) |
47 | 0 | { |
48 | 0 | unsigned i; |
49 | 0 | int is_good = 1; |
50 | | |
51 | 0 | for (i = 0; i<3; i++, key += DES_KEY_SIZE) |
52 | 0 | if (!des_set_key(&ctx->des[i], key)) |
53 | 0 | is_good = 0; |
54 | |
|
55 | 0 | return is_good; |
56 | 0 | } |
57 | | |
58 | | void |
59 | | des3_encrypt(const struct des3_ctx *ctx, |
60 | | size_t length, uint8_t *dst, |
61 | | const uint8_t *src) |
62 | 0 | { |
63 | 0 | des_encrypt(&ctx->des[0], |
64 | 0 | length, dst, src); |
65 | 0 | des_decrypt(&ctx->des[1], |
66 | 0 | length, dst, dst); |
67 | 0 | des_encrypt(&ctx->des[2], |
68 | 0 | length, dst, dst); |
69 | 0 | } |
70 | | |
71 | | void |
72 | | des3_decrypt(const struct des3_ctx *ctx, |
73 | | size_t length, uint8_t *dst, |
74 | | const uint8_t *src) |
75 | 0 | { |
76 | 0 | des_decrypt(&ctx->des[2], |
77 | 0 | length, dst, src); |
78 | 0 | des_encrypt(&ctx->des[1], |
79 | 0 | length, dst, dst); |
80 | 0 | des_decrypt(&ctx->des[0], |
81 | 0 | length, dst, dst); |
82 | 0 | } |