more places where we can use cm_lengths;
[citadel.git] / citadel / md5.c
index 5eb763610532d381f1b59f4fd46ef4fa99f4c696..9d0b4421e7e90fb83092942a268de4d6474285a5 100644 (file)
@@ -32,11 +32,11 @@ void byteReverse(unsigned char *buf, unsigned longs);
  */
 void byteReverse(unsigned char *buf, unsigned longs)
 {
-    u_int32_t t;
+    cit_uint32_t t;
     do {
-       t = (u_int32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
+       t = (cit_uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
            ((unsigned) buf[1] << 8 | buf[0]);
-       *(u_int32_t *) buf = t;
+       *(cit_uint32_t *) buf = t;
        buf += 4;
     } while (--longs);
 }
@@ -66,12 +66,12 @@ void MD5Init(struct MD5Context *ctx)
  */
 void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
 {
-    u_int32_t t;
+    cit_uint32_t t;
 
     /* Update bitcount */
 
     t = ctx->bits[0];
-    if ((ctx->bits[0] = t + ((u_int32_t) len << 3)) < t)
+    if ((ctx->bits[0] = t + ((cit_uint32_t) len << 3)) < t)
        ctx->bits[1]++;         /* Carry from low to high */
     ctx->bits[1] += len >> 29;
 
@@ -89,7 +89,7 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
        }
        memcpy(p, buf, t);
        byteReverse(ctx->in, 16);
-       MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
+       MD5Transform(ctx->buf, ctx->in);
        buf += t;
        len -= t;
     }
@@ -98,7 +98,7 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
     while (len >= 64) {
        memcpy(ctx->in, buf, 64);
        byteReverse(ctx->in, 16);
-       MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
+       MD5Transform(ctx->buf, ctx->in);
        buf += 64;
        len -= 64;
     }
@@ -122,7 +122,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
 
     /* Set the first char of padding to 0x80.  This is safe since there is
        always at least one byte free */
-    p = ctx->in + count;
+    p = ((unsigned char*)ctx->in) + count;
     *p++ = 0x80;
 
     /* Bytes of padding needed to make 64 bytes */
@@ -133,7 +133,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
        /* Two lots of padding:  Pad the first block to 64 bytes */
        memset(p, 0, count);
        byteReverse(ctx->in, 16);
-       MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
+       MD5Transform(ctx->buf, ctx->in);
 
        /* Now fill the next block with 56 bytes */
        memset(ctx->in, 0, 56);
@@ -144,13 +144,13 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
     byteReverse(ctx->in, 14);
 
     /* Append length in bits and transform */
-    ((u_int32_t *) ctx->in)[14] = ctx->bits[0];
-    ((u_int32_t *) ctx->in)[15] = ctx->bits[1];
+    ((cit_uint32_t *) ctx->in)[14] = ctx->bits[0];
+    ((cit_uint32_t *) ctx->in)[15] = ctx->bits[1];
 
-    MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
+    MD5Transform(ctx->buf, (cit_uint32_t *) ctx->in);
     byteReverse((unsigned char *) ctx->buf, 4);
     memcpy(digest, ctx->buf, 16);
-    memset(ctx, 0, sizeof(ctx));       /* In case it's sensitive */
+    memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */
 }
 
 #ifndef ASM_MD5
@@ -177,9 +177,9 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
  * reflect the addition of 16 longwords of new data.  MD5Update blocks
  * the data and converts bytes into longwords for this routine.
  */
-void MD5Transform(u_int32_t buf[4], u_int32_t const in[16])
+void MD5Transform(cit_uint32_t buf[4], cit_uint32_t const in[16])
 {
-    register u_int32_t a, b, c, d;
+    register cit_uint32_t a, b, c, d;
 
     a = buf[0];
     b = buf[1];
@@ -336,25 +336,22 @@ void MD5Transform(u_int32_t buf[4], u_int32_t const in[16])
  * md5 string
  */
 
-char *make_apop_string(char *realpass, char *nonce, u_char *buffer)
+char *make_apop_string(char *realpass, char *nonce, char *buffer, size_t n)
 {
    struct MD5Context ctx;
    u_char rawdigest[MD5_DIGEST_LEN];
    int i;
    
    MD5Init(&ctx);
-//   printf("MD5@@: Adding nonce: %s\n", nonce);
-   MD5Update(&ctx, nonce, strlen(nonce));
-//   printf("MD5@@: Adding password %s\n", realpass);
-   MD5Update(&ctx, realpass, strlen(realpass));
+   MD5Update(&ctx, (u_char*)nonce, strlen(nonce));
+   MD5Update(&ctx, (u_char*)realpass, strlen(realpass));
    MD5Final(rawdigest, &ctx);
    for (i=0; i<MD5_DIGEST_LEN; i++)
    {
-      sprintf(&buffer[i*2], "%02X", (unsigned char) (rawdigest[i] & 0xff));
+      snprintf(&buffer[i*2], n - i*2, "%02X", (unsigned char) (rawdigest[i] & 0xff));
       buffer[i*2] = tolower(buffer[i*2]);
       buffer[(i*2)+1] = tolower(buffer[(i*2)+1]);
    }
-//   printf("MD5@@: Result is %s\n", buffer);
    return buffer;
 }