Skip to content

Commit

Permalink
Unittest for cryspr methods (#864)
Browse files Browse the repository at this point in the history
* Unittest for cryspr methods
  • Loading branch information
jeandube authored and rndi committed Sep 13, 2019
1 parent a82e0c3 commit d90aef0
Show file tree
Hide file tree
Showing 6 changed files with 815 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -937,6 +937,7 @@ if (ENABLE_UNITTESTS AND ENABLE_CXX11)

srt_add_program(test_srt ${SOURCES_unittests})
srt_make_application(test_srt)
target_include_directories(test_srt PRIVATE ${SSL_INCLUDE_DIRS})

target_link_libraries(
test_srt
Expand Down
15 changes: 9 additions & 6 deletions haicrypt/cryspr-openssl.c
Expand Up @@ -58,16 +58,19 @@ int crysprOpenSSL_AES_SetKey(
int crysprOpenSSL_AES_EcbCipher(
bool bEncrypt, /* true:encrypt, false:decrypt */
CRYSPR_AESCTX *aes_key, /* CRYpto Service PRovider AES Key context */
const unsigned char *indata,/* src (clear text)*/
size_t inlen, /* length */
unsigned char *out_txt, /* dst (cipher text) */
size_t *outlen) /* dst len */
const unsigned char *indata,/* src (clear text if encrypt, cipher text otherwise)*/
size_t inlen, /* indata length */
unsigned char *out_txt, /* dst (cipher text if encrypt, clear text otherwise) */
size_t *outlen) /* in/out dst len */
{
int nblk = inlen/CRYSPR_AESBLKSZ;
int nmore = inlen%CRYSPR_AESBLKSZ;
size_t outsiz = (outlen ? *outlen : 0);
int i;

if (outsiz % CRYSPR_AESBLKSZ) return(-1); /* output buf size must be a multiple of AES block size (16) */
if (bEncrypt) {
if (outsiz > 16 && outsiz < (nblk+nmore)*CRYSPR_AESBLKSZ) return(-1); /* output buf size must have room for PKCS7 padding */
/* Encrypt packet payload, block by block, in output buffer */
for (i=0; i<nblk; i++){
AES_ecb_encrypt(&indata[(i*CRYSPR_AESBLKSZ)],
Expand All @@ -76,9 +79,9 @@ int crysprOpenSSL_AES_EcbCipher(
/* Encrypt last incomplete block */
if (0 < nmore) {
unsigned char intxt[CRYSPR_AESBLKSZ];

/* PKCS7 padding: padding value is number of bytes padded */
memcpy(intxt, &indata[(nblk*CRYSPR_AESBLKSZ)], nmore);
memset(intxt+nmore, 0, CRYSPR_AESBLKSZ-nmore);
memset(intxt+nmore, CRYSPR_AESBLKSZ-nmore, CRYSPR_AESBLKSZ-nmore);
AES_ecb_encrypt(intxt, &out_txt[(nblk*CRYSPR_AESBLKSZ)], aes_key, AES_ENCRYPT);
nblk++;
}
Expand Down
6 changes: 3 additions & 3 deletions haicrypt/cryspr.c
Expand Up @@ -176,7 +176,7 @@ int crysprFallback_AES_WrapKey(CRYSPR_cb *cryspr_cb,
}
}
memcpy(out, A, 8);
return inlen + 8;
return 0;
}

int crysprFallback_AES_UnwrapKey(CRYSPR_cb *cryspr_cb,
Expand Down Expand Up @@ -219,9 +219,9 @@ int crysprFallback_AES_UnwrapKey(CRYSPR_cb *cryspr_cb,
if (memcmp(A, iv, 8))
{
memset(out, 0, inlen);
return 0;
return -1;
}
return inlen;
return 0;
}

static unsigned char *_crysprFallback_GetOutbuf(CRYSPR_cb *cryspr_cb, size_t pfx_len, size_t out_len)
Expand Down
8 changes: 8 additions & 0 deletions haicrypt/cryspr.h
Expand Up @@ -32,6 +32,10 @@ written by
#include "haisrt/hcrypt_msg.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#include "cryspr-config.h"

typedef struct tag_CRYSPR_cb {
Expand Down Expand Up @@ -192,4 +196,8 @@ typedef struct tag_CRYSPR_methods {

CRYSPR_methods *crysprInit(CRYSPR_methods *cryspr);

#ifdef __cplusplus
}
#endif

#endif /* CRYSPR_H */
1 change: 1 addition & 0 deletions test/filelist.maf
Expand Up @@ -2,6 +2,7 @@
SOURCES
test_buffer.cpp
test_connection_timeout.cpp
test_cryspr.cpp
test_epoll.cpp
test_seqno.cpp
test_strict_encription.cpp
Expand Down

0 comments on commit d90aef0

Please sign in to comment.