Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kyber: Implementation in wolfSSL #7318

Merged
merged 1 commit into from
Mar 14, 2024
Merged

Kyber: Implementation in wolfSSL #7318

merged 1 commit into from
Mar 14, 2024

Conversation

SparkiDev
Copy link
Contributor

Description

Put Kyber implementation into wolfSSL.

Testing

Regression tested kyber.
--enable-kyber no defaults to wolfSSL when liboqs is not specified.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@SparkiDev SparkiDev self-assigned this Mar 12, 2024
@SparkiDev SparkiDev force-pushed the kyber_1 branch 3 times, most recently from 5fc8fd6 to a2695be Compare March 12, 2024 01:39
@SparkiDev SparkiDev assigned wolfSSL-Bot and unassigned SparkiDev Mar 12, 2024
Copy link
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./configure --enable-kyber --enable-smallstack --enable-intelasm && make
...
wolfcrypt/src/wc_kyber_asm.S:12065:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12066:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12126:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12127:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12187:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12188:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12248:21: error: invalid operand for instruction
  CC       examples/client/client-client.o
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12249:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12309:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12310:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12370:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12371:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12431:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12432:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12495:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12496:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12686:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12687:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12751:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12752:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
wolfcrypt/src/wc_kyber_asm.S:12817:21: error: invalid operand for instruction
        movzbl %bl, %r10b
                    ^~~~~
wolfcrypt/src/wc_kyber_asm.S:12818:21: error: invalid operand for instruction
        movzbl %bl, %cl
                    ^~~
Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: x86_64-apple-darwin23.3.0
Thread model: posix```


/* Initialize Kyber implementation.
*/
void kyber_init()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void kyber_init()
               ^
                void
1 error generated.```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

