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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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
64280304Sjkim# ifndef OPENSSL_NO_SHA
65160814Ssimon
66280304Sjkim#  include <openssl/evp.h>
67280304Sjkim#  include <openssl/objects.h>
68280304Sjkim#  include <openssl/sha.h>
69280304Sjkim#  ifndef OPENSSL_NO_RSA
70280304Sjkim#   include <openssl/rsa.h>
71280304Sjkim#  endif
7255714Skris
73109998Smarkmstatic int init(EVP_MD_CTX *ctx)
74280304Sjkim{
75280304Sjkim    return SHA1_Init(ctx->md_data);
76280304Sjkim}
77109998Smarkm
78280304Sjkimstatic int update(EVP_MD_CTX *ctx, const void *data, size_t count)
79280304Sjkim{
80280304Sjkim    return SHA1_Update(ctx->md_data, data, count);
81280304Sjkim}
82109998Smarkm
83280304Sjkimstatic int final(EVP_MD_CTX *ctx, unsigned char *md)
84280304Sjkim{
85280304Sjkim    return SHA1_Final(md, ctx->md_data);
86280304Sjkim}
87109998Smarkm
88280304Sjkimstatic const EVP_MD sha1_md = {
89280304Sjkim    NID_sha1,
90280304Sjkim    NID_sha1WithRSAEncryption,
91280304Sjkim    SHA_DIGEST_LENGTH,
92280304Sjkim    EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
93280304Sjkim    init,
94280304Sjkim    update,
95280304Sjkim    final,
96280304Sjkim    NULL,
97280304Sjkim    NULL,
98280304Sjkim    EVP_PKEY_RSA_method,
99280304Sjkim    SHA_CBLOCK,
100280304Sjkim    sizeof(EVP_MD *) + sizeof(SHA_CTX),
101280304Sjkim};
10255714Skris
103109998Smarkmconst EVP_MD *EVP_sha1(void)
104280304Sjkim{
105280304Sjkim    return (&sha1_md);
106280304Sjkim}
107280304Sjkim# endif
108160814Ssimon
109280304Sjkim# ifndef OPENSSL_NO_SHA256
110160814Ssimonstatic int init224(EVP_MD_CTX *ctx)
111280304Sjkim{
112280304Sjkim    return SHA224_Init(ctx->md_data);
113280304Sjkim}
114280304Sjkim
115160814Ssimonstatic int init256(EVP_MD_CTX *ctx)
116280304Sjkim{
117280304Sjkim    return SHA256_Init(ctx->md_data);
118280304Sjkim}
119280304Sjkim
120160814Ssimon/*
121160814Ssimon * Even though there're separate SHA224_[Update|Final], we call
122160814Ssimon * SHA256 functions even in SHA224 context. This is what happens
123160814Ssimon * there anyway, so we can spare few CPU cycles:-)
124160814Ssimon */
125280304Sjkimstatic int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
126280304Sjkim{
127280304Sjkim    return SHA256_Update(ctx->md_data, data, count);
128280304Sjkim}
129160814Ssimon
130280304Sjkimstatic int final256(EVP_MD_CTX *ctx, unsigned char *md)
131280304Sjkim{
132280304Sjkim    return SHA256_Final(md, ctx->md_data);
133280304Sjkim}
134160814Ssimon
135280304Sjkimstatic const EVP_MD sha224_md = {
136280304Sjkim    NID_sha224,
137280304Sjkim    NID_sha224WithRSAEncryption,
138280304Sjkim    SHA224_DIGEST_LENGTH,
139280304Sjkim    EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
140280304Sjkim    init224,
141280304Sjkim    update256,
142280304Sjkim    final256,
143280304Sjkim    NULL,
144280304Sjkim    NULL,
145280304Sjkim    EVP_PKEY_RSA_method,
146280304Sjkim    SHA256_CBLOCK,
147280304Sjkim    sizeof(EVP_MD *) + sizeof(SHA256_CTX),
148280304Sjkim};
149280304Sjkim
150160814Ssimonconst EVP_MD *EVP_sha224(void)
151280304Sjkim{
152280304Sjkim    return (&sha224_md);
153280304Sjkim}
154160814Ssimon
155280304Sjkimstatic const EVP_MD sha256_md = {
156280304Sjkim    NID_sha256,
157280304Sjkim    NID_sha256WithRSAEncryption,
158280304Sjkim    SHA256_DIGEST_LENGTH,
159280304Sjkim    EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
160280304Sjkim    init256,
161280304Sjkim    update256,
162280304Sjkim    final256,
163280304Sjkim    NULL,
164280304Sjkim    NULL,
165280304Sjkim    EVP_PKEY_RSA_method,
166280304Sjkim    SHA256_CBLOCK,
167280304Sjkim    sizeof(EVP_MD *) + sizeof(SHA256_CTX),
168280304Sjkim};
169160814Ssimon
170160814Ssimonconst EVP_MD *EVP_sha256(void)
171280304Sjkim{
172280304Sjkim    return (&sha256_md);
173280304Sjkim}
174280304Sjkim# endif                         /* ifndef OPENSSL_NO_SHA256 */
175160814Ssimon
176280304Sjkim# ifndef OPENSSL_NO_SHA512
177160814Ssimonstatic int init384(EVP_MD_CTX *ctx)
178280304Sjkim{
179280304Sjkim    return SHA384_Init(ctx->md_data);
180280304Sjkim}
181280304Sjkim
182160814Ssimonstatic int init512(EVP_MD_CTX *ctx)
183280304Sjkim{
184280304Sjkim    return SHA512_Init(ctx->md_data);
185280304Sjkim}
186280304Sjkim
187160814Ssimon/* See comment in SHA224/256 section */
188280304Sjkimstatic int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
189280304Sjkim{
190280304Sjkim    return SHA512_Update(ctx->md_data, data, count);
191280304Sjkim}
192160814Ssimon
193280304Sjkimstatic int final512(EVP_MD_CTX *ctx, unsigned char *md)
194280304Sjkim{
195280304Sjkim    return SHA512_Final(md, ctx->md_data);
196280304Sjkim}
197160814Ssimon
198280304Sjkimstatic const EVP_MD sha384_md = {
199280304Sjkim    NID_sha384,
200280304Sjkim    NID_sha384WithRSAEncryption,
201280304Sjkim    SHA384_DIGEST_LENGTH,
202280304Sjkim    EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
203280304Sjkim    init384,
204280304Sjkim    update512,
205280304Sjkim    final512,
206280304Sjkim    NULL,
207280304Sjkim    NULL,
208280304Sjkim    EVP_PKEY_RSA_method,
209280304Sjkim    SHA512_CBLOCK,
210280304Sjkim    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
211280304Sjkim};
212280304Sjkim
213160814Ssimonconst EVP_MD *EVP_sha384(void)
214280304Sjkim{
215280304Sjkim    return (&sha384_md);
216280304Sjkim}
217160814Ssimon
218280304Sjkimstatic const EVP_MD sha512_md = {
219280304Sjkim    NID_sha512,
220280304Sjkim    NID_sha512WithRSAEncryption,
221280304Sjkim    SHA512_DIGEST_LENGTH,
222280304Sjkim    EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
223280304Sjkim    init512,
224280304Sjkim    update512,
225280304Sjkim    final512,
226280304Sjkim    NULL,
227280304Sjkim    NULL,
228280304Sjkim    EVP_PKEY_RSA_method,
229280304Sjkim    SHA512_CBLOCK,
230280304Sjkim    sizeof(EVP_MD *) + sizeof(SHA512_CTX),
231280304Sjkim};
232160814Ssimon
233160814Ssimonconst EVP_MD *EVP_sha512(void)
234280304Sjkim{
235280304Sjkim    return (&sha512_md);
236280304Sjkim}
237280304Sjkim# endif                         /* ifndef OPENSSL_NO_SHA512 */
238194206Ssimon
239194206Ssimon#endif
240