| 1 |
// 20260619 djb: more cryptoint usage |
| 2 |
// 20240805 djb: more cryptoint usage |
| 3 |
// 20240805 djb: more mask usage |
| 4 |
// 20221230 djb: add linker lines |
| 5 |
// 20221230 djb: split out of operations.c |
| 6 |
|
| 7 |
// linker define operation_dec |
| 8 |
// linker use decrypt |
| 9 |
|
| 10 |
#include "operations.h" |
| 11 |
|
| 12 |
#include "hash.h" |
| 13 |
#include "decrypt.h" |
| 14 |
#include "params.h" |
| 15 |
#include "util.h" |
| 16 |
|
| 17 |
#include <stdint.h> |
| 18 |
#include <string.h> |
| 19 |
#include "crypto_int8.h" |
| 20 |
#include "crypto_uint8.h" |
| 21 |
|
| 22 |
/* check if the padding bits of c are all zero */ |
| 23 |
static int check_c_padding(const unsigned char * c) |
| 24 |
{ |
| 25 |
unsigned char b; |
| 26 |
|
| 27 |
b = c[ SYND_BYTES-1 ] >> (PK_NROWS % 8); |
| 28 |
return crypto_int8_nonzero_mask(b); |
| 29 |
} |
| 30 |
|
| 31 |
int operation_dec( |
| 32 |
unsigned char *key, |
| 33 |
const unsigned char *c, |
| 34 |
const unsigned char *sk |
| 35 |
) |
| 36 |
{ |
| 37 |
int i, padding_ok; |
| 38 |
|
| 39 |
unsigned char mask; |
| 40 |
unsigned char fail = 0; |
| 41 |
|
| 42 |
unsigned char m; |
| 43 |
|
| 44 |
unsigned char conf[32]; |
| 45 |
unsigned char two_e[ 1 + SYS_N/8 ] = {2}; |
| 46 |
unsigned char *e = two_e + 1; |
| 47 |
unsigned char preimage[ 1 + SYS_N/8 + (SYND_BYTES + 32) ]; |
| 48 |
unsigned char *x = preimage; |
| 49 |
const unsigned char *s = sk + 40 + IRR_BYTES + COND_BYTES; |
| 50 |
|
| 51 |
// |
| 52 |
|
| 53 |
padding_ok = check_c_padding(c); |
| 54 |
|
| 55 |
fail = decrypt(e, sk + 40, c); |
| 56 |
|
| 57 |
crypto_hash_32b(conf, two_e, sizeof(two_e)); |
| 58 |
|
| 59 |
for (i = 0; i < 32; i++) |
| 60 |
fail |= conf[i] ^ c[SYND_BYTES + i]; |
| 61 |
|
| 62 |
m = crypto_uint8_zero_mask(fail); |
| 63 |
|
| 64 |
*x++ = crypto_uint8_bottombit_01(m); |
| 65 |
for (i = 0; i < SYS_N/8; i++) |
| 66 |
*x++ = (~m & s[i]) | (m & e[i]); |
| 67 |
|
| 68 |
for (i = 0; i < SYND_BYTES + 32; i++) |
| 69 |
*x++ = c[i]; |
| 70 |
|
| 71 |
crypto_hash_32b(key, preimage, sizeof(preimage)); |
| 72 |
|
| 73 |
// clear outputs (set to all 1's) if padding bits are not all zero |
| 74 |
|
| 75 |
mask = padding_ok; |
| 76 |
|
| 77 |
for (i = 0; i < 32; i++) |
| 78 |
key[i] |= mask; |
| 79 |
|
| 80 |
return padding_ok; |
| 81 |
} |
| 82 |
|