plan9-sha1.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <mp.h>
  13. #include <libsec.h>
  14. extern void vtSha1Block(uint32_t *s, uint8_t *p, uint32_t len);
  15. struct VtSha1
  16. {
  17. DigestState *s;
  18. };
  19. VtSha1 *
  20. vtSha1Alloc(void)
  21. {
  22. VtSha1 *s;
  23. s = vtMemAlloc(sizeof(VtSha1));
  24. vtSha1Init(s);
  25. return s;
  26. }
  27. void
  28. vtSha1Free(VtSha1 *s)
  29. {
  30. if(s == nil)
  31. return;
  32. if(s->s != nil)
  33. free(s->s);
  34. vtMemFree(s);
  35. }
  36. void
  37. vtSha1Init(VtSha1 *s)
  38. {
  39. s->s = nil;
  40. }
  41. void
  42. vtSha1Update(VtSha1 *s, uint8_t *p, int len)
  43. {
  44. s->s = sha1(p, len, nil, s->s);
  45. }
  46. void
  47. vtSha1Final(VtSha1 *s, uint8_t *digest)
  48. {
  49. sha1(nil, 0, digest, s->s);
  50. s->s = nil;
  51. }
  52. void
  53. vtSha1(uint8_t sha1[VtScoreSize], uint8_t *p, int n)
  54. {
  55. VtSha1 s;
  56. vtSha1Init(&s);
  57. vtSha1Update(&s, p, n);
  58. vtSha1Final(&s, sha1);
  59. }
  60. int
  61. vtSha1Check(uint8_t score[VtScoreSize], uint8_t *p, int n)
  62. {
  63. VtSha1 s;
  64. uint8_t score2[VtScoreSize];
  65. vtSha1Init(&s);
  66. vtSha1Update(&s, p, n);
  67. vtSha1Final(&s, score2);
  68. if(memcmp(score, score2, VtScoreSize) != 0) {
  69. vtSetError("vtSha1Check failed");
  70. return 0;
  71. }
  72. return 1;
  73. }