Skip to content

Commit

Permalink
C89 comment translation, plus some fixes to C89-ize c25519-donna.
Browse files Browse the repository at this point in the history
  • Loading branch information
diagprov committed Nov 22, 2019
1 parent 16db7d0 commit f3ec4dc
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 133 deletions.
10 changes: 5 additions & 5 deletions include/e4/crypto/aes256enc.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// aes256enc.h
// 2018-07-01 Markku-Juhani O. Saarinen <markku@teserakt.io>
// (c) 2018 Copyright Teserakt AG
/* aes256enc.h */
/* 2018-07-01 Markku-Juhani O. Saarinen <markku@teserakt.io> */
/* (c) 2018 Copyright Teserakt AG */

#ifndef _AES256ENC_H_
#define _AES256ENC_H_

// expand 256-bit key. ek[] needs 240 bytes of storage
/* expand 256-bit key. ek[] needs 240 bytes of storage */
#define AES256_EXPKEY_LEN 240

void aes256_enc_exp_key(void *ek, const void *key);

// encrypt a block
/* encrypt a block */
void aes256_encrypt_ecb(void *v, const void *ek);

#endif
2 changes: 1 addition & 1 deletion mk/conf_default.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

CC ?= clang
CC ?= clang
AR ?= ar
ARFLAGS = rcs
CFLAGS = -Wall -Werror -g -DE4_STORE_FILE -std=c89
Expand Down
2 changes: 1 addition & 1 deletion mk/objects.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OBJS = $(OBJDIR)/e4symclient.$O \
$(OBJDIR)/strlcpy.$O


OBJS += $(OBJDIR)/crypto/curve25519/curve25519.$O \
OBJS += $(OBJDIR)/crypto/curve25519/curve25519-donna.$O \
$(OBJDIR)/crypto/ed25519/add_scalar.$O \
$(OBJDIR)/crypto/ed25519/ed25519_test.$O \
$(OBJDIR)/crypto/ed25519/fe.$O \
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/aes256enc_openssl.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// aes256enc_openssl.c
// 2018-07-01 Markku-Juhani O. Saarinen <markku@teserakt.io>
/* aes256enc_openssl.c */
/* 2018-07-01 Markku-Juhani O. Saarinen <markku@teserakt.io> */

// (c) 2018 Copyright Teserakt AG
/* (c) 2018 Copyright Teserakt AG */

// OpenSSL wrapper
/* OpenSSL wrapper */

#include "e4/crypto/aes256enc.h"
#include <openssl/aes.h>
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/aes256enc_ref.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// aes256enc_ref.c
// 2018-07-09 Markku-Juhani O. Saarinen
/* aes256enc_ref.c */
/* 2018-07-09 Markku-Juhani O. Saarinen */

// A baseline reference implementation of AES-256 Encrypt
/* A baseline reference implementation of AES-256 Encrypt */

/**
* Hacked from: rijndael-alg-fst.c
Expand Down Expand Up @@ -274,7 +274,7 @@ static const uint32_t rcon[] = {
}
#endif

// Expand the cipher key into the encryption key schedule.
/* Expand the cipher key into the encryption key schedule. */

