Skip to content

Commit

Permalink
Refactor bn.cpp with more clear code/less lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 authored and ronaldtse committed Mar 11, 2021
1 parent 821569c commit fb3e2de
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/lib/crypto/bn.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2018 Ribose Inc.
* Copyright (c) 2017-2021 Ribose Inc.
* Copyright (c) 2012 Alistair Crooks <[email protected]>
* All rights reserved.
*
Expand Down Expand Up @@ -27,10 +27,9 @@
#include "bn.h"
#include <botan/ffi.h>
#include <stdlib.h>
#include <assert.h>
#include "utils.h"

/**************************************************************************/

/* essentiually, these are just wrappers around the botan functions */
/* usually the order of args changes */
/* the bignum_t API tends to have more const poisoning */
Expand All @@ -39,38 +38,35 @@
bignum_t *
bn_bin2bn(const uint8_t *data, int len, bignum_t *ret)
{
if (data == NULL) {
return bn_new();
assert(data);
if (!data) {
RNP_LOG("NULL data.");
return NULL;
}
if (ret == NULL) {
if (!ret) {
ret = bn_new();
}

if (ret == NULL) {
if (!ret) {
return NULL;
}

return (botan_mp_from_bin(ret->mp, data, len) == 0) ? ret : NULL;
return !botan_mp_from_bin(ret->mp, data, len) ? ret : NULL;
}

/* store in unsigned [big endian] format */
int
bn_bn2bin(const bignum_t *a, unsigned char *b)
{
if (a == NULL || b == NULL) {
if (!a || !b) {
return -1;
}

return botan_mp_to_bin(a->mp, b);
}

bignum_t *
bn_new(void)
{
bignum_t *a;

a = (bignum_t *) calloc(1, sizeof(*a));
if (a == NULL) {
bignum_t *a = (bignum_t *) calloc(1, sizeof(*a));
if (!a) {
return NULL;
}
botan_mp_init(&a->mp);
Expand All @@ -80,7 +76,7 @@ bn_new(void)
void
bn_free(bignum_t *a)
{
if (a != NULL) {
if (a) {
botan_mp_destroy(a->mp);
free(a);
}
Expand All @@ -89,10 +85,7 @@ bn_free(bignum_t *a)
bool
bn_num_bits(const bignum_t *a, size_t *bits)
{
if (!a || botan_mp_num_bits(a->mp, bits)) {
return false;
}
return true;
return a && !botan_mp_num_bits(a->mp, bits);
}

bool
Expand Down

0 comments on commit fb3e2de

Please sign in to comment.