mac.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Centralised parts of the SSH-2 MAC API, which don't need to vary
  3. * with the MAC implementation.
  4. */
  5. #include <assert.h>
  6. #include "ssh.h"
  7. bool ssh2_mac_verresult(ssh2_mac *mac, const void *candidate)
  8. {
  9. unsigned char correct[64]; /* at least as big as all known MACs */
  10. bool toret;
  11. assert(mac->vt->len <= sizeof(correct));
  12. ssh2_mac_genresult(mac, correct);
  13. toret = smemeq(correct, candidate, mac->vt->len);
  14. smemclr(correct, sizeof(correct));
  15. return toret;
  16. }
  17. static void ssh2_mac_prepare(ssh2_mac *mac, const void *blk, int len,
  18. unsigned long seq)
  19. {
  20. ssh2_mac_start(mac);
  21. put_uint32(mac, seq);
  22. put_data(mac, blk, len);
  23. }
  24. void ssh2_mac_generate(ssh2_mac *mac, void *blk, int len, unsigned long seq)
  25. {
  26. ssh2_mac_prepare(mac, blk, len, seq);
  27. ssh2_mac_genresult(mac, (unsigned char *)blk + len);
  28. }
  29. bool ssh2_mac_verify(
  30. ssh2_mac *mac, const void *blk, int len, unsigned long seq)
  31. {
  32. ssh2_mac_prepare(mac, blk, len, seq);
  33. return ssh2_mac_verresult(mac, (const unsigned char *)blk + len);
  34. }