mpunsafe.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include "defs.h"
  5. #include "misc.h"
  6. #include "puttymem.h"
  7. #include "mpint.h"
  8. #include "mpunsafe.h"
  9. #include "crypto/mpint_i.h"
  10. /*
  11. * This global symbol is also defined in ssh/kex2-client.c, to ensure
  12. * that these unsafe non-constant-time mp_int functions can't end up
  13. * accidentally linked in to any PuTTY tool that actually makes an SSH
  14. * client connection.
  15. *
  16. * (Only _client_ connections, however. Uppity, being a test server
  17. * only, is exempt.)
  18. */
  19. const int deliberate_symbol_clash = 12345;
  20. static size_t mp_unsafe_words_needed(mp_int *x)
  21. {
  22. size_t words = x->nw;
  23. while (words > 1 && !x->w[words-1])
  24. words--;
  25. return words;
  26. }
  27. mp_int *mp_unsafe_shrink(mp_int *x)
  28. {
  29. x->nw = mp_unsafe_words_needed(x);
  30. /* This potentially leaves some allocated words between the new
  31. * and old values of x->nw, which won't be wiped by mp_free now
  32. * that x->nw doesn't mention that they exist. But we've just
  33. * checked they're all zero, so we don't need to wipe them now
  34. * either. */
  35. return x;
  36. }
  37. mp_int *mp_unsafe_copy(mp_int *x)
  38. {
  39. mp_int *copy = mp_make_sized(mp_unsafe_words_needed(x));
  40. mp_copy_into(copy, x);
  41. return copy;
  42. }