~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/eax_init.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
 
 *
3
 
 * LibTomCrypt is a library that provides various cryptographic
4
 
 * algorithms in a highly modular and flexible manner.
5
 
 *
6
 
 * The library is free for all purposes without any express
7
 
 * guarantee it works.
8
 
 *
9
 
 * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
10
 
 */
11
 
 
12
 
/* EAX Implementation by Tom St Denis */
13
 
#include "mycrypt.h"
14
 
 
15
 
#ifdef EAX_MODE
16
 
 
17
 
int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
18
 
             const unsigned char *nonce, unsigned long noncelen,
19
 
             const unsigned char *header, unsigned long headerlen)
20
 
{
21
 
   unsigned char *buf;
22
 
   int           err, blklen;
23
 
   omac_state    *omac;
24
 
   unsigned long len;
25
 
 
26
 
 
27
 
   _ARGCHK(eax   != NULL);
28
 
   _ARGCHK(key   != NULL);
29
 
   _ARGCHK(nonce != NULL);
30
 
   if (headerlen > 0) {
31
 
      _ARGCHK(header != NULL);
32
 
   }
33
 
 
34
 
   if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
35
 
      return err;
36
 
   }
37
 
   blklen = cipher_descriptor[cipher].block_length;
38
 
 
39
 
   /* allocate ram */
40
 
   buf  = XMALLOC(MAXBLOCKSIZE);
41
 
   omac = XMALLOC(sizeof(omac_state));
42
 
 
43
 
   if (buf == NULL || omac == NULL) {
44
 
      if (buf != NULL) {
45
 
         XFREE(buf);
46
 
      }
47
 
      if (omac != NULL) {
48
 
         XFREE(omac);
49
 
      }
50
 
      return CRYPT_MEM;
51
 
   }
52
 
 
53
 
   /* N = OMAC_0K(nonce) */
54
 
   zeromem(buf, MAXBLOCKSIZE);
55
 
   if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
56
 
      goto __ERR; 
57
 
   }
58
 
 
59
 
   /* omac the [0]_n */
60
 
   if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
61
 
      goto __ERR; 
62
 
   }
63
 
   /* omac the nonce */
64
 
   if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
65
 
      goto __ERR; 
66
 
   }
67
 
   /* store result */
68
 
   len = sizeof(eax->N);
69
 
   if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
70
 
      goto __ERR; 
71
 
   }
72
 
 
73
 
   /* H = OMAC_1K(header) */
74
 
   zeromem(buf, MAXBLOCKSIZE);
75
 
   buf[blklen - 1] = 1;
76
 
 
77
 
   if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
78
 
      goto __ERR; 
79
 
   }
80
 
 
81
 
   /* omac the [1]_n */
82
 
   if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
83
 
      goto __ERR; 
84
 
   }
85
 
   /* omac the header */
86
 
   if (headerlen != 0) {
87
 
      if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
88
 
          goto __ERR; 
89
 
      }
90
 
   }
91
 
 
92
 
   /* note we don't finish the headeromac, this allows us to add more header later */
93
 
 
94
 
   /* setup the CTR mode */
95
 
   if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) {
96
 
      goto __ERR; 
97
 
   }
98
 
   /* use big-endian counter */
99
 
   eax->ctr.mode = 1;
100
 
 
101
 
   /* setup the OMAC for the ciphertext */
102
 
   if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) { 
103
 
      goto __ERR; 
104
 
   }
105
 
 
106
 
   /* omac [2]_n */
107
 
   zeromem(buf, MAXBLOCKSIZE);
108
 
   buf[blklen-1] = 2;
109
 
   if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
110
 
      goto __ERR; 
111
 
   }
112
 
 
113
 
   err = CRYPT_OK;
114
 
__ERR:
115
 
#ifdef CLEAN_STACK
116
 
   zeromem(buf,  MAXBLOCKSIZE);
117
 
   zeromem(omac, sizeof(omac_state));
118
 
#endif
119
 
 
120
 
   XFREE(omac);
121
 
   XFREE(buf);
122
 
 
123
 
   return err;
124
 
}
125
 
 
126
 
#endif