Debian Codegraph @

crypto_kem/6960119pcf/vec/kem_enc.c

raw
1 // 20240805 djb: more mask usage
2 // 20230102 djb: rename encrypt() as pke_encrypt()
3 // 20221230 djb: add linker lines
4 // 20221230 djb: split out of operations.c
5
6 // linker define operation_enc
7 // linker use pke_encrypt
8
9 #include "operations.h"
10
11 #include "hash.h"
12 #include "encrypt.h"
13 #include "params.h"
14 #include "util.h"
15
16 #include <stdint.h>
17 #include <string.h>
18 #include "crypto_int8.h"
19
20 /* check if the padding bits of pk are all zero */
21 static int check_pk_padding(const unsigned char * pk)
22 {
23 unsigned char b;
24 int i;
25
26 b = 0;
27 for (i = 0; i < PK_NROWS; i++)
28 b |= pk[i*PK_ROW_BYTES + PK_ROW_BYTES-1];
29
30 b >>= (PK_NCOLS % 8);
31 return crypto_int8_nonzero_mask(b);
32 }
33
34 int operation_enc(
35 unsigned char *c,
36 unsigned char *key,
37 const unsigned char *pk
38 )
39 {
40 unsigned char two_e[ 1 + SYS_N/8 ] = {2};
41 unsigned char *e = two_e + 1;
42 unsigned char one_ec[ 1 + SYS_N/8 + (SYND_BYTES + 32) ] = {1};
43 unsigned char mask;
44 int i, padding_ok;
45
46 //
47
48 padding_ok = check_pk_padding(pk);
49
50 pke_encrypt(c, pk, e);
51
52 crypto_hash_32b(c + SYND_BYTES, two_e, sizeof(two_e));
53
54 memcpy(one_ec + 1, e, SYS_N/8);
55 memcpy(one_ec + 1 + SYS_N/8, c, SYND_BYTES + 32);
56
57 crypto_hash_32b(key, one_ec, sizeof(one_ec));
58
59 // clear outputs (set to all 0's) if padding bits are not all zero
60
61 mask = padding_ok;
62 mask ^= 0xFF;
63
64 for (i = 0; i < SYND_BYTES + 32; i++)
65 c[i] &= mask;
66
67 for (i = 0; i < 32; i++)
68 key[i] &= mask;
69
70 return padding_ok;
71 }
72