void aes256_enc_exp_key(void *ek_out, const void *key_in)
{
Expand Down
56 changes: 28 additions & 28 deletions src/crypto/aes_siv.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ siv_cmac(uint8_t res[16], const uint8_t *data, size_t len, const uint8_t xorend[
j = 0;

for (i = 0; i + 16 < len; i++)
{ // process blocks
{ /* process blocks */
if (j >= 16)
{
aes256_encrypt_ecb(blk, eky);
Expand All @@ -49,19 +49,19 @@ siv_cmac(uint8_t res[16], const uint8_t *data, size_t len, const uint8_t xorend[
}

for (; i < len; i++)
{ // last 16 bytes
{ /* last 16 bytes */
if (j >= 16)
{
aes256_encrypt_ecb(blk, eky);
j = 0;
}
if (xorend != NULL) // xor 16 chaining bytes at end
if (xorend != NULL) /* xor 16 chaining bytes at end */
blk[j] ^= xorend[i + 16 - len];
blk[j++] ^= data[i];
}

if (j < 16)
{ // padding
{ /* padding */
blk[j] ^= 0x80;
cmac_dbl(k12);
}
Expand All @@ -72,7 +72,7 @@ siv_cmac(uint8_t res[16], const uint8_t *data, size_t len, const uint8_t xorend[
memcpy(res, blk, 16);
}

// counter mode encryption / decryption
/* counter mode encryption / decryption */

static void
siv_ctr(uint8_t *dst, const uint8_t *src, size_t len, const uint8_t iv[16], void *eky)
Expand All @@ -94,7 +94,7 @@ siv_ctr(uint8_t *dst, const uint8_t *src, size_t len, const uint8_t iv[16], void
aes256_encrypt_ecb(blk, eky);

for (j = 15; j >= 0; j--)
{ // will actually terminate early
{ /* will actually terminate early */
cnt[j]++;
if (cnt[j] != 0xFF) break;
}
Expand All @@ -104,34 +104,34 @@ siv_ctr(uint8_t *dst, const uint8_t *src, size_t len, const uint8_t iv[16], void
}
}

// AES256-SIV Encrypt
/* AES256-SIV Encrypt */

int aes256_encrypt_siv(uint8_t *ct,
size_t *ctlen, // out: ciphertext
size_t *ctlen, /* out: ciphertext */
const uint8_t *ad,
size_t adlen, // in: associated data / nonce
size_t adlen, /* in: associated data / nonce */
const uint8_t *pt,
size_t ptlen, // in: plaintext
const uint8_t *key) // in: secret key (32 bytes)
size_t ptlen, /* in: plaintext */
const uint8_t *key) /* in: secret key (32 bytes) */
{
size_t i;
uint8_t d[16], x[16];

uint8_t eky[AES256_EXPKEY_LEN];

// CMAC part
/* CMAC part */

aes256_enc_exp_key(eky, key);

memset(d, 0, 16); // zero
memset(d, 0, 16); /* zero */
siv_cmac(d, d, 16, NULL, eky);

cmac_dbl(d);
siv_cmac(x, ad, adlen, NULL, eky); // cmac associated data
siv_cmac(x, ad, adlen, NULL, eky); /* cmac associated data */
for (i = 0; i < 16; i++) d[i] ^= x[i];

if (ptlen >= 16)
{ // cmac plaintext
{ /* cmac plaintext */
siv_cmac(ct, pt, ptlen, d, eky);
}
else
Expand All @@ -142,26 +142,26 @@ int aes256_encrypt_siv(uint8_t *ct,
siv_cmac(ct, d, 16, NULL, eky);
}

// CTR part
/* CTR part */

aes256_enc_exp_key(eky, key);

siv_ctr(ct + 16, pt, ptlen, ct, eky); // (siv) counter mode
siv_ctr(ct + 16, pt, ptlen, ct, eky); /* (siv) counter mode */

*ctlen = ptlen + 16; // set the length
*ctlen = ptlen + 16; /* set the length */

return 0;
}

// AES256-SIV Decrypt
/* AES256-SIV Decrypt */

int aes256_decrypt_siv(uint8_t *pt,
size_t *ptlen, // out: plaintext
size_t *ptlen, /* out: plaintext */
const uint8_t *ad,
size_t adlen, // in: associated data / nonce
size_t adlen, /* in: associated data / nonce */
const uint8_t *ct,
size_t ctlen, // in: ciphertext
const uint8_t *key) // in: secret key (32 bytes)
size_t ctlen, /* in: ciphertext */
const uint8_t *key) /* in: secret key (32 bytes) */
{
size_t i;
uint8_t d[16], x[16];
Expand All @@ -170,22 +170,22 @@ int aes256_decrypt_siv(uint8_t *pt,

aes256_enc_exp_key(eky, key);

ctlen -= 16; // ctlen is ptlen now
ctlen -= 16; /* ctlen is ptlen now */
*ptlen = ctlen;

siv_ctr(pt, ct + 16, ctlen, ct, eky);

aes256_enc_exp_key(eky, key); // CMAC part
aes256_enc_exp_key(eky, key); /* CMAC part */

memset(d, 0, 16); // zero
memset(d, 0, 16); /* zero */
siv_cmac(d, d, 16, NULL, eky);

cmac_dbl(d);
siv_cmac(x, ad, adlen, NULL, eky); // cmac associated data
siv_cmac(x, ad, adlen, NULL, eky); /* cmac associated data */
for (i = 0; i < 16; i++) d[i] ^= x[i];

if (ctlen >= 16)
{ // cmac plaintext
{ /* cmac plaintext */
siv_cmac(x, pt, ctlen, d, eky);
}
else
Expand Down
10 changes: 5 additions & 5 deletions src/crypto/curve25519/curve25519-donna.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* from the sample implementation. */

#include <string.h>
#include <stdint.h>
#include "e4/stdint.h" /* if you are re-using this code you may want <stdint.h> */

#ifdef _MSC_VER
#define inline __inline
Expand Down Expand Up @@ -679,7 +679,7 @@ static void fmonty(limb *x2, limb *z2, /* output 2Q */
freduce_degree(x2);
freduce_coefficients(x2);
/* |x2[i]| < 2^26 */
fdifference(zz, xx); // does zz = xx - zz
fdifference(zz, xx); /* does zz = xx - zz */
/* |zz[i]| < 2^27 */
memset(zzz + 10, 0, sizeof(limb) * 9);
fscalar_product(zzz, zz, 121665);
Expand Down Expand Up @@ -770,9 +770,9 @@ cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q) {
memcpy(resultz, nqz, sizeof(limb) * 10);
}

// -----------------------------------------------------------------------------
// Shamelessly copied from djb's code
// -----------------------------------------------------------------------------
/* ----------------------------------------------------------------------------- */
/* Shamelessly copied from djb's code */
/* ----------------------------------------------------------------------------- */
static void
crecip(limb *out, const limb *z) {
limb z2[10];
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/ed25519/add_scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "sha512.h"


/* see http://crypto.stackexchange.com/a/6215/4697 */
/* see http:/*crypto.stackexchange.com/a/6215/4697 */ */
void ed25519_add_scalar(unsigned char *public_key, unsigned char *private_key, const unsigned char *scalar) {
const unsigned char SC_1[32] = {1}; /* scalar with value 1 */

Expand All @@ -30,7 +30,7 @@ void ed25519_add_scalar(unsigned char *public_key, unsigned char *private_key, c
if (private_key) {
sc_muladd(private_key, SC_1, n, private_key);

// https://github.com/orlp/ed25519/issues/3
/* https:/*github.com/orlp/ed25519/issues/3 */
sha512_init(&hash);
sha512_update(&hash, private_key + 32, 32);
sha512_update(&hash, scalar, 32);
Expand Down
18 changes: 9 additions & 9 deletions src/crypto/keccakf1600.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* Based on the public domain implementation in
* crypto_hash/keccakc512/simple/ from http://bench.cr.yp.to/supercop.html
* crypto_hash/keccakc512/simple/ from http://bench.cr.yp.to/supercop.html
* by Ronny Van Keer
* and the public domain "TweetFips202" implementation
* from https://twitter.com/tweetfips202
* from https://twitter.com/tweetfips202
* by Gilles Van Assche, Daniel J. Bernstein, and Peter Schwabe */

#include <stdint.h>
//#include <assert.h>
/*#include <assert.h> */

#define NROUNDS 24
#define ROL(a, offset) ((a << offset) ^ (a >> (64 - offset)))
Expand Down Expand Up @@ -91,7 +91,7 @@ void keccak_f1600(void *st, uint8_t rc)
uint64_t Esa, Ese, Esi, Eso, Esu;


// copyFromState(A, state)
/* copyFromState(A, state) */
Aba = state[0];
Abe = state[1];
Abi = state[2];
Expand Down Expand Up @@ -120,14 +120,14 @@ void keccak_f1600(void *st, uint8_t rc)

for (round = 0; round < rc; round += 2)
{
// prepareTheta
/* prepareTheta */
BCa = Aba ^ Aga ^ Aka ^ Ama ^ Asa;
BCe = Abe ^ Age ^ Ake ^ Ame ^ Ase;
BCi = Abi ^ Agi ^ Aki ^ Ami ^ Asi;
BCo = Abo ^ Ago ^ Ako ^ Amo ^ Aso;
BCu = Abu ^ Agu ^ Aku ^ Amu ^ Asu;

// thetaRhoPiChiIotaPrepareTheta(round , A, E)
/* thetaRhoPiChiIotaPrepareTheta(round , A, E) */
Da = BCu ^ ROL(BCe, 1);
De = BCa ^ ROL(BCi, 1);
Di = BCe ^ ROL(BCo, 1);
Expand Down Expand Up @@ -215,14 +215,14 @@ void keccak_f1600(void *st, uint8_t rc)
Eso = BCo ^ ((~BCu) & BCa);
Esu = BCu ^ ((~BCa) & BCe);

// prepareTheta
/* prepareTheta */
BCa = Eba ^ Ega ^ Eka ^ Ema ^ Esa;
BCe = Ebe ^ Ege ^ Eke ^ Eme ^ Ese;
BCi = Ebi ^ Egi ^ Eki ^ Emi ^ Esi;
BCo = Ebo ^ Ego ^ Eko ^ Emo ^ Eso;
BCu = Ebu ^ Egu ^ Eku ^ Emu ^ Esu;

// thetaRhoPiChiIotaPrepareTheta(round+1, E, A)
/* thetaRhoPiChiIotaPrepareTheta(round+1, E, A) */
Da = BCu ^ ROL(BCe, 1);
De = BCa ^ ROL(BCi, 1);
Di = BCe ^ ROL(BCo, 1);
Expand Down Expand Up @@ -311,7 +311,7 @@ void keccak_f1600(void *st, uint8_t rc)
Asu = BCu ^ ((~BCa) & BCe);
}

// copyToState(state, A)
/* copyToState(state, A) */
state[0] = Aba;
state[1] = Abe;
state[2] = Abi;
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <stdio.h>

int test_sha3(); // test_sha3.c
int test_sha3(); /* test_sha3.c */
int test_shake();

int test_aes256(); // in test_aes_siv.c
int test_aes256(); /* in test_aes_siv.c */
int test_aes_siv();

int main(int argc, char **argv)
Expand Down
Loading

0 comments on commit f3ec4dc

Please sign in to comment.