1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 Adler-32 checksum algorithm
14 Written and placed in the public domain by Wei Dai
15 Adapted for libtomcrypt by Steffen Jaeckel
19 static const unsigned long _adler32_base = 65521;
21 void adler32_init(adler32_state *ctx)
23 LTC_ARGCHKVD(ctx != NULL);
28 void adler32_update(adler32_state *ctx, const unsigned char *input, unsigned long length)
32 LTC_ARGCHKVD(ctx != NULL);
33 LTC_ARGCHKVD(input != NULL);
37 if (length % 8 != 0) {
42 } while (length % 8 != 0);
44 if (s1 >= _adler32_base)
70 if (s1 >= _adler32_base)
75 LTC_ARGCHKVD(s1 < _adler32_base);
76 LTC_ARGCHKVD(s2 < _adler32_base);
78 ctx->s[0] = (unsigned short)s1;
79 ctx->s[1] = (unsigned short)s2;
82 void adler32_finish(adler32_state *ctx, void *hash, unsigned long size)
86 LTC_ARGCHKVD(ctx != NULL);
87 LTC_ARGCHKVD(hash != NULL);
93 h[3] = ctx->s[0] & 0x0ff;
96 h[2] = (ctx->s[0] >> 8) & 0x0ff;
99 h[1] = ctx->s[1] & 0x0ff;
102 h[0] = (ctx->s[1] >> 8) & 0x0ff;
109 int adler32_test(void)
114 const void* in = "libtomcrypt";
115 const unsigned char adler32[] = { 0x1b, 0xe8, 0x04, 0xba };
116 unsigned char out[4];
119 adler32_update(&ctx, in, strlen(in));
120 adler32_finish(&ctx, out, 4);
121 if (compare_testvector(adler32, 4, out, 4, "adler32", 0)) {
122 return CRYPT_FAIL_TESTVECTOR;
129 /* ref: $Format:%D$ */
130 /* git commit: $Format:%H$ */
131 /* commit time: $Format:%ai$ */