/**
* Make a Kyber key object using a random number generator.
*
* @param [in, out] key Kyber key ovject.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ovject -> object. 4 places in code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}


/* Encapsuluate message.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encapsuluate -> Encapsulate. Two places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

r += 4;
}
#else
/* Calculate sizteen integer coefficients at a time. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizteen -> sixteen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

/* Get the noise/error by calculating random bytes and sampling to a binomial
* deistributions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deistributions -> distributions

@SparkiDev
Copy link
Contributor Author

Assembly code fixed.

Copy link
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builds, but fails.

./configure --enable-kyber --enable-smallstack --enable-intelasm && make
./wolfcrypt/test/testwolfcrypt
...
KYBER    test failed!
 error L=35378
 [fiducial line numbers: 8247 25532 38178 50526]

Many build errors with: ./configure --enable-kyber=small --enable-smallstack --enable-intelasm && make. Lots also without the intelasm.

./wolfssl/wolfcrypt/wc_kyber.h:124:18: error: use of undeclared identifier 'KYBER512_K'; did you mean 'KYBER512'?
    sword16 priv[KYBER_MAX_K * KYBER_N];
                 ^~~~~~~~~~~
                 KYBER512
./wolfssl/wolfcrypt/kyber.h:148:37: note: expanded from macro 'KYBER_MAX_K'
#define KYBER_MAX_K                 KYBER512_K
                                    ^
./wolfssl/wolfcrypt/kyber.h:156:5: note: 'KYBER512' declared here
    KYBER512  = 0,
    ^
In file included from wolfcrypt/benchmark/benchmark.c:171:
./wolfssl/wolfcrypt/wc_kyber.h:126:17: error: use of undeclared identifier 'KYBER512_K'; did you mean 'KYBER512'?
    sword16 pub[KYBER_MAX_K * KYBER_N];
                ^~~~~~~~~~~
                KYBER512
./wolfssl/wolfcrypt/kyber.h:148:37: note: expanded from macro 'KYBER_MAX_K'
#define KYBER_MAX_K                 KYBER512_K
                                    ^
./wolfssl/wolfcrypt/kyber.h:156:5: note: 'KYBER512' declared here
    KYBER512  = 0,
    ^
In file included from wolfcrypt/src/wc_kyber_poly.c:31:
./wolfssl/wolfcrypt/wc_kyber.h:124:18: error: use of undeclared identifier 'KYBER512_K'; did you mean 'KYBER512'?
    sword16 priv[KYBER_MAX_K * KYBER_N];
                 ^~~~~~~~~~~
                 KYBER512
./wolfssl/wolfcrypt/kyber.h:148:37: note: expanded from macro 'KYBER_MAX_K'
#define KYBER_MAX_K                 KYBER512_K
                                    ^
./wolfssl/wolfcrypt/kyber.h:156:5: note: 'KYBER512' declared here
    KYBER512  = 0,
    ^
In file included from wolfcrypt/src/wc_kyber_poly.c:31:
./wolfssl/wolfcrypt/wc_kyber.h:126:17: error: use of undeclared identifier 'KYBER512_K'; did you mean 'KYBER512'?
    sword16 pub[KYBER_MAX_K * KYBER_N];
                ^~~~~~~~~~~
                KYBER512
./wolfssl/wolfcrypt/kyber.h:148:37: note: expanded from macro 'KYBER_MAX_K'
#define KYBER_MAX_K                 KYBER512_K
                                    ^
./wolfssl/wolfcrypt/kyber.h:156:5: note: 'KYBER512' declared here
    KYBER512  = 0,
    ^
wolfcrypt/src/wc_kyber_poly.c:1280:35: error: unused parameter 'prf' [-Werror,-Wunused-parameter]
int kyber_gen_matrix(KYBER_PRF_T* prf, sword16* a, int kp, byte* seed,
                                  ^
wolfcrypt/src/wc_kyber_poly.c:1280:49: error: unused parameter 'a' [-Werror,-Wunused-parameter]
int kyber_gen_matrix(KYBER_PRF_T* prf, sword16* a, int kp, byte* seed,
                                                ^
wolfcrypt/src/wc_kyber_poly.c:1280:56: error: unused parameter 'kp' [-Werror,-Wunused-parameter]
int kyber_gen_matrix(KYBER_PRF_T* prf, sword16* a, int kp, byte* seed,
                                                       ^
wolfcrypt/src/wc_kyber_poly.c:1280:66: error: unused parameter 'seed' [-Werror,-Wunused-parameter]
int kyber_gen_matrix(KYBER_PRF_T* prf, sword16* a, int kp, byte* seed,
                                                                 ^
wolfcrypt/src/wc_kyber_poly.c:1281:9: error: unused parameter 'transposed' [-Werror,-Wunused-parameter]
    int transposed)
        ^
wolfcrypt/src/wc_kyber_poly.c:1843:34: error: unused parameter 'prf' [-Werror,-Wunused-parameter]
int kyber_get_noise(KYBER_PRF_T* prf, int kp, sword16* vec1,
                                 ^
wolfcrypt/src/wc_kyber_poly.c:1843:43: error: unused parameter 'kp' [-Werror,-Wunused-parameter]
int kyber_get_noise(KYBER_PRF_T* prf, int kp, sword16* vec1,
                                          ^
wolfcrypt/src/wc_kyber_poly.c:1843:56: error: unused parameter 'vec1' [-Werror,-Wunused-parameter]
int kyber_get_noise(KYBER_PRF_T* prf, int kp, sword16* vec1,
                                                       ^
wolfcrypt/src/wc_kyber_poly.c:1844:14: error: unused parameter 'vec2' [-Werror,-Wunused-parameter]
    sword16* vec2, sword16* poly, byte* seed)
             ^
wolfcrypt/src/wc_kyber_poly.c:1844:29: error: unused parameter 'poly' [-Werror,-Wunused-parameter]
    sword16* vec2, sword16* poly, byte* seed)
                            ^
wolfcrypt/src/wc_kyber_poly.c:1844:41: error: unused parameter 'seed' [-Werror,-Wunused-parameter]
    sword16* vec2, sword16* poly, byte* seed)
                                        ^
13 errors generated.
make[2]: *** [wolfcrypt/src/src_libwolfssl_la-wc_kyber_poly.lo] Error 1
make[2]: *** Waiting for unfinished jobs....
wolfcrypt/benchmark/benchmark.c:9195:13: error: use of undeclared identifier 'KYBER512_CIPHER_TEXT_SIZE'
    byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
            ^
./wolfssl/wolfcrypt/kyber.h:151:37: note: expanded from macro 'KYBER_MAX_CIPHER_TEXT_SIZE'
#define KYBER_MAX_CIPHER_TEXT_SIZE  KYBER512_CIPHER_TEXT_SIZE
                                    ^
3 errors generated.```


#ifdef WOLFSSL_KYBER1024
/* Get the noise/error by calculating random bytes and sampling to a binomial
* deistributions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deistributions -> distributions. Four places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Put Kyber implementation into wolfSSL.
@SparkiDev
Copy link
Contributor Author

SparkiDev commented Mar 13, 2024

Can't do --enable-kyber=small.
Must be --enable-kyber=small,all or specific sizes.

@SparkiDev
Copy link
Contributor Author

Assembly code works now.

@dgarske dgarske merged commit 8fd8548 into wolfSSL:master Mar 14, 2024
105 of 110 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants