Blame include/crypto/md32_common.h

Packit Service 084de1
/*
Packit Service 084de1
 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit Service 084de1
 *
Packit Service 084de1
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit Service 084de1
 * this file except in compliance with the License.  You can obtain a copy
Packit Service 084de1
 * in the file LICENSE in the source distribution or at
Packit Service 084de1
 * https://www.openssl.org/source/license.html
Packit Service 084de1
 */
Packit Service 084de1
Packit Service 084de1
/*-
Packit Service 084de1
 * This is a generic 32 bit "collector" for message digest algorithms.
Packit Service 084de1
 * Whenever needed it collects input character stream into chunks of
Packit Service 084de1
 * 32 bit values and invokes a block function that performs actual hash
Packit Service 084de1
 * calculations.
Packit Service 084de1
 *
Packit Service 084de1
 * Porting guide.
Packit Service 084de1
 *
Packit Service 084de1
 * Obligatory macros:
Packit Service 084de1
 *
Packit Service 084de1
 * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
Packit Service 084de1
 *      this macro defines byte order of input stream.
Packit Service 084de1
 * HASH_CBLOCK
Packit Service 084de1
 *      size of a unit chunk HASH_BLOCK operates on.
Packit Service 084de1
 * HASH_LONG
Packit Service 084de1
 *      has to be at least 32 bit wide.
Packit Service 084de1
 * HASH_CTX
Packit Service 084de1
 *      context structure that at least contains following
Packit Service 084de1
 *      members:
Packit Service 084de1
 *              typedef struct {
Packit Service 084de1
 *                      ...
Packit Service 084de1
 *                      HASH_LONG       Nl,Nh;
Packit Service 084de1
 *                      either {
Packit Service 084de1
 *                      HASH_LONG       data[HASH_LBLOCK];
Packit Service 084de1
 *                      unsigned char   data[HASH_CBLOCK];
Packit Service 084de1
 *                      };
Packit Service 084de1
 *                      unsigned int    num;
Packit Service 084de1
 *                      ...
Packit Service 084de1
 *                      } HASH_CTX;
Packit Service 084de1
 *      data[] vector is expected to be zeroed upon first call to
Packit Service 084de1
 *      HASH_UPDATE.
Packit Service 084de1
 * HASH_UPDATE
Packit Service 084de1
 *      name of "Update" function, implemented here.
Packit Service 084de1
 * HASH_TRANSFORM
Packit Service 084de1
 *      name of "Transform" function, implemented here.
Packit Service 084de1
 * HASH_FINAL
Packit Service 084de1
 *      name of "Final" function, implemented here.
Packit Service 084de1
 * HASH_BLOCK_DATA_ORDER
Packit Service 084de1
 *      name of "block" function capable of treating *unaligned* input
Packit Service 084de1
 *      message in original (data) byte order, implemented externally.
Packit Service 084de1
 * HASH_MAKE_STRING
Packit Service 084de1
 *      macro converting context variables to an ASCII hash string.
Packit Service 084de1
 *
Packit Service 084de1
 * MD5 example:
Packit Service 084de1
 *
Packit Service 084de1
 *      #define DATA_ORDER_IS_LITTLE_ENDIAN
Packit Service 084de1
 *
Packit Service 084de1
 *      #define HASH_LONG               MD5_LONG
Packit Service 084de1
 *      #define HASH_CTX                MD5_CTX
Packit Service 084de1
 *      #define HASH_CBLOCK             MD5_CBLOCK
Packit Service 084de1
 *      #define HASH_UPDATE             MD5_Update
Packit Service 084de1
 *      #define HASH_TRANSFORM          MD5_Transform
Packit Service 084de1
 *      #define HASH_FINAL              MD5_Final
Packit Service 084de1
 *      #define HASH_BLOCK_DATA_ORDER   md5_block_data_order
Packit Service 084de1
 */
