/src/gnupg/common/openpgp-s2k.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* openpgp-s2ks.c - OpenPGP S2K helper functions |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
3 | | * 2005, 2006 Free Software Foundation, Inc. |
4 | | * Copyright (C) 2010, 2019 g10 Code GmbH |
5 | | * |
6 | | * This file is part of GnuPG. |
7 | | * |
8 | | * This file is free software; you can redistribute it and/or modify |
9 | | * it under the terms of either |
10 | | * |
11 | | * - the GNU Lesser General Public License as published by the Free |
12 | | * Software Foundation; either version 3 of the License, or (at |
13 | | * your option) any later version. |
14 | | * |
15 | | * or |
16 | | * |
17 | | * - the GNU General Public License as published by the Free |
18 | | * Software Foundation; either version 2 of the License, or (at |
19 | | * your option) any later version. |
20 | | * |
21 | | * or both in parallel, as here. |
22 | | * |
23 | | * This file is distributed in the hope that it will be useful, |
24 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 | | * GNU General Public License for more details. |
27 | | * |
28 | | * You should have received a copy of the GNU General Public License |
29 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
30 | | */ |
31 | | |
32 | | #include <config.h> |
33 | | #include <stdlib.h> |
34 | | #include <errno.h> |
35 | | #include <ctype.h> |
36 | | #include <assert.h> |
37 | | |
38 | | #include "util.h" |
39 | | #include "openpgpdefs.h" |
40 | | |
41 | | |
42 | | /* Pack an s2k iteration count into the form specified in RFC-4880. |
43 | | * If we're in between valid values, round up. */ |
44 | | unsigned char |
45 | | encode_s2k_iterations (int iterations) |
46 | 0 | { |
47 | 0 | unsigned char c=0; |
48 | 0 | unsigned char result; |
49 | 0 | unsigned int count; |
50 | |
|
51 | 0 | if (iterations <= 1024) |
52 | 0 | return 0; /* Command line arg compatibility. */ |
53 | | |
54 | 0 | if (iterations >= 65011712) |
55 | 0 | return 255; |
56 | | |
57 | | /* Need count to be in the range 16-31 */ |
58 | 0 | for (count=iterations>>6; count>=32; count>>=1) |
59 | 0 | c++; |
60 | |
|
61 | 0 | result = (c<<4)|(count-16); |
62 | |
|
63 | 0 | if (S2K_DECODE_COUNT(result) < iterations) |
64 | 0 | result++; |
65 | |
|
66 | 0 | return result; |
67 | 0 | } |