Camellia cipher, contributed by NTT
[openssl.git] / crypto / evp / bio_ok.c
index 6b00741e476f616356f5021d1c5903f0bcb98def..98bc1ab40963eddef3f7ed3418fb60f60395f7df 100644 (file)
 
                *) digest is initialized with random seed instead of 
                standardized one.
-               *) same seed is written to ouput
+               *) same seed is written to output
                *) well-known text is then hashed and the output 
                of the digest is also written to output.
 
 
 #include <stdio.h>
 #include <errno.h>
+#include <assert.h>
 #include "cryptlib.h"
 #include <openssl/buffer.h>
 #include <openssl/bio.h>
@@ -141,22 +142,12 @@ static void block_in(BIO* b);
 #define IOBS           (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
 #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
 
-#ifndef L_ENDIAN
-#define swapem(x) \
-       ((unsigned long int)((((unsigned long int)(x) & 0x000000ffU) << 24) | \
-                            (((unsigned long int)(x) & 0x0000ff00U) <<  8) | \
-                            (((unsigned long int)(x) & 0x00ff0000U) >>  8) | \
-                            (((unsigned long int)(x) & 0xff000000U) >> 24)))
-#else
-#define swapem(x) (x)
-#endif
-
 typedef struct ok_struct
        {
-       int buf_len;
-       int buf_off;
-       int buf_len_save;
-       int buf_off_save;
+       size_t buf_len;
+       size_t buf_off;
+       size_t buf_len_save;
+       size_t buf_off_save;
        int cont;               /* <= 0 when finished */
        int finished;
        EVP_MD_CTX md;
@@ -211,7 +202,7 @@ static int ok_free(BIO *a)
        {
        if (a == NULL) return(0);
        EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md);
-       memset(a->ptr,0,sizeof(BIO_OK_CTX));
+       OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX));
        OPENSSL_free(a->ptr);
        a->ptr=NULL;
        a->init=0;
@@ -295,6 +286,8 @@ static int ok_write(BIO *b, const char *in, int inl)
        int ret=0,n,i;
        BIO_OK_CTX *ctx;
 
+       if (inl <= 0) return inl;
+
        ctx=(BIO_OK_CTX *)b->ptr;
        ret=inl;
 
@@ -330,7 +323,7 @@ static int ok_write(BIO *b, const char *in, int inl)
                if ((in == NULL) || (inl <= 0)) return(0);
 
                n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ? 
-                               OK_BLOCK_SIZE+ OK_BLOCK_BLOCK- ctx->buf_len : inl;
+                       (int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl;
 
                memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
                ctx->buf_len+= n;
@@ -415,7 +408,7 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
                break;
        case BIO_C_SET_MD:
                md=ptr;
-               EVP_DigestInit(&ctx->md,md);
+               EVP_DigestInit_ex(&ctx->md, md, NULL);
                b->init=1;
                break;
        case BIO_C_GET_MD:
@@ -448,16 +441,18 @@ static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
        return(ret);
        }
 
-static void longswap(void *_ptr, int len)
-{
-#ifndef L_ENDIAN
-       int i;
-       char *ptr=_ptr;
+static void longswap(void *_ptr, size_t len)
+{      const union { long one; char little; } is_endian = {1};
 
-       for(i= 0;i < len;i+= 4){
-               *((unsigned long *)&(ptr[i]))= swapem(*((unsigned long *)&(ptr[i])));
+       if (is_endian.little) {
+               size_t i;
+               unsigned char *p=_ptr,c;
+
+               for(i= 0;i < len;i+= 4) {
+                       c=p[0],p[0]=p[3],p[3]=c;
+                       c=p[1],p[1]=p[2],p[2]=c;
+               }
        }
-#endif
 }
 
 static void sig_out(BIO* b)
@@ -470,7 +465,7 @@ static void sig_out(BIO* b)
 
        if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
 
-       EVP_DigestInit(md, md->digest);
+       EVP_DigestInit_ex(md, md->digest, NULL);
        /* FIXME: there's absolutely no guarantee this makes any sense at all,
         * particularly now EVP_MD_CTX has been restructured.
         */
@@ -480,7 +475,7 @@ static void sig_out(BIO* b)
        ctx->buf_len+= md->digest->md_size;
 
        EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
-       EVP_DigestFinal(md, &(ctx->buf[ctx->buf_len]), NULL);
+       EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
        ctx->buf_len+= md->digest->md_size;
        ctx->blockout= 1;
        ctx->sigio= 0;
@@ -496,15 +491,15 @@ static void sig_in(BIO* b)
        ctx=b->ptr;
        md=&ctx->md;
 
-       if(ctx->buf_len- ctx->buf_off < 2* md->digest->md_size) return;
+       if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return;
 
-       EVP_DigestInit(md, md->digest);
+       EVP_DigestInit_ex(md, md->digest, NULL);
        memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
        longswap(md->md_data, md->digest->md_size);
        ctx->buf_off+= md->digest->md_size;
 
        EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
-       EVP_DigestFinal(md, tmp, NULL);
+       EVP_DigestFinal_ex(md, tmp, NULL);
        ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
        ctx->buf_off+= md->digest->md_size;
        if(ret == 1)
@@ -533,11 +528,12 @@ static void block_out(BIO* b)
        md=&ctx->md;
 
        tl= ctx->buf_len- OK_BLOCK_BLOCK;
-       tl= swapem(tl);
-       memcpy(ctx->buf, &tl, OK_BLOCK_BLOCK);
-       tl= swapem(tl);
+       ctx->buf[0]=(unsigned char)(tl>>24);
+       ctx->buf[1]=(unsigned char)(tl>>16);
+       ctx->buf[2]=(unsigned char)(tl>>8);
+       ctx->buf[3]=(unsigned char)(tl);
        EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
-       EVP_DigestFinal(md, &(ctx->buf[ctx->buf_len]), NULL);
+       EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
        ctx->buf_len+= md->digest->md_size;
        ctx->blockout= 1;
        }
@@ -546,18 +542,22 @@ static void block_in(BIO* b)
        {
        BIO_OK_CTX *ctx;
        EVP_MD_CTX *md;
-       long tl= 0;
+       unsigned long tl= 0;
        unsigned char tmp[EVP_MAX_MD_SIZE];
 
        ctx=b->ptr;
        md=&ctx->md;
 
-       memcpy(&tl, ctx->buf, OK_BLOCK_BLOCK);
-       tl= swapem(tl);
+       assert(sizeof(tl)>=OK_BLOCK_BLOCK);     /* always true */
+       tl =ctx->buf[0]; tl<<=8;
+       tl|=ctx->buf[1]; tl<<=8;
+       tl|=ctx->buf[2]; tl<<=8;
+       tl|=ctx->buf[3];
+
        if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
  
        EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
-       EVP_DigestFinal(md, tmp, NULL);
+       EVP_DigestFinal_ex(md, tmp, NULL);
        if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
                {
                /* there might be parts from next block lurking around ! */