Packit Service 084de1
Packit Service 084de1
#include <openssl/crypto.h>
Packit Service 084de1
Packit Service 084de1
#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
Packit Service 084de1
# error "DATA_ORDER must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
Packit Service 084de1
#ifndef HASH_CBLOCK
Packit Service 084de1
# error "HASH_CBLOCK must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
#ifndef HASH_LONG
Packit Service 084de1
# error "HASH_LONG must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
#ifndef HASH_CTX
Packit Service 084de1
# error "HASH_CTX must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
Packit Service 084de1
#ifndef HASH_UPDATE
Packit Service 084de1
# error "HASH_UPDATE must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
#ifndef HASH_TRANSFORM
Packit Service 084de1
# error "HASH_TRANSFORM must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
#ifndef HASH_FINAL
Packit Service 084de1
# error "HASH_FINAL must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
Packit Service 084de1
#ifndef HASH_BLOCK_DATA_ORDER
Packit Service 084de1
# error "HASH_BLOCK_DATA_ORDER must be defined!"
Packit Service 084de1
#endif
Packit Service 084de1
Packit Service 084de1
#define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
Packit Service 084de1
Packit Service 084de1
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
Packit Service 084de1
Packit Service 084de1
# define HOST_c2l(c,l)  (l =(((unsigned long)(*((c)++)))<<24),          \
Packit Service 084de1
                         l|=(((unsigned long)(*((c)++)))<<16),          \
Packit Service 084de1
                         l|=(((unsigned long)(*((c)++)))<< 8),          \
Packit Service 084de1
                         l|=(((unsigned long)(*((c)++)))    )           )
Packit Service 084de1
# define HOST_l2c(l,c)  (*((c)++)=(unsigned char)(((l)>>24)&0xff),      \
Packit Service 084de1
                         *((c)++)=(unsigned char)(((l)>>16)&0xff),      \
Packit Service 084de1
                         *((c)++)=(unsigned char)(((l)>> 8)&0xff),      \
Packit Service 084de1
                         *((c)++)=(unsigned char)(((l)    )&0xff),      \
Packit Service 084de1
                         l)
Packit Service 084de1
Packit Service 084de1
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
Packit Service 084de1
Packit Service 084de1
# define HOST_c2l(c,l)  (l =(((unsigned long)(*((c)++)))    ),          \
Packit Service 084de1
                         l|=(((unsigned long)(*((c)++)))<< 8),          \
Packit Service 084de1
                         l|=(((unsigned long)(*((c)++)))<<16),          \
Packit Service 084de1
                         l|=(((unsigned long)(*((c)++)))<<24)           )
Packit Service 084de1
# define HOST_l2c(l,c)  (*((c)++)=(unsigned char)(((l)    )&0xff),      \
Packit Service 084de1
                         *((c)++)=(unsigned char)(((l)>> 8)&0xff),      \
Packit Service 084de1
                         *((c)++)=(unsigned char)(((l)>>16)&0xff),      \
Packit Service 084de1
                         *((c)++)=(unsigned char)(((l)>>24)&0xff),      \
Packit Service 084de1
                         l)
Packit Service 084de1
Packit Service 084de1
#endif
Packit Service 084de1
Packit Service 084de1
/*
Packit Service 084de1
 * Time for some action :-)
Packit Service 084de1
 */
