Skip to content

Commit

Permalink
perf:
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjerry committed Apr 12, 2024
1 parent d00824c commit df9187f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
22 changes: 10 additions & 12 deletions python/goodsync/goodsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ int aes_encrypt_master_key(unsigned char *master_key, int master_key_len, unsign
key_out[offset++] = salt[i];
}

if (!EVP_BytesToKey(cipher, md, salt, key, 10, count, _key, iv)) {
return MASTER_KEY_BYTES_TO_KEY_ERROR;
}

EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex(ctx, cipher, NULL, _key, iv)) {
EVP_CIPHER_CTX_free(ctx);
return MASTER_KEY_ENCRYPT_INIT_ERROR;
}

EVP_MD_CTX *ctx_md = EVP_MD_CTX_create(); // Compatible with openssl 1.0
if (!EVP_DigestInit(ctx_md, md)) {
return MASTER_KEY_DIGEST_INIT_ERROR;
Expand All @@ -40,18 +50,6 @@ int aes_encrypt_master_key(unsigned char *master_key, int master_key_len, unsign
}
EVP_MD_CTX_destroy(ctx_md);

if (!EVP_BytesToKey(cipher, md, salt, key, 10, count, _key, iv)) {
free(digest);
return MASTER_KEY_BYTES_TO_KEY_ERROR;
}

EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex(ctx, cipher, NULL, _key, iv)) {
free(digest);
EVP_CIPHER_CTX_free(ctx);
return MASTER_KEY_ENCRYPT_INIT_ERROR;
}

if (!EVP_EncryptUpdate(ctx, key_out + offset, &key_out_len, digest, 8)) {
free(digest);
EVP_CIPHER_CTX_free(ctx);
Expand Down
22 changes: 10 additions & 12 deletions src/goodsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ int aes_encrypt_master_key(unsigned char *master_key, int master_key_len, unsign
key_out[offset++] = salt[i];
}

if (!EVP_BytesToKey(cipher, md, salt, key, 10, count, _key, iv)) {
return MASTER_KEY_BYTES_TO_KEY_ERROR;
}

EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex(ctx, cipher, NULL, _key, iv)) {
EVP_CIPHER_CTX_free(ctx);
return MASTER_KEY_ENCRYPT_INIT_ERROR;
}

EVP_MD_CTX *ctx_md = EVP_MD_CTX_create(); // Compatible with openssl 1.0
if (!EVP_DigestInit(ctx_md, md)) {
return MASTER_KEY_DIGEST_INIT_ERROR;
Expand All @@ -40,18 +50,6 @@ int aes_encrypt_master_key(unsigned char *master_key, int master_key_len, unsign
}
EVP_MD_CTX_destroy(ctx_md);

if (!EVP_BytesToKey(cipher, md, salt, key, 10, count, _key, iv)) {
free(digest);
return MASTER_KEY_BYTES_TO_KEY_ERROR;
}

EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex(ctx, cipher, NULL, _key, iv)) {
free(digest);
EVP_CIPHER_CTX_free(ctx);
return MASTER_KEY_ENCRYPT_INIT_ERROR;
}

if (!EVP_EncryptUpdate(ctx, key_out + offset, &key_out_len, digest, 8)) {
free(digest);
EVP_CIPHER_CTX_free(ctx);
Expand Down

0 comments on commit df9187f

Please sign in to comment.