repo
stringlengths 1
152
⌀ | file
stringlengths 14
221
| code
stringlengths 501
25k
| file_length
int64 501
25k
| avg_line_length
float64 20
99.5
| max_line_length
int64 21
134
| extension_type
stringclasses 2
values |
---|---|---|---|---|---|---|
openssl | openssl-master/demos/kdf/argon2.c | /*
* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/kdf.h>
#include <openssl/params.h>
#include <openssl/thread.h>
/*
* Example showing how to use Argon2 KDF.
* See man EVP_KDF-ARGON2 for more information.
*
* test vector from
* https://datatracker.ietf.org/doc/html/rfc9106
*/
/*
* Hard coding a password into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char password[] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
};
/*
* The salt is better not being hard coded too. Each password should have a
* different salt if possible. The salt is not considered secret information
* and is safe to store with an encrypted password.
*/
static unsigned char salt[] = {
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02
};
/*
* Optional secret for KDF
*/
static unsigned char secret[] = {
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
};
/*
* Optional additional data
*/
static unsigned char ad[] = {
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04
};
/*
* Argon2 cost parameters
*/
static uint32_t memory_cost = 32;
static uint32_t iteration_cost = 3;
static uint32_t parallel_cost = 4;
static const unsigned char expected_output[] = {
0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c,
0x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9,
0xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e,
0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59
};
int main(int argc, char **argv)
{
int rv = EXIT_FAILURE;
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
unsigned char out[32];
OSSL_PARAM params[9], *p = params;
OSSL_LIB_CTX *library_context = NULL;
unsigned int threads;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the key derivation function implementation */
kdf = EVP_KDF_fetch(library_context, "argon2id", NULL);
if (kdf == NULL) {
fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
goto end;
}
/* Create a context for the key derivation operation */
kctx = EVP_KDF_CTX_new(kdf);
if (kctx == NULL) {
fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
goto end;
}
/*
* Thread support can be turned off; use serialization if we cannot
* set requested number of threads.
*/
threads = parallel_cost;
if (OSSL_set_max_threads(library_context, parallel_cost) != 1) {
uint64_t max_threads = OSSL_get_max_threads(library_context);
if (max_threads == 0)
threads = 1;
else if (max_threads < parallel_cost)
threads = max_threads;
}
/* Set password */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password, sizeof(password));
/* Set salt */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt));
/* Set optional additional data */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_ARGON2_AD, ad, sizeof(ad));
/* Set optional secret */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, secret, sizeof(secret));
/* Set iteration count */
*p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &iteration_cost);
/* Set threads performing derivation (can be decreased) */
*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_THREADS, &threads);
/* Set parallel cost */
*p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, ¶llel_cost);
/* Set memory requirement */
*p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &memory_cost);
*p = OSSL_PARAM_construct_end();
/* Derive the key */
if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
fprintf(stderr, "EVP_KDF_derive() failed\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated key does not match expected value\n");
goto end;
}
rv = EXIT_SUCCESS;
end:
EVP_KDF_CTX_free(kctx);
EVP_KDF_free(kdf);
OSSL_LIB_CTX_free(library_context);
return rv;
}
| 4,808 | 30.025806 | 98 | c |
openssl | openssl-master/demos/kdf/hkdf.c | /*
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/kdf.h>
#include <openssl/obj_mac.h>
#include <openssl/params.h>
/*
* test vector from
* https://datatracker.ietf.org/doc/html/rfc5869
*/
static unsigned char hkdf_salt[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c
};
static unsigned char hkdf_ikm[] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
};
static unsigned char hkdf_info[] = {
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9
};
/* Expected output keying material */
static unsigned char hkdf_okm[] = {
0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64,
0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08,
0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
};
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
unsigned char out[42];
OSSL_PARAM params[5], *p = params;
OSSL_LIB_CTX *library_context = NULL;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the key derivation function implementation */
kdf = EVP_KDF_fetch(library_context, "HKDF", NULL);
if (kdf == NULL) {
fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
goto end;
}
/* Create a context for the key derivation operation */
kctx = EVP_KDF_CTX_new(kdf);
if (kctx == NULL) {
fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
goto end;
}
/* Set the underlying hash function used to derive the key */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
"SHA256", 0);
/* Set input keying material */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, hkdf_ikm,
sizeof(hkdf_ikm));
/* Set application specific information */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, hkdf_info,
sizeof(hkdf_info));
/* Set salt */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, hkdf_salt,
sizeof(hkdf_salt));
*p = OSSL_PARAM_construct_end();
/* Derive the key */
if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
fprintf(stderr, "EVP_KDF_derive() failed\n");
goto end;
}
if (CRYPTO_memcmp(hkdf_okm, out, sizeof(hkdf_okm)) != 0) {
fprintf(stderr, "Generated key does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
EVP_KDF_CTX_free(kctx);
EVP_KDF_free(kdf);
OSSL_LIB_CTX_free(library_context);
return ret;
}
| 3,364 | 31.047619 | 76 | c |
openssl | openssl-master/demos/kdf/pbkdf2.c | /*
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/kdf.h>
#include <openssl/obj_mac.h>
#include <openssl/params.h>
/*
* test vector from
* https://datatracker.ietf.org/doc/html/rfc7914
*/
/*
* Hard coding a password into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char password[] = {
'P', 'a', 's', 's', 'w', 'o', 'r', 'd'
};
/*
* The salt is better not being hard coded too. Each password should have a
* different salt if possible. The salt is not considered secret information
* and is safe to store with an encrypted password.
*/
static unsigned char pbkdf2_salt[] = {
'N', 'a', 'C', 'l'
};
/*
* The iteration parameter can be variable or hard coded. The disadvantage with
* hard coding them is that they cannot easily be adjusted for future
* technological improvements appear.
*/
static unsigned int pbkdf2_iterations = 80000;
static const unsigned char expected_output[] = {
0x4d, 0xdc, 0xd8, 0xf6, 0x0b, 0x98, 0xbe, 0x21,
0x83, 0x0c, 0xee, 0x5e, 0xf2, 0x27, 0x01, 0xf9,
0x64, 0x1a, 0x44, 0x18, 0xd0, 0x4c, 0x04, 0x14,
0xae, 0xff, 0x08, 0x87, 0x6b, 0x34, 0xab, 0x56,
0xa1, 0xd4, 0x25, 0xa1, 0x22, 0x58, 0x33, 0x54,
0x9a, 0xdb, 0x84, 0x1b, 0x51, 0xc9, 0xb3, 0x17,
0x6a, 0x27, 0x2b, 0xde, 0xbb, 0xa1, 0xd0, 0x78,
0x47, 0x8f, 0x62, 0xb3, 0x97, 0xf3, 0x3c, 0x8d
};
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
unsigned char out[64];
OSSL_PARAM params[5], *p = params;
OSSL_LIB_CTX *library_context = NULL;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the key derivation function implementation */
kdf = EVP_KDF_fetch(library_context, "PBKDF2", NULL);
if (kdf == NULL) {
fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
goto end;
}
/* Create a context for the key derivation operation */
kctx = EVP_KDF_CTX_new(kdf);
if (kctx == NULL) {
fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
goto end;
}
/* Set password */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,
sizeof(password));
/* Set salt */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, pbkdf2_salt,
sizeof(pbkdf2_salt));
/* Set iteration count (default 2048) */
*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &pbkdf2_iterations);
/* Set the underlying hash function used to derive the key */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
"SHA256", 0);
*p = OSSL_PARAM_construct_end();
/* Derive the key */
if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
fprintf(stderr, "EVP_KDF_derive() failed\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated key does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
EVP_KDF_CTX_free(kctx);
EVP_KDF_free(kdf);
OSSL_LIB_CTX_free(library_context);
return ret;
}
| 3,751 | 30.79661 | 80 | c |
openssl | openssl-master/demos/kdf/scrypt.c | /*
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/kdf.h>
#include <openssl/obj_mac.h>
#include <openssl/params.h>
/*
* test vector from
* https://datatracker.ietf.org/doc/html/rfc7914
*/
/*
* Hard coding a password into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char password[] = {
'p', 'a', 's', 's', 'w', 'o', 'r', 'd'
};
/*
* The salt is better not being hard coded too. Each password should have a
* different salt if possible. The salt is not considered secret information
* and is safe to store with an encrypted password.
*/
static unsigned char scrypt_salt[] = {
'N', 'a', 'C', 'l'
};
/*
* The SCRYPT parameters can be variable or hard coded. The disadvantage with
* hard coding them is that they cannot easily be adjusted for future
* technological improvements appear.
*/
static unsigned int scrypt_n = 1024;
static unsigned int scrypt_r = 8;
static unsigned int scrypt_p = 16;
static const unsigned char expected_output[] = {
0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
};
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
unsigned char out[64];
OSSL_PARAM params[6], *p = params;
OSSL_LIB_CTX *library_context = NULL;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the key derivation function implementation */
kdf = EVP_KDF_fetch(library_context, "SCRYPT", NULL);
if (kdf == NULL) {
fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
goto end;
}
/* Create a context for the key derivation operation */
kctx = EVP_KDF_CTX_new(kdf);
if (kctx == NULL) {
fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
goto end;
}
/* Set password */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,
sizeof(password));
/* Set salt */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, scrypt_salt,
sizeof(scrypt_salt));
/* Set N (default 1048576) */
*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &scrypt_n);
/* Set R (default 8) */
*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &scrypt_r);
/* Set P (default 1) */
*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &scrypt_p);
*p = OSSL_PARAM_construct_end();
/* Derive the key */
if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
fprintf(stderr, "EVP_KDF_derive() failed\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated key does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
EVP_KDF_CTX_free(kctx);
EVP_KDF_free(kdf);
OSSL_LIB_CTX_free(library_context);
return ret;
}
| 3,805 | 30.454545 | 79 | c |
openssl | openssl-master/demos/keyexch/x25519.c | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
/*
* This is a demonstration of key exchange using X25519.
*
* The variables beginning `peer1_` / `peer2_` are data which would normally be
* accessible to that peer.
*
* Ordinarily you would use random keys, which are demonstrated
* below when use_kat=0. A known answer test is demonstrated
* when use_kat=1.
*/
/* A property query used for selecting the X25519 implementation. */
static const char *propq = NULL;
static const unsigned char peer1_privk_data[32] = {
0x80, 0x5b, 0x30, 0x20, 0x25, 0x4a, 0x70, 0x2c,
0xad, 0xa9, 0x8d, 0x7d, 0x47, 0xf8, 0x1b, 0x20,
0x89, 0xd2, 0xf9, 0x14, 0xac, 0x92, 0x27, 0xf2,
0x10, 0x7e, 0xdb, 0x21, 0xbd, 0x73, 0x73, 0x5d
};
static const unsigned char peer2_privk_data[32] = {
0xf8, 0x84, 0x19, 0x69, 0x79, 0x13, 0x0d, 0xbd,
0xb1, 0x76, 0xd7, 0x0e, 0x7e, 0x0f, 0xb6, 0xf4,
0x8c, 0x4a, 0x8c, 0x5f, 0xd8, 0x15, 0x09, 0x0a,
0x71, 0x78, 0x74, 0x92, 0x0f, 0x85, 0xc8, 0x43
};
static const unsigned char expected_result[32] = {
0x19, 0x71, 0x26, 0x12, 0x74, 0xb5, 0xb1, 0xce,
0x77, 0xd0, 0x79, 0x24, 0xb6, 0x0a, 0x5c, 0x72,
0x0c, 0xa6, 0x56, 0xc0, 0x11, 0xeb, 0x43, 0x11,
0x94, 0x3b, 0x01, 0x45, 0xca, 0x19, 0xfe, 0x09
};
typedef struct peer_data_st {
const char *name; /* name of peer */
EVP_PKEY *privk; /* privk generated for peer */
unsigned char pubk_data[32]; /* generated pubk to send to other peer */
unsigned char *secret; /* allocated shared secret buffer */
size_t secret_len;
} PEER_DATA;
/*
* Prepare for X25519 key exchange. The public key to be sent to the remote peer
* is put in pubk_data, which should be a 32-byte buffer. Returns 1 on success.
*/
static int keyexch_x25519_before(
OSSL_LIB_CTX *libctx,
const unsigned char *kat_privk_data,
PEER_DATA *local_peer)
{
int ret = 0;
size_t pubk_data_len = 0;
/* Generate or load X25519 key for the peer */
if (kat_privk_data != NULL)
local_peer->privk =
EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,
kat_privk_data,
sizeof(peer1_privk_data));
else
local_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");
if (local_peer->privk == NULL) {
fprintf(stderr, "Could not load or generate private key\n");
goto end;
}
/* Get public key corresponding to the private key */
if (EVP_PKEY_get_octet_string_param(local_peer->privk,
OSSL_PKEY_PARAM_PUB_KEY,
local_peer->pubk_data,
sizeof(local_peer->pubk_data),
&pubk_data_len) == 0) {
fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");
goto end;
}
/* X25519 public keys are always 32 bytes */
if (pubk_data_len != 32) {
fprintf(stderr, "EVP_PKEY_get_octet_string_param() "
"yielded wrong length\n");
goto end;
}
ret = 1;
end:
if (ret == 0) {
EVP_PKEY_free(local_peer->privk);
local_peer->privk = NULL;
}
return ret;
}
/*
* Complete X25519 key exchange. remote_peer_pubk_data should be the 32 byte
* public key value received from the remote peer. On success, returns 1 and the
* secret is pointed to by *secret. The caller must free it.
*/
static int keyexch_x25519_after(
OSSL_LIB_CTX *libctx,
int use_kat,
PEER_DATA *local_peer,
const unsigned char *remote_peer_pubk_data)
{
int ret = 0;
EVP_PKEY *remote_peer_pubk = NULL;
EVP_PKEY_CTX *ctx = NULL;
local_peer->secret = NULL;
/* Load public key for remote peer. */
remote_peer_pubk =
EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,
remote_peer_pubk_data, 32);
if (remote_peer_pubk == NULL) {
fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");
goto end;
}
/* Create key exchange context. */
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
goto end;
}
/* Initialize derivation process. */
if (EVP_PKEY_derive_init(ctx) == 0) {
fprintf(stderr, "EVP_PKEY_derive_init() failed\n");
goto end;
}
/* Configure each peer with the other peer's public key. */
if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {
fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");
goto end;
}
/* Determine the secret length. */
if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {
fprintf(stderr, "EVP_PKEY_derive() failed\n");
goto end;
}
/*
* We are using X25519, so the secret generated will always be 32 bytes.
* However for exposition, the code below demonstrates a generic
* implementation for arbitrary lengths.
*/
if (local_peer->secret_len != 32) { /* unreachable */
fprintf(stderr, "Secret is always 32 bytes for X25519\n");
goto end;
}
/* Allocate memory for shared secrets. */
local_peer->secret = OPENSSL_malloc(local_peer->secret_len);
if (local_peer->secret == NULL) {
fprintf(stderr, "Could not allocate memory for secret\n");
goto end;
}
/* Derive the shared secret. */
if (EVP_PKEY_derive(ctx, local_peer->secret,
&local_peer->secret_len) == 0) {
fprintf(stderr, "EVP_PKEY_derive() failed\n");
goto end;
}
printf("Shared secret (%s):\n", local_peer->name);
BIO_dump_indent_fp(stdout, local_peer->secret, local_peer->secret_len, 2);
putchar('\n');
ret = 1;
end:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(remote_peer_pubk);
if (ret == 0) {
OPENSSL_clear_free(local_peer->secret, local_peer->secret_len);
local_peer->secret = NULL;
}
return ret;
}
static int keyexch_x25519(int use_kat)
{
int ret = 0;
OSSL_LIB_CTX *libctx = NULL;
PEER_DATA peer1 = {"peer 1"}, peer2 = {"peer 2"};
/*
* Each peer generates its private key and sends its public key
* to the other peer. The private key is stored locally for
* later use.
*/
if (keyexch_x25519_before(libctx, use_kat ? peer1_privk_data : NULL,
&peer1) == 0)
return 0;
if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,
&peer2) == 0)
return 0;
/*
* Each peer uses the other peer's public key to perform key exchange.
* After this succeeds, each peer has the same secret in its
* PEER_DATA.
*/
if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)
return 0;
if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)
return 0;
/*
* Here we demonstrate the secrets are equal for exposition purposes.
*
* Although in practice you will generally not need to compare secrets
* produced through key exchange, if you do compare cryptographic secrets,
* always do so using a constant-time function such as CRYPTO_memcmp, never
* using memcmp(3).
*/
if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {
fprintf(stderr, "Negotiated secrets do not match\n");
goto end;
}
/* If we are doing the KAT, the secret should equal our reference result. */
if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result,
peer1.secret_len) != 0) {
fprintf(stderr, "Did not get expected result\n");
goto end;
}
ret = 1;
end:
/* The secrets are sensitive, so ensure they are erased before freeing. */
OPENSSL_clear_free(peer1.secret, peer1.secret_len);
OPENSSL_clear_free(peer2.secret, peer2.secret_len);
EVP_PKEY_free(peer1.privk);
EVP_PKEY_free(peer2.privk);
OSSL_LIB_CTX_free(libctx);
return ret;
}
int main(int argc, char **argv)
{
/* Test X25519 key exchange with known result. */
printf("Key exchange using known answer (deterministic):\n");
if (keyexch_x25519(1) == 0)
return EXIT_FAILURE;
/* Test X25519 key exchange with random keys. */
printf("Key exchange using random keys:\n");
if (keyexch_x25519(0) == 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
| 8,995 | 31.243728 | 80 | c |
openssl | openssl-master/demos/mac/cmac-aes256.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example of using EVP_MAC_ methods to calculate
* a CMAC of static buffers
*/
#include <string.h>
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/cmac.h>
#include <openssl/params.h>
/*
* Hard coding the key into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char key[] = {
0x6c, 0xde, 0x14, 0xf5, 0xd5, 0x2a, 0x4a, 0xdf,
0x12, 0x39, 0x1e, 0xbf, 0x36, 0xf9, 0x6a, 0x46,
0x48, 0xd0, 0xb6, 0x51, 0x89, 0xfc, 0x24, 0x85,
0xa8, 0x8d, 0xdf, 0x7e, 0x80, 0x14, 0xc8, 0xce,
};
static const unsigned char data[] =
"To be, or not to be, that is the question,\n"
"Whether tis nobler in the minde to suffer\n"
"The ſlings and arrowes of outragious fortune,\n"
"Or to take Armes again in a sea of troubles,\n"
"And by opposing, end them, to die to sleep;\n"
"No more, and by a sleep, to say we end\n"
"The heart-ache, and the thousand natural shocks\n"
"That flesh is heir to? tis a consumation\n"
"Devoutly to be wished. To die to sleep,\n"
"To sleepe, perchance to dreame, Aye, there's the rub,\n"
"For in that sleep of death what dreams may come\n"
"When we haue shuffled off this mortal coil\n"
"Must give us pause. There's the respect\n"
"That makes calamity of so long life:\n"
"For who would bear the Ships and Scorns of time,\n"
"The oppressor's wrong, the proud man's Contumely,\n"
"The pangs of dispised love, the Law's delay,\n"
;
/* The known value of the CMAC/AES256 MAC of the above soliloqy */
static const unsigned char expected_output[] = {
0x67, 0x92, 0x32, 0x23, 0x50, 0x3d, 0xc5, 0xba,
0x78, 0xd4, 0x6d, 0x63, 0xf2, 0x2b, 0xe9, 0x56,
};
/*
* A property query used for selecting the MAC implementation.
*/
static const char *propq = NULL;
int main(void)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *library_context = NULL;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
unsigned char *out = NULL;
size_t out_len = 0;
OSSL_PARAM params[4], *p = params;
char cipher_name[] = "AES-256-CBC";
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the CMAC implementation */
mac = EVP_MAC_fetch(library_context, "CMAC", propq);
if (mac == NULL) {
fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
goto end;
}
/* Create a context for the CMAC operation */
mctx = EVP_MAC_CTX_new(mac);
if (mctx == NULL) {
fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
goto end;
}
/* The underlying cipher to be used */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, cipher_name,
sizeof(cipher_name));
*p = OSSL_PARAM_construct_end();
/* Initialise the CMAC operation */
if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
fprintf(stderr, "EVP_MAC_init() failed\n");
goto end;
}
/* Make one or more calls to process the data to be authenticated */
if (!EVP_MAC_update(mctx, data, sizeof(data))) {
fprintf(stderr, "EVP_MAC_update() failed\n");
goto end;
}
/* Make a call to the final with a NULL buffer to get the length of the MAC */
if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
out = OPENSSL_malloc(out_len);
if (out == NULL) {
fprintf(stderr, "malloc failed\n");
goto end;
}
/* Make one call to the final to get the MAC */
if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
printf("Generated MAC:\n");
BIO_dump_indent_fp(stdout, out, out_len, 2);
putchar('\n');
if (out_len != sizeof(expected_output)) {
fprintf(stderr, "Generated MAC has an unexpected length\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated MAC does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
/* OpenSSL free functions will ignore NULL arguments */
OPENSSL_free(out);
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
return ret;
}
| 4,912 | 30.696774 | 82 | c |
openssl | openssl-master/demos/mac/gmac.c | /*
* Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/err.h>
/*
* Taken from NIST's GCM Test Vectors
* http://csrc.nist.gov/groups/STM/cavp/
*/
/*
* Hard coding the key into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char key[] = {
0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2,
0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb
};
/*
* The initialisation vector (IV) is better not being hard coded too.
* Repeating password/IV pairs compromises the integrity of GMAC.
* The IV is not considered secret information and is safe to store with
* an encrypted password.
*/
static unsigned char iv[] = {
0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba,
0x01, 0x36, 0xa7, 0x97, 0xf3
};
static unsigned char data[] = {
0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78,
0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab
};
static const unsigned char expected_output[] = {
0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93,
0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46
};
/*
* A property query used for selecting the GMAC implementation and the
* underlying GCM mode cipher.
*/
static char *propq = NULL;
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
unsigned char out[16];
OSSL_PARAM params[4], *p = params;
OSSL_LIB_CTX *library_context = NULL;
size_t out_len = 0;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the GMAC implementation */
mac = EVP_MAC_fetch(library_context, "GMAC", propq);
if (mac == NULL) {
fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
goto end;
}
/* Create a context for the GMAC operation */
mctx = EVP_MAC_CTX_new(mac);
if (mctx == NULL) {
fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
goto end;
}
/* GMAC requires a GCM mode cipher to be specified */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
"AES-128-GCM", 0);
/*
* If a non-default property query is required when fetching the GCM mode
* cipher, it needs to be specified too.
*/
if (propq != NULL)
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
propq, 0);
/* Set the initialisation vector (IV) */
*p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,
iv, sizeof(iv));
*p = OSSL_PARAM_construct_end();
/* Initialise the GMAC operation */
if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
fprintf(stderr, "EVP_MAC_init() failed\n");
goto end;
}
/* Make one or more calls to process the data to be authenticated */
if (!EVP_MAC_update(mctx, data, sizeof(data))) {
fprintf(stderr, "EVP_MAC_update() failed\n");
goto end;
}
/* Make one call to the final to get the MAC */
if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
printf("Generated MAC:\n");
BIO_dump_indent_fp(stdout, out, out_len, 2);
putchar('\n');
if (out_len != sizeof(expected_output)) {
fprintf(stderr, "Generated MAC has an unexpected length\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated MAC does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
return ret;
}
| 4,315 | 28.561644 | 77 | c |
openssl | openssl-master/demos/mac/hmac-sha512.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example of using EVP_MAC_ methods to calculate
* a HMAC of static buffers
*/
#include <string.h>
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/params.h>
/*
* Hard coding the key into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char key[] = {
0x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03,
0x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c,
0x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75,
0x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61,
0x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4,
0xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7,
0x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a,
0x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14,
};
static const unsigned char data[] =
"To be, or not to be, that is the question,\n"
"Whether tis nobler in the minde to suffer\n"
"The ſlings and arrowes of outragious fortune,\n"
"Or to take Armes again in a sea of troubles,\n"
"And by opposing, end them, to die to sleep;\n"
"No more, and by a sleep, to say we end\n"
"The heart-ache, and the thousand natural shocks\n"
"That flesh is heir to? tis a consumation\n"
"Devoutly to be wished. To die to sleep,\n"
"To sleepe, perchance to dreame, Aye, there's the rub,\n"
"For in that sleep of death what dreams may come\n"
"When we haue shuffled off this mortal coil\n"
"Must give us pause. There's the respect\n"
"That makes calamity of so long life:\n"
"For who would bear the Ships and Scorns of time,\n"
"The oppressor's wrong, the proud man's Contumely,\n"
"The pangs of dispised love, the Law's delay,\n"
;
/* The known value of the HMAC/SHA3-512 MAC of the above soliloqy */
static const unsigned char expected_output[] = {
0x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23,
0x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1,
0x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89,
0x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3,
0x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06,
0x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb,
0x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7,
0x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30,
};
/*
* A property query used for selecting the MAC implementation.
*/
static const char *propq = NULL;
int main(void)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *library_context = NULL;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
EVP_MD_CTX *digest_context = NULL;
unsigned char *out = NULL;
size_t out_len = 0;
OSSL_PARAM params[4], *p = params;
char digest_name[] = "SHA3-512";
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the HMAC implementation */
mac = EVP_MAC_fetch(library_context, "HMAC", propq);
if (mac == NULL) {
fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
goto end;
}
/* Create a context for the HMAC operation */
mctx = EVP_MAC_CTX_new(mac);
if (mctx == NULL) {
fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
goto end;
}
/* The underlying digest to be used */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,
sizeof(digest_name));
*p = OSSL_PARAM_construct_end();
/* Initialise the HMAC operation */
if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
fprintf(stderr, "EVP_MAC_init() failed\n");
goto end;
}
/* Make one or more calls to process the data to be authenticated */
if (!EVP_MAC_update(mctx, data, sizeof(data))) {
fprintf(stderr, "EVP_MAC_update() failed\n");
goto end;
}
/* Make a call to the final with a NULL buffer to get the length of the MAC */
if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
out = OPENSSL_malloc(out_len);
if (out == NULL) {
fprintf(stderr, "malloc failed\n");
goto end;
}
/* Make one call to the final to get the MAC */
if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
printf("Generated MAC:\n");
BIO_dump_indent_fp(stdout, out, out_len, 2);
putchar('\n');
if (out_len != sizeof(expected_output)) {
fprintf(stderr, "Generated MAC has an unexpected length\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated MAC does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
/* OpenSSL free functions will ignore NULL arguments */
OPENSSL_free(out);
EVP_MD_CTX_free(digest_context);
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
return ret;
}
| 5,507 | 31.982036 | 82 | c |
openssl | openssl-master/demos/mac/poly1305.c | /*
* Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/err.h>
/*
* This is a demonstration of how to compute Poly1305-AES using the OpenSSL
* Poly1305 and AES providers and the EVP API.
*
* Please note that:
*
* - Poly1305 must never be used alone and must be used in conjunction with
* another primitive which processes the input nonce to be secure;
*
* - you must never pass a nonce to the Poly1305 primitive directly;
*
* - Poly1305 exhibits catastrophic failure (that is, can be broken) if a
* nonce is ever reused for a given key.
*
* If you are looking for a general purpose MAC, you should consider using a
* different MAC and looking at one of the other examples, unless you have a
* good familiarity with the details and caveats of Poly1305.
*
* This example uses AES, as described in the original paper, "The Poly1305-AES
* message authentication code":
* https://cr.yp.to/mac/poly1305-20050329.pdf
*
* The test vectors below are from that paper.
*/
/*
* Hard coding the key into an application is very bad.
* It is done here solely for educational purposes.
* These are the "r" and "k" inputs to Poly1305-AES.
*/
static const unsigned char test_r[] = {
0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b,
0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00
};
static const unsigned char test_k[] = {
0xec, 0x07, 0x4c, 0x83, 0x55, 0x80, 0x74, 0x17,
0x01, 0x42, 0x5b, 0x62, 0x32, 0x35, 0xad, 0xd6
};
/*
* Hard coding a nonce must not be done under any circumstances and is done here
* purely for demonstration purposes. Please note that Poly1305 exhibits
* catastrophic failure (that is, can be broken) if a nonce is ever reused for a
* given key.
*/
static const unsigned char test_n[] = {
0xfb, 0x44, 0x73, 0x50, 0xc4, 0xe8, 0x68, 0xc5,
0x2a, 0xc3, 0x27, 0x5c, 0xf9, 0xd4, 0x32, 0x7e
};
/* Input message. */
static const unsigned char test_m[] = {
0xf3, 0xf6
};
static const unsigned char expected_output[] = {
0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45,
0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde
};
/*
* A property query used for selecting the POLY1305 implementation.
*/
static char *propq = NULL;
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
EVP_CIPHER *aes = NULL;
EVP_CIPHER_CTX *aesctx = NULL;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
unsigned char composite_key[32];
unsigned char out[16];
OSSL_LIB_CTX *library_context = NULL;
size_t out_len = 0;
int aes_len = 0;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the Poly1305 implementation */
mac = EVP_MAC_fetch(library_context, "POLY1305", propq);
if (mac == NULL) {
fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
goto end;
}
/* Create a context for the Poly1305 operation */
mctx = EVP_MAC_CTX_new(mac);
if (mctx == NULL) {
fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the AES implementation */
aes = EVP_CIPHER_fetch(library_context, "AES-128-ECB", propq);
if (aes == NULL) {
fprintf(stderr, "EVP_CIPHER_fetch() returned NULL\n");
goto end;
}
/* Create a context for AES */
aesctx = EVP_CIPHER_CTX_new();
if (aesctx == NULL) {
fprintf(stderr, "EVP_CIPHER_CTX_new() returned NULL\n");
goto end;
}
/* Initialize the AES cipher with the 128-bit key k */
if (!EVP_EncryptInit_ex(aesctx, aes, NULL, test_k, NULL)) {
fprintf(stderr, "EVP_EncryptInit_ex() failed\n");
goto end;
}
/*
* Disable padding for the AES cipher. We do not strictly need to do this as
* we are encrypting a single block and thus there are no alignment or
* padding concerns, but this ensures that the operation below fails if
* padding would be required for some reason, which in this circumstance
* would indicate an implementation bug.
*/
if (!EVP_CIPHER_CTX_set_padding(aesctx, 0)) {
fprintf(stderr, "EVP_CIPHER_CTX_set_padding() failed\n");
goto end;
}
/*
* Computes the value AES_k(n) which we need for our Poly1305-AES
* computation below.
*/
if (!EVP_EncryptUpdate(aesctx, composite_key + 16, &aes_len,
test_n, sizeof(test_n))) {
fprintf(stderr, "EVP_EncryptUpdate() failed\n");
goto end;
}
/*
* The Poly1305 provider expects the key r to be passed as the first 16
* bytes of the "key" and the processed nonce (that is, AES_k(n)) to be
* passed as the second 16 bytes of the "key". We already put the processed
* nonce in the correct place above, so copy r into place.
*/
memcpy(composite_key, test_r, 16);
/* Initialise the Poly1305 operation */
if (!EVP_MAC_init(mctx, composite_key, sizeof(composite_key), NULL)) {
fprintf(stderr, "EVP_MAC_init() failed\n");
goto end;
}
/* Make one or more calls to process the data to be authenticated */
if (!EVP_MAC_update(mctx, test_m, sizeof(test_m))) {
fprintf(stderr, "EVP_MAC_update() failed\n");
goto end;
}
/* Make one call to the final to get the MAC */
if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
printf("Generated MAC:\n");
BIO_dump_indent_fp(stdout, out, out_len, 2);
putchar('\n');
if (out_len != sizeof(expected_output)) {
fprintf(stderr, "Generated MAC has an unexpected length\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated MAC does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
EVP_CIPHER_CTX_free(aesctx);
EVP_CIPHER_free(aes);
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
return ret;
}
| 6,613 | 30.495238 | 80 | c |
openssl | openssl-master/demos/mac/siphash.c | /*
* Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/err.h>
/*
* Taken from the test vector from the paper "SipHash: a fast short-input PRF".
* https://www.aumasson.jp/siphash/siphash.pdf
*/
/*
* Hard coding the key into an application is very bad.
* It is done here solely for educational purposes.
*/
static unsigned char key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
static unsigned char data[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
};
static const unsigned char expected_output[] = {
0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1
};
/*
* A property query used for selecting the SIPHASH implementation.
*/
static char *propq = NULL;
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
unsigned char out[8];
OSSL_PARAM params[4], *p = params;
OSSL_LIB_CTX *library_context = NULL;
unsigned int digest_len = 8, c_rounds = 2, d_rounds = 4;
size_t out_len = 0;
library_context = OSSL_LIB_CTX_new();
if (library_context == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto end;
}
/* Fetch the SipHash implementation */
mac = EVP_MAC_fetch(library_context, "SIPHASH", propq);
if (mac == NULL) {
fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
goto end;
}
/* Create a context for the SipHash operation */
mctx = EVP_MAC_CTX_new(mac);
if (mctx == NULL) {
fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
goto end;
}
/* SipHash can support either 8 or 16-byte digests. */
*p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_SIZE, &digest_len);
/*
* The number of C-rounds and D-rounds is configurable. Standard SipHash
* uses values of 2 and 4 respectively. The following lines are unnecessary
* as they set the default, but demonstrate how to change these values.
*/
*p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_C_ROUNDS, &c_rounds);
*p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_D_ROUNDS, &d_rounds);
*p = OSSL_PARAM_construct_end();
/* Initialise the SIPHASH operation */
if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
fprintf(stderr, "EVP_MAC_init() failed\n");
goto end;
}
/* Make one or more calls to process the data to be authenticated */
if (!EVP_MAC_update(mctx, data, sizeof(data))) {
fprintf(stderr, "EVP_MAC_update() failed\n");
goto end;
}
/* Make one call to the final to get the MAC */
if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
fprintf(stderr, "EVP_MAC_final() failed\n");
goto end;
}
printf("Generated MAC:\n");
BIO_dump_indent_fp(stdout, out, out_len, 2);
putchar('\n');
if (out_len != sizeof(expected_output)) {
fprintf(stderr, "Generated MAC has an unexpected length\n");
goto end;
}
if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
fprintf(stderr, "Generated MAC does not match expected value\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
return ret;
}
| 3,857 | 28.676923 | 79 | c |
openssl | openssl-master/demos/pkcs12/pkread.c | /*
* Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>
/* Simple PKCS#12 file reader */
static char *find_friendly_name(PKCS12 *p12)
{
STACK_OF(PKCS7) *safes;
int n, m;
char *name = NULL;
PKCS7 *safe;
STACK_OF(PKCS12_SAFEBAG) *bags;
PKCS12_SAFEBAG *bag;
if ((safes = PKCS12_unpack_authsafes(p12)) == NULL)
return NULL;
for (n = 0; n < sk_PKCS7_num(safes) && name == NULL; n++) {
safe = sk_PKCS7_value(safes, n);
if (OBJ_obj2nid(safe->type) != NID_pkcs7_data
|| (bags = PKCS12_unpack_p7data(safe)) == NULL)
continue;
for (m = 0; m < sk_PKCS12_SAFEBAG_num(bags) && name == NULL; m++) {
bag = sk_PKCS12_SAFEBAG_value(bags, m);
name = PKCS12_get_friendlyname(bag);
}
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
}
sk_PKCS7_pop_free(safes, PKCS7_free);
return name;
}
int main(int argc, char **argv)
{
FILE *fp;
EVP_PKEY *pkey = NULL;
X509 *cert = NULL;
STACK_OF(X509) *ca = NULL;
PKCS12 *p12 = NULL;
char *name = NULL;
int i, ret = EXIT_FAILURE;
if (argc != 4) {
fprintf(stderr, "Usage: pkread p12file password opfile\n");
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
p12 = d2i_PKCS12_fp(fp, NULL);
fclose(fp);
if (p12 == NULL) {
fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr);
goto err;
}
if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
fprintf(stderr, "Error parsing PKCS#12 file\n");
ERR_print_errors_fp(stderr);
goto err;
}
name = find_friendly_name(p12);
PKCS12_free(p12);
if ((fp = fopen(argv[3], "w")) == NULL) {
fprintf(stderr, "Error opening file %s\n", argv[3]);
goto err;
}
if (name != NULL)
fprintf(fp, "***Friendly Name***\n%s\n", name);
if (pkey != NULL) {
fprintf(fp, "***Private Key***\n");
PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
}
if (cert != NULL) {
fprintf(fp, "***User Certificate***\n");
PEM_write_X509_AUX(fp, cert);
}
if (ca != NULL && sk_X509_num(ca) > 0) {
fprintf(fp, "***Other Certificates***\n");
for (i = 0; i < sk_X509_num(ca); i++)
PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
}
fclose(fp);
ret = EXIT_SUCCESS;
err:
OPENSSL_free(name);
X509_free(cert);
EVP_PKEY_free(pkey);
OSSL_STACK_OF_X509_free(ca);
return ret;
}
| 3,067 | 26.392857 | 75 | c |
openssl | openssl-master/demos/pkcs12/pkwrite.c | /*
* Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>
/* Simple PKCS#12 file creator */
int main(int argc, char **argv)
{
FILE *fp;
EVP_PKEY *pkey;
X509 *cert;
PKCS12 *p12;
if (argc != 5) {
fprintf(stderr, "Usage: pkwrite infile password name p12file\n");
exit(EXIT_FAILURE);
}
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
cert = PEM_read_X509(fp, NULL, NULL, NULL);
rewind(fp);
pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
p12 = PKCS12_create(argv[2], argv[3], pkey, cert, NULL, 0, 0, 0, 0, 0);
if (!p12) {
fprintf(stderr, "Error creating PKCS#12 structure\n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[4], "wb")) == NULL) {
fprintf(stderr, "Error opening file %s\n", argv[4]);
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
i2d_PKCS12_fp(fp, p12);
PKCS12_free(p12);
fclose(fp);
return EXIT_SUCCESS;
}
| 1,558 | 27.87037 | 75 | c |
openssl | openssl-master/demos/pkey/EVP_PKEY_DSA_keygen.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example showing how to generate an DSA key pair.
*/
#include <openssl/evp.h>
#include "dsa.inc"
/*
* Generate dsa params using default values.
* See the EVP_PKEY_DSA_param_fromdata demo if you need
* to load DSA params from raw values.
* See the EVP_PKEY_DSA_paramgen demo if you need to
* use non default parameters.
*/
EVP_PKEY *dsa_genparams(OSSL_LIB_CTX *libctx, const char *propq)
{
EVP_PKEY *dsaparamkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
/* Use the dsa params in a EVP_PKEY ctx */
ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
return NULL;
}
if (EVP_PKEY_paramgen_init(ctx) <= 0
|| EVP_PKEY_paramgen(ctx, &dsaparamkey) <= 0) {
fprintf(stderr, "DSA paramgen failed\n");
goto cleanup;
}
cleanup:
EVP_PKEY_CTX_free(ctx);
return dsaparamkey;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
const char *propq = NULL;
EVP_PKEY *dsaparamskey = NULL;
EVP_PKEY *dsakey = NULL;
EVP_PKEY_CTX *ctx = NULL;
/* Generate random dsa params */
dsaparamskey = dsa_genparams(libctx, propq);
if (dsaparamskey == NULL)
goto cleanup;
/* Use the dsa params in a EVP_PKEY ctx */
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
goto cleanup;
}
/* Generate a key using the dsa params */
if (EVP_PKEY_keygen_init(ctx) <= 0
|| EVP_PKEY_keygen(ctx, &dsakey) <= 0) {
fprintf(stderr, "DSA keygen failed\n");
goto cleanup;
}
if (!dsa_print_key(dsakey, 1, libctx, propq))
goto cleanup;
ret = EXIT_SUCCESS;
cleanup:
EVP_PKEY_free(dsakey);
EVP_PKEY_free(dsaparamskey);
EVP_PKEY_CTX_free(ctx);
return ret;
}
| 2,286 | 26.22619 | 74 | c |
openssl | openssl-master/demos/pkey/EVP_PKEY_DSA_paramfromdata.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example showing how to load DSA params from raw data
* using EVP_PKEY_fromdata()
*/
#include <openssl/param_build.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include "dsa.inc"
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
const char *propq = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *dsaparamkey = NULL;
OSSL_PARAM_BLD *bld = NULL;
OSSL_PARAM *params = NULL;
BIGNUM *p = NULL, *q = NULL, *g = NULL;
p = BN_bin2bn(dsa_p, sizeof(dsa_p), NULL);
q = BN_bin2bn(dsa_q, sizeof(dsa_q), NULL);
g = BN_bin2bn(dsa_g, sizeof(dsa_g), NULL);
if (p == NULL || q == NULL || g == NULL)
goto cleanup;
/* Use OSSL_PARAM_BLD if you need to handle BIGNUM Parameters */
bld = OSSL_PARAM_BLD_new();
if (bld == NULL)
goto cleanup;
if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)
|| !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)
|| !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
goto cleanup;
params = OSSL_PARAM_BLD_to_param(bld);
if (params == NULL)
goto cleanup;
ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
goto cleanup;
}
if (EVP_PKEY_fromdata_init(ctx) <= 0
|| EVP_PKEY_fromdata(ctx, &dsaparamkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) {
fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
goto cleanup;
}
if (!dsa_print_key(dsaparamkey, 0, libctx, propq))
goto cleanup;
ret = EXIT_SUCCESS;
cleanup:
EVP_PKEY_free(dsaparamkey);
EVP_PKEY_CTX_free(ctx);
OSSL_PARAM_free(params);
OSSL_PARAM_BLD_free(bld);
BN_free(g);
BN_free(q);
BN_free(p);
return ret;
}
| 2,197 | 27.921053 | 92 | c |
openssl | openssl-master/demos/pkey/EVP_PKEY_DSA_paramgen.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example showing how to generate DSA params using
* FIPS 186-4 DSA FFC parameter generation.
*/
#include <openssl/evp.h>
#include "dsa.inc"
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
const char *propq = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *dsaparamkey = NULL;
OSSL_PARAM params[7];
unsigned int pbits = 2048;
unsigned int qbits = 256;
int gindex = 42;
ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
if (ctx == NULL)
goto cleanup;
/*
* Demonstrate how to set optional DSA fields as params.
* See doc/man7/EVP_PKEY-FFC.pod and doc/man7/EVP_PKEY-DSA.pod
* for more information.
*/
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
"fips186_4", 0);
params[1] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
params[2] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_QBITS, &qbits);
params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
"SHA384", 0);
params[5] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
"provider=default", 0);
params[6] = OSSL_PARAM_construct_end();
/* Generate a dsa param key using optional params */
if (EVP_PKEY_paramgen_init(ctx) <= 0
|| EVP_PKEY_CTX_set_params(ctx, params) <= 0
|| EVP_PKEY_paramgen(ctx, &dsaparamkey) <= 0) {
fprintf(stderr, "DSA paramgen failed\n");
goto cleanup;
}
if (!dsa_print_key(dsaparamkey, 0, libctx, propq))
goto cleanup;
ret = EXIT_SUCCESS;
cleanup:
EVP_PKEY_free(dsaparamkey);
EVP_PKEY_CTX_free(ctx);
return ret;
}
| 2,256 | 32.686567 | 82 | c |
openssl | openssl-master/demos/pkey/EVP_PKEY_DSA_paramvalidate.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example showing how to validate DSA parameters.
*
* Proper FIPS 186-4 DSA (FFC) parameter validation requires that all
* the parameters used during parameter generation are supplied
* when doing the validation. Unfortunately saving DSA parameters as
* a PEM or DER file does not write out all required fields. Because
* of this the default provider normally only does a partial
* validation. The FIPS provider will however try to do a full
* validation. To force the default provider to use full
* validation the 'seed' that is output during generation must be
* added to the key. See doc/man7/EVP_PKEY-FFC for more information.
*/
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include <openssl/pem.h>
#include "dsa.inc"
/* The following values were output from the EVP_PKEY_DSA_paramgen demo */
static const char dsapem[] =
"-----BEGIN DSA PARAMETERS-----\n"
"MIICLAKCAQEA1pobSR1FJ3+Tvi0J6Tk1PSV2owZey1Nuo847hGw/59VCS6RPQEqr\n"
"vp5fhbvBjupBeVGA/AMH6rI4i4h6jlhurrqH1CqUHVcDhJzxV668bMLiP3mIxg5o\n"
"9Yq8x6BnSOtH5Je0tpeE0/fEvvLjCwBUbwnwWxzjANcvDUEt9XYeRrtB2v52fr56\n"
"hVYz3wMMNog4CEDOLTvx7/84eVPuUeWDRQFH1EaHMdulP34KBcatEEpEZapkepng\n"
"nohm9sFSPQhq2utpkH7pNXdG0EILBtRDCvUpF5720a48LYofdggh2VEZfgElAGFk\n"
"dW/CkvyBDmGIzil5aTz4MMsdudaVYgzt6wIhAPsSGC42Qa+X0AFGvonb5nmfUVm/\n"
"8aC+tHk7Nb2AYLHXAoIBADx5C0H1+QHsmGKvuOaY+WKUt7aWUrEivD1zBMJAQ6bL\n"
"Wv9lbCq1CFHvVzojeOVpn872NqDEpkx4HTpvqhxWL5CkbN/HaGItsQzkD59AQg3v\n"
"4YsLlkesq9Jq6x/aWetJXWO36fszFv1gpD3NY3wliBvMYHx62jfc5suh9D3ZZvu7\n"
"PLGH4X4kcfzK/R2b0oVbEBjVTe5GMRYZRqnvfSW2f2fA7BzI1OL83UxDDe58cL2M\n"
"GcAoUYXOBAfZ37qLMm2juf+o5gCrT4CXfRPu6kbapt7V/YIc1nsNgeAOKKoFBHBQ\n"
"gc5u5G6G/j79FVoSDq9DYwTJcHPsU+eHj1uWHso1AjQ=\n"
"-----END DSA PARAMETERS-----\n";
static const char hexseed[] =
"cba30ccd905aa7675a0b81769704bf3c"
"ccf2ca1892b2eaf6b9e2b38d9bf6affc"
"42ada55986d8a1772b442770954d0b65";
const int gindex = 42;
const int pcounter = 363;
static const char digest[] = "SHA384";
/*
* Create a new dsa param key that is the combination of an existing param key
* plus extra parameters.
*/
EVP_PKEY_CTX *create_merged_key(EVP_PKEY *dsaparams, const OSSL_PARAM *newparams,
OSSL_LIB_CTX *libctx, const char *propq)
{
EVP_PKEY_CTX *out = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *pkey = NULL;
OSSL_PARAM *mergedparams = NULL;
OSSL_PARAM *loadedparams = NULL;
/* Specify EVP_PKEY_KEY_PUBLIC here if you have a public key */
if (EVP_PKEY_todata(dsaparams, EVP_PKEY_KEY_PARAMETERS, &loadedparams) <= 0) {
fprintf(stderr, "EVP_PKEY_todata() failed\n");
goto cleanup;
}
mergedparams = OSSL_PARAM_merge(loadedparams, newparams);
if (mergedparams == NULL) {
fprintf(stderr, "OSSL_PARAM_merge() failed\n");
goto cleanup;
}
ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
goto cleanup;
}
if (EVP_PKEY_fromdata_init(ctx) <= 0
|| EVP_PKEY_fromdata(ctx, &pkey,
EVP_PKEY_KEY_PARAMETERS, mergedparams) <= 0) {
fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
goto cleanup;
}
out = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
if (out == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
goto cleanup;
}
cleanup:
EVP_PKEY_free(pkey);
OSSL_PARAM_free(loadedparams);
OSSL_PARAM_free(mergedparams);
EVP_PKEY_CTX_free(ctx);
return out;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
const char *propq = NULL;
EVP_PKEY *dsaparamskey = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY_CTX *ctx1 = NULL;
EVP_PKEY_CTX *ctx2 = NULL;
BIO *in = NULL;
OSSL_PARAM params[6];
unsigned char seed[64];
size_t seedlen;
if (!OPENSSL_hexstr2buf_ex(seed, sizeof(seed), &seedlen, hexseed, '\0'))
goto cleanup;
/*
* This example loads the PEM data from a memory buffer
* Use BIO_new_fp() to load a PEM file instead
*/
in = BIO_new_mem_buf(dsapem, strlen(dsapem));
if (in == NULL) {
fprintf(stderr, "BIO_new_mem_buf() failed\n");
goto cleanup;
}
/* Load DSA params from pem data */
dsaparamskey = PEM_read_bio_Parameters_ex(in, NULL, libctx, propq);
if (dsaparamskey == NULL) {
fprintf(stderr, "Failed to load dsa params\n");
goto cleanup;
}
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
goto cleanup;
}
/*
* When using the default provider this only does a partial check to
* make sure that the values of p, q and g are ok.
* This will fail however if the FIPS provider is used since it does
* a proper FIPS 186-4 key validation which requires extra parameters
*/
if (EVP_PKEY_param_check(ctx) <= 0) {
fprintf(stderr, "Simple EVP_PKEY_param_check() failed \n");
goto cleanup;
}
/*
* Setup parameters that we want to add.
* For illustration purposes it deliberately omits a required parameter.
*/
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
"fips186_4", 0);
/* Force it to do a proper validation by setting the seed */
params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
(void *)seed, seedlen);
params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, (int *)&gindex);
params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, (int *)&pcounter);
params[4] = OSSL_PARAM_construct_end();
/* generate a new key that is the combination of the existing key and the new params */
ctx1 = create_merged_key(dsaparamskey, params, libctx, propq);
if (ctx1 == NULL)
goto cleanup;
/* This will fail since not all the parameters used for key generation are added */
if (EVP_PKEY_param_check(ctx1) > 0) {
fprintf(stderr, "EVP_PKEY_param_check() should fail\n");
goto cleanup;
}
/*
* Add the missing parameters onto the end of the existing list of params
* If the default was used for the generation then this parameter is not
* needed
*/
params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
(char *)digest, 0);
params[5] = OSSL_PARAM_construct_end();
ctx2 = create_merged_key(dsaparamskey, params, libctx, propq);
if (ctx2 == NULL)
goto cleanup;
if (EVP_PKEY_param_check(ctx2) <= 0) {
fprintf(stderr, "EVP_PKEY_param_check() failed\n");
goto cleanup;
}
if (!dsa_print_key(EVP_PKEY_CTX_get0_pkey(ctx2), 0, libctx, propq))
goto cleanup;
ret = EXIT_SUCCESS;
cleanup:
EVP_PKEY_free(dsaparamskey);
EVP_PKEY_CTX_free(ctx2);
EVP_PKEY_CTX_free(ctx1);
EVP_PKEY_CTX_free(ctx);
BIO_free(in);
return ret;
}
| 7,570 | 36.295567 | 91 | c |
openssl | openssl-master/demos/pkey/EVP_PKEY_EC_keygen.c | /*-
* Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example showing how to generate an EC key and extract values from the
* generated key.
*/
#include <string.h>
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>
static int get_key_values(EVP_PKEY *pkey);
/*
* The following code shows how to generate an EC key from a curve name
* with additional parameters. If only the curve name is required then the
* simple helper can be used instead i.e. Either
* pkey = EVP_EC_gen(curvename); OR
* pkey = EVP_PKEY_Q_keygen(libctx, propq, "EC", curvename);
*/
static EVP_PKEY *do_ec_keygen(void)
{
/*
* The libctx and propq can be set if required, they are included here
* to show how they are passed to EVP_PKEY_CTX_new_from_name().
*/
OSSL_LIB_CTX *libctx = NULL;
const char *propq = NULL;
EVP_PKEY *key = NULL;
OSSL_PARAM params[3];
EVP_PKEY_CTX *genctx = NULL;
const char *curvename = "P-256";
int use_cofactordh = 1;
genctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);
if (genctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
goto cleanup;
}
if (EVP_PKEY_keygen_init(genctx) <= 0) {
fprintf(stderr, "EVP_PKEY_keygen_init() failed\n");
goto cleanup;
}
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
(char *)curvename, 0);
/*
* This is an optional parameter.
* For many curves where the cofactor is 1, setting this has no effect.
*/
params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
&use_cofactordh);
params[2] = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_set_params(genctx, params)) {
fprintf(stderr, "EVP_PKEY_CTX_set_params() failed\n");
goto cleanup;
}
fprintf(stdout, "Generating EC key\n\n");
if (EVP_PKEY_generate(genctx, &key) <= 0) {
fprintf(stderr, "EVP_PKEY_generate() failed\n");
goto cleanup;
}
cleanup:
EVP_PKEY_CTX_free(genctx);
return key;
}
/*
* The following code shows how retrieve key data from the generated
* EC key. See doc/man7/EVP_PKEY-EC.pod for more information.
*
* EVP_PKEY_print_private() could also be used to display the values.
*/
static int get_key_values(EVP_PKEY *pkey)
{
int ret = 0;
char out_curvename[80];
unsigned char out_pubkey[80];
unsigned char out_privkey[80];
BIGNUM *out_priv = NULL;
size_t out_pubkey_len, out_privkey_len = 0;
if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
out_curvename, sizeof(out_curvename),
NULL)) {
fprintf(stderr, "Failed to get curve name\n");
goto cleanup;
}
if (!EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY,
out_pubkey, sizeof(out_pubkey),
&out_pubkey_len)) {
fprintf(stderr, "Failed to get public key\n");
goto cleanup;
}
if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &out_priv)) {
fprintf(stderr, "Failed to get private key\n");
goto cleanup;
}
out_privkey_len = BN_bn2bin(out_priv, out_privkey);
if (out_privkey_len <= 0 || out_privkey_len > sizeof(out_privkey)) {
fprintf(stderr, "BN_bn2bin failed\n");
goto cleanup;
}
fprintf(stdout, "Curve name: %s\n", out_curvename);
fprintf(stdout, "Public key:\n");
BIO_dump_indent_fp(stdout, out_pubkey, out_pubkey_len, 2);
fprintf(stdout, "Private Key:\n");
BIO_dump_indent_fp(stdout, out_privkey, out_privkey_len, 2);
ret = 1;
cleanup:
/* Zeroize the private key data when we free it */
BN_clear_free(out_priv);
return ret;
}
int main(void)
{
int ret = EXIT_FAILURE;
EVP_PKEY *pkey;
pkey = do_ec_keygen();
if (pkey == NULL)
goto cleanup;
if (!get_key_values(pkey))
goto cleanup;
/*
* At this point we can write out the generated key using
* i2d_PrivateKey() and i2d_PublicKey() if required.
*/
ret = EXIT_SUCCESS;
cleanup:
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
EVP_PKEY_free(pkey);
return ret;
}
| 4,712 | 29.211538 | 77 | c |
openssl | openssl-master/demos/pkey/EVP_PKEY_RSA_keygen.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Example showing how to generate an RSA key pair.
*
* When generating an RSA key, you must specify the number of bits in the key. A
* reasonable value would be 4096. Avoid using values below 2048. These values
* are reasonable as of 2022.
*/
#include <string.h>
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/core_names.h>
#include <openssl/pem.h>
/* A property query used for selecting algorithm implementations. */
static const char *propq = NULL;
/*
* Generates an RSA public-private key pair and returns it.
* The number of bits is specified by the bits argument.
*
* This uses the long way of generating an RSA key.
*/
static EVP_PKEY *generate_rsa_key_long(OSSL_LIB_CTX *libctx, unsigned int bits)
{
EVP_PKEY_CTX *genctx = NULL;
EVP_PKEY *pkey = NULL;
unsigned int primes = 2;
/* Create context using RSA algorithm. "RSA-PSS" could also be used here. */
genctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", propq);
if (genctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
goto cleanup;
}
/* Initialize context for key generation purposes. */
if (EVP_PKEY_keygen_init(genctx) <= 0) {
fprintf(stderr, "EVP_PKEY_keygen_init() failed\n");
goto cleanup;
}
/*
* Here we set the number of bits to use in the RSA key.
* See comment at top of file for information on appropriate values.
*/
if (EVP_PKEY_CTX_set_rsa_keygen_bits(genctx, bits) <= 0) {
fprintf(stderr, "EVP_PKEY_CTX_set_rsa_keygen_bits() failed\n");
goto cleanup;
}
/*
* It is possible to create an RSA key using more than two primes.
* Do not do this unless you know why you need this.
* You ordinarily do not need to specify this, as the default is two.
*
* Both of these parameters can also be set via EVP_PKEY_CTX_set_params, but
* these functions provide a more concise way to do so.
*/
if (EVP_PKEY_CTX_set_rsa_keygen_primes(genctx, primes) <= 0) {
fprintf(stderr, "EVP_PKEY_CTX_set_rsa_keygen_primes() failed\n");
goto cleanup;
}
/*
* Generating an RSA key with a number of bits large enough to be secure for
* modern applications can take a fairly substantial amount of time (e.g.
* one second). If you require fast key generation, consider using an EC key
* instead.
*
* If you require progress information during the key generation process,
* you can set a progress callback using EVP_PKEY_set_cb; see the example in
* EVP_PKEY_generate(3).
*/
fprintf(stderr, "Generating RSA key, this may take some time...\n");
if (EVP_PKEY_generate(genctx, &pkey) <= 0) {
fprintf(stderr, "EVP_PKEY_generate() failed\n");
goto cleanup;
}
/* pkey is now set to an object representing the generated key pair. */
cleanup:
EVP_PKEY_CTX_free(genctx);
return pkey;
}
/*
* Generates an RSA public-private key pair and returns it.
* The number of bits is specified by the bits argument.
*
* This uses a more concise way of generating an RSA key, which is suitable for
* simple cases. It is used if -s is passed on the command line, otherwise the
* long method above is used. The ability to choose between these two methods is
* shown here only for demonstration; the results are equivalent.
*/
static EVP_PKEY *generate_rsa_key_short(OSSL_LIB_CTX *libctx, unsigned int bits)
{
EVP_PKEY *pkey = NULL;
fprintf(stderr, "Generating RSA key, this may take some time...\n");
pkey = EVP_PKEY_Q_keygen(libctx, propq, "RSA", (size_t)bits);
if (pkey == NULL)
fprintf(stderr, "EVP_PKEY_Q_keygen() failed\n");
return pkey;
}
/*
* Prints information on an EVP_PKEY object representing an RSA key pair.
*/
static int dump_key(const EVP_PKEY *pkey)
{
int ret = 0;
int bits = 0;
BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
/*
* Retrieve value of n. This value is not secret and forms part of the
* public key.
*
* Calling EVP_PKEY_get_bn_param with a NULL BIGNUM pointer causes
* a new BIGNUM to be allocated, so these must be freed subsequently.
*/
if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_N, &n) == 0) {
fprintf(stderr, "Failed to retrieve n\n");
goto cleanup;
}
/*
* Retrieve value of e. This value is not secret and forms part of the
* public key. It is typically 65537 and need not be changed.
*/
if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, &e) == 0) {
fprintf(stderr, "Failed to retrieve e\n");
goto cleanup;
}
/*
* Retrieve value of d. This value is secret and forms part of the private
* key. It must not be published.
*/
if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D, &d) == 0) {
fprintf(stderr, "Failed to retrieve d\n");
goto cleanup;
}
/*
* Retrieve value of the first prime factor, commonly known as p. This value
* is secret and forms part of the private key. It must not be published.
*/
if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_FACTOR1, &p) == 0) {
fprintf(stderr, "Failed to retrieve p\n");
goto cleanup;
}
/*
* Retrieve value of the second prime factor, commonly known as q. This value
* is secret and forms part of the private key. It must not be published.
*
* If you are creating an RSA key with more than two primes for special
* applications, you can retrieve these primes with
* OSSL_PKEY_PARAM_RSA_FACTOR3, etc.
*/
if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_FACTOR2, &q) == 0) {
fprintf(stderr, "Failed to retrieve q\n");
goto cleanup;
}
/*
* We can also retrieve the key size in bits for informational purposes.
*/
if (EVP_PKEY_get_int_param(pkey, OSSL_PKEY_PARAM_BITS, &bits) == 0) {
fprintf(stderr, "Failed to retrieve bits\n");
goto cleanup;
}
/* Output hexadecimal representations of the BIGNUM objects. */
fprintf(stdout, "\nNumber of bits: %d\n\n", bits);
fprintf(stderr, "Public values:\n");
fprintf(stdout, " n = 0x");
BN_print_fp(stdout, n);
fprintf(stdout, "\n");
fprintf(stdout, " e = 0x");
BN_print_fp(stdout, e);
fprintf(stdout, "\n\n");
fprintf(stdout, "Private values:\n");
fprintf(stdout, " d = 0x");
BN_print_fp(stdout, d);
fprintf(stdout, "\n");
fprintf(stdout, " p = 0x");
BN_print_fp(stdout, p);
fprintf(stdout, "\n");
fprintf(stdout, " q = 0x");
BN_print_fp(stdout, q);
fprintf(stdout, "\n\n");
/* Output a PEM encoding of the public key. */
if (PEM_write_PUBKEY(stdout, pkey) == 0) {
fprintf(stderr, "Failed to output PEM-encoded public key\n");
goto cleanup;
}
/*
* Output a PEM encoding of the private key. Please note that this output is
* not encrypted. You may wish to use the arguments to specify encryption of
* the key if you are storing it on disk. See PEM_write_PrivateKey(3).
*/
if (PEM_write_PrivateKey(stdout, pkey, NULL, NULL, 0, NULL, NULL) == 0) {
fprintf(stderr, "Failed to output PEM-encoded private key\n");
goto cleanup;
}
ret = 1;
cleanup:
BN_free(n); /* not secret */
BN_free(e); /* not secret */
BN_clear_free(d); /* secret - scrub before freeing */
BN_clear_free(p); /* secret - scrub before freeing */
BN_clear_free(q); /* secret - scrub before freeing */
return ret;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
EVP_PKEY *pkey = NULL;
unsigned int bits = 4096;
int bits_i, use_short = 0;
/* usage: [-s] [<bits>] */
if (argc > 1 && strcmp(argv[1], "-s") == 0) {
--argc;
++argv;
use_short = 1;
}
if (argc > 1) {
bits_i = atoi(argv[1]);
if (bits < 512) {
fprintf(stderr, "Invalid RSA key size\n");
return EXIT_FAILURE;
}
bits = (unsigned int)bits_i;
}
/* Avoid using key sizes less than 2048 bits; see comment at top of file. */
if (bits < 2048)
fprintf(stderr, "Warning: very weak key size\n\n");
/* Generate RSA key. */
if (use_short)
pkey = generate_rsa_key_short(libctx, bits);
else
pkey = generate_rsa_key_long(libctx, bits);
if (pkey == NULL)
goto cleanup;
/* Dump the integers comprising the key. */
if (dump_key(pkey) == 0) {
fprintf(stderr, "Failed to dump key\n");
goto cleanup;
}
ret = EXIT_SUCCESS;
cleanup:
EVP_PKEY_free(pkey);
OSSL_LIB_CTX_free(libctx);
return ret;
}
| 9,178 | 30.651724 | 81 | c |
openssl | openssl-master/demos/signature/EVP_DSA_Signature_demo.c | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* An example that uses the EVP_PKEY*, EVP_DigestSign* and EVP_DigestVerify*
* methods to calculate public/private DSA keypair and to sign and verify
* two static buffers.
*/
#include <string.h>
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/decoder.h>
#include <openssl/dsa.h>
/*
* This demonstration will calculate and verify a signature of data using
* the soliloquy from Hamlet scene 1 act 3
*/
static const char *hamlet_1 =
"To be, or not to be, that is the question,\n"
"Whether tis nobler in the minde to suffer\n"
"The slings and arrowes of outragious fortune,\n"
"Or to take Armes again in a sea of troubles,\n"
;
static const char *hamlet_2 =
"And by opposing, end them, to die to sleep;\n"
"No more, and by a sleep, to say we end\n"
"The heart-ache, and the thousand natural shocks\n"
"That flesh is heir to? tis a consumation\n"
;
static const char ALG[] = "DSA";
static const char DIGEST[] = "SHA256";
static const int NUMBITS = 2048;
static const char * const PROPQUERY = NULL;
static int generate_dsa_params(OSSL_LIB_CTX *libctx,
EVP_PKEY **p_params)
{
int ret = 0;
EVP_PKEY_CTX *pkey_ctx = NULL;
EVP_PKEY *params = NULL;
pkey_ctx = EVP_PKEY_CTX_new_from_name(libctx, ALG, PROPQUERY);
if (pkey_ctx == NULL)
goto end;
if (EVP_PKEY_paramgen_init(pkey_ctx) <= 0)
goto end;
if (EVP_PKEY_CTX_set_dsa_paramgen_bits(pkey_ctx, NUMBITS) <= 0)
goto end;
if (EVP_PKEY_paramgen(pkey_ctx, ¶ms) <= 0)
goto end;
if (params == NULL)
goto end;
ret = 1;
end:
if(ret != 1) {
EVP_PKEY_free(params);
params = NULL;
}
EVP_PKEY_CTX_free(pkey_ctx);
*p_params = params;
fprintf(stdout, "Params:\n");
EVP_PKEY_print_params_fp(stdout, params, 4, NULL);
fprintf(stdout, "\n");
return ret;
}
static int generate_dsa_key(OSSL_LIB_CTX *libctx,
EVP_PKEY *params,
EVP_PKEY **p_pkey)
{
int ret = 0;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *pkey = NULL;
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, params,
NULL);
if (ctx == NULL)
goto end;
if (EVP_PKEY_keygen_init(ctx) <= 0)
goto end;
if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
goto end;
if (pkey == NULL)
goto end;
ret = 1;
end:
if(ret != 1) {
EVP_PKEY_free(pkey);
pkey = NULL;
}
EVP_PKEY_CTX_free(ctx);
*p_pkey = pkey;
fprintf(stdout, "Generating public/private key pair:\n");
EVP_PKEY_print_public_fp(stdout, pkey, 4, NULL);
fprintf(stdout, "\n");
EVP_PKEY_print_private_fp(stdout, pkey, 4, NULL);
fprintf(stdout, "\n");
EVP_PKEY_print_params_fp(stdout, pkey, 4, NULL);
fprintf(stdout, "\n");
return ret;
}
static int extract_public_key(const EVP_PKEY *pkey,
OSSL_PARAM **p_public_key)
{
int ret = 0;
OSSL_PARAM *public_key = NULL;
if (EVP_PKEY_todata(pkey, EVP_PKEY_PUBLIC_KEY, &public_key) != 1)
goto end;
ret = 1;
end:
if (ret != 1) {
OSSL_PARAM_free(public_key);
public_key = NULL;
}
*p_public_key = public_key;
return ret;
}
static int extract_keypair(const EVP_PKEY *pkey,
OSSL_PARAM **p_keypair)
{
int ret = 0;
OSSL_PARAM *keypair = NULL;
if (EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &keypair) != 1)
goto end;
ret = 1;
end:
if (ret != 1) {
OSSL_PARAM_free(keypair);
keypair = NULL;
}
*p_keypair = keypair;
return ret;
}
static int demo_sign(OSSL_LIB_CTX *libctx,
size_t *p_sig_len, unsigned char **p_sig_value,
OSSL_PARAM keypair[])
{
int ret = 0;
size_t sig_len = 0;
unsigned char *sig_value = NULL;
EVP_MD_CTX *ctx = NULL;
EVP_PKEY_CTX *pkey_ctx = NULL;
EVP_PKEY *pkey = NULL;
pkey_ctx = EVP_PKEY_CTX_new_from_name(libctx, ALG, PROPQUERY);
if (pkey_ctx == NULL)
goto end;
if (EVP_PKEY_fromdata_init(pkey_ctx) != 1)
goto end;
if (EVP_PKEY_fromdata(pkey_ctx, &pkey, EVP_PKEY_KEYPAIR, keypair) != 1)
goto end;
ctx = EVP_MD_CTX_create();
if (ctx == NULL)
goto end;
if (EVP_DigestSignInit_ex(ctx, NULL, DIGEST, libctx, NULL, pkey, NULL) != 1)
goto end;
if (EVP_DigestSignUpdate(ctx, hamlet_1, sizeof(hamlet_1)) != 1)
goto end;
if (EVP_DigestSignUpdate(ctx, hamlet_2, sizeof(hamlet_2)) != 1)
goto end;
/* Calculate the signature size */
if (EVP_DigestSignFinal(ctx, NULL, &sig_len) != 1)
goto end;
if (sig_len == 0)
goto end;
sig_value = OPENSSL_malloc(sig_len);
if (sig_value == NULL)
goto end;
/* Calculate the signature */
if (EVP_DigestSignFinal(ctx, sig_value, &sig_len) != 1)
goto end;
ret = 1;
end:
EVP_MD_CTX_free(ctx);
if (ret != 1) {
OPENSSL_free(sig_value);
sig_len = 0;
sig_value = NULL;
}
*p_sig_len = sig_len;
*p_sig_value = sig_value;
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pkey_ctx);
fprintf(stdout, "Generating signature:\n");
BIO_dump_indent_fp(stdout, sig_value, sig_len, 2);
fprintf(stdout, "\n");
return ret;
}
static int demo_verify(OSSL_LIB_CTX *libctx,
size_t sig_len, unsigned char *sig_value,
OSSL_PARAM public_key[])
{
int ret = 0;
EVP_MD_CTX *ctx = NULL;
EVP_PKEY_CTX *pkey_ctx = NULL;
EVP_PKEY *pkey = NULL;
pkey_ctx = EVP_PKEY_CTX_new_from_name(libctx, ALG, PROPQUERY);
if (pkey_ctx == NULL)
goto end;
if (EVP_PKEY_fromdata_init(pkey_ctx) != 1)
goto end;
if (EVP_PKEY_fromdata(pkey_ctx, &pkey, EVP_PKEY_PUBLIC_KEY, public_key) != 1)
goto end;
ctx = EVP_MD_CTX_create();
if(ctx == NULL)
goto end;
if (EVP_DigestVerifyInit_ex(ctx, NULL, DIGEST, libctx, NULL, pkey, NULL) != 1)
goto end;
if (EVP_DigestVerifyUpdate(ctx, hamlet_1, sizeof(hamlet_1)) != 1)
goto end;
if (EVP_DigestVerifyUpdate(ctx, hamlet_2, sizeof(hamlet_2)) != 1)
goto end;
if (EVP_DigestVerifyFinal(ctx, sig_value, sig_len) != 1)
goto end;
ret = 1;
end:
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pkey_ctx);
EVP_MD_CTX_free(ctx);
return ret;
}
int main(void)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
EVP_PKEY *params = NULL;
EVP_PKEY *pkey = NULL;
OSSL_PARAM *public_key = NULL;
OSSL_PARAM *keypair = NULL;
size_t sig_len = 0;
unsigned char *sig_value = NULL;
libctx = OSSL_LIB_CTX_new();
if (libctx == NULL)
goto end;
if (generate_dsa_params(libctx, ¶ms) != 1)
goto end;
if (generate_dsa_key(libctx, params, &pkey) != 1)
goto end;
if (extract_public_key(pkey, &public_key) != 1)
goto end;
if (extract_keypair(pkey, &keypair) != 1)
goto end;
/* The signer signs with his private key, and distributes his public key */
if (demo_sign(libctx, &sig_len, &sig_value, keypair) != 1)
goto end;
/* A verifier uses the signers public key to verify the signature */
if (demo_verify(libctx, sig_len, sig_value, public_key) != 1)
goto end;
ret = EXIT_SUCCESS;
end:
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
OPENSSL_free(sig_value);
EVP_PKEY_free(params);
EVP_PKEY_free(pkey);
OSSL_PARAM_free(public_key);
OSSL_PARAM_free(keypair);
OSSL_LIB_CTX_free(libctx);
return ret;
}
| 8,077 | 24.402516 | 82 | c |
openssl | openssl-master/demos/signature/EVP_EC_Signature_demo.c | /*-
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* An example that uses the EVP_MD*, EVP_DigestSign* and EVP_DigestVerify*
* methods to calculate and verify a signature of two static buffers.
*/
#include <string.h>
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/decoder.h>
#include "EVP_EC_Signature_demo.h"
/*
* This demonstration will calculate and verify a signature of data using
* the soliloquy from Hamlet scene 1 act 3
*/
static const char *hamlet_1 =
"To be, or not to be, that is the question,\n"
"Whether tis nobler in the minde to suffer\n"
"The slings and arrowes of outragious fortune,\n"
"Or to take Armes again in a sea of troubles,\n"
;
static const char *hamlet_2 =
"And by opposing, end them, to die to sleep;\n"
"No more, and by a sleep, to say we end\n"
"The heart-ache, and the thousand natural shocks\n"
"That flesh is heir to? tis a consumation\n"
;
/*
* For demo_sign, load EC private key priv_key from priv_key_der[].
* For demo_verify, load EC public key pub_key from pub_key_der[].
*/
static EVP_PKEY *get_key(OSSL_LIB_CTX *libctx, const char *propq, int public)
{
OSSL_DECODER_CTX *dctx = NULL;
EVP_PKEY *pkey = NULL;
int selection;
const unsigned char *data;
size_t data_len;
if (public) {
selection = EVP_PKEY_PUBLIC_KEY;
data = pub_key_der;
data_len = sizeof(pub_key_der);
} else {
selection = EVP_PKEY_KEYPAIR;
data = priv_key_der;
data_len = sizeof(priv_key_der);
}
dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, "EC",
selection, libctx, propq);
(void)OSSL_DECODER_from_data(dctx, &data, &data_len);
OSSL_DECODER_CTX_free(dctx);
if (pkey == NULL)
fprintf(stderr, "Failed to load %s key.\n", public ? "public" : "private");
return pkey;
}
static int demo_sign(OSSL_LIB_CTX *libctx, const char *sig_name,
size_t *sig_out_len, unsigned char **sig_out_value)
{
int ret = 0, public = 0;
size_t sig_len;
unsigned char *sig_value = NULL;
const char *propq = NULL;
EVP_MD_CTX *sign_context = NULL;
EVP_PKEY *priv_key = NULL;
/* Get private key */
priv_key = get_key(libctx, propq, public);
if (priv_key == NULL) {
fprintf(stderr, "Get private key failed.\n");
goto cleanup;
}
/*
* Make a message signature context to hold temporary state
* during signature creation
*/
sign_context = EVP_MD_CTX_new();
if (sign_context == NULL) {
fprintf(stderr, "EVP_MD_CTX_new failed.\n");
goto cleanup;
}
/*
* Initialize the sign context to use the fetched
* sign provider.
*/
if (!EVP_DigestSignInit_ex(sign_context, NULL, sig_name,
libctx, NULL, priv_key, NULL)) {
fprintf(stderr, "EVP_DigestSignInit_ex failed.\n");
goto cleanup;
}
/*
* EVP_DigestSignUpdate() can be called several times on the same context
* to include additional data.
*/
if (!EVP_DigestSignUpdate(sign_context, hamlet_1, strlen(hamlet_1))) {
fprintf(stderr, "EVP_DigestSignUpdate(hamlet_1) failed.\n");
goto cleanup;
}
if (!EVP_DigestSignUpdate(sign_context, hamlet_2, strlen(hamlet_2))) {
fprintf(stderr, "EVP_DigestSignUpdate(hamlet_2) failed.\n");
goto cleanup;
}
/* Call EVP_DigestSignFinal to get signature length sig_len */
if (!EVP_DigestSignFinal(sign_context, NULL, &sig_len)) {
fprintf(stderr, "EVP_DigestSignFinal failed.\n");
goto cleanup;
}
if (sig_len <= 0) {
fprintf(stderr, "EVP_DigestSignFinal returned invalid signature length.\n");
goto cleanup;
}
sig_value = OPENSSL_malloc(sig_len);
if (sig_value == NULL) {
fprintf(stderr, "No memory.\n");
goto cleanup;
}
if (!EVP_DigestSignFinal(sign_context, sig_value, &sig_len)) {
fprintf(stderr, "EVP_DigestSignFinal failed.\n");
goto cleanup;
}
*sig_out_len = sig_len;
*sig_out_value = sig_value;
fprintf(stdout, "Generating signature:\n");
BIO_dump_indent_fp(stdout, sig_value, sig_len, 2);
fprintf(stdout, "\n");
ret = 1;
cleanup:
/* OpenSSL free functions will ignore NULL arguments */
if (!ret)
OPENSSL_free(sig_value);
EVP_PKEY_free(priv_key);
EVP_MD_CTX_free(sign_context);
return ret;
}
static int demo_verify(OSSL_LIB_CTX *libctx, const char *sig_name,
size_t sig_len, unsigned char *sig_value)
{
int ret = 0, public = 1;
const char *propq = NULL;
EVP_MD_CTX *verify_context = NULL;
EVP_PKEY *pub_key = NULL;
/*
* Make a verify signature context to hold temporary state
* during signature verification
*/
verify_context = EVP_MD_CTX_new();
if (verify_context == NULL) {
fprintf(stderr, "EVP_MD_CTX_new failed.\n");
goto cleanup;
}
/* Get public key */
pub_key = get_key(libctx, propq, public);
if (pub_key == NULL) {
fprintf(stderr, "Get public key failed.\n");
goto cleanup;
}
/* Verify */
if (!EVP_DigestVerifyInit_ex(verify_context, NULL, sig_name,
libctx, NULL, pub_key, NULL)) {
fprintf(stderr, "EVP_DigestVerifyInit failed.\n");
goto cleanup;
}
/*
* EVP_DigestVerifyUpdate() can be called several times on the same context
* to include additional data.
*/
if (!EVP_DigestVerifyUpdate(verify_context, hamlet_1, strlen(hamlet_1))) {
fprintf(stderr, "EVP_DigestVerifyUpdate(hamlet_1) failed.\n");
goto cleanup;
}
if (!EVP_DigestVerifyUpdate(verify_context, hamlet_2, strlen(hamlet_2))) {
fprintf(stderr, "EVP_DigestVerifyUpdate(hamlet_2) failed.\n");
goto cleanup;
}
if (EVP_DigestVerifyFinal(verify_context, sig_value, sig_len) <= 0) {
fprintf(stderr, "EVP_DigestVerifyFinal failed.\n");
goto cleanup;
}
fprintf(stdout, "Signature verified.\n");
ret = 1;
cleanup:
/* OpenSSL free functions will ignore NULL arguments */
EVP_PKEY_free(pub_key);
EVP_MD_CTX_free(verify_context);
return ret;
}
int main(void)
{
OSSL_LIB_CTX *libctx = NULL;
const char *sig_name = "SHA3-512";
size_t sig_len = 0;
unsigned char *sig_value = NULL;
int ret = EXIT_FAILURE;
libctx = OSSL_LIB_CTX_new();
if (libctx == NULL) {
fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
goto cleanup;
}
if (!demo_sign(libctx, sig_name, &sig_len, &sig_value)) {
fprintf(stderr, "demo_sign failed.\n");
goto cleanup;
}
if (!demo_verify(libctx, sig_name, sig_len, sig_value)) {
fprintf(stderr, "demo_verify failed.\n");
goto cleanup;
}
ret = EXIT_SUCCESS;
cleanup:
if (ret != EXIT_SUCCESS)
ERR_print_errors_fp(stderr);
/* OpenSSL free functions will ignore NULL arguments */
OSSL_LIB_CTX_free(libctx);
OPENSSL_free(sig_value);
return ret;
}
| 7,450 | 30.572034 | 84 | c |
openssl | openssl-master/demos/signature/EVP_EC_Signature_demo.h | /*-
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Signers private EC key */
static const unsigned char priv_key_der[] = {
0x30, 0x82, 0x01, 0x68, 0x02, 0x01, 0x01, 0x04, 0x20, 0x51, 0x77, 0xae,
0xf4, 0x18, 0xf4, 0x6b, 0xc4, 0xe5, 0xbb, 0xe9, 0xe6, 0x9e, 0x6d, 0xb0,
0xea, 0x12, 0xf9, 0xf3, 0xdb, 0x9d, 0x56, 0x59, 0xf7, 0x5a, 0x17, 0xd7,
0xd1, 0xe4, 0xd7, 0x47, 0x28, 0xa0, 0x81, 0xfa, 0x30, 0x81, 0xf7, 0x02,
0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01,
0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x30, 0x5b, 0x04, 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
0x04, 0x20, 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb,
0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15,
0x00, 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78,
0xe1, 0x13, 0x9d, 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04,
0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5,
0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2,
0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
0x37, 0xbf, 0x51, 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00,
0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc,
0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc,
0x63, 0x25, 0x51, 0x02, 0x01, 0x01, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
0x4f, 0xe7, 0x7b, 0xb6, 0xbb, 0x54, 0x42, 0x39, 0xed, 0x5d, 0xe5, 0x40,
0xc8, 0xd8, 0x71, 0xca, 0x6d, 0x83, 0x71, 0xd1, 0x88, 0x2a, 0x65, 0x00,
0x6c, 0xc6, 0x2f, 0x01, 0x31, 0x49, 0xbe, 0x76, 0x7a, 0x67, 0x6a, 0x28,
0x33, 0xc7, 0x5b, 0xb9, 0x24, 0x45, 0x24, 0x6e, 0xf0, 0x6d, 0x2f, 0x34,
0x06, 0x53, 0x73, 0x6a, 0xff, 0x90, 0x90, 0xc1, 0x6d, 0x9b, 0x94, 0x0d,
0x0e, 0x1f, 0x95, 0x65,
};
/* The matching public key used for verifying */
static const unsigned char pub_key_der[] = {
0x30, 0x82, 0x01, 0x4b, 0x30, 0x82, 0x01, 0x03, 0x06, 0x07, 0x2a, 0x86,
0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0xf7, 0x02, 0x01, 0x01, 0x30,
0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21,
0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x30, 0x5b, 0x04,
0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x04, 0x20, 0x5a,
0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd, 0x55, 0x76,
0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6, 0x3b,
0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15, 0x00, 0xc4, 0x9d,
0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d,
0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04, 0x6b, 0x17, 0xd1,
0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40,
0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39,
0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f,
0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33,
0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51,
0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad,
0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04, 0x4f, 0xe7, 0x7b, 0xb6, 0xbb,
0x54, 0x42, 0x39, 0xed, 0x5d, 0xe5, 0x40, 0xc8, 0xd8, 0x71, 0xca, 0x6d,
0x83, 0x71, 0xd1, 0x88, 0x2a, 0x65, 0x00, 0x6c, 0xc6, 0x2f, 0x01, 0x31,
0x49, 0xbe, 0x76, 0x7a, 0x67, 0x6a, 0x28, 0x33, 0xc7, 0x5b, 0xb9, 0x24,
0x45, 0x24, 0x6e, 0xf0, 0x6d, 0x2f, 0x34, 0x06, 0x53, 0x73, 0x6a, 0xff,
0x90, 0x90, 0xc1, 0x6d, 0x9b, 0x94, 0x0d, 0x0e, 0x1f, 0x95, 0x65,
};
| 4,704 | 60.103896 | 74 | h |
openssl | openssl-master/demos/signature/rsa_pss.h | /*-
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* 4096-bit RSA private key, DER. */
static const unsigned char rsa_priv_key[] = {
0x30, 0x82, 0x09, 0x28, 0x02, 0x01, 0x00, 0x02, 0x82, 0x02, 0x01, 0x00,
0xa3, 0x14, 0xe4, 0xb8, 0xd8, 0x58, 0x0d, 0xab, 0xd7, 0x87, 0xa4, 0xf6,
0x84, 0x51, 0x74, 0x60, 0x4c, 0xe3, 0x60, 0x28, 0x89, 0x49, 0x65, 0x18,
0x5c, 0x8f, 0x1a, 0x1b, 0xe9, 0xdb, 0xc1, 0xc1, 0xf7, 0x08, 0x27, 0x44,
0xe5, 0x9d, 0x9a, 0x33, 0xc3, 0xac, 0x5a, 0xca, 0xba, 0x20, 0x5a, 0x9e,
0x3a, 0x18, 0xb5, 0x3d, 0xe3, 0x9d, 0x94, 0x58, 0xa7, 0xa9, 0x5a, 0x0b,
0x4f, 0xb8, 0xe5, 0xa3, 0x7b, 0x01, 0x11, 0x0f, 0x16, 0x11, 0xb8, 0x65,
0x2f, 0xa8, 0x95, 0xf7, 0x58, 0x2c, 0xec, 0x1d, 0x41, 0xad, 0xd1, 0x12,
0xca, 0x4a, 0x80, 0x35, 0x35, 0x43, 0x7e, 0xe0, 0x97, 0xfc, 0x86, 0x8f,
0xcf, 0x4b, 0xdc, 0xbc, 0x15, 0x2c, 0x8e, 0x90, 0x84, 0x26, 0x83, 0xc1,
0x96, 0x97, 0xf4, 0xd7, 0x90, 0xce, 0xfe, 0xd4, 0xf3, 0x70, 0x22, 0xa8,
0xb0, 0x1f, 0xed, 0x08, 0xd7, 0xc5, 0xc0, 0xd6, 0x41, 0x6b, 0x24, 0x68,
0x5c, 0x07, 0x1f, 0x44, 0x97, 0xd8, 0x6e, 0x18, 0x93, 0x67, 0xc3, 0xba,
0x3a, 0xaf, 0xfd, 0xc2, 0x65, 0x00, 0x21, 0x63, 0xdf, 0xb7, 0x28, 0x68,
0xd6, 0xc0, 0x20, 0x86, 0x92, 0xed, 0x68, 0x6a, 0x27, 0x3a, 0x07, 0xec,
0x66, 0x00, 0xfe, 0x51, 0x51, 0x86, 0x41, 0x6f, 0x83, 0x69, 0xd2, 0xf0,
0xe6, 0xf7, 0x61, 0xda, 0x12, 0x45, 0x53, 0x09, 0xdf, 0xf8, 0x42, 0xc7,
0x30, 0x6a, 0xe5, 0xd8, 0x2b, 0xa2, 0x49, 0x7a, 0x05, 0x10, 0xee, 0xb2,
0x59, 0x0a, 0xe5, 0xbe, 0xf8, 0x4d, 0x0f, 0xa8, 0x9e, 0x63, 0x81, 0x39,
0x32, 0xaa, 0xfd, 0xa8, 0x03, 0xf6, 0xd8, 0xc6, 0xaa, 0x02, 0x93, 0x03,
0xeb, 0x15, 0xd3, 0x38, 0xc8, 0x1a, 0x78, 0xcf, 0xf3, 0xa7, 0x9f, 0x98,
0x4b, 0x91, 0x5b, 0x79, 0xf8, 0x4e, 0x53, 0xaf, 0x0c, 0x65, 0xe9, 0xb0,
0x93, 0xc2, 0xcb, 0x5d, 0x3c, 0x5f, 0x6e, 0x39, 0xd2, 0x58, 0x23, 0x50,
0xe5, 0x2e, 0xef, 0x12, 0x00, 0xa4, 0x59, 0x13, 0x2b, 0x2f, 0x2c, 0x0a,
0x7b, 0x36, 0x89, 0xc5, 0xe5, 0x8f, 0x95, 0x5e, 0x14, 0x0f, 0x0f, 0x94,
0x5a, 0xe9, 0xdc, 0x0b, 0x49, 0x14, 0xbe, 0x0a, 0x70, 0x45, 0xc1, 0x7c,
0xbf, 0x83, 0x70, 0xfd, 0x3d, 0x99, 0xe6, 0x8a, 0xf5, 0x9c, 0x09, 0x71,
0x84, 0x9a, 0x18, 0xa0, 0xe0, 0x6c, 0x43, 0x5c, 0x7e, 0x48, 0x33, 0xc8,
0xbe, 0x5d, 0xdd, 0xd8, 0x77, 0xe3, 0xe7, 0x6b, 0x34, 0x4b, 0xa2, 0xb7,
0x54, 0x07, 0x72, 0x2e, 0xab, 0xa9, 0x91, 0x1e, 0x4b, 0xe3, 0xb5, 0xd8,
0xfa, 0x35, 0x64, 0x8a, 0xe9, 0x03, 0xa1, 0xa8, 0x26, 0xbd, 0x72, 0x58,
0x10, 0x6a, 0xec, 0x1a, 0xf6, 0x1e, 0xb8, 0xc0, 0x46, 0x19, 0x31, 0x2c,
0xca, 0xf9, 0x6a, 0xd7, 0x2e, 0xd0, 0xa7, 0x2c, 0x60, 0x58, 0xc4, 0x8f,
0x46, 0x63, 0x61, 0x8d, 0x29, 0x6f, 0xe2, 0x5f, 0xe2, 0x43, 0x90, 0x9c,
0xe6, 0xfc, 0x08, 0x41, 0xc8, 0xb5, 0x23, 0x56, 0x24, 0x3e, 0x3a, 0x2c,
0x41, 0x22, 0x43, 0xda, 0x22, 0x15, 0x2b, 0xad, 0xd0, 0xfa, 0xc8, 0x47,
0x44, 0xe6, 0x2a, 0xf9, 0x38, 0x90, 0x13, 0x62, 0x22, 0xea, 0x06, 0x8c,
0x44, 0x9c, 0xd6, 0xca, 0x50, 0x93, 0xe9, 0xd4, 0x03, 0xd8, 0x3e, 0x71,
0x36, 0x4b, 0xaa, 0xab, 0xbb, 0xe2, 0x48, 0x66, 0x26, 0x53, 0xb1, 0x6d,
0x3b, 0x82, 0x2c, 0x8c, 0x25, 0x05, 0xf0, 0xf8, 0xcf, 0x55, 0xbf, 0x8e,
0x29, 0xf7, 0x54, 0x5b, 0x6f, 0x30, 0x54, 0xa6, 0xad, 0x46, 0xff, 0x22,
0x95, 0xb1, 0x87, 0x98, 0x00, 0x51, 0x69, 0x15, 0x07, 0xbd, 0x3d, 0x9c,
0x6e, 0xaa, 0xaa, 0x3b, 0x0b, 0x74, 0x65, 0x4c, 0x04, 0xe0, 0x80, 0x3e,
0xaf, 0x5e, 0x10, 0xd6, 0x9b, 0x28, 0x37, 0x6f, 0x02, 0x03, 0x01, 0x00,
0x01, 0x02, 0x82, 0x02, 0x00, 0x09, 0x6e, 0xf8, 0xf8, 0x14, 0x53, 0xab,
0x9e, 0xc8, 0x1d, 0xe9, 0x95, 0xf4, 0xfb, 0x7d, 0x3e, 0xe0, 0xd3, 0xba,
0x49, 0x3d, 0xff, 0xc7, 0xe0, 0x4b, 0xe2, 0x5f, 0x41, 0x44, 0x1a, 0xd9,
0x2f, 0x6e, 0x29, 0xc3, 0x93, 0xc1, 0xb0, 0x87, 0x2d, 0xfd, 0x60, 0xa7,
0xf3, 0xd8, 0x26, 0x6c, 0xf7, 0x80, 0x26, 0xd3, 0xbd, 0x1b, 0xc0, 0x8e,
0xc7, 0x3e, 0x13, 0x96, 0xc8, 0xd6, 0xb8, 0xbc, 0x57, 0xe3, 0x92, 0xa1,
0x38, 0xfd, 0x2e, 0xd3, 0x3a, 0xcf, 0x31, 0xf2, 0x52, 0xd7, 0x7f, 0xe9,
0xbc, 0x9b, 0x83, 0x01, 0x78, 0x13, 0xc9, 0x91, 0x77, 0x02, 0x78, 0xc0,
0x0b, 0x1f, 0xdf, 0x94, 0xad, 0x16, 0xf1, 0xad, 0x78, 0x17, 0xc5, 0x77,
0x0d, 0xb7, 0x07, 0x3f, 0x51, 0xe0, 0x73, 0x33, 0xcf, 0x90, 0x69, 0xd8,
0xe5, 0xda, 0x9b, 0x1e, 0xf6, 0x21, 0x12, 0x07, 0xb5, 0x1e, 0x3e, 0x2b,
0x34, 0x79, 0x9e, 0x48, 0x01, 0xdd, 0x68, 0xf0, 0x0f, 0x18, 0xb5, 0x85,
0x50, 0xd8, 0x9e, 0x04, 0xfd, 0x6d, 0xcd, 0xa6, 0x61, 0x2b, 0x54, 0x81,
0x99, 0xf4, 0x63, 0xf4, 0xeb, 0x73, 0x98, 0xb3, 0x88, 0xf5, 0x50, 0xd4,
0x5c, 0x67, 0x9e, 0x7c, 0xbc, 0xd8, 0xfd, 0xaf, 0xb8, 0x66, 0x7d, 0xdc,
0xa5, 0x25, 0xb5, 0xe6, 0x64, 0xd7, 0x07, 0x72, 0x5a, 0x99, 0xf9, 0xf6,
0x9e, 0xb8, 0x9c, 0xf4, 0xc7, 0xee, 0xee, 0x10, 0x13, 0x9c, 0x1a, 0x8c,
0x23, 0x89, 0xcd, 0x7b, 0xf1, 0x47, 0x23, 0x51, 0x3c, 0xe5, 0xc2, 0x17,
0x68, 0xca, 0x98, 0xb8, 0xed, 0xe5, 0x17, 0x6d, 0x0a, 0xde, 0x07, 0xd6,
0x6c, 0x4f, 0x83, 0x4c, 0x9b, 0xca, 0x6a, 0x7d, 0xc8, 0x68, 0x12, 0xd7,
0xf0, 0x37, 0x88, 0xf7, 0xbb, 0x68, 0x8b, 0xa4, 0xfd, 0xfe, 0x36, 0x11,
0xb3, 0x2b, 0x85, 0x6d, 0xaa, 0x30, 0x31, 0xf1, 0x6f, 0x80, 0x72, 0x42,
0x23, 0xfe, 0x93, 0x88, 0xcc, 0x1e, 0x4b, 0x53, 0x4f, 0x8e, 0x24, 0x67,
0x4a, 0x72, 0xb6, 0x3c, 0x13, 0x00, 0x11, 0x4f, 0xe1, 0x30, 0xd6, 0xe7,
0x45, 0x8f, 0xaf, 0xdd, 0xe5, 0xaa, 0xb7, 0x02, 0x17, 0x04, 0xf8, 0xd2,
0xc1, 0x7b, 0x6c, 0x92, 0xec, 0x76, 0x94, 0x1b, 0xb0, 0xe4, 0xc3, 0x0c,
0x9e, 0xee, 0xb5, 0xdc, 0x97, 0xca, 0x10, 0x1d, 0x17, 0x96, 0x45, 0xd4,
0x04, 0x0c, 0xea, 0xca, 0x45, 0xfc, 0x52, 0x54, 0x82, 0x9b, 0xdf, 0x64,
0xd6, 0x59, 0x6c, 0x12, 0x70, 0xf0, 0x19, 0xd8, 0x46, 0xbb, 0x08, 0x43,
0x81, 0xa1, 0x73, 0xa8, 0x00, 0xc9, 0x4e, 0xb9, 0xd5, 0xfd, 0x42, 0x5f,
0xcf, 0x94, 0x14, 0x18, 0xab, 0x9d, 0x11, 0xd0, 0xbd, 0x44, 0x88, 0x2c,
0xd8, 0x29, 0xec, 0x94, 0x70, 0xf9, 0x42, 0x14, 0xf4, 0xb0, 0x3f, 0xfe,
0x27, 0x16, 0x43, 0x59, 0x90, 0x14, 0x48, 0x61, 0x8c, 0x91, 0xd9, 0x37,
0x41, 0xef, 0xf1, 0xe9, 0x15, 0x4a, 0x4f, 0x5e, 0x1f, 0x50, 0x25, 0x20,
0x2d, 0xa6, 0xf8, 0x79, 0x0d, 0x92, 0xb0, 0x00, 0x0b, 0xa2, 0xfb, 0xc3,
0x7b, 0x0f, 0xa6, 0xff, 0x75, 0x5d, 0x70, 0xaa, 0xcf, 0x0a, 0xdf, 0xe1,
0xfc, 0x32, 0x53, 0x1e, 0xf6, 0xe6, 0x69, 0x9f, 0x09, 0xd0, 0xc8, 0xab,
0xaf, 0xec, 0xb0, 0x04, 0xfa, 0x83, 0xe2, 0x29, 0x23, 0x54, 0x37, 0x87,
0x63, 0x47, 0x75, 0x9b, 0xdb, 0x1f, 0x4f, 0x1b, 0x6b, 0xa6, 0xe2, 0x67,
0x1c, 0xb4, 0x74, 0x9e, 0x48, 0x77, 0x61, 0xc2, 0x9a, 0x3e, 0x6b, 0x89,
0xa9, 0x68, 0x74, 0x27, 0x01, 0x29, 0xd6, 0x46, 0xe8, 0x0f, 0xd0, 0x33,
0x22, 0x00, 0x45, 0x6c, 0xde, 0x32, 0x28, 0x42, 0x57, 0xaf, 0x70, 0x28,
0xa0, 0xd5, 0x99, 0xbb, 0x1f, 0xd7, 0x3c, 0x84, 0x20, 0x70, 0x1f, 0xe3,
0xa9, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe6, 0x68, 0xfe, 0x5f, 0x75, 0x71,
0x2a, 0xd8, 0xcf, 0x0d, 0x1d, 0xf4, 0xa1, 0x06, 0x8b, 0xa5, 0x70, 0x6f,
0x29, 0x03, 0xf3, 0x50, 0xd3, 0x83, 0x39, 0xf9, 0xf6, 0xe5, 0x79, 0x7a,
0x29, 0x75, 0xde, 0xda, 0x6a, 0x98, 0x7c, 0x33, 0xf8, 0x64, 0xca, 0x86,
0x5a, 0xda, 0x55, 0x5b, 0x4d, 0x7b, 0x1a, 0xe5, 0x5d, 0x19, 0x7d, 0xf3,
0x57, 0x49, 0x3d, 0x7a, 0xe8, 0x3f, 0x5a, 0x40, 0x8c, 0x15, 0xc7, 0xb0,
0x53, 0xf8, 0x63, 0x42, 0x17, 0x7c, 0x20, 0xb9, 0xfc, 0xff, 0x27, 0xd0,
0xc2, 0x0c, 0x45, 0x52, 0x1b, 0x75, 0x1f, 0x89, 0x87, 0xc4, 0xa8, 0x07,
0x3b, 0x73, 0x16, 0xc7, 0xd7, 0x77, 0x2e, 0x47, 0xa2, 0x7d, 0x12, 0xb4,
0x25, 0x24, 0x5e, 0xa5, 0xb2, 0x12, 0x76, 0x65, 0xd1, 0xcd, 0xa4, 0x66,
0x33, 0x2d, 0xed, 0xb2, 0x85, 0xb0, 0xb3, 0x33, 0x56, 0x18, 0x5a, 0xb3,
0x75, 0x43, 0x4d, 0x40, 0x14, 0x22, 0x55, 0xf6, 0x5a, 0x0c, 0x6a, 0xb3,
0xc3, 0x8a, 0x9b, 0x76, 0x1e, 0x23, 0x8d, 0x4a, 0x8f, 0x38, 0x21, 0x25,
0x43, 0x45, 0xf6, 0x25, 0x46, 0xdb, 0xae, 0x42, 0x43, 0x74, 0x69, 0x15,
0x46, 0xf0, 0x3a, 0x41, 0x4f, 0x9f, 0xfe, 0xda, 0x07, 0x0b, 0x38, 0xbe,
0x6b, 0xad, 0xc2, 0xef, 0x5b, 0x97, 0x18, 0x42, 0x13, 0xac, 0x13, 0x15,
0x70, 0x7b, 0xe2, 0x00, 0xbb, 0x41, 0x22, 0x99, 0xe5, 0xd3, 0x67, 0xfe,
0xfd, 0xbd, 0x8e, 0xc3, 0xca, 0x60, 0x59, 0x3d, 0x8f, 0x85, 0x76, 0x41,
0xf0, 0xb8, 0x09, 0x1a, 0x48, 0x50, 0xe4, 0x9c, 0x4a, 0x56, 0x02, 0x60,
0x76, 0xff, 0xde, 0xd4, 0x8e, 0x76, 0xa3, 0x9c, 0x30, 0xb4, 0xa4, 0x73,
0xe6, 0xb0, 0x70, 0xac, 0x67, 0x5f, 0x25, 0xd2, 0x94, 0xc5, 0x25, 0xb6,
0xbf, 0xf6, 0x0b, 0xd8, 0x9f, 0x35, 0x8c, 0x20, 0xb6, 0xdd, 0x02, 0x82,
0x01, 0x01, 0x00, 0xb5, 0x31, 0x9e, 0xa2, 0x10, 0x38, 0xca, 0x2b, 0x07,
0xc9, 0x3f, 0x0f, 0x18, 0x2c, 0x98, 0x7f, 0x15, 0x87, 0x92, 0x93, 0x2e,
0xce, 0x6b, 0x11, 0x42, 0x2a, 0x94, 0x3e, 0x31, 0xd0, 0xf5, 0xae, 0x9d,
0xc7, 0x67, 0x51, 0x3c, 0x0a, 0x52, 0x04, 0x94, 0x86, 0x2e, 0x50, 0x32,
0xe1, 0x48, 0x83, 0x85, 0xe8, 0x82, 0x04, 0x2f, 0x25, 0xbc, 0xea, 0xfc,
0x3d, 0x4b, 0xd1, 0x53, 0x90, 0x61, 0x97, 0x47, 0x73, 0xcd, 0x1f, 0xa9,
0x5a, 0x3f, 0xfb, 0xbf, 0xc3, 0xd5, 0x19, 0xb6, 0xd3, 0x59, 0x57, 0x37,
0xd9, 0x09, 0x29, 0xd3, 0x80, 0xc4, 0xae, 0x52, 0xce, 0xce, 0x82, 0x29,
0x6b, 0x95, 0x44, 0x69, 0x33, 0xfd, 0x6a, 0x6d, 0x65, 0xf7, 0xa9, 0xc0,
0x65, 0x25, 0x91, 0x05, 0xdf, 0x07, 0xbe, 0x61, 0x5c, 0xaa, 0x8f, 0x87,
0xc8, 0x43, 0xd7, 0x30, 0xd0, 0x8b, 0x25, 0xaf, 0xb8, 0x5d, 0x50, 0x4e,
0x31, 0x4a, 0xc9, 0x79, 0x56, 0xbf, 0x8d, 0xcc, 0x40, 0xa7, 0xea, 0xd4,
0xf7, 0x66, 0x86, 0xe2, 0x0b, 0xf3, 0x13, 0xbc, 0xdc, 0x0d, 0x62, 0x28,
0x4e, 0xb7, 0x31, 0xb4, 0x5a, 0x9b, 0x97, 0x65, 0x76, 0x24, 0xbb, 0xef,
0x90, 0x1b, 0xdb, 0x93, 0x98, 0xae, 0xce, 0xb0, 0x69, 0x82, 0x49, 0x94,
0xc0, 0xc3, 0x8f, 0x9c, 0x5d, 0x26, 0x45, 0xa0, 0xad, 0x15, 0x3b, 0x6e,
0xda, 0x6e, 0x78, 0xc1, 0x78, 0xc3, 0x15, 0x8e, 0x64, 0xaf, 0x50, 0xa6,
0xb7, 0xd9, 0xfb, 0x8f, 0x68, 0xa0, 0x2d, 0x59, 0xa9, 0xce, 0x5b, 0xa7,
0x91, 0x36, 0xb8, 0x05, 0x28, 0x31, 0x25, 0xc7, 0x7e, 0xa4, 0x68, 0x9d,
0xea, 0x5c, 0x71, 0x10, 0x84, 0xab, 0xc4, 0xd7, 0xbe, 0x7d, 0xe9, 0x4a,
0x11, 0x22, 0xa6, 0xd5, 0xa3, 0x6e, 0x46, 0x07, 0x70, 0x78, 0xcc, 0xd5,
0xbc, 0xfe, 0xc4, 0x39, 0x58, 0xf4, 0xbb, 0x02, 0x82, 0x01, 0x01, 0x00,
0xaa, 0x0c, 0x73, 0x30, 0x20, 0x8d, 0x15, 0x02, 0x4e, 0x4d, 0x6f, 0xfe,
0x4b, 0x99, 0x79, 0x16, 0xf0, 0x94, 0x19, 0xc1, 0x40, 0xa2, 0x36, 0x78,
0x73, 0x21, 0x78, 0x86, 0x83, 0xd1, 0x15, 0x28, 0x59, 0x00, 0xfa, 0x0a,
0xf0, 0x1f, 0xab, 0x03, 0x38, 0x35, 0x50, 0x78, 0x32, 0xe6, 0xdf, 0x98,
0x2b, 0x91, 0x7b, 0xd4, 0x84, 0x90, 0x43, 0xab, 0x5a, 0x24, 0x8b, 0xa3,
0xb6, 0x08, 0x4d, 0x5b, 0x05, 0xb5, 0xad, 0x43, 0x74, 0x7e, 0x22, 0xb7,
0x09, 0xb0, 0x3a, 0x78, 0x55, 0xfa, 0x4c, 0x3c, 0xa2, 0x2c, 0xa6, 0xf7,
0x19, 0xff, 0x76, 0xa4, 0x3d, 0x1e, 0x99, 0x51, 0xa7, 0x4e, 0x76, 0x47,
0x0f, 0x70, 0xef, 0x0b, 0x3f, 0xf2, 0x94, 0x36, 0xf3, 0x63, 0x76, 0xb9,
0x09, 0x88, 0xbb, 0xfe, 0xf9, 0x86, 0x33, 0xdf, 0x81, 0xbe, 0x6f, 0xcc,
0xa9, 0x75, 0x09, 0xe5, 0x8f, 0x8b, 0x42, 0xd0, 0x19, 0x03, 0x61, 0xd8,
0xb5, 0x78, 0xcb, 0x9c, 0xbe, 0x63, 0x4d, 0xbd, 0xce, 0x5e, 0xae, 0x7f,
0xae, 0x97, 0x88, 0x7b, 0xf4, 0x7a, 0x7b, 0xdb, 0xf6, 0x7e, 0x2c, 0x7d,
0x95, 0x6e, 0x72, 0x3a, 0x48, 0x13, 0xdb, 0xf7, 0x10, 0x07, 0x83, 0xac,
0xa1, 0x7a, 0x68, 0x18, 0x70, 0x18, 0x99, 0x7f, 0xf4, 0x8e, 0x93, 0x1a,
0x40, 0x5d, 0x04, 0x07, 0xcb, 0x4d, 0xd7, 0x66, 0x96, 0xb5, 0xd3, 0x7d,
0x8e, 0xfb, 0xe6, 0x12, 0xd0, 0x7d, 0xf0, 0xe7, 0x25, 0xa6, 0x7a, 0x86,
0x01, 0x56, 0xdd, 0xc5, 0xb2, 0x31, 0x98, 0x67, 0x3a, 0xd0, 0x9a, 0xee,
0x98, 0xca, 0x80, 0x52, 0x5a, 0x0e, 0xb7, 0xc4, 0xbf, 0xc0, 0x40, 0x24,
0x6f, 0x3b, 0xa6, 0xf6, 0xab, 0x28, 0x9e, 0xe9, 0x39, 0x3f, 0x04, 0x4b,
0xc4, 0xae, 0x55, 0xfd, 0xea, 0x87, 0xa5, 0xc5, 0x01, 0x99, 0x2e, 0x67,
0x66, 0xb3, 0xfe, 0x41, 0x02, 0x82, 0x01, 0x00, 0x05, 0x26, 0x96, 0xf2,
0xd6, 0x71, 0x36, 0xd6, 0x08, 0x4f, 0xa1, 0x3a, 0x45, 0x9e, 0xa6, 0xeb,
0x1d, 0xea, 0x8f, 0xb1, 0x1d, 0x68, 0x82, 0xc4, 0xa7, 0xd3, 0xdc, 0x08,
0xf4, 0x93, 0x93, 0x18, 0x56, 0xa5, 0xdf, 0x7b, 0x00, 0xb0, 0xee, 0x69,
0xf0, 0xea, 0xeb, 0x90, 0x1e, 0x12, 0x27, 0x64, 0x8d, 0xbe, 0xf1, 0x4b,
0x3b, 0x27, 0xe0, 0x79, 0xf1, 0x97, 0xb0, 0x7b, 0x0f, 0xdc, 0x0f, 0xda,
0x24, 0x0e, 0xd7, 0xaa, 0xe9, 0xbe, 0x86, 0x09, 0x1b, 0x07, 0x6f, 0x1c,
0x5f, 0x05, 0x1d, 0x0a, 0x0c, 0xad, 0x5f, 0xc4, 0x4f, 0x9d, 0xde, 0x79,
0x72, 0x23, 0x2c, 0xdd, 0xa8, 0x5d, 0xc5, 0x8d, 0x7f, 0x4c, 0x1a, 0x0d,
0x17, 0x75, 0x09, 0x98, 0x4a, 0xbe, 0xd5, 0x55, 0x8d, 0x0c, 0x2d, 0x05,
0x2d, 0x71, 0x5b, 0xeb, 0xde, 0x99, 0x43, 0xcc, 0x6f, 0x37, 0xce, 0x6c,
0xd0, 0xd4, 0xf5, 0xda, 0x1d, 0x8e, 0xeb, 0x28, 0x55, 0x09, 0xb1, 0x42,
0x4f, 0xa7, 0x1a, 0xde, 0xe3, 0x14, 0xf1, 0x56, 0x2e, 0x40, 0xd6, 0xb5,
0x1d, 0xee, 0x47, 0x77, 0x1d, 0xdc, 0x36, 0xfa, 0xf3, 0xbc, 0x8b, 0xa5,
0xbf, 0x1d, 0x9f, 0xa7, 0xb4, 0x04, 0xad, 0xb6, 0x0d, 0x39, 0x0e, 0xe7,
0x13, 0x3e, 0xbc, 0x94, 0x68, 0xe5, 0x1d, 0xea, 0x0c, 0x30, 0xdd, 0xb0,
0xa7, 0x03, 0xa4, 0x91, 0xde, 0xf1, 0xd8, 0xa8, 0x18, 0x1f, 0xdd, 0xb3,
0xd4, 0x2b, 0x6a, 0x8c, 0x69, 0x60, 0xda, 0x92, 0x7b, 0x1e, 0x27, 0x47,
0x82, 0xbf, 0xff, 0xfc, 0xbd, 0x03, 0xb4, 0xc1, 0x80, 0x6c, 0x07, 0x11,
0xa2, 0xdd, 0x27, 0xc1, 0x4d, 0x93, 0xe6, 0xf2, 0xd3, 0xdc, 0x61, 0xa1,
0xa3, 0xdc, 0x67, 0x69, 0xe5, 0x50, 0x1d, 0x63, 0x0e, 0xb9, 0xa9, 0x9d,
0xd6, 0x02, 0x4d, 0x7c, 0xcd, 0x2a, 0xa5, 0x37, 0x60, 0xc5, 0xf5, 0x97,
0x02, 0x82, 0x01, 0x00, 0x14, 0x8b, 0x04, 0xdb, 0x4e, 0x41, 0x4a, 0xcd,
0x86, 0x2e, 0x5f, 0x13, 0xb3, 0x48, 0x1e, 0x00, 0xdf, 0x8d, 0x0b, 0x35,
0x51, 0x51, 0x1b, 0x16, 0x3d, 0x49, 0x4e, 0xe1, 0xee, 0x4d, 0xc7, 0x03,
0xc0, 0xf6, 0x5c, 0x6c, 0x36, 0xe8, 0x22, 0xa5, 0x79, 0xb4, 0x4c, 0xce,
0xa8, 0x45, 0x12, 0x2c, 0xf3, 0x6a, 0xcd, 0x33, 0xbd, 0xd0, 0x84, 0x4d,
0xf7, 0x8f, 0xb5, 0x80, 0x1f, 0x18, 0x52, 0xad, 0xad, 0xce, 0xcd, 0x94,
0xc9, 0xc6, 0xb4, 0xd2, 0x14, 0x29, 0xe4, 0xc7, 0x40, 0xf1, 0x0b, 0x85,
0x43, 0xaf, 0x11, 0xd3, 0x46, 0x0a, 0xb1, 0x15, 0x87, 0x1f, 0x4e, 0x2e,
0xc1, 0x11, 0xe9, 0x24, 0x70, 0x40, 0xba, 0x0b, 0x0e, 0x4a, 0xac, 0x45,
0x21, 0xcc, 0x6d, 0xa4, 0x1d, 0x55, 0x33, 0x89, 0x4c, 0x65, 0x21, 0x23,
0xab, 0x61, 0x31, 0xcb, 0x11, 0x65, 0xb3, 0x80, 0xa4, 0x5a, 0x2b, 0xf1,
0x65, 0xdb, 0x4c, 0x58, 0x5a, 0xbe, 0xf3, 0x15, 0xcd, 0x94, 0xa1, 0xe4,
0xcb, 0x30, 0xfa, 0xe1, 0x28, 0x51, 0x52, 0xd2, 0xb8, 0xb4, 0x8c, 0xfc,
0x3a, 0xcc, 0xd1, 0x19, 0xa2, 0x27, 0x36, 0xfa, 0xc4, 0x23, 0x96, 0xb9,
0xc7, 0x74, 0xca, 0xf1, 0x45, 0x1f, 0x4b, 0xc2, 0x77, 0x4d, 0x32, 0x3f,
0xab, 0x7a, 0xd9, 0x2b, 0x22, 0x1d, 0xcb, 0x24, 0x58, 0x29, 0xa3, 0xb8,
0x92, 0xdb, 0x1c, 0xda, 0x84, 0x01, 0xca, 0x6d, 0x4a, 0x50, 0xd4, 0x2b,
0x79, 0xfa, 0xc5, 0x4c, 0x9d, 0x79, 0x49, 0xf1, 0xde, 0xbd, 0x3f, 0x50,
0xa7, 0xa6, 0xc6, 0xc7, 0x99, 0x61, 0x9b, 0xda, 0x38, 0xdc, 0xbe, 0x85,
0x75, 0x81, 0xb9, 0x0f, 0x33, 0xd0, 0xd4, 0xd0, 0xaa, 0xbd, 0x32, 0xc9,
0x62, 0xe8, 0x21, 0x24, 0xeb, 0x03, 0x73, 0x46, 0xb3, 0x84, 0x65, 0xf2,
0x40, 0x7d, 0x1b, 0x1b, 0x8f, 0x86, 0x7c, 0xe7
};
/* The corresponding public key, DER. */
static const unsigned char rsa_pub_key[] = {
0x30, 0x82, 0x02, 0x0a, 0x02, 0x82, 0x02, 0x01, 0x00, 0xa3, 0x14, 0xe4,
0xb8, 0xd8, 0x58, 0x0d, 0xab, 0xd7, 0x87, 0xa4, 0xf6, 0x84, 0x51, 0x74,
0x60, 0x4c, 0xe3, 0x60, 0x28, 0x89, 0x49, 0x65, 0x18, 0x5c, 0x8f, 0x1a,
0x1b, 0xe9, 0xdb, 0xc1, 0xc1, 0xf7, 0x08, 0x27, 0x44, 0xe5, 0x9d, 0x9a,
0x33, 0xc3, 0xac, 0x5a, 0xca, 0xba, 0x20, 0x5a, 0x9e, 0x3a, 0x18, 0xb5,
0x3d, 0xe3, 0x9d, 0x94, 0x58, 0xa7, 0xa9, 0x5a, 0x0b, 0x4f, 0xb8, 0xe5,
0xa3, 0x7b, 0x01, 0x11, 0x0f, 0x16, 0x11, 0xb8, 0x65, 0x2f, 0xa8, 0x95,
0xf7, 0x58, 0x2c, 0xec, 0x1d, 0x41, 0xad, 0xd1, 0x12, 0xca, 0x4a, 0x80,
0x35, 0x35, 0x43, 0x7e, 0xe0, 0x97, 0xfc, 0x86, 0x8f, 0xcf, 0x4b, 0xdc,
0xbc, 0x15, 0x2c, 0x8e, 0x90, 0x84, 0x26, 0x83, 0xc1, 0x96, 0x97, 0xf4,
0xd7, 0x90, 0xce, 0xfe, 0xd4, 0xf3, 0x70, 0x22, 0xa8, 0xb0, 0x1f, 0xed,
0x08, 0xd7, 0xc5, 0xc0, 0xd6, 0x41, 0x6b, 0x24, 0x68, 0x5c, 0x07, 0x1f,
0x44, 0x97, 0xd8, 0x6e, 0x18, 0x93, 0x67, 0xc3, 0xba, 0x3a, 0xaf, 0xfd,
0xc2, 0x65, 0x00, 0x21, 0x63, 0xdf, 0xb7, 0x28, 0x68, 0xd6, 0xc0, 0x20,
0x86, 0x92, 0xed, 0x68, 0x6a, 0x27, 0x3a, 0x07, 0xec, 0x66, 0x00, 0xfe,
0x51, 0x51, 0x86, 0x41, 0x6f, 0x83, 0x69, 0xd2, 0xf0, 0xe6, 0xf7, 0x61,
0xda, 0x12, 0x45, 0x53, 0x09, 0xdf, 0xf8, 0x42, 0xc7, 0x30, 0x6a, 0xe5,
0xd8, 0x2b, 0xa2, 0x49, 0x7a, 0x05, 0x10, 0xee, 0xb2, 0x59, 0x0a, 0xe5,
0xbe, 0xf8, 0x4d, 0x0f, 0xa8, 0x9e, 0x63, 0x81, 0x39, 0x32, 0xaa, 0xfd,
0xa8, 0x03, 0xf6, 0xd8, 0xc6, 0xaa, 0x02, 0x93, 0x03, 0xeb, 0x15, 0xd3,
0x38, 0xc8, 0x1a, 0x78, 0xcf, 0xf3, 0xa7, 0x9f, 0x98, 0x4b, 0x91, 0x5b,
0x79, 0xf8, 0x4e, 0x53, 0xaf, 0x0c, 0x65, 0xe9, 0xb0, 0x93, 0xc2, 0xcb,
0x5d, 0x3c, 0x5f, 0x6e, 0x39, 0xd2, 0x58, 0x23, 0x50, 0xe5, 0x2e, 0xef,
0x12, 0x00, 0xa4, 0x59, 0x13, 0x2b, 0x2f, 0x2c, 0x0a, 0x7b, 0x36, 0x89,
0xc5, 0xe5, 0x8f, 0x95, 0x5e, 0x14, 0x0f, 0x0f, 0x94, 0x5a, 0xe9, 0xdc,
0x0b, 0x49, 0x14, 0xbe, 0x0a, 0x70, 0x45, 0xc1, 0x7c, 0xbf, 0x83, 0x70,
0xfd, 0x3d, 0x99, 0xe6, 0x8a, 0xf5, 0x9c, 0x09, 0x71, 0x84, 0x9a, 0x18,
0xa0, 0xe0, 0x6c, 0x43, 0x5c, 0x7e, 0x48, 0x33, 0xc8, 0xbe, 0x5d, 0xdd,
0xd8, 0x77, 0xe3, 0xe7, 0x6b, 0x34, 0x4b, 0xa2, 0xb7, 0x54, 0x07, 0x72,
0x2e, 0xab, 0xa9, 0x91, 0x1e, 0x4b, 0xe3, 0xb5, 0xd8, 0xfa, 0x35, 0x64,
0x8a, 0xe9, 0x03, 0xa1, 0xa8, 0x26, 0xbd, 0x72, 0x58, 0x10, 0x6a, 0xec,
0x1a, 0xf6, 0x1e, 0xb8, 0xc0, 0x46, 0x19, 0x31, 0x2c, 0xca, 0xf9, 0x6a,
0xd7, 0x2e, 0xd0, 0xa7, 0x2c, 0x60, 0x58, 0xc4, 0x8f, 0x46, 0x63, 0x61,
0x8d, 0x29, 0x6f, 0xe2, 0x5f, 0xe2, 0x43, 0x90, 0x9c, 0xe6, 0xfc, 0x08,
0x41, 0xc8, 0xb5, 0x23, 0x56, 0x24, 0x3e, 0x3a, 0x2c, 0x41, 0x22, 0x43,
0xda, 0x22, 0x15, 0x2b, 0xad, 0xd0, 0xfa, 0xc8, 0x47, 0x44, 0xe6, 0x2a,
0xf9, 0x38, 0x90, 0x13, 0x62, 0x22, 0xea, 0x06, 0x8c, 0x44, 0x9c, 0xd6,
0xca, 0x50, 0x93, 0xe9, 0xd4, 0x03, 0xd8, 0x3e, 0x71, 0x36, 0x4b, 0xaa,
0xab, 0xbb, 0xe2, 0x48, 0x66, 0x26, 0x53, 0xb1, 0x6d, 0x3b, 0x82, 0x2c,
0x8c, 0x25, 0x05, 0xf0, 0xf8, 0xcf, 0x55, 0xbf, 0x8e, 0x29, 0xf7, 0x54,
0x5b, 0x6f, 0x30, 0x54, 0xa6, 0xad, 0x46, 0xff, 0x22, 0x95, 0xb1, 0x87,
0x98, 0x00, 0x51, 0x69, 0x15, 0x07, 0xbd, 0x3d, 0x9c, 0x6e, 0xaa, 0xaa,
0x3b, 0x0b, 0x74, 0x65, 0x4c, 0x04, 0xe0, 0x80, 0x3e, 0xaf, 0x5e, 0x10,
0xd6, 0x9b, 0x28, 0x37, 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01
};
| 18,711 | 71.809339 | 75 | h |
openssl | openssl-master/demos/signature/rsa_pss_direct.c | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include "rsa_pss.h"
/*
* The digest to be signed. This should be the output of a hash function.
* Here we sign an all-zeroes digest for demonstration purposes.
*/
static const unsigned char test_digest[32] = {0};
/* A property query used for selecting algorithm implementations. */
static const char *propq = NULL;
/*
* This function demonstrates RSA signing of a SHA-256 digest using the PSS
* padding scheme. You must already have hashed the data you want to sign.
* For a higher-level demonstration which does the hashing for you, see
* rsa_pss_hash.c.
*
* For more information, see RFC 8017 section 9.1. The digest passed in
* (test_digest above) corresponds to the 'mHash' value.
*/
static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_MD *md = NULL;
const unsigned char *ppriv_key = NULL;
*sig = NULL;
/* Load DER-encoded RSA private key. */
ppriv_key = rsa_priv_key;
pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,
sizeof(rsa_priv_key), libctx, propq);
if (pkey == NULL) {
fprintf(stderr, "Failed to load private key\n");
goto end;
}
/* Fetch hash algorithm we want to use. */
md = EVP_MD_fetch(libctx, "SHA256", propq);
if (md == NULL) {
fprintf(stderr, "Failed to fetch hash algorithm\n");
goto end;
}
/* Create signing context. */
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
if (ctx == NULL) {
fprintf(stderr, "Failed to create signing context\n");
goto end;
}
/* Initialize context for signing and set options. */
if (EVP_PKEY_sign_init(ctx) == 0) {
fprintf(stderr, "Failed to initialize signing context\n");
goto end;
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
fprintf(stderr, "Failed to configure padding\n");
goto end;
}
if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
fprintf(stderr, "Failed to configure digest type\n");
goto end;
}
/* Determine length of signature. */
if (EVP_PKEY_sign(ctx, NULL, sig_len,
test_digest, sizeof(test_digest)) == 0) {
fprintf(stderr, "Failed to get signature length\n");
goto end;
}
/* Allocate memory for signature. */
*sig = OPENSSL_malloc(*sig_len);
if (*sig == NULL) {
fprintf(stderr, "Failed to allocate memory for signature\n");
goto end;
}
/* Generate signature. */
if (EVP_PKEY_sign(ctx, *sig, sig_len,
test_digest, sizeof(test_digest)) != 1) {
fprintf(stderr, "Failed to sign\n");
goto end;
}
ret = 1;
end:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
EVP_MD_free(md);
if (ret == 0)
OPENSSL_free(*sig);
return ret;
}
/*
* This function demonstrates verification of an RSA signature over a SHA-256
* digest using the PSS signature scheme.
*/
static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
{
int ret = 0;
const unsigned char *ppub_key = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_MD *md = NULL;
/* Load DER-encoded RSA public key. */
ppub_key = rsa_pub_key;
pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));
if (pkey == NULL) {
fprintf(stderr, "Failed to load public key\n");
goto end;
}
/* Fetch hash algorithm we want to use. */
md = EVP_MD_fetch(libctx, "SHA256", propq);
if (md == NULL) {
fprintf(stderr, "Failed to fetch hash algorithm\n");
goto end;
}
/* Create verification context. */
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
if (ctx == NULL) {
fprintf(stderr, "Failed to create verification context\n");
goto end;
}
/* Initialize context for verification and set options. */
if (EVP_PKEY_verify_init(ctx) == 0) {
fprintf(stderr, "Failed to initialize verification context\n");
goto end;
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
fprintf(stderr, "Failed to configure padding\n");
goto end;
}
if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
fprintf(stderr, "Failed to configure digest type\n");
goto end;
}
/* Verify signature. */
if (EVP_PKEY_verify(ctx, sig, sig_len,
test_digest, sizeof(test_digest)) == 0) {
fprintf(stderr, "Failed to verify signature; "
"signature may be invalid\n");
goto end;
}
ret = 1;
end:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
EVP_MD_free(md);
return ret;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
unsigned char *sig = NULL;
size_t sig_len = 0;
if (sign(libctx, &sig, &sig_len) == 0)
goto end;
if (verify(libctx, sig, sig_len) == 0)
goto end;
ret = EXIT_SUCCESS;
end:
OPENSSL_free(sig);
OSSL_LIB_CTX_free(libctx);
return ret;
}
| 5,742 | 27.014634 | 81 | c |
openssl | openssl-master/demos/signature/rsa_pss_hash.c | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include "rsa_pss.h"
/* The data to be signed. This will be hashed. */
static const char test_message[] =
"This is an example message to be signed.";
/* A property query used for selecting algorithm implementations. */
static const char *propq = NULL;
/*
* This function demonstrates RSA signing of an arbitrary-length message.
* Hashing is performed automatically. In this example, SHA-256 is used. If you
* have already hashed your message and simply want to sign the hash directly,
* see rsa_pss_direct.c.
*/
static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
EVP_MD_CTX *mctx = NULL;
OSSL_PARAM params[2], *p = params;
const unsigned char *ppriv_key = NULL;
*sig = NULL;
/* Load DER-encoded RSA private key. */
ppriv_key = rsa_priv_key;
pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,
sizeof(rsa_priv_key), libctx, propq);
if (pkey == NULL) {
fprintf(stderr, "Failed to load private key\n");
goto end;
}
/* Create MD context used for signing. */
mctx = EVP_MD_CTX_new();
if (mctx == NULL) {
fprintf(stderr, "Failed to create MD context\n");
goto end;
}
/* Initialize MD context for signing. */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,
OSSL_PKEY_RSA_PAD_MODE_PSS, 0);
*p = OSSL_PARAM_construct_end();
if (EVP_DigestSignInit_ex(mctx, NULL, "SHA256", libctx, propq,
pkey, params) == 0) {
fprintf(stderr, "Failed to initialize signing context\n");
goto end;
}
/*
* Feed data to be signed into the algorithm. This may
* be called multiple times.
*/
if (EVP_DigestSignUpdate(mctx, test_message, sizeof(test_message)) == 0) {
fprintf(stderr, "Failed to hash message into signing context\n");
goto end;
}
/* Determine signature length. */
if (EVP_DigestSignFinal(mctx, NULL, sig_len) == 0) {
fprintf(stderr, "Failed to get signature length\n");
goto end;
}
/* Allocate memory for signature. */
*sig = OPENSSL_malloc(*sig_len);
if (*sig == NULL) {
fprintf(stderr, "Failed to allocate memory for signature\n");
goto end;
}
/* Generate signature. */
if (EVP_DigestSignFinal(mctx, *sig, sig_len) == 0) {
fprintf(stderr, "Failed to sign\n");
goto end;
}
ret = 1;
end:
EVP_MD_CTX_free(mctx);
EVP_PKEY_free(pkey);
if (ret == 0)
OPENSSL_free(*sig);
return ret;
}
/*
* This function demonstrates verification of an RSA signature over an
* arbitrary-length message using the PSS signature scheme. Hashing is performed
* automatically.
*/
static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
EVP_MD_CTX *mctx = NULL;
OSSL_PARAM params[2], *p = params;
const unsigned char *ppub_key = NULL;
/* Load DER-encoded RSA public key. */
ppub_key = rsa_pub_key;
pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));
if (pkey == NULL) {
fprintf(stderr, "Failed to load public key\n");
goto end;
}
/* Create MD context used for verification. */
mctx = EVP_MD_CTX_new();
if (mctx == NULL) {
fprintf(stderr, "Failed to create MD context\n");
goto end;
}
/* Initialize MD context for verification. */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,
OSSL_PKEY_RSA_PAD_MODE_PSS, 0);
*p = OSSL_PARAM_construct_end();
if (EVP_DigestVerifyInit_ex(mctx, NULL, "SHA256", libctx, propq,
pkey, params) == 0) {
fprintf(stderr, "Failed to initialize signing context\n");
goto end;
}
/*
* Feed data to be signed into the algorithm. This may
* be called multiple times.
*/
if (EVP_DigestVerifyUpdate(mctx, test_message, sizeof(test_message)) == 0) {
fprintf(stderr, "Failed to hash message into signing context\n");
goto end;
}
/* Verify signature. */
if (EVP_DigestVerifyFinal(mctx, sig, sig_len) == 0) {
fprintf(stderr, "Failed to verify signature; "
"signature may be invalid\n");
goto end;
}
ret = 1;
end:
EVP_MD_CTX_free(mctx);
EVP_PKEY_free(pkey);
return ret;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
unsigned char *sig = NULL;
size_t sig_len = 0;
if (sign(libctx, &sig, &sig_len) == 0)
goto end;
if (verify(libctx, sig, sig_len) == 0)
goto end;
ret = EXIT_SUCCESS;
end:
OPENSSL_free(sig);
OSSL_LIB_CTX_free(libctx);
return ret;
}
| 5,475 | 27.821053 | 81 | c |
openssl | openssl-master/demos/smime/smdec.c | /*
* Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Simple S/MIME signing example */
#include <openssl/pem.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL, *tbio = NULL;
X509 *rcert = NULL;
EVP_PKEY *rkey = NULL;
PKCS7 *p7 = NULL;
int ret = EXIT_FAILURE;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Read in recipient certificate and private key */
tbio = BIO_new_file("signer.pem", "r");
if (!tbio)
goto err;
rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
BIO_reset(tbio);
rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
if (!rcert || !rkey)
goto err;
/* Open content being signed */
in = BIO_new_file("smencr.txt", "r");
if (!in)
goto err;
/* Sign content */
p7 = SMIME_read_PKCS7(in, NULL);
if (!p7)
goto err;
out = BIO_new_file("encrout.txt", "w");
if (!out)
goto err;
/* Decrypt S/MIME message */
if (!PKCS7_decrypt(p7, rkey, rcert, out, 0))
goto err;
ret = EXIT_SUCCESS;
err:
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Signing Data\n");
ERR_print_errors_fp(stderr);
}
PKCS7_free(p7);
X509_free(rcert);
EVP_PKEY_free(rkey);
BIO_free(in);
BIO_free(out);
BIO_free(tbio);
return ret;
}
| 1,693 | 20.443038 | 74 | c |
openssl | openssl-master/demos/smime/smenc.c | /*
* Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Simple S/MIME encrypt example */
#include <openssl/pem.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL, *tbio = NULL;
X509 *rcert = NULL;
STACK_OF(X509) *recips = NULL;
PKCS7 *p7 = NULL;
int ret = EXIT_FAILURE;
/*
* On OpenSSL 0.9.9 only:
* for streaming set PKCS7_STREAM
*/
int flags = PKCS7_STREAM;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Read in recipient certificate */
tbio = BIO_new_file("signer.pem", "r");
if (!tbio)
goto err;
rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
if (!rcert)
goto err;
/* Create recipient STACK and add recipient cert to it */
recips = sk_X509_new_null();
if (!recips || !sk_X509_push(recips, rcert))
goto err;
/*
* OSSL_STACK_OF_X509_free() will free up recipient STACK and its contents
* so set rcert to NULL so it isn't freed up twice.
*/
rcert = NULL;
/* Open content being encrypted */
in = BIO_new_file("encr.txt", "r");
if (!in)
goto err;
/* encrypt content */
p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
if (!p7)
goto err;
out = BIO_new_file("smencr.txt", "w");
if (!out)
goto err;
/* Write out S/MIME message */
if (!SMIME_write_PKCS7(out, p7, in, flags))
goto err;
ret = EXIT_SUCCESS;
err:
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Encrypting Data\n");
ERR_print_errors_fp(stderr);
}
PKCS7_free(p7);
X509_free(rcert);
OSSL_STACK_OF_X509_free(recips);
BIO_free(in);
BIO_free(out);
BIO_free(tbio);
return ret;
}
| 2,087 | 21.695652 | 78 | c |
openssl | openssl-master/demos/smime/smsign.c | /*
* Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Simple S/MIME signing example */
#include <openssl/pem.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL, *tbio = NULL;
X509 *scert = NULL;
EVP_PKEY *skey = NULL;
PKCS7 *p7 = NULL;
int ret = EXIT_FAILURE;
/*
* For simple S/MIME signing use PKCS7_DETACHED. On OpenSSL 0.9.9 only:
* for streaming detached set PKCS7_DETACHED|PKCS7_STREAM for streaming
* non-detached set PKCS7_STREAM
*/
int flags = PKCS7_DETACHED | PKCS7_STREAM;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Read in signer certificate and private key */
tbio = BIO_new_file("signer.pem", "r");
if (!tbio)
goto err;
scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
BIO_reset(tbio);
skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
if (!scert || !skey)
goto err;
/* Open content being signed */
in = BIO_new_file("sign.txt", "r");
if (!in)
goto err;
/* Sign content */
p7 = PKCS7_sign(scert, skey, NULL, in, flags);
if (!p7)
goto err;
out = BIO_new_file("smout.txt", "w");
if (!out)
goto err;
if (!(flags & PKCS7_STREAM))
BIO_reset(in);
/* Write out S/MIME message */
if (!SMIME_write_PKCS7(out, p7, in, flags))
goto err;
ret = EXIT_SUCCESS;
err:
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Signing Data\n");
ERR_print_errors_fp(stderr);
}
PKCS7_free(p7);
X509_free(scert);
EVP_PKEY_free(skey);
BIO_free(in);
BIO_free(out);
BIO_free(tbio);
return ret;
}
| 2,010 | 21.595506 | 75 | c |
openssl | openssl-master/demos/smime/smsign2.c | /*
* Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* S/MIME signing example: 2 signers. OpenSSL 0.9.9 only */
#include <openssl/pem.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL, *tbio = NULL;
X509 *scert = NULL, *scert2 = NULL;
EVP_PKEY *skey = NULL, *skey2 = NULL;
PKCS7 *p7 = NULL;
int ret = EXIT_FAILURE;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
tbio = BIO_new_file("signer.pem", "r");
if (!tbio)
goto err;
scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
BIO_reset(tbio);
skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
BIO_free(tbio);
tbio = BIO_new_file("signer2.pem", "r");
if (!tbio)
goto err;
scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
BIO_reset(tbio);
skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
if (!scert2 || !skey2)
goto err;
in = BIO_new_file("sign.txt", "r");
if (!in)
goto err;
p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);
if (!p7)
goto err;
/* Add each signer in turn */
if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
goto err;
if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
goto err;
out = BIO_new_file("smout.txt", "w");
if (!out)
goto err;
/* NB: content included and finalized by SMIME_write_PKCS7 */
if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
goto err;
ret = EXIT_SUCCESS;
err:
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Signing Data\n");
ERR_print_errors_fp(stderr);
}
PKCS7_free(p7);
X509_free(scert);
EVP_PKEY_free(skey);
X509_free(scert2);
EVP_PKEY_free(skey2);
BIO_free(in);
BIO_free(out);
BIO_free(tbio);
return ret;
}
| 2,180 | 21.484536 | 74 | c |
openssl | openssl-master/demos/smime/smver.c | /*
* Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Simple S/MIME verification example */
#include <openssl/pem.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
int main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
X509_STORE *st = NULL;
X509 *cacert = NULL;
PKCS7 *p7 = NULL;
int ret = EXIT_FAILURE;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Set up trusted CA certificate store */
st = X509_STORE_new();
if (st == NULL)
goto err;
/* Read in signer certificate and private key */
tbio = BIO_new_file("cacert.pem", "r");
if (tbio == NULL)
goto err;
cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
if (cacert == NULL)
goto err;
if (!X509_STORE_add_cert(st, cacert))
goto err;
/* Open content being signed */
in = BIO_new_file("smout.txt", "r");
if (in == NULL)
goto err;
/* Sign content */
p7 = SMIME_read_PKCS7(in, &cont);
if (p7 == NULL)
goto err;
/* File to output verified content to */
out = BIO_new_file("smver.txt", "w");
if (out == NULL)
goto err;
if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
fprintf(stderr, "Verification Failure\n");
goto err;
}
fprintf(stderr, "Verification Successful\n");
ret = EXIT_SUCCESS;
err:
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Verifying Data\n");
ERR_print_errors_fp(stderr);
}
X509_STORE_free(st);
PKCS7_free(p7);
X509_free(cacert);
BIO_free(in);
BIO_free(out);
BIO_free(tbio);
return ret;
}
| 1,956 | 21.494253 | 74 | c |
openssl | openssl-master/demos/sslecho/main.c | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <signal.h>
static const int server_port = 4433;
typedef unsigned char bool;
#define true 1
#define false 0
/*
* This flag won't be useful until both accept/read (TCP & SSL) methods
* can be called with a timeout. TBD.
*/
static volatile bool server_running = true;
int create_socket(bool isServer)
{
int s;
int optval = 1;
struct sockaddr_in addr;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("Unable to create socket");
exit(EXIT_FAILURE);
}
if (isServer) {
addr.sin_family = AF_INET;
addr.sin_port = htons(server_port);
addr.sin_addr.s_addr = INADDR_ANY;
/* Reuse the address; good for quick restarts */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))
< 0) {
perror("setsockopt(SO_REUSEADDR) failed");
exit(EXIT_FAILURE);
}
if (bind(s, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
perror("Unable to bind");
exit(EXIT_FAILURE);
}
if (listen(s, 1) < 0) {
perror("Unable to listen");
exit(EXIT_FAILURE);
}
}
return s;
}
SSL_CTX* create_context(bool isServer)
{
const SSL_METHOD *method;
SSL_CTX *ctx;
if (isServer)
method = TLS_server_method();
else
method = TLS_client_method();
ctx = SSL_CTX_new(method);
if (ctx == NULL) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
void configure_server_context(SSL_CTX *ctx)
{
/* Set the key and cert */
if (SSL_CTX_use_certificate_chain_file(ctx, "cert.pem") <= 0) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
}
void configure_client_context(SSL_CTX *ctx)
{
/*
* Configure the client to abort the handshake if certificate verification
* fails
*/
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/*
* In a real application you would probably just use the default system certificate trust store and call:
* SSL_CTX_set_default_verify_paths(ctx);
* In this demo though we are using a self-signed certificate, so the client must trust it directly.
*/
if (!SSL_CTX_load_verify_locations(ctx, "cert.pem", NULL)) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
}
void usage()
{
printf("Usage: sslecho s\n");
printf(" --or--\n");
printf(" sslecho c ip\n");
printf(" c=client, s=server, ip=dotted ip of server\n");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
bool isServer;
int result;
SSL_CTX *ssl_ctx = NULL;
SSL *ssl = NULL;
int server_skt = -1;
int client_skt = -1;
/* used by getline relying on realloc, can't be statically allocated */
char *txbuf = NULL;
size_t txcap = 0;
int txlen;
char rxbuf[128];
size_t rxcap = sizeof(rxbuf);
int rxlen;
char *rem_server_ip = NULL;
struct sockaddr_in addr;
unsigned int addr_len = sizeof(addr);
/* ignore SIGPIPE so that server can continue running when client pipe closes abruptly */
signal(SIGPIPE, SIG_IGN);
/* Splash */
printf("\nsslecho : Simple Echo Client/Server (OpenSSL 3.0.1-dev) : %s : %s\n\n", __DATE__,
__TIME__);
/* Need to know if client or server */
if (argc < 2) {
usage();
/* NOTREACHED */
}
isServer = (argv[1][0] == 's') ? true : false;
/* If client get remote server address (could be 127.0.0.1) */
if (!isServer) {
if (argc != 3) {
usage();
/* NOTREACHED */
}
rem_server_ip = argv[2];
}
/* Create context used by both client and server */
ssl_ctx = create_context(isServer);
/* If server */
if (isServer) {
printf("We are the server on port: %d\n\n", server_port);
/* Configure server context with appropriate key files */
configure_server_context(ssl_ctx);
/* Create server socket; will bind with server port and listen */
server_skt = create_socket(true);
/*
* Loop to accept clients.
* Need to implement timeouts on TCP & SSL connect/read functions
* before we can catch a CTRL-C and kill the server.
*/
while (server_running) {
/* Wait for TCP connection from client */
client_skt = accept(server_skt, (struct sockaddr*) &addr,
&addr_len);
if (client_skt < 0) {
perror("Unable to accept");
exit(EXIT_FAILURE);
}
printf("Client TCP connection accepted\n");
/* Create server SSL structure using newly accepted client socket */
ssl = SSL_new(ssl_ctx);
SSL_set_fd(ssl, client_skt);
/* Wait for SSL connection from the client */
if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(stderr);
server_running = false;
} else {
printf("Client SSL connection accepted\n\n");
/* Echo loop */
while (true) {
/* Get message from client; will fail if client closes connection */
if ((rxlen = SSL_read(ssl, rxbuf, rxcap)) <= 0) {
if (rxlen == 0) {
printf("Client closed connection\n");
} else {
printf("SSL_read returned %d\n", rxlen);
}
ERR_print_errors_fp(stderr);
break;
}
/* Insure null terminated input */
rxbuf[rxlen] = 0;
/* Look for kill switch */
if (strcmp(rxbuf, "kill\n") == 0) {
/* Terminate...with extreme prejudice */
printf("Server received 'kill' command\n");
server_running = false;
break;
}
/* Show received message */
printf("Received: %s", rxbuf);
/* Echo it back */
if (SSL_write(ssl, rxbuf, rxlen) <= 0) {
ERR_print_errors_fp(stderr);
}
}
}
if (server_running) {
/* Cleanup for next client */
SSL_shutdown(ssl);
SSL_free(ssl);
close(client_skt);
}
}
printf("Server exiting...\n");
}
/* Else client */
else {
printf("We are the client\n\n");
/* Configure client context so we verify the server correctly */
configure_client_context(ssl_ctx);
/* Create "bare" socket */
client_skt = create_socket(false);
/* Set up connect address */
addr.sin_family = AF_INET;
inet_pton(AF_INET, rem_server_ip, &addr.sin_addr.s_addr);
addr.sin_port = htons(server_port);
/* Do TCP connect with server */
if (connect(client_skt, (struct sockaddr*) &addr, sizeof(addr)) != 0) {
perror("Unable to TCP connect to server");
goto exit;
} else {
printf("TCP connection to server successful\n");
}
/* Create client SSL structure using dedicated client socket */
ssl = SSL_new(ssl_ctx);
SSL_set_fd(ssl, client_skt);
/* Set hostname for SNI */
SSL_set_tlsext_host_name(ssl, rem_server_ip);
/* Configure server hostname check */
SSL_set1_host(ssl, rem_server_ip);
/* Now do SSL connect with server */
if (SSL_connect(ssl) == 1) {
printf("SSL connection to server successful\n\n");
/* Loop to send input from keyboard */
while (true) {
/* Get a line of input */
txlen = getline(&txbuf, &txcap, stdin);
/* Exit loop on error */
if (txlen < 0 || txbuf == NULL) {
break;
}
/* Exit loop if just a carriage return */
if (txbuf[0] == '\n') {
break;
}
/* Send it to the server */
if ((result = SSL_write(ssl, txbuf, txlen)) <= 0) {
printf("Server closed connection\n");
ERR_print_errors_fp(stderr);
break;
}
/* Wait for the echo */
rxlen = SSL_read(ssl, rxbuf, rxcap);
if (rxlen <= 0) {
printf("Server closed connection\n");
ERR_print_errors_fp(stderr);
break;
} else {
/* Show it */
rxbuf[rxlen] = 0;
printf("Received: %s", rxbuf);
}
}
printf("Client exiting...\n");
} else {
printf("SSL connection to server failed\n\n");
ERR_print_errors_fp(stderr);
}
}
exit:
/* Close up */
if (ssl != NULL) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
SSL_CTX_free(ssl_ctx);
if (client_skt != -1)
close(client_skt);
if (server_skt != -1)
close(server_skt);
if (txbuf != NULL && txcap > 0)
free(txbuf);
printf("sslecho exiting\n");
return EXIT_SUCCESS;
}
| 10,304 | 28.358974 | 109 | c |
openssl | openssl-master/doc/designs/ddd/ddd-01-conn-blocking.c | #include <openssl/ssl.h>
/*
* Demo 1: Client — Managed Connection — Blocking
* ==============================================
*
* This is an example of (part of) an application which uses libssl in a simple,
* synchronous, blocking fashion. The functions show all interactions with
* libssl the application makes, and would hypothetically be linked into a
* larger application.
*/
/*
* The application is initializing and wants an SSL_CTX which it will use for
* some number of outgoing connections, which it creates in subsequent calls to
* new_conn. The application may also call this function multiple times to
* create multiple SSL_CTX.
*/
SSL_CTX *create_ssl_ctx(void)
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL)
return NULL;
/* Enable trust chain verification. */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* Load default root CA store. */
if (SSL_CTX_set_default_verify_paths(ctx) == 0) {
SSL_CTX_free(ctx);
return NULL;
}
return ctx;
}
/*
* The application wants to create a new outgoing connection using a given
* SSL_CTX.
*
* hostname is a string like "openssl.org:443" or "[::1]:443".
*/
BIO *new_conn(SSL_CTX *ctx, const char *hostname)
{
BIO *out;
SSL *ssl = NULL;
const char *bare_hostname;
out = BIO_new_ssl_connect(ctx);
if (out == NULL)
return NULL;
if (BIO_get_ssl(out, &ssl) == 0) {
BIO_free_all(out);
return NULL;
}
if (BIO_set_conn_hostname(out, hostname) == 0) {
BIO_free_all(out);
return NULL;
}
/* Returns the parsed hostname extracted from the hostname:port string. */
bare_hostname = BIO_get_conn_hostname(out);
if (bare_hostname == NULL) {
BIO_free_all(out);
return NULL;
}
/* Tell the SSL object the hostname to check certificates against. */
if (SSL_set1_host(ssl, bare_hostname) <= 0) {
BIO_free_all(out);
return NULL;
}
return out;
}
/*
* The application wants to send some block of data to the peer.
* This is a blocking call.
*/
int tx(BIO *bio, const void *buf, int buf_len)
{
return BIO_write(bio, buf, buf_len);
}
/*
* The application wants to receive some block of data from
* the peer. This is a blocking call.
*/
int rx(BIO *bio, void *buf, int buf_len)
{
return BIO_read(bio, buf, buf_len);
}
/*
* The application wants to close the connection and free bookkeeping
* structures.
*/
void teardown(BIO *bio)
{
BIO_free_all(bio);
}
/*
* The application is shutting down and wants to free a previously
* created SSL_CTX.
*/
void teardown_ctx(SSL_CTX *ctx)
{
SSL_CTX_free(ctx);
}
/*
* ============================================================================
* Example driver for the above code. This is just to demonstrate that the code
* works and is not intended to be representative of a real application.
*/
int main(int argc, char **argv)
{
const char msg[] = "GET / HTTP/1.0\r\nHost: www.openssl.org\r\n\r\n";
SSL_CTX *ctx = NULL;
BIO *b = NULL;
char buf[2048];
int l, res = 1;
ctx = create_ssl_ctx();
if (ctx == NULL) {
fprintf(stderr, "could not create context\n");
goto fail;
}
b = new_conn(ctx, "www.openssl.org:443");
if (b == NULL) {
fprintf(stderr, "could not create conn\n");
goto fail;
}
if (tx(b, msg, sizeof(msg)) < sizeof(msg)) {
fprintf(stderr, "tx error\n");
goto fail;
}
for (;;) {
l = rx(b, buf, sizeof(buf));
if (l <= 0)
break;
fwrite(buf, 1, l, stdout);
}
res = 0;
fail:
if (b != NULL)
teardown(b);
if (ctx != NULL)
teardown_ctx(ctx);
return res;
}
| 3,796 | 22.438272 | 80 | c |
openssl | openssl-master/doc/designs/ddd/ddd-02-conn-nonblocking.c | #include <sys/poll.h>
#include <openssl/ssl.h>
/*
* Demo 2: Client — Managed Connection — Asynchronous Nonblocking
* ==============================================================
*
* This is an example of (part of) an application which uses libssl in an
* asynchronous, nonblocking fashion. The functions show all interactions with
* libssl the application makes, and would hypothetically be linked into a
* larger application.
*
* In this example, libssl still makes syscalls directly using an fd, which is
* configured in nonblocking mode. As such, the application can still be
* abstracted from the details of what that fd is (is it a TCP socket? is it a
* UDP socket?); this code passes the application an fd and the application
* simply calls back into this code when poll()/etc. indicates it is ready.
*/
typedef struct app_conn_st {
SSL *ssl;
BIO *ssl_bio;
int rx_need_tx, tx_need_rx;
} APP_CONN;
/*
* The application is initializing and wants an SSL_CTX which it will use for
* some number of outgoing connections, which it creates in subsequent calls to
* new_conn. The application may also call this function multiple times to
* create multiple SSL_CTX.
*/
SSL_CTX *create_ssl_ctx(void)
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL)
return NULL;
/* Enable trust chain verification. */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* Load default root CA store. */
if (SSL_CTX_set_default_verify_paths(ctx) == 0) {
SSL_CTX_free(ctx);
return NULL;
}
return ctx;
}
/*
* The application wants to create a new outgoing connection using a given
* SSL_CTX.
*
* hostname is a string like "openssl.org:443" or "[::1]:443".
*/
APP_CONN *new_conn(SSL_CTX *ctx, const char *hostname)
{
APP_CONN *conn;
BIO *out, *buf;
SSL *ssl = NULL;
const char *bare_hostname;
conn = calloc(1, sizeof(APP_CONN));
if (conn == NULL)
return NULL;
out = BIO_new_ssl_connect(ctx);
if (out == NULL) {
free(conn);
return NULL;
}
if (BIO_get_ssl(out, &ssl) == 0) {
BIO_free_all(out);
free(conn);
return NULL;
}
buf = BIO_new(BIO_f_buffer());
if (buf == NULL) {
BIO_free_all(out);
free(conn);
return NULL;
}
BIO_push(out, buf);
if (BIO_set_conn_hostname(out, hostname) == 0) {
BIO_free_all(out);
free(conn);
return NULL;
}
/* Returns the parsed hostname extracted from the hostname:port string. */
bare_hostname = BIO_get_conn_hostname(out);
if (bare_hostname == NULL) {
BIO_free_all(out);
free(conn);
return NULL;
}
/* Tell the SSL object the hostname to check certificates against. */
if (SSL_set1_host(ssl, bare_hostname) <= 0) {
BIO_free_all(out);
free(conn);
return NULL;
}
/* Make the BIO nonblocking. */
BIO_set_nbio(out, 1);
conn->ssl_bio = out;
return conn;
}
/*
* Non-blocking transmission.
*
* Returns -1 on error. Returns -2 if the function would block (corresponds to
* EWOULDBLOCK).
*/
int tx(APP_CONN *conn, const void *buf, int buf_len)
{
int l;
conn->tx_need_rx = 0;
l = BIO_write(conn->ssl_bio, buf, buf_len);
if (l <= 0) {
if (BIO_should_retry(conn->ssl_bio)) {
conn->tx_need_rx = BIO_should_read(conn->ssl_bio);
return -2;
} else {
return -1;
}
}
return l;
}
/*
* Non-blocking reception.
*
* Returns -1 on error. Returns -2 if the function would block (corresponds to
* EWOULDBLOCK).
*/
int rx(APP_CONN *conn, void *buf, int buf_len)
{
int l;
conn->rx_need_tx = 0;
l = BIO_read(conn->ssl_bio, buf, buf_len);
if (l <= 0) {
if (BIO_should_retry(conn->ssl_bio)) {
conn->rx_need_tx = BIO_should_write(conn->ssl_bio);
return -2;
} else {
return -1;
}
}
return l;
}
/*
* The application wants to know a fd it can poll on to determine when the
* SSL state machine needs to be pumped.
*/
int get_conn_fd(APP_CONN *conn)
{
return BIO_get_fd(conn->ssl_bio, NULL);
}
/*
* These functions returns zero or more of:
*
* POLLIN: The SSL state machine is interested in socket readability events.
*
* POLLOUT: The SSL state machine is interested in socket writeability events.
*
* POLLERR: The SSL state machine is interested in socket error events.
*
* get_conn_pending_tx returns events which may cause SSL_write to make
* progress and get_conn_pending_rx returns events which may cause SSL_read
* to make progress.
*/
int get_conn_pending_tx(APP_CONN *conn)
{
return (conn->tx_need_rx ? POLLIN : 0) | POLLOUT | POLLERR;
}
int get_conn_pending_rx(APP_CONN *conn)
{
return (conn->rx_need_tx ? POLLOUT : 0) | POLLIN | POLLERR;
}
/*
* The application wants to close the connection and free bookkeeping
* structures.
*/
void teardown(APP_CONN *conn)
{
BIO_free_all(conn->ssl_bio);
free(conn);
}
/*
* The application is shutting down and wants to free a previously
* created SSL_CTX.
*/
void teardown_ctx(SSL_CTX *ctx)
{
SSL_CTX_free(ctx);
}
/*
* ============================================================================
* Example driver for the above code. This is just to demonstrate that the code
* works and is not intended to be representative of a real application.
*/
int main(int argc, char **argv)
{
const char tx_msg[] = "GET / HTTP/1.0\r\nHost: www.openssl.org\r\n\r\n";
const char *tx_p = tx_msg;
char rx_buf[2048];
int res = 1, l, tx_len = sizeof(tx_msg)-1;
int timeout = 2000 /* ms */;
APP_CONN *conn = NULL;
SSL_CTX *ctx;
ctx = create_ssl_ctx();
if (ctx == NULL) {
fprintf(stderr, "cannot create SSL context\n");
goto fail;
}
conn = new_conn(ctx, "www.openssl.org:443");
if (conn == NULL) {
fprintf(stderr, "cannot establish connection\n");
goto fail;
}
/* TX */
while (tx_len != 0) {
l = tx(conn, tx_p, tx_len);
if (l > 0) {
tx_p += l;
tx_len -= l;
} else if (l == -1) {
fprintf(stderr, "tx error\n");
} else if (l == -2) {
struct pollfd pfd = {0};
pfd.fd = get_conn_fd(conn);
pfd.events = get_conn_pending_tx(conn);
if (poll(&pfd, 1, timeout) == 0) {
fprintf(stderr, "tx timeout\n");
goto fail;
}
}
}
/* RX */
for (;;) {
l = rx(conn, rx_buf, sizeof(rx_buf));
if (l > 0) {
fwrite(rx_buf, 1, l, stdout);
} else if (l == -1) {
break;
} else if (l == -2) {
struct pollfd pfd = {0};
pfd.fd = get_conn_fd(conn);
pfd.events = get_conn_pending_rx(conn);
if (poll(&pfd, 1, timeout) == 0) {
fprintf(stderr, "rx timeout\n");
goto fail;
}
}
}
res = 0;
fail:
if (conn != NULL)
teardown(conn);
if (ctx != NULL)
teardown_ctx(ctx);
return res;
}
| 7,247 | 23.993103 | 82 | c |
openssl | openssl-master/doc/designs/ddd/ddd-03-fd-blocking.c | #include <openssl/ssl.h>
/*
* Demo 3: Client — Client Creates FD — Blocking
* =============================================
*
* This is an example of (part of) an application which uses libssl in a simple,
* synchronous, blocking fashion. The client is responsible for creating the
* socket and passing it to libssl. The functions show all interactions with
* libssl the application makes, and would hypothetically be linked into a
* larger application.
*/
/*
* The application is initializing and wants an SSL_CTX which it will use for
* some number of outgoing connections, which it creates in subsequent calls to
* new_conn. The application may also call this function multiple times to
* create multiple SSL_CTX.
*/
SSL_CTX *create_ssl_ctx(void)
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL)
return NULL;
/* Enable trust chain verification. */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* Load default root CA store. */
if (SSL_CTX_set_default_verify_paths(ctx) == 0) {
SSL_CTX_free(ctx);
return NULL;
}
return ctx;
}
/*
* The application wants to create a new outgoing connection using a given
* SSL_CTX.
*
* hostname is a string like "openssl.org" used for certificate validation.
*/
SSL *new_conn(SSL_CTX *ctx, int fd, const char *bare_hostname)
{
SSL *ssl;
ssl = SSL_new(ctx);
if (ssl == NULL)
return NULL;
SSL_set_connect_state(ssl); /* cannot fail */
if (SSL_set_fd(ssl, fd) <= 0) {
SSL_free(ssl);
return NULL;
}
if (SSL_set1_host(ssl, bare_hostname) <= 0) {
SSL_free(ssl);
return NULL;
}
if (SSL_set_tlsext_host_name(ssl, bare_hostname) <= 0) {
SSL_free(ssl);
return NULL;
}
return ssl;
}
/*
* The application wants to send some block of data to the peer.
* This is a blocking call.
*/
int tx(SSL *ssl, const void *buf, int buf_len)
{
return SSL_write(ssl, buf, buf_len);
}
/*
* The application wants to receive some block of data from
* the peer. This is a blocking call.
*/
int rx(SSL *ssl, void *buf, int buf_len)
{
return SSL_read(ssl, buf, buf_len);
}
/*
* The application wants to close the connection and free bookkeeping
* structures.
*/
void teardown(SSL *ssl)
{
SSL_free(ssl);
}
/*
* The application is shutting down and wants to free a previously
* created SSL_CTX.
*/
void teardown_ctx(SSL_CTX *ctx)
{
SSL_CTX_free(ctx);
}
/*
* ============================================================================
* Example driver for the above code. This is just to demonstrate that the code
* works and is not intended to be representative of a real application.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netdb.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int rc, fd = -1, l, res = 1;
const char msg[] = "GET / HTTP/1.0\r\nHost: www.openssl.org\r\n\r\n";
struct addrinfo hints = {0}, *result = NULL;
SSL *ssl = NULL;
SSL_CTX *ctx;
char buf[2048];
ctx = create_ssl_ctx();
if (ctx == NULL) {
fprintf(stderr, "cannot create context\n");
goto fail;
}
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
rc = getaddrinfo("www.openssl.org", "443", &hints, &result);
if (rc < 0) {
fprintf(stderr, "cannot resolve\n");
goto fail;
}
signal(SIGPIPE, SIG_IGN);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
fprintf(stderr, "cannot create socket\n");
goto fail;
}
rc = connect(fd, result->ai_addr, result->ai_addrlen);
if (rc < 0) {
fprintf(stderr, "cannot connect\n");
goto fail;
}
ssl = new_conn(ctx, fd, "www.openssl.org");
if (ssl == NULL) {
fprintf(stderr, "cannot create connection\n");
goto fail;
}
if (tx(ssl, msg, sizeof(msg)-1) < sizeof(msg)-1) {
fprintf(stderr, "tx error\n");
goto fail;
}
for (;;) {
l = rx(ssl, buf, sizeof(buf));
if (l <= 0)
break;
fwrite(buf, 1, l, stdout);
}
res = 0;
fail:
if (ssl != NULL)
teardown(ssl);
if (ctx != NULL)
teardown_ctx(ctx);
if (fd >= 0)
close(fd);
if (result != NULL)
freeaddrinfo(result);
return res;
}
| 4,452 | 22.560847 | 80 | c |
openssl | openssl-master/doc/designs/ddd/ddd-04-fd-nonblocking.c | #include <sys/poll.h>
#include <openssl/ssl.h>
/*
* Demo 4: Client — Client Creates FD — Nonblocking
* ================================================
*
* This is an example of (part of) an application which uses libssl in an
* asynchronous, nonblocking fashion. The client is responsible for creating the
* socket and passing it to libssl. The functions show all interactions with
* libssl the application makes, and wouldn hypothetically be linked into a
* larger application.
*/
typedef struct app_conn_st {
SSL *ssl;
int fd;
int rx_need_tx, tx_need_rx;
} APP_CONN;
/*
* The application is initializing and wants an SSL_CTX which it will use for
* some number of outgoing connections, which it creates in subsequent calls to
* new_conn. The application may also call this function multiple times to
* create multiple SSL_CTX.
*/
SSL_CTX *create_ssl_ctx(void)
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL)
return NULL;
/* Enable trust chain verification. */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* Load default root CA store. */
if (SSL_CTX_set_default_verify_paths(ctx) == 0) {
SSL_CTX_free(ctx);
return NULL;
}
return ctx;
}
/*
* The application wants to create a new outgoing connection using a given
* SSL_CTX.
*
* hostname is a string like "openssl.org" used for certificate validation.
*/
APP_CONN *new_conn(SSL_CTX *ctx, int fd, const char *bare_hostname)
{
APP_CONN *conn;
SSL *ssl;
conn = calloc(1, sizeof(APP_CONN));
if (conn == NULL)
return NULL;
ssl = conn->ssl = SSL_new(ctx);
if (ssl == NULL) {
free(conn);
return NULL;
}
SSL_set_connect_state(ssl); /* cannot fail */
if (SSL_set_fd(ssl, fd) <= 0) {
SSL_free(ssl);
free(conn);
return NULL;
}
if (SSL_set1_host(ssl, bare_hostname) <= 0) {
SSL_free(ssl);
free(conn);
return NULL;
}
if (SSL_set_tlsext_host_name(ssl, bare_hostname) <= 0) {
SSL_free(ssl);
free(conn);
return NULL;
}
conn->fd = fd;
return conn;
}
/*
* Non-blocking transmission.
*
* Returns -1 on error. Returns -2 if the function would block (corresponds to
* EWOULDBLOCK).
*/
int tx(APP_CONN *conn, const void *buf, int buf_len)
{
int rc, l;
conn->tx_need_rx = 0;
l = SSL_write(conn->ssl, buf, buf_len);
if (l <= 0) {
rc = SSL_get_error(conn->ssl, l);
switch (rc) {
case SSL_ERROR_WANT_READ:
conn->tx_need_rx = 1;
case SSL_ERROR_WANT_CONNECT:
case SSL_ERROR_WANT_WRITE:
return -2;
default:
return -1;
}
}
return l;
}
/*
* Non-blocking reception.
*
* Returns -1 on error. Returns -2 if the function would block (corresponds to
* EWOULDBLOCK).
*/
int rx(APP_CONN *conn, void *buf, int buf_len)
{
int rc, l;
conn->rx_need_tx = 0;
l = SSL_read(conn->ssl, buf, buf_len);
if (l <= 0) {
rc = SSL_get_error(conn->ssl, l);
switch (rc) {
case SSL_ERROR_WANT_WRITE:
conn->rx_need_tx = 1;
case SSL_ERROR_WANT_READ:
return -2;
default:
return -1;
}
}
return l;
}
/*
* The application wants to know a fd it can poll on to determine when the
* SSL state machine needs to be pumped.
*
* If the fd returned has:
*
* POLLIN: SSL_read *may* return data;
* if application does not want to read yet, it should call pump().
*
* POLLOUT: SSL_write *may* accept data
*
* POLLERR: An application should call pump() if it is not likely to call
* SSL_read or SSL_write soon.
*
*/
int get_conn_fd(APP_CONN *conn)
{
return conn->fd;
}
/*
* These functions returns zero or more of:
*
* POLLIN: The SSL state machine is interested in socket readability events.
*
* POLLOUT: The SSL state machine is interested in socket writeability events.
*
* POLLERR: The SSL state machine is interested in socket error events.
*
* get_conn_pending_tx returns events which may cause SSL_write to make
* progress and get_conn_pending_rx returns events which may cause SSL_read
* to make progress.
*/
int get_conn_pending_tx(APP_CONN *conn)
{
return (conn->tx_need_rx ? POLLIN : 0) | POLLOUT | POLLERR;
}
int get_conn_pending_rx(APP_CONN *conn)
{
return (conn->rx_need_tx ? POLLOUT : 0) | POLLIN | POLLERR;
}
/*
* The application wants to close the connection and free bookkeeping
* structures.
*/
void teardown(APP_CONN *conn)
{
SSL_shutdown(conn->ssl);
SSL_free(conn->ssl);
free(conn);
}
/*
* The application is shutting down and wants to free a previously
* created SSL_CTX.
*/
void teardown_ctx(SSL_CTX *ctx)
{
SSL_CTX_free(ctx);
}
/*
* ============================================================================
* Example driver for the above code. This is just to demonstrate that the code
* works and is not intended to be representative of a real application.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int rc, fd = -1, res = 1;
const char tx_msg[] = "GET / HTTP/1.0\r\nHost: www.openssl.org\r\n\r\n";
const char *tx_p = tx_msg;
char rx_buf[2048];
int l, tx_len = sizeof(tx_msg)-1;
int timeout = 2000 /* ms */;
APP_CONN *conn = NULL;
struct addrinfo hints = {0}, *result = NULL;
SSL_CTX *ctx;
ctx = create_ssl_ctx();
if (ctx == NULL) {
fprintf(stderr, "cannot create SSL context\n");
goto fail;
}
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
rc = getaddrinfo("www.openssl.org", "443", &hints, &result);
if (rc < 0) {
fprintf(stderr, "cannot resolve\n");
goto fail;
}
signal(SIGPIPE, SIG_IGN);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
fprintf(stderr, "cannot create socket\n");
goto fail;
}
rc = connect(fd, result->ai_addr, result->ai_addrlen);
if (rc < 0) {
fprintf(stderr, "cannot connect\n");
goto fail;
}
rc = fcntl(fd, F_SETFL, O_NONBLOCK);
if (rc < 0) {
fprintf(stderr, "cannot make socket nonblocking\n");
goto fail;
}
conn = new_conn(ctx, fd, "www.openssl.org");
if (conn == NULL) {
fprintf(stderr, "cannot establish connection\n");
goto fail;
}
/* TX */
while (tx_len != 0) {
l = tx(conn, tx_p, tx_len);
if (l > 0) {
tx_p += l;
tx_len -= l;
} else if (l == -1) {
fprintf(stderr, "tx error\n");
goto fail;
} else if (l == -2) {
struct pollfd pfd = {0};
pfd.fd = get_conn_fd(conn);
pfd.events = get_conn_pending_tx(conn);
if (poll(&pfd, 1, timeout) == 0) {
fprintf(stderr, "tx timeout\n");
goto fail;
}
}
}
/* RX */
for (;;) {
l = rx(conn, rx_buf, sizeof(rx_buf));
if (l > 0) {
fwrite(rx_buf, 1, l, stdout);
} else if (l == -1) {
break;
} else if (l == -2) {
struct pollfd pfd = {0};
pfd.fd = get_conn_fd(conn);
pfd.events = get_conn_pending_rx(conn);
if (poll(&pfd, 1, timeout) == 0) {
fprintf(stderr, "rx timeout\n");
goto fail;
}
}
}
res = 0;
fail:
if (conn != NULL)
teardown(conn);
if (ctx != NULL)
teardown_ctx(ctx);
if (result != NULL)
freeaddrinfo(result);
return res;
}
| 7,931 | 23.481481 | 82 | c |
openssl | openssl-master/doc/designs/ddd/ddd-05-mem-nonblocking.c | #include <sys/poll.h>
#include <openssl/ssl.h>
/*
* Demo 5: Client — Client Uses Memory BIO — Nonblocking
* =====================================================
*
* This is an example of (part of) an application which uses libssl in an
* asynchronous, nonblocking fashion. The application passes memory BIOs to
* OpenSSL, meaning that it controls both when data is read/written from an SSL
* object on the decrypted side but also when encrypted data from the network is
* shunted to/from OpenSSL. In this way OpenSSL is used as a pure state machine
* which does not make its own network I/O calls. OpenSSL never sees or creates
* any file descriptor for a network socket. The functions below show all
* interactions with libssl the application makes, and would hypothetically be
* linked into a larger application.
*/
typedef struct app_conn_st {
SSL *ssl;
BIO *ssl_bio, *net_bio;
int rx_need_tx, tx_need_rx;
} APP_CONN;
/*
* The application is initializing and wants an SSL_CTX which it will use for
* some number of outgoing connections, which it creates in subsequent calls to
* new_conn. The application may also call this function multiple times to
* create multiple SSL_CTX.
*/
SSL_CTX *create_ssl_ctx(void)
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL)
return NULL;
/* Enable trust chain verification. */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* Load default root CA store. */
if (SSL_CTX_set_default_verify_paths(ctx) == 0) {
SSL_CTX_free(ctx);
return NULL;
}
return ctx;
}
/*
* The application wants to create a new outgoing connection using a given
* SSL_CTX.
*
* hostname is a string like "openssl.org" used for certificate validation.
*/
APP_CONN *new_conn(SSL_CTX *ctx, const char *bare_hostname)
{
BIO *ssl_bio, *internal_bio, *net_bio;
APP_CONN *conn;
SSL *ssl;
conn = calloc(1, sizeof(APP_CONN));
if (conn == NULL)
return NULL;
ssl = conn->ssl = SSL_new(ctx);
if (ssl == NULL) {
free(conn);
return NULL;
}
SSL_set_connect_state(ssl); /* cannot fail */
if (BIO_new_bio_pair(&internal_bio, 0, &net_bio, 0) <= 0) {
SSL_free(ssl);
free(conn);
return NULL;
}
SSL_set_bio(ssl, internal_bio, internal_bio);
if (SSL_set1_host(ssl, bare_hostname) <= 0) {
SSL_free(ssl);
free(conn);
return NULL;
}
if (SSL_set_tlsext_host_name(ssl, bare_hostname) <= 0) {
SSL_free(ssl);
free(conn);
return NULL;
}
ssl_bio = BIO_new(BIO_f_ssl());
if (ssl_bio == NULL) {
SSL_free(ssl);
free(conn);
return NULL;
}
if (BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE) <= 0) {
SSL_free(ssl);
BIO_free(ssl_bio);
return NULL;
}
conn->ssl_bio = ssl_bio;
conn->net_bio = net_bio;
return conn;
}
/*
* Non-blocking transmission.
*
* Returns -1 on error. Returns -2 if the function would block (corresponds to
* EWOULDBLOCK).
*/
int tx(APP_CONN *conn, const void *buf, int buf_len)
{
int rc, l;
l = BIO_write(conn->ssl_bio, buf, buf_len);
if (l <= 0) {
rc = SSL_get_error(conn->ssl, l);
switch (rc) {
case SSL_ERROR_WANT_READ:
conn->tx_need_rx = 1;
case SSL_ERROR_WANT_CONNECT:
case SSL_ERROR_WANT_WRITE:
return -2;
default:
return -1;
}
} else {
conn->tx_need_rx = 0;
}
return l;
}
/*
* Non-blocking reception.
*
* Returns -1 on error. Returns -2 if the function would block (corresponds to
* EWOULDBLOCK).
*/
int rx(APP_CONN *conn, void *buf, int buf_len)
{
int rc, l;
l = BIO_read(conn->ssl_bio, buf, buf_len);
if (l <= 0) {
rc = SSL_get_error(conn->ssl, l);
switch (rc) {
case SSL_ERROR_WANT_WRITE:
conn->rx_need_tx = 1;
case SSL_ERROR_WANT_READ:
return -2;
default:
return -1;
}
} else {
conn->rx_need_tx = 0;
}
return l;
}
/*
* Called to get data which has been enqueued for transmission to the network
* by OpenSSL.
*/
int read_net_tx(APP_CONN *conn, void *buf, int buf_len)
{
return BIO_read(conn->net_bio, buf, buf_len);
}
/*
* Called to feed data which has been received from the network to OpenSSL.
*/
int write_net_rx(APP_CONN *conn, const void *buf, int buf_len)
{
return BIO_write(conn->net_bio, buf, buf_len);
}
/*
* Determine how much data can be written to the network RX BIO.
*/
size_t net_rx_space(APP_CONN *conn)
{
return BIO_ctrl_get_write_guarantee(conn->net_bio);
}
/*
* Determine how much data is currently queued for transmission in the network
* TX BIO.
*/
size_t net_tx_avail(APP_CONN *conn)
{
return BIO_ctrl_pending(conn->net_bio);
}
/*
* These functions returns zero or more of:
*
* POLLIN: The SSL state machine is interested in socket readability events.
*
* POLLOUT: The SSL state machine is interested in socket writeability events.
*
* POLLERR: The SSL state machine is interested in socket error events.
*
* get_conn_pending_tx returns events which may cause SSL_write to make
* progress and get_conn_pending_rx returns events which may cause SSL_read
* to make progress.
*/
int get_conn_pending_tx(APP_CONN *conn)
{
return (conn->tx_need_rx ? POLLIN : 0) | POLLOUT | POLLERR;
}
int get_conn_pending_rx(APP_CONN *conn)
{
return (conn->rx_need_tx ? POLLOUT : 0) | POLLIN | POLLERR;
}
/*
* The application wants to close the connection and free bookkeeping
* structures.
*/
void teardown(APP_CONN *conn)
{
BIO_free_all(conn->ssl_bio);
BIO_free_all(conn->net_bio);
free(conn);
}
/*
* The application is shutting down and wants to free a previously
* created SSL_CTX.
*/
void teardown_ctx(SSL_CTX *ctx)
{
SSL_CTX_free(ctx);
}
/*
* ============================================================================
* Example driver for the above code. This is just to demonstrate that the code
* works and is not intended to be representative of a real application.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
static int pump(APP_CONN *conn, int fd, int events, int timeout)
{
int l, l2;
char buf[2048];
size_t wspace;
struct pollfd pfd = {0};
pfd.fd = fd;
pfd.events = (events & (POLLIN | POLLERR));
if (net_rx_space(conn) == 0)
pfd.events &= ~POLLIN;
if (net_tx_avail(conn) > 0)
pfd.events |= POLLOUT;
if ((pfd.events & (POLLIN|POLLOUT)) == 0)
return 1;
if (poll(&pfd, 1, timeout) == 0)
return -1;
if (pfd.revents & POLLIN) {
while ((wspace = net_rx_space(conn)) > 0) {
l = read(fd, buf, wspace > sizeof(buf) ? sizeof(buf) : wspace);
if (l <= 0) {
switch (errno) {
case EAGAIN:
goto stop;
default:
if (l == 0) /* EOF */
goto stop;
fprintf(stderr, "error on read: %d\n", errno);
return -1;
}
break;
}
l2 = write_net_rx(conn, buf, l);
if (l2 < l)
fprintf(stderr, "short write %d %d\n", l2, l);
} stop:;
}
if (pfd.revents & POLLOUT) {
for (;;) {
l = read_net_tx(conn, buf, sizeof(buf));
if (l <= 0)
break;
l2 = write(fd, buf, l);
if (l2 < l)
fprintf(stderr, "short read %d %d\n", l2, l);
}
}
return 1;
}
int main(int argc, char **argv)
{
int rc, fd = -1, res = 1;
const char tx_msg[] = "GET / HTTP/1.0\r\nHost: www.openssl.org\r\n\r\n";
const char *tx_p = tx_msg;
char rx_buf[2048];
int l, tx_len = sizeof(tx_msg)-1;
int timeout = 2000 /* ms */;
APP_CONN *conn = NULL;
struct addrinfo hints = {0}, *result = NULL;
SSL_CTX *ctx;
ctx = create_ssl_ctx();
if (ctx == NULL) {
fprintf(stderr, "cannot create SSL context\n");
goto fail;
}
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
rc = getaddrinfo("www.openssl.org", "443", &hints, &result);
if (rc < 0) {
fprintf(stderr, "cannot resolve\n");
goto fail;
}
signal(SIGPIPE, SIG_IGN);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
fprintf(stderr, "cannot create socket\n");
goto fail;
}
rc = connect(fd, result->ai_addr, result->ai_addrlen);
if (rc < 0) {
fprintf(stderr, "cannot connect\n");
goto fail;
}
rc = fcntl(fd, F_SETFL, O_NONBLOCK);
if (rc < 0) {
fprintf(stderr, "cannot make socket nonblocking\n");
goto fail;
}
conn = new_conn(ctx, "www.openssl.org");
if (conn == NULL) {
fprintf(stderr, "cannot establish connection\n");
goto fail;
}
/* TX */
while (tx_len != 0) {
l = tx(conn, tx_p, tx_len);
if (l > 0) {
tx_p += l;
tx_len -= l;
} else if (l == -1) {
fprintf(stderr, "tx error\n");
} else if (l == -2) {
if (pump(conn, fd, get_conn_pending_tx(conn), timeout) != 1) {
fprintf(stderr, "pump error\n");
goto fail;
}
}
}
/* RX */
for (;;) {
l = rx(conn, rx_buf, sizeof(rx_buf));
if (l > 0) {
fwrite(rx_buf, 1, l, stdout);
} else if (l == -1) {
break;
} else if (l == -2) {
if (pump(conn, fd, get_conn_pending_rx(conn), timeout) != 1) {
fprintf(stderr, "pump error\n");
goto fail;
}
}
}
res = 0;
fail:
if (conn != NULL)
teardown(conn);
if (ctx != NULL)
teardown_ctx(ctx);
if (result != NULL)
freeaddrinfo(result);
return res;
}
| 10,294 | 24.171149 | 82 | c |
openssl | openssl-master/engines/e_afalg.h | /*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_ENGINES_E_AFALG_H
# define OSSL_ENGINES_E_AFALG_H
# if defined(__GNUC__) && __GNUC__ >= 4 && \
(!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
# pragma GCC diagnostic ignored "-Wvariadic-macros"
# endif
# ifdef ALG_DEBUG
# define ALG_DGB(x, ...) fprintf(stderr, "ALG_DBG: " x, __VA_ARGS__)
# define ALG_INFO(x, ...) fprintf(stderr, "ALG_INFO: " x, __VA_ARGS__)
# define ALG_WARN(x, ...) fprintf(stderr, "ALG_WARN: " x, __VA_ARGS__)
# else
# define ALG_DGB(x, ...)
# define ALG_INFO(x, ...)
# define ALG_WARN(x, ...)
# endif
# define ALG_ERR(x, ...) fprintf(stderr, "ALG_ERR: " x, __VA_ARGS__)
# define ALG_PERR(x, ...) \
do { \
fprintf(stderr, "ALG_PERR: " x, __VA_ARGS__); \
perror(NULL); \
} while(0)
# define ALG_PWARN(x, ...) \
do { \
fprintf(stderr, "ALG_PERR: " x, __VA_ARGS__); \
perror(NULL); \
} while(0)
# ifndef AES_BLOCK_SIZE
# define AES_BLOCK_SIZE 16
# endif
# define AES_KEY_SIZE_128 16
# define AES_KEY_SIZE_192 24
# define AES_KEY_SIZE_256 32
# define AES_IV_LEN 16
# define MAX_INFLIGHTS 1
typedef enum {
MODE_UNINIT = 0,
MODE_SYNC,
MODE_ASYNC
} op_mode;
enum {
AES_CBC_128 = 0,
AES_CBC_192,
AES_CBC_256
};
struct cbc_cipher_handles {
int key_size;
EVP_CIPHER *_hidden;
};
typedef struct cbc_cipher_handles cbc_handles;
struct afalg_aio_st {
int efd;
op_mode mode;
aio_context_t aio_ctx;
struct io_event events[MAX_INFLIGHTS];
struct iocb cbt[MAX_INFLIGHTS];
};
typedef struct afalg_aio_st afalg_aio;
/*
* MAGIC Number to identify correct initialisation
* of afalg_ctx.
*/
# define MAGIC_INIT_NUM 0x1890671
struct afalg_ctx_st {
int init_done;
int sfd;
int bfd;
# ifdef ALG_ZERO_COPY
int zc_pipe[2];
# endif
afalg_aio aio;
};
typedef struct afalg_ctx_st afalg_ctx;
#endif
| 2,299 | 22.958333 | 74 | h |
openssl | openssl-master/engines/e_afalg_err.c | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <openssl/err.h>
#include "e_afalg_err.h"
#ifndef OPENSSL_NO_ERR
static ERR_STRING_DATA AFALG_str_reasons[] = {
{ERR_PACK(0, 0, AFALG_R_EVENTFD_FAILED), "eventfd failed"},
{ERR_PACK(0, 0, AFALG_R_FAILED_TO_GET_PLATFORM_INFO),
"failed to get platform info"},
{ERR_PACK(0, 0, AFALG_R_INIT_FAILED), "init failed"},
{ERR_PACK(0, 0, AFALG_R_IO_SETUP_FAILED), "io setup failed"},
{ERR_PACK(0, 0, AFALG_R_KERNEL_DOES_NOT_SUPPORT_AFALG),
"kernel does not support afalg"},
{ERR_PACK(0, 0, AFALG_R_KERNEL_DOES_NOT_SUPPORT_ASYNC_AFALG),
"kernel does not support async afalg"},
{ERR_PACK(0, 0, AFALG_R_KERNEL_OP_FAILED), "kernel op failed"},
{ERR_PACK(0, 0, AFALG_R_MEM_ALLOC_FAILED), "mem alloc failed"},
{ERR_PACK(0, 0, AFALG_R_SOCKET_ACCEPT_FAILED), "socket accept failed"},
{ERR_PACK(0, 0, AFALG_R_SOCKET_BIND_FAILED), "socket bind failed"},
{ERR_PACK(0, 0, AFALG_R_SOCKET_CREATE_FAILED), "socket create failed"},
{ERR_PACK(0, 0, AFALG_R_SOCKET_OPERATION_FAILED),
"socket operation failed"},
{ERR_PACK(0, 0, AFALG_R_SOCKET_SET_KEY_FAILED), "socket set key failed"},
{0, NULL}
};
#endif
static int lib_code = 0;
static int error_loaded = 0;
static int ERR_load_AFALG_strings(void)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
if (!error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_load_strings(lib_code, AFALG_str_reasons);
#endif
error_loaded = 1;
}
return 1;
}
static void ERR_unload_AFALG_strings(void)
{
if (error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_unload_strings(lib_code, AFALG_str_reasons);
#endif
error_loaded = 0;
}
}
static void ERR_AFALG_error(int function, int reason, const char *file, int line)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
ERR_raise(lib_code, reason);
ERR_set_debug(file, line, NULL);
}
| 2,289 | 30.369863 | 81 | c |
openssl | openssl-master/engines/e_afalg_err.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_E_AFALG_ERR_H
# define OSSL_E_AFALG_ERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# define AFALGerr(f, r) ERR_AFALG_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
/*
* AFALG reason codes.
*/
# define AFALG_R_EVENTFD_FAILED 108
# define AFALG_R_FAILED_TO_GET_PLATFORM_INFO 111
# define AFALG_R_INIT_FAILED 100
# define AFALG_R_IO_SETUP_FAILED 105
# define AFALG_R_KERNEL_DOES_NOT_SUPPORT_AFALG 101
# define AFALG_R_KERNEL_DOES_NOT_SUPPORT_ASYNC_AFALG 107
# define AFALG_R_KERNEL_OP_FAILED 112
# define AFALG_R_MEM_ALLOC_FAILED 102
# define AFALG_R_SOCKET_ACCEPT_FAILED 110
# define AFALG_R_SOCKET_BIND_FAILED 103
# define AFALG_R_SOCKET_CREATE_FAILED 109
# define AFALG_R_SOCKET_OPERATION_FAILED 104
# define AFALG_R_SOCKET_SET_KEY_FAILED 106
#endif
| 1,438 | 34.975 | 75 | h |
openssl | openssl-master/engines/e_capi_err.c | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <openssl/err.h>
#include "e_capi_err.h"
#ifndef OPENSSL_NO_ERR
static ERR_STRING_DATA CAPI_str_reasons[] = {
{ERR_PACK(0, 0, CAPI_R_CANT_CREATE_HASH_OBJECT), "cant create hash object"},
{ERR_PACK(0, 0, CAPI_R_CANT_FIND_CAPI_CONTEXT), "cant find capi context"},
{ERR_PACK(0, 0, CAPI_R_CANT_GET_KEY), "cant get key"},
{ERR_PACK(0, 0, CAPI_R_CANT_SET_HASH_VALUE), "cant set hash value"},
{ERR_PACK(0, 0, CAPI_R_CRYPTACQUIRECONTEXT_ERROR),
"cryptacquirecontext error"},
{ERR_PACK(0, 0, CAPI_R_CRYPTENUMPROVIDERS_ERROR),
"cryptenumproviders error"},
{ERR_PACK(0, 0, CAPI_R_DECRYPT_ERROR), "decrypt error"},
{ERR_PACK(0, 0, CAPI_R_ENGINE_NOT_INITIALIZED), "engine not initialized"},
{ERR_PACK(0, 0, CAPI_R_ENUMCONTAINERS_ERROR), "enumcontainers error"},
{ERR_PACK(0, 0, CAPI_R_ERROR_ADDING_CERT), "error adding cert"},
{ERR_PACK(0, 0, CAPI_R_ERROR_CREATING_STORE), "error creating store"},
{ERR_PACK(0, 0, CAPI_R_ERROR_GETTING_FRIENDLY_NAME),
"error getting friendly name"},
{ERR_PACK(0, 0, CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO),
"error getting key provider info"},
{ERR_PACK(0, 0, CAPI_R_ERROR_OPENING_STORE), "error opening store"},
{ERR_PACK(0, 0, CAPI_R_ERROR_SIGNING_HASH), "error signing hash"},
{ERR_PACK(0, 0, CAPI_R_FILE_OPEN_ERROR), "file open error"},
{ERR_PACK(0, 0, CAPI_R_FUNCTION_NOT_SUPPORTED), "function not supported"},
{ERR_PACK(0, 0, CAPI_R_GETUSERKEY_ERROR), "getuserkey error"},
{ERR_PACK(0, 0, CAPI_R_INVALID_DIGEST_LENGTH), "invalid digest length"},
{ERR_PACK(0, 0, CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER),
"invalid dsa public key blob magic number"},
{ERR_PACK(0, 0, CAPI_R_INVALID_LOOKUP_METHOD), "invalid lookup method"},
{ERR_PACK(0, 0, CAPI_R_INVALID_PUBLIC_KEY_BLOB), "invalid public key blob"},
{ERR_PACK(0, 0, CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER),
"invalid rsa public key blob magic number"},
{ERR_PACK(0, 0, CAPI_R_PUBKEY_EXPORT_ERROR), "pubkey export error"},
{ERR_PACK(0, 0, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR),
"pubkey export length error"},
{ERR_PACK(0, 0, CAPI_R_UNKNOWN_COMMAND), "unknown command"},
{ERR_PACK(0, 0, CAPI_R_UNSUPPORTED_ALGORITHM_NID),
"unsupported algorithm nid"},
{ERR_PACK(0, 0, CAPI_R_UNSUPPORTED_PADDING), "unsupported padding"},
{ERR_PACK(0, 0, CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM),
"unsupported public key algorithm"},
{ERR_PACK(0, 0, CAPI_R_WIN32_ERROR), "win32 error"},
{0, NULL}
};
#endif
static int lib_code = 0;
static int error_loaded = 0;
static int ERR_load_CAPI_strings(void)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
if (!error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_load_strings(lib_code, CAPI_str_reasons);
#endif
error_loaded = 1;
}
return 1;
}
static void ERR_unload_CAPI_strings(void)
{
if (error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_unload_strings(lib_code, CAPI_str_reasons);
#endif
error_loaded = 0;
}
}
static void ERR_CAPI_error(int function, int reason, const char *file, int line)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
ERR_raise(lib_code, reason);
ERR_set_debug(file, line, NULL);
}
static int ERR_CAPI_lib(void)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
return lib_code;
}
| 3,796 | 36.22549 | 80 | c |
openssl | openssl-master/engines/e_capi_err.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_E_CAPI_ERR_H
# define OSSL_E_CAPI_ERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# define CAPIerr(f, r) ERR_CAPI_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
# define ERR_R_CAPI_LIB ERR_CAPI_lib()
/*
* CAPI reason codes.
*/
# define CAPI_R_CANT_CREATE_HASH_OBJECT 100
# define CAPI_R_CANT_FIND_CAPI_CONTEXT 101
# define CAPI_R_CANT_GET_KEY 102
# define CAPI_R_CANT_SET_HASH_VALUE 103
# define CAPI_R_CRYPTACQUIRECONTEXT_ERROR 104
# define CAPI_R_CRYPTENUMPROVIDERS_ERROR 105
# define CAPI_R_DECRYPT_ERROR 106
# define CAPI_R_ENGINE_NOT_INITIALIZED 107
# define CAPI_R_ENUMCONTAINERS_ERROR 108
# define CAPI_R_ERROR_ADDING_CERT 109
# define CAPI_R_ERROR_CREATING_STORE 110
# define CAPI_R_ERROR_GETTING_FRIENDLY_NAME 111
# define CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO 112
# define CAPI_R_ERROR_OPENING_STORE 113
# define CAPI_R_ERROR_SIGNING_HASH 114
# define CAPI_R_FILE_OPEN_ERROR 115
# define CAPI_R_FUNCTION_NOT_SUPPORTED 116
# define CAPI_R_GETUSERKEY_ERROR 117
# define CAPI_R_INVALID_DIGEST_LENGTH 118
# define CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER 119
# define CAPI_R_INVALID_LOOKUP_METHOD 120
# define CAPI_R_INVALID_PUBLIC_KEY_BLOB 121
# define CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER 122
# define CAPI_R_PUBKEY_EXPORT_ERROR 123
# define CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR 124
# define CAPI_R_UNKNOWN_COMMAND 125
# define CAPI_R_UNSUPPORTED_ALGORITHM_NID 126
# define CAPI_R_UNSUPPORTED_PADDING 127
# define CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM 128
# define CAPI_R_WIN32_ERROR 129
#endif
| 2,526 | 42.568966 | 74 | h |
openssl | openssl-master/engines/e_dasync_err.c | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <openssl/err.h>
#include "e_dasync_err.h"
#ifndef OPENSSL_NO_ERR
static ERR_STRING_DATA DASYNC_str_reasons[] = {
{ERR_PACK(0, 0, DASYNC_R_INIT_FAILED), "init failed"},
{0, NULL}
};
#endif
static int lib_code = 0;
static int error_loaded = 0;
static int ERR_load_DASYNC_strings(void)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
if (!error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_load_strings(lib_code, DASYNC_str_reasons);
#endif
error_loaded = 1;
}
return 1;
}
static void ERR_unload_DASYNC_strings(void)
{
if (error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_unload_strings(lib_code, DASYNC_str_reasons);
#endif
error_loaded = 0;
}
}
static void ERR_DASYNC_error(int function, int reason, const char *file, int line)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
ERR_raise(lib_code, reason);
ERR_set_debug(file, line, NULL);
}
| 1,341 | 22.54386 | 82 | c |
openssl | openssl-master/engines/e_dasync_err.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_E_DASYNC_ERR_H
# define OSSL_E_DASYNC_ERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# define DASYNCerr(f, r) ERR_DASYNC_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
/*
* DASYNC reason codes.
*/
# define DASYNC_R_INIT_FAILED 100
#endif
| 699 | 24 | 77 | h |
openssl | openssl-master/engines/e_loader_attic_err.c | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <openssl/err.h>
#include "e_loader_attic_err.h"
#ifndef OPENSSL_NO_ERR
static ERR_STRING_DATA ATTIC_str_reasons[] = {
{ERR_PACK(0, 0, ATTIC_R_AMBIGUOUS_CONTENT_TYPE), "ambiguous content type"},
{ERR_PACK(0, 0, ATTIC_R_BAD_PASSWORD_READ), "bad password read"},
{ERR_PACK(0, 0, ATTIC_R_ERROR_VERIFYING_PKCS12_MAC),
"error verifying pkcs12 mac"},
{ERR_PACK(0, 0, ATTIC_R_INIT_FAILED), "init failed"},
{ERR_PACK(0, 0, ATTIC_R_PASSPHRASE_CALLBACK_ERROR),
"passphrase callback error"},
{ERR_PACK(0, 0, ATTIC_R_PATH_MUST_BE_ABSOLUTE), "path must be absolute"},
{ERR_PACK(0, 0, ATTIC_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES),
"search only supported for directories"},
{ERR_PACK(0, 0, ATTIC_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED),
"ui process interrupted or cancelled"},
{ERR_PACK(0, 0, ATTIC_R_UNSUPPORTED_CONTENT_TYPE),
"unsupported content type"},
{ERR_PACK(0, 0, ATTIC_R_UNSUPPORTED_SEARCH_TYPE),
"unsupported search type"},
{ERR_PACK(0, 0, ATTIC_R_URI_AUTHORITY_UNSUPPORTED),
"uri authority unsupported"},
{0, NULL}
};
#endif
static int lib_code = 0;
static int error_loaded = 0;
static int ERR_load_ATTIC_strings(void)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
if (!error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_load_strings(lib_code, ATTIC_str_reasons);
#endif
error_loaded = 1;
}
return 1;
}
static void ERR_unload_ATTIC_strings(void)
{
if (error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_unload_strings(lib_code, ATTIC_str_reasons);
#endif
error_loaded = 0;
}
}
static void ERR_ATTIC_error(int function, int reason, const char *file, int line)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
ERR_raise(lib_code, reason);
ERR_set_debug(file, line, NULL);
}
| 2,238 | 29.256757 | 81 | c |
openssl | openssl-master/engines/e_loader_attic_err.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_E_LOADER_ATTIC_ERR_H
# define OSSL_E_LOADER_ATTIC_ERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# define ATTICerr(f, r) ERR_ATTIC_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
/*
* ATTIC reason codes.
*/
# define ATTIC_R_AMBIGUOUS_CONTENT_TYPE 100
# define ATTIC_R_BAD_PASSWORD_READ 101
# define ATTIC_R_ERROR_VERIFYING_PKCS12_MAC 102
# define ATTIC_R_INIT_FAILED 103
# define ATTIC_R_PASSPHRASE_CALLBACK_ERROR 104
# define ATTIC_R_PATH_MUST_BE_ABSOLUTE 105
# define ATTIC_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES 106
# define ATTIC_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED 107
# define ATTIC_R_UNSUPPORTED_CONTENT_TYPE 108
# define ATTIC_R_UNSUPPORTED_SEARCH_TYPE 109
# define ATTIC_R_URI_AUTHORITY_UNSUPPORTED 110
#endif
| 1,328 | 33.973684 | 75 | h |
openssl | openssl-master/engines/e_ossltest_err.c | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <openssl/err.h>
#include "e_ossltest_err.h"
#ifndef OPENSSL_NO_ERR
static ERR_STRING_DATA OSSLTEST_str_reasons[] = {
{ERR_PACK(0, 0, OSSLTEST_R_INIT_FAILED), "init failed"},
{0, NULL}
};
#endif
static int lib_code = 0;
static int error_loaded = 0;
static int ERR_load_OSSLTEST_strings(void)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
if (!error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_load_strings(lib_code, OSSLTEST_str_reasons);
#endif
error_loaded = 1;
}
return 1;
}
static void ERR_unload_OSSLTEST_strings(void)
{
if (error_loaded) {
#ifndef OPENSSL_NO_ERR
ERR_unload_strings(lib_code, OSSLTEST_str_reasons);
#endif
error_loaded = 0;
}
}
static void ERR_OSSLTEST_error(int function, int reason, const char *file, int line)
{
if (lib_code == 0)
lib_code = ERR_get_next_error_library();
ERR_raise(lib_code, reason);
ERR_set_debug(file, line, NULL);
}
| 1,357 | 22.824561 | 84 | c |
openssl | openssl-master/engines/e_ossltest_err.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_E_OSSLTEST_ERR_H
# define OSSL_E_OSSLTEST_ERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# define OSSLTESTerr(f, r) ERR_OSSLTEST_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
/*
* OSSLTEST reason codes.
*/
# define OSSLTEST_R_INIT_FAILED 100
#endif
| 709 | 24.357143 | 81 | h |
openssl | openssl-master/engines/e_padlock.c | /*
* Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* This file uses the low level AES and engine functions (which are deprecated
* for non-internal use) in order to implement the padlock engine AES ciphers.
*/
#define OPENSSL_SUPPRESS_DEPRECATED
#include <stdio.h>
#include <string.h>
#include <openssl/opensslconf.h>
#include <openssl/crypto.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <openssl/modes.h>
#ifndef OPENSSL_NO_PADLOCKENG
/*
* VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it
* doesn't exist elsewhere, but it even can't be compiled on other platforms!
*/
# undef COMPILE_PADLOCKENG
# if defined(PADLOCK_ASM)
# define COMPILE_PADLOCKENG
# ifdef OPENSSL_NO_DYNAMIC_ENGINE
static ENGINE *ENGINE_padlock(void);
# endif
# endif
# ifdef OPENSSL_NO_DYNAMIC_ENGINE
void engine_load_padlock_int(void);
void engine_load_padlock_int(void)
{
/* On non-x86 CPUs it just returns. */
# ifdef COMPILE_PADLOCKENG
ENGINE *toadd = ENGINE_padlock();
if (!toadd)
return;
ERR_set_mark();
ENGINE_add(toadd);
/*
* If the "add" worked, it gets a structural reference. So either way, we
* release our just-created reference.
*/
ENGINE_free(toadd);
/*
* If the "add" didn't work, it was probably a conflict because it was
* already added (eg. someone calling ENGINE_load_blah then calling
* ENGINE_load_builtin_engines() perhaps).
*/
ERR_pop_to_mark();
# endif
}
# endif
# ifdef COMPILE_PADLOCKENG
/* Function for ENGINE detection and control */
static int padlock_available(void);
static int padlock_init(ENGINE *e);
/* RNG Stuff */
static RAND_METHOD padlock_rand;
/* Cipher Stuff */
static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
const int **nids, int nid);
/* Engine names */
static const char *padlock_id = "padlock";
static char padlock_name[100];
/* Available features */
static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
static int padlock_use_rng = 0; /* Random Number Generator */
/* ===== Engine "management" functions ===== */
/* Prepare the ENGINE structure for registration */
static int padlock_bind_helper(ENGINE *e)
{
/* Check available features */
padlock_available();
/*
* RNG is currently disabled for reasons discussed in commentary just
* before padlock_rand_bytes function.
*/
padlock_use_rng = 0;
/* Generate a nice engine name with available features */
BIO_snprintf(padlock_name, sizeof(padlock_name),
"VIA PadLock (%s, %s)",
padlock_use_rng ? "RNG" : "no-RNG",
padlock_use_ace ? "ACE" : "no-ACE");
/* Register everything or return with an error */
if (!ENGINE_set_id(e, padlock_id) ||
!ENGINE_set_name(e, padlock_name) ||
!ENGINE_set_init_function(e, padlock_init) ||
(padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
(padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
return 0;
}
/* Everything looks good */
return 1;
}
# ifdef OPENSSL_NO_DYNAMIC_ENGINE
/* Constructor */
static ENGINE *ENGINE_padlock(void)
{
ENGINE *eng = ENGINE_new();
if (eng == NULL) {
return NULL;
}
if (!padlock_bind_helper(eng)) {
ENGINE_free(eng);
return NULL;
}
return eng;
}
# endif
/* Check availability of the engine */
static int padlock_init(ENGINE *e)
{
return (padlock_use_rng || padlock_use_ace);
}
# ifndef AES_ASM
static int padlock_aes_set_encrypt_key(const unsigned char *userKey,
const int bits,
AES_KEY *key);
static int padlock_aes_set_decrypt_key(const unsigned char *userKey,
const int bits,
AES_KEY *key);
# define AES_ASM
# define AES_set_encrypt_key padlock_aes_set_encrypt_key
# define AES_set_decrypt_key padlock_aes_set_decrypt_key
# include "../crypto/aes/aes_core.c"
# endif
/*
* This stuff is needed if this ENGINE is being compiled into a
* self-contained shared-library.
*/
# ifndef OPENSSL_NO_DYNAMIC_ENGINE
static int padlock_bind_fn(ENGINE *e, const char *id)
{
if (id && (strcmp(id, padlock_id) != 0)) {
return 0;
}
if (!padlock_bind_helper(e)) {
return 0;
}
return 1;
}
IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
# endif /* !OPENSSL_NO_DYNAMIC_ENGINE */
/* ===== Here comes the "real" engine ===== */
/* Some AES-related constants */
# define AES_BLOCK_SIZE 16
# define AES_KEY_SIZE_128 16
# define AES_KEY_SIZE_192 24
# define AES_KEY_SIZE_256 32
/*
* Here we store the status information relevant to the current context.
*/
/*
* BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
* the order of items in this structure. Don't blindly modify, reorder,
* etc!
*/
struct padlock_cipher_data {
unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
union {
unsigned int pad[4];
struct {
int rounds:4;
int dgst:1; /* n/a in C3 */
int align:1; /* n/a in C3 */
int ciphr:1; /* n/a in C3 */
unsigned int keygen:1;
int interm:1;
unsigned int encdec:1;
int ksize:2;
} b;
} cword; /* Control word */
AES_KEY ks; /* Encryption key */
};
/* Interface to assembler module */
unsigned int padlock_capability(void);
void padlock_key_bswap(AES_KEY *key);
void padlock_verify_context(struct padlock_cipher_data *ctx);
void padlock_reload_key(void);
void padlock_aes_block(void *out, const void *inp,
struct padlock_cipher_data *ctx);
int padlock_ecb_encrypt(void *out, const void *inp,
struct padlock_cipher_data *ctx, size_t len);
int padlock_cbc_encrypt(void *out, const void *inp,
struct padlock_cipher_data *ctx, size_t len);
int padlock_cfb_encrypt(void *out, const void *inp,
struct padlock_cipher_data *ctx, size_t len);
int padlock_ofb_encrypt(void *out, const void *inp,
struct padlock_cipher_data *ctx, size_t len);
int padlock_ctr32_encrypt(void *out, const void *inp,
struct padlock_cipher_data *ctx, size_t len);
int padlock_xstore(void *out, int edx);
void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len);
void padlock_sha1(void *ctx, const void *inp, size_t len);
void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len);
void padlock_sha256(void *ctx, const void *inp, size_t len);
/*
* Load supported features of the CPU to see if the PadLock is available.
*/
static int padlock_available(void)
{
unsigned int edx = padlock_capability();
/* Fill up some flags */
padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
return padlock_use_ace + padlock_use_rng;
}
/* ===== AES encryption/decryption ===== */
# if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
# define NID_aes_128_cfb NID_aes_128_cfb128
# endif
# if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
# define NID_aes_128_ofb NID_aes_128_ofb128
# endif
# if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
# define NID_aes_192_cfb NID_aes_192_cfb128
# endif
# if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
# define NID_aes_192_ofb NID_aes_192_ofb128
# endif
# if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
# define NID_aes_256_cfb NID_aes_256_cfb128
# endif
# if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
# define NID_aes_256_ofb NID_aes_256_ofb128
# endif
/* List of supported ciphers. */
static const int padlock_cipher_nids[] = {
NID_aes_128_ecb,
NID_aes_128_cbc,
NID_aes_128_cfb,
NID_aes_128_ofb,
NID_aes_128_ctr,
NID_aes_192_ecb,
NID_aes_192_cbc,
NID_aes_192_cfb,
NID_aes_192_ofb,
NID_aes_192_ctr,
NID_aes_256_ecb,
NID_aes_256_cbc,
NID_aes_256_cfb,
NID_aes_256_ofb,
NID_aes_256_ctr
};
static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
sizeof(padlock_cipher_nids[0]));
/* Function prototypes ... */
static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc);
# define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) + \
( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F ) )
# define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx)))
static int
padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
const unsigned char *in_arg, size_t nbytes)
{
return padlock_ecb_encrypt(out_arg, in_arg,
ALIGNED_CIPHER_DATA(ctx), nbytes);
}
static int
padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
const unsigned char *in_arg, size_t nbytes)
{
struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
int ret;
memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes)))
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
return ret;
}
static int
padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
const unsigned char *in_arg, size_t nbytes)
{
struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
size_t chunk;
if ((chunk = EVP_CIPHER_CTX_get_num(ctx))) { /* borrow chunk variable */
unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
if (chunk >= AES_BLOCK_SIZE)
return 0; /* bogus value */
if (EVP_CIPHER_CTX_is_encrypting(ctx))
while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
chunk++, nbytes--;
} else
while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
unsigned char c = *(in_arg++);
*(out_arg++) = c ^ ivp[chunk];
ivp[chunk++] = c, nbytes--;
}
EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
}
if (nbytes == 0)
return 1;
memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk))
return 0;
nbytes -= chunk;
}
if (nbytes) {
unsigned char *ivp = cdata->iv;
out_arg += chunk;
in_arg += chunk;
EVP_CIPHER_CTX_set_num(ctx, nbytes);
if (cdata->cword.b.encdec) {
cdata->cword.b.encdec = 0;
padlock_reload_key();
padlock_aes_block(ivp, ivp, cdata);
cdata->cword.b.encdec = 1;
padlock_reload_key();
while (nbytes) {
unsigned char c = *(in_arg++);
*(out_arg++) = c ^ *ivp;
*(ivp++) = c, nbytes--;
}
} else {
padlock_reload_key();
padlock_aes_block(ivp, ivp, cdata);
padlock_reload_key();
while (nbytes) {
*ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
ivp++, nbytes--;
}
}
}
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
return 1;
}
static int
padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
const unsigned char *in_arg, size_t nbytes)
{
struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
size_t chunk;
/*
* ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
*/
if ((chunk = EVP_CIPHER_CTX_get_num(ctx))) { /* borrow chunk variable */
unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx);
if (chunk >= AES_BLOCK_SIZE)
return 0; /* bogus value */
while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
*(out_arg++) = *(in_arg++) ^ ivp[chunk];
chunk++, nbytes--;
}
EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE);
}
if (nbytes == 0)
return 1;
memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE);
if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) {
if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk))
return 0;
nbytes -= chunk;
}
if (nbytes) {
unsigned char *ivp = cdata->iv;
out_arg += chunk;
in_arg += chunk;
EVP_CIPHER_CTX_set_num(ctx, nbytes);
padlock_reload_key(); /* empirically found */
padlock_aes_block(ivp, ivp, cdata);
padlock_reload_key(); /* empirically found */
while (nbytes) {
*(out_arg++) = *(in_arg++) ^ *ivp;
ivp++, nbytes--;
}
}
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE);
return 1;
}
static void padlock_ctr32_encrypt_glue(const unsigned char *in,
unsigned char *out, size_t blocks,
struct padlock_cipher_data *ctx,
const unsigned char *ivec)
{
memcpy(ctx->iv, ivec, AES_BLOCK_SIZE);
padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks);
}
static int
padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
const unsigned char *in_arg, size_t nbytes)
{
struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx);
int n = EVP_CIPHER_CTX_get_num(ctx);
unsigned int num;
if (n < 0)
return 0;
num = (unsigned int)n;
CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes,
cdata, EVP_CIPHER_CTX_iv_noconst(ctx),
EVP_CIPHER_CTX_buf_noconst(ctx), &num,
(ctr128_f) padlock_ctr32_encrypt_glue);
EVP_CIPHER_CTX_set_num(ctx, (size_t)num);
return 1;
}
# define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
# define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
# define EVP_CIPHER_block_size_OFB 1
# define EVP_CIPHER_block_size_CFB 1
# define EVP_CIPHER_block_size_CTR 1
/*
* Declaring so many ciphers by hand would be a pain. Instead introduce a bit
* of preprocessor magic :-)
*/
# define DECLARE_AES_EVP(ksize,lmode,umode) \
static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \
static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \
{ \
if (_hidden_aes_##ksize##_##lmode == NULL \
&& ((_hidden_aes_##ksize##_##lmode = \
EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \
EVP_CIPHER_block_size_##umode, \
AES_KEY_SIZE_##ksize)) == NULL \
|| !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \
AES_BLOCK_SIZE) \
|| !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \
0 | EVP_CIPH_##umode##_MODE) \
|| !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \
padlock_aes_init_key) \
|| !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \
padlock_##lmode##_cipher) \
|| !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \
sizeof(struct padlock_cipher_data) + 16) \
|| !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \
EVP_CIPHER_set_asn1_iv) \
|| !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \
EVP_CIPHER_get_asn1_iv))) { \
EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode); \
_hidden_aes_##ksize##_##lmode = NULL; \
} \
return _hidden_aes_##ksize##_##lmode; \
}
DECLARE_AES_EVP(128, ecb, ECB)
DECLARE_AES_EVP(128, cbc, CBC)
DECLARE_AES_EVP(128, cfb, CFB)
DECLARE_AES_EVP(128, ofb, OFB)
DECLARE_AES_EVP(128, ctr, CTR)
DECLARE_AES_EVP(192, ecb, ECB)
DECLARE_AES_EVP(192, cbc, CBC)
DECLARE_AES_EVP(192, cfb, CFB)
DECLARE_AES_EVP(192, ofb, OFB)
DECLARE_AES_EVP(192, ctr, CTR)
DECLARE_AES_EVP(256, ecb, ECB)
DECLARE_AES_EVP(256, cbc, CBC)
DECLARE_AES_EVP(256, cfb, CFB)
DECLARE_AES_EVP(256, ofb, OFB)
DECLARE_AES_EVP(256, ctr, CTR)
static int
padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
int nid)
{
/* No specific cipher => return a list of supported nids ... */
if (!cipher) {
*nids = padlock_cipher_nids;
return padlock_cipher_nids_num;
}
/* ... or the requested "cipher" otherwise */
switch (nid) {
case NID_aes_128_ecb:
*cipher = padlock_aes_128_ecb();
break;
case NID_aes_128_cbc:
*cipher = padlock_aes_128_cbc();
break;
case NID_aes_128_cfb:
*cipher = padlock_aes_128_cfb();
break;
case NID_aes_128_ofb:
*cipher = padlock_aes_128_ofb();
break;
case NID_aes_128_ctr:
*cipher = padlock_aes_128_ctr();
break;
case NID_aes_192_ecb:
*cipher = padlock_aes_192_ecb();
break;
case NID_aes_192_cbc:
*cipher = padlock_aes_192_cbc();
break;
case NID_aes_192_cfb:
*cipher = padlock_aes_192_cfb();
break;
case NID_aes_192_ofb:
*cipher = padlock_aes_192_ofb();
break;
case NID_aes_192_ctr:
*cipher = padlock_aes_192_ctr();
break;
case NID_aes_256_ecb:
*cipher = padlock_aes_256_ecb();
break;
case NID_aes_256_cbc:
*cipher = padlock_aes_256_cbc();
break;
case NID_aes_256_cfb:
*cipher = padlock_aes_256_cfb();
break;
case NID_aes_256_ofb:
*cipher = padlock_aes_256_ofb();
break;
case NID_aes_256_ctr:
*cipher = padlock_aes_256_ctr();
break;
default:
/* Sorry, we don't support this NID */
*cipher = NULL;
return 0;
}
return 1;
}
/* Prepare the encryption key for PadLock usage */
static int
padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
struct padlock_cipher_data *cdata;
int key_len = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
unsigned long mode = EVP_CIPHER_CTX_get_mode(ctx);
if (key == NULL)
return 0; /* ERROR */
cdata = ALIGNED_CIPHER_DATA(ctx);
memset(cdata, 0, sizeof(*cdata));
/* Prepare Control word. */
if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE)
cdata->cword.b.encdec = 0;
else
cdata->cword.b.encdec = (EVP_CIPHER_CTX_is_encrypting(ctx) == 0);
cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
cdata->cword.b.ksize = (key_len - 128) / 64;
switch (key_len) {
case 128:
/*
* PadLock can generate an extended key for AES128 in hardware
*/
memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
cdata->cword.b.keygen = 0;
break;
case 192:
case 256:
/*
* Generate an extended AES key in software. Needed for AES192/AES256
*/
/*
* Well, the above applies to Stepping 8 CPUs and is listed as
* hardware errata. They most likely will fix it at some point and
* then a check for stepping would be due here.
*/
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
&& !enc)
AES_set_decrypt_key(key, key_len, &cdata->ks);
else
AES_set_encrypt_key(key, key_len, &cdata->ks);
/*
* OpenSSL C functions use byte-swapped extended key.
*/
padlock_key_bswap(&cdata->ks);
cdata->cword.b.keygen = 1;
break;
default:
/* ERROR */
return 0;
}
/*
* This is done to cover for cases when user reuses the
* context for new key. The catch is that if we don't do
* this, padlock_eas_cipher might proceed with old key...
*/
padlock_reload_key();
return 1;
}
/* ===== Random Number Generator ===== */
/*
* This code is not engaged. The reason is that it does not comply
* with recommendations for VIA RNG usage for secure applications
* (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
* provide meaningful error control...
*/
/*
* Wrapper that provides an interface between the API and the raw PadLock
* RNG
*/
static int padlock_rand_bytes(unsigned char *output, int count)
{
unsigned int eax, buf;
while (count >= 8) {
eax = padlock_xstore(output, 0);
if (!(eax & (1 << 6)))
return 0; /* RNG disabled */
/* this ---vv--- covers DC bias, Raw Bits and String Filter */
if (eax & (0x1F << 10))
return 0;
if ((eax & 0x1F) == 0)
continue; /* no data, retry... */
if ((eax & 0x1F) != 8)
return 0; /* fatal failure... */
output += 8;
count -= 8;
}
while (count > 0) {
eax = padlock_xstore(&buf, 3);
if (!(eax & (1 << 6)))
return 0; /* RNG disabled */
/* this ---vv--- covers DC bias, Raw Bits and String Filter */
if (eax & (0x1F << 10))
return 0;
if ((eax & 0x1F) == 0)
continue; /* no data, retry... */
if ((eax & 0x1F) != 1)
return 0; /* fatal failure... */
*output++ = (unsigned char)buf;
count--;
}
OPENSSL_cleanse(&buf, sizeof(buf));
return 1;
}
/* Dummy but necessary function */
static int padlock_rand_status(void)
{
return 1;
}
/* Prepare structure for registration */
static RAND_METHOD padlock_rand = {
NULL, /* seed */
padlock_rand_bytes, /* bytes */
NULL, /* cleanup */
NULL, /* add */
padlock_rand_bytes, /* pseudorand */
padlock_rand_status, /* rand status */
};
# endif /* COMPILE_PADLOCKENG */
#endif /* !OPENSSL_NO_PADLOCKENG */
#if defined(OPENSSL_NO_PADLOCKENG) || !defined(COMPILE_PADLOCKENG)
# ifndef OPENSSL_NO_DYNAMIC_ENGINE
OPENSSL_EXPORT
int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
OPENSSL_EXPORT
int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
{
return 0;
}
IMPLEMENT_DYNAMIC_CHECK_FN()
# endif
#endif
| 23,905 | 30.290576 | 92 | c |
openssl | openssl-master/fuzz/asn1.c | /*
* Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* Fuzz ASN.1 parsing for various data structures. Specify which on the
* command line:
*
* asn1 <data structure>
*/
/* We need to use some deprecated APIs */
#define OPENSSL_SUPPRESS_DEPRECATED
#include <stdio.h>
#include <string.h>
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/dh.h>
#include <openssl/dsa.h>
#include <openssl/ec.h>
#include <openssl/ocsp.h>
#include <openssl/pkcs12.h>
#include <openssl/rsa.h>
#include <openssl/ts.h>
#include <openssl/x509v3.h>
#include <openssl/cms.h>
#include <openssl/ess.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/ssl.h>
#include "internal/nelem.h"
#include "fuzzer.h"
static ASN1_ITEM_EXP *item_type[] = {
ASN1_ITEM_ref(ACCESS_DESCRIPTION),
#ifndef OPENSSL_NO_RFC3779
ASN1_ITEM_ref(ASIdentifierChoice),
ASN1_ITEM_ref(ASIdentifiers),
ASN1_ITEM_ref(ASIdOrRange),
#endif
ASN1_ITEM_ref(ASN1_ANY),
ASN1_ITEM_ref(ASN1_BIT_STRING),
ASN1_ITEM_ref(ASN1_BMPSTRING),
ASN1_ITEM_ref(ASN1_BOOLEAN),
ASN1_ITEM_ref(ASN1_ENUMERATED),
ASN1_ITEM_ref(ASN1_FBOOLEAN),
ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
ASN1_ITEM_ref(ASN1_GENERALSTRING),
ASN1_ITEM_ref(ASN1_IA5STRING),
ASN1_ITEM_ref(ASN1_INTEGER),
ASN1_ITEM_ref(ASN1_NULL),
ASN1_ITEM_ref(ASN1_OBJECT),
ASN1_ITEM_ref(ASN1_OCTET_STRING),
ASN1_ITEM_ref(ASN1_OCTET_STRING_NDEF),
ASN1_ITEM_ref(ASN1_PRINTABLE),
ASN1_ITEM_ref(ASN1_PRINTABLESTRING),
ASN1_ITEM_ref(ASN1_SEQUENCE),
ASN1_ITEM_ref(ASN1_SEQUENCE_ANY),
ASN1_ITEM_ref(ASN1_SET_ANY),
ASN1_ITEM_ref(ASN1_T61STRING),
ASN1_ITEM_ref(ASN1_TBOOLEAN),
ASN1_ITEM_ref(ASN1_TIME),
ASN1_ITEM_ref(ASN1_UNIVERSALSTRING),
ASN1_ITEM_ref(ASN1_UTCTIME),
ASN1_ITEM_ref(ASN1_UTF8STRING),
ASN1_ITEM_ref(ASN1_VISIBLESTRING),
#ifndef OPENSSL_NO_RFC3779
ASN1_ITEM_ref(ASRange),
#endif
ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
ASN1_ITEM_ref(AUTHORITY_KEYID),
ASN1_ITEM_ref(BASIC_CONSTRAINTS),
ASN1_ITEM_ref(BIGNUM),
ASN1_ITEM_ref(CBIGNUM),
ASN1_ITEM_ref(CERTIFICATEPOLICIES),
#ifndef OPENSSL_NO_CMS
ASN1_ITEM_ref(CMS_ContentInfo),
ASN1_ITEM_ref(CMS_ReceiptRequest),
ASN1_ITEM_ref(CRL_DIST_POINTS),
#endif
#ifndef OPENSSL_NO_DH
ASN1_ITEM_ref(DHparams),
#endif
ASN1_ITEM_ref(DIRECTORYSTRING),
ASN1_ITEM_ref(DISPLAYTEXT),
ASN1_ITEM_ref(DIST_POINT),
ASN1_ITEM_ref(DIST_POINT_NAME),
#if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
ASN1_ITEM_ref(ECPARAMETERS),
ASN1_ITEM_ref(ECPKPARAMETERS),
#endif
ASN1_ITEM_ref(EDIPARTYNAME),
ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
ASN1_ITEM_ref(GENERAL_NAME),
ASN1_ITEM_ref(GENERAL_NAMES),
ASN1_ITEM_ref(GENERAL_SUBTREE),
#ifndef OPENSSL_NO_RFC3779
ASN1_ITEM_ref(IPAddressChoice),
ASN1_ITEM_ref(IPAddressFamily),
ASN1_ITEM_ref(IPAddressOrRange),
ASN1_ITEM_ref(IPAddressRange),
#endif
ASN1_ITEM_ref(ISSUING_DIST_POINT),
#ifndef OPENSSL_NO_DEPRECATED_3_0
ASN1_ITEM_ref(LONG),
#endif
ASN1_ITEM_ref(NAME_CONSTRAINTS),
ASN1_ITEM_ref(NETSCAPE_CERT_SEQUENCE),
ASN1_ITEM_ref(NETSCAPE_SPKAC),
ASN1_ITEM_ref(NETSCAPE_SPKI),
ASN1_ITEM_ref(NOTICEREF),
#ifndef OPENSSL_NO_OCSP
ASN1_ITEM_ref(OCSP_BASICRESP),
ASN1_ITEM_ref(OCSP_CERTID),
ASN1_ITEM_ref(OCSP_CERTSTATUS),
ASN1_ITEM_ref(OCSP_CRLID),
ASN1_ITEM_ref(OCSP_ONEREQ),
ASN1_ITEM_ref(OCSP_REQINFO),
ASN1_ITEM_ref(OCSP_REQUEST),
ASN1_ITEM_ref(OCSP_RESPBYTES),
ASN1_ITEM_ref(OCSP_RESPDATA),
ASN1_ITEM_ref(OCSP_RESPID),
ASN1_ITEM_ref(OCSP_RESPONSE),
ASN1_ITEM_ref(OCSP_REVOKEDINFO),
ASN1_ITEM_ref(OCSP_SERVICELOC),
ASN1_ITEM_ref(OCSP_SIGNATURE),
ASN1_ITEM_ref(OCSP_SINGLERESP),
#endif
ASN1_ITEM_ref(OTHERNAME),
ASN1_ITEM_ref(PBE2PARAM),
ASN1_ITEM_ref(PBEPARAM),
ASN1_ITEM_ref(PBKDF2PARAM),
ASN1_ITEM_ref(PKCS12),
ASN1_ITEM_ref(PKCS12_AUTHSAFES),
ASN1_ITEM_ref(PKCS12_BAGS),
ASN1_ITEM_ref(PKCS12_MAC_DATA),
ASN1_ITEM_ref(PKCS12_SAFEBAG),
ASN1_ITEM_ref(PKCS12_SAFEBAGS),
ASN1_ITEM_ref(PKCS7),
ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
ASN1_ITEM_ref(PKCS7_DIGEST),
ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
ASN1_ITEM_ref(PKCS7_ENCRYPT),
ASN1_ITEM_ref(PKCS7_ENVELOPE),
ASN1_ITEM_ref(PKCS7_ISSUER_AND_SERIAL),
ASN1_ITEM_ref(PKCS7_RECIP_INFO),
ASN1_ITEM_ref(PKCS7_SIGNED),
ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
ASN1_ITEM_ref(PKCS8_PRIV_KEY_INFO),
ASN1_ITEM_ref(PKEY_USAGE_PERIOD),
ASN1_ITEM_ref(POLICY_CONSTRAINTS),
ASN1_ITEM_ref(POLICYINFO),
ASN1_ITEM_ref(POLICY_MAPPING),
ASN1_ITEM_ref(POLICY_MAPPINGS),
ASN1_ITEM_ref(POLICYQUALINFO),
ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
ASN1_ITEM_ref(PROXY_POLICY),
ASN1_ITEM_ref(RSA_OAEP_PARAMS),
ASN1_ITEM_ref(RSA_PSS_PARAMS),
#ifndef OPENSSL_NO_DEPRECATED_3_0
ASN1_ITEM_ref(RSAPrivateKey),
ASN1_ITEM_ref(RSAPublicKey),
#endif
ASN1_ITEM_ref(SXNET),
ASN1_ITEM_ref(SXNETID),
ASN1_ITEM_ref(USERNOTICE),
ASN1_ITEM_ref(X509),
ASN1_ITEM_ref(X509_ALGOR),
ASN1_ITEM_ref(X509_ALGORS),
ASN1_ITEM_ref(X509_ATTRIBUTE),
ASN1_ITEM_ref(X509_CERT_AUX),
ASN1_ITEM_ref(X509_CINF),
ASN1_ITEM_ref(X509_CRL),
ASN1_ITEM_ref(X509_CRL_INFO),
ASN1_ITEM_ref(X509_EXTENSION),
ASN1_ITEM_ref(X509_EXTENSIONS),
ASN1_ITEM_ref(X509_NAME),
ASN1_ITEM_ref(X509_NAME_ENTRY),
ASN1_ITEM_ref(X509_PUBKEY),
ASN1_ITEM_ref(X509_REQ),
ASN1_ITEM_ref(X509_REQ_INFO),
ASN1_ITEM_ref(X509_REVOKED),
ASN1_ITEM_ref(X509_SIG),
ASN1_ITEM_ref(X509_VAL),
#ifndef OPENSSL_NO_DEPRECATED_3_0
ASN1_ITEM_ref(ZLONG),
#endif
ASN1_ITEM_ref(INT32),
ASN1_ITEM_ref(ZINT32),
ASN1_ITEM_ref(UINT32),
ASN1_ITEM_ref(ZUINT32),
ASN1_ITEM_ref(INT64),
ASN1_ITEM_ref(ZINT64),
ASN1_ITEM_ref(UINT64),
ASN1_ITEM_ref(ZUINT64),
NULL
};
static ASN1_PCTX *pctx;
#define DO_TEST(TYPE, D2I, I2D, PRINT) { \
const unsigned char *p = buf; \
unsigned char *der = NULL; \
TYPE *type = D2I(NULL, &p, len); \
\
if (type != NULL) { \
int len2; \
BIO *bio = BIO_new(BIO_s_null()); \
\
if (bio != NULL) { \
PRINT(bio, type); \
BIO_free(bio); \
} \
len2 = I2D(type, &der); \
if (len2 != 0) {} \
OPENSSL_free(der); \
TYPE ## _free(type); \
} \
}
#define DO_TEST_PRINT_OFFSET(TYPE, D2I, I2D, PRINT) { \
const unsigned char *p = buf; \
unsigned char *der = NULL; \
TYPE *type = D2I(NULL, &p, len); \
\
if (type != NULL) { \
BIO *bio = BIO_new(BIO_s_null()); \
\
if (bio != NULL) { \
PRINT(bio, type, 0); \
BIO_free(bio); \
} \
I2D(type, &der); \
OPENSSL_free(der); \
TYPE ## _free(type); \
} \
}
#define DO_TEST_PRINT_PCTX(TYPE, D2I, I2D, PRINT) { \
const unsigned char *p = buf; \
unsigned char *der = NULL; \
TYPE *type = D2I(NULL, &p, len); \
\
if (type != NULL) { \
BIO *bio = BIO_new(BIO_s_null()); \
\
if (bio != NULL) { \
PRINT(bio, type, 0, pctx); \
BIO_free(bio); \
} \
I2D(type, &der); \
OPENSSL_free(der); \
TYPE ## _free(type); \
} \
}
#define DO_TEST_NO_PRINT(TYPE, D2I, I2D) { \
const unsigned char *p = buf; \
unsigned char *der = NULL; \
TYPE *type = D2I(NULL, &p, len); \
\
if (type != NULL) { \
BIO *bio = BIO_new(BIO_s_null()); \
\
BIO_free(bio); \
I2D(type, &der); \
OPENSSL_free(der); \
TYPE ## _free(type); \
} \
}
int FuzzerInitialize(int *argc, char ***argv)
{
FuzzerSetRand();
pctx = ASN1_PCTX_new();
ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT |
ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF |
ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT |
ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL);
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
int n;
for (n = 0; item_type[n] != NULL; ++n) {
const uint8_t *b = buf;
unsigned char *der = NULL;
const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
if (o != NULL) {
BIO *bio = BIO_new(BIO_s_null());
if (bio != NULL) {
ASN1_item_print(bio, o, 4, i, pctx);
BIO_free(bio);
}
if (ASN1_item_i2d(o, &der, i) > 0) {
OPENSSL_free(der);
}
ASN1_item_free(o, i);
}
}
#ifndef OPENSSL_NO_TS
DO_TEST(TS_REQ, d2i_TS_REQ, i2d_TS_REQ, TS_REQ_print_bio);
DO_TEST(TS_MSG_IMPRINT, d2i_TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, TS_MSG_IMPRINT_print_bio);
DO_TEST(TS_RESP, d2i_TS_RESP, i2d_TS_RESP, TS_RESP_print_bio);
DO_TEST(TS_STATUS_INFO, d2i_TS_STATUS_INFO, i2d_TS_STATUS_INFO, TS_STATUS_INFO_print_bio);
DO_TEST(TS_TST_INFO, d2i_TS_TST_INFO, i2d_TS_TST_INFO, TS_TST_INFO_print_bio);
DO_TEST_NO_PRINT(TS_ACCURACY, d2i_TS_ACCURACY, i2d_TS_ACCURACY);
#endif
DO_TEST_NO_PRINT(ESS_ISSUER_SERIAL, d2i_ESS_ISSUER_SERIAL, i2d_ESS_ISSUER_SERIAL);
DO_TEST_NO_PRINT(ESS_CERT_ID, d2i_ESS_CERT_ID, i2d_ESS_CERT_ID);
DO_TEST_NO_PRINT(ESS_SIGNING_CERT, d2i_ESS_SIGNING_CERT, i2d_ESS_SIGNING_CERT);
DO_TEST_NO_PRINT(ESS_CERT_ID_V2, d2i_ESS_CERT_ID_V2, i2d_ESS_CERT_ID_V2);
DO_TEST_NO_PRINT(ESS_SIGNING_CERT_V2, d2i_ESS_SIGNING_CERT_V2, i2d_ESS_SIGNING_CERT_V2);
#if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
DO_TEST_NO_PRINT(DH, d2i_DHparams, i2d_DHparams);
DO_TEST_NO_PRINT(DH, d2i_DHxparams, i2d_DHxparams);
#endif
#ifndef OPENSSL_NO_DSA
DO_TEST_NO_PRINT(DSA_SIG, d2i_DSA_SIG, i2d_DSA_SIG);
# ifndef OPENSSL_NO_DEPRECATED_3_0
DO_TEST_NO_PRINT(DSA, d2i_DSAPrivateKey, i2d_DSAPrivateKey);
DO_TEST_NO_PRINT(DSA, d2i_DSAPublicKey, i2d_DSAPublicKey);
DO_TEST_NO_PRINT(DSA, d2i_DSAparams, i2d_DSAparams);
# endif
#endif
#ifndef OPENSSL_NO_DEPRECATED_3_0
DO_TEST_NO_PRINT(RSA, d2i_RSAPublicKey, i2d_RSAPublicKey);
#endif
#ifndef OPENSSL_NO_EC
# ifndef OPENSSL_NO_DEPRECATED_3_0
DO_TEST_PRINT_OFFSET(EC_GROUP, d2i_ECPKParameters, i2d_ECPKParameters, ECPKParameters_print);
DO_TEST_PRINT_OFFSET(EC_KEY, d2i_ECPrivateKey, i2d_ECPrivateKey, EC_KEY_print);
DO_TEST(EC_KEY, d2i_ECParameters, i2d_ECParameters, ECParameters_print);
DO_TEST_NO_PRINT(ECDSA_SIG, d2i_ECDSA_SIG, i2d_ECDSA_SIG);
# endif
#endif
DO_TEST_PRINT_PCTX(EVP_PKEY, d2i_AutoPrivateKey, i2d_PrivateKey, EVP_PKEY_print_private);
DO_TEST(SSL_SESSION, d2i_SSL_SESSION, i2d_SSL_SESSION, SSL_SESSION_print);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
ASN1_PCTX_free(pctx);
FuzzerClearRand();
}
| 11,628 | 29.928191 | 97 | c |
openssl | openssl-master/fuzz/asn1parse.c | /*
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* Fuzz the parser used for dumping ASN.1 using "openssl asn1parse".
*/
#include <stdio.h>
#include <openssl/asn1.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include "fuzzer.h"
static BIO *bio_out;
int FuzzerInitialize(int *argc, char ***argv)
{
bio_out = BIO_new(BIO_s_null()); /* output will be ignored */
if (bio_out == NULL)
return 0;
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
(void)ASN1_parse_dump(bio_out, buf, len, 0, 0);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
BIO_free(bio_out);
}
| 1,094 | 22.804348 | 72 | c |
openssl | openssl-master/fuzz/bignum.c | /*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* Confirm that a^b mod c agrees when calculated cleverly vs naively, for
* random a, b and c.
*/
#include <stdio.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
int success = 0;
size_t l1 = 0, l2 = 0, l3 = 0;
int s1 = 0, s3 = 0;
BN_CTX *ctx;
BIGNUM *b1;
BIGNUM *b2;
BIGNUM *b3;
BIGNUM *b4;
BIGNUM *b5;
b1 = BN_new();
b2 = BN_new();
b3 = BN_new();
b4 = BN_new();
b5 = BN_new();
ctx = BN_CTX_new();
/* Divide the input into three parts, using the values of the first two
* bytes to choose lengths, which generate b1, b2 and b3. Use three bits
* of the third byte to choose signs for the three numbers.
*/
if (len > 2) {
len -= 3;
l1 = (buf[0] * len) / 255;
++buf;
l2 = (buf[0] * (len - l1)) / 255;
++buf;
l3 = len - l1 - l2;
s1 = buf[0] & 1;
s3 = buf[0] & 4;
++buf;
}
OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
BN_set_negative(b1, s1);
OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
OPENSSL_assert(BN_bin2bn(buf + l1 + l2, l3, b3) == b3);
BN_set_negative(b3, s3);
/* mod 0 is undefined */
if (BN_is_zero(b3)) {
success = 1;
goto done;
}
OPENSSL_assert(BN_mod_exp(b4, b1, b2, b3, ctx));
OPENSSL_assert(BN_mod_exp_simple(b5, b1, b2, b3, ctx));
success = BN_cmp(b4, b5) == 0;
if (!success) {
BN_print_fp(stdout, b1);
putchar('\n');
BN_print_fp(stdout, b2);
putchar('\n');
BN_print_fp(stdout, b3);
putchar('\n');
BN_print_fp(stdout, b4);
putchar('\n');
BN_print_fp(stdout, b5);
putchar('\n');
}
done:
OPENSSL_assert(success);
BN_free(b1);
BN_free(b2);
BN_free(b3);
BN_free(b4);
BN_free(b5);
BN_CTX_free(ctx);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
}
| 2,516 | 21.881818 | 76 | c |
openssl | openssl-master/fuzz/bndiv.c | /*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* Confirm that if (d, r) = a / b, then b * d + r == a, and that sign(d) ==
* sign(a), and 0 <= r <= b
*/
#include <stdio.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include "fuzzer.h"
/* 256 kB */
#define MAX_LEN (256 * 1000)
static BN_CTX *ctx;
static BIGNUM *b1;
static BIGNUM *b2;
static BIGNUM *b3;
static BIGNUM *b4;
static BIGNUM *b5;
int FuzzerInitialize(int *argc, char ***argv)
{
b1 = BN_new();
b2 = BN_new();
b3 = BN_new();
b4 = BN_new();
b5 = BN_new();
ctx = BN_CTX_new();
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
int success = 0;
size_t l1 = 0, l2 = 0;
/* s1 and s2 will be the signs for b1 and b2. */
int s1 = 0, s2 = 0;
/* limit the size of the input to avoid timeout */
if (len > MAX_LEN)
len = MAX_LEN;
/* We are going to split the buffer in two, sizes l1 and l2, giving b1 and
* b2.
*/
if (len > 0) {
--len;
/* Use first byte to divide the remaining buffer into 3Fths. I admit
* this disallows some number sizes. If it matters, better ideas are
* welcome (Ben).
*/
l1 = ((buf[0] & 0x3f) * len) / 0x3f;
s1 = buf[0] & 0x40;
s2 = buf[0] & 0x80;
++buf;
l2 = len - l1;
}
OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
BN_set_negative(b1, s1);
OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
BN_set_negative(b2, s2);
/* divide by 0 is an error */
if (BN_is_zero(b2)) {
success = 1;
goto done;
}
OPENSSL_assert(BN_div(b3, b4, b1, b2, ctx));
if (BN_is_zero(b1))
success = BN_is_zero(b3) && BN_is_zero(b4);
else if (BN_is_negative(b1))
success = (BN_is_negative(b3) != BN_is_negative(b2) || BN_is_zero(b3))
&& (BN_is_negative(b4) || BN_is_zero(b4));
else
success = (BN_is_negative(b3) == BN_is_negative(b2) || BN_is_zero(b3))
&& (!BN_is_negative(b4) || BN_is_zero(b4));
OPENSSL_assert(BN_mul(b5, b3, b2, ctx));
OPENSSL_assert(BN_add(b5, b5, b4));
success = success && BN_cmp(b5, b1) == 0;
if (!success) {
BN_print_fp(stdout, b1);
putchar('\n');
BN_print_fp(stdout, b2);
putchar('\n');
BN_print_fp(stdout, b3);
putchar('\n');
BN_print_fp(stdout, b4);
putchar('\n');
BN_print_fp(stdout, b5);
putchar('\n');
printf("%d %d %d %d %d %d %d\n", BN_is_negative(b1),
BN_is_negative(b2),
BN_is_negative(b3), BN_is_negative(b4), BN_is_zero(b4),
BN_is_negative(b3) != BN_is_negative(b2)
&& (BN_is_negative(b4) || BN_is_zero(b4)),
BN_cmp(b5, b1));
puts("----\n");
}
done:
OPENSSL_assert(success);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
BN_free(b1);
BN_free(b2);
BN_free(b3);
BN_free(b4);
BN_free(b5);
BN_CTX_free(ctx);
}
| 3,434 | 25.022727 | 79 | c |
openssl | openssl-master/fuzz/client.c | /*
* Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <time.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/dsa.h>
#include <openssl/ec.h>
#include <openssl/dh.h>
#include <openssl/err.h>
#include "fuzzer.h"
/* unused, to avoid warning. */
static int idx;
#define FUZZTIME 1485898104
#define TIME_IMPL(t) { if (t != NULL) *t = FUZZTIME; return FUZZTIME; }
/*
* This might not work in all cases (and definitely not on Windows
* because of the way linkers are) and callees can still get the
* current time instead of the fixed time. This will just result
* in things not being fully reproducible and have a slightly
* different coverage.
*/
#if !defined(_WIN32)
time_t time(time_t *t) TIME_IMPL(t)
#endif
int FuzzerInitialize(int *argc, char ***argv)
{
STACK_OF(SSL_COMP) *comp_methods;
FuzzerSetRand();
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
idx = SSL_get_ex_data_X509_STORE_CTX_idx();
comp_methods = SSL_COMP_get_compression_methods();
if (comp_methods != NULL)
sk_SSL_COMP_sort(comp_methods);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
SSL *client = NULL;
BIO *in;
BIO *out;
SSL_CTX *ctx;
if (len == 0)
return 0;
/* This only fuzzes the initial flow from the client so far. */
ctx = SSL_CTX_new(SSLv23_method());
if (ctx == NULL)
goto end;
client = SSL_new(ctx);
if (client == NULL)
goto end;
OPENSSL_assert(SSL_set_min_proto_version(client, 0) == 1);
OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1);
SSL_set_tlsext_host_name(client, "localhost");
in = BIO_new(BIO_s_mem());
if (in == NULL)
goto end;
out = BIO_new(BIO_s_mem());
if (out == NULL) {
BIO_free(in);
goto end;
}
SSL_set_bio(client, in, out);
SSL_set_connect_state(client);
OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
if (SSL_do_handshake(client) == 1) {
/* Keep reading application data until error or EOF. */
uint8_t tmp[1024];
for (;;) {
if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
break;
}
}
}
end:
SSL_free(client);
ERR_clear_error();
SSL_CTX_free(ctx);
return 0;
}
void FuzzerCleanup(void)
{
FuzzerClearRand();
}
| 2,852 | 25.174312 | 85 | c |
openssl | openssl-master/fuzz/cmp.c | /*
* Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* Test CMP DER parsing.
*/
#include <openssl/bio.h>
#include <openssl/cmp.h>
#include "../crypto/cmp/cmp_local.h"
#include <openssl/err.h>
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
FuzzerSetRand();
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
static int num_responses;
static OSSL_CMP_MSG *transfer_cb(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req)
{
if (num_responses++ > 2)
return NULL; /* prevent loops due to repeated pollRep */
return OSSL_CMP_MSG_dup((OSSL_CMP_MSG *)
OSSL_CMP_CTX_get_transfer_cb_arg(ctx));
}
static int print_noop(const char *func, const char *file, int line,
OSSL_CMP_severity level, const char *msg)
{
return 1;
}
static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *rep,
int invalid_protection, int expected_type)
{
return 1;
}
static void cmp_client_process_response(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
{
X509_NAME *name = X509_NAME_new();
ASN1_INTEGER *serial = ASN1_INTEGER_new();
ctx->unprotectedSend = 1; /* satisfy ossl_cmp_msg_protect() */
ctx->disableConfirm = 1; /* check just one response message */
ctx->popoMethod = OSSL_CRMF_POPO_NONE; /* satisfy ossl_cmp_certReq_new() */
ctx->oldCert = X509_new(); /* satisfy crm_new() and ossl_cmp_rr_new() */
if (!OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)"",
0) /* prevent too unspecific error */
|| ctx->oldCert == NULL
|| name == NULL || !X509_set_issuer_name(ctx->oldCert, name)
|| serial == NULL || !X509_set_serialNumber(ctx->oldCert, serial))
goto err;
(void)OSSL_CMP_CTX_set_transfer_cb(ctx, transfer_cb);
(void)OSSL_CMP_CTX_set_transfer_cb_arg(ctx, msg);
(void)OSSL_CMP_CTX_set_log_cb(ctx, print_noop);
num_responses = 0;
switch (msg->body != NULL ? msg->body->type : -1) {
case OSSL_CMP_PKIBODY_IP:
(void)OSSL_CMP_exec_IR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_CP:
(void)OSSL_CMP_exec_CR_ses(ctx);
(void)OSSL_CMP_exec_P10CR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_KUP:
(void)OSSL_CMP_exec_KUR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_POLLREP:
ctx->status = OSSL_CMP_PKISTATUS_waiting;
(void)OSSL_CMP_try_certreq(ctx, OSSL_CMP_PKIBODY_CR, NULL, NULL);
break;
case OSSL_CMP_PKIBODY_RP:
(void)OSSL_CMP_exec_RR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_GENP:
sk_OSSL_CMP_ITAV_pop_free(OSSL_CMP_exec_GENM_ses(ctx),
OSSL_CMP_ITAV_free);
break;
default:
(void)ossl_cmp_msg_check_update(ctx, msg, allow_unprotected, 0);
break;
}
err:
X509_NAME_free(name);
ASN1_INTEGER_free(serial);
}
static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *cert_req,
int certReqId,
const OSSL_CRMF_MSG *crm,
const X509_REQ *p10cr,
X509 **certOut,
STACK_OF(X509) **chainOut,
STACK_OF(X509) **caPubs)
{
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
return NULL;
}
static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *rr,
const X509_NAME *issuer,
const ASN1_INTEGER *serial)
{
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
return NULL;
}
static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *genm,
const STACK_OF(OSSL_CMP_ITAV) *in,
STACK_OF(OSSL_CMP_ITAV) **out)
{
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
return 0;
}
static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
const OSSL_CMP_PKISI *statusInfo,
const ASN1_INTEGER *errorCode,
const OSSL_CMP_PKIFREETEXT *errorDetails)
{
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
}
static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *certConf, int certReqId,
const ASN1_OCTET_STRING *certHash,
const OSSL_CMP_PKISI *si)
{
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
return 0;
}
static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *pollReq, int certReqId,
OSSL_CMP_MSG **certReq, int64_t *check_after)
{
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
return 0;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
OSSL_CMP_MSG *msg;
BIO *in;
if (len == 0)
return 0;
in = BIO_new(BIO_s_mem());
OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
msg = d2i_OSSL_CMP_MSG_bio(in, NULL);
if (msg != NULL) {
BIO *out = BIO_new(BIO_s_null());
OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new(NULL, NULL);
OSSL_CMP_CTX *client_ctx = OSSL_CMP_CTX_new(NULL, NULL);
i2d_OSSL_CMP_MSG_bio(out, msg);
ASN1_item_print(out, (ASN1_VALUE *)msg, 4,
ASN1_ITEM_rptr(OSSL_CMP_MSG), NULL);
BIO_free(out);
if (client_ctx != NULL)
cmp_client_process_response(client_ctx, msg);
if (srv_ctx != NULL
&& OSSL_CMP_CTX_set_log_cb(OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx),
print_noop)
&& OSSL_CMP_SRV_CTX_init(srv_ctx, NULL, process_cert_request,
process_rr, process_genm, process_error,
process_certConf, process_pollReq))
OSSL_CMP_MSG_free(OSSL_CMP_SRV_process_request(srv_ctx, msg));
OSSL_CMP_CTX_free(client_ctx);
OSSL_CMP_SRV_CTX_free(srv_ctx);
OSSL_CMP_MSG_free(msg);
}
BIO_free(in);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
FuzzerClearRand();
}
| 6,888 | 32.769608 | 79 | c |
openssl | openssl-master/fuzz/crl.c | /*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <openssl/x509.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
const unsigned char *p = buf;
unsigned char *der = NULL;
X509_CRL *crl = d2i_X509_CRL(NULL, &p, len);
if (crl != NULL) {
BIO *bio = BIO_new(BIO_s_null());
X509_CRL_print(bio, crl);
BIO_free(bio);
i2d_X509_CRL(crl, &der);
OPENSSL_free(der);
X509_CRL_free(crl);
}
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
}
| 1,092 | 21.770833 | 67 | c |
openssl | openssl-master/fuzz/ct.c | /*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* Fuzz the SCT parser.
*/
#include <stdio.h>
#include <openssl/ct.h>
#include <openssl/err.h>
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
CRYPTO_free_ex_index(0, -1);
ERR_clear_error();
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
const uint8_t **pp = &buf;
unsigned char *der = NULL;
STACK_OF(SCT) *scts = d2i_SCT_LIST(NULL, pp, len);
if (scts != NULL) {
BIO *bio = BIO_new(BIO_s_null());
SCT_LIST_print(scts, bio, 4, "\n", NULL);
BIO_free(bio);
if (i2d_SCT_LIST(scts, &der)) {
/* Silence unused result warning */
}
OPENSSL_free(der);
SCT_LIST_free(scts);
}
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
}
| 1,200 | 22.096154 | 67 | c |
openssl | openssl-master/fuzz/decoder.c | /*
* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <openssl/decoder.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "fuzzer.h"
static ASN1_PCTX *pctx;
int FuzzerInitialize(int *argc, char ***argv)
{
FuzzerSetRand();
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
| OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
pctx = ASN1_PCTX_new();
ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT
| ASN1_PCTX_FLAGS_SHOW_SEQUENCE
| ASN1_PCTX_FLAGS_SHOW_SSOF
| ASN1_PCTX_FLAGS_SHOW_TYPE
| ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT
| ASN1_STRFLGS_SHOW_TYPE
| ASN1_STRFLGS_DUMP_ALL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
OSSL_DECODER_CTX *dctx;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
BIO *bio;
bio = BIO_new(BIO_s_null());
dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, NULL, NULL, NULL, 0, NULL,
NULL);
if (dctx == NULL) {
return 0;
}
if (OSSL_DECODER_from_data(dctx, &buf, &len)) {
EVP_PKEY *pkey2;
EVP_PKEY_print_public(bio, pkey, 1, pctx);
EVP_PKEY_print_private(bio, pkey, 1, pctx);
EVP_PKEY_print_params(bio, pkey, 1, pctx);
pkey2 = EVP_PKEY_dup(pkey);
OPENSSL_assert(pkey2 != NULL);
EVP_PKEY_eq(pkey, pkey2);
EVP_PKEY_free(pkey2);
ctx = EVP_PKEY_CTX_new(pkey, NULL);
EVP_PKEY_param_check(ctx);
EVP_PKEY_public_check(ctx);
EVP_PKEY_private_check(ctx);
EVP_PKEY_pairwise_check(ctx);
OPENSSL_assert(ctx != NULL);
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
}
OSSL_DECODER_CTX_free(dctx);
BIO_free(bio);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
ASN1_PCTX_free(pctx);
FuzzerClearRand();
}
| 2,517 | 27.942529 | 74 | c |
openssl | openssl-master/fuzz/driver.c | /*
* Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <openssl/opensslconf.h>
#include "fuzzer.h"
#ifndef OPENSSL_NO_FUZZ_LIBFUZZER
int LLVMFuzzerInitialize(int *argc, char ***argv);
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
int LLVMFuzzerInitialize(int *argc, char ***argv)
{
return FuzzerInitialize(argc, argv);
}
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
{
return FuzzerTestOneInput(buf, len);
}
#elif !defined(OPENSSL_NO_FUZZ_AFL)
#define BUF_SIZE 65536
int main(int argc, char** argv)
{
FuzzerInitialize(&argc, &argv);
while (__AFL_LOOP(10000)) {
uint8_t *buf = malloc(BUF_SIZE);
size_t size = read(0, buf, BUF_SIZE);
FuzzerTestOneInput(buf, size);
free(buf);
}
FuzzerCleanup();
return 0;
}
#else
#error "Unsupported fuzzer"
#endif
| 1,213 | 20.678571 | 72 | c |
openssl | openssl-master/fuzz/fuzz_rand.c | /*
* Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <openssl/core_names.h>
#include <openssl/rand.h>
#include <openssl/provider.h>
#include "fuzzer.h"
static OSSL_FUNC_rand_newctx_fn fuzz_rand_newctx;
static OSSL_FUNC_rand_freectx_fn fuzz_rand_freectx;
static OSSL_FUNC_rand_instantiate_fn fuzz_rand_instantiate;
static OSSL_FUNC_rand_uninstantiate_fn fuzz_rand_uninstantiate;
static OSSL_FUNC_rand_generate_fn fuzz_rand_generate;
static OSSL_FUNC_rand_gettable_ctx_params_fn fuzz_rand_gettable_ctx_params;
static OSSL_FUNC_rand_get_ctx_params_fn fuzz_rand_get_ctx_params;
static OSSL_FUNC_rand_enable_locking_fn fuzz_rand_enable_locking;
static void *fuzz_rand_newctx(
void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch)
{
int *st = OPENSSL_malloc(sizeof(*st));
if (st != NULL)
*st = EVP_RAND_STATE_UNINITIALISED;
return st;
}
static void fuzz_rand_freectx(ossl_unused void *vrng)
{
OPENSSL_free(vrng);
}
static int fuzz_rand_instantiate(ossl_unused void *vrng,
ossl_unused unsigned int strength,
ossl_unused int prediction_resistance,
ossl_unused const unsigned char *pstr,
ossl_unused size_t pstr_len,
ossl_unused const OSSL_PARAM params[])
{
*(int *)vrng = EVP_RAND_STATE_READY;
return 1;
}
static int fuzz_rand_uninstantiate(ossl_unused void *vrng)
{
*(int *)vrng = EVP_RAND_STATE_UNINITIALISED;
return 1;
}
static int fuzz_rand_generate(ossl_unused void *vdrbg,
unsigned char *out, size_t outlen,
ossl_unused unsigned int strength,
ossl_unused int prediction_resistance,
ossl_unused const unsigned char *adin,
ossl_unused size_t adinlen)
{
unsigned char val = 1;
size_t i;
for (i = 0; i < outlen; i++)
out[i] = val++;
return 1;
}
static int fuzz_rand_enable_locking(ossl_unused void *vrng)
{
return 1;
}
static int fuzz_rand_get_ctx_params(void *vrng, OSSL_PARAM params[])
{
OSSL_PARAM *p;
p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
if (p != NULL && !OSSL_PARAM_set_int(p, *(int *)vrng))
return 0;
p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
if (p != NULL && !OSSL_PARAM_set_int(p, 500))
return 0;
p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
return 0;
return 1;
}
static const OSSL_PARAM *fuzz_rand_gettable_ctx_params(ossl_unused void *vrng,
ossl_unused void *provctx)
{
static const OSSL_PARAM known_gettable_ctx_params[] = {
OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
OSSL_PARAM_END
};
return known_gettable_ctx_params;
}
static const OSSL_DISPATCH fuzz_rand_functions[] = {
{ OSSL_FUNC_RAND_NEWCTX, (void (*)(void))fuzz_rand_newctx },
{ OSSL_FUNC_RAND_FREECTX, (void (*)(void))fuzz_rand_freectx },
{ OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))fuzz_rand_instantiate },
{ OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))fuzz_rand_uninstantiate },
{ OSSL_FUNC_RAND_GENERATE, (void (*)(void))fuzz_rand_generate },
{ OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))fuzz_rand_enable_locking },
{ OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
(void(*)(void))fuzz_rand_gettable_ctx_params },
{ OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))fuzz_rand_get_ctx_params },
OSSL_DISPATCH_END
};
static const OSSL_ALGORITHM fuzz_rand_rand[] = {
{ "fuzz", "provider=fuzz-rand", fuzz_rand_functions },
{ NULL, NULL, NULL }
};
static const OSSL_ALGORITHM *fuzz_rand_query(void *provctx,
int operation_id,
int *no_cache)
{
*no_cache = 0;
switch (operation_id) {
case OSSL_OP_RAND:
return fuzz_rand_rand;
}
return NULL;
}
/* Functions we provide to the core */
static const OSSL_DISPATCH fuzz_rand_method[] = {
{ OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fuzz_rand_query },
OSSL_DISPATCH_END
};
static int fuzz_rand_provider_init(const OSSL_CORE_HANDLE *handle,
const OSSL_DISPATCH *in,
const OSSL_DISPATCH **out, void **provctx)
{
*provctx = OSSL_LIB_CTX_new();
if (*provctx == NULL)
return 0;
*out = fuzz_rand_method;
return 1;
}
static OSSL_PROVIDER *r_prov;
void FuzzerSetRand(void)
{
if (!OSSL_PROVIDER_add_builtin(NULL, "fuzz-rand", fuzz_rand_provider_init)
|| !RAND_set_DRBG_type(NULL, "fuzz", NULL, NULL, NULL)
|| (r_prov = OSSL_PROVIDER_try_load(NULL, "fuzz-rand", 1)) == NULL)
exit(1);
}
void FuzzerClearRand(void)
{
OSSL_PROVIDER_unload(r_prov);
}
| 5,514 | 31.633136 | 81 | c |
openssl | openssl-master/fuzz/fuzzer.h | /*
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <stddef.h> /* for size_t */
#include <openssl/e_os2.h> /* for uint8_t */
int FuzzerTestOneInput(const uint8_t *buf, size_t len);
int FuzzerInitialize(int *argc, char ***argv);
void FuzzerCleanup(void);
void FuzzerSetRand(void);
void FuzzerClearRand(void);
| 640 | 31.05 | 72 | h |
openssl | openssl-master/fuzz/pem.c | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <openssl/pem.h>
#include <openssl/err.h>
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
BIO *in;
char *name = NULL, *header = NULL;
unsigned char *data = NULL;
long outlen;
if (len <= 1)
return 0;
in = BIO_new(BIO_s_mem());
OPENSSL_assert((size_t)BIO_write(in, buf + 1, len - 1) == len - 1);
if (PEM_read_bio_ex(in, &name, &header, &data, &outlen, buf[0]) == 1) {
/* Try to read all the data we get to see if allocated properly. */
BIO_write(in, name, strlen(name));
BIO_write(in, header, strlen(header));
BIO_write(in, data, outlen);
}
if (buf[0] & PEM_FLAG_SECURE) {
OPENSSL_secure_free(name);
OPENSSL_secure_free(header);
OPENSSL_secure_free(data);
} else {
OPENSSL_free(name);
OPENSSL_free(header);
OPENSSL_free(data);
}
BIO_free(in);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
}
| 1,506 | 24.116667 | 75 | c |
openssl | openssl-master/fuzz/punycode.c | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include "crypto/punycode.h"
#include "internal/nelem.h"
#include <openssl/crypto.h>
#include "fuzzer.h"
#include <stdio.h>
#include <string.h>
int FuzzerInitialize(int *argc, char ***argv)
{
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
char *b;
unsigned int out[16], outlen = OSSL_NELEM(out);
char outc[16];
b = OPENSSL_malloc(len + 1);
if (b != NULL) {
ossl_punycode_decode((const char *)buf, len, out, &outlen);
memcpy(b, buf, len);
b[len] = '\0';
ossl_a2ulabel(b, outc, sizeof(outc));
OPENSSL_free(b);
}
return 0;
}
void FuzzerCleanup(void)
{
}
| 982 | 21.860465 | 74 | c |
openssl | openssl-master/fuzz/smime.c | /*
* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include "fuzzer.h"
#include <openssl/err.h>
#include <openssl/pkcs7.h>
#include <openssl/x509.h>
#include <stdio.h>
int FuzzerInitialize(int *argc, char ***argv)
{
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
BIO *b = BIO_new_mem_buf(buf, len);
PKCS7 *p7 = SMIME_read_PKCS7(b, NULL);
if (p7 != NULL) {
STACK_OF(PKCS7_SIGNER_INFO) *p7si = PKCS7_get_signer_info(p7);
int i;
for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(p7si); i++) {
STACK_OF(X509_ALGOR) *algs;
PKCS7_cert_from_signer_info(p7,
sk_PKCS7_SIGNER_INFO_value(p7si, i));
algs = PKCS7_get_smimecap(sk_PKCS7_SIGNER_INFO_value(p7si, i));
sk_X509_ALGOR_pop_free(algs, X509_ALGOR_free);
}
PKCS7_free(p7);
}
BIO_free(b);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
}
| 1,270 | 24.42 | 77 | c |
openssl | openssl-master/fuzz/test-corpus.c | /*
* Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* Given a list of files, run each of them through the fuzzer. Note that
* failure will be indicated by some kind of crash. Switching on things like
* asan improves the test.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <openssl/crypto.h>
#include "fuzzer.h"
#include "internal/o_dir.h"
#if defined(_WIN32) && defined(_MAX_PATH) && !defined(PATH_MAX)
# define PATH_MAX _MAX_PATH
#endif
#ifndef PATH_MAX
# define PATH_MAX 4096
#endif
# if !defined(S_ISREG)
# define S_ISREG(m) ((m) & S_IFREG)
# endif
static void testfile(const char *pathname)
{
struct stat st;
FILE *f;
unsigned char *buf;
size_t s;
if (stat(pathname, &st) < 0 || !S_ISREG(st.st_mode))
return;
printf("# %s\n", pathname);
fflush(stdout);
f = fopen(pathname, "rb");
if (f == NULL)
return;
buf = malloc(st.st_size);
if (buf != NULL) {
s = fread(buf, 1, st.st_size, f);
OPENSSL_assert(s == (size_t)st.st_size);
FuzzerTestOneInput(buf, s);
free(buf);
}
fclose(f);
}
int main(int argc, char **argv) {
int n;
FuzzerInitialize(&argc, &argv);
for (n = 1; n < argc; ++n) {
size_t dirname_len = strlen(argv[n]);
const char *filename = NULL;
char *pathname = NULL;
OPENSSL_DIR_CTX *ctx = NULL;
int wasdir = 0;
/*
* We start with trying to read the given path as a directory.
*/
while ((filename = OPENSSL_DIR_read(&ctx, argv[n])) != NULL) {
wasdir = 1;
if (pathname == NULL) {
pathname = malloc(PATH_MAX);
if (pathname == NULL)
break;
strcpy(pathname, argv[n]);
#ifdef __VMS
if (strchr(":<]", pathname[dirname_len - 1]) == NULL)
#endif
pathname[dirname_len++] = '/';
pathname[dirname_len] = '\0';
}
strcpy(pathname + dirname_len, filename);
testfile(pathname);
}
OPENSSL_DIR_end(&ctx);
/* If it wasn't a directory, treat it as a file instead */
if (!wasdir)
testfile(argv[n]);
free(pathname);
}
FuzzerCleanup();
return 0;
}
| 2,635 | 24.104762 | 76 | c |
openssl | openssl-master/fuzz/v3name.c | /*
* Copyright 2012-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <string.h>
#include <openssl/e_os2.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "internal/nelem.h"
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
return 1;
}
int FuzzerTestOneInput(const uint8_t* data, size_t size){
GENERAL_NAME *namesa;
GENERAL_NAME *namesb;
const unsigned char *derp = data;
/*
* We create two versions of each GENERAL_NAME so that we ensure when
* we compare them they are always different pointers.
*/
namesa = d2i_GENERAL_NAME(NULL, &derp, size);
derp = data;
namesb = d2i_GENERAL_NAME(NULL, &derp, size);
GENERAL_NAME_cmp(namesa, namesb);
if (namesa != NULL)
GENERAL_NAME_free(namesa);
if (namesb != NULL)
GENERAL_NAME_free(namesb);
return 0;
}
void FuzzerCleanup(void)
{
}
| 1,168 | 24.977778 | 74 | c |
openssl | openssl-master/fuzz/x509.c | /*
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
#include <openssl/x509.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "fuzzer.h"
int FuzzerInitialize(int *argc, char ***argv)
{
FuzzerSetRand();
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
return 1;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
const unsigned char *p = buf;
unsigned char *der = NULL;
X509 *x509 = d2i_X509(NULL, &p, len);
if (x509 != NULL) {
BIO *bio = BIO_new(BIO_s_null());
/* This will load and print the public key as well as extensions */
X509_print(bio, x509);
BIO_free(bio);
X509_issuer_and_serial_hash(x509);
i2d_X509(x509, &der);
OPENSSL_free(der);
X509_free(x509);
}
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
FuzzerClearRand();
}
| 1,271 | 23 | 75 | c |
openssl | openssl-master/include/crypto/aria.h | /*
* Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Copyright (c) 2017 National Security Research Institute. All rights reserved. */
#ifndef OSSL_CRYPTO_ARIA_H
# define OSSL_CRYPTO_ARIA_H
# pragma once
# include <openssl/opensslconf.h>
# ifdef OPENSSL_NO_ARIA
# error ARIA is disabled.
# endif
# define ARIA_ENCRYPT 1
# define ARIA_DECRYPT 0
# define ARIA_BLOCK_SIZE 16 /* Size of each encryption/decryption block */
# define ARIA_MAX_KEYS 17 /* Number of keys needed in the worst case */
typedef union {
unsigned char c[ARIA_BLOCK_SIZE];
unsigned int u[ARIA_BLOCK_SIZE / sizeof(unsigned int)];
} ARIA_u128;
typedef unsigned char ARIA_c128[ARIA_BLOCK_SIZE];
struct aria_key_st {
ARIA_u128 rd_key[ARIA_MAX_KEYS];
unsigned int rounds;
};
typedef struct aria_key_st ARIA_KEY;
int ossl_aria_set_encrypt_key(const unsigned char *userKey, const int bits,
ARIA_KEY *key);
int ossl_aria_set_decrypt_key(const unsigned char *userKey, const int bits,
ARIA_KEY *key);
void ossl_aria_encrypt(const unsigned char *in, unsigned char *out,
const ARIA_KEY *key);
#endif
| 1,550 | 28.826923 | 85 | h |
openssl | openssl-master/include/crypto/asn1.h | /*
* Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ASN1_H
# define OSSL_CRYPTO_ASN1_H
# pragma once
# include <openssl/asn1.h>
# include <openssl/core_dispatch.h> /* OSSL_FUNC_keymgmt_import() */
/* Internal ASN1 structures and functions: not for application use */
/* ASN1 public key method structure */
#include <openssl/core.h>
struct evp_pkey_asn1_method_st {
int pkey_id;
int pkey_base_id;
unsigned long pkey_flags;
char *pem_str;
char *info;
int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub);
int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx);
int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx);
int (*pkey_size) (const EVP_PKEY *pk);
int (*pkey_bits) (const EVP_PKEY *pk);
int (*pkey_security_bits) (const EVP_PKEY *pk);
int (*param_decode) (EVP_PKEY *pkey,
const unsigned char **pder, int derlen);
int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
int (*param_missing) (const EVP_PKEY *pk);
int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx);
int (*sig_print) (BIO *out,
const X509_ALGOR *sigalg, const ASN1_STRING *sig,
int indent, ASN1_PCTX *pctx);
void (*pkey_free) (EVP_PKEY *pkey);
int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
/* Legacy functions for old PEM */
int (*old_priv_decode) (EVP_PKEY *pkey,
const unsigned char **pder, int derlen);
int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
/* Custom ASN1 signature verification */
int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
const X509_ALGOR *a, const ASN1_BIT_STRING *sig,
EVP_PKEY *pkey);
int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
X509_ALGOR *alg1, X509_ALGOR *alg2,
ASN1_BIT_STRING *sig);
int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
const ASN1_STRING *sig);
/* Check */
int (*pkey_check) (const EVP_PKEY *pk);
int (*pkey_public_check) (const EVP_PKEY *pk);
int (*pkey_param_check) (const EVP_PKEY *pk);
/* Get/set raw private/public key data */
int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
int (*get_priv_key) (const EVP_PKEY *pk, unsigned char *priv, size_t *len);
int (*get_pub_key) (const EVP_PKEY *pk, unsigned char *pub, size_t *len);
/* Exports and imports to / from providers */
size_t (*dirty_cnt) (const EVP_PKEY *pk);
int (*export_to) (const EVP_PKEY *pk, void *to_keydata,
OSSL_FUNC_keymgmt_import_fn *importer,
OSSL_LIB_CTX *libctx, const char *propq);
OSSL_CALLBACK *import_from;
int (*copy) (EVP_PKEY *to, EVP_PKEY *from);
int (*priv_decode_ex) (EVP_PKEY *pk,
const PKCS8_PRIV_KEY_INFO *p8inf,
OSSL_LIB_CTX *libctx,
const char *propq);
} /* EVP_PKEY_ASN1_METHOD */ ;
DEFINE_STACK_OF_CONST(EVP_PKEY_ASN1_METHOD)
extern const EVP_PKEY_ASN1_METHOD ossl_dh_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_dhx_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5];
extern const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_ecx25519_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_ecx448_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_ed25519_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_ed448_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2];
extern const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth;
/*
* These are used internally in the ASN1_OBJECT to keep track of whether the
* names and data need to be free()ed
*/
# define ASN1_OBJECT_FLAG_DYNAMIC 0x01/* internal use */
# define ASN1_OBJECT_FLAG_CRITICAL 0x02/* critical x509v3 object id */
# define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04/* internal use */
# define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08/* internal use */
struct asn1_object_st {
const char *sn, *ln;
int nid;
int length;
const unsigned char *data; /* data remains const after init */
int flags; /* Should we free this one */
};
/* ASN1 print context structure */
struct asn1_pctx_st {
unsigned long flags;
unsigned long nm_flags;
unsigned long cert_flags;
unsigned long oid_flags;
unsigned long str_flags;
} /* ASN1_PCTX */ ;
/* ASN1 type functions */
int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
unsigned char *data, int len);
int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
unsigned char *data, int max_len);
int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md);
const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg);
X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg);
int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md);
int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags);
EVP_PKEY * ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a,
const unsigned char **pp, long length,
OSSL_LIB_CTX *libctx, const char *propq);
X509_ALGOR *ossl_X509_ALGOR_from_nid(int nid, int ptype, void *pval);
time_t ossl_asn1_string_to_time_t(const char *asn1_string);
void ossl_asn1_string_set_bits_left(ASN1_STRING *str, unsigned int num);
#endif /* ndef OSSL_CRYPTO_ASN1_H */
| 6,682 | 42.396104 | 79 | h |
openssl | openssl-master/include/crypto/asn1_dsa.h | /*
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ASN1_DSA_H
# define OSSL_CRYPTO_ASN1_DSA_H
# pragma once
#include "internal/packet.h"
int ossl_encode_der_length(WPACKET *pkt, size_t cont_len);
int ossl_encode_der_integer(WPACKET *pkt, const BIGNUM *n);
int ossl_encode_der_dsa_sig(WPACKET *pkt, const BIGNUM *r, const BIGNUM *s);
int ossl_decode_der_length(PACKET *pkt, PACKET *subpkt);
int ossl_decode_der_integer(PACKET *pkt, BIGNUM *n);
size_t ossl_decode_der_dsa_sig(BIGNUM *r, BIGNUM *s, const unsigned char **ppin,
size_t len);
#endif
| 884 | 34.4 | 80 | h |
openssl | openssl-master/include/crypto/asn1err.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ASN1ERR_H
# define OSSL_CRYPTO_ASN1ERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_ASN1_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 641 | 21.928571 | 74 | h |
openssl | openssl-master/include/crypto/asyncerr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ASYNCERR_H
# define OSSL_CRYPTO_ASYNCERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_ASYNC_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 644 | 22.035714 | 74 | h |
openssl | openssl-master/include/crypto/bioerr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_BIOERR_H
# define OSSL_CRYPTO_BIOERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_BIO_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 638 | 21.821429 | 74 | h |
openssl | openssl-master/include/crypto/bn.h | /*
* Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_BN_H
# define OSSL_CRYPTO_BN_H
# pragma once
# include <openssl/bn.h>
# include <limits.h>
BIGNUM *bn_wexpand(BIGNUM *a, int words);
BIGNUM *bn_expand2(BIGNUM *a, int words);
void bn_correct_top(BIGNUM *a);
/*
* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
* This is an array r[] of values that are either zero or odd with an
* absolute value less than 2^w satisfying scalar = \sum_j r[j]*2^j where at
* most one of any w+1 consecutive digits is non-zero with the exception that
* the most significant digit may be only w-1 zeros away from that next
* non-zero digit.
*/
signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len);
int bn_get_top(const BIGNUM *a);
int bn_get_dmax(const BIGNUM *a);
/* Set all words to zero */
void bn_set_all_zero(BIGNUM *a);
/*
* Copy the internal BIGNUM words into out which holds size elements (and size
* must be bigger than top)
*/
int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size);
BN_ULONG *bn_get_words(const BIGNUM *a);
/*
* Set the internal data words in a to point to words which contains size
* elements. The BN_FLG_STATIC_DATA flag is set
*/
void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size);
/*
* Copy words into the BIGNUM |a|, reallocating space as necessary.
* The negative flag of |a| is not modified.
* Returns 1 on success and 0 on failure.
*/
/*
* |num_words| is int because bn_expand2 takes an int. This is an internal
* function so we simply trust callers not to pass negative values.
*/
int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words);
/*
* Some BIGNUM functions assume most significant limb to be non-zero, which
* is customarily arranged by bn_correct_top. Output from below functions
* is not processed with bn_correct_top, and for this reason it may not be
* returned out of public API. It may only be passed internally into other
* functions known to support non-minimal or zero-padded BIGNUMs. Even
* though the goal is to facilitate constant-time-ness, not each subroutine
* is constant-time by itself. They all have pre-conditions, consult source
* code...
*/
int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
BN_MONT_CTX *mont, BN_CTX *ctx);
int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx);
int bn_from_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx);
int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m);
int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m);
int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n);
int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n);
int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
const BIGNUM *d, BN_CTX *ctx);
#define BN_PRIMETEST_COMPOSITE 0
#define BN_PRIMETEST_COMPOSITE_WITH_FACTOR 1
#define BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME 2
#define BN_PRIMETEST_PROBABLY_PRIME 3
int ossl_bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
BN_GENCB *cb, int enhanced, int *status);
int ossl_bn_check_generated_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
BN_GENCB *cb);
const BIGNUM *ossl_bn_get0_small_factors(void);
int ossl_bn_rsa_fips186_4_gen_prob_primes(BIGNUM *p, BIGNUM *Xpout,
BIGNUM *p1, BIGNUM *p2,
const BIGNUM *Xp, const BIGNUM *Xp1,
const BIGNUM *Xp2, int nlen,
const BIGNUM *e, BN_CTX *ctx,
BN_GENCB *cb);
int ossl_bn_rsa_fips186_4_derive_prime(BIGNUM *Y, BIGNUM *X, const BIGNUM *Xin,
const BIGNUM *r1, const BIGNUM *r2,
int nlen, const BIGNUM *e, BN_CTX *ctx,
BN_GENCB *cb);
OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx);
extern const BIGNUM ossl_bn_inv_sqrt_2;
#if defined(OPENSSL_SYS_LINUX) && !defined(FIPS_MODULE) && defined (__s390x__)
# define S390X_MOD_EXP
#endif
int s390x_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
int s390x_crt(BIGNUM *r, const BIGNUM *i, const BIGNUM *p, const BIGNUM *q,
const BIGNUM *dmp, const BIGNUM *dmq, const BIGNUM *iqmp);
#endif
| 5,203 | 39.341085 | 79 | h |
openssl | openssl-master/include/crypto/bn_dh.h | /*
* Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#define declare_dh_bn(x) \
extern const BIGNUM ossl_bignum_dh##x##_p; \
extern const BIGNUM ossl_bignum_dh##x##_q; \
extern const BIGNUM ossl_bignum_dh##x##_g; \
declare_dh_bn(1024_160)
declare_dh_bn(2048_224)
declare_dh_bn(2048_256)
extern const BIGNUM ossl_bignum_const_2;
extern const BIGNUM ossl_bignum_ffdhe2048_p;
extern const BIGNUM ossl_bignum_ffdhe3072_p;
extern const BIGNUM ossl_bignum_ffdhe4096_p;
extern const BIGNUM ossl_bignum_ffdhe6144_p;
extern const BIGNUM ossl_bignum_ffdhe8192_p;
extern const BIGNUM ossl_bignum_ffdhe2048_q;
extern const BIGNUM ossl_bignum_ffdhe3072_q;
extern const BIGNUM ossl_bignum_ffdhe4096_q;
extern const BIGNUM ossl_bignum_ffdhe6144_q;
extern const BIGNUM ossl_bignum_ffdhe8192_q;
extern const BIGNUM ossl_bignum_modp_1536_p;
extern const BIGNUM ossl_bignum_modp_2048_p;
extern const BIGNUM ossl_bignum_modp_3072_p;
extern const BIGNUM ossl_bignum_modp_4096_p;
extern const BIGNUM ossl_bignum_modp_6144_p;
extern const BIGNUM ossl_bignum_modp_8192_p;
extern const BIGNUM ossl_bignum_modp_1536_q;
extern const BIGNUM ossl_bignum_modp_2048_q;
extern const BIGNUM ossl_bignum_modp_3072_q;
extern const BIGNUM ossl_bignum_modp_4096_q;
extern const BIGNUM ossl_bignum_modp_6144_q;
extern const BIGNUM ossl_bignum_modp_8192_q;
| 1,657 | 36.681818 | 74 | h |
openssl | openssl-master/include/crypto/bn_srp.h | /*
* Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OPENSSL_NO_SRP
extern const BIGNUM ossl_bn_group_1024;
extern const BIGNUM ossl_bn_group_1536;
extern const BIGNUM ossl_bn_group_2048;
extern const BIGNUM ossl_bn_group_3072;
extern const BIGNUM ossl_bn_group_4096;
extern const BIGNUM ossl_bn_group_6144;
extern const BIGNUM ossl_bn_group_8192;
extern const BIGNUM ossl_bn_generator_19;
extern const BIGNUM ossl_bn_generator_5;
extern const BIGNUM ossl_bn_generator_2;
#endif
| 782 | 22.727273 | 74 | h |
openssl | openssl-master/include/crypto/bnerr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_BNERR_H
# define OSSL_CRYPTO_BNERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_BN_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 635 | 21.714286 | 74 | h |
openssl | openssl-master/include/crypto/buffererr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_BUFFERERR_H
# define OSSL_CRYPTO_BUFFERERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_BUF_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 644 | 22.035714 | 74 | h |
openssl | openssl-master/include/crypto/chacha.h | /*
* Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CHACHA_H
#define OSSL_CRYPTO_CHACHA_H
# pragma once
#include <stddef.h>
/*
* ChaCha20_ctr32 encrypts |len| bytes from |inp| with the given key and
* nonce and writes the result to |out|, which may be equal to |inp|.
* The |key| is not 32 bytes of verbatim key material though, but the
* said material collected into 8 32-bit elements array in host byte
* order. Same approach applies to nonce: the |counter| argument is
* pointer to concatenated nonce and counter values collected into 4
* 32-bit elements. This, passing crypto material collected into 32-bit
* elements as opposite to passing verbatim byte vectors, is chosen for
* efficiency in multi-call scenarios.
*/
void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
size_t len, const unsigned int key[8],
const unsigned int counter[4]);
/*
* You can notice that there is no key setup procedure. Because it's
* as trivial as collecting bytes into 32-bit elements, it's reckoned
* that below macro is sufficient.
*/
#define CHACHA_U8TOU32(p) ( \
((unsigned int)(p)[0]) | ((unsigned int)(p)[1]<<8) | \
((unsigned int)(p)[2]<<16) | ((unsigned int)(p)[3]<<24) )
#define CHACHA_KEY_SIZE 32
#define CHACHA_CTR_SIZE 16
#define CHACHA_BLK_SIZE 64
#endif
| 1,693 | 37.5 | 74 | h |
openssl | openssl-master/include/crypto/cmll_platform.h | /*
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CMLL_PLATFORM_H
# define OSSL_CMLL_PLATFORM_H
# pragma once
# if defined(CMLL_ASM) && (defined(__sparc) || defined(__sparc__))
/* Fujitsu SPARC64 X support */
# include "crypto/sparc_arch.h"
# ifndef OPENSSL_NO_CAMELLIA
# define SPARC_CMLL_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_CAMELLIA)
# include <openssl/camellia.h>
void cmll_t4_set_key(const unsigned char *key, int bits, CAMELLIA_KEY *ks);
void cmll_t4_encrypt(const unsigned char *in, unsigned char *out,
const CAMELLIA_KEY *key);
void cmll_t4_decrypt(const unsigned char *in, unsigned char *out,
const CAMELLIA_KEY *key);
void cmll128_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const CAMELLIA_KEY *key,
unsigned char *ivec, int /*unused*/);
void cmll128_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
size_t len, const CAMELLIA_KEY *key,
unsigned char *ivec, int /*unused*/);
void cmll256_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const CAMELLIA_KEY *key,
unsigned char *ivec, int /*unused*/);
void cmll256_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
size_t len, const CAMELLIA_KEY *key,
unsigned char *ivec, int /*unused*/);
void cmll128_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
size_t blocks, const CAMELLIA_KEY *key,
unsigned char *ivec);
void cmll256_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
size_t blocks, const CAMELLIA_KEY *key,
unsigned char *ivec);
# endif /* OPENSSL_NO_CAMELLIA */
# endif /* CMLL_ASM && sparc */
#endif /* OSSL_CRYPTO_CIPHERMODE_PLATFORM_H */
| 2,314 | 43.519231 | 75 | h |
openssl | openssl-master/include/crypto/cmperr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CMPERR_H
# define OSSL_CRYPTO_CMPERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_CMP
int ossl_err_load_CMP_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 671 | 20.677419 | 74 | h |
openssl | openssl-master/include/crypto/cmserr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CMSERR_H
# define OSSL_CRYPTO_CMSERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_CMS
int ossl_err_load_CMS_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 671 | 20.677419 | 74 | h |
openssl | openssl-master/include/crypto/comperr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_COMPERR_H
# define OSSL_CRYPTO_COMPERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_COMP
int ossl_err_load_COMP_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 675 | 20.806452 | 74 | h |
openssl | openssl-master/include/crypto/conferr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CONFERR_H
# define OSSL_CRYPTO_CONFERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_CONF_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 641 | 21.928571 | 74 | h |
openssl | openssl-master/include/crypto/context.h | /*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#include <openssl/core.h>
void *ossl_provider_store_new(OSSL_LIB_CTX *);
void *ossl_property_string_data_new(OSSL_LIB_CTX *);
void *ossl_stored_namemap_new(OSSL_LIB_CTX *);
void *ossl_property_defns_new(OSSL_LIB_CTX *);
void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *);
void *ossl_rand_ctx_new(OSSL_LIB_CTX *);
void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *);
void *ossl_bio_core_globals_new(OSSL_LIB_CTX *);
void *ossl_child_prov_ctx_new(OSSL_LIB_CTX *);
void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *);
void *ossl_self_test_set_callback_new(OSSL_LIB_CTX *);
void *ossl_rand_crng_ctx_new(OSSL_LIB_CTX *);
void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *);
void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *);
#if defined(OPENSSL_THREADS)
void *ossl_threads_ctx_new(OSSL_LIB_CTX *);
#endif
void ossl_provider_store_free(void *);
void ossl_property_string_data_free(void *);
void ossl_stored_namemap_free(void *);
void ossl_property_defns_free(void *);
void ossl_ctx_global_properties_free(void *);
void ossl_rand_ctx_free(void *);
void ossl_prov_conf_ctx_free(void *);
void ossl_bio_core_globals_free(void *);
void ossl_child_prov_ctx_free(void *);
void ossl_prov_drbg_nonce_ctx_free(void *);
void ossl_self_test_set_callback_free(void *);
void ossl_rand_crng_ctx_free(void *);
void ossl_thread_event_ctx_free(void *);
void ossl_fips_prov_ossl_ctx_free(void *);
void ossl_release_default_drbg_ctx(void);
#if defined(OPENSSL_THREADS)
void ossl_threads_ctx_free(void *);
#endif
| 1,810 | 36.729167 | 74 | h |
openssl | openssl-master/include/crypto/crmferr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CRMFERR_H
# define OSSL_CRYPTO_CRMFERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_CRMF
int ossl_err_load_CRMF_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 675 | 20.806452 | 74 | h |
openssl | openssl-master/include/crypto/cryptlib.h | /*
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CRYPTLIB_H
# define OSSL_CRYPTO_CRYPTLIB_H
# pragma once
# include <openssl/core.h>
# include "internal/cryptlib.h"
/* This file is not scanned by mkdef.pl, whereas cryptlib.h is */
int ossl_init_thread_start(const void *index, void *arg,
OSSL_thread_stop_handler_fn handfn);
int ossl_init_thread_deregister(void *index);
int ossl_init_thread(void);
void ossl_cleanup_thread(void);
void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx);
/*
* OPENSSL_INIT flags. The primary list of these is in crypto.h. Flags below
* are those omitted from crypto.h because they are "reserved for internal
* use".
*/
# define OPENSSL_INIT_BASE_ONLY 0x00040000L
void ossl_trace_cleanup(void);
void ossl_malloc_setup_failures(void);
int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj,
CRYPTO_EX_DATA *ad, int idx);
#endif /* OSSL_CRYPTO_CRYPTLIB_H */
| 1,283 | 31.1 | 76 | h |
openssl | openssl-master/include/crypto/cryptoerr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CRYPTOERR_H
# define OSSL_CRYPTO_CRYPTOERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_CRYPTO_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 647 | 22.142857 | 74 | h |
openssl | openssl-master/include/crypto/cterr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_CTERR_H
# define OSSL_CRYPTO_CTERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_CT
int ossl_err_load_CT_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 667 | 20.548387 | 74 | h |
openssl | openssl-master/include/crypto/ctype.h | /*
* Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/*
* This version of ctype.h provides a standardised and platform
* independent implementation that supports seven bit ASCII characters.
* The specific intent is to not pass extended ASCII characters (> 127)
* even if the host operating system would.
*
* There is EBCDIC support included for machines which use this. However,
* there are a number of concerns about how well EBCDIC is supported
* throughout the rest of the source code. Refer to issue #4154 for
* details.
*/
#ifndef OSSL_CRYPTO_CTYPE_H
# define OSSL_CRYPTO_CTYPE_H
# pragma once
# include <openssl/e_os2.h>
# define CTYPE_MASK_lower 0x1
# define CTYPE_MASK_upper 0x2
# define CTYPE_MASK_digit 0x4
# define CTYPE_MASK_space 0x8
# define CTYPE_MASK_xdigit 0x10
# define CTYPE_MASK_blank 0x20
# define CTYPE_MASK_cntrl 0x40
# define CTYPE_MASK_graph 0x80
# define CTYPE_MASK_print 0x100
# define CTYPE_MASK_punct 0x200
# define CTYPE_MASK_base64 0x400
# define CTYPE_MASK_asn1print 0x800
# define CTYPE_MASK_alpha (CTYPE_MASK_lower | CTYPE_MASK_upper)
# define CTYPE_MASK_alnum (CTYPE_MASK_alpha | CTYPE_MASK_digit)
/*
* The ascii mask assumes that any other classification implies that
* the character is ASCII and that there are no ASCII characters
* that aren't in any of the classifications.
*
* This assumption holds at the moment, but it might not in the future.
*/
# define CTYPE_MASK_ascii (~0)
# ifdef CHARSET_EBCDIC
int ossl_toascii(int c);
int ossl_fromascii(int c);
# else
# define ossl_toascii(c) (c)
# define ossl_fromascii(c) (c)
# endif
int ossl_ctype_check(int c, unsigned int mask);
int ossl_tolower(int c);
int ossl_toupper(int c);
int ossl_isdigit(int c);
int ossl_islower(int c);
int ossl_isupper(int c);
int ossl_ascii_isdigit(int c);
# define ossl_isalnum(c) (ossl_ctype_check((c), CTYPE_MASK_alnum))
# define ossl_isalpha(c) (ossl_ctype_check((c), CTYPE_MASK_alpha))
# ifdef CHARSET_EBCDIC
# define ossl_isascii(c) (ossl_ctype_check((c), CTYPE_MASK_ascii))
# else
# define ossl_isascii(c) (((c) & ~127) == 0)
# endif
# define ossl_isblank(c) (ossl_ctype_check((c), CTYPE_MASK_blank))
# define ossl_iscntrl(c) (ossl_ctype_check((c), CTYPE_MASK_cntrl))
# define ossl_isgraph(c) (ossl_ctype_check((c), CTYPE_MASK_graph))
# define ossl_isprint(c) (ossl_ctype_check((c), CTYPE_MASK_print))
# define ossl_ispunct(c) (ossl_ctype_check((c), CTYPE_MASK_punct))
# define ossl_isspace(c) (ossl_ctype_check((c), CTYPE_MASK_space))
# define ossl_isxdigit(c) (ossl_ctype_check((c), CTYPE_MASK_xdigit))
# define ossl_isbase64(c) (ossl_ctype_check((c), CTYPE_MASK_base64))
# define ossl_isasn1print(c) (ossl_ctype_check((c), CTYPE_MASK_asn1print))
#endif
| 3,160 | 35.333333 | 77 | h |
openssl | openssl-master/include/crypto/decoder.h | /*
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_DECODER_H
# define OSSL_CRYPTO_DECODER_H
# pragma once
# include <openssl/decoder.h>
/*
* These are specially made for the 'file:' provider-native loader, which
* uses this to install a DER to anything decoder, which doesn't do much
* except read a DER blob and pass it on as a provider object abstraction
* (provider-object(7)).
*/
void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
OSSL_PROVIDER *prov);
OSSL_DECODER_INSTANCE *
ossl_decoder_instance_new(OSSL_DECODER *decoder, void *decoderctx);
void ossl_decoder_instance_free(OSSL_DECODER_INSTANCE *decoder_inst);
OSSL_DECODER_INSTANCE *ossl_decoder_instance_dup(const OSSL_DECODER_INSTANCE *src);
int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx,
OSSL_DECODER_INSTANCE *di);
int ossl_decoder_get_number(const OSSL_DECODER *encoder);
int ossl_decoder_store_cache_flush(OSSL_LIB_CTX *libctx);
int ossl_decoder_store_remove_all_provided(const OSSL_PROVIDER *prov);
#endif
| 1,396 | 36.756757 | 83 | h |
openssl | openssl-master/include/crypto/decodererr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_DECODERERR_H
# define OSSL_CRYPTO_DECODERERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_OSSL_DECODER_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 655 | 22.428571 | 74 | h |
openssl | openssl-master/include/crypto/des_platform.h | /*
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_DES_PLATFORM_H
# define OSSL_DES_PLATFORM_H
# pragma once
# if defined(DES_ASM) && (defined(__sparc) || defined(__sparc__))
/* Fujitsu SPARC64 X support */
# include "crypto/sparc_arch.h"
# ifndef OPENSSL_NO_DES
# define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES)
# include <openssl/des.h>
void des_t4_key_expand(const void *key, DES_key_schedule *ks);
void des_t4_ede3_cbc_encrypt(const void *inp, void *out, size_t len,
const DES_key_schedule ks[3], unsigned char iv[8]);
void des_t4_ede3_cbc_decrypt(const void *inp, void *out, size_t len,
const DES_key_schedule ks[3], unsigned char iv[8]);
void des_t4_cbc_encrypt(const void *inp, void *out, size_t len,
const DES_key_schedule *ks, unsigned char iv[8]);
void des_t4_cbc_decrypt(const void *inp, void *out, size_t len,
const DES_key_schedule *ks, unsigned char iv[8]);
# endif /* OPENSSL_NO_DES */
# endif /* DES_ASM && sparc */
#endif /* OSSL_CRYPTO_CIPHERMODE_PLATFORM_H */
| 1,414 | 38.305556 | 80 | h |
openssl | openssl-master/include/crypto/dh.h | /*
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_DH_H
# define OSSL_CRYPTO_DH_H
# pragma once
# include <openssl/core.h>
# include <openssl/params.h>
# include <openssl/dh.h>
# include "internal/ffc.h"
DH *ossl_dh_new_by_nid_ex(OSSL_LIB_CTX *libctx, int nid);
DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx);
void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx);
int ossl_dh_generate_ffc_parameters(DH *dh, int type, int pbits, int qbits,
BN_GENCB *cb);
int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh,
const BIGNUM *priv_key, BIGNUM *pub_key);
int ossl_dh_get_named_group_uid_from_size(int pbits);
const char *ossl_dh_gen_type_id2name(int id);
int ossl_dh_gen_type_name2id(const char *name, int type);
void ossl_dh_cache_named_group(DH *dh);
int ossl_dh_is_named_safe_prime_group(const DH *dh);
FFC_PARAMS *ossl_dh_get0_params(DH *dh);
int ossl_dh_get0_nid(const DH *dh);
int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[]);
int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[], int include_private);
int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[]);
int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
int include_private);
DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
OSSL_LIB_CTX *libctx, const char *propq);
int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret);
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret);
int ossl_dh_check_pairwise(const DH *dh);
const DH_METHOD *ossl_dh_get_method(const DH *dh);
int ossl_dh_buf2key(DH *key, const unsigned char *buf, size_t len);
size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf, size_t size,
int alloc);
int ossl_dh_kdf_X9_42_asn1(unsigned char *out, size_t outlen,
const unsigned char *Z, size_t Zlen,
const char *cek_alg,
const unsigned char *ukm, size_t ukmlen,
const EVP_MD *md,
OSSL_LIB_CTX *libctx, const char *propq);
int ossl_dh_is_foreign(const DH *dh);
DH *ossl_dh_dup(const DH *dh, int selection);
#endif /* OSSL_CRYPTO_DH_H */
| 2,705 | 41.952381 | 81 | h |
openssl | openssl-master/include/crypto/dherr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_DHERR_H
# define OSSL_CRYPTO_DHERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_DH
int ossl_err_load_DH_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 667 | 20.548387 | 74 | h |
openssl | openssl-master/include/crypto/dsa.h | /*
* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_DSA_H
# define OSSL_CRYPTO_DSA_H
# pragma once
# include <openssl/core.h>
# include <openssl/dsa.h>
# include "internal/ffc.h"
#define DSA_PARAMGEN_TYPE_FIPS_186_4 0 /* Use FIPS186-4 standard */
#define DSA_PARAMGEN_TYPE_FIPS_186_2 1 /* Use legacy FIPS186-2 standard */
#define DSA_PARAMGEN_TYPE_FIPS_DEFAULT 2
DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx);
void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx);
int ossl_dsa_generate_ffc_parameters(DSA *dsa, int type, int pbits, int qbits,
BN_GENCB *cb);
int ossl_dsa_sign_int(int type, const unsigned char *dgst, int dlen,
unsigned char *sig, unsigned int *siglen, DSA *dsa,
unsigned int nonce_type, const char *digestname,
OSSL_LIB_CTX *libctx, const char *propq);
FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa);
int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[]);
int ossl_dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[],
int include_private);
DSA *ossl_dsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
OSSL_LIB_CTX *libctx, const char *propq);
int ossl_dsa_generate_public_key(BN_CTX *ctx, const DSA *dsa,
const BIGNUM *priv_key, BIGNUM *pub_key);
int ossl_dsa_check_params(const DSA *dsa, int checktype, int *ret);
int ossl_dsa_check_pub_key(const DSA *dsa, const BIGNUM *pub_key, int *ret);
int ossl_dsa_check_pub_key_partial(const DSA *dsa, const BIGNUM *pub_key,
int *ret);
int ossl_dsa_check_priv_key(const DSA *dsa, const BIGNUM *priv_key, int *ret);
int ossl_dsa_check_pairwise(const DSA *dsa);
int ossl_dsa_is_foreign(const DSA *dsa);
DSA *ossl_dsa_dup(const DSA *dsa, int selection);
#endif
| 2,175 | 40.846154 | 78 | h |
openssl | openssl-master/include/crypto/dsaerr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_DSAERR_H
# define OSSL_CRYPTO_DSAERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_DSA
int ossl_err_load_DSA_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 671 | 20.677419 | 74 | h |
openssl | openssl-master/include/crypto/ec.h | /*
* Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Internal EC functions for other submodules: not for application use */
#ifndef OSSL_CRYPTO_EC_H
# define OSSL_CRYPTO_EC_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/evp.h>
int ossl_ec_curve_name2nid(const char *name);
const char *ossl_ec_curve_nid2nist_int(int nid);
int ossl_ec_curve_nist2nid_int(const char *name);
int evp_pkey_ctx_set_ec_param_enc_prov(EVP_PKEY_CTX *ctx, int param_enc);
# ifndef OPENSSL_NO_EC
# include <openssl/core.h>
# include <openssl/ec.h>
# include "crypto/types.h"
/*-
* Computes the multiplicative inverse of x in the range
* [1,EC_GROUP::order), where EC_GROUP::order is the cardinality of the
* subgroup generated by the generator G:
*
* res := x^(-1) (mod EC_GROUP::order).
*
* This function expects the following two conditions to hold:
* - the EC_GROUP order is prime, and
* - x is included in the range [1, EC_GROUP::order).
*
* This function returns 1 on success, 0 on error.
*
* If the EC_GROUP order is even, this function explicitly returns 0 as
* an error.
* In case any of the two conditions stated above is not satisfied,
* the correctness of its output is not guaranteed, even if the return
* value could still be 1 (as primality testing and a conditional modular
* reduction round on the input can be omitted by the underlying
* implementations for better SCA properties on regular input values).
*/
__owur int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
const BIGNUM *x, BN_CTX *ctx);
/*-
* ECDH Key Derivation Function as defined in ANSI X9.63
*/
int ossl_ecdh_kdf_X9_63(unsigned char *out, size_t outlen,
const unsigned char *Z, size_t Zlen,
const unsigned char *sinfo, size_t sinfolen,
const EVP_MD *md, OSSL_LIB_CTX *libctx,
const char *propq);
int ossl_ec_key_public_check(const EC_KEY *eckey, BN_CTX *ctx);
int ossl_ec_key_public_check_quick(const EC_KEY *eckey, BN_CTX *ctx);
int ossl_ec_key_private_check(const EC_KEY *eckey);
int ossl_ec_key_pairwise_check(const EC_KEY *eckey, BN_CTX *ctx);
OSSL_LIB_CTX *ossl_ec_key_get_libctx(const EC_KEY *eckey);
const char *ossl_ec_key_get0_propq(const EC_KEY *eckey);
void ossl_ec_key_set0_libctx(EC_KEY *key, OSSL_LIB_CTX *libctx);
/* Backend support */
int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
OSSL_PARAM params[], OSSL_LIB_CTX *libctx,
const char *propq,
BN_CTX *bnctx, unsigned char **genbuf);
int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[]);
int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[]);
int ossl_ec_key_fromdata(EC_KEY *ecx, const OSSL_PARAM params[],
int include_private);
int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[]);
int ossl_ec_key_is_foreign(const EC_KEY *ec);
EC_KEY *ossl_ec_key_dup(const EC_KEY *key, int selection);
int ossl_x509_algor_is_sm2(const X509_ALGOR *palg);
EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
OSSL_LIB_CTX *libctx,
const char *propq);
EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
OSSL_LIB_CTX *libctx, const char *propq);
int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode);
int ossl_ec_encoding_name2id(const char *name);
int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id);
int ossl_ec_pt_format_name2id(const char *name);
int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id);
char *ossl_ec_pt_format_id2name(int id);
char *ossl_ec_check_group_type_id2name(int flags);
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name);
int ossl_ec_generate_key_dhkem(EC_KEY *eckey,
const unsigned char *ikm, size_t ikmlen);
int ossl_ecdsa_deterministic_sign(const unsigned char *dgst, int dlen,
unsigned char *sig, unsigned int *siglen,
EC_KEY *eckey, unsigned int nonce_type,
const char *digestname,
OSSL_LIB_CTX *libctx, const char *propq);
# endif /* OPENSSL_NO_EC */
#endif
| 4,732 | 43.233645 | 76 | h |
openssl | openssl-master/include/crypto/ecerr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ECERR_H
# define OSSL_CRYPTO_ECERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
# ifndef OPENSSL_NO_EC
int ossl_err_load_EC_strings(void);
# endif
# ifdef __cplusplus
}
# endif
#endif
| 667 | 20.548387 | 74 | h |
openssl | openssl-master/include/crypto/ecx.h | /*
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
/* Internal EC functions for other submodules: not for application use */
#ifndef OSSL_CRYPTO_ECX_H
# define OSSL_CRYPTO_ECX_H
# pragma once
# include <openssl/opensslconf.h>
# ifndef OPENSSL_NO_ECX
# include <openssl/core.h>
# include <openssl/e_os2.h>
# include <openssl/crypto.h>
# include "internal/refcount.h"
# include "crypto/types.h"
# define X25519_KEYLEN 32
# define X448_KEYLEN 56
# define ED25519_KEYLEN 32
# define ED448_KEYLEN 57
# define MAX_KEYLEN ED448_KEYLEN
# define X25519_BITS 253
# define X25519_SECURITY_BITS 128
# define X448_BITS 448
# define X448_SECURITY_BITS 224
# define ED25519_BITS 256
/* RFC8032 Section 8.5 */
# define ED25519_SECURITY_BITS 128
# define ED25519_SIGSIZE 64
# define ED448_BITS 456
/* RFC8032 Section 8.5 */
# define ED448_SECURITY_BITS 224
# define ED448_SIGSIZE 114
typedef enum {
ECX_KEY_TYPE_X25519,
ECX_KEY_TYPE_X448,
ECX_KEY_TYPE_ED25519,
ECX_KEY_TYPE_ED448
} ECX_KEY_TYPE;
#define KEYTYPE2NID(type) \
((type) == ECX_KEY_TYPE_X25519 \
? EVP_PKEY_X25519 \
: ((type) == ECX_KEY_TYPE_X448 \
? EVP_PKEY_X448 \
: ((type) == ECX_KEY_TYPE_ED25519 \
? EVP_PKEY_ED25519 \
: EVP_PKEY_ED448)))
struct ecx_key_st {
OSSL_LIB_CTX *libctx;
char *propq;
unsigned int haspubkey:1;
unsigned char pubkey[MAX_KEYLEN];
unsigned char *privkey;
size_t keylen;
ECX_KEY_TYPE type;
CRYPTO_REF_COUNT references;
};
size_t ossl_ecx_key_length(ECX_KEY_TYPE type);
ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type,
int haspubkey, const char *propq);
void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
void ossl_ecx_key_free(ECX_KEY *key);
int ossl_ecx_key_up_ref(ECX_KEY *key);
ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection);
int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
unsigned char *secret, size_t *secretlen,
size_t outlen);
int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
const uint8_t peer_public_value[32]);
void ossl_x25519_public_from_private(uint8_t out_public_value[32],
const uint8_t private_key[32]);
int
ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
const uint8_t private_key[32],
const char *propq);
int
ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *tbs, size_t tbs_len,
const uint8_t public_key[32], const uint8_t private_key[32],
const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag,
const uint8_t *context, size_t context_len,
OSSL_LIB_CTX *libctx, const char *propq);
int
ossl_ed25519_verify(const uint8_t *tbs, size_t tbs_len,
const uint8_t signature[64], const uint8_t public_key[32],
const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag,
const uint8_t *context, size_t context_len,
OSSL_LIB_CTX *libctx, const char *propq);
int
ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
const uint8_t private_key[57], const char *propq);
int
ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig,
const uint8_t *message, size_t message_len,
const uint8_t public_key[57], const uint8_t private_key[57],
const uint8_t *context, size_t context_len,
const uint8_t phflag, const char *propq);
int
ossl_ed448_verify(OSSL_LIB_CTX *ctx,
const uint8_t *message, size_t message_len,
const uint8_t signature[114], const uint8_t public_key[57],
const uint8_t *context, size_t context_len,
const uint8_t phflag, const char *propq);
int
ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
const uint8_t peer_public_value[56]);
void
ossl_x448_public_from_private(uint8_t out_public_value[56],
const uint8_t private_key[56]);
/* Backend support */
typedef enum {
KEY_OP_PUBLIC,
KEY_OP_PRIVATE,
KEY_OP_KEYGEN
} ecx_key_op_t;
ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
const unsigned char *p, int plen,
int pkey_id, ecx_key_op_t op,
OSSL_LIB_CTX *libctx, const char *propq);
int ossl_ecx_public_from_private(ECX_KEY *key);
int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
int include_private);
ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
OSSL_LIB_CTX *libctx, const char *propq);
ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey);
ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey);
ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey);
ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey);
# endif /* OPENSSL_NO_ECX */
#endif
| 5,636 | 34.45283 | 87 | h |
openssl | openssl-master/include/crypto/encoder.h | /*
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ENCODER_H
# define OSSL_CRYPTO_ENCODER_H
# pragma once
# include <openssl/types.h>
int ossl_encoder_get_number(const OSSL_ENCODER *encoder);
int ossl_encoder_store_cache_flush(OSSL_LIB_CTX *libctx);
int ossl_encoder_store_remove_all_provided(const OSSL_PROVIDER *prov);
#endif
| 637 | 29.380952 | 74 | h |
openssl | openssl-master/include/crypto/encodererr.h | /*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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
*/
#ifndef OSSL_CRYPTO_ENCODERERR_H
# define OSSL_CRYPTO_ENCODERERR_H
# pragma once
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
# ifdef __cplusplus
extern "C" {
# endif
int ossl_err_load_OSSL_ENCODER_strings(void);
# ifdef __cplusplus
}
# endif
#endif
| 655 | 22.428571 | 74 | h |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.