1  | 
     | 
     | 
    /* $OpenBSD: m_md5_sha1.c,v 1.1 2017/02/28 14:15:37 jsing Exp $ */  | 
    
    
    2  | 
     | 
     | 
    /*  | 
    
    
    3  | 
     | 
     | 
     * Copyright (c) 2017 Joel Sing <jsing@openbsd.org>  | 
    
    
    4  | 
     | 
     | 
     *  | 
    
    
    5  | 
     | 
     | 
     * Permission to use, copy, modify, and distribute this software for any  | 
    
    
    6  | 
     | 
     | 
     * purpose with or without fee is hereby granted, provided that the above  | 
    
    
    7  | 
     | 
     | 
     * copyright notice and this permission notice appear in all copies.  | 
    
    
    8  | 
     | 
     | 
     *  | 
    
    
    9  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
    
    
    10  | 
     | 
     | 
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
    
    
    11  | 
     | 
     | 
     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
    
    
    12  | 
     | 
     | 
     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
    
    
    13  | 
     | 
     | 
     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
    
    
    14  | 
     | 
     | 
     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
    
    
    15  | 
     | 
     | 
     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
    
    
    16  | 
     | 
     | 
     */  | 
    
    
    17  | 
     | 
     | 
     | 
    
    
    18  | 
     | 
     | 
    #include <openssl/evp.h>  | 
    
    
    19  | 
     | 
     | 
    #include <openssl/md5.h>  | 
    
    
    20  | 
     | 
     | 
    #include <openssl/objects.h>  | 
    
    
    21  | 
     | 
     | 
    #include <openssl/sha.h>  | 
    
    
    22  | 
     | 
     | 
     | 
    
    
    23  | 
     | 
     | 
    struct md5_sha1_ctx { | 
    
    
    24  | 
     | 
     | 
    	MD5_CTX md5;  | 
    
    
    25  | 
     | 
     | 
    	SHA_CTX sha1;  | 
    
    
    26  | 
     | 
     | 
    };  | 
    
    
    27  | 
     | 
     | 
     | 
    
    
    28  | 
     | 
     | 
    static int  | 
    
    
    29  | 
     | 
     | 
    md5_sha1_init(EVP_MD_CTX *ctx)  | 
    
    
    30  | 
     | 
     | 
    { | 
    
    
    31  | 
     | 
    700  | 
    	struct md5_sha1_ctx *mdctx = ctx->md_data;  | 
    
    
    32  | 
     | 
     | 
     | 
    
    
    33  | 
    ✗✓ | 
    350  | 
    	if (!MD5_Init(&mdctx->md5))  | 
    
    
    34  | 
     | 
     | 
    		return 0;  | 
    
    
    35  | 
    ✗✓ | 
    350  | 
    	if (!SHA1_Init(&mdctx->sha1))  | 
    
    
    36  | 
     | 
     | 
    		return 0;  | 
    
    
    37  | 
     | 
     | 
     | 
    
    
    38  | 
     | 
    350  | 
    	return 1;  | 
    
    
    39  | 
     | 
    350  | 
    }  | 
    
    
    40  | 
     | 
     | 
     | 
    
    
    41  | 
     | 
     | 
    static int  | 
    
    
    42  | 
     | 
     | 
    md5_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)  | 
    
    
    43  | 
     | 
     | 
    { | 
    
    
    44  | 
     | 
    4628  | 
    	struct md5_sha1_ctx *mdctx = ctx->md_data;  | 
    
    
    45  | 
     | 
     | 
     | 
    
    
    46  | 
    ✗✓ | 
    2314  | 
    	if (!MD5_Update(&mdctx->md5, data, count))  | 
    
    
    47  | 
     | 
     | 
    		return 0;  | 
    
    
    48  | 
    ✗✓ | 
    2314  | 
    	if (!SHA1_Update(&mdctx->sha1, data, count))  | 
    
    
    49  | 
     | 
     | 
    		return 0;  | 
    
    
    50  | 
     | 
     | 
     | 
    
    
    51  | 
     | 
    2314  | 
    	return 1;  | 
    
    
    52  | 
     | 
    2314  | 
    }  | 
    
    
    53  | 
     | 
     | 
     | 
    
    
    54  | 
     | 
     | 
    static int  | 
    
    
    55  | 
     | 
     | 
    md5_sha1_final(EVP_MD_CTX *ctx, unsigned char *out)  | 
    
    
    56  | 
     | 
     | 
    { | 
    
    
    57  | 
     | 
    1716  | 
    	struct md5_sha1_ctx *mdctx = ctx->md_data;  | 
    
    
    58  | 
     | 
     | 
     | 
    
    
    59  | 
    ✗✓ | 
    858  | 
    	if (!MD5_Final(out, &mdctx->md5))  | 
    
    
    60  | 
     | 
     | 
    		return 0;  | 
    
    
    61  | 
    ✗✓ | 
    858  | 
    	if (!SHA1_Final(out + MD5_DIGEST_LENGTH, &mdctx->sha1))  | 
    
    
    62  | 
     | 
     | 
    		return 0;  | 
    
    
    63  | 
     | 
     | 
     | 
    
    
    64  | 
     | 
    858  | 
    	return 1;  | 
    
    
    65  | 
     | 
    858  | 
    }  | 
    
    
    66  | 
     | 
     | 
     | 
    
    
    67  | 
     | 
     | 
    static const EVP_MD md5_sha1_md = { | 
    
    
    68  | 
     | 
     | 
            .type = NID_md5_sha1,  | 
    
    
    69  | 
     | 
     | 
            .pkey_type = NID_md5_sha1,  | 
    
    
    70  | 
     | 
     | 
            .md_size = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,  | 
    
    
    71  | 
     | 
     | 
            .flags = 0,  | 
    
    
    72  | 
     | 
     | 
            .init = md5_sha1_init,  | 
    
    
    73  | 
     | 
     | 
            .update = md5_sha1_update,  | 
    
    
    74  | 
     | 
     | 
            .final = md5_sha1_final,  | 
    
    
    75  | 
     | 
     | 
            .block_size = MD5_CBLOCK, /* MD5_CBLOCK == SHA_CBLOCK */  | 
    
    
    76  | 
     | 
     | 
            .ctx_size = sizeof(EVP_MD *) + sizeof(struct md5_sha1_ctx),  | 
    
    
    77  | 
     | 
     | 
    };  | 
    
    
    78  | 
     | 
     | 
     | 
    
    
    79  | 
     | 
     | 
    const EVP_MD *  | 
    
    
    80  | 
     | 
     | 
    EVP_md5_sha1(void)  | 
    
    
    81  | 
     | 
     | 
    { | 
    
    
    82  | 
     | 
    14644  | 
    	return &md5_sha1_md;  | 
    
    
    83  | 
     | 
     | 
    }  |