155714Skris/* crypto/evp/m_sha1.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
61160814Ssimon
62238405Sjkim#ifndef OPENSSL_FIPS
63238405Sjkim
64160814Ssimon#ifndef OPENSSL_NO_SHA
65160814Ssimon
6655714Skris#include <openssl/evp.h>
6755714Skris#include <openssl/objects.h>
68246772Sjkim#include <openssl/sha.h>
69160814Ssimon#ifndef OPENSSL_NO_RSA
70160814Ssimon#include <openssl/rsa.h>
71160814Ssimon#endif
7255714Skris
73194206Ssimon
74109998Smarkmstatic int init(EVP_MD_CTX *ctx)
75109998Smarkm	{ return SHA1_Init(ctx->md_data); }
76109998Smarkm
77160814Ssimonstatic int update(EVP_MD_CTX *ctx,const void *data,size_t count)
78109998Smarkm	{ return SHA1_Update(ctx->md_data,data,count); }
79109998Smarkm
80109998Smarkmstatic int final(EVP_MD_CTX *ctx,unsigned char *md)
81109998Smarkm	{ return SHA1_Final(md,ctx->md_data); }
82109998Smarkm
83109998Smarkmstatic const EVP_MD sha1_md=
8455714Skris	{
8555714Skris	NID_sha1,
8655714Skris	NID_sha1WithRSAEncryption,
8755714Skris	SHA_DIGEST_LENGTH,
88238405Sjkim	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT,
89109998Smarkm	init,
90109998Smarkm	update,
91109998Smarkm	final,
92109998Smarkm	NULL,
93109998Smarkm	NULL,
9455714Skris	EVP_PKEY_RSA_method,
9555714Skris	SHA_CBLOCK,
9655714Skris	sizeof(EVP_MD *)+sizeof(SHA_CTX),
9755714Skris	};
9855714Skris
99109998Smarkmconst EVP_MD *EVP_sha1(void)
10055714Skris	{
10155714Skris	return(&sha1_md);
10255714Skris	}
103238405Sjkim#endif
104160814Ssimon
105160814Ssimon#ifndef OPENSSL_NO_SHA256
106160814Ssimonstatic int init224(EVP_MD_CTX *ctx)
107160814Ssimon	{ return SHA224_Init(ctx->md_data); }
108160814Ssimonstatic int init256(EVP_MD_CTX *ctx)
109160814Ssimon	{ return SHA256_Init(ctx->md_data); }
110160814Ssimon/*
111160814Ssimon * Even though there're separate SHA224_[Update|Final], we call
112160814Ssimon * SHA256 functions even in SHA224 context. This is what happens
113160814Ssimon * there anyway, so we can spare few CPU cycles:-)
114160814Ssimon */
115160814Ssimonstatic int update256(EVP_MD_CTX *ctx,const void *data,size_t count)
116160814Ssimon	{ return SHA256_Update(ctx->md_data,data,count); }
117160814Ssimonstatic int final256(EVP_MD_CTX *ctx,unsigned char *md)
118160814Ssimon	{ return SHA256_Final(md,ctx->md_data); }
119160814Ssimon
120160814Ssimonstatic const EVP_MD sha224_md=
121160814Ssimon	{
122160814Ssimon	NID_sha224,
123160814Ssimon	NID_sha224WithRSAEncryption,
124160814Ssimon	SHA224_DIGEST_LENGTH,
125238405Sjkim	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT,
126160814Ssimon	init224,
127160814Ssimon	update256,
128160814Ssimon	final256,
129160814Ssimon	NULL,
130160814Ssimon	NULL,
131160814Ssimon	EVP_PKEY_RSA_method,
132160814Ssimon	SHA256_CBLOCK,
133160814Ssimon	sizeof(EVP_MD *)+sizeof(SHA256_CTX),
134160814Ssimon	};
135160814Ssimon
136160814Ssimonconst EVP_MD *EVP_sha224(void)
137160814Ssimon	{ return(&sha224_md); }
138160814Ssimon
139160814Ssimonstatic const EVP_MD sha256_md=
140160814Ssimon	{
141160814Ssimon	NID_sha256,
142160814Ssimon	NID_sha256WithRSAEncryption,
143160814Ssimon	SHA256_DIGEST_LENGTH,
144238405Sjkim	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT,
145160814Ssimon	init256,
146160814Ssimon	update256,
147160814Ssimon	final256,
148160814Ssimon	NULL,
149160814Ssimon	NULL,
150160814Ssimon	EVP_PKEY_RSA_method,
151160814Ssimon	SHA256_CBLOCK,
152160814Ssimon	sizeof(EVP_MD *)+sizeof(SHA256_CTX),
153160814Ssimon	};
154160814Ssimon
155160814Ssimonconst EVP_MD *EVP_sha256(void)
156160814Ssimon	{ return(&sha256_md); }
157160814Ssimon#endif	/* ifndef OPENSSL_NO_SHA256 */
158160814Ssimon
159160814Ssimon#ifndef OPENSSL_NO_SHA512
160160814Ssimonstatic int init384(EVP_MD_CTX *ctx)
161160814Ssimon	{ return SHA384_Init(ctx->md_data); }
162160814Ssimonstatic int init512(EVP_MD_CTX *ctx)
163160814Ssimon	{ return SHA512_Init(ctx->md_data); }
164160814Ssimon/* See comment in SHA224/256 section */
165160814Ssimonstatic int update512(EVP_MD_CTX *ctx,const void *data,size_t count)
166160814Ssimon	{ return SHA512_Update(ctx->md_data,data,count); }
167160814Ssimonstatic int final512(EVP_MD_CTX *ctx,unsigned char *md)
168160814Ssimon	{ return SHA512_Final(md,ctx->md_data); }
169160814Ssimon
170160814Ssimonstatic const EVP_MD sha384_md=
171160814Ssimon	{
172160814Ssimon	NID_sha384,
173160814Ssimon	NID_sha384WithRSAEncryption,
174160814Ssimon	SHA384_DIGEST_LENGTH,
175238405Sjkim	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT,
176160814Ssimon	init384,
177160814Ssimon	update512,
178160814Ssimon	final512,
179160814Ssimon	NULL,
180160814Ssimon	NULL,
181160814Ssimon	EVP_PKEY_RSA_method,
182160814Ssimon	SHA512_CBLOCK,
183160814Ssimon	sizeof(EVP_MD *)+sizeof(SHA512_CTX),
184160814Ssimon	};
185160814Ssimon
186160814Ssimonconst EVP_MD *EVP_sha384(void)
187160814Ssimon	{ return(&sha384_md); }
188160814Ssimon
189160814Ssimonstatic const EVP_MD sha512_md=
190160814Ssimon	{
191160814Ssimon	NID_sha512,
192160814Ssimon	NID_sha512WithRSAEncryption,
193160814Ssimon	SHA512_DIGEST_LENGTH,
194238405Sjkim	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT,
195160814Ssimon	init512,
196160814Ssimon	update512,
197160814Ssimon	final512,
198160814Ssimon	NULL,
199160814Ssimon	NULL,
200160814Ssimon	EVP_PKEY_RSA_method,
201160814Ssimon	SHA512_CBLOCK,
202160814Ssimon	sizeof(EVP_MD *)+sizeof(SHA512_CTX),
203160814Ssimon	};
204160814Ssimon
205160814Ssimonconst EVP_MD *EVP_sha512(void)
206160814Ssimon	{ return(&sha512_md); }
207160814Ssimon#endif	/* ifndef OPENSSL_NO_SHA512 */
208194206Ssimon
209194206Ssimon#endif
210