Packit Service 084de1
Packit Service 084de1
int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
Packit Service 084de1
{
Packit Service 084de1
    const unsigned char *data = data_;
Packit Service 084de1
    unsigned char *p;
Packit Service 084de1
    HASH_LONG l;
Packit Service 084de1
    size_t n;
Packit Service 084de1
Packit Service 084de1
    if (len == 0)
Packit Service 084de1
        return 1;
Packit Service 084de1
Packit Service 084de1
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
Packit Service 084de1
    if (l < c->Nl)              /* overflow */
Packit Service 084de1
        c->Nh++;
Packit Service 084de1
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
Packit Service 084de1
                                       * 16-bit */
Packit Service 084de1
    c->Nl = l;
Packit Service 084de1
Packit Service 084de1
    n = c->num;
Packit Service 084de1
    if (n != 0) {
Packit Service 084de1
        p = (unsigned char *)c->data;
Packit Service 084de1
Packit Service 084de1
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
Packit Service 084de1
            memcpy(p + n, data, HASH_CBLOCK - n);
Packit Service 084de1
            HASH_BLOCK_DATA_ORDER(c, p, 1);
Packit Service 084de1
            n = HASH_CBLOCK - n;
Packit Service 084de1
            data += n;
Packit Service 084de1
            len -= n;
Packit Service 084de1
            c->num = 0;
Packit Service 084de1
            /*
Packit Service 084de1
             * We use memset rather than OPENSSL_cleanse() here deliberately.
Packit Service 084de1
             * Using OPENSSL_cleanse() here could be a performance issue. It
Packit Service 084de1
             * will get properly cleansed on finalisation so this isn't a
Packit Service 084de1
             * security problem.
Packit Service 084de1
             */
Packit Service 084de1
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
Packit Service 084de1
        } else {
Packit Service 084de1
            memcpy(p + n, data, len);
Packit Service 084de1
            c->num += (unsigned int)len;
Packit Service 084de1
            return 1;
Packit Service 084de1
        }
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    n = len / HASH_CBLOCK;
Packit Service 084de1
    if (n > 0) {
Packit Service 084de1
        HASH_BLOCK_DATA_ORDER(c, data, n);
Packit Service 084de1
        n *= HASH_CBLOCK;
Packit Service 084de1
        data += n;
Packit Service 084de1
        len -= n;
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    if (len != 0) {
Packit Service 084de1
        p = (unsigned char *)c->data;
Packit Service 084de1
        c->num = (unsigned int)len;
Packit Service 084de1
        memcpy(p, data, len);
Packit Service 084de1
    }
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
Packit Service 084de1
{
Packit Service 084de1
    HASH_BLOCK_DATA_ORDER(c, data, 1);
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
int HASH_FINAL(unsigned char *md, HASH_CTX *c)
Packit Service 084de1
{
Packit Service 084de1
    unsigned char *p = (unsigned char *)c->data;
Packit Service 084de1
    size_t n = c->num;
Packit Service 084de1
Packit Service 084de1
    p[n] = 0x80;                /* there is always room for one */
Packit Service 084de1
    n++;
Packit Service 084de1
Packit Service 084de1
    if (n > (HASH_CBLOCK - 8)) {
Packit Service 084de1
        memset(p + n, 0, HASH_CBLOCK - n);
Packit Service 084de1
        n = 0;
Packit Service 084de1
        HASH_BLOCK_DATA_ORDER(c, p, 1);
Packit Service 084de1
    }
Packit Service 084de1
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
Packit Service 084de1
Packit Service 084de1
    p += HASH_CBLOCK - 8;
Packit Service 084de1
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
Packit Service 084de1
    (void)HOST_l2c(c->Nh, p);
Packit Service 084de1
    (void)HOST_l2c(c->Nl, p);
Packit Service 084de1
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
Packit Service 084de1
    (void)HOST_l2c(c->Nl, p);
Packit Service 084de1
    (void)HOST_l2c(c->Nh, p);
Packit Service 084de1
#endif
Packit Service 084de1
    p -= HASH_CBLOCK;
Packit Service 084de1
    HASH_BLOCK_DATA_ORDER(c, p, 1);
Packit Service 084de1
    c->num = 0;
Packit Service 084de1
    OPENSSL_cleanse(p, HASH_CBLOCK);
Packit Service 084de1
Packit Service 084de1
#ifndef HASH_MAKE_STRING
Packit Service 084de1
# error "HASH_MAKE_STRING must be defined!"
Packit Service 084de1
#else
Packit Service 084de1
    HASH_MAKE_STRING(c, md);
Packit Service 084de1
#endif
Packit Service 084de1
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
#ifndef MD32_REG_T
Packit Service 084de1
# if defined(__alpha) || defined(__sparcv9) || defined(__mips)
Packit Service 084de1
#  define MD32_REG_T long
Packit Service 084de1
/*
Packit Service 084de1
 * This comment was originally written for MD5, which is why it
Packit Service 084de1
 * discusses A-D. But it basically applies to all 32-bit digests,
Packit Service 084de1
 * which is why it was moved to common header file.
Packit Service 084de1
 *
Packit Service 084de1
 * In case you wonder why A-D are declared as long and not
Packit Service 084de1
 * as MD5_LONG. Doing so results in slight performance
Packit Service 084de1
 * boost on LP64 architectures. The catch is we don't
Packit Service 084de1
 * really care if 32 MSBs of a 64-bit register get polluted
Packit Service 084de1
 * with eventual overflows as we *save* only 32 LSBs in
Packit Service 084de1
 * *either* case. Now declaring 'em long excuses the compiler
Packit Service 084de1
 * from keeping 32 MSBs zeroed resulting in 13% performance
Packit Service 084de1
 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
Packit Service 084de1
 * Well, to be honest it should say that this *prevents*
Packit Service 084de1
 * performance degradation.
Packit Service 084de1
 */
Packit Service 084de1
# else
Packit Service 084de1
/*
Packit Service 084de1
 * Above is not absolute and there are LP64 compilers that
Packit Service 084de1
 * generate better code if MD32_REG_T is defined int. The above
Packit Service 084de1
 * pre-processor condition reflects the circumstances under which
Packit Service 084de1
 * the conclusion was made and is subject to further extension.
Packit Service 084de1
 */
Packit Service 084de1
#  define MD32_REG_T int
Packit Service 084de1
# endif
Packit Service 084de1
#endif