Skip to content

Commit

Permalink
SEH debug
Browse files Browse the repository at this point in the history
  • Loading branch information
geedo0 committed Dec 29, 2023
1 parent 75ebb08 commit 3f42fc7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
51 changes: 50 additions & 1 deletion crypto/fipsmodule/cipher/e_aesccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ static int ccm128_init_state(const struct ccm128_context *ctx,
const block128_f block = ctx->block;
const uint32_t M = ctx->M;
const uint32_t L = ctx->L;
fprintf(stdout, "I1\n");

// |L| determines the expected |nonce_len| and the limit for |plaintext_len|.
if (plaintext_len > CRYPTO_ccm128_max_input(ctx) ||
Expand All @@ -145,18 +146,22 @@ static int ccm128_init_state(const struct ccm128_context *ctx,
// Assemble the first block for computing the MAC.
OPENSSL_memset(state, 0, sizeof(*state));
state->nonce.c[0] = (uint8_t)((L - 1) | ((M - 2) / 2) << 3);
fprintf(stdout, "I2\n");
if (aad_len != 0) {
state->nonce.c[0] |= 0x40; // Set AAD Flag
}
OPENSSL_memcpy(&state->nonce.c[1], nonce, nonce_len);
fprintf(stdout, "I3\n");
for (uint32_t i = 0; i < L; i++) {
state->nonce.c[15 - i] = (uint8_t)((uint64_t) plaintext_len >> (8 * i));
}

(*block)(state->nonce.c, state->cmac.c, key);
size_t blocks = 1;
fprintf(stdout, "I4 aad_len: %zu\n", aad_len);

if (aad_len != 0) {
fprintf(stdout, "I4a\n");
unsigned i;
// Cast to u64 to avoid the compiler complaining about invalid shifts.
uint64_t aad_len_u64 = aad_len;
Expand Down Expand Up @@ -197,6 +202,7 @@ static int ccm128_init_state(const struct ccm128_context *ctx,
i = 0;
} while (aad_len != 0);
}
fprintf(stdout, "I5\n");

// Per RFC 3610, section 2.6, the total number of block cipher operations done
// must not exceed 2^61. There are two block cipher operations remaining per
Expand All @@ -205,34 +211,47 @@ static int ccm128_init_state(const struct ccm128_context *ctx,
if (plaintext_len + 15 < plaintext_len ||
remaining_blocks + blocks < blocks ||
(uint64_t) remaining_blocks + blocks > UINT64_C(1) << 61) {
fprintf(stdout, "I6\n");
return 0;
}

// Assemble the first block for encrypting and decrypting. The bottom |L|
// bytes are replaced with a counter and all bit the encoding of |L| is
// cleared in the first byte.

fprintf(stdout, "I7\n");
state->nonce.c[0] &= 7;

fprintf(stdout, "I8\n");
return 1;
}

static int ccm128_encrypt(const struct ccm128_context *ctx,
struct ccm128_state *state, const AES_KEY *key,
uint8_t *out, const uint8_t *in, size_t len) {
// The counter for encryption begins at one.
fprintf(stdout, "E1\n");
for (unsigned i = 0; i < ctx->L; i++) {
state->nonce.c[15 - i] = 0;
}

fprintf(stdout, "E2\n");
state->nonce.c[15] = 1;

uint8_t partial_buf[16];
unsigned num = 0;
if (ctx->ctr != NULL) {

fprintf(stdout, "E3\n");
CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, state->nonce.c, partial_buf,
&num, ctx->ctr);
} else {

fprintf(stdout, "E4\n");
CRYPTO_ctr128_encrypt(in, out, len, key, state->nonce.c, partial_buf, &num,
ctx->block);
}
fprintf(stdout, "E5\n");
return 1;
}

Expand All @@ -241,6 +260,8 @@ static int ccm128_compute_mac(const struct ccm128_context *ctx,
uint8_t *out_tag, size_t tag_len,
const uint8_t *in, size_t len) {
block128_f block = ctx->block;

fprintf(stdout, "M1\n");
if (tag_len != ctx->M) {
return 0;
}
Expand All @@ -258,13 +279,16 @@ static int ccm128_compute_mac(const struct ccm128_context *ctx,
in += 16;
len -= 16;
}

fprintf(stdout, "M2\n");
if (len > 0) {
for (size_t i = 0; i < len; i++) {
state->cmac.c[i] ^= in[i];
}
(*block)(state->cmac.c, state->cmac.c, key);
}

fprintf(stdout, "M3\n");
// Encrypt the MAC with counter zero.
for (unsigned i = 0; i < ctx->L; i++) {
state->nonce.c[15 - i] = 0;
Expand All @@ -273,7 +297,10 @@ static int ccm128_compute_mac(const struct ccm128_context *ctx,
state->cmac.u[0] ^= tmp.u[0];
state->cmac.u[1] ^= tmp.u[1];

fprintf(stdout, "M4\n");
OPENSSL_memcpy(out_tag, state->cmac.c, tag_len);

fprintf(stdout, "M5\n");
return 1;
}

Expand Down Expand Up @@ -334,7 +361,7 @@ static int aead_aes_ccm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
tag_len = M;
}

if (tag_len != M) {
if (tag_len != M) {\
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
return 0;
}
Expand Down Expand Up @@ -517,6 +544,7 @@ static int cipher_aes_ccm_init(EVP_CIPHER_CTX *ctx, const uint8_t *key,

static int cipher_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
const uint8_t *in, size_t len) {
fprintf(stdout, "A\n");
CIPHER_AES_CCM_CTX *cipher_ctx = CCM_CTX(ctx);
CCM128_CTX *ccm_ctx = CCM_INNER_CTX(cipher_ctx);
CCM128_STATE *ccm_state = CCM_INNER_STATE(cipher_ctx);
Expand All @@ -531,24 +559,29 @@ static int cipher_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
return -1;
}

fprintf(stdout, "B\n");
if (!out) {
if (!in) {
// If |out| and |in| are both NULL, |inl| is the total length of the
// message which we need to include that in the 0th block of the CBC-MAC.
cipher_ctx->message_len = len;
cipher_ctx->len_set = 1;

fprintf(stdout, "C\n");
return len;
} else {
// If only |out| is NULL then this is the AAD.
// The message length must be set apriori.
if (!cipher_ctx->len_set && len) {
return -1;
}
fprintf(stdout, "D\n");
// We now have everything we need to initialize the CBC-MAC state
if (ccm128_init_state(ccm_ctx, ccm_state,
&cipher_ctx->ks.ks, cipher_ctx->nonce,
15 - cipher_ctx->L, in, len,
cipher_ctx->message_len)) {
fprintf(stdout, "E\n");
cipher_ctx->ccm_set = 1;
return len;
} else {
Expand All @@ -565,24 +598,32 @@ static int cipher_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
return -1;
}
if (!cipher_ctx->ccm_set) {
fprintf(stdout, "F\n");
// Initialize the ccm_state if this did not happen during the AAD update.
if (!ccm128_init_state(ccm_ctx, ccm_state, &cipher_ctx->ks.ks,
cipher_ctx->nonce, 15 - cipher_ctx->L, NULL, 0,
cipher_ctx->message_len)) {
fprintf(stdout, "G\n");
return -1;
}
cipher_ctx->ccm_set = 1;
}

if (EVP_CIPHER_CTX_encrypting(ctx)) {

fprintf(stdout, "H\n");
// Encryption path. Compute CBC-MAC on plaintext and then encrypt.
if (!ccm128_compute_mac(ccm_ctx, ccm_state, &cipher_ctx->ks.ks,
cipher_ctx->tag, cipher_ctx->M, in, len)) {
return -1;
}

fprintf(stdout, "II\n");
if (!ccm128_encrypt(ccm_ctx, ccm_state, &cipher_ctx->ks.ks, out, in, len)) {
return -1;
}

fprintf(stdout, "J\n");
cipher_ctx->tag_set = 1;
} else {
// Decryption path. Compute the plaintext then compute its CBC-MAC.
Expand All @@ -605,6 +646,8 @@ static int cipher_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
cipher_ctx->len_set = 0;
cipher_ctx->ccm_set = 0;
}

fprintf(stdout, "K\n");
return (int) len;
}

Expand All @@ -631,6 +674,12 @@ static int cipher_aes_ccm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
cipher_ctx->L = 8;
cipher_ctx->M = 14;
cipher_ctx->message_len = 0;
fprintf(stdout, "%s, %p\n", "cipher_aes_ccm_ctrl", &cipher_aes_ccm_ctrl);
fprintf(stdout, "%s, %p\n", "cipher_aes_ccm_init", &cipher_aes_ccm_init);
fprintf(stdout, "%s, %p\n", "cipher_aes_ccm_cipher", &cipher_aes_ccm_cipher);
fprintf(stdout, "%s, %p\n", "ccm128_init_state", &ccm128_init_state);
fprintf(stdout, "%s, %p\n", "ccm128_compute_mac", &ccm128_compute_mac);
fprintf(stdout, "%s, %p\n", "ccm128_encrypt", &ccm128_encrypt);
return 1;
case EVP_CTRL_GET_IVLEN:
*(uint32_t *)ptr = 15 - cipher_ctx->L;
Expand Down
6 changes: 4 additions & 2 deletions third_party/googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
#include <wchar.h>
#include <wctype.h>

# include <dbghelp.h>

#include <algorithm>
#include <chrono> // NOLINT
#include <cmath>
Expand Down Expand Up @@ -739,6 +737,7 @@ int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code, LPEXCEPTION_POI
uint64_t address = p->ExceptionRecord->ExceptionInformation[1];
std::cout << "SEH EXCEPTION OCCURED!! flag: " << flag << ", address: " << address << std::endl;

/*
HANDLE proc = GetCurrentProcess();
SymInitialize(proc, NULL, true);
uint64_t displacement = 0;
Expand All @@ -751,6 +750,9 @@ int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code, LPEXCEPTION_POI
} else {
std::cout << "Exception at address: " << violationAddress << std::endl;
}
*/
PVOID violationAddress = p->ExceptionRecord->ExceptionAddress;
std::cout << "Exception at address: " << violationAddress << std::endl;

const DWORD kCxxExceptionCode = 0xe06d7363;

Expand Down

0 comments on commit 3f42fc7

Please sign in to comment.