2016-05-17 18:24:46 +00:00
|
|
|
/*
|
2018-01-17 03:20:22 +00:00
|
|
|
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2003-03-20 23:32:16 +00:00
|
|
|
|
2015-01-27 17:34:45 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/sha.h>
|
2016-03-18 18:30:20 +00:00
|
|
|
#include <openssl/rsa.h>
|
2019-09-27 21:57:58 +00:00
|
|
|
#include "crypto/evp.h"
|
|
|
|
#include "crypto/sha.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2001-09-02 20:05:27 +00:00
|
|
|
static int init(EVP_MD_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA1_Init(EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
|
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int final(EVP_MD_CTX *ctx, unsigned char *md)
|
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 14:20:58 +00:00
|
|
|
static int ctrl(EVP_MD_CTX *ctx, int cmd, int mslen, void *ms)
|
|
|
|
{
|
|
|
|
unsigned char padtmp[40];
|
|
|
|
unsigned char sha1tmp[SHA_DIGEST_LENGTH];
|
|
|
|
|
2016-11-22 21:59:21 +00:00
|
|
|
SHA_CTX *sha1;
|
2015-11-24 14:20:58 +00:00
|
|
|
|
|
|
|
if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
|
2016-11-22 21:59:21 +00:00
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
2015-11-24 14:20:58 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-11-22 21:59:21 +00:00
|
|
|
sha1 = EVP_MD_CTX_md_data(ctx);
|
|
|
|
|
2015-11-24 14:20:58 +00:00
|
|
|
/* SSLv3 client auth handling: see RFC-6101 5.6.8 */
|
|
|
|
if (mslen != 48)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* At this point hash contains all handshake messages, update
|
|
|
|
* with master secret and pad_1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (SHA1_Update(sha1, ms, mslen) <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Set padtmp to pad_1 value */
|
|
|
|
memset(padtmp, 0x36, sizeof(padtmp));
|
|
|
|
|
|
|
|
if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!SHA1_Final(sha1tmp, sha1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Reinitialise context */
|
|
|
|
|
|
|
|
if (!SHA1_Init(sha1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (SHA1_Update(sha1, ms, mslen) <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Set padtmp to pad_2 value */
|
|
|
|
memset(padtmp, 0x5c, sizeof(padtmp));
|
|
|
|
|
|
|
|
if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Now when ctx is finalised it will return the SSL v3 hash value */
|
|
|
|
OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static const EVP_MD sha1_md = {
|
|
|
|
NID_sha1,
|
|
|
|
NID_sha1WithRSAEncryption,
|
|
|
|
SHA_DIGEST_LENGTH,
|
2015-12-02 13:57:04 +00:00
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
2015-01-22 03:40:55 +00:00
|
|
|
init,
|
|
|
|
update,
|
|
|
|
final,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA_CTX),
|
2015-11-24 14:20:58 +00:00
|
|
|
ctrl
|
2015-01-22 03:40:55 +00:00
|
|
|
};
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2001-03-09 02:51:02 +00:00
|
|
|
const EVP_MD *EVP_sha1(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return &sha1_md;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2004-05-31 13:14:08 +00:00
|
|
|
|
|
|
|
static int init224(EVP_MD_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA224_Init(EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 03:43:15 +00:00
|
|
|
static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
|
|
|
|
{
|
|
|
|
return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int final224(EVP_MD_CTX *ctx, unsigned char *md)
|
|
|
|
{
|
|
|
|
return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
|
|
|
|
}
|
|
|
|
|
2004-05-31 13:14:08 +00:00
|
|
|
static int init256(EVP_MD_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA256_Init(EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
|
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int final256(EVP_MD_CTX *ctx, unsigned char *md)
|
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_MD sha224_md = {
|
|
|
|
NID_sha224,
|
|
|
|
NID_sha224WithRSAEncryption,
|
|
|
|
SHA224_DIGEST_LENGTH,
|
2015-12-02 13:57:04 +00:00
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
2015-01-22 03:40:55 +00:00
|
|
|
init224,
|
2018-02-14 03:43:15 +00:00
|
|
|
update224,
|
|
|
|
final224,
|
2015-01-22 03:40:55 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA256_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA256_CTX),
|
|
|
|
};
|
2004-05-31 13:14:08 +00:00
|
|
|
|
|
|
|
const EVP_MD *EVP_sha224(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return &sha224_md;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_MD sha256_md = {
|
|
|
|
NID_sha256,
|
|
|
|
NID_sha256WithRSAEncryption,
|
|
|
|
SHA256_DIGEST_LENGTH,
|
2015-12-02 13:57:04 +00:00
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
2015-01-22 03:40:55 +00:00
|
|
|
init256,
|
|
|
|
update256,
|
|
|
|
final256,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA256_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA256_CTX),
|
|
|
|
};
|
2004-05-31 13:14:08 +00:00
|
|
|
|
|
|
|
const EVP_MD *EVP_sha256(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return &sha256_md;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2004-05-31 13:14:08 +00:00
|
|
|
|
2018-01-17 03:20:22 +00:00
|
|
|
static int init512_224(EVP_MD_CTX *ctx)
|
|
|
|
{
|
|
|
|
return sha512_224_init(EVP_MD_CTX_md_data(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init512_256(EVP_MD_CTX *ctx)
|
|
|
|
{
|
|
|
|
return sha512_256_init(EVP_MD_CTX_md_data(ctx));
|
|
|
|
}
|
|
|
|
|
2004-05-31 13:14:08 +00:00
|
|
|
static int init384(EVP_MD_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA384_Init(EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 03:43:15 +00:00
|
|
|
static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
|
|
|
|
{
|
|
|
|
return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int final384(EVP_MD_CTX *ctx, unsigned char *md)
|
|
|
|
{
|
|
|
|
return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
|
|
|
|
}
|
|
|
|
|
2004-05-31 13:14:08 +00:00
|
|
|
static int init512(EVP_MD_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA512_Init(EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2004-05-31 13:14:08 +00:00
|
|
|
/* See comment in SHA224/256 section */
|
2015-01-22 03:40:55 +00:00
|
|
|
static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
|
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int final512(EVP_MD_CTX *ctx, unsigned char *md)
|
|
|
|
{
|
2015-11-27 13:02:12 +00:00
|
|
|
return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 03:20:22 +00:00
|
|
|
static const EVP_MD sha512_224_md = {
|
|
|
|
NID_sha512_224,
|
|
|
|
NID_sha512_224WithRSAEncryption,
|
|
|
|
SHA224_DIGEST_LENGTH,
|
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
|
|
|
init512_224,
|
|
|
|
update512,
|
|
|
|
final512,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA512_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
|
|
|
};
|
|
|
|
|
|
|
|
const EVP_MD *EVP_sha512_224(void)
|
|
|
|
{
|
|
|
|
return &sha512_224_md;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_MD sha512_256_md = {
|
|
|
|
NID_sha512_256,
|
|
|
|
NID_sha512_256WithRSAEncryption,
|
|
|
|
SHA256_DIGEST_LENGTH,
|
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
|
|
|
init512_256,
|
|
|
|
update512,
|
|
|
|
final512,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA512_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
|
|
|
};
|
|
|
|
|
|
|
|
const EVP_MD *EVP_sha512_256(void)
|
|
|
|
{
|
|
|
|
return &sha512_256_md;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static const EVP_MD sha384_md = {
|
|
|
|
NID_sha384,
|
|
|
|
NID_sha384WithRSAEncryption,
|
|
|
|
SHA384_DIGEST_LENGTH,
|
2015-12-02 13:57:04 +00:00
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
2015-01-22 03:40:55 +00:00
|
|
|
init384,
|
2018-02-14 03:43:15 +00:00
|
|
|
update384,
|
|
|
|
final384,
|
2015-01-22 03:40:55 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA512_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
|
|
|
};
|
2004-05-31 13:14:08 +00:00
|
|
|
|
|
|
|
const EVP_MD *EVP_sha384(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return &sha384_md;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_MD sha512_md = {
|
|
|
|
NID_sha512,
|
|
|
|
NID_sha512WithRSAEncryption,
|
|
|
|
SHA512_DIGEST_LENGTH,
|
2015-12-02 13:57:04 +00:00
|
|
|
EVP_MD_FLAG_DIGALGID_ABSENT,
|
2015-01-22 03:40:55 +00:00
|
|
|
init512,
|
|
|
|
update512,
|
|
|
|
final512,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
SHA512_CBLOCK,
|
|
|
|
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
|
|
|
};
|
2004-05-31 13:14:08 +00:00
|
|
|
|
|
|
|
const EVP_MD *EVP_sha512(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return &sha512_md;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|