Skip to content

Commit

Permalink
os_mem* deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
btclinux committed Jun 7, 2022
1 parent 59c4f78 commit 8b7b6e0
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 69 deletions.
20 changes: 10 additions & 10 deletions src/aes/aes-cbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ int aes_128_cbc_encrypt(const aes_uchar *key, const aes_uchar *iv, aes_uchar *da
ctx = aes_encrypt_init(key, 16);
if (ctx == NULL)
return -1;
os_memcpy(cbc, iv, AES_BLOCK_SIZE);
memcpy(cbc, iv, AES_BLOCK_SIZE);

blocks = data_len / AES_BLOCK_SIZE;
for (i = 0; i < blocks; i++) {
for (j = 0; j < AES_BLOCK_SIZE; j++)
cbc[j] ^= pos[j];
aes_encrypt(ctx, cbc, cbc);
os_memcpy(pos, cbc, AES_BLOCK_SIZE);
memcpy(pos, cbc, AES_BLOCK_SIZE);
pos += AES_BLOCK_SIZE;
}
aes_encrypt_deinit(ctx);
Expand All @@ -56,14 +56,14 @@ int aes_256_cbc_encrypt(const aes_uchar *key, const aes_uchar *iv, aes_uchar *da
ctx = aes_encrypt_init(key, 32);
if (ctx == NULL)
return -1;
os_memcpy(cbc, iv, AES_BLOCK_SIZE);
memcpy(cbc, iv, AES_BLOCK_SIZE);

blocks = data_len / AES_BLOCK_SIZE;
for (i = 0; i < blocks; i++) {
for (j = 0; j < AES_BLOCK_SIZE; j++)
cbc[j] ^= pos[j];
aes_encrypt(ctx, cbc, cbc);
os_memcpy(pos, cbc, AES_BLOCK_SIZE);
memcpy(pos, cbc, AES_BLOCK_SIZE);
pos += AES_BLOCK_SIZE;
}
aes_encrypt_deinit(ctx);
Expand All @@ -88,15 +88,15 @@ int aes_128_cbc_decrypt(const aes_uchar *key, const aes_uchar *iv, aes_uchar *da
ctx = aes_decrypt_init(key, 16);
if (ctx == NULL)
return -1;
os_memcpy(cbc, iv, AES_BLOCK_SIZE);
memcpy(cbc, iv, AES_BLOCK_SIZE);

blocks = data_len / AES_BLOCK_SIZE;
for (i = 0; i < blocks; i++) {
os_memcpy(tmp, pos, AES_BLOCK_SIZE);
memcpy(tmp, pos, AES_BLOCK_SIZE);
aes_decrypt(ctx, pos, pos);
for (j = 0; j < AES_BLOCK_SIZE; j++)
pos[j] ^= cbc[j];
os_memcpy(cbc, tmp, AES_BLOCK_SIZE);
memcpy(cbc, tmp, AES_BLOCK_SIZE);
pos += AES_BLOCK_SIZE;
}
aes_decrypt_deinit(ctx);
Expand All @@ -113,15 +113,15 @@ int aes_256_cbc_decrypt(const aes_uchar *key, const aes_uchar *iv, aes_uchar *da
ctx = aes_decrypt_init(key, 32);
if (ctx == NULL)
return -1;
os_memcpy(cbc, iv, AES_BLOCK_SIZE);
memcpy(cbc, iv, AES_BLOCK_SIZE);

blocks = data_len / AES_BLOCK_SIZE;
for (i = 0; i < blocks; i++) {
os_memcpy(tmp, pos, AES_BLOCK_SIZE);
memcpy(tmp, pos, AES_BLOCK_SIZE);
aes_decrypt(ctx, pos, pos);
for (j = 0; j < AES_BLOCK_SIZE; j++)
pos[j] ^= cbc[j];
os_memcpy(cbc, tmp, AES_BLOCK_SIZE);
memcpy(cbc, tmp, AES_BLOCK_SIZE);
pos += AES_BLOCK_SIZE;
}
aes_decrypt_deinit(ctx);
Expand Down
6 changes: 3 additions & 3 deletions src/aes/aes-ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const aes_uchar *n
b[0] = aad_len ? 0x40 : 0 /* Adata */;
b[0] |= (((M - 2) / 2) /* M' */ << 3);
b[0] |= (L - 1) /* L' */;
os_memcpy(&b[1], nonce, 15 - L);
memcpy(&b[1], nonce, 15 - L);
AES_PUT_BE16(&b[AES_BLOCK_SIZE - L], plain_len);

aes_hexdump_key(MSG_EXCESSIVE, "CCM B_0", b, AES_BLOCK_SIZE);
Expand All @@ -44,7 +44,7 @@ static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const aes_uchar *n
return;

AES_PUT_BE16(aad_buf, aad_len);
os_memcpy(aad_buf + 2, aad, aad_len);
memcpy(aad_buf + 2, aad, aad_len);
memset(aad_buf + 2 + aad_len, 0, sizeof(aad_buf) - 2 - aad_len);

xor_aes_block(aad_buf, x);
Expand Down Expand Up @@ -82,7 +82,7 @@ static void aes_ccm_encr_start(size_t L, const aes_uchar *nonce, aes_uchar *a)
{
/* A_i = Flags | Nonce N | Counter i */
a[0] = L - 1; /* Flags = L' */
os_memcpy(&a[1], nonce, 15 - L);
memcpy(&a[1], nonce, 15 - L);
}


Expand Down
2 changes: 1 addition & 1 deletion src/aes/aes-ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int aes_128_ctr_encrypt(const aes_uchar *key, const aes_uchar *nonce,
ctx = aes_encrypt_init(key, 16);
if (ctx == NULL)
return -1;
os_memcpy(counter, nonce, AES_BLOCK_SIZE);
memcpy(counter, nonce, AES_BLOCK_SIZE);

while (left > 0) {
aes_encrypt(ctx, counter, buf);
Expand Down
14 changes: 7 additions & 7 deletions src/aes/aes-gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void gf_mult(const aes_uchar *x, const aes_uchar *y, aes_uchar *z)
int i, j;

memset(z, 0, 16); /* Z_0 = 0^128 */
os_memcpy(v, y, 16); /* V_0 = Y */
memcpy(v, y, 16); /* V_0 = Y */

for (i = 0; i < 16; i++) {
for (j = 0; j < 8; j++) {
Expand Down Expand Up @@ -114,13 +114,13 @@ static void ghash(const aes_uchar *h, const aes_uchar *x, size_t xlen, aes_uchar
* multiplication operation for binary Galois (finite) field of
* 2^128 elements */
gf_mult(y, h, tmp);
os_memcpy(y, tmp, 16);
memcpy(y, tmp, 16);
}

if (x + xlen > xpos) {
/* Add zero padded last block */
size_t last = x + xlen - xpos;
os_memcpy(tmp, xpos, last);
memcpy(tmp, xpos, last);
memset(tmp + last, 0, sizeof(tmp) - last);

/* Y_i = (Y^(i-1) XOR X_i) dot H */
Expand All @@ -130,7 +130,7 @@ static void ghash(const aes_uchar *h, const aes_uchar *x, size_t xlen, aes_uchar
* multiplication operation for binary Galois (finite) field of
* 2^128 elements */
gf_mult(y, h, tmp);
os_memcpy(y, tmp, 16);
memcpy(y, tmp, 16);
}

/* Return Y_m */
Expand All @@ -149,7 +149,7 @@ static void aes_gctr(void *aes, const aes_uchar *icb, const aes_uchar *x, size_t

n = xlen / 16;

os_memcpy(cb, icb, AES_BLOCK_SIZE);
memcpy(cb, icb, AES_BLOCK_SIZE);
/* Full blocks */
for (i = 0; i < n; i++) {
aes_encrypt(aes, cb, ypos);
Expand Down Expand Up @@ -191,7 +191,7 @@ static void aes_gcm_prepare_j0(const aes_uchar *iv, size_t iv_len, const aes_uch

if (iv_len == 12) {
/* Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
os_memcpy(J0, iv, iv_len);
memcpy(J0, iv, iv_len);
memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
J0[AES_BLOCK_SIZE - 1] = 0x01;
} else {
Expand All @@ -216,7 +216,7 @@ static void aes_gcm_gctr(void *aes, const aes_uchar *J0, const aes_uchar *in, si
if (len == 0)
return;

os_memcpy(J0inc, J0, AES_BLOCK_SIZE);
memcpy(J0inc, J0, AES_BLOCK_SIZE);
inc32(J0inc);
aes_gctr(aes, J0inc, in, len, out);
}
Expand Down
2 changes: 1 addition & 1 deletion src/aes/aes-internal-dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,5 @@ void aes_decrypt(void *ctx, const aes_uchar *crypt, aes_uchar *plain)

void aes_decrypt_deinit(void *ctx)
{
os_memset(ctx, 0, AES_PRIV_SIZE);
memset(ctx, 0, AES_PRIV_SIZE);
}
2 changes: 1 addition & 1 deletion src/aes/aes-internal-enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,6 @@ void aes_encrypt(void *ctx, const aes_uchar *plain, aes_uchar *crypt)

void aes_encrypt_deinit(void *ctx)
{
os_memset(ctx, 0, AES_PRIV_SIZE);
memset(ctx, 0, AES_PRIV_SIZE);
}

12 changes: 6 additions & 6 deletions src/aes/aes-unwrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ int aes_unwrap(const aes_uchar *kek, int n, const aes_uchar *cipher, aes_uchar *
void *ctx;

/* 1) Initialize variables. */
os_memcpy(a, cipher, 8);
memcpy(a, cipher, 8);
r = plain;
os_memcpy(r, cipher + 8, 8 * n);
memcpy(r, cipher + 8, 8 * n);

ctx = aes_decrypt_init(kek, 16);
if (ctx == NULL)
Expand All @@ -44,13 +44,13 @@ int aes_unwrap(const aes_uchar *kek, int n, const aes_uchar *cipher, aes_uchar *
for (j = 5; j >= 0; j--) {
r = plain + (n - 1) * 8;
for (i = n; i >= 1; i--) {
os_memcpy(b, a, 8);
memcpy(b, a, 8);
b[7] ^= n * j + i;

os_memcpy(b + 8, r, 8);
memcpy(b + 8, r, 8);
aes_decrypt(ctx, b, b);
os_memcpy(a, b, 8);
os_memcpy(r, b + 8, 8);
memcpy(a, b, 8);
memcpy(r, b + 8, 8);
r -= 8;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/aes/aes-wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int aes_wrap(const aes_uchar *kek, int n, const aes_uchar *plain, aes_uchar *cip

/* 1) Initialize variables. */
memset(a, 0xa6, 8);
os_memcpy(r, plain, 8 * n);
memcpy(r, plain, 8 * n);

ctx = aes_encrypt_init(kek, 16);
if (ctx == NULL)
Expand All @@ -46,12 +46,12 @@ int aes_wrap(const aes_uchar *kek, int n, const aes_uchar *plain, aes_uchar *cip
for (j = 0; j <= 5; j++) {
r = cipher + 8;
for (i = 1; i <= n; i++) {
os_memcpy(b, a, 8);
os_memcpy(b + 8, r, 8);
memcpy(b, a, 8);
memcpy(b + 8, r, 8);
aes_encrypt(ctx, b, b);
os_memcpy(a, b, 8);
memcpy(a, b, 8);
a[7] ^= n * j + i;
os_memcpy(r, b + 8, 8);
memcpy(r, b + 8, 8);
r += 8;
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/authAndSignTxn.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,19 @@ void addRecipientText() {

// Reads a uint8_t from buffer
void read_u8(uint8_t *val, uint8_t **ptr){
os_memmove(val, *ptr, sizeof(uint8_t));
memmove(val, *ptr, sizeof(uint8_t));
*ptr += sizeof(uint8_t);
}

// Reads a uint16_t from buffer
void read_u16(uint16_t *val, uint8_t **ptr){
os_memmove(val, *ptr, sizeof(uint16_t));
memmove(val, *ptr, sizeof(uint16_t));
*ptr += sizeof(uint16_t);
}

// Reads a uint64_t from buffer and put the resulting value on val
void read_u64(uint64_t *val, uint8_t **ptr){
os_memmove(val, *ptr, sizeof(uint64_t));
memmove(val, *ptr, sizeof(uint64_t));
*ptr += sizeof(uint64_t);
}

Expand Down Expand Up @@ -428,9 +428,9 @@ uint8_t parseTxnData() {
if (ptr == 0)
return R_TXN_SIZE_TOO_SMALL;
ptr += 1; //version
os_memmove(&(state.txnAuth.attachmentTempInt64Num1), ptr, sizeof(state.txnAuth.attachmentTempInt64Num1)); // assetID
memmove(&(state.txnAuth.attachmentTempInt64Num1), ptr, sizeof(state.txnAuth.attachmentTempInt64Num1)); // assetID
ptr += sizeof(state.txnAuth.attachmentTempInt64Num1);
os_memmove(&(state.txnAuth.attachmentTempInt64Num2), ptr, sizeof(state.txnAuth.attachmentTempInt64Num2)); // quantity
memmove(&(state.txnAuth.attachmentTempInt64Num2), ptr, sizeof(state.txnAuth.attachmentTempInt64Num2)); // quantity
ptr += sizeof(state.txnAuth.attachmentTempInt64Num2);

if(state.txnAuth.attachmentTempInt64Num1 == TRT_TOKEN){
Expand Down Expand Up @@ -459,11 +459,11 @@ uint8_t parseTxnData() {
return R_TXN_SIZE_TOO_SMALL;
ptr += 1; //version

os_memmove(&(state.txnAuth.attachmentTempInt64Num1), ptr, sizeof(state.txnAuth.attachmentTempInt64Num1)); // assetID
memmove(&(state.txnAuth.attachmentTempInt64Num1), ptr, sizeof(state.txnAuth.attachmentTempInt64Num1)); // assetID
ptr += sizeof(state.txnAuth.attachmentTempInt64Num1);
os_memmove(&(state.txnAuth.attachmentTempInt64Num2), ptr, sizeof(state.txnAuth.attachmentTempInt64Num2)); // quantity
memmove(&(state.txnAuth.attachmentTempInt64Num2), ptr, sizeof(state.txnAuth.attachmentTempInt64Num2)); // quantity
ptr += sizeof(state.txnAuth.attachmentTempInt64Num2);
os_memmove(&(state.txnAuth.attachmentTempInt64Num3), ptr, sizeof(state.txnAuth.attachmentTempInt64Num3)); // price
memmove(&(state.txnAuth.attachmentTempInt64Num3), ptr, sizeof(state.txnAuth.attachmentTempInt64Num3)); // price
ptr += sizeof(state.txnAuth.attachmentTempInt64Num3);

if(state.txnAuth.attachmentTempInt64Num1 == TRT_TOKEN){
Expand Down Expand Up @@ -499,7 +499,7 @@ uint8_t parseTxnData() {
if (ptr == 0)
return R_TXN_SIZE_TOO_SMALL;
ptr += 1; //version
os_memmove(&(state.txnAuth.attachmentTempInt64Num1), ptr, sizeof(state.txnAuth.attachmentTempInt64Num1)); // assetID
memmove(&(state.txnAuth.attachmentTempInt64Num1), ptr, sizeof(state.txnAuth.attachmentTempInt64Num1)); // assetID
ptr += sizeof(state.txnAuth.attachmentTempInt64Num1);

// Window 1 is order ID
Expand Down Expand Up @@ -548,7 +548,7 @@ uint8_t parseReferencedTxn() {
void addToReadBuffer(const uint8_t * const newData, const uint8_t numBytes) {
cx_hash(&state.txnAuth.hashstate.header, 0, newData, numBytes, 0, 0);

os_memcpy(state.txnAuth.readBuffer, newData, numBytes);
memcpy(state.txnAuth.readBuffer, newData, numBytes);
state.txnAuth.readBufferReadOffset = 0;
state.txnAuth.readBufferEndPos = numBytes;
}
Expand All @@ -561,7 +561,7 @@ void addToReadBuffer(const uint8_t * const newData, const uint8_t numBytes) {

uint8_t signTxn(const uint8_t * const dataBuffer, const uint8_t dataLength, uint8_t * const destBuffer, uint16_t * const outException) {

uint8_t sharedKey[32]; os_memset(sharedKey, 0, sizeof(sharedKey));
uint8_t sharedKey[32]; memset(sharedKey, 0, sizeof(sharedKey));
uint8_t ret = 0;

if (R_SUCCESS != (ret = burst_keys(dataBuffer, dataLength, NULL, NULL, sharedKey, outException))) {
Expand All @@ -574,7 +574,7 @@ uint8_t signTxn(const uint8_t * const dataBuffer, const uint8_t dataLength, uint
cx_hash(&state.txnAuth.hashstate.header, CX_LAST, NULL, 0, messageSha256, sizeof(messageSha256));

sign_msg(sharedKey, messageSha256, destBuffer); //is a void function, no ret value to check against
//os_memcpy(destBuffer+32, messageSha256, 32);
//memcpy(destBuffer+32, messageSha256, 32);

//clear
explicit_bzero(messageSha256, sizeof(messageSha256));
Expand Down
22 changes: 11 additions & 11 deletions src/burst.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ states_t state;
void sha256TwoBuffers(const uint8_t * const bufferTohash1, const uint16_t sizeOfBuffer1, const uint8_t * const bufferTohash2, const uint16_t sizeOfBuffer2, uint8_t * const output) {
cx_sha256_t shaContext;

os_memset(output, 0, 32);
memset(output, 0, 32);
cx_sha256_init(&shaContext); //return value has no info

cx_hash(&shaContext.header, 0, bufferTohash1, sizeOfBuffer1, output, 32);
Expand All @@ -62,9 +62,9 @@ void sha256Buffer(const uint8_t * const bufferTohash, const uint16_t sizeOfBuffe
//@param out: sig should point to 64 bytes allocated to hold the signiture of the message
void sign_msg(uint8_t * const sharedKey, const uint8_t * const msgSha256, uint8_t * const sig) {

uint8_t x[32]; os_memset(x, 0, sizeof(x));
uint8_t y[32]; os_memset(y, 0, sizeof(y));
uint8_t h[32]; os_memset(h, 0, sizeof(h));
uint8_t x[32]; memset(x, 0, sizeof(x));
uint8_t y[32]; memset(y, 0, sizeof(y));
uint8_t h[32]; memset(h, 0, sizeof(h));

// Get x = hash(m, s)
cx_sha256_init(&state.txnAuth.hashstate);
Expand All @@ -80,7 +80,7 @@ void sign_msg(uint8_t * const sharedKey, const uint8_t * const msgSha256, uint8_
cx_hash(&state.txnAuth.hashstate.header, CX_LAST, y, 32, h, 32);

// copy h first because sign25519 screws with parameters
os_memcpy(sig+32, h, 32);
memcpy(sig+32, h, 32);
sign25519(sig, h, x, sharedKey);

// clear sensitive data
Expand All @@ -107,15 +107,15 @@ uint8_t burst_keys(const uint8_t * const dataBuffer, const uint8_t dataLength, u

uint32_t pathPrefix[] = PATH_PREFIX; //defined in Makefile

uint8_t publicKey[32]; os_memset(publicKey, 0, sizeof(publicKey));
uint8_t privKey[32]; os_memset(privKey, 0, sizeof(privKey));
uint8_t publicKey[32]; memset(publicKey, 0, sizeof(publicKey));
uint8_t privKey[32]; memset(privKey, 0, sizeof(privKey));

if(dataLength < 3)
return R_NOT_ENOUGH_DERIVATION_INDEXES;

// BURST keypath of 44'/30'/account'/change'/index'
uint32_t derivationPath[5]; os_memset(derivationPath, 0, sizeof(derivationPath));
os_memmove(derivationPath, pathPrefix, 2 * sizeof(uint32_t));
uint32_t derivationPath[5]; memset(derivationPath, 0, sizeof(derivationPath));
memmove(derivationPath, pathPrefix, 2 * sizeof(uint32_t));
derivationPath[2] = dataBuffer[0] | 0x80000000; // account
derivationPath[3] = dataBuffer[1] | 0x80000000; // change
derivationPath[4] = dataBuffer[2] | 0x80000000; // index
Expand All @@ -129,10 +129,10 @@ uint8_t burst_keys(const uint8_t * const dataBuffer, const uint8_t dataLength, u
}

if (0 != publicKeyOut) {
os_memmove(publicKeyOut, publicKey, 32);
memmove(publicKeyOut, publicKey, 32);
}
if (0 != privKeyOut) {
os_memmove(privKeyOut, privKey, 32);
memmove(privKeyOut, privKey, 32);
}
}
CATCH_OTHER(exception) {
Expand Down
Loading

0 comments on commit 8b7b6e0

Please sign in to comment.