Debian Codegraph @

crypto_kem/6960119/avx/kem_enc.c

raw symbols SCIP
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 e[ SYS_N/8 ];
41 unsigned char one_ec[ 1 + SYS_N/8 + SYND_BYTES ] = {1};
42 unsigned char mask;
43 int i, padding_ok;
44
45 //
46
47 padding_ok = check_pk_padding(pk);
48
49 pke_encrypt(c, pk, e);
50
51 memcpy(one_ec + 1, e, SYS_N/8);
52 memcpy(one_ec + 1 + SYS_N/8, c, SYND_BYTES);
53
54 crypto_hash_32b(key, one_ec, sizeof(one_ec));
55
56 // clear outputs (set to all 0's) if padding bits are not all zero
57
58 mask = padding_ok;
59 mask ^= 0xFF;
60
61 for (i = 0; i < SYND_BYTES; i++)
62 c[i] &= mask;
63
64 for (i = 0; i < 32; i++)
65 key[i] &= mask;
66
67 return padding_ok;
68 }
69