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
18 static void _ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *nonce, unsigned long noncelen, unsigned long taglen)
22 unsigned char iNonce[MAXBLOCKSIZE];
23 unsigned char iKtop[MAXBLOCKSIZE];
24 unsigned char iStretch[MAXBLOCKSIZE+8];
26 /* Nonce = zeros(127-bitlen(N)) || 1 || N */
27 zeromem(iNonce, sizeof(iNonce));
28 for (x = ocb->block_len-1, y=0; y<(int)noncelen; x--, y++) {
29 iNonce[x] = nonce[noncelen-y-1];
32 iNonce[0] |= ((taglen*8) % 128) << 1;
34 /* bottom = str2num(Nonce[123..128]) */
35 bottom = iNonce[ocb->block_len-1] & 0x3F;
37 /* Ktop = ENCIPHER(K, Nonce[1..122] || zeros(6)) */
38 iNonce[ocb->block_len-1] = iNonce[ocb->block_len-1] & 0xC0;
39 if ((cipher_descriptor[ocb->cipher].ecb_encrypt(iNonce, iKtop, &ocb->key)) != CRYPT_OK) {
40 zeromem(ocb->Offset_current, ocb->block_len);
44 /* Stretch = Ktop || (Ktop[1..64] xor Ktop[9..72]) */
45 for (x = 0; x < ocb->block_len; x++) {
46 iStretch[x] = iKtop[x];
48 for (y = 0; y < 8; y++) {
49 iStretch[x+y] = iKtop[y] ^ iKtop[y+1];
52 /* Offset_0 = Stretch[1+bottom..128+bottom] */
55 for (x = 0; x < ocb->block_len; x++) {
56 ocb->Offset_current[x] = iStretch[idx+x] << shift;
58 ocb->Offset_current[x] |= iStretch[idx+x+1] >> (8-shift);
65 unsigned char poly_mul[MAXBLOCKSIZE];
69 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B }
72 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87 }
78 Initialize an OCB context
79 @param ocb [out] The destination of the OCB state
80 @param cipher The index of the desired cipher
81 @param key The secret key
82 @param keylen The length of the secret key (octets)
83 @param nonce The session nonce
84 @param noncelen The length of the session nonce (octets, up to 15)
85 @param taglen The length of the tag (octets, up to 16)
86 @return CRYPT_OK if successful
88 int ocb3_init(ocb3_state *ocb, int cipher,
89 const unsigned char *key, unsigned long keylen,
90 const unsigned char *nonce, unsigned long noncelen,
93 int poly, x, y, m, err;
94 unsigned char *previous, *current;
96 LTC_ARGCHK(ocb != NULL);
97 LTC_ARGCHK(key != NULL);
98 LTC_ARGCHK(nonce != NULL);
101 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
104 ocb->cipher = cipher;
107 * As of RFC7253: "string of no more than 120 bits" */
108 if (noncelen > (120/8)) {
109 return CRYPT_INVALID_ARG;
112 /* The blockcipher must have a 128-bit blocksize */
113 if (cipher_descriptor[cipher].block_length != 16) {
114 return CRYPT_INVALID_ARG;
117 /* The TAGLEN may be any value up to 128 (bits) */
119 return CRYPT_INVALID_ARG;
121 ocb->tag_len = taglen;
123 /* determine which polys to use */
124 ocb->block_len = cipher_descriptor[cipher].block_length;
125 x = (int)(sizeof(polys)/sizeof(polys[0]));
126 for (poly = 0; poly < x; poly++) {
127 if (polys[poly].len == ocb->block_len) {
132 return CRYPT_INVALID_ARG; /* block_len not found in polys */
134 if (polys[poly].len != ocb->block_len) {
135 return CRYPT_INVALID_ARG;
138 /* schedule the key */
139 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) {
143 /* L_* = ENCIPHER(K, zeros(128)) */
144 zeromem(ocb->L_star, ocb->block_len);
145 if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L_star, ocb->L_star, &ocb->key)) != CRYPT_OK) {
149 /* compute L_$, L_0, L_1, ... */
150 for (x = -1; x < 32; x++) {
151 if (x == -1) { /* gonna compute: L_$ = double(L_*) */
152 current = ocb->L_dollar;
153 previous = ocb->L_star;
155 else if (x == 0) { /* gonna compute: L_0 = double(L_$) */
156 current = ocb->L_[0];
157 previous = ocb->L_dollar;
159 else { /* gonna compute: L_i = double(L_{i-1}) for every integer i > 0 */
160 current = ocb->L_[x];
161 previous = ocb->L_[x-1];
163 m = previous[0] >> 7;
164 for (y = 0; y < ocb->block_len-1; y++) {
165 current[y] = ((previous[y] << 1) | (previous[y+1] >> 7)) & 255;
167 current[ocb->block_len-1] = (previous[ocb->block_len-1] << 1) & 255;
169 /* current[] = current[] XOR polys[poly].poly_mul[]*/
170 ocb3_int_xor_blocks(current, current, polys[poly].poly_mul, ocb->block_len);
174 /* initialize ocb->Offset_current = Offset_0 */
175 _ocb3_int_calc_offset_zero(ocb, nonce, noncelen, taglen);
177 /* initialize checksum to all zeros */
178 zeromem(ocb->checksum, ocb->block_len);
180 /* set block index */
181 ocb->block_index = 1;
183 /* initialize AAD related stuff */
184 ocb->ablock_index = 1;
185 ocb->adata_buffer_bytes = 0;
186 zeromem(ocb->aOffset_current, ocb->block_len);
187 zeromem(ocb->aSum_current, ocb->block_len);
194 /* ref: $Format:%D$ */
195 /* git commit: $Format:%H$ */
196 /* commit time: $Format:%ai$ */