Initialize XCBC-MAC state @param xcbc [out] XCBC state to initialize @param cipher Index of cipher to use @param key [in] Secret key @param keylen Length of secret key in octets Return CRYPT_OK on success */
| 23 | Return CRYPT_OK on success |
| 24 | */ |
| 25 | int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen) |
| 26 | { |
| 27 | int x, y, err; |
| 28 | symmetric_key *skey; |
| 29 | unsigned long k1; |
| 30 | |
| 31 | LTC_ARGCHK(xcbc != NULL); |
| 32 | LTC_ARGCHK(key != NULL); |
| 33 | |
| 34 | /* schedule the key */ |
| 35 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { |
| 36 | return err; |
| 37 | } |
| 38 | |
| 39 | #ifdef LTC_FAST |
| 40 | if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) { |
| 41 | return CRYPT_INVALID_ARG; |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | skey = NULL; |
| 46 | |
| 47 | /* are we in pure XCBC mode with three keys? */ |
| 48 | if (keylen & LTC_XCBC_PURE) { |
| 49 | keylen &= ~LTC_XCBC_PURE; |
| 50 | |
| 51 | if (keylen < 2UL*cipher_descriptor[cipher].block_length) { |
| 52 | return CRYPT_INVALID_ARG; |
| 53 | } |
| 54 | |
| 55 | k1 = keylen - 2*cipher_descriptor[cipher].block_length; |
| 56 | XMEMCPY(xcbc->K[0], key, k1); |
| 57 | XMEMCPY(xcbc->K[1], key+k1, cipher_descriptor[cipher].block_length); |
| 58 | XMEMCPY(xcbc->K[2], key+k1 + cipher_descriptor[cipher].block_length, cipher_descriptor[cipher].block_length); |
| 59 | } else { |
| 60 | /* use the key expansion */ |
| 61 | k1 = cipher_descriptor[cipher].block_length; |
| 62 | |
| 63 | /* schedule the user key */ |
| 64 | skey = XCALLOC(1, sizeof(*skey)); |
| 65 | if (skey == NULL) { |
| 66 | return CRYPT_MEM; |
| 67 | } |
| 68 | |
| 69 | if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) { |
| 70 | goto done; |
| 71 | } |
| 72 | |
| 73 | /* make the three keys */ |
| 74 | for (y = 0; y < 3; y++) { |
| 75 | for (x = 0; x < cipher_descriptor[cipher].block_length; x++) { |
| 76 | xcbc->K[y][x] = y + 1; |
| 77 | } |
| 78 | cipher_descriptor[cipher].ecb_encrypt(xcbc->K[y], xcbc->K[y], skey); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* setup K1 */ |
no test coverage detected