mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Add more hashing apis to redbean
This commit is contained in:
parent
0ea2907730
commit
8d5f60a9cd
10 changed files with 319 additions and 238 deletions
|
@ -16,6 +16,8 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/hyperion.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "third_party/mbedtls/aes.h"
|
||||
#include "third_party/mbedtls/base64.h"
|
||||
|
@ -41,8 +43,7 @@
|
|||
#include "third_party/mbedtls/x509.h"
|
||||
|
||||
#ifdef MBEDTLS_SELF_TEST
|
||||
|
||||
TEST(mbedtls, test) {
|
||||
TEST(mbedtls, selfTest) {
|
||||
#ifdef MBEDTLS_DES_C
|
||||
EXPECT_EQ(0, mbedtls_des_self_test(0));
|
||||
#endif
|
||||
|
@ -110,5 +111,77 @@ TEST(mbedtls, test) {
|
|||
EXPECT_EQ(0, mbedtls_nist_kw_self_test(0));
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
TEST(md5, test) {
|
||||
uint8_t d[16];
|
||||
uint8_t want[16] = {0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
|
||||
0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72};
|
||||
mbedtls_md5_ret("abc", 3, d);
|
||||
EXPECT_EQ(0, memcmp(want, d, 16));
|
||||
}
|
||||
|
||||
TEST(sha1, test) {
|
||||
uint8_t d[20];
|
||||
uint8_t want[20] = {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81,
|
||||
0x6A, 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50,
|
||||
0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D};
|
||||
mbedtls_sha1_ret("abc", 3, d);
|
||||
EXPECT_EQ(0, memcmp(want, d, 20));
|
||||
}
|
||||
|
||||
TEST(sha224, test) {
|
||||
uint8_t d[28];
|
||||
uint8_t want[28] = {0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8,
|
||||
0x22, 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2,
|
||||
0x55, 0xB3, 0x2A, 0xAD, 0xBC, 0xE4, 0xBD,
|
||||
0xA0, 0xB3, 0xF7, 0xE3, 0x6C, 0x9D, 0xA7};
|
||||
mbedtls_sha256_ret("abc", 3, d, 1);
|
||||
EXPECT_EQ(0, memcmp(want, d, 28));
|
||||
}
|
||||
|
||||
TEST(sha256, test) {
|
||||
uint8_t d[32];
|
||||
uint8_t want[32] = {0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
|
||||
0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
|
||||
0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
|
||||
0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD};
|
||||
mbedtls_sha256_ret("abc", 3, d, 0);
|
||||
EXPECT_EQ(0, memcmp(want, d, 32));
|
||||
}
|
||||
|
||||
TEST(sha384, test) {
|
||||
uint8_t d[48];
|
||||
uint8_t want[48] = {
|
||||
0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, 0xB5, 0xA0, 0x3D, 0x69,
|
||||
0x9A, 0xC6, 0x50, 0x07, 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
|
||||
0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, 0x80, 0x86, 0x07, 0x2B,
|
||||
0xA1, 0xE7, 0xCC, 0x23, 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7};
|
||||
mbedtls_sha512_ret("abc", 3, d, 1);
|
||||
EXPECT_EQ(0, memcmp(want, d, 48));
|
||||
}
|
||||
|
||||
TEST(sha512, test) {
|
||||
uint8_t d[64];
|
||||
uint8_t want[64] = {
|
||||
0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, 0xCC, 0x41, 0x73,
|
||||
0x49, 0xAE, 0x20, 0x41, 0x31, 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9,
|
||||
0x7E, 0xA2, 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, 0x21,
|
||||
0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, 0x36, 0xBA, 0x3C, 0x23,
|
||||
0xA3, 0xFE, 0xEB, 0xBD, 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8,
|
||||
0x0E, 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F};
|
||||
mbedtls_sha512_ret("abc", 3, d, 0);
|
||||
EXPECT_EQ(0, memcmp(want, d, 64));
|
||||
}
|
||||
|
||||
BENCH(mbedtls, bench) {
|
||||
uint8_t d[64];
|
||||
EZBENCH2("md5", donothing, mbedtls_md5_ret(kHyperion, kHyperionSize, d));
|
||||
EZBENCH2("sha1", donothing, mbedtls_sha1_ret(kHyperion, kHyperionSize, d));
|
||||
EZBENCH2("sha256", donothing,
|
||||
mbedtls_sha256_ret(kHyperion, kHyperionSize, d, 0));
|
||||
EZBENCH2("sha384", donothing,
|
||||
mbedtls_sha512_ret(kHyperion, kHyperionSize, d, 1));
|
||||
EZBENCH2("sha512", donothing,
|
||||
mbedtls_sha512_ret(kHyperion, kHyperionSize, d, 0));
|
||||
}
|
||||
|
|
182
third_party/mbedtls/md5.c
vendored
182
third_party/mbedtls/md5.c
vendored
|
@ -125,134 +125,108 @@ int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
|
|||
int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
|
||||
const unsigned char data[64] )
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint32_t X[16], A, B, C, D;
|
||||
} local;
|
||||
register uint32_t A, B, C, D;
|
||||
|
||||
GET_UINT32_LE( local.X[ 0], data, 0 );
|
||||
GET_UINT32_LE( local.X[ 1], data, 4 );
|
||||
GET_UINT32_LE( local.X[ 2], data, 8 );
|
||||
GET_UINT32_LE( local.X[ 3], data, 12 );
|
||||
GET_UINT32_LE( local.X[ 4], data, 16 );
|
||||
GET_UINT32_LE( local.X[ 5], data, 20 );
|
||||
GET_UINT32_LE( local.X[ 6], data, 24 );
|
||||
GET_UINT32_LE( local.X[ 7], data, 28 );
|
||||
GET_UINT32_LE( local.X[ 8], data, 32 );
|
||||
GET_UINT32_LE( local.X[ 9], data, 36 );
|
||||
GET_UINT32_LE( local.X[10], data, 40 );
|
||||
GET_UINT32_LE( local.X[11], data, 44 );
|
||||
GET_UINT32_LE( local.X[12], data, 48 );
|
||||
GET_UINT32_LE( local.X[13], data, 52 );
|
||||
GET_UINT32_LE( local.X[14], data, 56 );
|
||||
GET_UINT32_LE( local.X[15], data, 60 );
|
||||
|
||||
#define S(x,n) \
|
||||
#define S(x,n) \
|
||||
( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) )
|
||||
|
||||
#define P(a,b,c,d,k,s,t) \
|
||||
do \
|
||||
{ \
|
||||
(a) += F((b),(c),(d)) + local.X[(k)] + (t); \
|
||||
(a) = S((a),(s)) + (b); \
|
||||
} while( 0 )
|
||||
#define P(a,b,c,d,k,s,t) \
|
||||
a += F(b, c, d) + READ32LE(data + k * 4) + (t); \
|
||||
a = S(a, s) + b
|
||||
|
||||
local.A = ctx->state[0];
|
||||
local.B = ctx->state[1];
|
||||
local.C = ctx->state[2];
|
||||
local.D = ctx->state[3];
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
|
||||
#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
|
||||
P( local.A, local.B, local.C, local.D, 0, 7, 0xD76AA478 );
|
||||
P( local.D, local.A, local.B, local.C, 1, 12, 0xE8C7B756 );
|
||||
P( local.C, local.D, local.A, local.B, 2, 17, 0x242070DB );
|
||||
P( local.B, local.C, local.D, local.A, 3, 22, 0xC1BDCEEE );
|
||||
P( local.A, local.B, local.C, local.D, 4, 7, 0xF57C0FAF );
|
||||
P( local.D, local.A, local.B, local.C, 5, 12, 0x4787C62A );
|
||||
P( local.C, local.D, local.A, local.B, 6, 17, 0xA8304613 );
|
||||
P( local.B, local.C, local.D, local.A, 7, 22, 0xFD469501 );
|
||||
P( local.A, local.B, local.C, local.D, 8, 7, 0x698098D8 );
|
||||
P( local.D, local.A, local.B, local.C, 9, 12, 0x8B44F7AF );
|
||||
P( local.C, local.D, local.A, local.B, 10, 17, 0xFFFF5BB1 );
|
||||
P( local.B, local.C, local.D, local.A, 11, 22, 0x895CD7BE );
|
||||
P( local.A, local.B, local.C, local.D, 12, 7, 0x6B901122 );
|
||||
P( local.D, local.A, local.B, local.C, 13, 12, 0xFD987193 );
|
||||
P( local.C, local.D, local.A, local.B, 14, 17, 0xA679438E );
|
||||
P( local.B, local.C, local.D, local.A, 15, 22, 0x49B40821 );
|
||||
P( A, B, C, D, 0, 7, 0xD76AA478 );
|
||||
P( D, A, B, C, 1, 12, 0xE8C7B756 );
|
||||
P( C, D, A, B, 2, 17, 0x242070DB );
|
||||
P( B, C, D, A, 3, 22, 0xC1BDCEEE );
|
||||
P( A, B, C, D, 4, 7, 0xF57C0FAF );
|
||||
P( D, A, B, C, 5, 12, 0x4787C62A );
|
||||
P( C, D, A, B, 6, 17, 0xA8304613 );
|
||||
P( B, C, D, A, 7, 22, 0xFD469501 );
|
||||
P( A, B, C, D, 8, 7, 0x698098D8 );
|
||||
P( D, A, B, C, 9, 12, 0x8B44F7AF );
|
||||
P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
|
||||
P( B, C, D, A, 11, 22, 0x895CD7BE );
|
||||
P( A, B, C, D, 12, 7, 0x6B901122 );
|
||||
P( D, A, B, C, 13, 12, 0xFD987193 );
|
||||
P( C, D, A, B, 14, 17, 0xA679438E );
|
||||
P( B, C, D, A, 15, 22, 0x49B40821 );
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) ((y) ^ ((z) & ((x) ^ (y))))
|
||||
|
||||
P( local.A, local.B, local.C, local.D, 1, 5, 0xF61E2562 );
|
||||
P( local.D, local.A, local.B, local.C, 6, 9, 0xC040B340 );
|
||||
P( local.C, local.D, local.A, local.B, 11, 14, 0x265E5A51 );
|
||||
P( local.B, local.C, local.D, local.A, 0, 20, 0xE9B6C7AA );
|
||||
P( local.A, local.B, local.C, local.D, 5, 5, 0xD62F105D );
|
||||
P( local.D, local.A, local.B, local.C, 10, 9, 0x02441453 );
|
||||
P( local.C, local.D, local.A, local.B, 15, 14, 0xD8A1E681 );
|
||||
P( local.B, local.C, local.D, local.A, 4, 20, 0xE7D3FBC8 );
|
||||
P( local.A, local.B, local.C, local.D, 9, 5, 0x21E1CDE6 );
|
||||
P( local.D, local.A, local.B, local.C, 14, 9, 0xC33707D6 );
|
||||
P( local.C, local.D, local.A, local.B, 3, 14, 0xF4D50D87 );
|
||||
P( local.B, local.C, local.D, local.A, 8, 20, 0x455A14ED );
|
||||
P( local.A, local.B, local.C, local.D, 13, 5, 0xA9E3E905 );
|
||||
P( local.D, local.A, local.B, local.C, 2, 9, 0xFCEFA3F8 );
|
||||
P( local.C, local.D, local.A, local.B, 7, 14, 0x676F02D9 );
|
||||
P( local.B, local.C, local.D, local.A, 12, 20, 0x8D2A4C8A );
|
||||
P( A, B, C, D, 1, 5, 0xF61E2562 );
|
||||
P( D, A, B, C, 6, 9, 0xC040B340 );
|
||||
P( C, D, A, B, 11, 14, 0x265E5A51 );
|
||||
P( B, C, D, A, 0, 20, 0xE9B6C7AA );
|
||||
P( A, B, C, D, 5, 5, 0xD62F105D );
|
||||
P( D, A, B, C, 10, 9, 0x02441453 );
|
||||
P( C, D, A, B, 15, 14, 0xD8A1E681 );
|
||||
P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
|
||||
P( A, B, C, D, 9, 5, 0x21E1CDE6 );
|
||||
P( D, A, B, C, 14, 9, 0xC33707D6 );
|
||||
P( C, D, A, B, 3, 14, 0xF4D50D87 );
|
||||
P( B, C, D, A, 8, 20, 0x455A14ED );
|
||||
P( A, B, C, D, 13, 5, 0xA9E3E905 );
|
||||
P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
|
||||
P( C, D, A, B, 7, 14, 0x676F02D9 );
|
||||
P( B, C, D, A, 12, 20, 0x8D2A4C8A );
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) ((x) ^ (y) ^ (z))
|
||||
|
||||
P( local.A, local.B, local.C, local.D, 5, 4, 0xFFFA3942 );
|
||||
P( local.D, local.A, local.B, local.C, 8, 11, 0x8771F681 );
|
||||
P( local.C, local.D, local.A, local.B, 11, 16, 0x6D9D6122 );
|
||||
P( local.B, local.C, local.D, local.A, 14, 23, 0xFDE5380C );
|
||||
P( local.A, local.B, local.C, local.D, 1, 4, 0xA4BEEA44 );
|
||||
P( local.D, local.A, local.B, local.C, 4, 11, 0x4BDECFA9 );
|
||||
P( local.C, local.D, local.A, local.B, 7, 16, 0xF6BB4B60 );
|
||||
P( local.B, local.C, local.D, local.A, 10, 23, 0xBEBFBC70 );
|
||||
P( local.A, local.B, local.C, local.D, 13, 4, 0x289B7EC6 );
|
||||
P( local.D, local.A, local.B, local.C, 0, 11, 0xEAA127FA );
|
||||
P( local.C, local.D, local.A, local.B, 3, 16, 0xD4EF3085 );
|
||||
P( local.B, local.C, local.D, local.A, 6, 23, 0x04881D05 );
|
||||
P( local.A, local.B, local.C, local.D, 9, 4, 0xD9D4D039 );
|
||||
P( local.D, local.A, local.B, local.C, 12, 11, 0xE6DB99E5 );
|
||||
P( local.C, local.D, local.A, local.B, 15, 16, 0x1FA27CF8 );
|
||||
P( local.B, local.C, local.D, local.A, 2, 23, 0xC4AC5665 );
|
||||
P( A, B, C, D, 5, 4, 0xFFFA3942 );
|
||||
P( D, A, B, C, 8, 11, 0x8771F681 );
|
||||
P( C, D, A, B, 11, 16, 0x6D9D6122 );
|
||||
P( B, C, D, A, 14, 23, 0xFDE5380C );
|
||||
P( A, B, C, D, 1, 4, 0xA4BEEA44 );
|
||||
P( D, A, B, C, 4, 11, 0x4BDECFA9 );
|
||||
P( C, D, A, B, 7, 16, 0xF6BB4B60 );
|
||||
P( B, C, D, A, 10, 23, 0xBEBFBC70 );
|
||||
P( A, B, C, D, 13, 4, 0x289B7EC6 );
|
||||
P( D, A, B, C, 0, 11, 0xEAA127FA );
|
||||
P( C, D, A, B, 3, 16, 0xD4EF3085 );
|
||||
P( B, C, D, A, 6, 23, 0x04881D05 );
|
||||
P( A, B, C, D, 9, 4, 0xD9D4D039 );
|
||||
P( D, A, B, C, 12, 11, 0xE6DB99E5 );
|
||||
P( C, D, A, B, 15, 16, 0x1FA27CF8 );
|
||||
P( B, C, D, A, 2, 23, 0xC4AC5665 );
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) ((y) ^ ((x) | ~(z)))
|
||||
|
||||
P( local.A, local.B, local.C, local.D, 0, 6, 0xF4292244 );
|
||||
P( local.D, local.A, local.B, local.C, 7, 10, 0x432AFF97 );
|
||||
P( local.C, local.D, local.A, local.B, 14, 15, 0xAB9423A7 );
|
||||
P( local.B, local.C, local.D, local.A, 5, 21, 0xFC93A039 );
|
||||
P( local.A, local.B, local.C, local.D, 12, 6, 0x655B59C3 );
|
||||
P( local.D, local.A, local.B, local.C, 3, 10, 0x8F0CCC92 );
|
||||
P( local.C, local.D, local.A, local.B, 10, 15, 0xFFEFF47D );
|
||||
P( local.B, local.C, local.D, local.A, 1, 21, 0x85845DD1 );
|
||||
P( local.A, local.B, local.C, local.D, 8, 6, 0x6FA87E4F );
|
||||
P( local.D, local.A, local.B, local.C, 15, 10, 0xFE2CE6E0 );
|
||||
P( local.C, local.D, local.A, local.B, 6, 15, 0xA3014314 );
|
||||
P( local.B, local.C, local.D, local.A, 13, 21, 0x4E0811A1 );
|
||||
P( local.A, local.B, local.C, local.D, 4, 6, 0xF7537E82 );
|
||||
P( local.D, local.A, local.B, local.C, 11, 10, 0xBD3AF235 );
|
||||
P( local.C, local.D, local.A, local.B, 2, 15, 0x2AD7D2BB );
|
||||
P( local.B, local.C, local.D, local.A, 9, 21, 0xEB86D391 );
|
||||
P( A, B, C, D, 0, 6, 0xF4292244 );
|
||||
P( D, A, B, C, 7, 10, 0x432AFF97 );
|
||||
P( C, D, A, B, 14, 15, 0xAB9423A7 );
|
||||
P( B, C, D, A, 5, 21, 0xFC93A039 );
|
||||
P( A, B, C, D, 12, 6, 0x655B59C3 );
|
||||
P( D, A, B, C, 3, 10, 0x8F0CCC92 );
|
||||
P( C, D, A, B, 10, 15, 0xFFEFF47D );
|
||||
P( B, C, D, A, 1, 21, 0x85845DD1 );
|
||||
P( A, B, C, D, 8, 6, 0x6FA87E4F );
|
||||
P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
|
||||
P( C, D, A, B, 6, 15, 0xA3014314 );
|
||||
P( B, C, D, A, 13, 21, 0x4E0811A1 );
|
||||
P( A, B, C, D, 4, 6, 0xF7537E82 );
|
||||
P( D, A, B, C, 11, 10, 0xBD3AF235 );
|
||||
P( C, D, A, B, 2, 15, 0x2AD7D2BB );
|
||||
P( B, C, D, A, 9, 21, 0xEB86D391 );
|
||||
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += local.A;
|
||||
ctx->state[1] += local.B;
|
||||
ctx->state[2] += local.C;
|
||||
ctx->state[3] += local.D;
|
||||
|
||||
/* Zeroise variables to clear sensitive data from memory. */
|
||||
mbedtls_platform_zeroize( &local, sizeof( local ) );
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
@ -401,7 +375,7 @@ int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
|
|||
* constitutes a security risk. We recommend considering
|
||||
* stronger message digests instead.
|
||||
*/
|
||||
int mbedtls_md5_ret( const unsigned char *input,
|
||||
int mbedtls_md5_ret( const void *input,
|
||||
size_t ilen,
|
||||
unsigned char output[16] )
|
||||
{
|
||||
|
|
2
third_party/mbedtls/md5.h
vendored
2
third_party/mbedtls/md5.h
vendored
|
@ -29,7 +29,7 @@ int mbedtls_md5_starts_ret( mbedtls_md5_context * );
|
|||
int mbedtls_md5_update_ret( mbedtls_md5_context *, const unsigned char *, size_t );
|
||||
int mbedtls_md5_finish_ret( mbedtls_md5_context *, unsigned char[16] );
|
||||
int mbedtls_internal_md5_process( mbedtls_md5_context *, const unsigned char[64] );
|
||||
int mbedtls_md5_ret( const unsigned char *, size_t, unsigned char[16] );
|
||||
int mbedtls_md5_ret( const void *, size_t, unsigned char[16] );
|
||||
int mbedtls_md5_self_test( int );
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
2
third_party/mbedtls/sha1.c
vendored
2
third_party/mbedtls/sha1.c
vendored
|
@ -544,7 +544,7 @@ int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
|
|||
* \return A negative error code on failure.
|
||||
*
|
||||
*/
|
||||
int mbedtls_sha1_ret( const unsigned char *input,
|
||||
int mbedtls_sha1_ret( const void *input,
|
||||
size_t ilen,
|
||||
unsigned char output[20] )
|
||||
{
|
||||
|
|
2
third_party/mbedtls/sha1.h
vendored
2
third_party/mbedtls/sha1.h
vendored
|
@ -31,7 +31,7 @@ int mbedtls_sha1_starts_ret( mbedtls_sha1_context * );
|
|||
int mbedtls_sha1_update_ret( mbedtls_sha1_context *, const unsigned char *, size_t );
|
||||
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *, unsigned char[20] );
|
||||
int mbedtls_internal_sha1_process( mbedtls_sha1_context *, const unsigned char[64] );
|
||||
int mbedtls_sha1_ret( const unsigned char *, size_t, unsigned char[20] );
|
||||
int mbedtls_sha1_ret( const void *, size_t, unsigned char[20] );
|
||||
int mbedtls_sha1_self_test( int );
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
97
third_party/mbedtls/sha256.c
vendored
97
third_party/mbedtls/sha256.c
vendored
|
@ -47,18 +47,36 @@ void sha256_transform_rorx(mbedtls_sha256_context *, const uint8_t *, int);
|
|||
|
||||
#if !defined(MBEDTLS_SHA256_ALT)
|
||||
|
||||
/**
|
||||
* \brief This function initializes a SHA-256 context.
|
||||
*
|
||||
* \param ctx The SHA-256 context to initialize. This must not be \c NULL.
|
||||
*/
|
||||
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
|
||||
{
|
||||
SHA256_VALIDATE( ctx != NULL );
|
||||
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief This function clears a SHA-256 context.
|
||||
*
|
||||
* \param ctx The SHA-256 context to clear. This may be \c NULL, in which
|
||||
* case this function returns immediately. If it is not \c NULL,
|
||||
* it must point to an initialized SHA-256 context.
|
||||
*/
|
||||
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
|
||||
{
|
||||
if( ctx == NULL ) return;
|
||||
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief This function clones the state of a SHA-256 context.
|
||||
*
|
||||
* \param dst The destination context. This must be initialized.
|
||||
* \param src The context to clone. This must be initialized.
|
||||
*/
|
||||
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
|
||||
const mbedtls_sha256_context *src )
|
||||
{
|
||||
|
@ -67,8 +85,16 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
|
|||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 context setup
|
||||
/**
|
||||
* \brief This function starts a SHA-224 or SHA-256 checksum
|
||||
* calculation.
|
||||
*
|
||||
* \param ctx The context to use. This must be initialized.
|
||||
* \param is224 This determines which function to use. This must be
|
||||
* either \c 0 for SHA-256, or \c 1 for SHA-224.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
|
||||
{
|
||||
|
@ -155,6 +181,18 @@ static const uint32_t K[] =
|
|||
(d) += local.temp1; (h) = local.temp1 + local.temp2; \
|
||||
} while( 0 )
|
||||
|
||||
/**
|
||||
* \brief This function processes a single data block within
|
||||
* the ongoing SHA-256 computation. This function is for
|
||||
* internal use only.
|
||||
*
|
||||
* \param ctx The SHA-256 context. This must be initialized.
|
||||
* \param data The buffer holding one block of data. This must
|
||||
* be a readable buffer of length \c 64 Bytes.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
|
||||
const unsigned char data[64] )
|
||||
{
|
||||
|
@ -243,8 +281,18 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
|
|||
|
||||
#endif /* !MBEDTLS_SHA256_PROCESS_ALT */
|
||||
|
||||
/*
|
||||
* SHA-256 process buffer
|
||||
/**
|
||||
* \brief This function feeds an input buffer into an ongoing
|
||||
* SHA-256 checksum calculation.
|
||||
*
|
||||
* \param ctx The SHA-256 context. This must be initialized
|
||||
* and have a hash operation started.
|
||||
* \param input The buffer holding the data. This must be a readable
|
||||
* buffer of length \p ilen Bytes.
|
||||
* \param ilen The length of the input data in Bytes.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
|
||||
const unsigned char *input,
|
||||
|
@ -302,8 +350,17 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
|
|||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 final digest
|
||||
/**
|
||||
* \brief This function finishes the SHA-256 operation, and writes
|
||||
* the result to the output buffer.
|
||||
*
|
||||
* \param ctx The SHA-256 context. This must be initialized
|
||||
* and have a hash operation started.
|
||||
* \param output The SHA-224 or SHA-256 checksum result.
|
||||
* This must be a writable buffer of length \c 32 Bytes.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
|
||||
unsigned char output[32] )
|
||||
|
@ -370,10 +427,25 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
|
|||
|
||||
#endif /* !MBEDTLS_SHA256_ALT */
|
||||
|
||||
/*
|
||||
* output = SHA-256( input buffer )
|
||||
/**
|
||||
* \brief This function calculates the SHA-224 or SHA-256
|
||||
* checksum of a buffer.
|
||||
*
|
||||
* The function allocates the context, performs the
|
||||
* calculation, and frees the context.
|
||||
*
|
||||
* The SHA-256 result is calculated as
|
||||
* output = SHA-256(input buffer).
|
||||
*
|
||||
* \param input The buffer holding the data. This must be a readable
|
||||
* buffer of length \p ilen Bytes.
|
||||
* \param ilen The length of the input data in Bytes.
|
||||
* \param output The SHA-224 or SHA-256 checksum result. This must
|
||||
* be a writable buffer of length \c 32 Bytes.
|
||||
* \param is224 Determines which function to use. This must be
|
||||
* either \c 0 for SHA-256, or \c 1 for SHA-224.
|
||||
*/
|
||||
int mbedtls_sha256_ret( const unsigned char *input,
|
||||
int mbedtls_sha256_ret( const void *input,
|
||||
size_t ilen,
|
||||
unsigned char output[32],
|
||||
int is224 )
|
||||
|
@ -453,8 +525,11 @@ static const unsigned char sha256_test_sum[6][32] =
|
|||
0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
|
||||
};
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
/**
|
||||
* \brief The SHA-224 and SHA-256 checkup routine.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return \c 1 on failure.
|
||||
*/
|
||||
int mbedtls_sha256_self_test( int verbose )
|
||||
{
|
||||
|
|
124
third_party/mbedtls/sha256.h
vendored
124
third_party/mbedtls/sha256.h
vendored
|
@ -24,121 +24,15 @@ typedef struct mbedtls_sha256_context
|
|||
}
|
||||
mbedtls_sha256_context;
|
||||
|
||||
/**
|
||||
* \brief This function initializes a SHA-256 context.
|
||||
*
|
||||
* \param ctx The SHA-256 context to initialize. This must not be \c NULL.
|
||||
*/
|
||||
void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief This function clears a SHA-256 context.
|
||||
*
|
||||
* \param ctx The SHA-256 context to clear. This may be \c NULL, in which
|
||||
* case this function returns immediately. If it is not \c NULL,
|
||||
* it must point to an initialized SHA-256 context.
|
||||
*/
|
||||
void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief This function clones the state of a SHA-256 context.
|
||||
*
|
||||
* \param dst The destination context. This must be initialized.
|
||||
* \param src The context to clone. This must be initialized.
|
||||
*/
|
||||
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
|
||||
const mbedtls_sha256_context *src );
|
||||
|
||||
/**
|
||||
* \brief This function starts a SHA-224 or SHA-256 checksum
|
||||
* calculation.
|
||||
*
|
||||
* \param ctx The context to use. This must be initialized.
|
||||
* \param is224 This determines which function to use. This must be
|
||||
* either \c 0 for SHA-256, or \c 1 for SHA-224.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
|
||||
|
||||
/**
|
||||
* \brief This function feeds an input buffer into an ongoing
|
||||
* SHA-256 checksum calculation.
|
||||
*
|
||||
* \param ctx The SHA-256 context. This must be initialized
|
||||
* and have a hash operation started.
|
||||
* \param input The buffer holding the data. This must be a readable
|
||||
* buffer of length \p ilen Bytes.
|
||||
* \param ilen The length of the input data in Bytes.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen );
|
||||
|
||||
/**
|
||||
* \brief This function finishes the SHA-256 operation, and writes
|
||||
* the result to the output buffer.
|
||||
*
|
||||
* \param ctx The SHA-256 context. This must be initialized
|
||||
* and have a hash operation started.
|
||||
* \param output The SHA-224 or SHA-256 checksum result.
|
||||
* This must be a writable buffer of length \c 32 Bytes.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
|
||||
unsigned char output[32] );
|
||||
|
||||
/**
|
||||
* \brief This function processes a single data block within
|
||||
* the ongoing SHA-256 computation. This function is for
|
||||
* internal use only.
|
||||
*
|
||||
* \param ctx The SHA-256 context. This must be initialized.
|
||||
* \param data The buffer holding one block of data. This must
|
||||
* be a readable buffer of length \c 64 Bytes.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
|
||||
const unsigned char data[64] );
|
||||
|
||||
/**
|
||||
* \brief This function calculates the SHA-224 or SHA-256
|
||||
* checksum of a buffer.
|
||||
*
|
||||
* The function allocates the context, performs the
|
||||
* calculation, and frees the context.
|
||||
*
|
||||
* The SHA-256 result is calculated as
|
||||
* output = SHA-256(input buffer).
|
||||
*
|
||||
* \param input The buffer holding the data. This must be a readable
|
||||
* buffer of length \p ilen Bytes.
|
||||
* \param ilen The length of the input data in Bytes.
|
||||
* \param output The SHA-224 or SHA-256 checksum result. This must
|
||||
* be a writable buffer of length \c 32 Bytes.
|
||||
* \param is224 Determines which function to use. This must be
|
||||
* either \c 0 for SHA-256, or \c 1 for SHA-224.
|
||||
*/
|
||||
int mbedtls_sha256_ret( const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[32],
|
||||
int is224 );
|
||||
|
||||
/**
|
||||
* \brief The SHA-224 and SHA-256 checkup routine.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return \c 1 on failure.
|
||||
*/
|
||||
int mbedtls_sha256_self_test( int verbose );
|
||||
void mbedtls_sha256_init( mbedtls_sha256_context * );
|
||||
void mbedtls_sha256_free( mbedtls_sha256_context * );
|
||||
void mbedtls_sha256_clone( mbedtls_sha256_context *, const mbedtls_sha256_context * );
|
||||
int mbedtls_sha256_starts_ret( mbedtls_sha256_context *, int );
|
||||
int mbedtls_sha256_update_ret( mbedtls_sha256_context *, const unsigned char *, size_t );
|
||||
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *, unsigned char[32] );
|
||||
int mbedtls_internal_sha256_process( mbedtls_sha256_context *, const unsigned char[64] );
|
||||
int mbedtls_sha256_ret( const void *, size_t, unsigned char[32], int );
|
||||
int mbedtls_sha256_self_test( int );
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* MBEDTLS_SHA256_H_ */
|
||||
|
|
2
third_party/mbedtls/sha512.c
vendored
2
third_party/mbedtls/sha512.c
vendored
|
@ -501,7 +501,7 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
|
|||
* \return \c 0 on success.
|
||||
* \return A negative error code on failure.
|
||||
*/
|
||||
int mbedtls_sha512_ret( const unsigned char *input,
|
||||
int mbedtls_sha512_ret( const void *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384 )
|
||||
|
|
2
third_party/mbedtls/sha512.h
vendored
2
third_party/mbedtls/sha512.h
vendored
|
@ -33,7 +33,7 @@ int mbedtls_sha512_starts_ret( mbedtls_sha512_context *, int );
|
|||
int mbedtls_sha512_update_ret( mbedtls_sha512_context *, const unsigned char *, size_t );
|
||||
int mbedtls_sha512_finish_ret( mbedtls_sha512_context *, unsigned char[64] );
|
||||
int mbedtls_internal_sha512_process( mbedtls_sha512_context *, const unsigned char[128] );
|
||||
int mbedtls_sha512_ret( const unsigned char *, size_t, unsigned char[64], int );
|
||||
int mbedtls_sha512_ret( const void *, size_t, unsigned char[64], int );
|
||||
int mbedtls_sha512_self_test( int );
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
|
@ -97,10 +97,12 @@
|
|||
#include "third_party/mbedtls/ecp.h"
|
||||
#include "third_party/mbedtls/entropy.h"
|
||||
#include "third_party/mbedtls/entropy_poll.h"
|
||||
#include "third_party/mbedtls/md5.h"
|
||||
#include "third_party/mbedtls/oid.h"
|
||||
#include "third_party/mbedtls/pk.h"
|
||||
#include "third_party/mbedtls/rsa.h"
|
||||
#include "third_party/mbedtls/san.h"
|
||||
#include "third_party/mbedtls/sha1.h"
|
||||
#include "third_party/mbedtls/ssl.h"
|
||||
#include "third_party/mbedtls/x509.h"
|
||||
#include "third_party/mbedtls/x509_crt.h"
|
||||
|
@ -3905,6 +3907,63 @@ static int LuaVisualizeControlCodes(lua_State *L) {
|
|||
return LuaCoder(L, VisualizeControlCodes);
|
||||
}
|
||||
|
||||
static int Sha224(const void *p, size_t n, uint8_t d[28]) {
|
||||
return mbedtls_sha256_ret(p, n, d, 1);
|
||||
}
|
||||
|
||||
static int Sha256(const void *p, size_t n, uint8_t d[32]) {
|
||||
return mbedtls_sha256_ret(p, n, d, 0);
|
||||
}
|
||||
|
||||
static int Sha384(const void *p, size_t n, uint8_t d[48]) {
|
||||
return mbedtls_sha512_ret(p, n, d, 1);
|
||||
}
|
||||
|
||||
static int Sha512(const void *p, size_t n, uint8_t d[64]) {
|
||||
return mbedtls_sha512_ret(p, n, d, 0);
|
||||
}
|
||||
|
||||
static noinline int LuaHasherImpl(lua_State *L, size_t k,
|
||||
int H(const void *, size_t, uint8_t *)) {
|
||||
void *p;
|
||||
size_t n;
|
||||
uint8_t d[64];
|
||||
p = luaL_checklstring(L, 1, &n);
|
||||
H(p, n, d);
|
||||
lua_pushlstring(L, (void *)d, k);
|
||||
mbedtls_platform_zeroize(d, sizeof(d));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static noinline int LuaHasher(lua_State *L, size_t k,
|
||||
int H(const void *, size_t, uint8_t *)) {
|
||||
return LuaHasherImpl(L, k, H);
|
||||
}
|
||||
|
||||
static int LuaMd5(lua_State *L) {
|
||||
return LuaHasher(L, 16, mbedtls_md5_ret);
|
||||
}
|
||||
|
||||
static int LuaSha1(lua_State *L) {
|
||||
return LuaHasher(L, 20, mbedtls_sha1_ret);
|
||||
}
|
||||
|
||||
static int LuaSha224(lua_State *L) {
|
||||
return LuaHasher(L, 28, Sha224);
|
||||
}
|
||||
|
||||
static int LuaSha256(lua_State *L) {
|
||||
return LuaHasher(L, 32, Sha256);
|
||||
}
|
||||
|
||||
static int LuaSha384(lua_State *L) {
|
||||
return LuaHasher(L, 48, Sha384);
|
||||
}
|
||||
|
||||
static int LuaSha512(lua_State *L) {
|
||||
return LuaHasher(L, 64, Sha512);
|
||||
}
|
||||
|
||||
static int LuaEncodeLatin1(lua_State *L) {
|
||||
int f;
|
||||
char *p;
|
||||
|
@ -4448,6 +4507,12 @@ static const luaL_Reg kLuaFuncs[] = {
|
|||
{"Underlong", LuaUnderlong}, //
|
||||
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
|
||||
{"Write", LuaWrite}, //
|
||||
{"Md5", LuaMd5}, //
|
||||
{"Sha1", LuaSha1}, //
|
||||
{"Sha224", LuaSha224}, //
|
||||
{"Sha256", LuaSha256}, //
|
||||
{"Sha384", LuaSha384}, //
|
||||
{"Sha512", LuaSha512}, //
|
||||
{"bsf", LuaBsf}, //
|
||||
{"bsr", LuaBsr}, //
|
||||
{"crc32", LuaCrc32}, //
|
||||
|
|
Loading…
Reference in a new issue