m_sha1.c revision 296465
12061Sjkh/* crypto/evp/m_sha1.c */
22279Spaul/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
32061Sjkh * All rights reserved.
42061Sjkh *
52061Sjkh * This package is an SSL implementation written
62061Sjkh * by Eric Young (eay@cryptsoft.com).
72061Sjkh * The implementation was written so as to conform with Netscapes SSL.
82061Sjkh *
92160Scsgr * This library is free for commercial and non-commercial use as long as
102160Scsgr * the following conditions are aheared to.  The following conditions
112061Sjkh * apply to all code found in this distribution, be it the RC4, RSA,
122061Sjkh * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
132160Scsgr * included with this distribution is covered by the same copyright terms
142061Sjkh * except that the holder is Tim Hudson (tjh@cryptsoft.com).
152160Scsgr *
161594Srgrimes * Copyright remains Eric Young's, and as such any Copyright notices in
172061Sjkh * the code are not to be removed.
182160Scsgr * If this package is used in a product, Eric Young should be given attribution
192061Sjkh * as the author of the parts of the library used.
201594Srgrimes * This can be in the form of a textual message at program startup or
212061Sjkh * in documentation (online or textual) provided with the package.
222061Sjkh *
232061Sjkh * Redistribution and use in source and binary forms, with or without
242061Sjkh * modification, are permitted provided that the following conditions
252061Sjkh * are met:
262061Sjkh * 1. Redistributions of source code must retain the copyright
272061Sjkh *    notice, this list of conditions and the following disclaimer.
282061Sjkh * 2. Redistributions in binary form must reproduce the above copyright
292061Sjkh *    notice, this list of conditions and the following disclaimer in the
302061Sjkh *    documentation and/or other materials provided with the distribution.
312061Sjkh * 3. All advertising materials mentioning features or use of this software
322061Sjkh *    must display the following acknowledgement:
332061Sjkh *    "This product includes cryptographic software written by
342061Sjkh *     Eric Young (eay@cryptsoft.com)"
352061Sjkh *    The word 'cryptographic' can be left out if the rouines from the library
362061Sjkh *    being used are not cryptographic related :-).
372061Sjkh * 4. If you include any Windows specific code (or a derivative thereof) from
382061Sjkh *    the apps directory (application code) you must include an acknowledgement:
392061Sjkh *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
402061Sjkh *
412061Sjkh * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
422160Scsgr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
432061Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
442061Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
452061Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
462061Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
472061Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
482061Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
492061Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
502061Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
512160Scsgr * SUCH DAMAGE.
522160Scsgr *
532160Scsgr * The licence and distribution terms for any publically available version or
542061Sjkh * derivative of this code cannot be changed.  i.e. this code cannot simply be
552061Sjkh * copied and put under another distribution licence
562061Sjkh * [including the GNU Public Licence.]
572061Sjkh */
582061Sjkh
592061Sjkh#include <stdio.h>
602061Sjkh#include "cryptlib.h"
612061Sjkh
622061Sjkh#ifndef OPENSSL_NO_SHA
632061Sjkh
642061Sjkh# include <openssl/evp.h>
652061Sjkh# include <openssl/objects.h>
661594Srgrimes# include <openssl/x509.h>
672061Sjkh# ifndef OPENSSL_NO_RSA
682061Sjkh#  include <openssl/rsa.h>
692061Sjkh# endif
702061Sjkh
712061Sjkh# ifndef OPENSSL_FIPS
722061Sjkh
732061Sjkhstatic int init(EVP_MD_CTX *ctx)
742061Sjkh{
752061Sjkh    return SHA1_Init(ctx->md_data);
762061Sjkh}
772061Sjkh
782061Sjkhstatic int update(EVP_MD_CTX *ctx, const void *data, size_t count)
792061Sjkh{
802061Sjkh    return SHA1_Update(ctx->md_data, data, count);
812061Sjkh}
822061Sjkh
832061Sjkhstatic int final(EVP_MD_CTX *ctx, unsigned char *md)
842061Sjkh{
852061Sjkh    return SHA1_Final(md, ctx->md_data);
862061Sjkh}
872061Sjkh
882279Spaulstatic const EVP_MD sha1_md = {
892061Sjkh    NID_sha1,
902061Sjkh    NID_sha1WithRSAEncryption,
912061Sjkh    SHA_DIGEST_LENGTH,
922061Sjkh    0,
932061Sjkh    init,
942061Sjkh    update,
952061Sjkh    final,
962061Sjkh    NULL,
972061Sjkh    NULL,
982061Sjkh    EVP_PKEY_RSA_method,
992061Sjkh    SHA_CBLOCK,
1002061Sjkh    sizeof(EVP_MD *) + sizeof(SHA_CTX),
1012061Sjkh};
1022061Sjkh
1032061Sjkhconst EVP_MD *EVP_sha1(void)
1042061Sjkh{
1052061Sjkh    return (&sha1_md);
1062061Sjkh}
1072061Sjkh
1082061Sjkh#  ifndef OPENSSL_NO_SHA256
1092061Sjkhstatic int init224(EVP_MD_CTX *ctx)
1102061Sjkh{
1112061Sjkh    return SHA224_Init(ctx->md_data);
1122061Sjkh}
1132061Sjkh
1142061Sjkhstatic int init256(EVP_MD_CTX *ctx)
1152061Sjkh{
1162061Sjkh    return SHA256_Init(ctx->md_data);
1172061Sjkh}
1182061Sjkh
1192061Sjkh/*
1202061Sjkh * Even though there're separate SHA224_[Update|Final], we call
1212061Sjkh * SHA256 functions even in SHA224 context. This is what happens
1222061Sjkh * there anyway, so we can spare few CPU cycles:-)
1232061Sjkh */
1242061Sjkhstatic int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
1252061Sjkh{
1262061Sjkh    return SHA256_Update(ctx->md_data, data, count);
1272061Sjkh}
1282061Sjkh
1292061Sjkhstatic int final256(EVP_MD_CTX *ctx, unsigned char *md)
1302061Sjkh{
1312061Sjkh    return SHA256_Final(md, ctx->md_data);
1322061Sjkh}
1332061Sjkh
1342061Sjkhstatic const EVP_MD sha224_md = {
1352061Sjkh    NID_sha224,
1362061Sjkh    NID_sha224WithRSAEncryption,
1372061Sjkh    SHA224_DIGEST_LENGTH,
1382061Sjkh    0,
1392061Sjkh    init224,
1402061Sjkh    update256,
1412061Sjkh    final256,
1422061Sjkh    NULL,
1432061Sjkh    NULL,
1442061Sjkh    EVP_PKEY_RSA_method,
1452061Sjkh    SHA256_CBLOCK,
1462061Sjkh    sizeof(EVP_MD *) + sizeof(SHA256_CTX),
1472068Sjkh};
1482273Spaul
1492160Scsgrconst EVP_MD *EVP_sha224(void)
1502061Sjkh{
1512061Sjkh    return (&sha224_md);
1522061Sjkh}
1532061Sjkh
1542061Sjkhstatic const EVP_MD sha256_md = {
1552061Sjkh    NID_sha256,
1562061Sjkh    NID_sha256WithRSAEncryption,
1572061Sjkh    SHA256_DIGEST_LENGTH,
1582061Sjkh    0,
1592061Sjkh    init256,
1602061Sjkh    update256,
1612061Sjkh    final256,
1622061Sjkh    NULL,
1632061Sjkh    NULL,
1642061Sjkh    EVP_PKEY_RSA_method,
1652061Sjkh    SHA256_CBLOCK,
1662061Sjkh    sizeof(EVP_MD *) + sizeof(SHA256_CTX),
1672061Sjkh};
1682061Sjkh
1692061Sjkhconst EVP_MD *EVP_sha256(void)
1702061Sjkh{
1712061Sjkh    return (&sha256_md);
1722061Sjkh}
1732061Sjkh#  endif                        /* ifndef OPENSSL_NO_SHA256 */
1742061Sjkh
1752273Spaul#  ifndef OPENSSL_NO_SHA512
1762061Sjkhstatic int init384(EVP_MD_CTX *ctx)
1772160Scsgr{
1782160Scsgr    return SHA384_Init(ctx->md_data);
1792160Scsgr}
1802160Scsgr
1812279Spaulstatic int init512(EVP_MD_CTX *ctx)
1822279Spaul{
1832279Spaul    return SHA512_Init(ctx->md_data);
1842279Spaul}
1852279Spaul
1862061Sjkh/* See comment in SHA224/256 section */
1872061Sjkhstatic int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
1882279Spaul{
1892061Sjkh    return SHA512_Update(ctx->md_data, data, count);
1902061Sjkh}
1912061Sjkh
1922061Sjkhstatic int final512(EVP_MD_CTX *ctx, unsigned char *md)
1932160Scsgr{
1942061Sjkh    return SHA512_Final(md, ctx->md_data);
1952061Sjkh}
1962061Sjkh
1972061Sjkhstatic const EVP_MD sha384_md = {
1982061Sjkh    NID_sha384,
1992061Sjkh    NID_sha384WithRSAEncryption,
2002061Sjkh    SHA384_DIGEST_LENGTH,
2012061Sjkh    0,
2022061Sjkh    init384,
2032061Sjkh    update512,
2042061Sjkh    final512,
2052061Sjkh    NULL,
2062061Sjkh    NULL,
2072061Sjkh    EVP_PKEY_RSA_method,
2082061Sjkh    SHA512_CBLOCK,
2092273Spaul    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
2102061Sjkh};
2112061Sjkh
2122061Sjkhconst EVP_MD *EVP_sha384(void)
2132061Sjkh{
2141594Srgrimes    return (&sha384_md);
215}
216
217static const EVP_MD sha512_md = {
218    NID_sha512,
219    NID_sha512WithRSAEncryption,
220    SHA512_DIGEST_LENGTH,
221    0,
222    init512,
223    update512,
224    final512,
225    NULL,
226    NULL,
227    EVP_PKEY_RSA_method,
228    SHA512_CBLOCK,
229    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
230};
231
232const EVP_MD *EVP_sha512(void)
233{
234    return (&sha512_md);
235}
236#  endif                        /* ifndef OPENSSL_NO_SHA512 */
237
238# endif
239
240#endif
241