]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sha1.c
Nit: use _exit when exec fails and restrict values to 0-255 per POSIX.
[ppp.git] / pppd / sha1.c
index b56a7c9a6efb80decccd448a57838f3960377efb..f4f975cf516f6c9b58516f08a71152bdef62beca 100644 (file)
 
 #include <string.h>
 #include <netinet/in.h>        /* htonl() */
+#include <net/ppp_defs.h>
 #include "sha1.h"
 
+static void
+SHA1_Transform(u_int32_t[5], const unsigned char[64]);
+
 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
 
 /* blk0() and blk() perform the initial expand. */
 
 /* Hash a single 512-bit block. This is the core of the algorithm. */
 
-void SHA1_Transform(unsigned long state[5], const unsigned char buffer[64])
+static void
+SHA1_Transform(u_int32_t state[5], const unsigned char buffer[64])
 {
-    unsigned long a, b, c, d, e;
+    u_int32_t a, b, c, d, e;
     typedef union {
        unsigned char c[64];
-       unsigned long l[16];
+       u_int32_t l[16];
     } CHAR64LONG16;
     CHAR64LONG16 *block;
 
@@ -94,7 +99,8 @@ void SHA1_Transform(unsigned long state[5], const unsigned char buffer[64])
 
 /* SHA1Init - Initialize new context */
 
-void SHA1_Init(SHA1_CTX *context)
+void
+SHA1_Init(SHA1_CTX *context)
 {
     /* SHA1 initialization constants */
     context->state[0] = 0x67452301;
@@ -108,33 +114,34 @@ void SHA1_Init(SHA1_CTX *context)
 
 /* Run your data through this. */
 
-void SHA1_Update(SHA1_CTX *context, const unsigned char *data, unsigned int len)
+void
+SHA1_Update(SHA1_CTX *context, const unsigned char *data, unsigned int len)
 {
     unsigned int i, j;
 
     j = (context->count[0] >> 3) & 63;
     if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
     context->count[1] += (len >> 29);
-    if ((j + len) > 63) {
-       memcpy(&context->buffer[j], data, (i = 64-j));
+    i = 64 - j;
+    while (len >= i) {
+       memcpy(&context->buffer[j], data, i);
        SHA1_Transform(context->state, context->buffer);
-       for ( ; i + 63 < len; i += 64) {
-           SHA1_Transform(context->state, &data[i]);
-       }
+       data += i;
+       len -= i;
+       i = 64;
        j = 0;
     }
-    else
-       i = 0;
 
-    memcpy(&context->buffer[j], &data[i], len - i);
+    memcpy(&context->buffer[j], data, len);
 }
 
 
 /* Add padding and return the message digest. */
 
-void SHA1_Final(unsigned char digest[20], SHA1_CTX *context)
+void
+SHA1_Final(unsigned char digest[20], SHA1_CTX *context)
 {
-    unsigned long i, j;
+    u_int32_t i, j;
     unsigned char finalcount[8];
 
     for (i = 0; i < 8; i++) {