changing {} style to match majority of previous style

This commit is contained in:
Philippe Teuwen 2019-03-10 11:20:22 +01:00
commit 961d929f4d
320 changed files with 5502 additions and 10485 deletions

View file

@ -2,8 +2,7 @@
extern void bucket_sort_intersect(uint32_t *const estart, uint32_t *const estop,
uint32_t *const ostart, uint32_t *const ostop,
bucket_info_t *bucket_info, bucket_array_t bucket)
{
bucket_info_t *bucket_info, bucket_array_t bucket) {
uint32_t *p1, *p2;
uint32_t *start[2];
uint32_t *stop[2];

View file

@ -31,8 +31,7 @@
*/
#include "cmd.h"
uint8_t cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len)
{
uint8_t cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) {
UsbCommand txcmd;
for (size_t i = 0; i < sizeof(UsbCommand); i++)

View file

@ -24,8 +24,7 @@
#if !defined LOWMEM && defined __GNUC__
static uint8_t filterlut[1 << 20];
static void __attribute__((constructor)) fill_lut()
{
static void __attribute__((constructor)) fill_lut() {
uint32_t i;
for (i = 0; i < 1 << 20; ++i)
filterlut[i] = filter(i);
@ -36,8 +35,7 @@ static void __attribute__((constructor)) fill_lut()
/** update_contribution
* helper, calculates the partial linear feedback contributions and puts in MSB
*/
static inline void update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2)
{
static inline void update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2) {
uint32_t p = *item >> 25;
p = p << 1 | evenparity32(*item & mask1);
@ -48,8 +46,7 @@ static inline void update_contribution(uint32_t *item, const uint32_t mask1, con
/** extend_table
* using a bit of the keystream extend the table of possible lfsr states
*/
static inline void extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in)
{
static inline void extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in) {
in <<= 24;
for (*tbl <<= 1; tbl <= *end; *++tbl <<= 1)
if (filter(*tbl) ^ filter(*tbl | 1)) {
@ -69,8 +66,7 @@ static inline void extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1,
/** extend_table_simple
* using a bit of the keystream extend the table of possible lfsr states
*/
static inline void extend_table_simple(uint32_t *tbl, uint32_t **end, int bit)
{
static inline void extend_table_simple(uint32_t *tbl, uint32_t **end, int bit) {
for (*tbl <<= 1; tbl <= *end; *++tbl <<= 1) {
if (filter(*tbl) ^ filter(*tbl | 1)) { // replace
*tbl |= filter(*tbl) ^ bit;
@ -88,8 +84,7 @@ static inline void extend_table_simple(uint32_t *tbl, uint32_t **end, int bit)
static struct Crypto1State *
recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
uint32_t *e_head, uint32_t *e_tail, uint32_t eks, int rem,
struct Crypto1State *sl, uint32_t in, bucket_array_t bucket)
{
struct Crypto1State *sl, uint32_t in, bucket_array_t bucket) {
uint32_t *o, *e;
bucket_info_t bucket_info;
@ -133,8 +128,7 @@ recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
* additionally you can use the in parameter to specify the value
* that was fed into the lfsr at the time the keystream was generated
*/
struct Crypto1State *lfsr_recovery32(uint32_t ks2, uint32_t in)
{
struct Crypto1State *lfsr_recovery32(uint32_t ks2, uint32_t in) {
struct Crypto1State *statelist;
uint32_t *odd_head = 0, *odd_tail = 0, oks = 0;
uint32_t *even_head = 0, *even_tail = 0, eks = 0;
@ -225,8 +219,7 @@ static const uint32_t C2[] = { 0x1A822E0, 0x21A822E0, 0x21A822E0};
/** Reverse 64 bits of keystream into possible cipher states
* Variation mentioned in the paper. Somewhat optimized version
*/
struct Crypto1State *lfsr_recovery64(uint32_t ks2, uint32_t ks3)
{
struct Crypto1State *lfsr_recovery64(uint32_t ks2, uint32_t ks3) {
struct Crypto1State *statelist, *sl;
uint8_t oks[32], eks[32], hi[32];
uint32_t low = 0, win = 0;
@ -296,8 +289,7 @@ continue2:
/** lfsr_rollback_bit
* Rollback the shift register in order to get previous states
*/
uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
{
uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb) {
int out;
uint8_t ret;
uint32_t t;
@ -317,8 +309,7 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
/** lfsr_rollback_byte
* Rollback the shift register in order to get previous states
*/
uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb)
{
uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb) {
uint8_t ret = 0;
ret |= lfsr_rollback_bit(s, BIT(in, 7), fb) << 7;
ret |= lfsr_rollback_bit(s, BIT(in, 6), fb) << 6;
@ -333,8 +324,7 @@ uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb)
/** lfsr_rollback_word
* Rollback the shift register in order to get previous states
*/
uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb)
{
uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb) {
uint32_t ret = 0;
ret |= lfsr_rollback_bit(s, BEBIT(in, 31), fb) << (31 ^ 24);
@ -379,8 +369,7 @@ uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb)
* x,y valid tag nonces, then prng_successor(x, nonce_distance(x, y)) = y
*/
static uint16_t *dist = 0;
int nonce_distance(uint32_t from, uint32_t to)
{
int nonce_distance(uint32_t from, uint32_t to) {
uint16_t x, i;
if (!dist) {
dist = calloc(2 << 16, sizeof(uint8_t));
@ -400,8 +389,7 @@ int nonce_distance(uint32_t from, uint32_t to)
* true = weak prng
* false = hardend prng
*/
bool validate_prng_nonce(uint32_t nonce)
{
bool validate_prng_nonce(uint32_t nonce) {
// init prng table:
nonce_distance(nonce, nonce);
return ((65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535) == 16;
@ -421,8 +409,7 @@ static uint32_t fastfwd[2][8] = {
* encrypt the NACK which is observed when varying only the 3 last bits of Nr
* only correct iff [NR_3] ^ NR_3 does not depend on Nr_3
*/
uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
{
uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd) {
uint32_t *candidates = calloc(4 << 10, sizeof(uint8_t));
if (!candidates) return 0;
@ -447,8 +434,7 @@ uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
/** check_pfx_parity
* helper function which eliminates possible secret states using parity bits
*/
static struct Crypto1State *check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8], uint32_t odd, uint32_t even, struct Crypto1State *sl, uint32_t no_par)
{
static struct Crypto1State *check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8], uint32_t odd, uint32_t even, struct Crypto1State *sl, uint32_t no_par) {
uint32_t ks1, nr, ks2, rr, ks3, c, good = 1;
for (c = 0; good && c < 8; ++c) {
@ -489,8 +475,7 @@ static struct Crypto1State *check_pfx_parity(uint32_t prefix, uint32_t rresp, ui
* tag nonce was fed in
*/
struct Crypto1State *lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint32_t no_par)
{
struct Crypto1State *lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint32_t no_par) {
struct Crypto1State *statelist, *s;
uint32_t *odd, *even, *o, *e, top;

View file

@ -67,8 +67,7 @@ extern bool validate_prng_nonce(uint32_t nonce);
#define LF_POLY_EVEN (0x870804)
#define BIT(x, n) ((x) >> (n) & 1)
#define BEBIT(x, n) BIT(x, (n) ^ 24)
static inline int filter(uint32_t const x)
{
static inline int filter(uint32_t const x) {
uint32_t f;
f = 0xf22c0 >> (x & 0xf) & 16;

View file

@ -26,8 +26,7 @@
(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
void crypto1_create(struct Crypto1State *s, uint64_t key)
{
void crypto1_create(struct Crypto1State *s, uint64_t key) {
int i;
for (i = 47; s && i > 0; i -= 2) {
@ -36,14 +35,12 @@ void crypto1_create(struct Crypto1State *s, uint64_t key)
}
return;
}
void crypto1_destroy(struct Crypto1State *state)
{
void crypto1_destroy(struct Crypto1State *state) {
state->odd = 0;
state->even = 0;
}
#else
struct Crypto1State *crypto1_create(uint64_t key)
{
struct Crypto1State *crypto1_create(uint64_t key) {
struct Crypto1State *s = malloc(sizeof(*s));
if (!s) return NULL;
@ -56,21 +53,18 @@ struct Crypto1State *crypto1_create(uint64_t key)
}
return s;
}
void crypto1_destroy(struct Crypto1State *state)
{
void crypto1_destroy(struct Crypto1State *state) {
free(state);
}
#endif
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr)
{
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr) {
int i;
for (*lfsr = 0, i = 23; i >= 0; --i) {
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
}
}
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
{
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted) {
uint32_t feedin, t;
uint8_t ret = filter(s->odd);
@ -86,8 +80,7 @@ uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
return ret;
}
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted)
{
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted) {
uint8_t ret = 0;
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
@ -99,8 +92,7 @@ uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted)
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
return ret;
}
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
{
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted) {
uint32_t ret = 0;
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
@ -143,8 +135,7 @@ uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
/* prng_successor
* helper used to obscure the keystream during authentication
*/
uint32_t prng_successor(uint32_t x, uint32_t n)
{
uint32_t prng_successor(uint32_t x, uint32_t n) {
SWAPENDIAN(x);
while (n--)
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;

View file

@ -9,16 +9,14 @@
//
#include "crc.h"
void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout)
{
void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout) {
crc_init(crc, order, polynom, initial_value, final_xor);
crc->refin = refin;
crc->refout = refout;
crc_clear(crc);
}
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor)
{
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor) {
crc->order = order;
crc->topbit = BITMASK(order - 1);
crc->polynom = polynom;
@ -30,16 +28,14 @@ void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, u
crc_clear(crc);
}
void crc_clear(crc_t *crc)
{
void crc_clear(crc_t *crc) {
crc->state = crc->initial_value & crc->mask;
if (crc->refin)
crc->state = reflect(crc->state, crc->order);
}
void crc_update2(crc_t *crc, uint32_t data, int data_width)
{
void crc_update2(crc_t *crc, uint32_t data, int data_width) {
if (crc->refin)
data = reflect(data, data_width);
@ -56,8 +52,7 @@ void crc_update2(crc_t *crc, uint32_t data, int data_width)
}
}
void crc_update(crc_t *crc, uint32_t data, int data_width)
{
void crc_update(crc_t *crc, uint32_t data, int data_width) {
if (crc->refin)
data = reflect(data, data_width);
@ -72,8 +67,7 @@ void crc_update(crc_t *crc, uint32_t data, int data_width)
}
}
uint32_t crc_finish(crc_t *crc)
{
uint32_t crc_finish(crc_t *crc) {
uint32_t val = crc->state;
if (crc->refout)
val = reflect(val, crc->order);
@ -97,8 +91,7 @@ static void print_crc(crc_t *crc) {
*/
// width=8 poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xA1 name="CRC-8/MAXIM"
uint32_t CRC8Maxim(uint8_t *buff, size_t size)
{
uint32_t CRC8Maxim(uint8_t *buff, size_t size) {
crc_t crc;
crc_init_ref(&crc, 8, 0x31, 0, 0, true, true);
for (int i = 0; i < size; ++i)
@ -106,8 +99,7 @@ uint32_t CRC8Maxim(uint8_t *buff, size_t size)
return crc_finish(&crc);
}
// width=8 poly=0x1d, init=0xc7 (0xe3 - WRONG! but it mentioned in MAD datasheet) refin=false refout=false xorout=0x00 name="CRC-8/MIFARE-MAD"
uint32_t CRC8Mad(uint8_t *buff, size_t size)
{
uint32_t CRC8Mad(uint8_t *buff, size_t size) {
crc_t crc;
crc_init_ref(&crc, 8, 0x1d, 0xc7, 0, false, false);
for (int i = 0; i < size; ++i)
@ -115,8 +107,7 @@ uint32_t CRC8Mad(uint8_t *buff, size_t size)
return crc_finish(&crc);
}
// width=4 poly=0xC, reversed poly=0x7 init=0x5 refin=true refout=true xorout=0x0000 check= name="CRC-4/LEGIC"
uint32_t CRC4Legic(uint8_t *cmd, size_t size)
{
uint32_t CRC4Legic(uint8_t *cmd, size_t size) {
crc_t crc;
crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, true, true);
crc_update2(&crc, 1, 1); /* CMD_READ */
@ -126,8 +117,7 @@ uint32_t CRC4Legic(uint8_t *cmd, size_t size)
}
// width=8 poly=0x63, reversed poly=0x8D init=0x55 refin=true refout=true xorout=0x0000 check=0xC6 name="CRC-8/LEGIC"
// the CRC needs to be reversed before returned.
uint32_t CRC8Legic(uint8_t *buff, size_t size)
{
uint32_t CRC8Legic(uint8_t *buff, size_t size) {
crc_t crc;
crc_init_ref(&crc, 8, 0x63, 0x55, 0, true, true);
for (int i = 0; i < size; ++i)

View file

@ -11,8 +11,7 @@ static uint16_t crc_table[256];
static bool crc_table_init = false;
static CrcType_t crc_type = CRC_NONE;
void init_table(CrcType_t ct)
{
void init_table(CrcType_t ct) {
// same crc algo, and initialised already
if (ct == crc_type && crc_table_init)
@ -50,8 +49,7 @@ void init_table(CrcType_t ct)
}
}
void generate_table(uint16_t polynomial, bool refin)
{
void generate_table(uint16_t polynomial, bool refin) {
uint16_t i, j, crc, c;
@ -79,16 +77,14 @@ void generate_table(uint16_t polynomial, bool refin)
crc_table_init = true;
}
void reset_table(void)
{
void reset_table(void) {
memset(crc_table, 0, sizeof(crc_table));
crc_table_init = false;
crc_type = CRC_NONE;
}
// table lookup LUT solution
uint16_t crc16_fast(uint8_t const *d, size_t n, uint16_t initval, bool refin, bool refout)
{
uint16_t crc16_fast(uint8_t const *d, size_t n, uint16_t initval, bool refin, bool refout) {
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
// only usable with polynom orders of 8, 16, 24 or 32.
@ -112,8 +108,7 @@ uint16_t crc16_fast(uint8_t const *d, size_t n, uint16_t initval, bool refin, bo
}
// bit looped solution TODO REMOVED
uint16_t update_crc16_ex(uint16_t crc, uint8_t c, uint16_t polynomial)
{
uint16_t update_crc16_ex(uint16_t crc, uint8_t c, uint16_t polynomial) {
uint16_t i, v, tmp = 0;
v = (crc ^ c) & 0xff;
@ -129,14 +124,12 @@ uint16_t update_crc16_ex(uint16_t crc, uint8_t c, uint16_t polynomial)
}
return ((crc >> 8) ^ tmp) & 0xffff;
}
uint16_t update_crc16(uint16_t crc, uint8_t c)
{
uint16_t update_crc16(uint16_t crc, uint8_t c) {
return update_crc16_ex(crc, c, CRC16_POLY_CCITT);
}
// two ways. msb or lsb loop.
uint16_t crc16(uint8_t const *d, size_t length, uint16_t remainder, uint16_t polynomial, bool refin, bool refout)
{
uint16_t crc16(uint8_t const *d, size_t length, uint16_t remainder, uint16_t polynomial, bool refin, bool refout) {
if (length == 0)
return (~remainder);
@ -163,8 +156,7 @@ uint16_t crc16(uint8_t const *d, size_t length, uint16_t remainder, uint16_t pol
return remainder;
}
void compute_crc(CrcType_t ct, const uint8_t *d, size_t n, uint8_t *first, uint8_t *second)
{
void compute_crc(CrcType_t ct, const uint8_t *d, size_t n, uint8_t *first, uint8_t *second) {
// can't calc a crc on less than 1 byte
if (n == 0) return;
@ -199,8 +191,7 @@ void compute_crc(CrcType_t ct, const uint8_t *d, size_t n, uint8_t *first, uint8
*first = (crc & 0xFF);
*second = ((crc >> 8) & 0xFF);
}
uint16_t crc(CrcType_t ct, const uint8_t *d, size_t n)
{
uint16_t crc(CrcType_t ct, const uint8_t *d, size_t n) {
// can't calc a crc on less than 3 byte. (1byte + 2 crc bytes)
if (n < 3) return 0;
@ -236,8 +227,7 @@ uint16_t crc(CrcType_t ct, const uint8_t *d, size_t n)
// crc16 algos like CRC-A become 0x000
// while CRC-15693 become 0x0F47
// If calculated with crc bytes, the residue should be 0xF0B8
bool check_crc(CrcType_t ct, const uint8_t *d, size_t n)
{
bool check_crc(CrcType_t ct, const uint8_t *d, size_t n) {
// can't calc a crc on less than 3 byte. (1byte + 2 crc bytes)
if (n < 3) return false;
@ -265,22 +255,19 @@ bool check_crc(CrcType_t ct, const uint8_t *d, size_t n)
}
// poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 residue=0x0000 name="CRC-16/CCITT-FALSE"
uint16_t crc16_ccitt(uint8_t const *d, size_t n)
{
uint16_t crc16_ccitt(uint8_t const *d, size_t n) {
return crc16_fast(d, n, 0xffff, false, false);
}
// FDX-B ISO11784/85) uses KERMIT
// poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 name="KERMIT"
uint16_t crc16_kermit(uint8_t const *d, size_t n)
{
uint16_t crc16_kermit(uint8_t const *d, size_t n) {
return crc16_fast(d, n, 0x0000, true, true);
}
// FeliCa uses XMODEM
// poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 name="XMODEM"
uint16_t crc16_xmodem(uint8_t const *d, size_t n)
{
uint16_t crc16_xmodem(uint8_t const *d, size_t n) {
return crc16_fast(d, n, 0x0000, false, false);
}
@ -289,16 +276,14 @@ uint16_t crc16_xmodem(uint8_t const *d, size_t n)
// ISO 14443 CRC-B
// ISO/IEC 13239 (formerly ISO/IEC 3309)
// poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff name="X-25"
uint16_t crc16_x25(uint8_t const *d, size_t n)
{
uint16_t crc16_x25(uint8_t const *d, size_t n) {
uint16_t crc = crc16_fast(d, n, 0xffff, true, true);
crc = ~crc;
return crc;
}
// CRC-A (14443-3)
// poly=0x1021 init=0xc6c6 refin=true refout=true xorout=0x0000 name="CRC-A"
uint16_t crc16_a(uint8_t const *d, size_t n)
{
uint16_t crc16_a(uint8_t const *d, size_t n) {
return crc16_fast(d, n, 0xC6C6, true, true);
}
@ -306,15 +291,13 @@ uint16_t crc16_a(uint8_t const *d, size_t n)
// initvalue 0x4807 reflected 0xE012
// poly 0x1021 reflected 0x8408
// poly=0x1021 init=0x4807 refin=true refout=true xorout=0x0BC3 check=0xF0B8 name="CRC-16/ICLASS"
uint16_t crc16_iclass(uint8_t const *d, size_t n)
{
uint16_t crc16_iclass(uint8_t const *d, size_t n) {
return crc16_fast(d, n, 0x4807, true, true);
}
// This CRC-16 is used in Legic Advant systems.
// poly=0xB400, init=depends refin=true refout=true xorout=0x0000 check= name="CRC-16/LEGIC"
uint16_t crc16_legic(uint8_t const *d, size_t n, uint8_t uidcrc)
{
uint16_t crc16_legic(uint8_t const *d, size_t n, uint8_t uidcrc) {
uint16_t initial = uidcrc << 8 | uidcrc;
return crc16_fast(d, n, initial, true, true);
}

View file

@ -5,8 +5,7 @@
static void crc32_byte(uint32_t *crc, const uint8_t value);
static void crc32_byte(uint32_t *crc, const uint8_t value)
{
static void crc32_byte(uint32_t *crc, const uint8_t value) {
/* x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 */
const uint32_t poly = 0xEDB88320;
@ -19,8 +18,7 @@ static void crc32_byte(uint32_t *crc, const uint8_t value)
}
}
void crc32_ex(const uint8_t *data, const size_t len, uint8_t *crc)
{
void crc32_ex(const uint8_t *data, const size_t len, uint8_t *crc) {
uint32_t desfire_crc = CRC32_PRESET;
for (size_t i = 0; i < len; i++) {
crc32_byte(&desfire_crc, data[i]);
@ -29,7 +27,6 @@ void crc32_ex(const uint8_t *data, const size_t len, uint8_t *crc)
*((uint32_t *)(crc)) = htole32(desfire_crc);
}
void crc32_append(uint8_t *data, const size_t len)
{
void crc32_append(uint8_t *data, const size_t len) {
crc32_ex(data, len, data + len);
}

View file

@ -72,8 +72,7 @@ const uint64_t crc64_table[] = {
0x5DEDC41A34BBEEB2, 0x1F1D25F19D51D821, 0xD80C07CD676F8394, 0x9AFCE626CE85B507
};
void crc64(const uint8_t *data, const size_t len, uint64_t *crc)
{
void crc64(const uint8_t *data, const size_t len, uint64_t *crc) {
for (size_t i = 0; i < len; i++) {
uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;

View file

@ -30,8 +30,7 @@ volatile unsigned long c;
// timer.
// I2CSpinDelayClk(4) = 12.31us
// I2CSpinDelayClk(1) = 3.07us
void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay)
{
void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
for (c = delay * 2; c; c--) {};
}
@ -42,8 +41,7 @@ void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay)
#define ISO7618_MAX_FRAME 255
// try i2c bus recovery at 100kHz = 5uS high, 5uS low
void I2C_recovery(void)
{
void I2C_recovery(void) {
DbpString("Performing i2c bus recovery");
@ -76,8 +74,7 @@ void I2C_recovery(void)
DbpString("I2C bus recovery complete");
}
void I2C_init(void)
{
void I2C_init(void) {
// Configure reset pin, close up pull up, push-pull output, default high
AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST;
AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST;
@ -100,8 +97,7 @@ void I2C_init(void)
}
// set the reset state
void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA)
{
void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA) {
if (LineRST)
HIGH(GPIO_RST);
else
@ -120,8 +116,7 @@ void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA)
// Reset the SIM_Adapter, then enter the main program
// Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
void I2C_Reset_EnterMainProgram(void)
{
void I2C_Reset_EnterMainProgram(void) {
StartTicks();
I2C_init();
I2C_SetResetStatus(0, 0, 0);
@ -134,8 +129,7 @@ void I2C_Reset_EnterMainProgram(void)
// Reset the SIM_Adapter, then enter the bootloader program
// Reserve for firmware update.
void I2C_Reset_EnterBootloader(void)
{
void I2C_Reset_EnterBootloader(void) {
StartTicks();
I2C_init();
I2C_SetResetStatus(0, 1, 1);
@ -145,8 +139,7 @@ void I2C_Reset_EnterBootloader(void)
}
// Wait for the clock to go High.
bool WaitSCL_H_delay(uint32_t delay)
{
bool WaitSCL_H_delay(uint32_t delay) {
while (delay--) {
if (SCL_read) {
return true;
@ -158,13 +151,11 @@ bool WaitSCL_H_delay(uint32_t delay)
// 5000 * 3.07us = 15350us. 15.35ms
// 15000 * 3.07us = 46050us. 46.05ms
bool WaitSCL_H(void)
{
bool WaitSCL_H(void) {
return WaitSCL_H_delay(15000);
}
bool WaitSCL_L_delay(uint32_t delay)
{
bool WaitSCL_L_delay(uint32_t delay) {
while (delay--) {
if (!SCL_read) {
return true;
@ -174,16 +165,14 @@ bool WaitSCL_L_delay(uint32_t delay)
return false;
}
// 5000 * 3.07us = 15350us. 15.35ms
bool WaitSCL_L(void)
{
bool WaitSCL_L(void) {
return WaitSCL_L_delay(15000);
}
// Wait max 1800ms or until SCL goes LOW.
// It timeout reading response from card
// Which ever comes first
bool WaitSCL_L_timeout(void)
{
bool WaitSCL_L_timeout(void) {
volatile uint16_t delay = 1800;
while (delay--) {
// exit on SCL LOW
@ -195,8 +184,7 @@ bool WaitSCL_L_timeout(void)
return (delay == 0);
}
bool I2C_Start(void)
{
bool I2C_Start(void) {
I2C_DELAY_XCLK(4);
SDA_H;
@ -214,8 +202,7 @@ bool I2C_Start(void)
return true;
}
bool I2C_WaitForSim()
{
bool I2C_WaitForSim() {
// wait for data from card
if (!WaitSCL_L_timeout())
@ -231,8 +218,7 @@ bool I2C_WaitForSim()
}
// send i2c STOP
void I2C_Stop(void)
{
void I2C_Stop(void) {
SCL_L;
I2C_DELAY_2CLK;
SDA_L;
@ -245,8 +231,7 @@ void I2C_Stop(void)
}
// Send i2c ACK
void I2C_Ack(void)
{
void I2C_Ack(void) {
SCL_L;
I2C_DELAY_2CLK;
SDA_L;
@ -259,8 +244,7 @@ void I2C_Ack(void)
}
// Send i2c NACK
void I2C_NoAck(void)
{
void I2C_NoAck(void) {
SCL_L;
I2C_DELAY_2CLK;
SDA_H;
@ -272,8 +256,7 @@ void I2C_NoAck(void)
I2C_DELAY_2CLK;
}
bool I2C_WaitAck(void)
{
bool I2C_WaitAck(void) {
SCL_L;
I2C_DELAY_1CLK;
SDA_H;
@ -292,8 +275,7 @@ bool I2C_WaitAck(void)
return true;
}
void I2C_SendByte(uint8_t data)
{
void I2C_SendByte(uint8_t data) {
uint8_t bits = 8;
while (bits--) {
@ -319,8 +301,7 @@ void I2C_SendByte(uint8_t data)
SCL_L;
}
int16_t I2C_ReadByte(void)
{
int16_t I2C_ReadByte(void) {
uint8_t bits = 8, b = 0;
SDA_H;
@ -343,8 +324,7 @@ int16_t I2C_ReadByte(void)
}
// Sends one byte ( command to be written, SlaveDevice address)
bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address)
{
bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
bool bBreak = true;
do {
if (!I2C_Start())
@ -370,8 +350,7 @@ bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address)
}
// Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ).
bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address)
{
bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) {
bool bBreak = true;
do {
if (!I2C_Start())
@ -402,8 +381,7 @@ bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address)
//Sends array of data (Array, length, command to be written , SlaveDevice address ).
// len = uint8 (max buffer to write 256bytes)
bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address)
{
bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
bool bBreak = true;
do {
if (!I2C_Start())
@ -441,8 +419,7 @@ bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t dev
// read one array of data (Data array, Readout length, command to be written , SlaveDevice address ).
// len = uint8 (max buffer to read 256bytes)
int16_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address)
{
int16_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
if (!data || len == 0)
return 0;
@ -513,8 +490,7 @@ int16_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
return --readcount;
}
int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address)
{
int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
//START, 0xB0, 0x00, 0x00, START, 0xB1, xx, yy, zz, ......, STOP
bool bBreak = true;
uint8_t readcount = 0;
@ -576,8 +552,7 @@ int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t
return readcount;
}
bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address)
{
bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
//START, 0xB0, 0x00, 0x00, xx, yy, zz, ......, STOP
bool bBreak = true;
@ -619,8 +594,7 @@ bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t d
return true;
}
void I2C_print_status(void)
{
void I2C_print_status(void) {
DbpString("Smart card module (ISO 7816)");
uint8_t resp[] = {0, 0, 0, 0};
I2C_Reset_EnterMainProgram();
@ -632,8 +606,7 @@ void I2C_print_status(void)
}
// Will read response from smart card module, retries 3 times to get the data.
bool sc_rx_bytes(uint8_t *dest, uint8_t *destlen)
{
bool sc_rx_bytes(uint8_t *dest, uint8_t *destlen) {
uint8_t i = 3;
int16_t len = 0;
@ -660,8 +633,7 @@ bool sc_rx_bytes(uint8_t *dest, uint8_t *destlen)
return true;
}
bool GetATR(smart_card_atr_t *card_ptr)
{
bool GetATR(smart_card_atr_t *card_ptr) {
if (!card_ptr)
return false;
@ -713,8 +685,7 @@ bool GetATR(smart_card_atr_t *card_ptr)
return true;
}
void SmartCardAtr(void)
{
void SmartCardAtr(void) {
smart_card_atr_t card;
LED_D_ON();
clear_trace();
@ -726,8 +697,7 @@ void SmartCardAtr(void)
LEDsoff();
}
void SmartCardRaw(uint64_t arg0, uint64_t arg1, uint8_t *data)
{
void SmartCardRaw(uint64_t arg0, uint64_t arg1, uint8_t *data) {
LED_D_ON();
@ -779,8 +749,7 @@ OUT:
LEDsoff();
}
void SmartCardUpgrade(uint64_t arg0)
{
void SmartCardUpgrade(uint64_t arg0) {
LED_C_ON();
@ -840,12 +809,10 @@ void SmartCardUpgrade(uint64_t arg0)
BigBuf_free();
}
void SmartCardSetBaud(uint64_t arg0)
{
void SmartCardSetBaud(uint64_t arg0) {
}
void SmartCardSetClock(uint64_t arg0)
{
void SmartCardSetClock(uint64_t arg0) {
LED_D_ON();
set_tracing(true);
I2C_Reset_EnterMainProgram();

View file

@ -9,8 +9,7 @@
#include "iso14443crc.h"
uint16_t UpdateCrc14443(uint8_t b, uint16_t *crc)
{
uint16_t UpdateCrc14443(uint8_t b, uint16_t *crc) {
b = (b ^ (uint8_t)((*crc) & 0x00FF));
b = (b ^ (b << 4));
*crc = (*crc >> 8) ^ ((uint16_t) b << 8) ^ ((uint16_t) b << 3) ^ ((uint16_t) b >> 4);
@ -18,8 +17,7 @@ uint16_t UpdateCrc14443(uint8_t b, uint16_t *crc)
}
void ComputeCrc14443(uint16_t CrcType, const uint8_t *data, int length,
uint8_t *TransmitFirst, uint8_t *TransmitSecond)
{
uint8_t *TransmitFirst, uint8_t *TransmitSecond) {
uint8_t b;
uint16_t crc = CrcType;
@ -36,8 +34,7 @@ void ComputeCrc14443(uint16_t CrcType, const uint8_t *data, int length,
return;
}
bool CheckCrc14443(uint16_t CrcType, const uint8_t *data, int length)
{
bool CheckCrc14443(uint16_t CrcType, const uint8_t *data, int length) {
if (length < 3) return false;
uint8_t b1, b2;
ComputeCrc14443(CrcType, data, length - 2, &b1, &b2);

View file

@ -12,8 +12,7 @@
// target char* buffer, where to put the UID, if NULL a static buffer is returned
// uid[] the UID in transmission order
// return: ptr to string
char *Iso15693sprintUID(char *target, uint8_t *uid)
{
char *Iso15693sprintUID(char *target, uint8_t *uid) {
static char tempbuf[2 * 8 + 1] = {0};
if (target == NULL)

View file

@ -27,8 +27,7 @@ struct lfsr {
// it sets b to 0 aswell to make sure we get a all zero keystream out
// which is used in the initialisation phase sending the IV
//
void legic_prng_init(uint8_t iv)
{
void legic_prng_init(uint8_t iv) {
lfsr.a = iv;
lfsr.b = 0; // hack to get a always 0 keystream
lfsr.c = 0;
@ -36,8 +35,7 @@ void legic_prng_init(uint8_t iv)
lfsr.b = (iv << 1) | 1;
}
void legic_prng_forward(int count)
{
void legic_prng_forward(int count) {
if (count == 0) return;
lfsr.c += count;
@ -48,19 +46,16 @@ void legic_prng_forward(int count)
}
}
uint32_t legic_prng_count()
{
uint32_t legic_prng_count() {
return lfsr.c;
}
uint8_t legic_prng_get_bit()
{
uint8_t legic_prng_get_bit() {
uint8_t idx = 7 - ((lfsr.a & 4) | (lfsr.a >> 2 & 2) | (lfsr.a >> 4 & 1));
return lfsr.b >> idx & 1;
}
uint32_t legic_prng_get_bits(uint8_t len)
{
uint32_t legic_prng_get_bits(uint8_t len) {
uint32_t a = 0;
for (uint8_t i = 0; i < len; ++i) {
a |= legic_prng_get_bit() << i;

View file

@ -64,13 +64,11 @@ uint8_t g_debugMode = 0;
#endif
signal_t signalprop = { 255, -255, 0, 0, true };
signal_t *getSignalProperties(void)
{
signal_t *getSignalProperties(void) {
return &signalprop;
}
static void resetSignal(void)
{
static void resetSignal(void) {
signalprop.low = 255;
signalprop.high = -255;
signalprop.mean = 0;
@ -78,8 +76,7 @@ static void resetSignal(void)
signalprop.isnoise = true;
}
static void printSignal(void)
{
static void printSignal(void) {
prnt("LF signal properties:");
prnt(" high..........%d", signalprop.high);
prnt(" low...........%d", signalprop.low);
@ -89,8 +86,7 @@ static void printSignal(void)
prnt(" THRESHOLD noise amplitude......%d", NOISE_AMPLITUDE_THRESHOLD);
}
void computeSignalProperties(uint8_t *samples, uint32_t size)
{
void computeSignalProperties(uint8_t *samples, uint32_t size) {
resetSignal();
uint32_t sum = 0;
@ -111,8 +107,7 @@ void computeSignalProperties(uint8_t *samples, uint32_t size)
printSignal();
}
void removeSignalOffset(uint8_t *samples, uint32_t size)
{
void removeSignalOffset(uint8_t *samples, uint32_t size) {
if (samples == NULL || size < SIGNAL_MIN_SAMPLES) return;
int acc_off = 0;
@ -134,8 +129,7 @@ void removeSignalOffset(uint8_t *samples, uint32_t size)
//by marshmellow
//get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise
//void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo)
{
void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
// add fuzz.
*high = (signalprop.high * fuzzHi) / 100;
if (signalprop.low < 0) {
@ -159,16 +153,14 @@ void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo)
// by marshmellow
// pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
// returns 1 if passed
bool parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType)
{
bool parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType) {
return oddparity32(bits) ^ pType;
}
//by marshmellow
// takes a array of binary values, start position, length of bits per parity (includes parity bit - MAX 32),
// Parity Type (1 for odd; 0 for even; 2 for Always 1's; 3 for Always 0's), and binary Length (length to run)
size_t removeParity(uint8_t *bits, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen)
{
size_t removeParity(uint8_t *bits, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen) {
uint32_t parityWd = 0;
size_t bitCnt = 0;
for (int word = 0; word < (bLen); word += pLen) {
@ -203,8 +195,7 @@ size_t removeParity(uint8_t *bits, size_t startIdx, uint8_t pLen, uint8_t pType,
// takes a array of binary values, length of bits per parity (includes parity bit),
// Parity Type (1 for odd; 0 for even; 2 Always 1's; 3 Always 0's), and binary Length (length to run)
// Make sure *dest is long enough to store original sourceLen + #_of_parities_to_be_added
size_t addParity(uint8_t *src, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType)
{
size_t addParity(uint8_t *src, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType) {
uint32_t parityWd = 0;
size_t j = 0, bitCnt = 0;
for (int word = 0; word < sourceLen; word += pLen - 1) {
@ -233,8 +224,7 @@ size_t addParity(uint8_t *src, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, u
}
// array must be size dividable with 8
uint8_t bits_to_array(const uint8_t *bits, size_t size, uint8_t *dest)
{
uint8_t bits_to_array(const uint8_t *bits, size_t size, uint8_t *dest) {
if ((size == 0) || (size % 8) != 0) return 0;
for (uint32_t i = 0; i < (size / 8); i++)
@ -243,8 +233,7 @@ uint8_t bits_to_array(const uint8_t *bits, size_t size, uint8_t *dest)
return 0;
}
uint32_t bytebits_to_byte(uint8_t *src, size_t numbits)
{
uint32_t bytebits_to_byte(uint8_t *src, size_t numbits) {
uint32_t num = 0;
for (int i = 0 ; i < numbits ; i++) {
num = (num << 1) | (*src);
@ -254,8 +243,7 @@ uint32_t bytebits_to_byte(uint8_t *src, size_t numbits)
}
//least significant bit first
uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits)
{
uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits) {
uint32_t num = 0;
for (int i = 0 ; i < numbits ; i++) {
num = (num << 1) | *(src + (numbits - (i + 1)));
@ -265,16 +253,14 @@ uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits)
//by marshmellow
//search for given preamble in given BitStream and return success = TRUE or fail = FALSE and startIndex and length
bool preambleSearch(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx)
{
bool preambleSearch(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx) {
return preambleSearchEx(bits, preamble, pLen, size, startIdx, false);
}
//by marshmellow
// search for given preamble in given BitStream and return success=1 or fail=0 and startIndex (where it was found) and length if not fineone
// fineone does not look for a repeating preamble for em4x05/4x69 sends preamble once, so look for it once in the first pLen bits
//(iceman) FINDONE, only finds start index. NOT SIZE!. I see Em410xDecode (lfdemod.c) uses SIZE to determine success
bool preambleSearchEx(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone)
{
bool preambleSearchEx(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone) {
// Sanity check. If preamble length is bigger than bits length.
if (*size <= pLen) return false;
@ -299,8 +285,7 @@ bool preambleSearchEx(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *siz
}
// find start of modulating data (for fsk and psk) in case of beginning noise or slow chip startup.
size_t findModStart(uint8_t *src, size_t size, uint8_t expWaveSize)
{
size_t findModStart(uint8_t *src, size_t size, uint8_t expWaveSize) {
size_t i = 0;
size_t waveSizeCnt = 0;
uint8_t thresholdCnt = 0;
@ -325,8 +310,7 @@ size_t findModStart(uint8_t *src, size_t size, uint8_t expWaveSize)
return i;
}
int getClosestClock(int testclk)
{
int getClosestClock(int testclk) {
uint16_t clocks[] = {8, 16, 32, 40, 50, 64, 128, 256, 384};
uint8_t limit[] = {1, 2, 4, 4, 5, 8, 8, 8, 8};
@ -337,21 +321,18 @@ int getClosestClock(int testclk)
return 0;
}
void getNextLow(uint8_t *samples, size_t size, int low, size_t *i)
{
void getNextLow(uint8_t *samples, size_t size, int low, size_t *i) {
while ((samples[*i] > low) && (*i < size))
*i += 1;
}
void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i)
{
void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i) {
while ((samples[*i] < high) && (*i < size))
*i += 1;
}
// load wave counters
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low)
{
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) {
size_t i = 0, firstLow, firstHigh;
//size_t testsize = (size < 512) ? size : 512;
@ -391,8 +372,7 @@ bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int
return true;
}
size_t pskFindFirstPhaseShift(uint8_t *samples, size_t size, uint8_t *curPhase, size_t waveStart, uint16_t fc, uint16_t *fullWaveLen)
{
size_t pskFindFirstPhaseShift(uint8_t *samples, size_t size, uint8_t *curPhase, size_t waveStart, uint16_t fc, uint16_t *fullWaveLen) {
uint16_t loopCnt = (size + 3 < 4096) ? size : 4096; //don't need to loop through entire array...
uint16_t avgWaveVal = 0, lastAvgWaveVal = 0;
@ -421,8 +401,7 @@ size_t pskFindFirstPhaseShift(uint8_t *samples, size_t size, uint8_t *curPhase,
//by marshmellow
//amplify based on ask edge detection - not accurate enough to use all the time
void askAmp(uint8_t *bits, size_t size)
{
void askAmp(uint8_t *bits, size_t size) {
uint8_t last = 128;
for (size_t i = 1; i < size; ++i) {
if (bits[i] - bits[i - 1] >= 30) //large jump up
@ -435,8 +414,7 @@ void askAmp(uint8_t *bits, size_t size)
}
// iceman, simplify this
uint32_t manchesterEncode2Bytes(uint16_t datain)
{
uint32_t manchesterEncode2Bytes(uint16_t datain) {
uint32_t output = 0;
uint8_t curBit = 0;
for (uint8_t i = 0; i < 16; i++) {
@ -449,8 +427,7 @@ uint32_t manchesterEncode2Bytes(uint16_t datain)
//by marshmellow
//encode binary data into binary manchester
//NOTE: bitstream must have triple the size of "size" available in memory to do the swap
int ManchesterEncode(uint8_t *bits, size_t size)
{
int ManchesterEncode(uint8_t *bits, size_t size) {
//allow up to 4096b out (means bits must be at least 2048+4096 to handle the swap)
size = (size > 2048) ? 2048 : size;
size_t modIdx = size;
@ -468,8 +445,7 @@ int ManchesterEncode(uint8_t *bits, size_t size)
// by marshmellow
// to detect a wave that has heavily clipped (clean) samples
// loop 512 samples, if 250 of them is deemed maxed out, we assume the wave is clipped.
bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low)
{
bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low) {
bool allArePeaks = true;
uint16_t cntPeaks = 0;
size_t loopEnd = 512 + 160;
@ -500,8 +476,7 @@ bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low)
// by marshmellow
// to help detect clocks on heavily clipped samples
// based on count of low to low
int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock)
{
int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock) {
size_t startwave;
size_t i = 100;
size_t minClk = 512;
@ -540,8 +515,7 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
// maybe somehow adjust peak trimming value based on samples to fix?
// return start index of best starting position for that clock and return clock (by reference)
int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr)
{
int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
//don't need to loop through entire array. (cotag has clock of 384)
uint16_t loopCnt = 1500;
@ -680,8 +654,7 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr)
return bestStart[best];
}
int DetectStrongNRZClk(uint8_t *dest, size_t size, int peak, int low, bool *strong)
{
int DetectStrongNRZClk(uint8_t *dest, size_t size, int peak, int low, bool *strong) {
//find shortest transition from high to low
*strong = false;
size_t i = 0;
@ -726,8 +699,7 @@ int DetectStrongNRZClk(uint8_t *dest, size_t size, int peak, int low, bool *stro
//by marshmellow
//detect nrz clock by reading #peaks vs no peaks(or errors)
int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx)
{
int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx) {
size_t i = 0;
uint8_t clk[] = {8, 16, 32, 40, 50, 64, 100, 128, 255};
size_t loopCnt = 4096; //don't need to loop through entire array...
@ -857,8 +829,7 @@ int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx)
//countFC is to detect the field clock lengths.
//counts and returns the 2 most common wave lengths
//mainly used for FSK field clock detection
uint16_t countFC(uint8_t *bits, size_t size, bool fskAdj)
{
uint16_t countFC(uint8_t *bits, size_t size, bool fskAdj) {
uint8_t fcLens[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint16_t fcCnts[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t fcLensFnd = 0;
@ -948,8 +919,7 @@ uint16_t countFC(uint8_t *bits, size_t size, bool fskAdj)
//by marshmellow
//detect psk clock by reading each phase shift
// a phase shift is determined by measuring the sample length of each wave
int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc)
{
int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc) {
uint8_t clk[] = {255, 16, 32, 40, 50, 64, 100, 128, 255}; //255 is not a valid clock
uint16_t loopCnt = 4096; //don't need to loop through entire array...
@ -1048,8 +1018,7 @@ int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShif
//by marshmellow
//detects the bit clock for FSK given the high and low Field Clocks
uint8_t detectFSKClk(uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge)
{
uint8_t detectFSKClk(uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge) {
if (size == 0)
return 0;
@ -1163,8 +1132,7 @@ uint8_t detectFSKClk(uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow,
// look for Sequence Terminator - should be pulses of clk*(1 or 2), clk*2, clk*(1.5 or 2), by idx we mean graph position index...
bool findST(int *stStopLoc, int *stStartIdx, int lowToLowWaveLen[], int highToLowWaveLen[], int clk, int tol, int buffSize, size_t *i)
{
bool findST(int *stStopLoc, int *stStartIdx, int lowToLowWaveLen[], int highToLowWaveLen[], int clk, int tol, int buffSize, size_t *i) {
if (buffSize < *i + 4) return false;
for (; *i < buffSize - 4; *i += 1) {
@ -1184,8 +1152,7 @@ bool findST(int *stStopLoc, int *stStartIdx, int lowToLowWaveLen[], int highToLo
}
//by marshmellow
//attempt to identify a Sequence Terminator in ASK modulated raw wave
bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, size_t *stend)
{
bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, size_t *stend) {
size_t bufsize = *size;
//need to loop through all samples and identify our clock, look for the ST pattern
int clk = 0;
@ -1312,8 +1279,7 @@ bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, s
//check for phase errors - should never have half a 1 or 0 by itself and should never exceed 1111 or 0000 in a row
//decodes miller encoded binary
//NOTE askrawdemod will NOT demod miller encoded ask unless the clock is manually set to 1/2 what it is detected as!
int millerRawDecode(uint8_t *bits, size_t *size, int invert)
{
int millerRawDecode(uint8_t *bits, size_t *size, int invert) {
if (*size < 16) return -1;
uint16_t MaxBits = 512, errCnt = 0;
@ -1351,8 +1317,7 @@ int millerRawDecode(uint8_t *bits, size_t *size, int invert)
//take 01 or 10 = 1 and 11 or 00 = 0
//check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
//decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
int BiphaseRawDecode(uint8_t *bits, size_t *size, int *offset, int invert)
{
int BiphaseRawDecode(uint8_t *bits, size_t *size, int *offset, int invert) {
//sanity check
if (*size < 51) return -1;
@ -1392,8 +1357,7 @@ int BiphaseRawDecode(uint8_t *bits, size_t *size, int *offset, int invert)
//by marshmellow
//take 10 and 01 and manchester decode
//run through 2 times and take least errCnt
int manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos)
{
int manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos) {
// sanity check
if (*size < 16) return -1;
@ -1433,8 +1397,7 @@ int manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos)
//by marshmellow
//demodulates strong heavily clipped samples
//RETURN: num of errors. if 0, is ok.
int cleanAskRawDemod(uint8_t *bits, size_t *size, int clk, int invert, int high, int low, int *startIdx)
{
int cleanAskRawDemod(uint8_t *bits, size_t *size, int clk, int invert, int high, int low, int *startIdx) {
*startIdx = 0;
size_t bitCnt = 0, smplCnt = 1, errCnt = 0, pos = 0;
uint8_t cl_4 = clk / 4;
@ -1505,8 +1468,7 @@ int cleanAskRawDemod(uint8_t *bits, size_t *size, int clk, int invert, int high,
//by marshmellow
//attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx)
{
int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx) {
if (*size == 0) return -1;
@ -1604,16 +1566,14 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
return errCnt;
}
int askdemod(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType)
{
int askdemod(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType) {
int start = 0;
return askdemod_ext(bits, size, clk, invert, maxErr, amp, askType, &start);
}
// by marshmellow - demodulate NRZ wave - requires a read with strong signal
// peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int *startIdx)
{
int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int *startIdx) {
if (signalprop.isnoise) return -1;
size_t clkStartIdx = 0;
@ -1663,8 +1623,7 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int *startId
}
//translate wave to 11111100000 (1 for each short wave [higher freq] 0 for each long wave [lower freq])
size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow, int *startIdx)
{
size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow, int *startIdx) {
if (size < 1024) return 0; // not enough samples
@ -1762,8 +1721,7 @@ size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow,
//translate 11111100000 to 10
//rfLen = clock, fchigh = larger field clock, fclow = smaller field clock
size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx)
{
size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx) {
uint8_t lastval = dest[0];
size_t i = 0;
@ -1822,8 +1780,7 @@ size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, u
//by marshmellow (from holiman's base)
// full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *start_idx)
{
size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *start_idx) {
if (signalprop.isnoise) return 0;
// FSK demodulator
size = fsk_wave_demod(dest, size, fchigh, fclow, start_idx);
@ -1835,8 +1792,7 @@ size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8
// convert psk1 demod to psk2 demod
// only transition waves are 1s
//TODO: Iceman - hard coded value 7, should be #define
void psk1TOpsk2(uint8_t *bits, size_t size)
{
void psk1TOpsk2(uint8_t *bits, size_t size) {
uint8_t lastbit = bits[0];
for (size_t i = 1; i < size; i++) {
//ignore errors
@ -1854,8 +1810,7 @@ void psk1TOpsk2(uint8_t *bits, size_t size)
// by marshmellow
// convert psk2 demod to psk1 demod
// from only transition waves are 1s to phase shifts change bit
void psk2TOpsk1(uint8_t *bits, size_t size)
{
void psk2TOpsk1(uint8_t *bits, size_t size) {
uint8_t phase = 0;
for (size_t i = 0; i < size; i++) {
if (bits[i] == 1) {
@ -1868,8 +1823,7 @@ void psk2TOpsk1(uint8_t *bits, size_t size)
//by marshmellow - demodulate PSK1 wave
//uses wave lengths (# Samples)
//TODO: Iceman - hard coded value 7, should be #define
int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *startIdx)
{
int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *startIdx) {
// sanity check
if (*size < 170) return -1;
@ -1951,8 +1905,7 @@ int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *s
return errCnt;
}
int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert)
{
int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert) {
int start_idx = 0;
return pskRawDemod_ext(dest, size, clock, invert, &start_idx);
}
@ -1965,8 +1918,7 @@ int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert)
// by marshmellow
// FSK Demod then try to locate an AWID ID
int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx)
{
int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx) {
//make sure buffer has enough data (96bits * 50clock samples)
if (*size < 96 * 50) return -1;
@ -1991,8 +1943,7 @@ int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx)
//by marshmellow
//takes 1s and 0s and searches for EM410x format - output EM ID
int Em410xDecode(uint8_t *bits, size_t *size, size_t *start_idx, uint32_t *hi, uint64_t *lo)
{
int Em410xDecode(uint8_t *bits, size_t *size, size_t *start_idx, uint32_t *hi, uint64_t *lo) {
// sanity check
if (bits[1] > 1) return -1;
if (*size < 64) return -2;
@ -2033,8 +1984,7 @@ int Em410xDecode(uint8_t *bits, size_t *size, size_t *start_idx, uint32_t *hi, u
// loop to get raw HID waveform then FSK demodulate the TAG ID from it
int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx)
{
int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx) {
//make sure buffer has data
if (*size < 96 * 50) return -1;
@ -2075,8 +2025,7 @@ int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32
// Find IDTEC PSK1, RF Preamble == 0x4944544B, Demodsize 64bits
// by iceman
int detectIdteck(uint8_t *dest, size_t *size)
{
int detectIdteck(uint8_t *dest, size_t *size) {
//make sure buffer has data
if (*size < 64 * 2) return -1;
@ -2094,8 +2043,7 @@ int detectIdteck(uint8_t *dest, size_t *size)
return (int)start_idx;
}
int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx)
{
int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) {
//make sure buffer has data
if (*size < 66 * 64) return -1;
@ -2125,11 +2073,11 @@ int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx)
if (*size != 64) return -5;
if (!dest[start_idx + 8]
&& dest[start_idx + 17] == 1
&& dest[start_idx + 26] == 1
&& dest[start_idx + 35] == 1
&& dest[start_idx + 44] == 1
&& dest[start_idx + 53] == 1) {
&& dest[start_idx + 17] == 1
&& dest[start_idx + 26] == 1
&& dest[start_idx + 35] == 1
&& dest[start_idx + 44] == 1
&& dest[start_idx + 53] == 1) {
//confirmed proper separator bits found
//return start position
return (int) start_idx;

View file

@ -393,8 +393,7 @@ static uint32_t RCON[10];
static int aes_init_done = 0;
static void aes_gen_tables(void)
{
static void aes_gen_tables(void) {
int i, x, y, z;
int pow[256];
int log[256];
@ -507,13 +506,11 @@ static void aes_gen_tables(void)
#endif /* MBEDTLS_AES_FEWER_TABLES */
void mbedtls_aes_init(mbedtls_aes_context *ctx)
{
void mbedtls_aes_init(mbedtls_aes_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_aes_context));
}
void mbedtls_aes_free(mbedtls_aes_context *ctx)
{
void mbedtls_aes_free(mbedtls_aes_context *ctx) {
if (ctx == NULL)
return;
@ -521,14 +518,12 @@ void mbedtls_aes_free(mbedtls_aes_context *ctx)
}
#if defined(MBEDTLS_CIPHER_MODE_XTS)
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx)
{
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx) {
mbedtls_aes_init(&ctx->crypt);
mbedtls_aes_init(&ctx->tweak);
}
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
{
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx) {
mbedtls_aes_free(&ctx->crypt);
mbedtls_aes_free(&ctx->tweak);
}
@ -539,8 +534,7 @@ void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
*/
#if !defined(MBEDTLS_AES_SETKEY_ENC_ALT)
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
unsigned int i;
uint32_t *RK;
@ -653,8 +647,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
*/
#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT)
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
int i, j, ret;
mbedtls_aes_context cty;
uint32_t *RK;
@ -719,8 +712,7 @@ static int mbedtls_aes_xts_decode_keys(const unsigned char *key,
const unsigned char **key1,
unsigned int *key1bits,
const unsigned char **key2,
unsigned int *key2bits)
{
unsigned int *key2bits) {
const unsigned int half_keybits = keybits / 2;
const unsigned int half_keybytes = half_keybits / 8;
@ -743,8 +735,7 @@ static int mbedtls_aes_xts_decode_keys(const unsigned char *key,
int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
int ret;
const unsigned char *key1, *key2;
unsigned int key1bits, key2bits;
@ -765,8 +756,7 @@ int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
int ret;
const unsigned char *key1, *key2;
unsigned int key1bits, key2bits;
@ -840,8 +830,7 @@ int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
#if !defined(MBEDTLS_AES_ENCRYPT_ALT)
int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16])
{
unsigned char output[16]) {
int i;
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
@ -899,8 +888,7 @@ int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16])
{
unsigned char output[16]) {
mbedtls_internal_aes_encrypt(ctx, input, output);
}
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
@ -911,8 +899,7 @@ void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
#if !defined(MBEDTLS_AES_DECRYPT_ALT)
int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16])
{
unsigned char output[16]) {
int i;
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
@ -970,8 +957,7 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_aes_decrypt(mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16])
{
unsigned char output[16]) {
mbedtls_internal_aes_decrypt(ctx, input, output);
}
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
@ -982,8 +968,7 @@ void mbedtls_aes_decrypt(mbedtls_aes_context *ctx,
int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
int mode,
const unsigned char input[16],
unsigned char output[16])
{
unsigned char output[16]) {
#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES))
return (mbedtls_aesni_crypt_ecb(ctx, mode, input, output));
@ -1015,8 +1000,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int i;
unsigned char temp[16];
@ -1108,8 +1092,7 @@ typedef unsigned char mbedtls_be128[16];
* endian machines.
*/
static void mbedtls_gf128mul_x_ble(unsigned char r[16],
const unsigned char x[16])
{
const unsigned char x[16]) {
uint64_t a, b, ra, rb;
GET_UINT64_LE(a, x, 0);
@ -1130,8 +1113,7 @@ int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
size_t length,
const unsigned char data_unit[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int ret;
size_t blocks = length / 16;
size_t leftover = length % 16;
@ -1231,8 +1213,7 @@ int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int c;
size_t n = *iv_off;
@ -1271,8 +1252,7 @@ int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
unsigned char c;
unsigned char ov[17];
@ -1304,8 +1284,7 @@ int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int ret = 0;
size_t n = *iv_off;
@ -1337,8 +1316,7 @@ int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int c, i;
size_t n = *nc_off;
@ -1757,8 +1735,7 @@ static const unsigned char aes_test_xts_data_unit[][16] = {
/*
* Checkup routine
*/
int mbedtls_aes_self_test(int verbose)
{
int mbedtls_aes_self_test(int verbose) {
int ret = 0, i, j, u, mode;
unsigned int keybits;
unsigned char key[32];

View file

@ -50,13 +50,11 @@
#if !defined(MBEDTLS_ARC4_ALT)
void mbedtls_arc4_init(mbedtls_arc4_context *ctx)
{
void mbedtls_arc4_init(mbedtls_arc4_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_arc4_context));
}
void mbedtls_arc4_free(mbedtls_arc4_context *ctx)
{
void mbedtls_arc4_free(mbedtls_arc4_context *ctx) {
if (ctx == NULL)
return;
@ -67,8 +65,7 @@ void mbedtls_arc4_free(mbedtls_arc4_context *ctx)
* ARC4 key schedule
*/
void mbedtls_arc4_setup(mbedtls_arc4_context *ctx, const unsigned char *key,
unsigned int keylen)
{
unsigned int keylen) {
int i, j, a;
unsigned int k;
unsigned char *m;
@ -96,8 +93,7 @@ void mbedtls_arc4_setup(mbedtls_arc4_context *ctx, const unsigned char *key,
* ARC4 cipher function
*/
int mbedtls_arc4_crypt(mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int x, y, a, b;
size_t i;
unsigned char *m;
@ -154,8 +150,7 @@ static const unsigned char arc4_test_ct[3][8] = {
/*
* Checkup routine
*/
int mbedtls_arc4_self_test(int verbose)
{
int mbedtls_arc4_self_test(int verbose) {
int i, ret = 0;
unsigned char ibuf[8];
unsigned char obuf[8];

View file

@ -51,8 +51,7 @@
*/
int mbedtls_asn1_get_len(unsigned char **p,
const unsigned char *end,
size_t *len)
{
size_t *len) {
if ((end - *p) < 1)
return (MBEDTLS_ERR_ASN1_OUT_OF_DATA);
@ -107,8 +106,7 @@ int mbedtls_asn1_get_len(unsigned char **p,
int mbedtls_asn1_get_tag(unsigned char **p,
const unsigned char *end,
size_t *len, int tag)
{
size_t *len, int tag) {
if ((end - *p) < 1)
return (MBEDTLS_ERR_ASN1_OUT_OF_DATA);
@ -122,8 +120,7 @@ int mbedtls_asn1_get_tag(unsigned char **p,
int mbedtls_asn1_get_bool(unsigned char **p,
const unsigned char *end,
int *val)
{
int *val) {
int ret;
size_t len;
@ -141,8 +138,7 @@ int mbedtls_asn1_get_bool(unsigned char **p,
int mbedtls_asn1_get_int(unsigned char **p,
const unsigned char *end,
int *val)
{
int *val) {
int ret;
size_t len;
@ -165,8 +161,7 @@ int mbedtls_asn1_get_int(unsigned char **p,
#if defined(MBEDTLS_BIGNUM_C)
int mbedtls_asn1_get_mpi(unsigned char **p,
const unsigned char *end,
mbedtls_mpi *X)
{
mbedtls_mpi *X) {
int ret;
size_t len;
@ -182,8 +177,7 @@ int mbedtls_asn1_get_mpi(unsigned char **p,
#endif /* MBEDTLS_BIGNUM_C */
int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end,
mbedtls_asn1_bitstring *bs)
{
mbedtls_asn1_bitstring *bs) {
int ret;
/* Certificate type is a single byte bitstring */
@ -215,8 +209,7 @@ int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end,
* Get a bit string without unused bits
*/
int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end,
size_t *len)
{
size_t *len) {
int ret;
if ((ret = mbedtls_asn1_get_tag(p, end, len, MBEDTLS_ASN1_BIT_STRING)) != 0)
@ -236,8 +229,7 @@ int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end,
int mbedtls_asn1_get_sequence_of(unsigned char **p,
const unsigned char *end,
mbedtls_asn1_sequence *cur,
int tag)
{
int tag) {
int ret;
size_t len;
mbedtls_asn1_buf *buf;
@ -283,8 +275,7 @@ int mbedtls_asn1_get_sequence_of(unsigned char **p,
int mbedtls_asn1_get_alg(unsigned char **p,
const unsigned char *end,
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params)
{
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params) {
int ret;
size_t len;
@ -326,8 +317,7 @@ int mbedtls_asn1_get_alg(unsigned char **p,
int mbedtls_asn1_get_alg_null(unsigned char **p,
const unsigned char *end,
mbedtls_asn1_buf *alg)
{
mbedtls_asn1_buf *alg) {
int ret;
mbedtls_asn1_buf params;
@ -342,8 +332,7 @@ int mbedtls_asn1_get_alg_null(unsigned char **p,
return (0);
}
void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur)
{
void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur) {
if (cur == NULL)
return;
@ -353,8 +342,7 @@ void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur)
mbedtls_platform_zeroize(cur, sizeof(mbedtls_asn1_named_data));
}
void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head)
{
void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head) {
mbedtls_asn1_named_data *cur;
while ((cur = *head) != NULL) {
@ -365,11 +353,10 @@ void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head)
}
mbedtls_asn1_named_data *mbedtls_asn1_find_named_data(mbedtls_asn1_named_data *list,
const char *oid, size_t len)
{
const char *oid, size_t len) {
while (list != NULL) {
if (list->oid.len == len &&
memcmp(list->oid.p, oid, len) == 0) {
memcmp(list->oid.p, oid, len) == 0) {
break;
}

View file

@ -41,8 +41,7 @@
#define mbedtls_free free
#endif
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
{
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len) {
if (len < 0x80) {
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
@ -101,8 +100,7 @@ int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
#endif
}
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
{
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag) {
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
@ -112,8 +110,7 @@ int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned cha
}
int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size)
{
const unsigned char *buf, size_t size) {
size_t len = 0;
if (*p < start || (size_t)(*p - start) < size)
@ -127,8 +124,7 @@ int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
}
#if defined(MBEDTLS_BIGNUM_C)
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
{
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X) {
int ret;
size_t len = 0;
@ -163,8 +159,7 @@ cleanup:
}
#endif /* MBEDTLS_BIGNUM_C */
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
{
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start) {
int ret;
size_t len = 0;
@ -177,8 +172,7 @@ int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
}
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
const char *oid, size_t oid_len)
{
const char *oid, size_t oid_len) {
int ret;
size_t len = 0;
@ -192,8 +186,7 @@ int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start,
const char *oid, size_t oid_len,
size_t par_len)
{
size_t par_len) {
int ret;
size_t len = 0;
@ -211,8 +204,7 @@ int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *st
return ((int) len);
}
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
{
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean) {
int ret;
size_t len = 0;
@ -228,8 +220,7 @@ int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean
return ((int) len);
}
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
{
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val) {
int ret;
size_t len = 0;
@ -254,8 +245,7 @@ int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
}
int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
const char *text, size_t text_len)
{
const char *text, size_t text_len) {
int ret;
size_t len = 0;
@ -269,8 +259,7 @@ int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
}
int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
const char *text, size_t text_len)
{
const char *text, size_t text_len) {
int ret;
size_t len = 0;
@ -284,8 +273,7 @@ int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
}
int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t bits)
{
const unsigned char *buf, size_t bits) {
int ret;
size_t len = 0, size;
@ -311,8 +299,7 @@ int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
}
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size)
{
const unsigned char *buf, size_t size) {
int ret;
size_t len = 0;
@ -327,8 +314,7 @@ int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **head,
const char *oid, size_t oid_len,
const unsigned char *val,
size_t val_len)
{
size_t val_len) {
mbedtls_asn1_named_data *cur;
if ((cur = mbedtls_asn1_find_named_data(*head, oid, oid_len)) == NULL) {

View file

@ -75,8 +75,7 @@ static const unsigned char base64_dec_map[128] = {
* Encode a buffer into base64 format
*/
int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen)
{
const unsigned char *src, size_t slen) {
size_t i, n;
int C1, C2, C3;
unsigned char *p;
@ -137,8 +136,7 @@ int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
* Decode a base64-formatted buffer
*/
int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen)
{
const unsigned char *src, size_t slen) {
size_t i, n;
uint32_t j, x;
unsigned char *p;
@ -157,7 +155,7 @@ int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
break;
if ((slen - i) >= 2 &&
src[i] == '\r' && src[i + 1] == '\n')
src[i] == '\r' && src[i + 1] == '\n')
continue;
if (src[i] == '\n')
@ -236,8 +234,7 @@ static const unsigned char base64_test_enc[] =
/*
* Checkup routine
*/
int mbedtls_base64_self_test(int verbose)
{
int mbedtls_base64_self_test(int verbose) {
size_t len;
const unsigned char *src;
unsigned char buffer[128];
@ -248,7 +245,7 @@ int mbedtls_base64_self_test(int verbose)
src = base64_test_dec;
if (mbedtls_base64_encode(buffer, sizeof(buffer), &len, src, 64) != 0 ||
memcmp(base64_test_enc, buffer, 88) != 0) {
memcmp(base64_test_enc, buffer, 88) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");
@ -261,7 +258,7 @@ int mbedtls_base64_self_test(int verbose)
src = base64_test_enc;
if (mbedtls_base64_decode(buffer, sizeof(buffer), &len, src, 88) != 0 ||
memcmp(base64_test_dec, buffer, 64) != 0) {
memcmp(base64_test_dec, buffer, 64) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");

View file

@ -75,16 +75,14 @@
#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
/* Implementation that should never be optimized out by the compiler */
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
{
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n) {
mbedtls_platform_zeroize(v, ciL * n);
}
/*
* Initialize one MPI
*/
void mbedtls_mpi_init(mbedtls_mpi *X)
{
void mbedtls_mpi_init(mbedtls_mpi *X) {
if (X == NULL)
return;
@ -96,8 +94,7 @@ void mbedtls_mpi_init(mbedtls_mpi *X)
/*
* Unallocate one MPI
*/
void mbedtls_mpi_free(mbedtls_mpi *X)
{
void mbedtls_mpi_free(mbedtls_mpi *X) {
if (X == NULL)
return;
@ -114,8 +111,7 @@ void mbedtls_mpi_free(mbedtls_mpi *X)
/*
* Enlarge to the specified number of limbs
*/
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
{
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs) {
mbedtls_mpi_uint *p;
if (nblimbs > MBEDTLS_MPI_MAX_LIMBS)
@ -142,8 +138,7 @@ int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
* Resize down as much as possible,
* while keeping at least the specified number of limbs
*/
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
{
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs) {
mbedtls_mpi_uint *p;
size_t i;
@ -177,8 +172,7 @@ int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
/*
* Copy the contents of Y into X
*/
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
{
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y) {
int ret = 0;
size_t i;
@ -213,8 +207,7 @@ cleanup:
/*
* Swap the contents of X and Y
*/
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
{
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y) {
mbedtls_mpi T;
memcpy(&T, X, sizeof(mbedtls_mpi));
@ -227,8 +220,7 @@ void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
* about whether the assignment was made or not.
* (Leaking information about the respective sizes of X and Y is ok however.)
*/
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
{
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign) {
int ret = 0;
size_t i;
@ -255,8 +247,7 @@ cleanup:
* Here it is not ok to simply swap the pointers, which whould lead to
* different memory access patterns when X and Y are used afterwards.
*/
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap)
{
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap) {
int ret, s;
size_t i;
mbedtls_mpi_uint tmp;
@ -288,8 +279,7 @@ cleanup:
/*
* Set value from integer
*/
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
{
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z) {
int ret;
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
@ -306,8 +296,7 @@ cleanup:
/*
* Get a specific bit
*/
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
{
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos) {
if (X->n * biL <= pos)
return (0);
@ -317,8 +306,7 @@ int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
/*
* Set a bit to a specific value of 0 or 1
*/
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
{
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val) {
int ret = 0;
size_t off = pos / biL;
size_t idx = pos % biL;
@ -344,8 +332,7 @@ cleanup:
/*
* Return the number of less significant zero-bits
*/
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
{
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X) {
size_t i, j, count = 0;
for (i = 0; i < X->n; i++)
@ -359,8 +346,7 @@ size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
/*
* Count leading zero bits in a given integer
*/
static size_t mbedtls_clz(const mbedtls_mpi_uint x)
{
static size_t mbedtls_clz(const mbedtls_mpi_uint x) {
size_t j;
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
@ -376,8 +362,7 @@ static size_t mbedtls_clz(const mbedtls_mpi_uint x)
/*
* Return the number of bits
*/
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
{
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X) {
size_t i, j;
if (X->n == 0)
@ -395,16 +380,14 @@ size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
/*
* Return the total size in bytes
*/
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
{
size_t mbedtls_mpi_size(const mbedtls_mpi *X) {
return ((mbedtls_mpi_bitlen(X) + 7) >> 3);
}
/*
* Convert an ASCII character to digit value
*/
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
{
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c) {
*d = 255;
if (c >= 0x30 && c <= 0x39) *d = c - 0x30;
@ -420,8 +403,7 @@ static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
/*
* Import from an ASCII string
*/
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
{
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s) {
int ret;
size_t i, j, slen, n;
mbedtls_mpi_uint d;
@ -482,8 +464,7 @@ cleanup:
/*
* Helper to write the digits high-order first
*/
static int mpi_write_hlp(mbedtls_mpi *X, int radix, char **p)
{
static int mpi_write_hlp(mbedtls_mpi *X, int radix, char **p) {
int ret;
mbedtls_mpi_uint r;
@ -510,8 +491,7 @@ cleanup:
* Export into an ASCII string
*/
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
char *buf, size_t buflen, size_t *olen)
{
char *buf, size_t buflen, size_t *olen) {
int ret = 0;
size_t n;
char *p;
@ -580,8 +560,7 @@ cleanup:
/*
* Read X from an opened file
*/
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
{
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin) {
mbedtls_mpi_uint d;
size_t slen;
char *p;
@ -613,8 +592,7 @@ int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
/*
* Write X into an opened file (or stdout if fout == NULL)
*/
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
{
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout) {
int ret;
size_t n, slen, plen;
/*
@ -636,7 +614,7 @@ int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE
if (fout != NULL) {
if (fwrite(p, 1, plen, fout) != plen ||
fwrite(s, 1, slen, fout) != slen)
fwrite(s, 1, slen, fout) != slen)
return (MBEDTLS_ERR_MPI_FILE_IO_ERROR);
} else
mbedtls_printf("%s%s", p, s);
@ -650,8 +628,7 @@ cleanup:
/*
* Import X from unsigned binary data, big endian
*/
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
{
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen) {
int ret;
size_t i, j;
size_t const limbs = CHARS_TO_LIMBS(buflen);
@ -676,8 +653,7 @@ cleanup:
/*
* Export X into unsigned binary data, big endian
*/
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
{
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen) {
size_t i, j, n;
n = mbedtls_mpi_size(X);
@ -696,8 +672,7 @@ int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t bu
/*
* Left-shift: X <<= count
*/
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
{
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count) {
int ret;
size_t i, v0, t1;
mbedtls_mpi_uint r0 = 0, r1;
@ -743,8 +718,7 @@ cleanup:
/*
* Right-shift: X >>= count
*/
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
{
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count) {
size_t i, v0, v1;
mbedtls_mpi_uint r0 = 0, r1;
@ -783,8 +757,7 @@ int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
/*
* Compare unsigned values
*/
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
{
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y) {
size_t i, j;
for (i = X->n; i > 0; i--)
@ -812,8 +785,7 @@ int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
/*
* Compare signed values
*/
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
{
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y) {
size_t i, j;
for (i = X->n; i > 0; i--)
@ -844,8 +816,7 @@ int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
/*
* Compare signed values
*/
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
{
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z) {
mbedtls_mpi Y;
mbedtls_mpi_uint p[1];
@ -860,8 +831,7 @@ int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
/*
* Unsigned addition: X = |A| + |B| (HAC 14.7)
*/
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret;
size_t i, j;
mbedtls_mpi_uint *o, *p, c, tmp;
@ -921,8 +891,7 @@ cleanup:
/*
* Helper for mbedtls_mpi subtraction
*/
static void mpi_sub_hlp(size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d)
{
static void mpi_sub_hlp(size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d) {
size_t i;
mbedtls_mpi_uint c, z;
@ -944,8 +913,7 @@ static void mpi_sub_hlp(size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d)
/*
* Unsigned subtraction: X = |A| - |B| (HAC 14.9)
*/
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
mbedtls_mpi TB;
int ret;
size_t n;
@ -986,8 +954,7 @@ cleanup:
/*
* Signed addition: X = A + B
*/
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret, s = A->s;
if (A->s * B->s < 0) {
@ -1011,8 +978,7 @@ cleanup:
/*
* Signed subtraction: X = A - B
*/
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret, s = A->s;
if (A->s * B->s > 0) {
@ -1036,8 +1002,7 @@ cleanup:
/*
* Signed addition: X = A + b
*/
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
mbedtls_mpi _B;
mbedtls_mpi_uint p[1];
@ -1052,8 +1017,7 @@ int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b
/*
* Signed subtraction: X = A - b
*/
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
mbedtls_mpi _B;
mbedtls_mpi_uint p[1];
@ -1076,8 +1040,7 @@ static
*/
__attribute__((noinline))
#endif
void mpi_mul_hlp(size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b)
{
void mpi_mul_hlp(size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b) {
mbedtls_mpi_uint c = 0, t = 0;
#if defined(MULADDC_HUIT)
@ -1136,8 +1099,7 @@ void mpi_mul_hlp(size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi
/*
* Baseline multiplication: X = A * B (HAC 14.12)
*/
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret;
size_t i, j;
mbedtls_mpi TA, TB;
@ -1175,8 +1137,7 @@ cleanup:
/*
* Baseline multiplication: X = A * b
*/
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
{
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b) {
mbedtls_mpi _B;
mbedtls_mpi_uint p[1];
@ -1193,8 +1154,7 @@ int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b
* mbedtls_mpi_uint divisor, d
*/
static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r)
{
mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r) {
#if defined(MBEDTLS_HAVE_UDBL)
mbedtls_t_udbl dividend, quotient;
#else
@ -1284,8 +1244,7 @@ static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
/*
* Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
*/
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret;
size_t i, n, t, k;
mbedtls_mpi X, Y, Z, T1, T2;
@ -1394,8 +1353,7 @@ cleanup:
/*
* Division by int: A = Q * b + R
*/
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
mbedtls_mpi _B;
mbedtls_mpi_uint p[1];
@ -1410,8 +1368,7 @@ int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mb
/*
* Modulo: R = A mod B
*/
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret;
if (mbedtls_mpi_cmp_int(B, 0) < 0)
@ -1433,8 +1390,7 @@ cleanup:
/*
* Modulo: r = A mod b
*/
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
size_t i;
mbedtls_mpi_uint x, y, z;
@ -1487,8 +1443,7 @@ int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_s
/*
* Fast Montgomery initialization (thanks to Tom St Denis)
*/
static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
{
static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N) {
mbedtls_mpi_uint x, m0 = N->p[0];
unsigned int i;
@ -1505,8 +1460,7 @@ static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
* Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
*/
static int mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
const mbedtls_mpi *T)
{
const mbedtls_mpi *T) {
size_t i, n, m;
mbedtls_mpi_uint u0, u1, *d;
@ -1547,8 +1501,7 @@ static int mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *
/*
* Montgomery reduction: A = A * R^-1 mod N
*/
static int mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T)
{
static int mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T) {
mbedtls_mpi_uint z = 1;
mbedtls_mpi U;
@ -1561,8 +1514,7 @@ static int mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm
/*
* Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
*/
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
{
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR) {
int ret;
size_t wbits, wsize, one = 1;
size_t i, j, nblimbs;
@ -1761,8 +1713,7 @@ cleanup:
/*
* Greatest common divisor: G = gcd(A, B) (HAC 14.54)
*/
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B) {
int ret;
size_t lz, lzt;
mbedtls_mpi TG, TA, TB;
@ -1819,8 +1770,7 @@ cleanup:
*/
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
@ -1838,8 +1788,7 @@ cleanup:
/*
* Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
*/
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
{
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N) {
int ret;
mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
@ -1967,8 +1916,7 @@ static const int small_prime[] = {
* MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
* other negative: error
*/
static int mpi_check_small_factors(const mbedtls_mpi *X)
{
static int mpi_check_small_factors(const mbedtls_mpi *X) {
int ret = 0;
size_t i;
mbedtls_mpi_uint r;
@ -1995,8 +1943,7 @@ cleanup:
*/
static int mpi_miller_rabin(const mbedtls_mpi *X,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret, count;
size_t i, j, k, n, s;
mbedtls_mpi W, R, T, A, RR;
@ -2059,7 +2006,7 @@ static int mpi_miller_rabin(const mbedtls_mpi *X,
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
mbedtls_mpi_cmp_int(&A, 1) == 0)
mbedtls_mpi_cmp_int(&A, 1) == 0)
continue;
j = 1;
@ -2080,7 +2027,7 @@ static int mpi_miller_rabin(const mbedtls_mpi *X,
* not prime if A != |X| - 1 or A == 1
*/
if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
mbedtls_mpi_cmp_int(&A, 1) == 0) {
mbedtls_mpi_cmp_int(&A, 1) == 0) {
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
break;
}
@ -2101,8 +2048,7 @@ cleanup:
*/
int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
mbedtls_mpi XX;
@ -2111,7 +2057,7 @@ int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
XX.p = X->p;
if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
mbedtls_mpi_cmp_int(&XX, 1) == 0)
mbedtls_mpi_cmp_int(&XX, 1) == 0)
return (MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
if (mbedtls_mpi_cmp_int(&XX, 2) == 0)
@ -2136,8 +2082,7 @@ int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
*/
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
#ifdef MBEDTLS_HAVE_INT64
// ceil(2^63.5)
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
@ -2196,9 +2141,9 @@ int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag,
* before doing Miller-Rabin on any of them
*/
if ((ret = mpi_check_small_factors(X)) == 0 &&
(ret = mpi_check_small_factors(&Y)) == 0 &&
(ret = mpi_miller_rabin(X, f_rng, p_rng)) == 0 &&
(ret = mpi_miller_rabin(&Y, f_rng, p_rng)) == 0)
(ret = mpi_check_small_factors(&Y)) == 0 &&
(ret = mpi_miller_rabin(X, f_rng, p_rng)) == 0 &&
(ret = mpi_miller_rabin(&Y, f_rng, p_rng)) == 0)
goto cleanup;
if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE)
@ -2237,8 +2182,7 @@ static const int gcd_pairs[GCD_PAIR_COUNT][3] = {
/*
* Checkup routine
*/
int mbedtls_mpi_self_test(int verbose)
{
int mbedtls_mpi_self_test(int verbose) {
int ret, i;
mbedtls_mpi A, E, N, X, Y, U, V;
@ -2306,7 +2250,7 @@ int mbedtls_mpi_self_test(int verbose)
mbedtls_printf(" MPI test #2 (div_mpi): ");
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");

View file

@ -76,8 +76,7 @@ static const uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2] = {
/* declarations of data at the end of this file */
static const uint32_t S[4][256];
static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x)
{
static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x) {
unsigned short a, b, c, d;
uint32_t y;
@ -95,8 +94,7 @@ static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x)
return (y);
}
static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr)
{
static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr) {
uint32_t Xl, Xr, temp;
short i;
@ -123,8 +121,7 @@ static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *
*xr = Xr;
}
static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr)
{
static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr) {
uint32_t Xl, Xr, temp;
short i;
@ -151,13 +148,11 @@ static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *
*xr = Xr;
}
void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx)
{
void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_blowfish_context));
}
void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx)
{
void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx) {
if (ctx == NULL)
return;
@ -168,13 +163,12 @@ void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx)
* Blowfish key schedule
*/
int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
unsigned int i, j, k;
uint32_t data, datal, datar;
if (keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS || keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS ||
(keybits % 8)) {
(keybits % 8)) {
return (MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH);
}
@ -221,8 +215,7 @@ int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *
int mbedtls_blowfish_crypt_ecb(mbedtls_blowfish_context *ctx,
int mode,
const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE])
{
unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]) {
uint32_t X0, X1;
GET_UINT32_BE(X0, input, 0);
@ -249,8 +242,7 @@ int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx,
size_t length,
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int i;
unsigned char temp[MBEDTLS_BLOWFISH_BLOCKSIZE];
@ -299,8 +291,7 @@ int mbedtls_blowfish_crypt_cfb64(mbedtls_blowfish_context *ctx,
size_t *iv_off,
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int c;
size_t n = *iv_off;
@ -342,8 +333,7 @@ int mbedtls_blowfish_crypt_ctr(mbedtls_blowfish_context *ctx,
unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int c, i;
size_t n = *nc_off;

View file

@ -304,8 +304,7 @@ static const signed char transposes[2][20] = {
}
static void camellia_feistel(const uint32_t x[2], const uint32_t k[2],
uint32_t z[2])
{
uint32_t z[2]) {
uint32_t I0, I1;
I0 = x[0] ^ k[0];
I1 = x[1] ^ k[1];
@ -328,13 +327,11 @@ static void camellia_feistel(const uint32_t x[2], const uint32_t k[2],
z[1] ^= I0;
}
void mbedtls_camellia_init(mbedtls_camellia_context *ctx)
{
void mbedtls_camellia_init(mbedtls_camellia_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_camellia_context));
}
void mbedtls_camellia_free(mbedtls_camellia_context *ctx)
{
void mbedtls_camellia_free(mbedtls_camellia_context *ctx) {
if (ctx == NULL)
return;
@ -345,8 +342,7 @@ void mbedtls_camellia_free(mbedtls_camellia_context *ctx)
* Camellia key schedule (encryption)
*/
int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx, const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
int idx;
size_t i;
uint32_t *RK;
@ -456,8 +452,7 @@ int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx, const unsigned ch
* Camellia key schedule (decryption)
*/
int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx, const unsigned char *key,
unsigned int keybits)
{
unsigned int keybits) {
int idx, ret;
size_t i;
mbedtls_camellia_context cty;
@ -505,8 +500,7 @@ exit:
int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
int mode,
const unsigned char input[16],
unsigned char output[16])
{
unsigned char output[16]) {
int NR;
uint32_t *RK, X[4];
@ -570,8 +564,7 @@ int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int i;
unsigned char temp[16];
@ -620,8 +613,7 @@ int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int c;
size_t n = *iv_off;
@ -663,8 +655,7 @@ int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int c, i;
size_t n = *nc_off;
@ -964,8 +955,7 @@ static const int camellia_test_ctr_len[3] =
/*
* Checkup routine
*/
int mbedtls_camellia_self_test(int verbose)
{
int mbedtls_camellia_self_test(int verbose) {
int i, j, u, v;
unsigned char key[32];
unsigned char buf[64];

View file

@ -73,8 +73,7 @@
* a non-zero value.
* This is currently only used by GCM and ChaCha20+Poly1305.
*/
static int mbedtls_constant_time_memcmp(const void *v1, const void *v2, size_t len)
{
static int mbedtls_constant_time_memcmp(const void *v1, const void *v2, size_t len) {
const unsigned char *p1 = (const unsigned char *) v1;
const unsigned char *p2 = (const unsigned char *) v2;
size_t i;
@ -89,8 +88,7 @@ static int mbedtls_constant_time_memcmp(const void *v1, const void *v2, size_t l
static int supported_init = 0;
const int *mbedtls_cipher_list(void)
{
const int *mbedtls_cipher_list(void) {
const mbedtls_cipher_definition_t *def;
int *type;
@ -109,8 +107,7 @@ const int *mbedtls_cipher_list(void)
return (mbedtls_cipher_supported);
}
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
{
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type) {
const mbedtls_cipher_definition_t *def;
for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
@ -120,8 +117,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_
return (NULL);
}
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name)
{
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name) {
const mbedtls_cipher_definition_t *def;
if (NULL == cipher_name)
@ -136,26 +132,23 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id,
int key_bitlen,
const mbedtls_cipher_mode_t mode)
{
const mbedtls_cipher_mode_t mode) {
const mbedtls_cipher_definition_t *def;
for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
if (def->info->base->cipher == cipher_id &&
def->info->key_bitlen == (unsigned) key_bitlen &&
def->info->mode == mode)
def->info->key_bitlen == (unsigned) key_bitlen &&
def->info->mode == mode)
return (def->info);
return (NULL);
}
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
{
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx) {
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
}
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
{
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx) {
if (ctx == NULL)
return;
@ -173,8 +166,7 @@ void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
}
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
{
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info) {
if (NULL == cipher_info || NULL == ctx)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -200,13 +192,12 @@ int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_inf
}
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key,
int key_bitlen, const mbedtls_operation_t operation)
{
int key_bitlen, const mbedtls_operation_t operation) {
if (NULL == ctx || NULL == ctx->cipher_info)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
(int) ctx->cipher_info->key_bitlen != key_bitlen) {
(int) ctx->cipher_info->key_bitlen != key_bitlen) {
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
}
@ -217,9 +208,9 @@ int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *ke
* For OFB, CFB and CTR mode always use the encryption key schedule
*/
if (MBEDTLS_ENCRYPT == operation ||
MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
ctx->key_bitlen);
}
@ -232,8 +223,7 @@ int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *ke
}
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len)
{
const unsigned char *iv, size_t iv_len) {
size_t actual_iv_size;
if (NULL == ctx || NULL == ctx->cipher_info || NULL == iv)
@ -269,8 +259,7 @@ int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
return (0);
}
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
{
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -281,8 +270,7 @@ int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
const unsigned char *ad, size_t ad_len)
{
const unsigned char *ad, size_t ad_len) {
if (NULL == ctx || NULL == ctx->cipher_info)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -318,8 +306,7 @@ int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
size_t ilen, unsigned char *output, size_t *olen)
{
size_t ilen, unsigned char *output, size_t *olen) {
int ret;
size_t block_size = 0;
@ -365,7 +352,7 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
}
if (input == output &&
(ctx->unprocessed_len != 0 || ilen % block_size)) {
(ctx->unprocessed_len != 0 || ilen % block_size)) {
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
}
@ -377,11 +364,11 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
* If there is not enough data for a full block, cache it.
*/
if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
ilen <= block_size - ctx->unprocessed_len) ||
(ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
ilen < block_size - ctx->unprocessed_len) ||
(ctx->operation == MBEDTLS_ENCRYPT &&
ilen < block_size - ctx->unprocessed_len)) {
ilen <= block_size - ctx->unprocessed_len) ||
(ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
ilen < block_size - ctx->unprocessed_len) ||
(ctx->operation == MBEDTLS_ENCRYPT &&
ilen < block_size - ctx->unprocessed_len)) {
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
ilen);
@ -426,8 +413,8 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
*/
copy_len = ilen % block_size;
if (copy_len == 0 &&
ctx->operation == MBEDTLS_DECRYPT &&
NULL != ctx->add_padding) {
ctx->operation == MBEDTLS_DECRYPT &&
NULL != ctx->add_padding) {
copy_len = block_size;
}
@ -536,8 +523,7 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
* PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
*/
static void add_pkcs_padding(unsigned char *output, size_t output_len,
size_t data_len)
{
size_t data_len) {
size_t padding_len = output_len - data_len;
unsigned char i;
@ -546,8 +532,7 @@ static void add_pkcs_padding(unsigned char *output, size_t output_len,
}
static int get_pkcs_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t *data_len) {
size_t i, pad_idx;
unsigned char padding_len, bad = 0;
@ -576,8 +561,7 @@ static int get_pkcs_padding(unsigned char *input, size_t input_len,
* One and zeros padding: fill with 80 00 ... 00
*/
static void add_one_and_zeros_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
size_t output_len, size_t data_len) {
size_t padding_len = output_len - data_len;
unsigned char i = 0;
@ -587,8 +571,7 @@ static void add_one_and_zeros_padding(unsigned char *output,
}
static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t *data_len) {
size_t i;
unsigned char done = 0, prev_done, bad;
@ -614,8 +597,7 @@ static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
* Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
*/
static void add_zeros_and_len_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
size_t output_len, size_t data_len) {
size_t padding_len = output_len - data_len;
unsigned char i = 0;
@ -625,8 +607,7 @@ static void add_zeros_and_len_padding(unsigned char *output,
}
static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t *data_len) {
size_t i, pad_idx;
unsigned char padding_len, bad = 0;
@ -654,8 +635,7 @@ static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
* Zero padding: fill with 00 ... 00
*/
static void add_zeros_padding(unsigned char *output,
size_t output_len, size_t data_len)
{
size_t output_len, size_t data_len) {
size_t i;
for (i = data_len; i < output_len; i++)
@ -663,8 +643,7 @@ static void add_zeros_padding(unsigned char *output,
}
static int get_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t *data_len) {
size_t i;
unsigned char done = 0, prev_done;
@ -689,8 +668,7 @@ static int get_zeros_padding(unsigned char *input, size_t input_len,
* but a trivial get_padding function
*/
static int get_no_padding(unsigned char *input, size_t input_len,
size_t *data_len)
{
size_t *data_len) {
if (NULL == input || NULL == data_len)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -701,24 +679,23 @@ static int get_no_padding(unsigned char *input, size_t input_len,
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
unsigned char *output, size_t *olen)
{
unsigned char *output, size_t *olen) {
if (NULL == ctx || NULL == ctx->cipher_info || NULL == olen)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
*olen = 0;
if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
return (0);
}
if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
(MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
(MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
return (0);
}
@ -779,10 +756,9 @@ int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
}
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode)
{
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode) {
if (NULL == ctx ||
MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
}
@ -826,8 +802,7 @@ int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_ciphe
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
unsigned char *tag, size_t tag_len)
{
unsigned char *tag, size_t tag_len) {
if (NULL == ctx || NULL == ctx->cipher_info || NULL == tag)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -854,13 +829,12 @@ int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
}
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
const unsigned char *tag, size_t tag_len)
{
const unsigned char *tag, size_t tag_len) {
unsigned char check_tag[16];
int ret;
if (NULL == ctx || NULL == ctx->cipher_info ||
MBEDTLS_DECRYPT != ctx->operation) {
MBEDTLS_DECRYPT != ctx->operation) {
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
}
@ -912,8 +886,7 @@ int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen)
{
unsigned char *output, size_t *olen) {
int ret;
size_t finish_olen;
@ -943,8 +916,7 @@ int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen,
unsigned char *tag, size_t tag_len)
{
unsigned char *tag, size_t tag_len) {
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
*olen = ilen;
@ -965,7 +937,7 @@ int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
/* ChachaPoly has fixed length nonce and MAC (tag) */
if ((iv_len != ctx->cipher_info->iv_size) ||
(tag_len != 16U)) {
(tag_len != 16U)) {
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
}
@ -986,8 +958,7 @@ int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
const unsigned char *ad, size_t ad_len,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen,
const unsigned char *tag, size_t tag_len)
{
const unsigned char *tag, size_t tag_len) {
#if defined(MBEDTLS_GCM_C)
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
int ret;
@ -1024,7 +995,7 @@ int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
/* ChachaPoly has fixed length nonce and MAC (tag) */
if ((iv_len != ctx->cipher_info->iv_size) ||
(tag_len != 16U)) {
(tag_len != 16U)) {
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
}

View file

@ -413,8 +413,7 @@ int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_inf
* \return The size of the blocks of the cipher.
* \return 0 if \p ctx has not been initialized.
*/
static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx)
{
static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return 0;
@ -430,8 +429,7 @@ static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_co
* \return The mode of operation.
* \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
*/
static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx)
{
static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return MBEDTLS_MODE_NONE;
@ -448,8 +446,7 @@ static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls
* \return \c 0 for ciphers not using an IV or a nonce.
* \return The actual size if an IV has been set.
*/
static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx)
{
static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return 0;
@ -467,8 +464,7 @@ static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx
* \return The type of the cipher.
* \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
*/
static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx)
{
static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return MBEDTLS_CIPHER_NONE;
@ -484,8 +480,7 @@ static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher
* \return The name of the cipher.
* \return NULL if \p ctx has not been not initialized.
*/
static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx)
{
static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return 0;
@ -501,8 +496,7 @@ static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t
* \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
* initialized.
*/
static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
{
static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return MBEDTLS_KEY_LENGTH_NONE;
@ -517,8 +511,7 @@ static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *
* \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
* \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
*/
static inline mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx)
{
static inline mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx) {
if (NULL == ctx || NULL == ctx->cipher_info)
return MBEDTLS_OPERATION_NONE;

View file

@ -89,8 +89,7 @@
#if defined(MBEDTLS_GCM_C)
/* shared by all GCM ciphers */
static void *gcm_ctx_alloc(void)
{
static void *gcm_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_gcm_context));
if (ctx != NULL)
@ -99,8 +98,7 @@ static void *gcm_ctx_alloc(void)
return (ctx);
}
static void gcm_ctx_free(void *ctx)
{
static void gcm_ctx_free(void *ctx) {
mbedtls_gcm_free(ctx);
mbedtls_free(ctx);
}
@ -108,8 +106,7 @@ static void gcm_ctx_free(void *ctx)
#if defined(MBEDTLS_CCM_C)
/* shared by all CCM ciphers */
static void *ccm_ctx_alloc(void)
{
static void *ccm_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ccm_context));
if (ctx != NULL)
@ -118,8 +115,7 @@ static void *ccm_ctx_alloc(void)
return (ctx);
}
static void ccm_ctx_free(void *ctx)
{
static void ccm_ctx_free(void *ctx) {
mbedtls_ccm_free(ctx);
mbedtls_free(ctx);
}
@ -128,15 +124,13 @@ static void ccm_ctx_free(void *ctx)
#if defined(MBEDTLS_AES_C)
static int aes_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_aes_crypt_ecb((mbedtls_aes_context *) ctx, operation, input, output);
}
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
unsigned char *iv, const unsigned char *input, unsigned char *output)
{
unsigned char *iv, const unsigned char *input, unsigned char *output) {
return mbedtls_aes_crypt_cbc((mbedtls_aes_context *) ctx, operation, length, iv, input,
output);
}
@ -145,8 +139,7 @@ static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t l
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, size_t *iv_off, unsigned char *iv,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_aes_crypt_cfb128((mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
input, output);
}
@ -154,8 +147,7 @@ static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_OFB)
static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
unsigned char *iv, const unsigned char *input, unsigned char *output)
{
unsigned char *iv, const unsigned char *input, unsigned char *output) {
return mbedtls_aes_crypt_ofb((mbedtls_aes_context *) ctx, length, iv_off,
iv, input, output);
}
@ -164,8 +156,7 @@ static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
#if defined(MBEDTLS_CIPHER_MODE_CTR)
static int aes_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
unsigned char *nonce_counter, unsigned char *stream_block,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_aes_crypt_ctr((mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
stream_block, input, output);
}
@ -176,8 +167,7 @@ static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
size_t length,
const unsigned char data_unit[16],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
mbedtls_aes_xts_context *xts_ctx = ctx;
int mode;
@ -198,19 +188,16 @@ static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
#endif /* MBEDTLS_CIPHER_MODE_XTS */
static int aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_aes_setkey_dec((mbedtls_aes_context *) ctx, key, key_bitlen);
}
static int aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_aes_setkey_enc((mbedtls_aes_context *) ctx, key, key_bitlen);
}
static void *aes_ctx_alloc(void)
{
static void *aes_ctx_alloc(void) {
mbedtls_aes_context *aes = mbedtls_calloc(1, sizeof(mbedtls_aes_context));
if (aes == NULL)
@ -221,8 +208,7 @@ static void *aes_ctx_alloc(void)
return (aes);
}
static void aes_ctx_free(void *ctx)
{
static void aes_ctx_free(void *ctx) {
mbedtls_aes_free((mbedtls_aes_context *) ctx);
mbedtls_free(ctx);
}
@ -429,21 +415,18 @@ static const mbedtls_cipher_info_t aes_256_ctr_info = {
#if defined(MBEDTLS_CIPHER_MODE_XTS)
static int xts_aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
mbedtls_aes_xts_context *xts_ctx = ctx;
return (mbedtls_aes_xts_setkey_enc(xts_ctx, key, key_bitlen));
}
static int xts_aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
mbedtls_aes_xts_context *xts_ctx = ctx;
return (mbedtls_aes_xts_setkey_dec(xts_ctx, key, key_bitlen));
}
static void *xts_aes_ctx_alloc(void)
{
static void *xts_aes_ctx_alloc(void) {
mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc(1, sizeof(*xts_ctx));
if (xts_ctx != NULL)
@ -452,8 +435,7 @@ static void *xts_aes_ctx_alloc(void)
return (xts_ctx);
}
static void xts_aes_ctx_free(void *ctx)
{
static void xts_aes_ctx_free(void *ctx) {
mbedtls_aes_xts_context *xts_ctx = ctx;
if (xts_ctx == NULL)
@ -515,8 +497,7 @@ static const mbedtls_cipher_info_t aes_256_xts_info = {
#if defined(MBEDTLS_GCM_C)
static int gcm_aes_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
key, key_bitlen);
}
@ -584,8 +565,7 @@ static const mbedtls_cipher_info_t aes_256_gcm_info = {
#if defined(MBEDTLS_CCM_C)
static int ccm_aes_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
key, key_bitlen);
}
@ -656,8 +636,7 @@ static const mbedtls_cipher_info_t aes_256_ccm_info = {
#if defined(MBEDTLS_CAMELLIA_C)
static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_camellia_crypt_ecb((mbedtls_camellia_context *) ctx, operation, input,
output);
}
@ -665,8 +644,7 @@ static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, unsigned char *iv,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_camellia_crypt_cbc((mbedtls_camellia_context *) ctx, operation, length, iv,
input, output);
}
@ -675,8 +653,7 @@ static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, size_t *iv_off, unsigned char *iv,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_camellia_crypt_cfb128((mbedtls_camellia_context *) ctx, operation, length,
iv_off, iv, input, output);
}
@ -685,27 +662,23 @@ static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CTR)
static int camellia_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
unsigned char *nonce_counter, unsigned char *stream_block,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_camellia_crypt_ctr((mbedtls_camellia_context *) ctx, length, nc_off,
nonce_counter, stream_block, input, output);
}
#endif /* MBEDTLS_CIPHER_MODE_CTR */
static int camellia_setkey_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_camellia_setkey_dec((mbedtls_camellia_context *) ctx, key, key_bitlen);
}
static int camellia_setkey_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_camellia_setkey_enc((mbedtls_camellia_context *) ctx, key, key_bitlen);
}
static void *camellia_ctx_alloc(void)
{
static void *camellia_ctx_alloc(void) {
mbedtls_camellia_context *ctx;
ctx = mbedtls_calloc(1, sizeof(mbedtls_camellia_context));
@ -717,8 +690,7 @@ static void *camellia_ctx_alloc(void)
return (ctx);
}
static void camellia_ctx_free(void *ctx)
{
static void camellia_ctx_free(void *ctx) {
mbedtls_camellia_free((mbedtls_camellia_context *) ctx);
mbedtls_free(ctx);
}
@ -890,8 +862,7 @@ static const mbedtls_cipher_info_t camellia_256_ctr_info = {
#if defined(MBEDTLS_GCM_C)
static int gcm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
key, key_bitlen);
}
@ -959,8 +930,7 @@ static const mbedtls_cipher_info_t camellia_256_gcm_info = {
#if defined(MBEDTLS_CCM_C)
static int ccm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
key, key_bitlen);
}
@ -1031,8 +1001,7 @@ static const mbedtls_cipher_info_t camellia_256_ccm_info = {
#if defined(MBEDTLS_ARIA_C)
static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
(void) operation;
return mbedtls_aria_crypt_ecb((mbedtls_aria_context *) ctx, input,
output);
@ -1041,8 +1010,7 @@ static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, unsigned char *iv,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_aria_crypt_cbc((mbedtls_aria_context *) ctx, operation, length, iv,
input, output);
}
@ -1051,8 +1019,7 @@ static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, size_t *iv_off, unsigned char *iv,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_aria_crypt_cfb128((mbedtls_aria_context *) ctx, operation, length,
iv_off, iv, input, output);
}
@ -1061,27 +1028,23 @@ static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CTR)
static int aria_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
unsigned char *nonce_counter, unsigned char *stream_block,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_aria_crypt_ctr((mbedtls_aria_context *) ctx, length, nc_off,
nonce_counter, stream_block, input, output);
}
#endif /* MBEDTLS_CIPHER_MODE_CTR */
static int aria_setkey_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_aria_setkey_dec((mbedtls_aria_context *) ctx, key, key_bitlen);
}
static int aria_setkey_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_aria_setkey_enc((mbedtls_aria_context *) ctx, key, key_bitlen);
}
static void *aria_ctx_alloc(void)
{
static void *aria_ctx_alloc(void) {
mbedtls_aria_context *ctx;
ctx = mbedtls_calloc(1, sizeof(mbedtls_aria_context));
@ -1093,8 +1056,7 @@ static void *aria_ctx_alloc(void)
return (ctx);
}
static void aria_ctx_free(void *ctx)
{
static void aria_ctx_free(void *ctx) {
mbedtls_aria_free((mbedtls_aria_context *) ctx);
mbedtls_free(ctx);
}
@ -1266,8 +1228,7 @@ static const mbedtls_cipher_info_t aria_256_ctr_info = {
#if defined(MBEDTLS_GCM_C)
static int gcm_aria_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
key, key_bitlen);
}
@ -1335,8 +1296,7 @@ static const mbedtls_cipher_info_t aria_256_gcm_info = {
#if defined(MBEDTLS_CCM_C)
static int ccm_aria_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
key, key_bitlen);
}
@ -1407,23 +1367,20 @@ static const mbedtls_cipher_info_t aria_256_ccm_info = {
#if defined(MBEDTLS_DES_C)
static int des_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
((void) operation);
return mbedtls_des_crypt_ecb((mbedtls_des_context *) ctx, input, output);
}
static int des3_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
((void) operation);
return mbedtls_des3_crypt_ecb((mbedtls_des3_context *) ctx, input, output);
}
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
unsigned char *iv, const unsigned char *input, unsigned char *output)
{
unsigned char *iv, const unsigned char *input, unsigned char *output) {
return mbedtls_des_crypt_cbc((mbedtls_des_context *) ctx, operation, length, iv, input,
output);
}
@ -1431,63 +1388,55 @@ static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t l
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int des3_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
unsigned char *iv, const unsigned char *input, unsigned char *output)
{
unsigned char *iv, const unsigned char *input, unsigned char *output) {
return mbedtls_des3_crypt_cbc((mbedtls_des3_context *) ctx, operation, length, iv, input,
output);
}
#endif /* MBEDTLS_CIPHER_MODE_CBC */
static int des_setkey_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) key_bitlen);
return mbedtls_des_setkey_dec((mbedtls_des_context *) ctx, key);
}
static int des_setkey_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) key_bitlen);
return mbedtls_des_setkey_enc((mbedtls_des_context *) ctx, key);
}
static int des3_set2key_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) key_bitlen);
return mbedtls_des3_set2key_dec((mbedtls_des3_context *) ctx, key);
}
static int des3_set2key_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) key_bitlen);
return mbedtls_des3_set2key_enc((mbedtls_des3_context *) ctx, key);
}
static int des3_set3key_dec_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) key_bitlen);
return mbedtls_des3_set3key_dec((mbedtls_des3_context *) ctx, key);
}
static int des3_set3key_enc_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) key_bitlen);
return mbedtls_des3_set3key_enc((mbedtls_des3_context *) ctx, key);
}
static void *des_ctx_alloc(void)
{
static void *des_ctx_alloc(void) {
mbedtls_des_context *des = mbedtls_calloc(1, sizeof(mbedtls_des_context));
if (des == NULL)
@ -1498,14 +1447,12 @@ static void *des_ctx_alloc(void)
return (des);
}
static void des_ctx_free(void *ctx)
{
static void des_ctx_free(void *ctx) {
mbedtls_des_free((mbedtls_des_context *) ctx);
mbedtls_free(ctx);
}
static void *des3_ctx_alloc(void)
{
static void *des3_ctx_alloc(void) {
mbedtls_des3_context *des3;
des3 = mbedtls_calloc(1, sizeof(mbedtls_des3_context));
@ -1517,8 +1464,7 @@ static void *des3_ctx_alloc(void)
return (des3);
}
static void des3_ctx_free(void *ctx)
{
static void des3_ctx_free(void *ctx) {
mbedtls_des3_free((mbedtls_des3_context *) ctx);
mbedtls_free(ctx);
}
@ -1679,8 +1625,7 @@ static const mbedtls_cipher_info_t des_ede3_cbc_info = {
#if defined(MBEDTLS_BLOWFISH_C)
static int blowfish_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_blowfish_crypt_ecb((mbedtls_blowfish_context *) ctx, operation, input,
output);
}
@ -1688,8 +1633,7 @@ static int blowfish_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int blowfish_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, unsigned char *iv, const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
return mbedtls_blowfish_crypt_cbc((mbedtls_blowfish_context *) ctx, operation, length, iv,
input, output);
}
@ -1698,8 +1642,7 @@ static int blowfish_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static int blowfish_crypt_cfb64_wrap(void *ctx, mbedtls_operation_t operation,
size_t length, size_t *iv_off, unsigned char *iv,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_blowfish_crypt_cfb64((mbedtls_blowfish_context *) ctx, operation, length,
iv_off, iv, input, output);
}
@ -1708,21 +1651,18 @@ static int blowfish_crypt_cfb64_wrap(void *ctx, mbedtls_operation_t operation,
#if defined(MBEDTLS_CIPHER_MODE_CTR)
static int blowfish_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
unsigned char *nonce_counter, unsigned char *stream_block,
const unsigned char *input, unsigned char *output)
{
const unsigned char *input, unsigned char *output) {
return mbedtls_blowfish_crypt_ctr((mbedtls_blowfish_context *) ctx, length, nc_off,
nonce_counter, stream_block, input, output);
}
#endif /* MBEDTLS_CIPHER_MODE_CTR */
static int blowfish_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
return mbedtls_blowfish_setkey((mbedtls_blowfish_context *) ctx, key, key_bitlen);
}
static void *blowfish_ctx_alloc(void)
{
static void *blowfish_ctx_alloc(void) {
mbedtls_blowfish_context *ctx;
ctx = mbedtls_calloc(1, sizeof(mbedtls_blowfish_context));
@ -1734,8 +1674,7 @@ static void *blowfish_ctx_alloc(void)
return (ctx);
}
static void blowfish_ctx_free(void *ctx)
{
static void blowfish_ctx_free(void *ctx) {
mbedtls_blowfish_free((mbedtls_blowfish_context *) ctx);
mbedtls_free(ctx);
}
@ -1821,14 +1760,12 @@ static const mbedtls_cipher_info_t blowfish_ctr_info = {
#if defined(MBEDTLS_ARC4_C)
static int arc4_crypt_stream_wrap(void *ctx, size_t length,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
return (mbedtls_arc4_crypt((mbedtls_arc4_context *) ctx, length, input, output));
}
static int arc4_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
/* we get key_bitlen in bits, arc4 expects it in bytes */
if (key_bitlen % 8 != 0)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -1837,8 +1774,7 @@ static int arc4_setkey_wrap(void *ctx, const unsigned char *key,
return (0);
}
static void *arc4_ctx_alloc(void)
{
static void *arc4_ctx_alloc(void) {
mbedtls_arc4_context *ctx;
ctx = mbedtls_calloc(1, sizeof(mbedtls_arc4_context));
@ -1850,8 +1786,7 @@ static void *arc4_ctx_alloc(void)
return (ctx);
}
static void arc4_ctx_free(void *ctx)
{
static void arc4_ctx_free(void *ctx) {
mbedtls_arc4_free((mbedtls_arc4_context *) ctx);
mbedtls_free(ctx);
}
@ -1898,8 +1833,7 @@ static const mbedtls_cipher_info_t arc4_128_info = {
#if defined(MBEDTLS_CHACHA20_C)
static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
if (key_bitlen != 256U)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -1911,8 +1845,7 @@ static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
static int chacha20_stream_wrap(void *ctx, size_t length,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int ret;
ret = mbedtls_chacha20_update(ctx, length, input, output);
@ -1922,8 +1855,7 @@ static int chacha20_stream_wrap(void *ctx, size_t length,
return (ret);
}
static void *chacha20_ctx_alloc(void)
{
static void *chacha20_ctx_alloc(void) {
mbedtls_chacha20_context *ctx;
ctx = mbedtls_calloc(1, sizeof(mbedtls_chacha20_context));
@ -1935,8 +1867,7 @@ static void *chacha20_ctx_alloc(void)
return (ctx);
}
static void chacha20_ctx_free(void *ctx)
{
static void chacha20_ctx_free(void *ctx) {
mbedtls_chacha20_free((mbedtls_chacha20_context *) ctx);
mbedtls_free(ctx);
}
@ -1983,8 +1914,7 @@ static const mbedtls_cipher_info_t chacha20_info = {
static int chachapoly_setkey_wrap(void *ctx,
const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
if (key_bitlen != 256U)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
@ -1994,8 +1924,7 @@ static int chachapoly_setkey_wrap(void *ctx,
return (0);
}
static void *chachapoly_ctx_alloc(void)
{
static void *chachapoly_ctx_alloc(void) {
mbedtls_chachapoly_context *ctx;
ctx = mbedtls_calloc(1, sizeof(mbedtls_chachapoly_context));
@ -2007,8 +1936,7 @@ static void *chachapoly_ctx_alloc(void)
return (ctx);
}
static void chachapoly_ctx_free(void *ctx)
{
static void chachapoly_ctx_free(void *ctx) {
mbedtls_chachapoly_free((mbedtls_chachapoly_context *) ctx);
mbedtls_free(ctx);
}
@ -2054,16 +1982,14 @@ static const mbedtls_cipher_info_t chachapoly_info = {
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
static int null_crypt_stream(void *ctx, size_t length,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
((void) ctx);
memmove(output, input, length);
return (0);
}
static int null_setkey(void *ctx, const unsigned char *key,
unsigned int key_bitlen)
{
unsigned int key_bitlen) {
((void) ctx);
((void) key);
((void) key_bitlen);
@ -2071,13 +1997,11 @@ static int null_setkey(void *ctx, const unsigned char *key,
return (0);
}
static void *null_ctx_alloc(void)
{
static void *null_ctx_alloc(void) {
return ((void *) 1);
}
static void null_ctx_free(void *ctx)
{
static void null_ctx_free(void *ctx) {
((void) ctx);
}

View file

@ -84,8 +84,7 @@
*/
static int cmac_multiply_by_u(unsigned char *output,
const unsigned char *input,
size_t blocksize)
{
size_t blocksize) {
const unsigned char R_128 = 0x87;
const unsigned char R_64 = 0x1B;
unsigned char R_n, mask;
@ -130,8 +129,7 @@ static int cmac_multiply_by_u(unsigned char *output,
* - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
*/
static int cmac_generate_subkeys(mbedtls_cipher_context_t *ctx,
unsigned char *K1, unsigned char *K2)
{
unsigned char *K1, unsigned char *K2) {
int ret;
unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
size_t olen, block_size;
@ -163,8 +161,7 @@ exit:
#if !defined(MBEDTLS_CMAC_ALT)
static void cmac_xor_block(unsigned char *output, const unsigned char *input1,
const unsigned char *input2,
const size_t block_size)
{
const size_t block_size) {
size_t idx;
for (idx = 0; idx < block_size; idx++)
@ -180,8 +177,7 @@ static void cmac_xor_block(unsigned char *output, const unsigned char *input1,
static void cmac_pad(unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
size_t padded_block_len,
const unsigned char *last_block,
size_t last_block_len)
{
size_t last_block_len) {
size_t j;
for (j = 0; j < padded_block_len; j++) {
@ -195,8 +191,7 @@ static void cmac_pad(unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
}
int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
const unsigned char *key, size_t keybits)
{
const unsigned char *key, size_t keybits) {
mbedtls_cipher_type_t type;
mbedtls_cmac_context_t *cmac_ctx;
int retval;
@ -234,15 +229,14 @@ int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
}
int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
const unsigned char *input, size_t ilen)
{
const unsigned char *input, size_t ilen) {
mbedtls_cmac_context_t *cmac_ctx;
unsigned char *state;
int ret = 0;
size_t n, j, olen, block_size;
if (ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
ctx->cmac_ctx == NULL)
ctx->cmac_ctx == NULL)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
cmac_ctx = ctx->cmac_ctx;
@ -252,7 +246,7 @@ int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
/* Is there data still to process from the last call, that's greater in
* size than a block? */
if (cmac_ctx->unprocessed_len > 0 &&
ilen > block_size - cmac_ctx->unprocessed_len) {
ilen > block_size - cmac_ctx->unprocessed_len) {
memcpy(&cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
input,
block_size - cmac_ctx->unprocessed_len);
@ -298,8 +292,7 @@ exit:
}
int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
unsigned char *output)
{
unsigned char *output) {
mbedtls_cmac_context_t *cmac_ctx;
unsigned char *state, *last_block;
unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
@ -309,7 +302,7 @@ int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
size_t olen, block_size;
if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
output == NULL)
output == NULL)
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
cmac_ctx = ctx->cmac_ctx;
@ -354,8 +347,7 @@ exit:
return (ret);
}
int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx)
{
int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx) {
mbedtls_cmac_context_t *cmac_ctx;
if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL)
@ -376,8 +368,7 @@ int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx)
int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info,
const unsigned char *key, size_t keylen,
const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
mbedtls_cipher_context_t ctx;
int ret;
@ -411,8 +402,7 @@ exit:
*/
int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_length,
const unsigned char *input, size_t in_len,
unsigned char *output)
{
unsigned char *output) {
int ret;
const mbedtls_cipher_info_t *cipher_info;
unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
@ -747,8 +737,7 @@ static int cmac_test_subkeys(int verbose,
const unsigned char *subkeys,
mbedtls_cipher_type_t cipher_type,
int block_size,
int num_tests)
{
int num_tests) {
int i, ret = 0;
mbedtls_cipher_context_t ctx;
const mbedtls_cipher_info_t *cipher_info;
@ -791,7 +780,7 @@ static int cmac_test_subkeys(int verbose,
}
if ((ret = memcmp(K1, subkeys, block_size)) != 0 ||
(ret = memcmp(K2, &subkeys[block_size], block_size)) != 0) {
(ret = memcmp(K2, &subkeys[block_size], block_size)) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");
@ -823,8 +812,7 @@ static int cmac_test_wth_cipher(int verbose,
const unsigned char *expected_result,
mbedtls_cipher_type_t cipher_type,
int block_size,
int num_tests)
{
int num_tests) {
const mbedtls_cipher_info_t *cipher_info;
int i, ret = 0;
unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
@ -863,8 +851,7 @@ exit:
}
#if defined(MBEDTLS_AES_C)
static int test_aes128_cmac_prf(int verbose)
{
static int test_aes128_cmac_prf(int verbose) {
int i;
int ret;
unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
@ -873,7 +860,7 @@ static int test_aes128_cmac_prf(int verbose)
mbedtls_printf(" AES CMAC 128 PRF #%u: ", i);
ret = mbedtls_aes_cmac_prf_128(PRFK, PRFKlen[i], PRFM, 20, output);
if (ret != 0 ||
memcmp(output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE) != 0) {
memcmp(output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");
@ -887,8 +874,7 @@ static int test_aes128_cmac_prf(int verbose)
}
#endif /* MBEDTLS_AES_C */
int mbedtls_cmac_self_test(int verbose)
{
int mbedtls_cmac_self_test(int verbose) {
int ret;
#if defined(MBEDTLS_AES_C)

View file

@ -55,8 +55,7 @@
/*
* CTR_DRBG context initialization
*/
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx)
{
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_ctr_drbg_context));
#if defined(MBEDTLS_THREADING_C)
@ -74,8 +73,7 @@ int mbedtls_ctr_drbg_seed_entropy_len(
void *p_entropy,
const unsigned char *custom,
size_t len,
size_t entropy_len)
{
size_t entropy_len) {
int ret;
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
@ -106,14 +104,12 @@ int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx,
int (*f_entropy)(void *, unsigned char *, size_t),
void *p_entropy,
const unsigned char *custom,
size_t len)
{
size_t len) {
return (mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy, custom, len,
MBEDTLS_CTR_DRBG_ENTROPY_LEN));
}
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
{
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx) {
if (ctx == NULL)
return;
@ -124,24 +120,20 @@ void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ctr_drbg_context));
}
void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx, int resistance)
{
void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx, int resistance) {
ctx->prediction_resistance = resistance;
}
void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx, size_t len)
{
void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx, size_t len) {
ctx->entropy_len = len;
}
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval)
{
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval) {
ctx->reseed_interval = interval;
}
static int block_cipher_df(unsigned char *output,
const unsigned char *data, size_t data_len)
{
const unsigned char *data, size_t data_len) {
unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
@ -249,8 +241,7 @@ exit:
}
static int ctr_drbg_update_internal(mbedtls_ctr_drbg_context *ctx,
const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN])
{
const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN]) {
unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
unsigned char *p = tmp;
int i, j;
@ -291,8 +282,7 @@ static int ctr_drbg_update_internal(mbedtls_ctr_drbg_context *ctx,
}
void mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
const unsigned char *additional, size_t add_len)
{
const unsigned char *additional, size_t add_len) {
unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
if (add_len > 0) {
@ -307,14 +297,13 @@ void mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
}
int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
const unsigned char *additional, size_t len)
{
const unsigned char *additional, size_t len) {
unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
size_t seedlen = 0;
int ret;
if (ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ||
len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len)
len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len)
return (MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
memset(seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT);
@ -357,8 +346,7 @@ int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
int mbedtls_ctr_drbg_random_with_add(void *p_rng,
unsigned char *output, size_t output_len,
const unsigned char *additional, size_t add_len)
{
const unsigned char *additional, size_t add_len) {
int ret = 0;
mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
@ -376,7 +364,7 @@ int mbedtls_ctr_drbg_random_with_add(void *p_rng,
memset(add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN);
if (ctx->reseed_counter > ctx->reseed_interval ||
ctx->prediction_resistance) {
ctx->prediction_resistance) {
if ((ret = mbedtls_ctr_drbg_reseed(ctx, additional, add_len)) != 0) {
return (ret);
}
@ -426,8 +414,7 @@ int mbedtls_ctr_drbg_random_with_add(void *p_rng,
return (0);
}
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
{
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len) {
int ret;
mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
@ -447,8 +434,7 @@ int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_le
}
#if defined(MBEDTLS_FS_IO)
int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
{
int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path) {
int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
FILE *f;
unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
@ -471,8 +457,7 @@ exit:
return (ret);
}
int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
{
int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path) {
int ret = 0;
FILE *f;
size_t n;
@ -556,8 +541,7 @@ static const unsigned char result_nopr[16] = {
static size_t test_offset;
static int ctr_drbg_self_test_entropy(void *data, unsigned char *buf,
size_t len)
{
size_t len) {
const unsigned char *p = data;
memcpy(buf, p + test_offset, len);
test_offset += len;
@ -574,8 +558,7 @@ static int ctr_drbg_self_test_entropy(void *data, unsigned char *buf,
/*
* Checkup routine
*/
int mbedtls_ctr_drbg_self_test(int verbose)
{
int mbedtls_ctr_drbg_self_test(int verbose) {
mbedtls_ctr_drbg_context ctx;
unsigned char buf[16];

View file

@ -294,26 +294,22 @@ static const uint32_t RHs[16] = {
#define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
void mbedtls_des_init(mbedtls_des_context *ctx)
{
void mbedtls_des_init(mbedtls_des_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_des_context));
}
void mbedtls_des_free(mbedtls_des_context *ctx)
{
void mbedtls_des_free(mbedtls_des_context *ctx) {
if (ctx == NULL)
return;
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_des_context));
}
void mbedtls_des3_init(mbedtls_des3_context *ctx)
{
void mbedtls_des3_init(mbedtls_des3_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_des3_context));
}
void mbedtls_des3_free(mbedtls_des3_context *ctx)
{
void mbedtls_des3_free(mbedtls_des3_context *ctx) {
if (ctx == NULL)
return;
@ -332,8 +328,7 @@ static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
254
};
void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
{
void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
int i;
for (i = 0; i < MBEDTLS_DES_KEY_SIZE; i++)
@ -343,8 +338,7 @@ void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
/*
* Check the given key's parity, returns 1 on failure, 0 on SUCCESS
*/
int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE])
{
int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
int i;
for (i = 0; i < MBEDTLS_DES_KEY_SIZE; i++)
@ -397,8 +391,7 @@ static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE]
{ 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
};
int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE])
{
int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
int i;
for (i = 0; i < WEAK_KEY_COUNT; i++)
@ -409,8 +402,7 @@ int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE])
}
#if !defined(MBEDTLS_DES_SETKEY_ALT)
void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE])
{
void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
int i;
uint32_t X, Y, T;
@ -482,8 +474,7 @@ void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY
/*
* DES key schedule (56-bit, encryption)
*/
int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE])
{
int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
mbedtls_des_setkey(ctx->sk, key);
return (0);
@ -492,8 +483,7 @@ int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBE
/*
* DES key schedule (56-bit, decryption)
*/
int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE])
{
int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
int i;
mbedtls_des_setkey(ctx->sk, key);
@ -508,8 +498,7 @@ int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBE
static void des3_set2key(uint32_t esk[96],
uint32_t dsk[96],
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2])
{
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]) {
int i;
mbedtls_des_setkey(esk, key);
@ -534,8 +523,7 @@ static void des3_set2key(uint32_t esk[96],
* Triple-DES key schedule (112-bit, encryption)
*/
int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2])
{
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]) {
uint32_t sk[96];
des3_set2key(ctx->sk, sk, key);
@ -548,8 +536,7 @@ int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
* Triple-DES key schedule (112-bit, decryption)
*/
int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2])
{
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]) {
uint32_t sk[96];
des3_set2key(sk, ctx->sk, key);
@ -560,8 +547,7 @@ int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
static void des3_set3key(uint32_t esk[96],
uint32_t dsk[96],
const unsigned char key[24])
{
const unsigned char key[24]) {
int i;
mbedtls_des_setkey(esk, key);
@ -584,8 +570,7 @@ static void des3_set3key(uint32_t esk[96],
* Triple-DES key schedule (168-bit, encryption)
*/
int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3])
{
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]) {
uint32_t sk[96];
des3_set3key(ctx->sk, sk, key);
@ -598,8 +583,7 @@ int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
* Triple-DES key schedule (168-bit, decryption)
*/
int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3])
{
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]) {
uint32_t sk[96];
des3_set3key(sk, ctx->sk, key);
@ -614,8 +598,7 @@ int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
#if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
const unsigned char input[8],
unsigned char output[8])
{
unsigned char output[8]) {
int i;
uint32_t X, Y, T, *SK;
@ -649,8 +632,7 @@ int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
size_t length,
unsigned char iv[8],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int i;
unsigned char temp[8];
@ -695,8 +677,7 @@ int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
#if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
const unsigned char input[8],
unsigned char output[8])
{
unsigned char output[8]) {
int i;
uint32_t X, Y, T, *SK;
@ -740,8 +721,7 @@ int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
size_t length,
unsigned char iv[8],
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int i;
unsigned char temp[8];
@ -831,8 +811,7 @@ static const unsigned char des3_test_cbc_enc[3][8] = {
/*
* Checkup routine
*/
int mbedtls_des_self_test(int verbose)
{
int mbedtls_des_self_test(int verbose) {
int i, j, u, v, ret = 0;
mbedtls_des_context ctx;
mbedtls_des3_context ctx3;
@ -895,9 +874,9 @@ int mbedtls_des_self_test(int verbose)
}
if ((v == MBEDTLS_DES_DECRYPT &&
memcmp(buf, des3_test_ecb_dec[u], 8) != 0) ||
(v != MBEDTLS_DES_DECRYPT &&
memcmp(buf, des3_test_ecb_enc[u], 8) != 0)) {
memcmp(buf, des3_test_ecb_dec[u], 8) != 0) ||
(v != MBEDTLS_DES_DECRYPT &&
memcmp(buf, des3_test_ecb_enc[u], 8) != 0)) {
if (verbose != 0)
mbedtls_printf("failed\n");
@ -983,9 +962,9 @@ int mbedtls_des_self_test(int verbose)
}
if ((v == MBEDTLS_DES_DECRYPT &&
memcmp(buf, des3_test_cbc_dec[u], 8) != 0) ||
(v != MBEDTLS_DES_DECRYPT &&
memcmp(buf, des3_test_cbc_enc[u], 8) != 0)) {
memcmp(buf, des3_test_cbc_dec[u], 8) != 0) ||
(v != MBEDTLS_DES_DECRYPT &&
memcmp(buf, des3_test_cbc_enc[u], 8) != 0)) {
if (verbose != 0)
mbedtls_printf("failed\n");

View file

@ -49,8 +49,7 @@
* SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
*/
static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x,
const unsigned char *buf, size_t blen)
{
const unsigned char *buf, size_t blen) {
int ret;
size_t n_size = (grp->nbits + 7) / 8;
size_t use_size = blen > n_size ? n_size : blen;
@ -74,8 +73,7 @@ cleanup:
*/
int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret, key_tries, sign_tries, blind_tries;
mbedtls_ecp_point R;
mbedtls_mpi k, e, t;
@ -164,8 +162,7 @@ cleanup:
*/
int mbedtls_ecdsa_sign_det(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
mbedtls_md_type_t md_alg)
{
mbedtls_md_type_t md_alg) {
int ret;
mbedtls_hmac_drbg_context rng_ctx;
unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
@ -203,8 +200,7 @@ cleanup:
*/
int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
const unsigned char *buf, size_t blen,
const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
{
const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) {
int ret;
mbedtls_mpi e, s_inv, u1, u2;
mbedtls_ecp_point R;
@ -223,7 +219,7 @@ int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
* Step 1: make sure r and s are in range 1..n-1
*/
if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
goto cleanup;
}
@ -291,8 +287,7 @@ cleanup:
* Convert a signature (given by context) to ASN.1
*/
static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
unsigned char *sig, size_t *slen)
{
unsigned char *sig, size_t *slen) {
int ret;
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
unsigned char *p = buf + sizeof(buf);
@ -318,8 +313,7 @@ int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, mbedtls_md_type_t
const unsigned char *hash, size_t hlen,
unsigned char *sig, size_t *slen,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
mbedtls_mpi r, s;
@ -353,8 +347,7 @@ cleanup:
int mbedtls_ecdsa_write_signature_det(mbedtls_ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
unsigned char *sig, size_t *slen,
mbedtls_md_type_t md_alg)
{
mbedtls_md_type_t md_alg) {
return (mbedtls_ecdsa_write_signature(ctx, md_alg, hash, hlen, sig, slen,
NULL, NULL));
}
@ -365,8 +358,7 @@ int mbedtls_ecdsa_write_signature_det(mbedtls_ecdsa_context *ctx,
*/
int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
const unsigned char *sig, size_t slen)
{
const unsigned char *sig, size_t slen) {
int ret;
unsigned char *p = (unsigned char *) sig;
const unsigned char *end = sig + slen;
@ -389,7 +381,7 @@ int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
}
if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
(ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
(ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}
@ -416,8 +408,7 @@ cleanup:
* Generate key pair
*/
int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
return (mbedtls_ecp_group_load(&ctx->grp, gid) ||
mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng));
}
@ -426,13 +417,12 @@ int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
/*
* Set context from an mbedtls_ecp_keypair
*/
int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
{
int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key) {
int ret;
if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 ||
(ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
(ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
(ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
(ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
mbedtls_ecdsa_free(ctx);
}
@ -442,16 +432,14 @@ int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_key
/*
* Initialize context
*/
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
{
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx) {
mbedtls_ecp_keypair_init(ctx);
}
/*
* Free context
*/
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
{
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx) {
mbedtls_ecp_keypair_free(ctx);
}

View file

@ -169,16 +169,14 @@ static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
/*
* List of supported curves and associated info
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
{
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void) {
return (ecp_supported_curves);
}
/*
* List of supported curves, group ID only
*/
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
{
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void) {
static int init_done = 0;
if (! init_done) {
@ -186,8 +184,8 @@ const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
ecp_supported_grp_id[i++] = curve_info->grp_id;
}
ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
@ -201,13 +199,12 @@ const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
/*
* Get the curve info for the internal identifier
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
{
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id) {
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
if (curve_info->grp_id == grp_id)
return (curve_info);
}
@ -218,13 +215,12 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_gro
/*
* Get the curve info from the TLS identifier
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
{
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id) {
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
if (curve_info->tls_id == tls_id)
return (curve_info);
}
@ -235,13 +231,12 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id
/*
* Get the curve info from the name
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
{
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name) {
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
if (strcmp(curve_info->name, name) == 0)
return (curve_info);
}
@ -252,8 +247,7 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
/*
* Get the type of a curve
*/
static inline ecp_curve_type ecp_get_type(const mbedtls_ecp_group *grp)
{
static inline ecp_curve_type ecp_get_type(const mbedtls_ecp_group *grp) {
if (grp->G.X.p == NULL)
return (ECP_TYPE_NONE);
@ -266,8 +260,7 @@ static inline ecp_curve_type ecp_get_type(const mbedtls_ecp_group *grp)
/*
* Initialize (the components of) a point
*/
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
{
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt) {
if (pt == NULL)
return;
@ -279,8 +272,7 @@ void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
/*
* Initialize (the components of) a group
*/
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
{
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp) {
if (grp == NULL)
return;
@ -290,8 +282,7 @@ void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
/*
* Initialize (the components of) a key pair
*/
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
{
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key) {
if (key == NULL)
return;
@ -303,8 +294,7 @@ void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
/*
* Unallocate (the components of) a point
*/
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
{
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt) {
if (pt == NULL)
return;
@ -316,8 +306,7 @@ void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
/*
* Unallocate (the components of) a group
*/
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
{
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp) {
size_t i;
if (grp == NULL)
@ -343,8 +332,7 @@ void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
/*
* Unallocate (the components of) a key pair
*/
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
{
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key) {
if (key == NULL)
return;
@ -356,8 +344,7 @@ void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
/*
* Copy the contents of a point
*/
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
{
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q) {
int ret;
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
@ -371,16 +358,14 @@ cleanup:
/*
* Copy the contents of a group object
*/
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
{
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src) {
return mbedtls_ecp_group_load(dst, src->id);
}
/*
* Set point to zero
*/
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
{
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt) {
int ret;
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
@ -394,8 +379,7 @@ cleanup:
/*
* Tell if a point is zero
*/
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
{
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt) {
return (mbedtls_mpi_cmp_int(&pt->Z, 0) == 0);
}
@ -403,11 +387,10 @@ int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
* Compare two points lazyly
*/
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
const mbedtls_ecp_point *Q)
{
const mbedtls_ecp_point *Q) {
if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
return (0);
}
@ -418,8 +401,7 @@ int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
* Import a non-zero point from ASCII strings
*/
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
const char *x, const char *y)
{
const char *x, const char *y) {
int ret;
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
@ -435,13 +417,12 @@ cleanup:
*/
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
int format, size_t *olen,
unsigned char *buf, size_t buflen)
{
unsigned char *buf, size_t buflen) {
int ret = 0;
size_t plen;
if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
format != MBEDTLS_ECP_PF_COMPRESSED)
format != MBEDTLS_ECP_PF_COMPRESSED)
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
/*
@ -486,8 +467,7 @@ cleanup:
* Import a point from unsigned binary data (SEC1 2.3.4)
*/
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
const unsigned char *buf, size_t ilen)
{
const unsigned char *buf, size_t ilen) {
int ret;
size_t plen;
@ -524,8 +504,7 @@ cleanup:
* } ECPoint;
*/
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
const unsigned char **buf, size_t buf_len)
{
const unsigned char **buf, size_t buf_len) {
unsigned char data_len;
const unsigned char *buf_start;
@ -556,8 +535,7 @@ int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *
*/
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
int format, size_t *olen,
unsigned char *buf, size_t blen)
{
unsigned char *buf, size_t blen) {
int ret;
/*
@ -582,8 +560,7 @@ int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_
/*
* Set a group from an ECParameters record (RFC 4492)
*/
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len)
{
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len) {
uint16_t tls_id;
const mbedtls_ecp_curve_info *curve_info;
@ -616,8 +593,7 @@ int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf
* Write the ECParameters record corresponding to a group (RFC 4492)
*/
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
unsigned char *buf, size_t blen)
{
unsigned char *buf, size_t blen) {
const mbedtls_ecp_curve_info *curve_info;
if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL)
@ -650,8 +626,7 @@ int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
*
* This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
*/
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
{
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp) {
int ret;
if (grp->modp == NULL)
@ -659,7 +634,7 @@ static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
/* N->s < 0 is a much faster test, which fails only if N is 0 */
if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
}
@ -729,8 +704,7 @@ cleanup:
* Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
* Cost: 1N := 1I + 3M + 1S
*/
static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
{
static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt) {
int ret;
mbedtls_mpi Zi, ZZi;
@ -787,8 +761,7 @@ cleanup:
* Cost: 1N(t) := 1I + (6t - 3)M + 1S
*/
static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *T[], size_t t_len)
{
mbedtls_ecp_point *T[], size_t t_len) {
int ret;
size_t i;
mbedtls_mpi *c, u, Zi, ZZi;
@ -881,8 +854,7 @@ cleanup:
*/
static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *Q,
unsigned char inv)
{
unsigned char inv) {
int ret;
unsigned char nonzero;
mbedtls_mpi mQY;
@ -915,8 +887,7 @@ cleanup:
* 3M + 6S + 1a otherwise
*/
static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point *P)
{
const mbedtls_ecp_point *P) {
int ret;
mbedtls_mpi M, S, T, U;
@ -1039,8 +1010,7 @@ cleanup:
* Cost: 1A := 8M + 3S
*/
static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
{
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q) {
int ret;
mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
@ -1151,8 +1121,7 @@ cleanup:
* This countermeasure was first suggested in [2].
*/
static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret;
mbedtls_mpi l, ll;
size_t p_size;
@ -1236,8 +1205,7 @@ cleanup:
* (the result will be incorrect if these assumptions are not satisfied)
*/
static void ecp_comb_fixed(unsigned char x[], size_t d,
unsigned char w, const mbedtls_mpi *m)
{
unsigned char w, const mbedtls_mpi *m) {
size_t i, j;
unsigned char c, cc, adjust;
@ -1276,8 +1244,7 @@ static void ecp_comb_fixed(unsigned char x[], size_t d,
*/
static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
unsigned char w, size_t d)
{
unsigned char w, size_t d) {
int ret;
unsigned char i, k;
size_t j;
@ -1326,8 +1293,7 @@ cleanup:
*/
static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point T[], unsigned char t_len,
unsigned char i)
{
unsigned char i) {
int ret;
unsigned char ii, j;
@ -1357,8 +1323,7 @@ static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point T[], unsigned char t_len,
const unsigned char x[], size_t d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
mbedtls_ecp_point Txi;
size_t i;
@ -1392,8 +1357,7 @@ cleanup:
static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
unsigned char w, m_is_odd, p_eq_g, pre_len, i;
size_t d;
@ -1521,8 +1485,7 @@ cleanup:
* Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
* Cost: 1M + 1I
*/
static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
{
static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P) {
int ret;
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
@ -1549,8 +1512,7 @@ cleanup:
* Cost: 2M
*/
static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret;
mbedtls_mpi l;
size_t p_size;
@ -1605,8 +1567,7 @@ cleanup:
static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *R, mbedtls_ecp_point *S,
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
const mbedtls_mpi *d)
{
const mbedtls_mpi *d) {
int ret;
mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
@ -1684,8 +1645,7 @@ cleanup:
static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
size_t i;
unsigned char b;
@ -1745,8 +1705,7 @@ cleanup:
*/
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
char is_grp_capable = 0;
@ -1757,7 +1716,7 @@ int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
if ((ret = mbedtls_ecp_check_privkey(grp, m)) != 0 ||
(ret = mbedtls_ecp_check_pubkey(grp, P)) != 0)
(ret = mbedtls_ecp_check_pubkey(grp, P)) != 0)
return (ret);
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
@ -1792,16 +1751,15 @@ cleanup:
* Check that an affine point is valid as a public key,
* short weierstrass curves (SEC1 3.2.3.1)
*/
static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
{
static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) {
int ret;
mbedtls_mpi YY, RHS;
/* pt coordinates must be normalized for our checks */
if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0)
mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0)
return (MBEDTLS_ERR_ECP_INVALID_KEY);
mbedtls_mpi_init(&YY);
@ -1849,8 +1807,7 @@ cleanup:
static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
mbedtls_ecp_point *R,
const mbedtls_mpi *m,
const mbedtls_ecp_point *P)
{
const mbedtls_ecp_point *P) {
int ret;
if (mbedtls_mpi_cmp_int(m, 1) == 0) {
@ -1873,8 +1830,7 @@ cleanup:
*/
int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
{
const mbedtls_mpi *n, const mbedtls_ecp_point *Q) {
int ret;
mbedtls_ecp_point mP;
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
@ -1916,8 +1872,7 @@ cleanup:
/*
* Check validity of a public key for Montgomery curves with x-only schemes
*/
static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
{
static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) {
/* [Curve25519 p. 5] Just check X is the correct number of bytes */
/* Allow any public value, if it's too big then we'll just reduce it mod p
* (RFC 7748 sec. 5 para. 3). */
@ -1931,8 +1886,7 @@ static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_p
/*
* Check that a point is valid as a public key
*/
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
{
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) {
/* Must use affine coordinates */
if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0)
return (MBEDTLS_ERR_ECP_INVALID_KEY);
@ -1951,14 +1905,13 @@ int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_poi
/*
* Check that an mbedtls_mpi is valid as a private key
*/
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d)
{
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d) {
#if defined(ECP_MONTGOMERY)
if (ecp_get_type(grp) == ECP_TYPE_MONTGOMERY) {
/* see RFC 7748 sec. 5 para. 5 */
if (mbedtls_mpi_get_bit(d, 0) != 0 ||
mbedtls_mpi_get_bit(d, 1) != 0 ||
mbedtls_mpi_bitlen(d) - 1 != grp->nbits) /* mbedtls_mpi_bitlen is one-based! */
mbedtls_mpi_get_bit(d, 1) != 0 ||
mbedtls_mpi_bitlen(d) - 1 != grp->nbits) /* mbedtls_mpi_bitlen is one-based! */
return (MBEDTLS_ERR_ECP_INVALID_KEY);
/* see [Curve25519] page 5 */
@ -1972,7 +1925,7 @@ int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d
if (ecp_get_type(grp) == ECP_TYPE_SHORT_WEIERSTRASS) {
/* see SEC1 3.2 */
if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0)
mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0)
return (MBEDTLS_ERR_ECP_INVALID_KEY);
else
return (0);
@ -1989,8 +1942,7 @@ int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
const mbedtls_ecp_point *G,
mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret;
size_t n_size = (grp->nbits + 7) / 8;
@ -2065,8 +2017,7 @@ cleanup:
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
return (mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng));
}
@ -2074,8 +2025,7 @@ int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
* Generate a keypair, prettier wrapper
*/
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret;
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0)
@ -2087,17 +2037,16 @@ int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
/*
* Check a public-private key pair
*/
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
{
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv) {
int ret;
mbedtls_ecp_point Q;
mbedtls_ecp_group grp;
if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
pub->grp.id != prv->grp.id ||
mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
pub->grp.id != prv->grp.id ||
mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
}
@ -2111,8 +2060,8 @@ int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, NULL, NULL));
if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}
@ -2129,8 +2078,7 @@ cleanup:
/*
* Checkup routine
*/
int mbedtls_ecp_self_test(int verbose)
{
int mbedtls_ecp_self_test(int verbose) {
int ret;
size_t i;
mbedtls_ecp_group grp;
@ -2184,8 +2132,8 @@ int mbedtls_ecp_self_test(int verbose)
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &R, &m, &grp.G, NULL, NULL));
if (add_count != add_c_prev ||
dbl_count != dbl_c_prev ||
mul_count != mul_c_prev) {
dbl_count != dbl_c_prev ||
mul_count != mul_c_prev) {
if (verbose != 0)
mbedtls_printf("failed (%u)\n", (unsigned int) i);
@ -2219,8 +2167,8 @@ int mbedtls_ecp_self_test(int verbose)
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &R, &m, &P, NULL, NULL));
if (add_count != add_c_prev ||
dbl_count != dbl_c_prev ||
mul_count != mul_c_prev) {
dbl_count != dbl_c_prev ||
mul_count != mul_c_prev) {
if (verbose != 0)
mbedtls_printf("failed (%u)\n", (unsigned int) i);

View file

@ -555,8 +555,7 @@ static const mbedtls_mpi_uint brainpoolP512r1_n[] = {
* Create an MPI from embedded constants
* (assumes len is an exact multiple of sizeof mbedtls_mpi_uint)
*/
static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len)
{
static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len) {
X->s = 1;
X->n = len / sizeof(mbedtls_mpi_uint);
X->p = (mbedtls_mpi_uint *) p;
@ -565,8 +564,7 @@ static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_
/*
* Set an MPI to static value 1
*/
static inline void ecp_mpi_set1(mbedtls_mpi *X)
{
static inline void ecp_mpi_set1(mbedtls_mpi *X) {
static mbedtls_mpi_uint one[] = { 1 };
X->s = 1;
X->n = 1;
@ -582,8 +580,7 @@ static int ecp_group_load(mbedtls_ecp_group *grp,
const mbedtls_mpi_uint *b, size_t blen,
const mbedtls_mpi_uint *gx, size_t gxlen,
const mbedtls_mpi_uint *gy, size_t gylen,
const mbedtls_mpi_uint *n, size_t nlen)
{
const mbedtls_mpi_uint *n, size_t nlen) {
ecp_mpi_load(&grp->P, p, plen);
if (a != NULL)
ecp_mpi_load(&grp->A, a, alen);
@ -662,8 +659,7 @@ static int ecp_mod_p256k1(mbedtls_mpi *);
/*
* Specialized function for creating the Curve25519 group
*/
static int ecp_use_curve25519(mbedtls_ecp_group *grp)
{
static int ecp_use_curve25519(mbedtls_ecp_group *grp) {
int ret;
/* Actually ( A + 2 ) / 4 */
@ -701,8 +697,7 @@ cleanup:
/*
* Specialized function for creating the Curve448 group
*/
static int ecp_use_curve448(mbedtls_ecp_group *grp)
{
static int ecp_use_curve448(mbedtls_ecp_group *grp) {
mbedtls_mpi Ns;
int ret;
@ -746,8 +741,7 @@ cleanup:
/*
* Set a group using well-known domain parameters
*/
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
{
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id) {
mbedtls_ecp_group_free(grp);
grp->id = id;
@ -859,8 +853,7 @@ int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
*/
/* Add 64-bit chunks (dst += src) and update carry */
static inline void add64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry)
{
static inline void add64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry) {
unsigned char i;
mbedtls_mpi_uint c = 0;
for (i = 0; i < 8 / sizeof(mbedtls_mpi_uint); i++, dst++, src++) {
@ -873,8 +866,7 @@ static inline void add64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_m
}
/* Add carry to a 64-bit chunk and update carry */
static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry)
{
static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry) {
unsigned char i;
for (i = 0; i < 8 / sizeof(mbedtls_mpi_uint); i++, dst++) {
*dst += *carry;
@ -891,8 +883,7 @@ static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry)
/*
* Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
*/
static int ecp_mod_p192(mbedtls_mpi *N)
{
static int ecp_mod_p192(mbedtls_mpi *N) {
int ret;
mbedtls_mpi_uint c = 0;
mbedtls_mpi_uint *p, *end;
@ -970,14 +961,12 @@ cleanup:
/*
* Helpers for addition and subtraction of chunks, with signed carry.
*/
static inline void add32(uint32_t *dst, uint32_t src, signed char *carry)
{
static inline void add32(uint32_t *dst, uint32_t src, signed char *carry) {
*dst += src;
*carry += (*dst < src);
}
static inline void sub32(uint32_t *dst, uint32_t src, signed char *carry)
{
static inline void sub32(uint32_t *dst, uint32_t src, signed char *carry) {
*carry -= (*dst < src);
*dst -= src;
}
@ -1023,8 +1012,7 @@ static inline void sub32(uint32_t *dst, uint32_t src, signed char *carry)
* If the result is negative, we get it in the form
* c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
*/
static inline int fix_negative(mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits)
{
static inline int fix_negative(mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits) {
int ret;
/* C = - c * 2^(bits + 32) */
@ -1050,8 +1038,7 @@ cleanup:
/*
* Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
*/
static int ecp_mod_p224(mbedtls_mpi *N)
{
static int ecp_mod_p224(mbedtls_mpi *N) {
INIT(224);
SUB(7);
@ -1088,8 +1075,7 @@ cleanup:
/*
* Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
*/
static int ecp_mod_p256(mbedtls_mpi *N)
{
static int ecp_mod_p256(mbedtls_mpi *N) {
INIT(256);
ADD(8);
@ -1172,8 +1158,7 @@ cleanup:
/*
* Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
*/
static int ecp_mod_p384(mbedtls_mpi *N)
{
static int ecp_mod_p384(mbedtls_mpi *N) {
INIT(384);
ADD(12);
@ -1299,8 +1284,7 @@ cleanup:
* Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
* Write N as A1 + 2^521 A0, return A0 + A1
*/
static int ecp_mod_p521(mbedtls_mpi *N)
{
static int ecp_mod_p521(mbedtls_mpi *N) {
int ret;
size_t i;
mbedtls_mpi M;
@ -1348,8 +1332,7 @@ cleanup:
* Fast quasi-reduction modulo p255 = 2^255 - 19
* Write N as A0 + 2^255 A1, return A0 + 19 * A1
*/
static int ecp_mod_p255(mbedtls_mpi *N)
{
static int ecp_mod_p255(mbedtls_mpi *N) {
int ret;
size_t i;
mbedtls_mpi M;
@ -1405,8 +1388,7 @@ cleanup:
* but for 64-bit targets it should use half the number of operations if we do
* the reduction with 224-bit limbs, since mpi_add_mpi will then use 64-bit adds.
*/
static int ecp_mod_p448(mbedtls_mpi *N)
{
static int ecp_mod_p448(mbedtls_mpi *N) {
int ret;
size_t i;
mbedtls_mpi M, Q;
@ -1467,8 +1449,7 @@ cleanup:
#define P_KOBLITZ_MAX ( 256 / 8 / sizeof( mbedtls_mpi_uint ) ) // Max limbs in P
#define P_KOBLITZ_R ( 8 / sizeof( mbedtls_mpi_uint ) ) // Limbs in R
static inline int ecp_mod_koblitz(mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p_limbs,
size_t adjust, size_t shift, mbedtls_mpi_uint mask)
{
size_t adjust, size_t shift, mbedtls_mpi_uint mask) {
int ret;
size_t i;
mbedtls_mpi M, R;
@ -1540,8 +1521,7 @@ cleanup:
* Fast quasi-reduction modulo p192k1 = 2^192 - R,
* with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
*/
static int ecp_mod_p192k1(mbedtls_mpi *N)
{
static int ecp_mod_p192k1(mbedtls_mpi *N) {
static mbedtls_mpi_uint Rp[] = {
BYTES_TO_T_UINT_8(0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
};
@ -1555,8 +1535,7 @@ static int ecp_mod_p192k1(mbedtls_mpi *N)
* Fast quasi-reduction modulo p224k1 = 2^224 - R,
* with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
*/
static int ecp_mod_p224k1(mbedtls_mpi *N)
{
static int ecp_mod_p224k1(mbedtls_mpi *N) {
static mbedtls_mpi_uint Rp[] = {
BYTES_TO_T_UINT_8(0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
};
@ -1575,8 +1554,7 @@ static int ecp_mod_p224k1(mbedtls_mpi *N)
* Fast quasi-reduction modulo p256k1 = 2^256 - R,
* with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
*/
static int ecp_mod_p256k1(mbedtls_mpi *N)
{
static int ecp_mod_p256k1(mbedtls_mpi *N) {
static mbedtls_mpi_uint Rp[] = {
BYTES_TO_T_UINT_8(0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
};

View file

@ -64,8 +64,7 @@
#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
{
void mbedtls_entropy_init(mbedtls_entropy_context *ctx) {
ctx->source_count = 0;
memset(ctx->source, 0, sizeof(ctx->source));
@ -121,8 +120,7 @@ void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
}
void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
{
void mbedtls_entropy_free(mbedtls_entropy_context *ctx) {
#if defined(MBEDTLS_HAVEGE_C)
mbedtls_havege_free(&ctx->havege_data);
#endif
@ -144,8 +142,7 @@ void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
mbedtls_entropy_f_source_ptr f_source, void *p_source,
size_t threshold, int strong)
{
size_t threshold, int strong) {
int idx, ret = 0;
#if defined(MBEDTLS_THREADING_C)
@ -179,8 +176,7 @@ exit:
* Entropy accumulator update
*/
static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
const unsigned char *data, size_t len)
{
const unsigned char *data, size_t len) {
unsigned char header[2];
unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
size_t use_len = len;
@ -209,7 +205,7 @@ static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
*/
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
if (ctx->accumulator_started == 0 &&
(ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0)
(ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0)
goto cleanup;
else
ctx->accumulator_started = 1;
@ -218,7 +214,7 @@ static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
ret = mbedtls_sha512_update_ret(&ctx->accumulator, p, use_len);
#else
if (ctx->accumulator_started == 0 &&
(ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0)
(ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0)
goto cleanup;
else
ctx->accumulator_started = 1;
@ -234,8 +230,7 @@ cleanup:
}
int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
const unsigned char *data, size_t len)
{
const unsigned char *data, size_t len) {
int ret;
#if defined(MBEDTLS_THREADING_C)
@ -256,8 +251,7 @@ int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
/*
* Run through the different sources to add entropy to our accumulator
*/
static int entropy_gather_internal(mbedtls_entropy_context *ctx)
{
static int entropy_gather_internal(mbedtls_entropy_context *ctx) {
int ret, i, have_one_strong = 0;
unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
size_t olen;
@ -301,8 +295,7 @@ cleanup:
/*
* Thread-safe wrapper for entropy_gather_internal()
*/
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
{
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx) {
int ret;
#if defined(MBEDTLS_THREADING_C)
@ -320,8 +313,7 @@ int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
return (ret);
}
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
{
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len) {
int ret, count = 0, i, done;
mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
@ -433,8 +425,7 @@ exit:
}
#if defined(MBEDTLS_ENTROPY_NV_SEED)
int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
{
int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx) {
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
@ -454,8 +445,7 @@ int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
#endif /* MBEDTLS_ENTROPY_NV_SEED */
#if defined(MBEDTLS_FS_IO)
int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
{
int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path) {
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
FILE *f;
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
@ -480,8 +470,7 @@ exit:
return (ret);
}
int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
{
int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path) {
int ret = 0;
FILE *f;
size_t n;
@ -519,8 +508,7 @@ int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *p
* Dummy source function
*/
static int entropy_dummy_source(void *data, unsigned char *output,
size_t len, size_t *olen)
{
size_t len, size_t *olen) {
((void) data);
memset(output, 0x2a, len);
@ -532,8 +520,7 @@ static int entropy_dummy_source(void *data, unsigned char *output,
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
{
static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len) {
int ret = 0;
size_t entropy_len = 0;
size_t olen = 0;
@ -557,8 +544,7 @@ static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t bu
static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
size_t buf_len)
{
size_t buf_len) {
unsigned char set = 0xFF;
unsigned char unset = 0x00;
size_t i;
@ -582,8 +568,7 @@ static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
* are not equal.
* - The error code returned by the entropy source is not an error.
*/
int mbedtls_entropy_source_self_test(int verbose)
{
int mbedtls_entropy_source_self_test(int verbose) {
int ret = 0;
unsigned char buf0[2 * sizeof(unsigned long long int)];
unsigned char buf1[2 * sizeof(unsigned long long int)];
@ -629,8 +614,7 @@ cleanup:
* test that the functions don't cause errors and write the correct
* amount of data to buffers.
*/
int mbedtls_entropy_self_test(int verbose)
{
int mbedtls_entropy_self_test(int verbose) {
int ret = 1;
#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
mbedtls_entropy_context ctx;

View file

@ -66,8 +66,7 @@
#include <wincrypt.h>
int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
size_t *olen)
{
size_t *olen) {
HCRYPTPROV provider;
((void) data);
*olen = 0;
@ -100,8 +99,7 @@ int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
#if defined(SYS_getrandom)
#define HAVE_GETRANDOM
static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
{
static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags) {
/* MemSan cannot understand that the syscall writes to the buffer */
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
@ -114,8 +112,7 @@ static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
#include <sys/utsname.h>
/* Check if version is at least 3.17.0 */
static int check_version_3_17_plus(void)
{
static int check_version_3_17_plus(void) {
int minor;
struct utsname un;
const char *ver;
@ -155,8 +152,7 @@ static int has_getrandom = -1;
#include <stdio.h>
int mbedtls_platform_entropy_poll(void *data,
unsigned char *output, size_t len, size_t *olen)
{
unsigned char *output, size_t len, size_t *olen) {
FILE *file;
size_t read_len;
((void) data);
@ -198,8 +194,7 @@ int mbedtls_platform_entropy_poll(void *data,
#if defined(MBEDTLS_TEST_NULL_ENTROPY)
int mbedtls_null_entropy_poll(void *data,
unsigned char *output, size_t len, size_t *olen)
{
unsigned char *output, size_t len, size_t *olen) {
((void) data);
((void) output);
*olen = 0;
@ -215,8 +210,7 @@ int mbedtls_null_entropy_poll(void *data,
#if defined(MBEDTLS_TIMING_C)
int mbedtls_hardclock_poll(void *data,
unsigned char *output, size_t len, size_t *olen)
{
unsigned char *output, size_t len, size_t *olen) {
unsigned long timer = mbedtls_timing_hardclock();
((void) data);
*olen = 0;
@ -233,8 +227,7 @@ int mbedtls_hardclock_poll(void *data,
#if defined(MBEDTLS_HAVEGE_C)
int mbedtls_havege_poll(void *data,
unsigned char *output, size_t len, size_t *olen)
{
unsigned char *output, size_t len, size_t *olen) {
mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
*olen = 0;
@ -249,8 +242,7 @@ int mbedtls_havege_poll(void *data,
#if defined(MBEDTLS_ENTROPY_NV_SEED)
int mbedtls_nv_seed_poll(void *data,
unsigned char *output, size_t len, size_t *olen)
{
unsigned char *output, size_t len, size_t *olen) {
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
((void) data);

View file

@ -208,8 +208,7 @@
#endif
void mbedtls_strerror(int ret, char *buf, size_t buflen)
{
void mbedtls_strerror(int ret, char *buf, size_t buflen) {
size_t len;
int use_ret;
@ -881,8 +880,7 @@ void mbedtls_strerror(int ret, char *buf, size_t buflen)
/*
* Provide an non-function in case MBEDTLS_ERROR_C is not defined
*/
void mbedtls_strerror(int ret, char *buf, size_t buflen)
{
void mbedtls_strerror(int ret, char *buf, size_t buflen) {
((void) ret);
if (buflen > 0)

View file

@ -89,13 +89,11 @@ static const int supported_digests[] = {
MBEDTLS_MD_NONE
};
const int *mbedtls_md_list(void)
{
const int *mbedtls_md_list(void) {
return (supported_digests);
}
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
{
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name) {
if (NULL == md_name)
return (NULL);
@ -135,8 +133,7 @@ const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
return (NULL);
}
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
{
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type) {
switch (md_type) {
#if defined(MBEDTLS_MD2_C)
case MBEDTLS_MD_MD2:
@ -175,13 +172,11 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
}
}
void mbedtls_md_init(mbedtls_md_context_t *ctx)
{
void mbedtls_md_init(mbedtls_md_context_t *ctx) {
memset(ctx, 0, sizeof(mbedtls_md_context_t));
}
void mbedtls_md_free(mbedtls_md_context_t *ctx)
{
void mbedtls_md_free(mbedtls_md_context_t *ctx) {
if (ctx == NULL || ctx->md_info == NULL)
return;
@ -198,11 +193,10 @@ void mbedtls_md_free(mbedtls_md_context_t *ctx)
}
int mbedtls_md_clone(mbedtls_md_context_t *dst,
const mbedtls_md_context_t *src)
{
const mbedtls_md_context_t *src) {
if (dst == NULL || dst->md_info == NULL ||
src == NULL || src->md_info == NULL ||
dst->md_info != src->md_info) {
src == NULL || src->md_info == NULL ||
dst->md_info != src->md_info) {
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
}
@ -212,14 +206,12 @@ int mbedtls_md_clone(mbedtls_md_context_t *dst,
}
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info)
{
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info) {
return mbedtls_md_setup(ctx, md_info, 1);
}
#endif
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
{
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac) {
if (md_info == NULL || ctx == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
@ -239,24 +231,21 @@ int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info
return (0);
}
int mbedtls_md_starts(mbedtls_md_context_t *ctx)
{
int mbedtls_md_starts(mbedtls_md_context_t *ctx) {
if (ctx == NULL || ctx->md_info == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
return (ctx->md_info->starts_func(ctx->md_ctx));
}
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
{
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen) {
if (ctx == NULL || ctx->md_info == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
return (ctx->md_info->update_func(ctx->md_ctx, input, ilen));
}
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
{
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output) {
if (ctx == NULL || ctx->md_info == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
@ -264,8 +253,7 @@ int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
}
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
if (md_info == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
@ -273,8 +261,7 @@ int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, siz
}
#if defined(MBEDTLS_FS_IO)
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
{
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output) {
int ret;
FILE *f;
size_t n;
@ -313,8 +300,7 @@ cleanup:
}
#endif /* MBEDTLS_FS_IO */
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
{
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen) {
int ret;
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
unsigned char *ipad, *opad;
@ -358,16 +344,14 @@ cleanup:
return (ret);
}
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
{
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen) {
if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
return (ctx->md_info->update_func(ctx->md_ctx, input, ilen));
}
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
{
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output) {
int ret;
unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
unsigned char *opad;
@ -390,8 +374,7 @@ int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
return (ctx->md_info->finish_func(ctx->md_ctx, output));
}
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
{
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx) {
int ret;
unsigned char *ipad;
@ -409,8 +392,7 @@ int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
int mbedtls_md_hmac(const mbedtls_md_info_t *md_info,
const unsigned char *key, size_t keylen,
const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
mbedtls_md_context_t ctx;
int ret;
@ -435,32 +417,28 @@ cleanup:
return (ret);
}
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data)
{
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data) {
if (ctx == NULL || ctx->md_info == NULL)
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
return (ctx->md_info->process_func(ctx->md_ctx, data));
}
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
{
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info) {
if (md_info == NULL)
return (0);
return md_info->size;
}
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
{
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info) {
if (md_info == NULL)
return (MBEDTLS_MD_NONE);
return md_info->type;
}
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
{
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info) {
if (md_info == NULL)
return (NULL);

View file

@ -73,13 +73,11 @@
}
#endif
void mbedtls_md5_init(mbedtls_md5_context *ctx)
{
void mbedtls_md5_init(mbedtls_md5_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_md5_context));
}
void mbedtls_md5_free(mbedtls_md5_context *ctx)
{
void mbedtls_md5_free(mbedtls_md5_context *ctx) {
if (ctx == NULL)
return;
@ -87,16 +85,14 @@ void mbedtls_md5_free(mbedtls_md5_context *ctx)
}
void mbedtls_md5_clone(mbedtls_md5_context *dst,
const mbedtls_md5_context *src)
{
const mbedtls_md5_context *src) {
*dst = *src;
}
/*
* MD5 context setup
*/
int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx)
{
int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx) {
ctx->total[0] = 0;
ctx->total[1] = 0;
@ -109,16 +105,14 @@ int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx)
}
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_md5_starts(mbedtls_md5_context *ctx)
{
void mbedtls_md5_starts(mbedtls_md5_context *ctx) {
mbedtls_md5_starts_ret(ctx);
}
#endif
#if !defined(MBEDTLS_MD5_PROCESS_ALT)
int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
const unsigned char data[64])
{
const unsigned char data[64]) {
uint32_t X[16], A, B, C, D;
GET_UINT32_LE(X[ 0], data, 0);
@ -244,8 +238,7 @@ int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_md5_process(mbedtls_md5_context *ctx,
const unsigned char data[64])
{
const unsigned char data[64]) {
mbedtls_internal_md5_process(ctx, data);
}
#endif
@ -256,8 +249,7 @@ void mbedtls_md5_process(mbedtls_md5_context *ctx,
*/
int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
int ret;
size_t fill;
uint32_t left;
@ -302,8 +294,7 @@ int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_md5_update(mbedtls_md5_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
mbedtls_md5_update_ret(ctx, input, ilen);
}
#endif
@ -312,8 +303,7 @@ void mbedtls_md5_update(mbedtls_md5_context *ctx,
* MD5 final digest
*/
int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
unsigned char output[16])
{
unsigned char output[16]) {
int ret;
uint32_t used;
uint32_t high, low;
@ -364,8 +354,7 @@ int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_md5_finish(mbedtls_md5_context *ctx,
unsigned char output[16])
{
unsigned char output[16]) {
mbedtls_md5_finish_ret(ctx, output);
}
#endif
@ -377,8 +366,7 @@ void mbedtls_md5_finish(mbedtls_md5_context *ctx,
*/
int mbedtls_md5_ret(const unsigned char *input,
size_t ilen,
unsigned char output[16])
{
unsigned char output[16]) {
int ret;
mbedtls_md5_context ctx;
@ -402,8 +390,7 @@ exit:
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_md5(const unsigned char *input,
size_t ilen,
unsigned char output[16])
{
unsigned char output[16]) {
mbedtls_md5_ret(input, ilen, output);
}
#endif
@ -463,8 +450,7 @@ static const unsigned char md5_test_sum[7][16] = {
/*
* Checkup routine
*/
int mbedtls_md5_self_test(int verbose)
{
int mbedtls_md5_self_test(int verbose) {
int i, ret = 0;
unsigned char md5sum[16];

View file

@ -73,24 +73,20 @@
#if defined(MBEDTLS_MD2_C)
static int md2_starts_wrap(void *ctx)
{
static int md2_starts_wrap(void *ctx) {
return (mbedtls_md2_starts_ret((mbedtls_md2_context *) ctx));
}
static int md2_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_md2_update_ret((mbedtls_md2_context *) ctx, input, ilen));
}
static int md2_finish_wrap(void *ctx, unsigned char *output)
{
static int md2_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_md2_finish_ret((mbedtls_md2_context *) ctx, output));
}
static void *md2_ctx_alloc(void)
{
static void *md2_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_md2_context));
if (ctx != NULL)
@ -99,20 +95,17 @@ static void *md2_ctx_alloc(void)
return (ctx);
}
static void md2_ctx_free(void *ctx)
{
static void md2_ctx_free(void *ctx) {
mbedtls_md2_free((mbedtls_md2_context *) ctx);
mbedtls_free(ctx);
}
static void md2_clone_wrap(void *dst, const void *src)
{
static void md2_clone_wrap(void *dst, const void *src) {
mbedtls_md2_clone((mbedtls_md2_context *) dst,
(const mbedtls_md2_context *) src);
}
static int md2_process_wrap(void *ctx, const unsigned char *data)
{
static int md2_process_wrap(void *ctx, const unsigned char *data) {
((void) data);
return (mbedtls_internal_md2_process((mbedtls_md2_context *) ctx));
@ -137,24 +130,20 @@ const mbedtls_md_info_t mbedtls_md2_info = {
#if defined(MBEDTLS_MD4_C)
static int md4_starts_wrap(void *ctx)
{
static int md4_starts_wrap(void *ctx) {
return (mbedtls_md4_starts_ret((mbedtls_md4_context *) ctx));
}
static int md4_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_md4_update_ret((mbedtls_md4_context *) ctx, input, ilen));
}
static int md4_finish_wrap(void *ctx, unsigned char *output)
{
static int md4_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_md4_finish_ret((mbedtls_md4_context *) ctx, output));
}
static void *md4_ctx_alloc(void)
{
static void *md4_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_md4_context));
if (ctx != NULL)
@ -163,20 +152,17 @@ static void *md4_ctx_alloc(void)
return (ctx);
}
static void md4_ctx_free(void *ctx)
{
static void md4_ctx_free(void *ctx) {
mbedtls_md4_free((mbedtls_md4_context *) ctx);
mbedtls_free(ctx);
}
static void md4_clone_wrap(void *dst, const void *src)
{
static void md4_clone_wrap(void *dst, const void *src) {
mbedtls_md4_clone((mbedtls_md4_context *) dst,
(const mbedtls_md4_context *) src);
}
static int md4_process_wrap(void *ctx, const unsigned char *data)
{
static int md4_process_wrap(void *ctx, const unsigned char *data) {
return (mbedtls_internal_md4_process((mbedtls_md4_context *) ctx, data));
}
@ -199,24 +185,20 @@ const mbedtls_md_info_t mbedtls_md4_info = {
#if defined(MBEDTLS_MD5_C)
static int md5_starts_wrap(void *ctx)
{
static int md5_starts_wrap(void *ctx) {
return (mbedtls_md5_starts_ret((mbedtls_md5_context *) ctx));
}
static int md5_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_md5_update_ret((mbedtls_md5_context *) ctx, input, ilen));
}
static int md5_finish_wrap(void *ctx, unsigned char *output)
{
static int md5_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_md5_finish_ret((mbedtls_md5_context *) ctx, output));
}
static void *md5_ctx_alloc(void)
{
static void *md5_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_md5_context));
if (ctx != NULL)
@ -225,20 +207,17 @@ static void *md5_ctx_alloc(void)
return (ctx);
}
static void md5_ctx_free(void *ctx)
{
static void md5_ctx_free(void *ctx) {
mbedtls_md5_free((mbedtls_md5_context *) ctx);
mbedtls_free(ctx);
}
static void md5_clone_wrap(void *dst, const void *src)
{
static void md5_clone_wrap(void *dst, const void *src) {
mbedtls_md5_clone((mbedtls_md5_context *) dst,
(const mbedtls_md5_context *) src);
}
static int md5_process_wrap(void *ctx, const unsigned char *data)
{
static int md5_process_wrap(void *ctx, const unsigned char *data) {
return (mbedtls_internal_md5_process((mbedtls_md5_context *) ctx, data));
}
@ -261,26 +240,22 @@ const mbedtls_md_info_t mbedtls_md5_info = {
#if defined(MBEDTLS_RIPEMD160_C)
static int ripemd160_starts_wrap(void *ctx)
{
static int ripemd160_starts_wrap(void *ctx) {
return (mbedtls_ripemd160_starts_ret((mbedtls_ripemd160_context *) ctx));
}
static int ripemd160_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_ripemd160_update_ret((mbedtls_ripemd160_context *) ctx,
input, ilen));
}
static int ripemd160_finish_wrap(void *ctx, unsigned char *output)
{
static int ripemd160_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_ripemd160_finish_ret((mbedtls_ripemd160_context *) ctx,
output));
}
static void *ripemd160_ctx_alloc(void)
{
static void *ripemd160_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ripemd160_context));
if (ctx != NULL)
@ -289,20 +264,17 @@ static void *ripemd160_ctx_alloc(void)
return (ctx);
}
static void ripemd160_ctx_free(void *ctx)
{
static void ripemd160_ctx_free(void *ctx) {
mbedtls_ripemd160_free((mbedtls_ripemd160_context *) ctx);
mbedtls_free(ctx);
}
static void ripemd160_clone_wrap(void *dst, const void *src)
{
static void ripemd160_clone_wrap(void *dst, const void *src) {
mbedtls_ripemd160_clone((mbedtls_ripemd160_context *) dst,
(const mbedtls_ripemd160_context *) src);
}
static int ripemd160_process_wrap(void *ctx, const unsigned char *data)
{
static int ripemd160_process_wrap(void *ctx, const unsigned char *data) {
return (mbedtls_internal_ripemd160_process(
(mbedtls_ripemd160_context *) ctx, data));
}
@ -326,25 +298,21 @@ const mbedtls_md_info_t mbedtls_ripemd160_info = {
#if defined(MBEDTLS_SHA1_C)
static int sha1_starts_wrap(void *ctx)
{
static int sha1_starts_wrap(void *ctx) {
return (mbedtls_sha1_starts_ret((mbedtls_sha1_context *) ctx));
}
static int sha1_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_sha1_update_ret((mbedtls_sha1_context *) ctx,
input, ilen));
}
static int sha1_finish_wrap(void *ctx, unsigned char *output)
{
static int sha1_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_sha1_finish_ret((mbedtls_sha1_context *) ctx, output));
}
static void *sha1_ctx_alloc(void)
{
static void *sha1_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_sha1_context));
if (ctx != NULL)
@ -353,20 +321,17 @@ static void *sha1_ctx_alloc(void)
return (ctx);
}
static void sha1_clone_wrap(void *dst, const void *src)
{
static void sha1_clone_wrap(void *dst, const void *src) {
mbedtls_sha1_clone((mbedtls_sha1_context *) dst,
(const mbedtls_sha1_context *) src);
}
static void sha1_ctx_free(void *ctx)
{
static void sha1_ctx_free(void *ctx) {
mbedtls_sha1_free((mbedtls_sha1_context *) ctx);
mbedtls_free(ctx);
}
static int sha1_process_wrap(void *ctx, const unsigned char *data)
{
static int sha1_process_wrap(void *ctx, const unsigned char *data) {
return (mbedtls_internal_sha1_process((mbedtls_sha1_context *) ctx,
data));
}
@ -393,32 +358,27 @@ const mbedtls_md_info_t mbedtls_sha1_info = {
*/
#if defined(MBEDTLS_SHA256_C)
static int sha224_starts_wrap(void *ctx)
{
static int sha224_starts_wrap(void *ctx) {
return (mbedtls_sha256_starts_ret((mbedtls_sha256_context *) ctx, 1));
}
static int sha224_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_sha256_update_ret((mbedtls_sha256_context *) ctx,
input, ilen));
}
static int sha224_finish_wrap(void *ctx, unsigned char *output)
{
static int sha224_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_sha256_finish_ret((mbedtls_sha256_context *) ctx,
output));
}
static int sha224_wrap(const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
return (mbedtls_sha256_ret(input, ilen, output, 1));
}
static void *sha224_ctx_alloc(void)
{
static void *sha224_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_sha256_context));
if (ctx != NULL)
@ -427,20 +387,17 @@ static void *sha224_ctx_alloc(void)
return (ctx);
}
static void sha224_ctx_free(void *ctx)
{
static void sha224_ctx_free(void *ctx) {
mbedtls_sha256_free((mbedtls_sha256_context *) ctx);
mbedtls_free(ctx);
}
static void sha224_clone_wrap(void *dst, const void *src)
{
static void sha224_clone_wrap(void *dst, const void *src) {
mbedtls_sha256_clone((mbedtls_sha256_context *) dst,
(const mbedtls_sha256_context *) src);
}
static int sha224_process_wrap(void *ctx, const unsigned char *data)
{
static int sha224_process_wrap(void *ctx, const unsigned char *data) {
return (mbedtls_internal_sha256_process((mbedtls_sha256_context *) ctx,
data));
}
@ -460,14 +417,12 @@ const mbedtls_md_info_t mbedtls_sha224_info = {
sha224_process_wrap,
};
static int sha256_starts_wrap(void *ctx)
{
static int sha256_starts_wrap(void *ctx) {
return (mbedtls_sha256_starts_ret((mbedtls_sha256_context *) ctx, 0));
}
static int sha256_wrap(const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
return (mbedtls_sha256_ret(input, ilen, output, 0));
}
@ -490,32 +445,27 @@ const mbedtls_md_info_t mbedtls_sha256_info = {
#if defined(MBEDTLS_SHA512_C)
static int sha384_starts_wrap(void *ctx)
{
static int sha384_starts_wrap(void *ctx) {
return (mbedtls_sha512_starts_ret((mbedtls_sha512_context *) ctx, 1));
}
static int sha384_update_wrap(void *ctx, const unsigned char *input,
size_t ilen)
{
size_t ilen) {
return (mbedtls_sha512_update_ret((mbedtls_sha512_context *) ctx,
input, ilen));
}
static int sha384_finish_wrap(void *ctx, unsigned char *output)
{
static int sha384_finish_wrap(void *ctx, unsigned char *output) {
return (mbedtls_sha512_finish_ret((mbedtls_sha512_context *) ctx,
output));
}
static int sha384_wrap(const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
return (mbedtls_sha512_ret(input, ilen, output, 1));
}
static void *sha384_ctx_alloc(void)
{
static void *sha384_ctx_alloc(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_sha512_context));
if (ctx != NULL)
@ -524,20 +474,17 @@ static void *sha384_ctx_alloc(void)
return (ctx);
}
static void sha384_ctx_free(void *ctx)
{
static void sha384_ctx_free(void *ctx) {
mbedtls_sha512_free((mbedtls_sha512_context *) ctx);
mbedtls_free(ctx);
}
static void sha384_clone_wrap(void *dst, const void *src)
{
static void sha384_clone_wrap(void *dst, const void *src) {
mbedtls_sha512_clone((mbedtls_sha512_context *) dst,
(const mbedtls_sha512_context *) src);
}
static int sha384_process_wrap(void *ctx, const unsigned char *data)
{
static int sha384_process_wrap(void *ctx, const unsigned char *data) {
return (mbedtls_internal_sha512_process((mbedtls_sha512_context *) ctx,
data));
}
@ -557,14 +504,12 @@ const mbedtls_md_info_t mbedtls_sha384_info = {
sha384_process_wrap,
};
static int sha512_starts_wrap(void *ctx)
{
static int sha512_starts_wrap(void *ctx) {
return (mbedtls_sha512_starts_ret((mbedtls_sha512_context *) ctx, 0));
}
static int sha512_wrap(const unsigned char *input, size_t ilen,
unsigned char *output)
{
unsigned char *output) {
return (mbedtls_sha512_ret(input, ilen, output, 0));
}

View file

@ -64,7 +64,7 @@
if( p == NULL || oid == NULL ) return( NULL ); \
while( cur->asn1 != NULL ) { \
if( cur->asn1_len == oid->len && \
memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
return( p ); \
} \
p++; \
@ -705,8 +705,7 @@ FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pb
/* Return the x.y.z.... style numeric string for the given OID */
int mbedtls_oid_get_numeric_string(char *buf, size_t size,
const mbedtls_asn1_buf *oid)
{
const mbedtls_asn1_buf *oid) {
int ret;
size_t i, n;
unsigned int value;

View file

@ -48,8 +48,7 @@
#endif
#if defined(MBEDTLS_PEM_PARSE_C)
void mbedtls_pem_init(mbedtls_pem_context *ctx)
{
void mbedtls_pem_init(mbedtls_pem_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_pem_context));
}
@ -59,8 +58,7 @@ void mbedtls_pem_init(mbedtls_pem_context *ctx)
* Read a 16-byte hex string and convert it to binary
*/
static int pem_get_iv(const unsigned char *s, unsigned char *iv,
size_t iv_len)
{
size_t iv_len) {
size_t i, j, k;
memset(iv, 0, iv_len);
@ -82,8 +80,7 @@ static int pem_get_iv(const unsigned char *s, unsigned char *iv,
static int pem_pbkdf1(unsigned char *key, size_t keylen,
unsigned char *iv,
const unsigned char *pwd, size_t pwdlen)
{
const unsigned char *pwd, size_t pwdlen) {
mbedtls_md5_context md5_ctx;
unsigned char md5sum[16];
size_t use_len;
@ -143,8 +140,7 @@ exit:
*/
static int pem_des_decrypt(unsigned char des_iv[8],
unsigned char *buf, size_t buflen,
const unsigned char *pwd, size_t pwdlen)
{
const unsigned char *pwd, size_t pwdlen) {
mbedtls_des_context des_ctx;
unsigned char des_key[8];
int ret;
@ -171,8 +167,7 @@ exit:
*/
static int pem_des3_decrypt(unsigned char des3_iv[8],
unsigned char *buf, size_t buflen,
const unsigned char *pwd, size_t pwdlen)
{
const unsigned char *pwd, size_t pwdlen) {
mbedtls_des3_context des3_ctx;
unsigned char des3_key[24];
int ret;
@ -201,8 +196,7 @@ exit:
*/
static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
unsigned char *buf, size_t buflen,
const unsigned char *pwd, size_t pwdlen)
{
const unsigned char *pwd, size_t pwdlen) {
mbedtls_aes_context aes_ctx;
unsigned char aes_key[32];
int ret;
@ -230,8 +224,7 @@ exit:
int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
const unsigned char *data, const unsigned char *pwd,
size_t pwdlen, size_t *use_len)
{
size_t pwdlen, size_t *use_len) {
int ret, enc;
size_t len;
unsigned char *buf;
@ -412,8 +405,7 @@ int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const
return (0);
}
void mbedtls_pem_free(mbedtls_pem_context *ctx)
{
void mbedtls_pem_free(mbedtls_pem_context *ctx) {
if (ctx->buf != NULL)
mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
mbedtls_free(ctx->buf);
@ -426,8 +418,7 @@ void mbedtls_pem_free(mbedtls_pem_context *ctx)
#if defined(MBEDTLS_PEM_WRITE_C)
int mbedtls_pem_write_buffer(const char *header, const char *footer,
const unsigned char *der_data, size_t der_len,
unsigned char *buf, size_t buf_len, size_t *olen)
{
unsigned char *buf, size_t buf_len, size_t *olen) {
int ret;
unsigned char *encode_buf = NULL, *c, *p = buf;
size_t len = 0, use_len, add_len = 0;
@ -441,7 +432,7 @@ int mbedtls_pem_write_buffer(const char *header, const char *footer,
}
if (use_len != 0 &&
((encode_buf = mbedtls_calloc(1, use_len)) == NULL))
((encode_buf = mbedtls_calloc(1, use_len)) == NULL))
return (MBEDTLS_ERR_PEM_ALLOC_FAILED);
if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,

View file

@ -49,8 +49,7 @@
/*
* Initialise a mbedtls_pk_context
*/
void mbedtls_pk_init(mbedtls_pk_context *ctx)
{
void mbedtls_pk_init(mbedtls_pk_context *ctx) {
if (ctx == NULL)
return;
@ -61,8 +60,7 @@ void mbedtls_pk_init(mbedtls_pk_context *ctx)
/*
* Free (the components of) a mbedtls_pk_context
*/
void mbedtls_pk_free(mbedtls_pk_context *ctx)
{
void mbedtls_pk_free(mbedtls_pk_context *ctx) {
if (ctx == NULL || ctx->pk_info == NULL)
return;
@ -74,8 +72,7 @@ void mbedtls_pk_free(mbedtls_pk_context *ctx)
/*
* Get pk_info structure from type
*/
const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
{
const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type) {
switch (pk_type) {
#if defined(MBEDTLS_RSA_C)
case MBEDTLS_PK_RSA:
@ -100,8 +97,7 @@ const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
/*
* Initialise context
*/
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
{
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info) {
if (ctx == NULL || info == NULL || ctx->pk_info != NULL)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
@ -120,8 +116,7 @@ int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
mbedtls_pk_rsa_alt_sign_func sign_func,
mbedtls_pk_rsa_alt_key_len_func key_len_func)
{
mbedtls_pk_rsa_alt_key_len_func key_len_func) {
mbedtls_rsa_alt_context *rsa_alt;
const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
@ -147,8 +142,7 @@ int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
/*
* Tell if a PK can do the operations of the given type
*/
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
{
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type) {
/* null or NONE context can't do anything */
if (ctx == NULL || ctx->pk_info == NULL)
return (0);
@ -159,8 +153,7 @@ int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
/*
* Helper for mbedtls_pk_sign and mbedtls_pk_verify
*/
static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
{
static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len) {
const mbedtls_md_info_t *md_info;
if (*hash_len != 0)
@ -178,10 +171,9 @@ static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
*/
int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
const unsigned char *sig, size_t sig_len)
{
const unsigned char *sig, size_t sig_len) {
if (ctx == NULL || ctx->pk_info == NULL ||
pk_hashlen_helper(md_alg, &hash_len) != 0)
pk_hashlen_helper(md_alg, &hash_len) != 0)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
if (ctx->pk_info->verify_func == NULL)
@ -197,8 +189,7 @@ int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
const unsigned char *sig, size_t sig_len)
{
const unsigned char *sig, size_t sig_len) {
if (ctx == NULL || ctx->pk_info == NULL)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
@ -254,10 +245,9 @@ int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
unsigned char *sig, size_t *sig_len,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
if (ctx == NULL || ctx->pk_info == NULL ||
pk_hashlen_helper(md_alg, &hash_len) != 0)
pk_hashlen_helper(md_alg, &hash_len) != 0)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
if (ctx->pk_info->sign_func == NULL)
@ -273,8 +263,7 @@ int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen, size_t osize,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
if (ctx == NULL || ctx->pk_info == NULL)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
@ -291,8 +280,7 @@ int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen, size_t osize,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
if (ctx == NULL || ctx->pk_info == NULL)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
@ -306,11 +294,10 @@ int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
/*
* Check public-private key pair
*/
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
{
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv) {
if (pub == NULL || pub->pk_info == NULL ||
prv == NULL || prv->pk_info == NULL ||
prv->pk_info->check_pair_func == NULL) {
prv == NULL || prv->pk_info == NULL ||
prv->pk_info->check_pair_func == NULL) {
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
}
@ -328,8 +315,7 @@ int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_contex
/*
* Get key size in bits
*/
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
{
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx) {
if (ctx == NULL || ctx->pk_info == NULL)
return (0);
@ -339,8 +325,7 @@ size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
/*
* Export debug information
*/
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
{
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items) {
if (ctx == NULL || ctx->pk_info == NULL)
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
@ -354,8 +339,7 @@ int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items
/*
* Access the PK type name
*/
const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
{
const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx) {
if (ctx == NULL || ctx->pk_info == NULL)
return ("invalid PK");
@ -365,8 +349,7 @@ const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
/*
* Access the PK type
*/
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
{
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx) {
if (ctx == NULL || ctx->pk_info == NULL)
return (MBEDTLS_PK_NONE);

View file

@ -136,8 +136,7 @@ typedef struct mbedtls_pk_context {
* \warning You must make sure the PK context actually holds an RSA context
* before using this function!
*/
static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
{
static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk) {
return ((mbedtls_rsa_context *)(pk).pk_ctx);
}
#endif /* MBEDTLS_RSA_C */
@ -149,8 +148,7 @@ static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
* \warning You must make sure the PK context actually holds an EC context
* before using this function!
*/
static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
{
static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk) {
return ((mbedtls_ecp_keypair *)(pk).pk_ctx);
}
#endif /* MBEDTLS_ECP_C */
@ -240,8 +238,7 @@ size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
*
* \return Key length in bytes, or 0 on error
*/
static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
{
static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx) {
return ((mbedtls_pk_get_bitlen(ctx) + 7) / 8);
}

View file

@ -59,22 +59,19 @@
#include <stdint.h>
#if defined(MBEDTLS_RSA_C)
static int rsa_can_do(mbedtls_pk_type_t type)
{
static int rsa_can_do(mbedtls_pk_type_t type) {
return (type == MBEDTLS_PK_RSA ||
type == MBEDTLS_PK_RSASSA_PSS);
}
static size_t rsa_get_bitlen(const void *ctx)
{
static size_t rsa_get_bitlen(const void *ctx) {
const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
return (8 * mbedtls_rsa_get_len(rsa));
}
static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
const unsigned char *sig, size_t sig_len)
{
const unsigned char *sig, size_t sig_len) {
int ret;
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
size_t rsa_len = mbedtls_rsa_get_len(rsa);
@ -106,8 +103,7 @@ static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
unsigned char *sig, size_t *sig_len,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
#if SIZE_MAX > UINT_MAX
@ -124,8 +120,7 @@ static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
static int rsa_decrypt_wrap(void *ctx,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen, size_t osize,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
if (ilen != mbedtls_rsa_get_len(rsa))
@ -138,8 +133,7 @@ static int rsa_decrypt_wrap(void *ctx,
static int rsa_encrypt_wrap(void *ctx,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen, size_t osize,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
*olen = mbedtls_rsa_get_len(rsa);
@ -150,14 +144,12 @@ static int rsa_encrypt_wrap(void *ctx,
ilen, input, output));
}
static int rsa_check_pair_wrap(const void *pub, const void *prv)
{
static int rsa_check_pair_wrap(const void *pub, const void *prv) {
return (mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
(const mbedtls_rsa_context *) prv));
}
static void *rsa_alloc_wrap(void)
{
static void *rsa_alloc_wrap(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
if (ctx != NULL)
@ -166,14 +158,12 @@ static void *rsa_alloc_wrap(void)
return (ctx);
}
static void rsa_free_wrap(void *ctx)
{
static void rsa_free_wrap(void *ctx) {
mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
mbedtls_free(ctx);
}
static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
{
static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items) {
items->type = MBEDTLS_PK_DEBUG_MPI;
items->name = "rsa.N";
items->value = &(((mbedtls_rsa_context *) ctx)->N);
@ -205,15 +195,13 @@ const mbedtls_pk_info_t mbedtls_rsa_info = {
/*
* Generic EC key
*/
static int eckey_can_do(mbedtls_pk_type_t type)
{
static int eckey_can_do(mbedtls_pk_type_t type) {
return (type == MBEDTLS_PK_ECKEY ||
type == MBEDTLS_PK_ECKEY_DH ||
type == MBEDTLS_PK_ECDSA);
}
static size_t eckey_get_bitlen(const void *ctx)
{
static size_t eckey_get_bitlen(const void *ctx) {
return (((mbedtls_ecp_keypair *) ctx)->grp.pbits);
}
@ -230,8 +218,7 @@ static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
const unsigned char *sig, size_t sig_len)
{
const unsigned char *sig, size_t sig_len) {
int ret;
mbedtls_ecdsa_context ecdsa;
@ -248,8 +235,7 @@ static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
unsigned char *sig, size_t *sig_len,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret;
mbedtls_ecdsa_context ecdsa;
@ -266,14 +252,12 @@ static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
#endif /* MBEDTLS_ECDSA_C */
static int eckey_check_pair(const void *pub, const void *prv)
{
static int eckey_check_pair(const void *pub, const void *prv) {
return (mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
(const mbedtls_ecp_keypair *) prv));
}
static void *eckey_alloc_wrap(void)
{
static void *eckey_alloc_wrap(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
if (ctx != NULL)
@ -282,14 +266,12 @@ static void *eckey_alloc_wrap(void)
return (ctx);
}
static void eckey_free_wrap(void *ctx)
{
static void eckey_free_wrap(void *ctx) {
mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
mbedtls_free(ctx);
}
static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
{
static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items) {
items->type = MBEDTLS_PK_DEBUG_ECP;
items->name = "eckey.Q";
items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
@ -318,8 +300,7 @@ const mbedtls_pk_info_t mbedtls_eckey_info = {
/*
* EC key restricted to ECDH
*/
static int eckeydh_can_do(mbedtls_pk_type_t type)
{
static int eckeydh_can_do(mbedtls_pk_type_t type) {
return (type == MBEDTLS_PK_ECKEY ||
type == MBEDTLS_PK_ECKEY_DH);
}
@ -341,15 +322,13 @@ const mbedtls_pk_info_t mbedtls_eckeydh_info = {
#endif /* MBEDTLS_ECP_C */
#if defined(MBEDTLS_ECDSA_C)
static int ecdsa_can_do(mbedtls_pk_type_t type)
{
static int ecdsa_can_do(mbedtls_pk_type_t type) {
return (type == MBEDTLS_PK_ECDSA);
}
static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
const unsigned char *sig, size_t sig_len)
{
const unsigned char *sig, size_t sig_len) {
int ret;
((void) md_alg);
@ -365,14 +344,12 @@ static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
unsigned char *sig, size_t *sig_len,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
return (mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng));
}
static void *ecdsa_alloc_wrap(void)
{
static void *ecdsa_alloc_wrap(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_context));
if (ctx != NULL)
@ -381,8 +358,7 @@ static void *ecdsa_alloc_wrap(void)
return (ctx);
}
static void ecdsa_free_wrap(void *ctx)
{
static void ecdsa_free_wrap(void *ctx) {
mbedtls_ecdsa_free((mbedtls_ecdsa_context *) ctx);
mbedtls_free(ctx);
}
@ -408,13 +384,11 @@ const mbedtls_pk_info_t mbedtls_ecdsa_info = {
* Support for alternative RSA-private implementations
*/
static int rsa_alt_can_do(mbedtls_pk_type_t type)
{
static int rsa_alt_can_do(mbedtls_pk_type_t type) {
return (type == MBEDTLS_PK_RSA);
}
static size_t rsa_alt_get_bitlen(const void *ctx)
{
static size_t rsa_alt_get_bitlen(const void *ctx) {
const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
return (8 * rsa_alt->key_len_func(rsa_alt->key));
@ -423,8 +397,7 @@ static size_t rsa_alt_get_bitlen(const void *ctx)
static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
const unsigned char *hash, size_t hash_len,
unsigned char *sig, size_t *sig_len,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
#if SIZE_MAX > UINT_MAX
@ -441,8 +414,7 @@ static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
static int rsa_alt_decrypt_wrap(void *ctx,
const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen, size_t osize,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
((void) f_rng);
@ -456,8 +428,7 @@ static int rsa_alt_decrypt_wrap(void *ctx,
}
#if defined(MBEDTLS_RSA_C)
static int rsa_alt_check_pair(const void *pub, const void *prv)
{
static int rsa_alt_check_pair(const void *pub, const void *prv) {
unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
unsigned char hash[32];
size_t sig_len = 0;
@ -483,8 +454,7 @@ static int rsa_alt_check_pair(const void *pub, const void *prv)
}
#endif /* MBEDTLS_RSA_C */
static void *rsa_alt_alloc_wrap(void)
{
static void *rsa_alt_alloc_wrap(void) {
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
if (ctx != NULL)
@ -493,8 +463,7 @@ static void *rsa_alt_alloc_wrap(void)
return (ctx);
}
static void rsa_alt_free_wrap(void *ctx)
{
static void rsa_alt_free_wrap(void *ctx) {
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
mbedtls_free(ctx);
}

View file

@ -51,8 +51,7 @@
#endif
static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
mbedtls_asn1_buf *salt, int *iterations)
{
mbedtls_asn1_buf *salt, int *iterations) {
int ret;
unsigned char **p = &params->p;
const unsigned char *end = params->p + params->len;
@ -89,8 +88,7 @@ static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
const unsigned char *pwd, size_t pwdlen,
unsigned char *key, size_t keylen,
unsigned char *iv, size_t ivlen)
{
unsigned char *iv, size_t ivlen) {
int ret, iterations = 0;
mbedtls_asn1_buf salt;
size_t i;
@ -131,8 +129,7 @@ static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_typ
int mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf *pbe_params, int mode,
const unsigned char *pwd, size_t pwdlen,
const unsigned char *data, size_t len,
unsigned char *output)
{
unsigned char *output) {
#if !defined(MBEDTLS_ARC4_C)
((void) pbe_params);
((void) mode);
@ -172,8 +169,7 @@ int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
const unsigned char *pwd, size_t pwdlen,
const unsigned char *data, size_t len,
unsigned char *output)
{
unsigned char *output) {
int ret, keylen = 0;
unsigned char key[32];
unsigned char iv[16];
@ -224,8 +220,7 @@ exit:
}
static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
const unsigned char *filler, size_t fill_len)
{
const unsigned char *filler, size_t fill_len) {
unsigned char *p = data;
size_t use_len;
@ -240,8 +235,7 @@ static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
const unsigned char *pwd, size_t pwdlen,
const unsigned char *salt, size_t saltlen,
mbedtls_md_type_t md_type, int id, int iterations)
{
mbedtls_md_type_t md_type, int id, int iterations) {
int ret;
unsigned int j;

View file

@ -60,8 +60,7 @@
int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
const unsigned char *pwd, size_t pwdlen,
const unsigned char *data, size_t datalen,
unsigned char *output)
{
unsigned char *output) {
((void) pbe_params);
((void) mode);
((void) pwd);
@ -74,8 +73,7 @@ int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
#else
static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
mbedtls_asn1_buf *salt, int *iterations,
int *keylen, mbedtls_md_type_t *md_type)
{
int *keylen, mbedtls_md_type_t *md_type) {
int ret;
mbedtls_asn1_buf prf_alg_oid;
unsigned char *p = params->p;
@ -129,8 +127,7 @@ static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
const unsigned char *pwd, size_t pwdlen,
const unsigned char *data, size_t datalen,
unsigned char *output)
{
unsigned char *output) {
int ret, iterations = 0, keylen = 0;
unsigned char *p, *end;
mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
@ -194,7 +191,7 @@ int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
keylen = cipher_info->key_bitlen / 8;
if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
enc_scheme_params.len != cipher_info->iv_size) {
enc_scheme_params.len != cipher_info->iv_size) {
return (MBEDTLS_ERR_PKCS5_INVALID_FORMAT);
}
@ -232,8 +229,7 @@ exit:
int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output)
{
uint32_t key_length, unsigned char *output) {
int ret, j;
unsigned int i;
unsigned char md1[MBEDTLS_MD_MAX_SIZE];
@ -303,8 +299,7 @@ int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, const unsigned char *pa
#if defined(MBEDTLS_SELF_TEST)
#if !defined(MBEDTLS_SHA1_C)
int mbedtls_pkcs5_self_test(int verbose)
{
int mbedtls_pkcs5_self_test(int verbose) {
if (verbose != 0)
mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n");
@ -370,8 +365,7 @@ static const unsigned char result_key[MAX_TESTS][32] = {
},
};
int mbedtls_pkcs5_self_test(int verbose)
{
int mbedtls_pkcs5_self_test(int verbose) {
mbedtls_md_context_t sha1_ctx;
const mbedtls_md_info_t *info_sha1;
int ret, i;
@ -397,7 +391,7 @@ int mbedtls_pkcs5_self_test(int verbose)
ret = mbedtls_pkcs5_pbkdf2_hmac(&sha1_ctx, password[i], plen[i], salt[i],
slen[i], it_cnt[i], key_len[i], key);
if (ret != 0 ||
memcmp(result_key[i], key, key_len[i]) != 0) {
memcmp(result_key[i], key, key_len[i]) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");

View file

@ -71,8 +71,7 @@
* A terminating null byte is always appended. It is included in the announced
* length only if the data looks like it is PEM encoded.
*/
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
{
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n) {
FILE *f;
long size;
@ -89,7 +88,7 @@ int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
*n = (size_t) size;
if (*n + 1 == 0 ||
(*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
(*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
fclose(f);
return (MBEDTLS_ERR_PK_ALLOC_FAILED);
}
@ -117,8 +116,7 @@ int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
* Load and parse a private key
*/
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
const char *path, const char *pwd)
{
const char *path, const char *pwd) {
int ret;
size_t n;
unsigned char *buf;
@ -141,8 +139,7 @@ int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
/*
* Load and parse a public key
*/
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
{
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path) {
int ret;
size_t n;
unsigned char *buf;
@ -169,8 +166,7 @@ int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
* }
*/
static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
mbedtls_asn1_buf *params)
{
mbedtls_asn1_buf *params) {
int ret;
if (end - *p < 1)
@ -181,7 +177,7 @@ static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
params->tag = **p;
if (params->tag != MBEDTLS_ASN1_OID
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
&& params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
&& params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
#endif
) {
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
@ -222,8 +218,7 @@ static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
*
* We only support prime-field as field type, and ignore hash and cofactor.
*/
static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
{
static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) {
int ret;
unsigned char *p = params->p;
const unsigned char *const end = params->p + params->len;
@ -262,7 +257,7 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
return (ret);
if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
}
@ -298,14 +293,14 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
* containing an integer in the case of a prime field
*/
if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
(ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
(ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret);
}
p += len;
if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
(ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
(ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret);
}
@ -332,11 +327,11 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
* reading only the X coordinate and the parity bit of Y.
*/
if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
(p[0] != 0x02 && p[0] != 0x03) ||
len != mbedtls_mpi_size(&grp->P) + 1 ||
mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
(p[0] != 0x02 && p[0] != 0x03) ||
len != mbedtls_mpi_size(&grp->P) + 1 ||
mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT);
}
}
@ -362,8 +357,7 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
* Find the group id associated with an (almost filled) group as generated by
* pk_group_from_specified(), or return an error if unknown.
*/
static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
{
static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id) {
int ret = 0;
mbedtls_ecp_group ref;
const mbedtls_ecp_group_id *id;
@ -377,14 +371,14 @@ static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_grou
/* Compare to the group we were given, starting with easy tests */
if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
/* For Y we may only know the parity bit, so compare only that */
mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
/* For Y we may only know the parity bit, so compare only that */
mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
break;
}
@ -405,8 +399,7 @@ cleanup:
* Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
*/
static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
mbedtls_ecp_group_id *grp_id)
{
mbedtls_ecp_group_id *grp_id) {
int ret;
mbedtls_ecp_group grp;
@ -432,8 +425,7 @@ cleanup:
* specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
* -- implicitCurve NULL
*/
static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
{
static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) {
int ret;
mbedtls_ecp_group_id grp_id;
@ -469,8 +461,7 @@ static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *gr
* return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
*/
static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
mbedtls_ecp_keypair *key)
{
mbedtls_ecp_keypair *key) {
int ret;
if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
@ -496,8 +487,7 @@ static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
*/
static int pk_get_rsapubkey(unsigned char **p,
const unsigned char *end,
mbedtls_rsa_context *rsa)
{
mbedtls_rsa_context *rsa) {
int ret;
size_t len;
@ -530,7 +520,7 @@ static int pk_get_rsapubkey(unsigned char **p,
*p += len;
if (mbedtls_rsa_complete(rsa) != 0 ||
mbedtls_rsa_check_pubkey(rsa) != 0) {
mbedtls_rsa_check_pubkey(rsa) != 0) {
return (MBEDTLS_ERR_PK_INVALID_PUBKEY);
}
@ -550,8 +540,7 @@ static int pk_get_rsapubkey(unsigned char **p,
*/
static int pk_get_pk_alg(unsigned char **p,
const unsigned char *end,
mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params)
{
mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params) {
int ret;
mbedtls_asn1_buf alg_oid;
@ -567,8 +556,8 @@ static int pk_get_pk_alg(unsigned char **p,
* No parameters with RSA (only for EC)
*/
if (*pk_alg == MBEDTLS_PK_RSA &&
((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
params->len != 0)) {
((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
params->len != 0)) {
return (MBEDTLS_ERR_PK_INVALID_ALG);
}
@ -581,8 +570,7 @@ static int pk_get_pk_alg(unsigned char **p,
* subjectPublicKey BIT STRING }
*/
int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
mbedtls_pk_context *pk)
{
mbedtls_pk_context *pk) {
int ret;
size_t len;
mbedtls_asn1_buf alg_params;
@ -642,8 +630,7 @@ int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
*/
static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
const unsigned char *key,
size_t keylen)
{
size_t keylen) {
int ret, version;
size_t len;
unsigned char *p, *end;
@ -688,40 +675,40 @@ static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
/* Import N */
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_INTEGER)) != 0 ||
(ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
NULL, 0, NULL, 0)) != 0)
(ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
NULL, 0, NULL, 0)) != 0)
goto cleanup;
p += len;
/* Import E */
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_INTEGER)) != 0 ||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
NULL, 0, p, len)) != 0)
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
NULL, 0, p, len)) != 0)
goto cleanup;
p += len;
/* Import D */
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_INTEGER)) != 0 ||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
p, len, NULL, 0)) != 0)
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
p, len, NULL, 0)) != 0)
goto cleanup;
p += len;
/* Import P */
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_INTEGER)) != 0 ||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, p, len, NULL, 0,
NULL, 0, NULL, 0)) != 0)
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, p, len, NULL, 0,
NULL, 0, NULL, 0)) != 0)
goto cleanup;
p += len;
/* Import Q */
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_INTEGER)) != 0 ||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, p, len,
NULL, 0, NULL, 0)) != 0)
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, p, len,
NULL, 0, NULL, 0)) != 0)
goto cleanup;
p += len;
@ -731,8 +718,8 @@ static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
/* Check optional parameters */
if ((ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0 ||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0 ||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0)
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0 ||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0)
goto cleanup;
if (p != end) {
@ -764,8 +751,7 @@ cleanup:
*/
static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
const unsigned char *key,
size_t keylen)
{
size_t keylen) {
int ret;
int version, pubkey_done;
size_t len;
@ -815,7 +801,7 @@ static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0)) == 0) {
if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
(ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
(ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
mbedtls_ecp_keypair_free(eck);
return (ret);
}
@ -858,8 +844,8 @@ static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
}
if (! pubkey_done &&
(ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
NULL, NULL)) != 0) {
(ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
NULL, NULL)) != 0) {
mbedtls_ecp_keypair_free(eck);
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret);
}
@ -889,8 +875,7 @@ static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
static int pk_parse_key_pkcs8_unencrypted_der(
mbedtls_pk_context *pk,
const unsigned char *key,
size_t keylen)
{
size_t keylen) {
int ret, version;
size_t len;
mbedtls_asn1_buf params;
@ -955,7 +940,7 @@ static int pk_parse_key_pkcs8_unencrypted_der(
#if defined(MBEDTLS_ECP_C)
if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len)) != 0) {
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len)) != 0) {
mbedtls_pk_free(pk);
return (ret);
}
@ -979,8 +964,7 @@ static int pk_parse_key_pkcs8_unencrypted_der(
static int pk_parse_key_pkcs8_encrypted_der(
mbedtls_pk_context *pk,
unsigned char *key, size_t keylen,
const unsigned char *pwd, size_t pwdlen)
{
const unsigned char *pwd, size_t pwdlen) {
int ret, decrypted = 0;
size_t len;
unsigned char *buf;
@ -1088,8 +1072,7 @@ static int pk_parse_key_pkcs8_encrypted_der(
*/
int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
const unsigned char *key, size_t keylen,
const unsigned char *pwd, size_t pwdlen)
{
const unsigned char *pwd, size_t pwdlen) {
int ret;
const mbedtls_pk_info_t *pk_info;
@ -1112,8 +1095,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
if (ret == 0) {
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
pem.buf, pem.buflen)) != 0) {
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
pem.buf, pem.buflen)) != 0) {
mbedtls_pk_free(pk);
}
@ -1140,8 +1123,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
pem.buf, pem.buflen)) != 0) {
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
pem.buf, pem.buflen)) != 0) {
mbedtls_pk_free(pk);
}
@ -1245,8 +1228,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
key, keylen)) != 0) {
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
key, keylen)) != 0) {
mbedtls_pk_free(pk);
} else {
return (0);
@ -1258,8 +1241,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
key, keylen)) != 0) {
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
key, keylen)) != 0) {
mbedtls_pk_free(pk);
} else {
return (0);
@ -1274,8 +1257,7 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
* Parse a public key
*/
int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
const unsigned char *key, size_t keylen)
{
const unsigned char *key, size_t keylen) {
int ret;
unsigned char *p;
#if defined(MBEDTLS_RSA_C)

View file

@ -64,8 +64,7 @@
* }
*/
static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
mbedtls_rsa_context *rsa)
{
mbedtls_rsa_context *rsa) {
int ret;
size_t len = 0;
mbedtls_mpi T;
@ -74,13 +73,13 @@ static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
/* Export E */
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
goto end_of_export;
len += ret;
/* Export N */
if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
goto end_of_export;
len += ret;
@ -103,8 +102,7 @@ end_of_export:
* EC public key is an EC point
*/
static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
mbedtls_ecp_keypair *ec)
{
mbedtls_ecp_keypair *ec) {
int ret;
size_t len = 0;
unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
@ -130,8 +128,7 @@ static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
* }
*/
static int pk_write_ec_param(unsigned char **p, unsigned char *start,
mbedtls_ecp_keypair *ec)
{
mbedtls_ecp_keypair *ec) {
int ret;
size_t len = 0;
const char *oid;
@ -147,8 +144,7 @@ static int pk_write_ec_param(unsigned char **p, unsigned char *start,
#endif /* MBEDTLS_ECP_C */
int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
const mbedtls_pk_context *key)
{
const mbedtls_pk_context *key) {
int ret;
size_t len = 0;
@ -167,8 +163,7 @@ int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
return ((int) len);
}
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size)
{
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
int ret;
unsigned char *c;
size_t len = 0, par_len = 0, oid_len;
@ -213,8 +208,7 @@ int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, siz
return ((int) len);
}
int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t size)
{
int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
int ret;
unsigned char *c = buf + size;
size_t len = 0;
@ -232,54 +226,54 @@ int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t
/* Export QP */
if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export DQ */
if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export DP */
if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export Q */
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
&T, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export P */
if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
NULL, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export D */
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
NULL, &T, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export E */
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
NULL, NULL, &T)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
/* Export N */
if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
NULL, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
goto end_of_export;
len += ret;
@ -446,8 +440,7 @@ end_of_export:
#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size)
{
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
int ret;
unsigned char output_buf[PUB_DER_MAX_BYTES];
size_t olen = 0;
@ -466,8 +459,7 @@ int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, siz
return (0);
}
int mbedtls_pk_write_key_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size)
{
int mbedtls_pk_write_key_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
int ret;
unsigned char output_buf[PRV_DER_MAX_BYTES];
const char *begin, *end;

View file

@ -34,8 +34,7 @@
#if defined(MBEDTLS_PLATFORM_MEMORY)
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
static void *platform_calloc_uninit(size_t n, size_t size)
{
static void *platform_calloc_uninit(size_t n, size_t size) {
((void) n);
((void) size);
return (NULL);
@ -45,8 +44,7 @@ static void *platform_calloc_uninit(size_t n, size_t size)
#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
static void platform_free_uninit(void *ptr)
{
static void platform_free_uninit(void *ptr) {
((void) ptr);
}
@ -56,19 +54,16 @@ static void platform_free_uninit(void *ptr)
static void *(*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
static void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
void *mbedtls_calloc(size_t nmemb, size_t size)
{
void *mbedtls_calloc(size_t nmemb, size_t size) {
return (*mbedtls_calloc_func)(nmemb, size);
}
void mbedtls_free(void *ptr)
{
void mbedtls_free(void *ptr) {
(*mbedtls_free_func)(ptr);
}
int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
void (*free_func)(void *))
{
void (*free_func)(void *)) {
mbedtls_calloc_func = calloc_func;
mbedtls_free_func = free_func;
return (0);
@ -77,8 +72,7 @@ int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
#if defined(_WIN32)
#include <stdarg.h>
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
{
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...) {
int ret;
va_list argp;
@ -108,8 +102,7 @@ int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
* Make dummy function to prevent NULL pointer dereferences
*/
static int platform_snprintf_uninit(char *s, size_t n,
const char *format, ...)
{
const char *format, ...) {
((void) s);
((void) n);
((void) format);
@ -125,8 +118,7 @@ int (*mbedtls_snprintf)(char *s, size_t n,
int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
const char *format,
...))
{
...)) {
mbedtls_snprintf = snprintf_func;
return (0);
}
@ -137,8 +129,7 @@ int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
/*
* Make dummy function to prevent NULL pointer dereferences
*/
static int platform_printf_uninit(const char *format, ...)
{
static int platform_printf_uninit(const char *format, ...) {
((void) format);
return (0);
}
@ -148,8 +139,7 @@ static int platform_printf_uninit(const char *format, ...)
int (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
{
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...)) {
mbedtls_printf = printf_func;
return (0);
}
@ -160,8 +150,7 @@ int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
/*
* Make dummy function to prevent NULL pointer dereferences
*/
static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
{
static int platform_fprintf_uninit(FILE *stream, const char *format, ...) {
((void) stream);
((void) format);
return (0);
@ -173,8 +162,7 @@ static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
int (*mbedtls_fprintf)(FILE *, const char *, ...) =
MBEDTLS_PLATFORM_STD_FPRINTF;
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
{
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...)) {
mbedtls_fprintf = fprintf_func;
return (0);
}
@ -185,8 +173,7 @@ int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
/*
* Make dummy function to prevent NULL pointer dereferences
*/
static void platform_exit_uninit(int status)
{
static void platform_exit_uninit(int status) {
((void) status);
}
@ -195,8 +182,7 @@ static void platform_exit_uninit(int status)
void (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
int mbedtls_platform_set_exit(void (*exit_func)(int status))
{
int mbedtls_platform_set_exit(void (*exit_func)(int status)) {
mbedtls_exit = exit_func;
return (0);
}
@ -209,8 +195,7 @@ int mbedtls_platform_set_exit(void (*exit_func)(int status))
/*
* Make dummy function to prevent NULL pointer dereferences
*/
static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
{
static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer) {
((void) timer);
return (0);
}
@ -220,8 +205,7 @@ static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
{
int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer)) {
mbedtls_time = time_func;
return (0);
}
@ -234,8 +218,7 @@ int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer)
/* Default implementations for the platform independent seed functions use
* standard libc file functions to read from and write to a pre-defined filename
*/
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
{
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len) {
FILE *file;
size_t n;
@ -252,8 +235,7 @@ int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
return ((int)n);
}
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
{
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len) {
FILE *file;
size_t n;
@ -275,8 +257,7 @@ int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
{
static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len) {
((void) buf);
((void) buf_len);
return (-1);
@ -289,8 +270,7 @@ static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
{
static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len) {
((void) buf);
((void) buf_len);
return (-1);
@ -306,8 +286,7 @@ int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
int mbedtls_platform_set_nv_seed(
int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
{
int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)) {
mbedtls_nv_seed_read = nv_seed_read_func;
mbedtls_nv_seed_write = nv_seed_write_func;
return (0);
@ -319,8 +298,7 @@ int mbedtls_platform_set_nv_seed(
/*
* Placeholder platform setup that does nothing by default
*/
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
{
int mbedtls_platform_setup(mbedtls_platform_context *ctx) {
(void)ctx;
return (0);
@ -329,8 +307,7 @@ int mbedtls_platform_setup(mbedtls_platform_context *ctx)
/*
* Placeholder platform teardown that does nothing by default
*/
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
{
void mbedtls_platform_teardown(mbedtls_platform_context *ctx) {
(void)ctx;
}
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */

View file

@ -62,8 +62,7 @@
*/
static void *(* const volatile memset_func)(void *, int, size_t) = memset;
void mbedtls_platform_zeroize(void *buf, size_t len)
{
void mbedtls_platform_zeroize(void *buf, size_t len) {
memset_func(buf, 0, len);
}
#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */

View file

@ -75,8 +75,7 @@
#if defined(MBEDTLS_PKCS1_V15)
/* constant-time buffer comparison */
static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n)
{
static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n) {
size_t i;
const unsigned char *A = (const unsigned char *) a;
const unsigned char *B = (const unsigned char *) b;
@ -92,15 +91,14 @@ static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n)
int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
const mbedtls_mpi *N,
const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, const mbedtls_mpi *E)
{
const mbedtls_mpi *D, const mbedtls_mpi *E) {
int ret;
if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
(P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
(Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
(D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
(E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
(P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
(Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
(D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
(E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret);
}
@ -115,8 +113,7 @@ int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
unsigned char const *P, size_t P_len,
unsigned char const *Q, size_t Q_len,
unsigned char const *D, size_t D_len,
unsigned char const *E, size_t E_len)
{
unsigned char const *E, size_t E_len) {
int ret = 0;
if (N != NULL) {
@ -150,8 +147,7 @@ cleanup:
* It does *not* make guarantees for consistency of the parameters.
*/
static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
int blinding_needed)
{
int blinding_needed) {
#if !defined(MBEDTLS_RSA_NO_CRT)
/* blinding_needed is only used for NO_CRT to decide whether
* P,Q need to be present or not. */
@ -159,7 +155,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
#endif
if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
ctx->len > MBEDTLS_MPI_MAX_SIZE) {
ctx->len > MBEDTLS_MPI_MAX_SIZE) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
}
@ -170,7 +166,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
/* Modular exponentiation wrt. N is always used for
* RSA public key operations. */
if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
}
@ -179,10 +175,10 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
* used for private key operations and if CRT
* is used. */
if (is_priv &&
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
}
#endif /* !MBEDTLS_RSA_NO_CRT */
@ -202,8 +198,8 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
#else
if (is_priv &&
(mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
(mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
}
#endif /* MBEDTLS_RSA_NO_CRT */
@ -213,8 +209,8 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
* done as part of 1. */
#if defined(MBEDTLS_RSA_NO_CRT)
if (is_priv && blinding_needed &&
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
}
#endif
@ -223,7 +219,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
* but check for QP >= 1 nonetheless. */
#if !defined(MBEDTLS_RSA_NO_CRT)
if (is_priv &&
mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
}
#endif
@ -231,8 +227,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
return (0);
}
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
{
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx) {
int ret = 0;
const int have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
@ -320,8 +315,7 @@ int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
unsigned char *P, size_t P_len,
unsigned char *Q, size_t Q_len,
unsigned char *D, size_t D_len,
unsigned char *E, size_t E_len)
{
unsigned char *E, size_t E_len) {
int ret = 0;
/* Check if key is private or public */
@ -362,8 +356,7 @@ cleanup:
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
mbedtls_mpi *D, mbedtls_mpi *E)
{
mbedtls_mpi *D, mbedtls_mpi *E) {
int ret;
/* Check if key is private or public */
@ -385,10 +378,10 @@ int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
/* Export all requested core parameters. */
if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
(P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
(Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
(D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
(E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
(P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
(Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
(D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
(E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
return (ret);
}
@ -402,8 +395,7 @@ int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
* can be used in this case.
*/
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
{
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP) {
int ret;
/* Check if key is private or public */
@ -420,8 +412,8 @@ int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
#if !defined(MBEDTLS_RSA_NO_CRT)
/* Export all requested blinding parameters. */
if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
(DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
(QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
(DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
(QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret);
}
#else
@ -439,8 +431,7 @@ int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
*/
void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
int padding,
int hash_id)
{
int hash_id) {
memset(ctx, 0, sizeof(mbedtls_rsa_context));
mbedtls_rsa_set_padding(ctx, padding, hash_id);
@ -453,8 +444,7 @@ void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
/*
* Set padding for an existing RSA context
*/
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
{
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id) {
ctx->padding = padding;
ctx->hash_id = hash_id;
}
@ -463,8 +453,7 @@ void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
* Get length in bytes of RSA modulus
*/
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
{
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx) {
return (ctx->len);
}
@ -480,8 +469,7 @@ size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
unsigned int nbits, int exponent)
{
unsigned int nbits, int exponent) {
int ret;
mbedtls_mpi H, G, L;
@ -580,8 +568,7 @@ cleanup:
/*
* Check a public RSA key
*/
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
{
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx) {
if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0)
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
@ -590,8 +577,8 @@ int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
}
if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
mbedtls_mpi_bitlen(&ctx->E) < 2 ||
mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
mbedtls_mpi_bitlen(&ctx->E) < 2 ||
mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
}
@ -601,10 +588,9 @@ int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
/*
* Check for the consistency of all fields in an RSA private key context
*/
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
{
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx) {
if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
}
@ -627,15 +613,14 @@ int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
* Check if contexts holding a public and private key match
*/
int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
const mbedtls_rsa_context *prv)
{
const mbedtls_rsa_context *prv) {
if (mbedtls_rsa_check_pubkey(pub) != 0 ||
mbedtls_rsa_check_privkey(prv) != 0) {
mbedtls_rsa_check_privkey(prv) != 0) {
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
}
if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
}
@ -647,8 +632,7 @@ int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
*/
int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int ret;
size_t olen;
mbedtls_mpi T;
@ -695,8 +679,7 @@ cleanup:
* Berlin Heidelberg, 1996. p. 104-113.
*/
static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
int ret, count = 0;
if (ctx->Vf.p != NULL) {
@ -755,8 +738,7 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
int ret;
size_t olen;
@ -980,8 +962,7 @@ cleanup:
* \param md_ctx message digest context to use
*/
static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
size_t slen, mbedtls_md_context_t *md_ctx)
{
size_t slen, mbedtls_md_context_t *md_ctx) {
unsigned char mask[MBEDTLS_MD_MAX_SIZE];
unsigned char counter[4];
unsigned char *p;
@ -1037,8 +1018,7 @@ int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
const unsigned char *label, size_t label_len,
size_t ilen,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
size_t olen;
int ret;
unsigned char *p = output;
@ -1116,8 +1096,7 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
void *p_rng,
int mode, size_t ilen,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
size_t nb_pad, olen;
int ret;
unsigned char *p = output;
@ -1178,8 +1157,7 @@ int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
void *p_rng,
int mode, size_t ilen,
const unsigned char *input,
unsigned char *output)
{
unsigned char *output) {
switch (ctx->padding) {
#if defined(MBEDTLS_PKCS1_V15)
case MBEDTLS_RSA_PKCS_V15:
@ -1210,8 +1188,7 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
size_t *olen,
const unsigned char *input,
unsigned char *output,
size_t output_max_len)
{
size_t output_max_len) {
int ret;
size_t ilen, i, pad_len;
unsigned char *p, bad, pad_done;
@ -1264,9 +1241,9 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
/* seed: Apply seedMask to maskedSeed */
if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
&md_ctx)) != 0 ||
/* DB: Apply dbMask to maskedDB */
(ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
&md_ctx)) != 0) {
/* DB: Apply dbMask to maskedDB */
(ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
&md_ctx)) != 0) {
mbedtls_md_free(&md_ctx);
goto cleanup;
}
@ -1341,8 +1318,7 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
int mode, size_t *olen,
const unsigned char *input,
unsigned char *output,
size_t output_max_len)
{
size_t output_max_len) {
int ret;
size_t ilen, pad_count = 0, i;
unsigned char *p, bad, pad_done = 0;
@ -1430,8 +1406,7 @@ int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
int mode, size_t *olen,
const unsigned char *input,
unsigned char *output,
size_t output_max_len)
{
size_t output_max_len) {
switch (ctx->padding) {
#if defined(MBEDTLS_PKCS1_V15)
case MBEDTLS_RSA_PKCS_V15:
@ -1462,8 +1437,7 @@ int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig)
{
unsigned char *sig) {
size_t olen;
unsigned char *p = sig;
unsigned char salt[MBEDTLS_MD_MAX_SIZE];
@ -1585,8 +1559,7 @@ static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
size_t dst_len,
unsigned char *dst)
{
unsigned char *dst) {
size_t oid_size = 0;
size_t nb_pad = dst_len;
unsigned char *p = dst;
@ -1606,8 +1579,8 @@ static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
/* Double-check that 8 + hashlen + oid_size can be used as a
* 1-byte ASN.1 length encoding and that there's no overflow. */
if (8 + hashlen + oid_size >= 0x80 ||
10 + hashlen < hashlen ||
10 + hashlen + oid_size < 10 + hashlen)
10 + hashlen < hashlen ||
10 + hashlen + oid_size < 10 + hashlen)
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
/*
@ -1698,8 +1671,7 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig)
{
unsigned char *sig) {
int ret;
unsigned char *sig_try = NULL, *verif = NULL;
@ -1767,8 +1739,7 @@ int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig)
{
unsigned char *sig) {
switch (ctx->padding) {
#if defined(MBEDTLS_PKCS1_V15)
case MBEDTLS_RSA_PKCS_V15:
@ -1800,8 +1771,7 @@ int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
const unsigned char *hash,
mbedtls_md_type_t mgf1_hash_id,
int expected_salt_len,
const unsigned char *sig)
{
const unsigned char *sig) {
int ret;
size_t siglen;
unsigned char *p;
@ -1890,7 +1860,7 @@ int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
observed_salt_len = hash_start - p;
if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
observed_salt_len != (size_t) expected_salt_len) {
observed_salt_len != (size_t) expected_salt_len) {
ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
goto exit;
}
@ -1935,8 +1905,7 @@ int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
const unsigned char *sig)
{
const unsigned char *sig) {
mbedtls_md_type_t mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
? (mbedtls_md_type_t) ctx->hash_id
: md_alg;
@ -1960,8 +1929,7 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
const unsigned char *sig)
{
const unsigned char *sig) {
int ret = 0;
const size_t sig_len = ctx->len;
unsigned char *encoded = NULL, *encoded_expected = NULL;
@ -1974,7 +1942,7 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
*/
if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
(encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
(encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
goto cleanup;
}
@ -2029,8 +1997,7 @@ int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
const unsigned char *sig)
{
const unsigned char *sig) {
switch (ctx->padding) {
#if defined(MBEDTLS_PKCS1_V15)
case MBEDTLS_RSA_PKCS_V15:
@ -2052,8 +2019,7 @@ int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
/*
* Copy the components of an RSA key
*/
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
{
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src) {
int ret;
dst->ver = src->ver;
@ -2092,8 +2058,7 @@ cleanup:
/*
* Free the components of an RSA key
*/
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
{
void mbedtls_rsa_free(mbedtls_rsa_context *ctx) {
mbedtls_mpi_free(&ctx->Vi);
mbedtls_mpi_free(&ctx->Vf);
mbedtls_mpi_free(&ctx->RN);
@ -2162,8 +2127,7 @@ void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
"\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
#if defined(MBEDTLS_PKCS1_V15)
static int myrand(void *rng_state, unsigned char *output, size_t len)
{
static int myrand(void *rng_state, unsigned char *output, size_t len) {
#if !defined(__OpenBSD__)
size_t i;
@ -2186,8 +2150,7 @@ static int myrand(void *rng_state, unsigned char *output, size_t len)
/*
* Checkup routine
*/
int mbedtls_rsa_self_test(int verbose)
{
int mbedtls_rsa_self_test(int verbose) {
int ret = 0;
#if defined(MBEDTLS_PKCS1_V15)
size_t len;
@ -2221,7 +2184,7 @@ int mbedtls_rsa_self_test(int verbose)
mbedtls_printf(" RSA key validation: ");
if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
mbedtls_rsa_check_privkey(&rsa) != 0) {
mbedtls_rsa_check_privkey(&rsa) != 0) {
if (verbose != 0)
mbedtls_printf("failed\n");

View file

@ -69,8 +69,7 @@
*/
int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
mbedtls_mpi const *E, mbedtls_mpi const *D,
mbedtls_mpi *P, mbedtls_mpi *Q)
{
mbedtls_mpi *P, mbedtls_mpi *Q) {
int ret = 0;
uint16_t attempt; /* Number of current attempt */
@ -97,10 +96,10 @@ int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
if (mbedtls_mpi_cmp_int(N, 0) <= 0 ||
mbedtls_mpi_cmp_int(D, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
mbedtls_mpi_cmp_int(D, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
}
@ -156,7 +155,7 @@ int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
if (mbedtls_mpi_cmp_int(P, 1) == 1 &&
mbedtls_mpi_cmp_mpi(P, N) == -1) {
mbedtls_mpi_cmp_mpi(P, N) == -1) {
/*
* Have found a nontrivial divisor P of N.
* Set Q := N / P.
@ -199,8 +198,7 @@ cleanup:
int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
mbedtls_mpi const *Q,
mbedtls_mpi const *E,
mbedtls_mpi *D)
{
mbedtls_mpi *D) {
int ret = 0;
mbedtls_mpi K, L;
@ -208,8 +206,8 @@ int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
mbedtls_mpi_cmp_int(E, 0) == 0) {
mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
mbedtls_mpi_cmp_int(E, 0) == 0) {
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
}
@ -243,8 +241,7 @@ cleanup:
*/
int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, const mbedtls_mpi *DP,
const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
{
const mbedtls_mpi *DQ, const mbedtls_mpi *QP) {
int ret = 0;
mbedtls_mpi K, L;
@ -305,8 +302,8 @@ cleanup:
/* Wrap MPI error codes by RSA check failure error code */
if (ret != 0 &&
ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
}
@ -323,8 +320,7 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
const mbedtls_mpi *Q, const mbedtls_mpi *D,
const mbedtls_mpi *E,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
void *p_rng) {
int ret = 0;
mbedtls_mpi K, L;
@ -337,13 +333,13 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
#if defined(MBEDTLS_GENPRIME)
if (f_rng != NULL && P != NULL &&
(ret = mbedtls_mpi_is_prime(P, f_rng, p_rng)) != 0) {
(ret = mbedtls_mpi_is_prime(P, f_rng, p_rng)) != 0) {
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
goto cleanup;
}
if (f_rng != NULL && Q != NULL &&
(ret = mbedtls_mpi_is_prime(Q, f_rng, p_rng)) != 0) {
(ret = mbedtls_mpi_is_prime(Q, f_rng, p_rng)) != 0) {
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
goto cleanup;
}
@ -359,7 +355,7 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
if (P != NULL && Q != NULL && N != NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
if (mbedtls_mpi_cmp_int(N, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(&K, N) != 0) {
mbedtls_mpi_cmp_mpi(&K, N) != 0) {
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
goto cleanup;
}
@ -371,9 +367,9 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
if (N != NULL && D != NULL && E != NULL) {
if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
goto cleanup;
}
@ -385,7 +381,7 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
if (P != NULL && Q != NULL && D != NULL && E != NULL) {
if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
mbedtls_mpi_cmp_int(Q, 1) <= 0) {
mbedtls_mpi_cmp_int(Q, 1) <= 0) {
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
goto cleanup;
}
@ -426,8 +422,7 @@ cleanup:
int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, mbedtls_mpi *DP,
mbedtls_mpi *DQ, mbedtls_mpi *QP)
{
mbedtls_mpi *DQ, mbedtls_mpi *QP) {
int ret = 0;
mbedtls_mpi K;
mbedtls_mpi_init(&K);

View file

@ -73,13 +73,11 @@
}
#endif
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
{
void mbedtls_sha1_init(mbedtls_sha1_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_sha1_context));
}
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
{
void mbedtls_sha1_free(mbedtls_sha1_context *ctx) {
if (ctx == NULL)
return;
@ -87,16 +85,14 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
}
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
const mbedtls_sha1_context *src)
{
const mbedtls_sha1_context *src) {
*dst = *src;
}
/*
* SHA-1 context setup
*/
int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
{
int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx) {
ctx->total[0] = 0;
ctx->total[1] = 0;
@ -110,16 +106,14 @@ int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
}
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
{
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx) {
mbedtls_sha1_starts_ret(ctx);
}
#endif
#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
const unsigned char data[64])
{
const unsigned char data[64]) {
uint32_t temp, W[16], A, B, C, D, E;
GET_UINT32_BE(W[ 0], data, 0);
@ -278,8 +272,7 @@ int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
const unsigned char data[64])
{
const unsigned char data[64]) {
mbedtls_internal_sha1_process(ctx, data);
}
#endif
@ -290,8 +283,7 @@ void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
*/
int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
int ret;
size_t fill;
uint32_t left;
@ -336,8 +328,7 @@ int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
mbedtls_sha1_update_ret(ctx, input, ilen);
}
#endif
@ -346,8 +337,7 @@ void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
* SHA-1 final digest
*/
int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
unsigned char output[20])
{
unsigned char output[20]) {
int ret;
uint32_t used;
uint32_t high, low;
@ -399,8 +389,7 @@ int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
unsigned char output[20])
{
unsigned char output[20]) {
mbedtls_sha1_finish_ret(ctx, output);
}
#endif
@ -412,8 +401,7 @@ void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
*/
int mbedtls_sha1_ret(const unsigned char *input,
size_t ilen,
unsigned char output[20])
{
unsigned char output[20]) {
int ret;
mbedtls_sha1_context ctx;
@ -437,8 +425,7 @@ exit:
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha1(const unsigned char *input,
size_t ilen,
unsigned char output[20])
{
unsigned char output[20]) {
mbedtls_sha1_ret(input, ilen, output);
}
#endif
@ -475,8 +462,7 @@ static const unsigned char sha1_test_sum[3][20] = {
/*
* Checkup routine
*/
int mbedtls_sha1_self_test(int verbose)
{
int mbedtls_sha1_self_test(int verbose) {
int i, j, buflen, ret = 0;
unsigned char buf[1024];
unsigned char sha1sum[20];

View file

@ -76,13 +76,11 @@
} while( 0 )
#endif
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
{
void mbedtls_sha256_init(mbedtls_sha256_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_sha256_context));
}
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
{
void mbedtls_sha256_free(mbedtls_sha256_context *ctx) {
if (ctx == NULL)
return;
@ -90,16 +88,14 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
}
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
const mbedtls_sha256_context *src)
{
const mbedtls_sha256_context *src) {
*dst = *src;
}
/*
* SHA-256 context setup
*/
int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
{
int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224) {
ctx->total[0] = 0;
ctx->total[1] = 0;
@ -132,8 +128,7 @@ int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
int is224)
{
int is224) {
mbedtls_sha256_starts_ret(ctx, is224);
}
#endif
@ -184,8 +179,7 @@ static const uint32_t K[] = {
}
int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
const unsigned char data[64])
{
const unsigned char data[64]) {
uint32_t temp1, temp2, W[64];
uint32_t A[8];
unsigned int i;
@ -247,8 +241,7 @@ int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
const unsigned char data[64])
{
const unsigned char data[64]) {
mbedtls_internal_sha256_process(ctx, data);
}
#endif
@ -259,8 +252,7 @@ void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
*/
int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
int ret;
size_t fill;
uint32_t left;
@ -305,8 +297,7 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
mbedtls_sha256_update_ret(ctx, input, ilen);
}
#endif
@ -315,8 +306,7 @@ void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
* SHA-256 final digest
*/
int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx,
unsigned char output[32])
{
unsigned char output[32]) {
int ret;
uint32_t used;
uint32_t high, low;
@ -373,8 +363,7 @@ int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
unsigned char output[32])
{
unsigned char output[32]) {
mbedtls_sha256_finish_ret(ctx, output);
}
#endif
@ -387,8 +376,7 @@ void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
int mbedtls_sha256_ret(const unsigned char *input,
size_t ilen,
unsigned char output[32],
int is224)
{
int is224) {
int ret;
mbedtls_sha256_context ctx;
@ -413,8 +401,7 @@ exit:
void mbedtls_sha256(const unsigned char *input,
size_t ilen,
unsigned char output[32],
int is224)
{
int is224) {
mbedtls_sha256_ret(input, ilen, output, is224);
}
#endif
@ -482,8 +469,7 @@ static const unsigned char sha256_test_sum[6][32] = {
/*
* Checkup routine
*/
int mbedtls_sha256_self_test(int verbose)
{
int mbedtls_sha256_self_test(int verbose) {
int i, j, k, buflen, ret = 0;
unsigned char *buf;
unsigned char sha256sum[32];

View file

@ -90,13 +90,11 @@
}
#endif /* PUT_UINT64_BE */
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
{
void mbedtls_sha512_init(mbedtls_sha512_context *ctx) {
memset(ctx, 0, sizeof(mbedtls_sha512_context));
}
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
{
void mbedtls_sha512_free(mbedtls_sha512_context *ctx) {
if (ctx == NULL)
return;
@ -104,16 +102,14 @@ void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
}
void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
const mbedtls_sha512_context *src)
{
const mbedtls_sha512_context *src) {
*dst = *src;
}
/*
* SHA-512 context setup
*/
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
{
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384) {
ctx->total[0] = 0;
ctx->total[1] = 0;
@ -146,8 +142,7 @@ int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
int is384)
{
int is384) {
mbedtls_sha512_starts_ret(ctx, is384);
}
#endif
@ -201,8 +196,7 @@ static const uint64_t K[80] = {
};
int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
const unsigned char data[128])
{
const unsigned char data[128]) {
int i;
uint64_t temp1, temp2, W[80];
uint64_t A, B, C, D, E, F, G, H;
@ -278,8 +272,7 @@ int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
const unsigned char data[128])
{
const unsigned char data[128]) {
mbedtls_internal_sha512_process(ctx, data);
}
#endif
@ -290,8 +283,7 @@ void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
*/
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
int ret;
size_t fill;
unsigned int left;
@ -335,8 +327,7 @@ int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen)
{
size_t ilen) {
mbedtls_sha512_update_ret(ctx, input, ilen);
}
#endif
@ -345,8 +336,7 @@ void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
* SHA-512 final digest
*/
int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx,
unsigned char output[64])
{
unsigned char output[64]) {
int ret;
unsigned used;
uint64_t high, low;
@ -404,8 +394,7 @@ int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx,
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
unsigned char output[64])
{
unsigned char output[64]) {
mbedtls_sha512_finish_ret(ctx, output);
}
#endif
@ -418,8 +407,7 @@ void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
int mbedtls_sha512_ret(const unsigned char *input,
size_t ilen,
unsigned char output[64],
int is384)
{
int is384) {
int ret;
mbedtls_sha512_context ctx;
@ -444,8 +432,7 @@ exit:
void mbedtls_sha512(const unsigned char *input,
size_t ilen,
unsigned char output[64],
int is384)
{
int is384) {
mbedtls_sha512_ret(input, ilen, output, is384);
}
#endif
@ -535,8 +522,7 @@ static const unsigned char sha512_test_sum[6][64] = {
/*
* Checkup routine
*/
int mbedtls_sha512_self_test(int verbose)
{
int mbedtls_sha512_self_test(int verbose) {
int i, j, k, buflen, ret = 0;
unsigned char *buf;
unsigned char sha512sum[64];

View file

@ -32,16 +32,14 @@
#include "mbedtls/threading.h"
#if defined(MBEDTLS_THREADING_PTHREAD)
static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
{
static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL)
return;
mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
}
static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
{
static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL || !mutex->is_valid)
return;
@ -49,8 +47,7 @@ static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
mutex->is_valid = 0;
}
static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
{
static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL || ! mutex->is_valid)
return (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
@ -60,8 +57,7 @@ static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
return (0);
}
static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
{
static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL || ! mutex->is_valid)
return (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
@ -84,13 +80,11 @@ int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unloc
#endif /* MBEDTLS_THREADING_PTHREAD */
#if defined(MBEDTLS_THREADING_ALT)
static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
{
static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex) {
((void) mutex);
return (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
}
static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
{
static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex) {
((void) mutex);
return;
}
@ -106,8 +100,7 @@ int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
void (*mutex_free)(mbedtls_threading_mutex_t *),
int (*mutex_lock)(mbedtls_threading_mutex_t *),
int (*mutex_unlock)(mbedtls_threading_mutex_t *))
{
int (*mutex_unlock)(mbedtls_threading_mutex_t *)) {
mbedtls_mutex_init = mutex_init;
mbedtls_mutex_free = mutex_free;
mbedtls_mutex_lock = mutex_lock;
@ -121,8 +114,7 @@ void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
/*
* Free global mutexes
*/
void mbedtls_threading_free_alt(void)
{
void mbedtls_threading_free_alt(void) {
#if defined(MBEDTLS_FS_IO)
mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
#endif

View file

@ -78,8 +78,7 @@ struct _hr_time {
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long tsc;
__asm rdtsc
__asm mov [tsc], eax
@ -95,8 +94,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long lo, hi;
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return (lo);
@ -109,8 +107,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long lo, hi;
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return (lo | (hi << 32));
@ -123,8 +120,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long tbl, tbu0, tbu1;
do {
@ -146,8 +142,7 @@ unsigned long mbedtls_timing_hardclock(void)
#else
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long tick;
asm volatile("rdpr %%tick, %0;" : "=&r"(tick));
return (tick);
@ -161,8 +156,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long tick;
asm volatile(".byte 0x83, 0x41, 0x00, 0x00");
asm volatile("mov %%g1, %0" : "=r"(tick));
@ -176,8 +170,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long cc;
asm volatile("rpcc %0" : "=r"(cc));
return (cc & 0xFFFFFFFF);
@ -190,8 +183,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
unsigned long itc;
asm volatile("mov %0 = ar.itc" : "=r"(itc));
return (itc);
@ -204,8 +196,7 @@ unsigned long mbedtls_timing_hardclock(void)
#define HAVE_HARDCLOCK
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
LARGE_INTEGER offset;
QueryPerformanceCounter(&offset);
@ -221,8 +212,7 @@ unsigned long mbedtls_timing_hardclock(void)
static int hardclock_init = 0;
static struct timeval tv_init;
unsigned long mbedtls_timing_hardclock(void)
{
unsigned long mbedtls_timing_hardclock(void) {
struct timeval tv_cur;
if (hardclock_init == 0) {
@ -248,8 +238,7 @@ volatile int mbedtls_timing_alarmed = 0;
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) {
struct _hr_time *t = (struct _hr_time *) val;
if (reset) {
@ -269,16 +258,14 @@ unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int r
/* It's OK to use a global because alarm() is supposed to be global anyway */
static DWORD alarmMs;
static DWORD WINAPI TimerProc(LPVOID TimerContext)
{
static DWORD WINAPI TimerProc(LPVOID TimerContext) {
((void) TimerContext);
Sleep(alarmMs);
mbedtls_timing_alarmed = 1;
return (TRUE);
}
void mbedtls_set_alarm(int seconds)
{
void mbedtls_set_alarm(int seconds) {
DWORD ThreadId;
if (seconds == 0) {
@ -295,8 +282,7 @@ void mbedtls_set_alarm(int seconds)
#else /* _WIN32 && !EFIX64 && !EFI32 */
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) {
struct _hr_time *t = (struct _hr_time *) val;
if (reset) {
@ -312,14 +298,12 @@ unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int r
}
}
static void sighandler(int signum)
{
static void sighandler(int signum) {
mbedtls_timing_alarmed = 1;
signal(signum, sighandler);
}
void mbedtls_set_alarm(int seconds)
{
void mbedtls_set_alarm(int seconds) {
mbedtls_timing_alarmed = 0;
signal(SIGALRM, sighandler);
alarm(seconds);
@ -335,8 +319,7 @@ void mbedtls_set_alarm(int seconds)
/*
* Set delays to watch
*/
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
{
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) {
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
ctx->int_ms = int_ms;
@ -349,8 +332,7 @@ void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
/*
* Get number of delays expired
*/
int mbedtls_timing_get_delay(void *data)
{
int mbedtls_timing_get_delay(void *data) {
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
unsigned long elapsed_ms;
@ -376,8 +358,7 @@ int mbedtls_timing_get_delay(void *data)
* Busy-waits for the given number of milliseconds.
* Used for testing mbedtls_timing_hardclock.
*/
static void busy_msleep(unsigned long msec)
{
static void busy_msleep(unsigned long msec) {
struct mbedtls_timing_hr_time hires;
unsigned long i = 0; /* for busy-waiting */
volatile unsigned long j; /* to prevent optimisation */
@ -413,8 +394,7 @@ static void busy_msleep(unsigned long msec)
* Warning: this is work in progress, some tests may not be reliable enough
* yet! False positives may happen.
*/
int mbedtls_timing_self_test(int verbose)
{
int mbedtls_timing_self_test(int verbose) {
unsigned long cycles = 0, ratio = 0;
unsigned long millisecs = 0, secs = 0;
int hardfail = 0;
@ -509,7 +489,7 @@ hard_test:
/* Allow variation up to 20% */
if (cycles / millisecs < ratio - ratio / 5 ||
cycles / millisecs > ratio + ratio / 5) {
cycles / millisecs > ratio + ratio / 5) {
hardfail++;
goto hard_test;
}

View file

@ -79,8 +79,7 @@
* CertificateSerialNumber ::= INTEGER
*/
int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
mbedtls_x509_buf *serial)
{
mbedtls_x509_buf *serial) {
int ret;
if ((end - *p) < 1)
@ -88,7 +87,7 @@ int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
**p != MBEDTLS_ASN1_INTEGER)
**p != MBEDTLS_ASN1_INTEGER)
return (MBEDTLS_ERR_X509_INVALID_SERIAL +
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
@ -110,8 +109,7 @@ int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
* parameters ANY DEFINED BY algorithm OPTIONAL }
*/
int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
mbedtls_x509_buf *alg)
{
mbedtls_x509_buf *alg) {
int ret;
if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0)
@ -124,8 +122,7 @@ int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
* Parse an algorithm identifier with (optional) paramaters
*/
int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
{
mbedtls_x509_buf *alg, mbedtls_x509_buf *params) {
int ret;
if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0)
@ -144,8 +141,7 @@ int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
*
* For HashAlgorithm, parameters MUST be NULL or absent.
*/
static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
{
static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg) {
int ret;
unsigned char *p;
const unsigned char *end;
@ -205,8 +201,7 @@ static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_
*/
int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
int *salt_len)
{
int *salt_len) {
int ret;
unsigned char *p;
const unsigned char *end, *end2;
@ -340,8 +335,7 @@ int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
*/
static int x509_get_attr_type_value(unsigned char **p,
const unsigned char *end,
mbedtls_x509_name *cur)
{
mbedtls_x509_name *cur) {
int ret;
size_t len;
mbedtls_x509_buf *oid;
@ -369,9 +363,9 @@ static int x509_get_attr_type_value(unsigned char **p,
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
**p != MBEDTLS_ASN1_BIT_STRING)
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
**p != MBEDTLS_ASN1_BIT_STRING)
return (MBEDTLS_ERR_X509_INVALID_NAME +
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
@ -413,8 +407,7 @@ static int x509_get_attr_type_value(unsigned char **p,
* this list, eg mbedtls_x509_dn_gets().
*/
int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
mbedtls_x509_name *cur)
{
mbedtls_x509_name *cur) {
int ret;
size_t set_len;
const unsigned char *end_set;
@ -463,8 +456,7 @@ int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
}
}
static int x509_parse_int(unsigned char **p, size_t n, int *res)
{
static int x509_parse_int(unsigned char **p, size_t n, int *res) {
*res = 0;
for (; n > 0; --n) {
@ -478,8 +470,7 @@ static int x509_parse_int(unsigned char **p, size_t n, int *res)
return (0);
}
static int x509_date_is_valid(const mbedtls_x509_time *t)
{
static int x509_date_is_valid(const mbedtls_x509_time *t) {
int ret = MBEDTLS_ERR_X509_INVALID_DATE;
int month_len;
@ -506,7 +497,7 @@ static int x509_date_is_valid(const mbedtls_x509_time *t)
break;
case 2:
if ((!(t->year % 4) && t->year % 100) ||
!(t->year % 400))
!(t->year % 400))
month_len = 29;
else
month_len = 28;
@ -524,8 +515,7 @@ static int x509_date_is_valid(const mbedtls_x509_time *t)
* field.
*/
static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
mbedtls_x509_time *tm)
{
mbedtls_x509_time *tm) {
int ret;
/*
@ -585,8 +575,7 @@ static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
* generalTime GeneralizedTime }
*/
int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
mbedtls_x509_time *tm)
{
mbedtls_x509_time *tm) {
int ret;
size_t len, year_len;
unsigned char tag;
@ -614,8 +603,7 @@ int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
return x509_parse_time(p, len, year_len, tm);
}
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
{
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig) {
int ret;
size_t len;
int tag_type;
@ -643,8 +631,7 @@ int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x5
*/
int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
void **sig_opts)
{
void **sig_opts) {
int ret;
if (*sig_opts != NULL)
@ -676,7 +663,7 @@ int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509
{
/* Make sure parameters are absent or NULL */
if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
sig_params->len != 0)
sig_params->len != 0)
return (MBEDTLS_ERR_X509_INVALID_ALG);
}
@ -688,8 +675,7 @@ int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509
* be either manually updated or extensions should be parsed!)
*/
int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
mbedtls_x509_buf *ext, int tag)
{
mbedtls_x509_buf *ext, int tag) {
int ret;
size_t len;
@ -728,8 +714,7 @@ int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
* Store the name in printable form into buf; no more
* than size characters will be written
*/
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
{
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn) {
int ret;
size_t i, n;
unsigned char c, merge = 0;
@ -786,8 +771,7 @@ int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
* Store the serial in printable form into buf; no more
* than size characters will be written
*/
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
{
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial) {
int ret;
size_t i, n, nr;
char *p;
@ -820,8 +804,7 @@ int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *ser
*/
int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
const void *sig_opts)
{
const void *sig_opts) {
int ret;
char *p = buf;
size_t n = size;
@ -862,8 +845,7 @@ int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *si
/*
* Helper for writing "RSA key size", "EC key size", etc
*/
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
{
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name) {
char *p = buf;
size_t n = buf_size;
int ret;
@ -879,8 +861,7 @@ int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
* Set the time structure to the current time.
* Return 0 on success, non-zero on failure.
*/
static int x509_get_current_time(mbedtls_x509_time *now)
{
static int x509_get_current_time(mbedtls_x509_time *now) {
struct tm *lt, tm_buf;
mbedtls_time_t tt;
int ret = 0;
@ -909,46 +890,44 @@ static int x509_get_current_time(mbedtls_x509_time *now)
/*
* Return 0 if before <= after, 1 otherwise
*/
static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
{
static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after) {
if (before->year > after->year)
return (1);
if (before->year == after->year &&
before->mon > after->mon)
before->mon > after->mon)
return (1);
if (before->year == after->year &&
before->mon == after->mon &&
before->day > after->day)
before->mon == after->mon &&
before->day > after->day)
return (1);
if (before->year == after->year &&
before->mon == after->mon &&
before->day == after->day &&
before->hour > after->hour)
before->mon == after->mon &&
before->day == after->day &&
before->hour > after->hour)
return (1);
if (before->year == after->year &&
before->mon == after->mon &&
before->day == after->day &&
before->hour == after->hour &&
before->min > after->min)
before->mon == after->mon &&
before->day == after->day &&
before->hour == after->hour &&
before->min > after->min)
return (1);
if (before->year == after->year &&
before->mon == after->mon &&
before->day == after->day &&
before->hour == after->hour &&
before->min == after->min &&
before->sec > after->sec)
before->mon == after->mon &&
before->day == after->day &&
before->hour == after->hour &&
before->min == after->min &&
before->sec > after->sec)
return (1);
return (0);
}
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
{
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) {
mbedtls_x509_time now;
if (x509_get_current_time(&now) != 0)
@ -957,8 +936,7 @@ int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
return (x509_check_time(&now, to));
}
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
{
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) {
mbedtls_x509_time now;
if (x509_get_current_time(&now) != 0)
@ -969,14 +947,12 @@ int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
#else /* MBEDTLS_HAVE_TIME_DATE */
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
{
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) {
((void) to);
return (0);
}
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
{
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) {
((void) from);
return (0);
}
@ -990,8 +966,7 @@ int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
/*
* Checkup routine
*/
int mbedtls_x509_self_test(int verbose)
{
int mbedtls_x509_self_test(int verbose) {
#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
int ret;
uint32_t flags;

View file

@ -74,8 +74,7 @@
*/
static int x509_crl_get_version(unsigned char **p,
const unsigned char *end,
int *ver)
{
int *ver) {
int ret;
if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
@ -99,8 +98,7 @@ static int x509_crl_get_version(unsigned char **p,
*/
static int x509_get_crl_ext(unsigned char **p,
const unsigned char *end,
mbedtls_x509_buf *ext)
{
mbedtls_x509_buf *ext) {
int ret;
/*
@ -142,7 +140,7 @@ static int x509_get_crl_ext(unsigned char **p,
/* Get optional critical */
if ((ret = mbedtls_asn1_get_bool(p, end_ext_data,
&is_critical)) != 0 &&
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
}
@ -175,8 +173,7 @@ static int x509_get_crl_ext(unsigned char **p,
*/
static int x509_get_crl_entry_ext(unsigned char **p,
const unsigned char *end,
mbedtls_x509_buf *ext)
{
mbedtls_x509_buf *ext) {
int ret;
size_t len = 0;
@ -226,8 +223,7 @@ static int x509_get_crl_entry_ext(unsigned char **p,
*/
static int x509_get_entries(unsigned char **p,
const unsigned char *end,
mbedtls_x509_crl_entry *entry)
{
mbedtls_x509_crl_entry *entry) {
int ret;
size_t entry_len;
mbedtls_x509_crl_entry *cur_entry = entry;
@ -287,8 +283,7 @@ static int x509_get_entries(unsigned char **p,
* Parse one CRLs in DER format and append it to the chained list
*/
int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
const unsigned char *buf, size_t buflen)
{
const unsigned char *buf, size_t buflen) {
int ret;
size_t len;
unsigned char *p = NULL, *end = NULL;
@ -379,7 +374,7 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
* signature AlgorithmIdentifier
*/
if ((ret = x509_crl_get_version(&p, end, &crl->version)) != 0 ||
(ret = mbedtls_x509_get_alg(&p, end, &crl->sig_oid, &sig_params1)) != 0) {
(ret = mbedtls_x509_get_alg(&p, end, &crl->sig_oid, &sig_params1)) != 0) {
mbedtls_x509_crl_free(crl);
return (ret);
}
@ -428,8 +423,8 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
if ((ret = mbedtls_x509_get_time(&p, end, &crl->next_update)) != 0) {
if (ret != (MBEDTLS_ERR_X509_INVALID_DATE +
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) &&
ret != (MBEDTLS_ERR_X509_INVALID_DATE +
MBEDTLS_ERR_ASN1_OUT_OF_DATA)) {
ret != (MBEDTLS_ERR_X509_INVALID_DATE +
MBEDTLS_ERR_ASN1_OUT_OF_DATA)) {
mbedtls_x509_crl_free(crl);
return (ret);
}
@ -479,10 +474,10 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
}
if (crl->sig_oid.len != sig_oid2.len ||
memcmp(crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len) != 0 ||
sig_params1.len != sig_params2.len ||
(sig_params1.len != 0 &&
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
memcmp(crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len) != 0 ||
sig_params1.len != sig_params2.len ||
(sig_params1.len != 0 &&
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
mbedtls_x509_crl_free(crl);
return (MBEDTLS_ERR_X509_SIG_MISMATCH);
}
@ -504,8 +499,7 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
/*
* Parse one or more CRLs and add them to the chained list
*/
int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen)
{
int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen) {
#if defined(MBEDTLS_PEM_PARSE_C)
int ret;
size_t use_len;
@ -564,8 +558,7 @@ int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, si
/*
* Load one or more CRLs and add them to the chained list
*/
int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path)
{
int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path) {
int ret;
size_t n;
unsigned char *buf;
@ -591,8 +584,7 @@ int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path)
* Return an informational string about the CRL.
*/
int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
const mbedtls_x509_crl *crl)
{
const mbedtls_x509_crl *crl) {
int ret;
size_t n;
char *p;
@ -664,16 +656,14 @@ int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
/*
* Initialize a CRL chain
*/
void mbedtls_x509_crl_init(mbedtls_x509_crl *crl)
{
void mbedtls_x509_crl_init(mbedtls_x509_crl *crl) {
memset(crl, 0, sizeof(mbedtls_x509_crl));
}
/*
* Unallocate all CRL data
*/
void mbedtls_x509_crl_free(mbedtls_x509_crl *crl)
{
void mbedtls_x509_crl_free(mbedtls_x509_crl *crl) {
mbedtls_x509_crl *crl_cur = crl;
mbedtls_x509_crl *crl_prv;
mbedtls_x509_name *name_cur;

View file

@ -160,8 +160,7 @@ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = {
* Return 0 if md_alg is acceptable for this profile, -1 otherwise
*/
static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
mbedtls_md_type_t md_alg)
{
mbedtls_md_type_t md_alg) {
if (md_alg == MBEDTLS_MD_NONE)
return (-1);
@ -176,8 +175,7 @@ static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
* Return 0 if pk_alg is acceptable for this profile, -1 otherwise
*/
static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
mbedtls_pk_type_t pk_alg)
{
mbedtls_pk_type_t pk_alg) {
if (pk_alg == MBEDTLS_PK_NONE)
return (-1);
@ -192,8 +190,7 @@ static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
* Return 0 if pk is acceptable for this profile, -1 otherwise
*/
static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
const mbedtls_pk_context *pk)
{
const mbedtls_pk_context *pk) {
const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
#if defined(MBEDTLS_RSA_C)
@ -207,8 +204,8 @@ static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
#if defined(MBEDTLS_ECP_C)
if (pk_alg == MBEDTLS_PK_ECDSA ||
pk_alg == MBEDTLS_PK_ECKEY ||
pk_alg == MBEDTLS_PK_ECKEY_DH) {
pk_alg == MBEDTLS_PK_ECKEY ||
pk_alg == MBEDTLS_PK_ECKEY_DH) {
const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
if (gid == MBEDTLS_ECP_DP_NONE)
@ -229,8 +226,7 @@ static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
*/
static int x509_get_version(unsigned char **p,
const unsigned char *end,
int *ver)
{
int *ver) {
int ret;
size_t len;
@ -264,8 +260,7 @@ static int x509_get_version(unsigned char **p,
static int x509_get_dates(unsigned char **p,
const unsigned char *end,
mbedtls_x509_time *from,
mbedtls_x509_time *to)
{
mbedtls_x509_time *to) {
int ret;
size_t len;
@ -293,8 +288,7 @@ static int x509_get_dates(unsigned char **p,
*/
static int x509_get_uid(unsigned char **p,
const unsigned char *end,
mbedtls_x509_buf *uid, int n)
{
mbedtls_x509_buf *uid, int n) {
int ret;
if (*p == end)
@ -319,8 +313,7 @@ static int x509_get_uid(unsigned char **p,
static int x509_get_basic_constraints(unsigned char **p,
const unsigned char *end,
int *ca_istrue,
int *max_pathlen)
{
int *max_pathlen) {
int ret;
size_t len;
@ -367,8 +360,7 @@ static int x509_get_basic_constraints(unsigned char **p,
static int x509_get_ns_cert_type(unsigned char **p,
const unsigned char *end,
unsigned char *ns_cert_type)
{
unsigned char *ns_cert_type) {
int ret;
mbedtls_x509_bitstring bs = { 0, 0, NULL };
@ -386,8 +378,7 @@ static int x509_get_ns_cert_type(unsigned char **p,
static int x509_get_key_usage(unsigned char **p,
const unsigned char *end,
unsigned int *key_usage)
{
unsigned int *key_usage) {
int ret;
size_t i;
mbedtls_x509_bitstring bs = { 0, 0, NULL };
@ -415,8 +406,7 @@ static int x509_get_key_usage(unsigned char **p,
*/
static int x509_get_ext_key_usage(unsigned char **p,
const unsigned char *end,
mbedtls_x509_sequence *ext_key_usage)
{
mbedtls_x509_sequence *ext_key_usage) {
int ret;
if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0)
@ -458,8 +448,7 @@ static int x509_get_ext_key_usage(unsigned char **p,
*/
static int x509_get_subject_alt_name(unsigned char **p,
const unsigned char *end,
mbedtls_x509_sequence *subject_alt_name)
{
mbedtls_x509_sequence *subject_alt_name) {
int ret;
size_t len, tag_len;
mbedtls_asn1_buf *buf;
@ -486,7 +475,7 @@ static int x509_get_subject_alt_name(unsigned char **p,
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
}
@ -534,8 +523,7 @@ static int x509_get_subject_alt_name(unsigned char **p,
*/
static int x509_get_crt_ext(unsigned char **p,
const unsigned char *end,
mbedtls_x509_crt *crt)
{
mbedtls_x509_crt *crt) {
int ret;
size_t len;
unsigned char *end_ext_data, *end_ext_octet;
@ -575,7 +563,7 @@ static int x509_get_crt_ext(unsigned char **p,
/* Get optional critical */
if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
/* Data should be octet string type */
@ -666,8 +654,7 @@ static int x509_get_crt_ext(unsigned char **p,
* Parse and fill a single X.509 certificate in DER format
*/
static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *buf,
size_t buflen)
{
size_t buflen) {
int ret;
size_t len;
unsigned char *p, *end, *crt_end;
@ -741,9 +728,9 @@ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *b
* signature AlgorithmIdentifier
*/
if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
(ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
(ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
&sig_params1)) != 0) {
(ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
(ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
&sig_params1)) != 0) {
mbedtls_x509_crt_free(crt);
return (ret);
}
@ -874,10 +861,10 @@ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *b
}
if (crt->sig_oid.len != sig_oid2.len ||
memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
sig_params1.len != sig_params2.len ||
(sig_params1.len != 0 &&
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
sig_params1.len != sig_params2.len ||
(sig_params1.len != 0 &&
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
mbedtls_x509_crt_free(crt);
return (MBEDTLS_ERR_X509_SIG_MISMATCH);
}
@ -901,8 +888,7 @@ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *b
* chained list
*/
int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, const unsigned char *buf,
size_t buflen)
{
size_t buflen) {
int ret;
mbedtls_x509_crt *crt = chain, *prev = NULL;
@ -948,8 +934,7 @@ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, const unsigned char *buf
* Parse one or more PEM certificates from a buffer and add them to the chained
* list
*/
int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen)
{
int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen) {
#if defined(MBEDTLS_PEM_PARSE_C)
int success = 0, first_error = 0, total_failed = 0;
int buf_format = MBEDTLS_X509_FORMAT_DER;
@ -967,7 +952,7 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, si
*/
#if defined(MBEDTLS_PEM_PARSE_C)
if (buflen != 0 && buf[buflen - 1] == '\0' &&
strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
buf_format = MBEDTLS_X509_FORMAT_PEM;
}
@ -1053,8 +1038,7 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, si
/*
* Load one or more certificates and add them to the chained list
*/
int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
{
int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path) {
int ret;
size_t n;
unsigned char *buf;
@ -1070,8 +1054,7 @@ int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
return (ret);
}
int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
{
int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path) {
int ret = 0;
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
int w_ret;
@ -1187,8 +1170,7 @@ cleanup:
#endif /* MBEDTLS_FS_IO */
static int x509_info_subject_alt_name(char **buf, size_t *size,
const mbedtls_x509_sequence *subject_alt_name)
{
const mbedtls_x509_sequence *subject_alt_name) {
size_t i;
size_t n = *size;
char *p = *buf;
@ -1234,8 +1216,7 @@ static int x509_info_subject_alt_name(char **buf, size_t *size,
PRINT_ITEM( name );
static int x509_info_cert_type(char **buf, size_t *size,
unsigned char ns_cert_type)
{
unsigned char ns_cert_type) {
int ret;
size_t n = *size;
char *p = *buf;
@ -1261,8 +1242,7 @@ static int x509_info_cert_type(char **buf, size_t *size,
PRINT_ITEM( name );
static int x509_info_key_usage(char **buf, size_t *size,
unsigned int key_usage)
{
unsigned int key_usage) {
int ret;
size_t n = *size;
char *p = *buf;
@ -1285,8 +1265,7 @@ static int x509_info_key_usage(char **buf, size_t *size,
}
static int x509_info_ext_key_usage(char **buf, size_t *size,
const mbedtls_x509_sequence *extended_key_usage)
{
const mbedtls_x509_sequence *extended_key_usage) {
int ret;
const char *desc;
size_t n = *size;
@ -1318,8 +1297,7 @@ static int x509_info_ext_key_usage(char **buf, size_t *size,
#define BEFORE_COLON 18
#define BC "18"
int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
const mbedtls_x509_crt *crt)
{
const mbedtls_x509_crt *crt) {
int ret;
size_t n;
char *p;
@ -1471,8 +1449,7 @@ static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
};
int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
uint32_t flags)
{
uint32_t flags) {
int ret;
const struct x509_crt_verify_string *cur;
char *p = buf;
@ -1498,8 +1475,7 @@ int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
unsigned int usage)
{
unsigned int usage) {
unsigned int usage_must, usage_may;
unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
| MBEDTLS_X509_KU_DECIPHER_ONLY;
@ -1524,8 +1500,7 @@ int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
const char *usage_oid,
size_t usage_len)
{
size_t usage_len) {
const mbedtls_x509_sequence *cur;
/* Extension is not mandatory, absent means no restriction */
@ -1539,7 +1514,7 @@ int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
const mbedtls_x509_buf *cur_oid = &cur->buf;
if (cur_oid->len == usage_len &&
memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
return (0);
}
@ -1555,13 +1530,12 @@ int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
/*
* Return 1 if the certificate is revoked, or 0 otherwise.
*/
int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
{
int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl) {
const mbedtls_x509_crl_entry *cur = &crl->entry;
while (cur != NULL && cur->serial.len != 0) {
if (crt->serial.len == cur->serial.len &&
memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
if (mbedtls_x509_time_is_past(&cur->revocation_date))
return (1);
}
@ -1578,8 +1552,7 @@ int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_
*/
static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
mbedtls_x509_crl *crl_list,
const mbedtls_x509_crt_profile *profile)
{
const mbedtls_x509_crt_profile *profile) {
int flags = 0;
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
const mbedtls_md_info_t *md_info;
@ -1589,9 +1562,9 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
while (crl_list != NULL) {
if (crl_list->version == 0 ||
crl_list->issuer_raw.len != ca->subject_raw.len ||
memcmp(crl_list->issuer_raw.p, ca->subject_raw.p,
crl_list->issuer_raw.len) != 0) {
crl_list->issuer_raw.len != ca->subject_raw.len ||
memcmp(crl_list->issuer_raw.p, ca->subject_raw.p,
crl_list->issuer_raw.len) != 0) {
crl_list = crl_list->next;
continue;
}
@ -1659,8 +1632,7 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
/*
* Like memcmp, but case-insensitive and always returns -1 if different
*/
static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
{
static int x509_memcasecmp(const void *s1, const void *s2, size_t len) {
size_t i;
unsigned char diff;
const unsigned char *n1 = s1, *n2 = s2;
@ -1672,8 +1644,8 @@ static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
continue;
if (diff == 32 &&
((n1[i] >= 'a' && n1[i] <= 'z') ||
(n1[i] >= 'A' && n1[i] <= 'Z'))) {
((n1[i] >= 'a' && n1[i] <= 'z') ||
(n1[i] >= 'A' && n1[i] <= 'Z'))) {
continue;
}
@ -1686,8 +1658,7 @@ static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
/*
* Return 0 if name matches wildcard, -1 otherwise
*/
static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
{
static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name) {
size_t i;
size_t cn_idx = 0, cn_len = strlen(cn);
@ -1706,7 +1677,7 @@ static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
return (-1);
if (cn_len - cn_idx == name->len - 1 &&
x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
return (0);
}
@ -1719,18 +1690,17 @@ static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
*
* Return 0 if equal, -1 otherwise.
*/
static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
{
static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b) {
if (a->tag == b->tag &&
a->len == b->len &&
memcmp(a->p, b->p, b->len) == 0) {
a->len == b->len &&
memcmp(a->p, b->p, b->len) == 0) {
return (0);
}
if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
(b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
a->len == b->len &&
x509_memcasecmp(a->p, b->p, b->len) == 0) {
(b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
a->len == b->len &&
x509_memcasecmp(a->p, b->p, b->len) == 0) {
return (0);
}
@ -1747,8 +1717,7 @@ static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
*
* Return 0 if equal, -1 otherwise.
*/
static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
{
static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b) {
/* Avoid recursion, it might not be optimised by the compiler */
while (a != NULL || b != NULL) {
if (a == NULL || b == NULL)
@ -1756,8 +1725,8 @@ static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
/* type */
if (a->oid.tag != b->oid.tag ||
a->oid.len != b->oid.len ||
memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
a->oid.len != b->oid.len ||
memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
return (-1);
}
@ -1781,8 +1750,7 @@ static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
* Check the signature of a certificate by its parent
*/
static int x509_crt_check_signature(const mbedtls_x509_crt *child,
mbedtls_x509_crt *parent)
{
mbedtls_x509_crt *parent) {
const mbedtls_md_info_t *md_info;
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
@ -1809,8 +1777,7 @@ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
*/
static int x509_crt_check_parent(const mbedtls_x509_crt *child,
const mbedtls_x509_crt *parent,
int top)
{
int top) {
int need_ca_bit;
/* Parent must be the issuer */
@ -1829,7 +1796,7 @@ static int x509_crt_check_parent(const mbedtls_x509_crt *child,
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
if (need_ca_bit &&
mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
return (-1);
}
#endif
@ -1880,8 +1847,7 @@ static mbedtls_x509_crt *x509_crt_find_parent_in(mbedtls_x509_crt *child,
mbedtls_x509_crt *candidates,
int top,
size_t path_cnt,
size_t self_cnt)
{
size_t self_cnt) {
mbedtls_x509_crt *parent, *badtime_parent = NULL;
for (parent = candidates; parent != NULL; parent = parent->next) {
@ -1891,7 +1857,7 @@ static mbedtls_x509_crt *x509_crt_find_parent_in(mbedtls_x509_crt *child,
/* +1 because stored max_pathlen is 1 higher that the actual value */
if (parent->max_pathlen > 0 &&
(size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
(size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
continue;
}
@ -1902,7 +1868,7 @@ static mbedtls_x509_crt *x509_crt_find_parent_in(mbedtls_x509_crt *child,
/* optional time check */
if (mbedtls_x509_time_is_past(&parent->valid_to) ||
mbedtls_x509_time_is_future(&parent->valid_from)) {
mbedtls_x509_time_is_future(&parent->valid_from)) {
if (badtime_parent == NULL)
badtime_parent = parent;
@ -1941,8 +1907,7 @@ static mbedtls_x509_crt *x509_crt_find_parent(mbedtls_x509_crt *child,
mbedtls_x509_crt *trust_ca,
int *parent_is_trusted,
size_t path_cnt,
size_t self_cnt)
{
size_t self_cnt) {
mbedtls_x509_crt *parent;
/* Look for a parent in trusted CAs */
@ -1965,8 +1930,7 @@ static mbedtls_x509_crt *x509_crt_find_parent(mbedtls_x509_crt *child,
*/
static int x509_crt_check_ee_locally_trusted(
mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca)
{
mbedtls_x509_crt *trust_ca) {
mbedtls_x509_crt *cur;
/* must be self-issued */
@ -1976,7 +1940,7 @@ static int x509_crt_check_ee_locally_trusted(
/* look for an exact match with trusted cert */
for (cur = trust_ca; cur != NULL; cur = cur->next) {
if (crt->raw.len == cur->raw.len &&
memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
return (0);
}
}
@ -2018,8 +1982,7 @@ static int x509_crt_verify_chain(
mbedtls_x509_crl *ca_crl,
const mbedtls_x509_crt_profile *profile,
x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
size_t *chain_len)
{
size_t *chain_len) {
uint32_t *flags;
mbedtls_x509_crt *child;
mbedtls_x509_crt *parent;
@ -2056,7 +2019,7 @@ static int x509_crt_verify_chain(
/* Special case: EE certs that are locally trusted */
if (*chain_len == 1 &&
x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
return (0);
}
@ -2074,14 +2037,14 @@ static int x509_crt_verify_chain(
* These can occur with some strategies for key rollover, see [SIRO],
* and should be excluded from max_pathlen checks. */
if (*chain_len != 1 &&
x509_name_cmp(&child->issuer, &child->subject) == 0) {
x509_name_cmp(&child->issuer, &child->subject) == 0) {
self_cnt++;
}
/* path_cnt is 0 for the first intermediate CA,
* and if parent is trusted it's not an intermediate CA */
if (! parent_is_trusted &&
*chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
*chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
/* return immediately to avoid overflow the chain array */
return (MBEDTLS_ERR_X509_FATAL_ERROR);
}
@ -2112,11 +2075,10 @@ static int x509_crt_verify_chain(
* Check for CN match
*/
static int x509_crt_check_cn(const mbedtls_x509_buf *name,
const char *cn, size_t cn_len)
{
const char *cn, size_t cn_len) {
/* try exact match */
if (name->len == cn_len &&
x509_memcasecmp(cn, name->p, cn_len) == 0) {
x509_memcasecmp(cn, name->p, cn_len) == 0) {
return (0);
}
@ -2133,8 +2095,7 @@ static int x509_crt_check_cn(const mbedtls_x509_buf *name,
*/
static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
const char *cn,
uint32_t *flags)
{
uint32_t *flags) {
const mbedtls_x509_name *name;
const mbedtls_x509_sequence *cur;
size_t cn_len = strlen(cn);
@ -2150,7 +2111,7 @@ static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
} else {
for (name = &crt->subject; name != NULL; name = name->next) {
if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
break;
}
}
@ -2168,8 +2129,7 @@ static int x509_crt_merge_flags_with_cb(
x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
size_t chain_len,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
void *p_vrfy) {
int ret;
size_t i;
uint32_t cur_flags;
@ -2195,8 +2155,7 @@ int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
mbedtls_x509_crl *ca_crl,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
void *p_vrfy) {
return (mbedtls_x509_crt_verify_with_profile(crt, trust_ca, ca_crl,
&mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy));
}
@ -2217,8 +2176,7 @@ int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
void *p_vrfy) {
int ret;
mbedtls_pk_type_t pk_type;
x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE];
@ -2278,16 +2236,14 @@ exit:
/*
* Initialize a certificate chain
*/
void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
{
void mbedtls_x509_crt_init(mbedtls_x509_crt *crt) {
memset(crt, 0, sizeof(mbedtls_x509_crt));
}
/*
* Unallocate all certificate data
*/
void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
{
void mbedtls_x509_crt_free(mbedtls_x509_crt *crt) {
mbedtls_x509_crt *cert_cur = crt;
mbedtls_x509_crt *cert_prv;
mbedtls_x509_name *name_cur;

View file

@ -21,20 +21,17 @@ extern "C" {
extern const uint8_t OddByteParity[256];
static inline bool oddparity8(const uint8_t x)
{
static inline bool oddparity8(const uint8_t x) {
return OddByteParity[x];
}
static inline bool evenparity8(const uint8_t x)
{
static inline bool evenparity8(const uint8_t x) {
return !OddByteParity[x];
}
static inline bool evenparity32(uint32_t x)
{
static inline bool evenparity32(uint32_t x) {
#if !defined __GNUC__
x ^= x >> 16;
x ^= x >> 8;
@ -45,8 +42,7 @@ static inline bool evenparity32(uint32_t x)
}
static inline bool oddparity32(uint32_t x)
{
static inline bool oddparity32(uint32_t x) {
#if !defined __GNUC__
x ^= x >> 16;
x ^= x >> 8;

View file

@ -6,8 +6,7 @@
#include "prng.h"
#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
uint32_t burtle_get_mod(prng_ctx *x)
{
uint32_t burtle_get_mod(prng_ctx *x) {
uint32_t e = x->a - rot(x->b, 21);
x->a = x->b ^ rot(x->c, 19);
x->b = x->c + rot(x->d, 6);
@ -16,8 +15,7 @@ uint32_t burtle_get_mod(prng_ctx *x)
return x->d;
}
void burtle_init_mod(prng_ctx *x, uint32_t seed)
{
void burtle_init_mod(prng_ctx *x, uint32_t seed) {
x->a = 0xf1ea5eed;
x->b = x->c = x->d = seed;
for (uint8_t i = 0; i < 42; ++i) {
@ -25,8 +23,7 @@ void burtle_init_mod(prng_ctx *x, uint32_t seed)
}
}
void burtle_init(prng_ctx *x, uint32_t seed)
{
void burtle_init(prng_ctx *x, uint32_t seed) {
uint32_t i;
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
for (i = 0; i < 20; ++i) {
@ -35,8 +32,7 @@ void burtle_init(prng_ctx *x, uint32_t seed)
}
uint32_t GetSimplePrng(uint32_t seed)
{
uint32_t GetSimplePrng(uint32_t seed) {
seed *= 0x19660D;
seed += 0x3C6EF35F;
return seed;

View file

@ -1,8 +1,7 @@
#include "protocols.h"
// ATA55xx shared presets & routines
uint32_t GetT55xxClockBit(uint32_t clock)
{
uint32_t GetT55xxClockBit(uint32_t clock) {
switch (clock) {
case 128:
return T55x7_BITRATE_RF_128;
@ -29,18 +28,15 @@ uint32_t GetT55xxClockBit(uint32_t clock)
#include "ui.h"
#define PrintAndLogDevice(level, format, args...) PrintAndLogEx(level, format , ## args)
uint8_t isset(uint8_t val, uint8_t mask)
{
uint8_t isset(uint8_t val, uint8_t mask) {
return (val & mask);
}
uint8_t notset(uint8_t val, uint8_t mask)
{
uint8_t notset(uint8_t val, uint8_t mask) {
return !(val & mask);
}
void fuse_config(const picopass_hdr *hdr)
{
void fuse_config(const picopass_hdr *hdr) {
uint8_t fuses = hdr->conf.fuses;
if (isset(fuses, FUSE_FPERS))
@ -71,8 +67,7 @@ void fuse_config(const picopass_hdr *hdr)
PrintAndLogDevice(WARNING, "\tRA: Read access not enabled");
}
void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *app_areas, uint8_t *kb)
{
void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *app_areas, uint8_t *kb) {
// mem-bit 5, mem-bit 7, chip-bit 4: defines chip type
uint8_t k16 = isset(mem_cfg, 0x80);
//uint8_t k2 = isset(mem_cfg, 0x08);
@ -105,8 +100,7 @@ void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *
}
}
void mem_app_config(const picopass_hdr *hdr)
{
void mem_app_config(const picopass_hdr *hdr) {
uint8_t mem = hdr->conf.mem_config;
uint8_t chip = hdr->conf.chip_config;
uint8_t applimit = hdr->conf.app_limit;
@ -142,13 +136,11 @@ void mem_app_config(const picopass_hdr *hdr)
PrintAndLogDevice(NORMAL, "\tCredit - Kc");
}
}
void print_picopass_info(const picopass_hdr *hdr)
{
void print_picopass_info(const picopass_hdr *hdr) {
fuse_config(hdr);
mem_app_config(hdr);
}
void printIclassDumpInfo(uint8_t *iclass_dump)
{
void printIclassDumpInfo(uint8_t *iclass_dump) {
print_picopass_info((picopass_hdr *) iclass_dump);
}

View file

@ -1,7 +1,6 @@
#include "radixsort.h"
uint64_t *radixSort(uint64_t *array, uint32_t size)
{
uint64_t *radixSort(uint64_t *array, uint32_t size) {
rscounts_t counts;
memset(&counts, 0, 256 * 8 * sizeof(uint32_t));
uint64_t *cpy = (uint64_t *)calloc(size * sizeof(uint64_t), sizeof(uint8_t));

View file

@ -13,17 +13,14 @@ static uint32_t g_nextrandom;
* https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/
*/
inline void fast_prand()
{
inline void fast_prand() {
fast_prandEx(GetTickCount());
}
inline void fast_prandEx(uint32_t seed)
{
inline void fast_prandEx(uint32_t seed) {
g_nextrandom = seed;
}
uint32_t prand()
{
uint32_t prand() {
// g_nextrandom *= 6364136223846793005;
// g_nextrandom += 1;
//return (uint32_t)(g_nextrandom >> 32) % 0xffffffff;

View file

@ -11,8 +11,7 @@
#define DELTA 0x9E3779B9
#define SUM 0xC6EF3720
void tea_encrypt(uint8_t *v, uint8_t *key)
{
void tea_encrypt(uint8_t *v, uint8_t *key) {
uint32_t a = 0, b = 0, c = 0, d = 0, y = 0, z = 0;
uint32_t sum = 0;
@ -38,8 +37,7 @@ void tea_encrypt(uint8_t *v, uint8_t *key)
num_to_bytes(z, 4, v + 4);
}
void tea_decrypt(uint8_t *v, uint8_t *key)
{
void tea_decrypt(uint8_t *v, uint8_t *key) {
uint32_t a = 0, b = 0, c = 0, d = 0, y = 0, z = 0;
uint32_t sum = SUM;

View file

@ -44,8 +44,7 @@ static uint8_t us_outbuf[sizeof(UsbCommand)];
/// Reads data from an USART peripheral
/// \param data Pointer to the buffer where the received data will be stored.
/// \param len Size of the data buffer (in bytes).
inline int16_t usart_readbuffer(uint8_t *data, size_t len)
{
inline int16_t usart_readbuffer(uint8_t *data, size_t len) {
// Check if the first PDC bank is free
if (!(pUS1->US_RCR)) {
@ -69,8 +68,7 @@ inline int16_t usart_readbuffer(uint8_t *data, size_t len)
// transfer from device to client
inline int16_t usart_writebuffer(uint8_t *data, size_t len)
{
inline int16_t usart_writebuffer(uint8_t *data, size_t len) {
// Check if the first PDC bank is free
if (!(pUS1->US_TCR)) {
@ -94,8 +92,7 @@ inline int16_t usart_writebuffer(uint8_t *data, size_t len)
}
}
void usart_init(void)
{
void usart_init(void) {
// disable & reset receiver / transmitter for configuration
pUS1->US_CR = (AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS);

View file

@ -391,8 +391,7 @@ static const char StrMS_OSDescriptor[] = {
'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0, MS_VENDOR_CODE, 0
};
const char *getStringDescriptor(uint8_t idx)
{
const char *getStringDescriptor(uint8_t idx) {
switch (idx) {
case 0:
return StrLanguageCodes;
@ -447,8 +446,7 @@ AT91S_CDC_LINE_CODING line = {
8
}; // 8 Data bits
static void SpinDelay(int ms)
{
static void SpinDelay(int ms) {
int us = ms * 1000;
int ticks = (48 * us) >> 10;
@ -475,8 +473,7 @@ static void SpinDelay(int ms)
//* \fn usb_disable
//* \brief This function deactivates the USB device
//*----------------------------------------------------------------------------
void usb_disable()
{
void usb_disable() {
// Disconnect the USB device
AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU;
@ -490,8 +487,7 @@ void usb_disable()
//* \fn usb_enable
//* \brief This function Activates the USB device
//*----------------------------------------------------------------------------
void usb_enable()
{
void usb_enable() {
// Set the PLL USB Divider
AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1 ;
@ -529,25 +525,20 @@ void usb_enable()
//*----------------------------------------------------------------------------
static int usb_reconnect = 0;
static int usb_configured = 0;
void SetUSBreconnect(int value)
{
void SetUSBreconnect(int value) {
usb_reconnect = value;
}
int GetUSBreconnect(void)
{
int GetUSBreconnect(void) {
return usb_reconnect;
}
void SetUSBconfigured(int value)
{
void SetUSBconfigured(int value) {
usb_configured = value;
}
int GetUSBconfigured(void)
{
int GetUSBconfigured(void) {
return usb_configured;
}
bool usb_check()
{
bool usb_check() {
/*
// reconnected ONCE and
@ -598,8 +589,7 @@ bool usb_check()
return (btConfiguration) ? true : false;
}
bool usb_poll()
{
bool usb_poll() {
if (!usb_check()) return false;
return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);
}
@ -612,8 +602,7 @@ bool usb_poll()
that the length available to read is non-zero, thus hopefully fixes the
bug.
**/
bool usb_poll_validate_length()
{
bool usb_poll_validate_length() {
if (!usb_check()) return false;
if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false;
return ((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) > 0;
@ -623,8 +612,7 @@ bool usb_poll_validate_length()
//* \fn usb_read
//* \brief Read available data from Endpoint 1 OUT (host to device)
//*----------------------------------------------------------------------------
uint32_t usb_read(byte_t *data, size_t len)
{
uint32_t usb_read(byte_t *data, size_t len) {
if (len == 0) return 0;
@ -662,8 +650,7 @@ uint32_t usb_read(byte_t *data, size_t len)
//* \fn usb_write
//* \brief Send through endpoint 2 (device to host)
//*----------------------------------------------------------------------------
uint32_t usb_write(const byte_t *data, const size_t len)
{
uint32_t usb_write(const byte_t *data, const size_t len) {
if (!len) return 0;
if (!usb_check()) return 0;
@ -719,8 +706,7 @@ uint32_t usb_write(const byte_t *data, const size_t len)
//* \fn AT91F_USB_SendData
//* \brief Send Data through the control endpoint
//*----------------------------------------------------------------------------
void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length)
{
void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {
uint32_t cpt = 0;
AT91_REG csr;
@ -761,8 +747,7 @@ void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length)
//* \fn AT91F_USB_SendZlp
//* \brief Send zero length packet through the control endpoint
//*----------------------------------------------------------------------------
void AT91F_USB_SendZlp(AT91PS_UDP pUdp)
{
void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {
UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);
while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP));
UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);
@ -773,8 +758,7 @@ void AT91F_USB_SendZlp(AT91PS_UDP pUdp)
//* \fn AT91F_USB_SendStall
//* \brief Stall the control endpoint
//*----------------------------------------------------------------------------
void AT91F_USB_SendStall(AT91PS_UDP pUdp)
{
void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL);
while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR));
UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR));
@ -788,8 +772,7 @@ void AT91F_USB_SendStall(AT91PS_UDP pUdp)
//* 1. this is for USB endpoint0. the control endpoint.
//* 2. mixed with CDC ACM endpoint3 , interrupt, control endpoint
//*----------------------------------------------------------------------------
void AT91F_CDC_Enumerate()
{
void AT91F_CDC_Enumerate() {
byte_t bmRequestType, bRequest;
uint16_t wValue, wIndex, wLength, wStatus;

View file

@ -15,8 +15,7 @@
* @param type use the defined values EVEN|ODD
* @return parity bit required to match type
*/
uint8_t getParity(uint8_t *bits, uint8_t len, uint8_t type)
{
uint8_t getParity(uint8_t *bits, uint8_t len, uint8_t type) {
uint8_t x = 0;
for (; len > 0; --len)
x += bits[len - 1];
@ -37,8 +36,7 @@ uint8_t checkParity(uint32_t bits, uint8_t len, uint8_t type);
// by marshmellow
// takes a array of binary values, start position, length of bits per parity (includes parity bit),
// Parity Type (1 for odd; 0 for even; 2 for Always 1's; 3 for Always 0's), and binary Length (length to run)
size_t removeParity(uint8_t *bitstream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen)
{
size_t removeParity(uint8_t *bitstream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen) {
uint32_t parityWd = 0;
size_t j = 0, bitcount = 0;
for (int word = 0; word < (bLen); word += pLen) {
@ -81,8 +79,7 @@ size_t removeParity(uint8_t *bitstream, size_t startIdx, uint8_t pLen, uint8_t p
* @param pType EVEN|ODD|2 (always 1's)|3 (always 0's)
* @return
*/
size_t addParity(uint8_t *bits, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType)
{
size_t addParity(uint8_t *bits, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType) {
uint32_t parityWd = 0;
size_t j = 0, bitCnt = 0;
for (int word = 0; word < sourceLen; word += pLen - 1) {
@ -119,8 +116,7 @@ size_t addParity(uint8_t *bits, uint8_t *dest, uint8_t sourceLen, uint8_t pLen,
* @param dest pointer to the destination where wiegandparity has been appended
* @param len number of bits which wiegand parity shall be calculated over. This number is without parities, so a wiegand 26 has 24 bits of data
*/
void wiegand_add_parity(uint8_t *source, uint8_t *dest, uint8_t len)
{
void wiegand_add_parity(uint8_t *source, uint8_t *dest, uint8_t len) {
// Copy to destination, shifted one step to make room for EVEN parity
memcpy(dest + 1, source, length);
@ -148,8 +144,7 @@ void wiegand_add_parity(uint8_t *source, uint8_t *dest, uint8_t len)
* @param dest pointer to the destination where wiegand bytes will be stored
* @param formatlen
*/
void num_to_wiegand_bytes(uint64_t oem, uint64_t fc, uint64_t cn, uint8_t *dest, uint8_t formatlen)
{
void num_to_wiegand_bytes(uint64_t oem, uint64_t fc, uint64_t cn, uint8_t *dest, uint8_t formatlen) {
uint8_t data[MAX_BITS_TXX55] = {0};
memset(data, 0, sizeof(data));
@ -173,8 +168,7 @@ void num_to_wiegand_bytes(uint64_t oem, uint64_t fc, uint64_t cn, uint8_t *dest,
* @param dest pointer to the destination where wiegand bits will be stored
* @param formatlen
*/
void num_to_wiegand_bits(uint64_t oem, uint64_t fc, uint64_t cn, uint8_t *dest, uint8_t formatlen)
{
void num_to_wiegand_bits(uint64_t oem, uint64_t fc, uint64_t cn, uint8_t *dest, uint8_t formatlen) {
uint8_t bits[MAX_BITS_TXX55] = {0};
memset(bits, 0, sizeof(bits));