|
|
@ -1058,14 +1058,14 @@ static int cipher_context_update(cipher_ctx_t *ctx, uint8_t *output, size_t *ole |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
int ss_onetimeauth(buffer_t *buf, uint8_t *iv) |
|
|
|
int ss_onetimeauth(buffer_t *buf, uint8_t *iv, size_t capacity) |
|
|
|
{ |
|
|
|
uint8_t hash[ONETIMEAUTH_BYTES * 2]; |
|
|
|
uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH]; |
|
|
|
memcpy(auth_key, iv, enc_iv_len); |
|
|
|
memcpy(auth_key + enc_iv_len, enc_key, enc_key_len); |
|
|
|
|
|
|
|
brealloc(buf, ONETIMEAUTH_BYTES + buf->len, buf->capacity); |
|
|
|
brealloc(buf, ONETIMEAUTH_BYTES + buf->len, capacity); |
|
|
|
|
|
|
|
#if defined(USE_CRYPTO_OPENSSL) |
|
|
|
HMAC(EVP_sha1(), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len, (uint8_t *)hash, NULL); |
|
|
@ -1100,7 +1100,7 @@ int ss_onetimeauth_verify(buffer_t *buf, uint8_t *iv) |
|
|
|
return safe_memcmp(buf->array + len, hash, ONETIMEAUTH_BYTES); |
|
|
|
} |
|
|
|
|
|
|
|
int ss_encrypt_all(buffer_t *plain, int method, int auth) |
|
|
|
int ss_encrypt_all(buffer_t *plain, int method, int auth, size_t capacity) |
|
|
|
{ |
|
|
|
if (method > TABLE) { |
|
|
|
cipher_ctx_t evp; |
|
|
@ -1110,7 +1110,7 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) |
|
|
|
int err = 1; |
|
|
|
|
|
|
|
static buffer_t tmp = { 0 }; |
|
|
|
brealloc(&tmp, iv_len + plain->len, plain->capacity); |
|
|
|
brealloc(&tmp, iv_len + plain->len, capacity); |
|
|
|
buffer_t *cipher = &tmp; |
|
|
|
cipher->len = plain->len; |
|
|
|
|
|
|
@ -1121,7 +1121,7 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) |
|
|
|
memcpy(cipher->array, iv, iv_len); |
|
|
|
|
|
|
|
if (auth) { |
|
|
|
ss_onetimeauth(plain, iv); |
|
|
|
ss_onetimeauth(plain, iv, capacity); |
|
|
|
cipher->len = plain->len; |
|
|
|
} |
|
|
|
|
|
|
@ -1149,7 +1149,7 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) |
|
|
|
|
|
|
|
cipher_context_release(&evp); |
|
|
|
|
|
|
|
brealloc(plain, iv_len + cipher->len, plain->capacity); |
|
|
|
brealloc(plain, iv_len + cipher->len, capacity); |
|
|
|
memcpy(plain->array, cipher->array, iv_len + cipher->len); |
|
|
|
plain->len = iv_len + cipher->len; |
|
|
|
|
|
|
@ -1165,7 +1165,7 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx) |
|
|
|
int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx, size_t capacity) |
|
|
|
{ |
|
|
|
if (ctx != NULL) { |
|
|
|
static buffer_t tmp = { 0 }; |
|
|
@ -1176,7 +1176,7 @@ int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx) |
|
|
|
iv_len = enc_iv_len; |
|
|
|
} |
|
|
|
|
|
|
|
brealloc(&tmp, iv_len + plain->len, plain->capacity); |
|
|
|
brealloc(&tmp, iv_len + plain->len, capacity); |
|
|
|
buffer_t *cipher = &tmp; |
|
|
|
cipher->len = plain->len; |
|
|
|
|
|
|
@ -1189,9 +1189,9 @@ int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx) |
|
|
|
|
|
|
|
if (enc_method >= SALSA20) { |
|
|
|
int padding = ctx->counter % SODIUM_BLOCK_SIZE; |
|
|
|
brealloc(cipher, iv_len + (padding + cipher->len) * 2, cipher->capacity); |
|
|
|
brealloc(cipher, iv_len + (padding + cipher->len) * 2, capacity); |
|
|
|
if (padding) { |
|
|
|
brealloc(plain, plain->len + padding, plain->capacity); |
|
|
|
brealloc(plain, plain->len + padding, capacity); |
|
|
|
memmove(plain->array + padding, plain->array, plain->len); |
|
|
|
memset(plain->array, 0, padding); |
|
|
|
} |
|
|
@ -1222,7 +1222,7 @@ int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx) |
|
|
|
dump("CIPHER", cipher->array + iv_len, cipher->len); |
|
|
|
#endif |
|
|
|
|
|
|
|
brealloc(plain, iv_len + cipher->len, plain->capacity); |
|
|
|
brealloc(plain, iv_len + cipher->len, capacity); |
|
|
|
memcpy(plain->array, cipher->array, iv_len + cipher->len); |
|
|
|
plain->len = iv_len + cipher->len; |
|
|
|
|
|
|
@ -1238,7 +1238,7 @@ int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int ss_decrypt_all(buffer_t *cipher, int method, int auth) |
|
|
|
int ss_decrypt_all(buffer_t *cipher, int method, int auth, size_t capacity) |
|
|
|
{ |
|
|
|
if (method > TABLE) { |
|
|
|
size_t iv_len = enc_iv_len; |
|
|
@ -1252,7 +1252,7 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth) |
|
|
|
cipher_context_init(&evp, method, 0); |
|
|
|
|
|
|
|
static buffer_t tmp = { 0 }; |
|
|
|
brealloc(&tmp, cipher->len, cipher->capacity); |
|
|
|
brealloc(&tmp, cipher->len, capacity); |
|
|
|
buffer_t *plain = &tmp; |
|
|
|
plain->len = cipher->len - iv_len; |
|
|
|
|
|
|
@ -1295,7 +1295,7 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth) |
|
|
|
|
|
|
|
cipher_context_release(&evp); |
|
|
|
|
|
|
|
brealloc(cipher, plain->len, plain->capacity); |
|
|
|
brealloc(cipher, plain->len, capacity); |
|
|
|
memcpy(cipher->array, plain->array, plain->len); |
|
|
|
cipher->len = plain->len; |
|
|
|
|
|
|
@ -1311,7 +1311,7 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) |
|
|
|
int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx, size_t capacity) |
|
|
|
{ |
|
|
|
if (ctx != NULL) { |
|
|
|
static buffer_t tmp = { 0 }; |
|
|
@ -1319,7 +1319,7 @@ int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) |
|
|
|
size_t iv_len = 0; |
|
|
|
int err = 1; |
|
|
|
|
|
|
|
brealloc(&tmp, cipher->len, cipher->capacity); |
|
|
|
brealloc(&tmp, cipher->len, capacity); |
|
|
|
buffer_t *plain = &tmp; |
|
|
|
plain->len = cipher->len; |
|
|
|
|
|
|
@ -1345,10 +1345,10 @@ int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) |
|
|
|
|
|
|
|
if (enc_method >= SALSA20) { |
|
|
|
int padding = ctx->counter % SODIUM_BLOCK_SIZE; |
|
|
|
brealloc(plain, (plain->len + padding) * 2, plain->capacity); |
|
|
|
brealloc(plain, (plain->len + padding) * 2, capacity); |
|
|
|
|
|
|
|
if (padding) { |
|
|
|
brealloc(cipher, cipher->len + padding, cipher->capacity); |
|
|
|
brealloc(cipher, cipher->len + padding, capacity); |
|
|
|
memmove(cipher->array + iv_len + padding, cipher->array + iv_len, |
|
|
|
cipher->len - iv_len); |
|
|
|
memset(cipher->array + iv_len, 0, padding); |
|
|
@ -1379,7 +1379,7 @@ int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) |
|
|
|
dump("CIPHER", cipher->array + iv_len, cipher->len - iv_len); |
|
|
|
#endif |
|
|
|
|
|
|
|
brealloc(cipher, plain->len, cipher->capacity); |
|
|
|
brealloc(cipher, plain->len, capacity); |
|
|
|
memcpy(cipher->array, plain->array, plain->len); |
|
|
|
cipher->len = plain->len; |
|
|
|
|
|
|
@ -1514,21 +1514,21 @@ int enc_init(const char *pass, const char *method) |
|
|
|
return m; |
|
|
|
} |
|
|
|
|
|
|
|
int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx) |
|
|
|
int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx, size_t capacity) |
|
|
|
{ |
|
|
|
int i, j, k; |
|
|
|
ssize_t blen = buf->len; |
|
|
|
uint32_t cidx = chunk->idx; |
|
|
|
|
|
|
|
brealloc(chunk->buf, chunk->len + blen, buf->capacity); |
|
|
|
brealloc(buf, chunk->len + blen, buf->capacity); |
|
|
|
brealloc(chunk->buf, chunk->len + blen, capacity); |
|
|
|
brealloc(buf, chunk->len + blen, capacity); |
|
|
|
|
|
|
|
for (i = 0, j = 0, k = 0; i < blen; i++) { |
|
|
|
chunk->buf->array[cidx++] = buf->array[k++]; |
|
|
|
|
|
|
|
if (cidx == CLEN_BYTES) { |
|
|
|
uint16_t clen = ntohs(*((uint16_t *)chunk->buf->array)); |
|
|
|
brealloc(chunk->buf, clen + AUTH_BYTES, buf->capacity); |
|
|
|
brealloc(chunk->buf, clen + AUTH_BYTES, capacity); |
|
|
|
chunk->len = clen; |
|
|
|
} |
|
|
|
|
|
|
@ -1572,7 +1572,7 @@ int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx) |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
int ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx) |
|
|
|
int ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx, size_t capacity) |
|
|
|
{ |
|
|
|
ssize_t blen = buf->len; |
|
|
|
uint16_t chunk_len = htons((uint16_t)blen); |
|
|
@ -1580,7 +1580,7 @@ int ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx) |
|
|
|
uint8_t key[MAX_IV_LENGTH + sizeof(uint32_t)]; |
|
|
|
uint32_t c = htonl(*counter); |
|
|
|
|
|
|
|
brealloc(buf, AUTH_BYTES + blen, buf->capacity); |
|
|
|
brealloc(buf, AUTH_BYTES + blen, capacity); |
|
|
|
memcpy(key, ctx->evp.iv, enc_iv_len); |
|
|
|
memcpy(key + enc_iv_len, &c, sizeof(uint32_t)); |
|
|
|
#if defined(USE_CRYPTO_OPENSSL) |
|
|
|