Use correct method for string comparison
[TortoiseGit.git] / src / TortoisePlink / mpunsafe.c
blob3f7ce3e9db9c8351261db683ed71681b274e19a4
1 #include <assert.h>
2 #include <limits.h>
3 #include <stdio.h>
5 #include "defs.h"
6 #include "misc.h"
7 #include "puttymem.h"
9 #include "mpint.h"
10 #include "mpint_i.h"
13 * This global symbol is also defined in ssh2kex-client.c, to ensure
14 * that these unsafe non-constant-time mp_int functions can't end up
15 * accidentally linked in to any PuTTY tool that actually makes an SSH
16 * client connection.
18 * (Only _client_ connections, however. Uppity, being a test server
19 * only, is exempt.)
21 const int deliberate_symbol_clash = 12345;
23 static size_t mp_unsafe_words_needed(mp_int *x)
25 size_t words = x->nw;
26 while (words > 1 && !x->w[words-1])
27 words--;
28 return words;
31 mp_int *mp_unsafe_shrink(mp_int *x)
33 x->nw = mp_unsafe_words_needed(x);
34 /* This potentially leaves some allocated words between the new
35 * and old values of x->nw, which won't be wiped by mp_free now
36 * that x->nw doesn't mention that they exist. But we've just
37 * checked they're all zero, so we don't need to wipe them now
38 * either. */
39 return x;
42 mp_int *mp_unsafe_copy(mp_int *x)
44 mp_int *copy = mp_make_sized(mp_unsafe_words_needed(x));
45 mp_copy_into(copy, x);
46 return copy;
49 uint32_t mp_unsafe_mod_integer(mp_int *x, uint32_t modulus)
51 uint64_t accumulator = 0;
52 for (size_t i = mp_max_bytes(x); i-- > 0 ;) {
53 accumulator = 0x100 * accumulator + mp_get_byte(x, i);
54 accumulator %= modulus;
56 return accumulator;