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

Polynomials for CMAC and GCM mode #423

Closed
noloader opened this issue May 16, 2017 · 6 comments
Closed

Polynomials for CMAC and GCM mode #423

noloader opened this issue May 16, 2017 · 6 comments

Comments

@noloader
Copy link
Collaborator

noloader commented May 16, 2017

We recently added variable block sizes and ciphers that incorporate them. Refer to the bug reports below. The larger block sizes require different polynomials. This ticket will track the related work.

We already accomplished the work for the polynomials for CMAC, but it should be documented. GCM mode is still outstanding.

According to Table of Low-Weight Binary Irreducible Polynomials, here are the 256, 512 and 1024-bit polynomials:

  • x^256 + x^10 + x^5 + x + 1
  • x^512 + x^8 + x^5 + x^2 + 1
  • x^1024 + x^19 + x^6 + x + 1

Also see OMAC/CMAC constant for different block sizes on Crypto Stack Exchange.


Related bug reports:

@noloader
Copy link
Collaborator Author

The library already had the 256-bit CMAC polynomial. The 512 and 1024-bit CMAC polynomials were handled at:

@kerukuro
Copy link

kerukuro commented Sep 12, 2017

Table of Low-Weight Binary Irreducible Polynomial (http://www.hpl.hp.com/techreports/98/HPL-98-135.pdf) lists 256,10,5,2 for 256-bit polynomial, which means x^256 + x^10 + x^5 + x^2 + 1.
Note that it ends with "x^2 + 1", not with "x + 1".
The same polynomial is listed in other sources, eg the book referenced in https://crypto.stackexchange.com/questions/35490/xts-and-256-bit-data-blocks
Which means the constant should be 0x425 (for ...x^2+1), not 0x423 (which is the constant for ...x+1)

See also this thread: http://sci.crypt.narkive.com/3lS5EbY4/omac-cmac-constants-for-different-block-sizes
-> 0x423 (x^256 + x^10 + x^5 + x + 1) is not irreducible.

@noloader
Copy link
Collaborator Author

Thanks @kerukuro.

Crud, it looks like a typo on out part. I think we need this change:

$ git diff cmac.cpp
diff --git a/cmac.cpp b/cmac.cpp
index 1b56662..ed56b10 100644
--- a/cmac.cpp
+++ b/cmac.cpp
@@ -32,9 +32,9 @@ static void MulU(byte *k, unsigned int length)
                        break;
                case 32:
                        // https://crypto.stackexchange.com/q/9815/10496
-                       // Polynomial x^256 + x^10 + x^5 + x + 1
+                       // Polynomial x^256 + x^10 + x^5 + x^2 + 1^M
                        k[30] ^= 4;
-                       k[31] ^= 0x23;
+                       k[31] ^= 0x25;^M
                        break;
                case 64:
                        // https://crypto.stackexchange.com/q/9815/10496

I was suspect of that for a long time. I think I convinced myself it was correct when it was wrong. My apologies for that.

Before I check it in, can you confirm it?

@kerukuro
Copy link

This looks like the correct fix. Thanks.

(FWIW I've also found an online gp calculator where one can test if a polynomial is irreducible using commands from the sci.crypt thread)

noloader added a commit that referenced this issue Sep 13, 2017
@noloader
Copy link
Collaborator Author

@kerukuro,

Thanks. Committed at fca8adc54976.

@noloader
Copy link
Collaborator Author

Thanks to @kerukuro we got the polynomials straitened out. Closing this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants