1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-22 23:48:58 +00:00

fix for big-endian

This commit is contained in:
Matthias J. Kannwischer 2019-10-04 13:10:29 +02:00
parent 9571a3b017
commit 39246b808d
3 changed files with 30 additions and 45 deletions

View File

@ -4,34 +4,29 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
static inline uint32_t br_swap32(uint32_t x) { static inline void br_enc32be(unsigned char *dst, uint32_t x) {
x = ((x & (uint32_t)0x00FF00FF) << 8) dst[3] = (unsigned char)x;
| ((x >> 8) & (uint32_t)0x00FF00FF); dst[2] = (unsigned char)(x >> 8);
return (x << 16) | (x >> 16); dst[1] = (unsigned char)(x >> 16);
} dst[0] = (unsigned char)(x >> 24);
static inline void inc1_be(uint32_t *x) {
uint32_t t = br_swap32(*x) + 1;
*x = br_swap32(t);
} }
static void aes256_ctr_xof(unsigned char *out, size_t outlen, const unsigned char *iv, uint32_t ctr, const aes256ctx *ctx) { static void aes256_ctr_xof(unsigned char *out, size_t outlen, const unsigned char *iv, uint32_t ctr, const aes256ctx *ctx) {
uint32_t ivw[4] = {0}; uint8_t ivw[16];
ivw[3] = br_swap32(ctr);
uint8_t buf[AES_BLOCKBYTES]; uint8_t buf[AES_BLOCKBYTES];
size_t i; size_t i;
memcpy(ivw, iv, AESCTR_NONCEBYTES); memcpy(ivw, iv, AESCTR_NONCEBYTES);
br_enc32be(ivw + AESCTR_NONCEBYTES, ctr);
while (outlen > AES_BLOCKBYTES) { while (outlen > AES_BLOCKBYTES) {
aes256_ecb(out, (uint8_t *) ivw, 1, ctx); aes256_ecb(out, ivw, 1, ctx);
inc1_be(ivw + 3); br_enc32be(ivw + AESCTR_NONCEBYTES, ++ctr);
out += AES_BLOCKBYTES; out += AES_BLOCKBYTES;
outlen -= AES_BLOCKBYTES; outlen -= AES_BLOCKBYTES;
} }
if (outlen > 0) { if (outlen > 0) {
aes256_ecb(buf, (uint8_t *) ivw, 1, ctx); aes256_ecb(buf, ivw, 1, ctx);
for (i = 0; i < outlen; i++) { for (i = 0; i < outlen; i++) {
out[i] = buf[i]; out[i] = buf[i];
} }

View File

@ -4,34 +4,29 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
static inline uint32_t br_swap32(uint32_t x) { static inline void br_enc32be(unsigned char *dst, uint32_t x) {
x = ((x & (uint32_t)0x00FF00FF) << 8) dst[3] = (unsigned char)x;
| ((x >> 8) & (uint32_t)0x00FF00FF); dst[2] = (unsigned char)(x >> 8);
return (x << 16) | (x >> 16); dst[1] = (unsigned char)(x >> 16);
} dst[0] = (unsigned char)(x >> 24);
static inline void inc1_be(uint32_t *x) {
uint32_t t = br_swap32(*x) + 1;
*x = br_swap32(t);
} }
static void aes256_ctr_xof(unsigned char *out, size_t outlen, const unsigned char *iv, uint32_t ctr, const aes256ctx *ctx) { static void aes256_ctr_xof(unsigned char *out, size_t outlen, const unsigned char *iv, uint32_t ctr, const aes256ctx *ctx) {
uint32_t ivw[4] = {0}; uint8_t ivw[16];
ivw[3] = br_swap32(ctr);
uint8_t buf[AES_BLOCKBYTES]; uint8_t buf[AES_BLOCKBYTES];
size_t i; size_t i;
memcpy(ivw, iv, AESCTR_NONCEBYTES); memcpy(ivw, iv, AESCTR_NONCEBYTES);
br_enc32be(ivw + AESCTR_NONCEBYTES, ctr);
while (outlen > AES_BLOCKBYTES) { while (outlen > AES_BLOCKBYTES) {
aes256_ecb(out, (uint8_t *) ivw, 1, ctx); aes256_ecb(out, ivw, 1, ctx);
inc1_be(ivw + 3); br_enc32be(ivw + AESCTR_NONCEBYTES, ++ctr);
out += AES_BLOCKBYTES; out += AES_BLOCKBYTES;
outlen -= AES_BLOCKBYTES; outlen -= AES_BLOCKBYTES;
} }
if (outlen > 0) { if (outlen > 0) {
aes256_ecb(buf, (uint8_t *) ivw, 1, ctx); aes256_ecb(buf, ivw, 1, ctx);
for (i = 0; i < outlen; i++) { for (i = 0; i < outlen; i++) {
out[i] = buf[i]; out[i] = buf[i];
} }

View File

@ -4,34 +4,29 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
static inline uint32_t br_swap32(uint32_t x) { static inline void br_enc32be(unsigned char *dst, uint32_t x) {
x = ((x & (uint32_t)0x00FF00FF) << 8) dst[3] = (unsigned char)x;
| ((x >> 8) & (uint32_t)0x00FF00FF); dst[2] = (unsigned char)(x >> 8);
return (x << 16) | (x >> 16); dst[1] = (unsigned char)(x >> 16);
} dst[0] = (unsigned char)(x >> 24);
static inline void inc1_be(uint32_t *x) {
uint32_t t = br_swap32(*x) + 1;
*x = br_swap32(t);
} }
static void aes256_ctr_xof(unsigned char *out, size_t outlen, const unsigned char *iv, uint32_t ctr, const aes256ctx *ctx) { static void aes256_ctr_xof(unsigned char *out, size_t outlen, const unsigned char *iv, uint32_t ctr, const aes256ctx *ctx) {
uint32_t ivw[4] = {0}; uint8_t ivw[16];
ivw[3] = br_swap32(ctr);
uint8_t buf[AES_BLOCKBYTES]; uint8_t buf[AES_BLOCKBYTES];
size_t i; size_t i;
memcpy(ivw, iv, AESCTR_NONCEBYTES); memcpy(ivw, iv, AESCTR_NONCEBYTES);
br_enc32be(ivw + AESCTR_NONCEBYTES, ctr);
while (outlen > AES_BLOCKBYTES) { while (outlen > AES_BLOCKBYTES) {
aes256_ecb(out, (uint8_t *) ivw, 1, ctx); aes256_ecb(out, ivw, 1, ctx);
inc1_be(ivw + 3); br_enc32be(ivw + AESCTR_NONCEBYTES, ++ctr);
out += AES_BLOCKBYTES; out += AES_BLOCKBYTES;
outlen -= AES_BLOCKBYTES; outlen -= AES_BLOCKBYTES;
} }
if (outlen > 0) { if (outlen > 0) {
aes256_ecb(buf, (uint8_t *) ivw, 1, ctx); aes256_ecb(buf, ivw, 1, ctx);
for (i = 0; i < outlen; i++) { for (i = 0; i < outlen; i++) {
out[i] = buf[i]; out[i] = buf[i];
} }