plan9-sha1.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <oventi.h>
  12. #include <libsec.h>
  13. extern void vtSha1Block(uint32_t *s, uint8_t *p, uint32_t len);
  14. struct VtSha1
  15. {
  16. DigestState *s;
  17. };
  18. VtSha1 *
  19. vtSha1Alloc(void)
  20. {
  21. VtSha1 *s;
  22. s = vtMemAlloc(sizeof(VtSha1));
  23. vtSha1Init(s);
  24. return s;
  25. }
  26. void
  27. vtSha1Free(VtSha1 *s)
  28. {
  29. if(s == nil)
  30. return;
  31. if(s->s != nil)
  32. free(s->s);
  33. vtMemFree(s);
  34. }
  35. void
  36. vtSha1Init(VtSha1 *s)
  37. {
  38. s->s = nil;
  39. }
  40. void
  41. vtSha1Update(VtSha1 *s, uint8_t *p, int len)
  42. {
  43. s->s = sha1(p, len, nil, s->s);
  44. }
  45. void
  46. vtSha1Final(VtSha1 *s, uint8_t *digest)
  47. {
  48. sha1(nil, 0, digest, s->s);
  49. s->s = nil;
  50. }
  51. void
  52. vtSha1(uint8_t sha1[VtScoreSize], uint8_t *p, int n)
  53. {
  54. VtSha1 s;
  55. vtSha1Init(&s);
  56. vtSha1Update(&s, p, n);
  57. vtSha1Final(&s, sha1);
  58. }
  59. int
  60. vtSha1Check(uint8_t score[VtScoreSize], uint8_t *p, int n)
  61. {
  62. VtSha1 s;
  63. uint8_t score2[VtScoreSize];
  64. vtSha1Init(&s);
  65. vtSha1Update(&s, p, n);
  66. vtSha1Final(&s, score2);
  67. if(memcmp(score, score2, VtScoreSize) != 0) {
  68. vtSetError("vtSha1Check failed");
  69. return 0;
  70. }
  71. return 1;
  72. }