1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 OCB implementation, initialize state, by Tom St Denis
20 unsigned char poly_div[MAXBLOCKSIZE],
21 poly_mul[MAXBLOCKSIZE];
25 { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D },
26 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B }
29 { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43 },
31 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87 }
37 Initialize an OCB context.
38 @param ocb [out] The destination of the OCB state
39 @param cipher The index of the desired cipher
40 @param key The secret key
41 @param keylen The length of the secret key (octets)
42 @param nonce The session nonce (length of the block size of the cipher)
43 @return CRYPT_OK if successful
45 int ocb_init(ocb_state *ocb, int cipher,
46 const unsigned char *key, unsigned long keylen, const unsigned char *nonce)
48 int poly, x, y, m, err;
50 LTC_ARGCHK(ocb != NULL);
51 LTC_ARGCHK(key != NULL);
52 LTC_ARGCHK(nonce != NULL);
55 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
59 /* determine which polys to use */
60 ocb->block_len = cipher_descriptor[cipher].block_length;
61 x = (int)(sizeof(polys)/sizeof(polys[0]));
62 for (poly = 0; poly < x; poly++) {
63 if (polys[poly].len == ocb->block_len) {
68 return CRYPT_INVALID_ARG; /* block_len not found in polys */
70 if (polys[poly].len != ocb->block_len) {
71 return CRYPT_INVALID_ARG;
74 /* schedule the key */
75 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) {
80 zeromem(ocb->L, ocb->block_len);
81 if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) {
85 /* find R = E[N xor L] */
86 for (x = 0; x < ocb->block_len; x++) {
87 ocb->R[x] = ocb->L[x] ^ nonce[x];
89 if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) {
93 /* find Ls[i] = L << i for i == 0..31 */
94 XMEMCPY(ocb->Ls[0], ocb->L, ocb->block_len);
95 for (x = 1; x < 32; x++) {
96 m = ocb->Ls[x-1][0] >> 7;
97 for (y = 0; y < ocb->block_len-1; y++) {
98 ocb->Ls[x][y] = ((ocb->Ls[x-1][y] << 1) | (ocb->Ls[x-1][y+1] >> 7)) & 255;
100 ocb->Ls[x][ocb->block_len-1] = (ocb->Ls[x-1][ocb->block_len-1] << 1) & 255;
103 for (y = 0; y < ocb->block_len; y++) {
104 ocb->Ls[x][y] ^= polys[poly].poly_mul[y];
109 /* find Lr = L / x */
110 m = ocb->L[ocb->block_len-1] & 1;
113 for (x = ocb->block_len - 1; x > 0; x--) {
114 ocb->Lr[x] = ((ocb->L[x] >> 1) | (ocb->L[x-1] << 7)) & 255;
116 ocb->Lr[0] = ocb->L[0] >> 1;
119 for (x = 0; x < ocb->block_len; x++) {
120 ocb->Lr[x] ^= polys[poly].poly_div[x];
124 /* set Li, checksum */
125 zeromem(ocb->Li, ocb->block_len);
126 zeromem(ocb->checksum, ocb->block_len);
128 /* set other params */
129 ocb->block_index = 1;
130 ocb->cipher = cipher;
137 /* ref: $Format:%D$ */
138 /* git commit: $Format:%H$ */
139 /* commit time: $Format:%ai$ */