2 *@memo Implementation of the modified counter mode
4 *@author A. MAGNIEZ (FT R&D - DTL/SSR)
6 * Copyright 2006 The FreeRADIUS server project
9 #include <freeradius-devel/ident.h>
18 *@memo default constructor
20 SOBMMO::SOBMMO():sizeBlock(0),nbOutputBlocks(0),outputBlocks(NULL) {
24 *@memo default destructor
27 if(outputBlocks!=NULL) {
33 *@memo this function initializes the modified counter mode
34 *@param K, the dedicated key (its size must be equal to the block size of E)
35 *@param E, the block cipher context
36 *@param inputBlock, the input block (its size must be equal to the block size of E)
37 *@param nb, the number of wanted output blocks
38 *@param counterValues, the counter values (its size must be nbOutputBlock*sizeBlock)
39 *@return 1 if the output blocks have been produced, 0 in the other cases.
41 int SOBMMO::initialize(const byte* K, BlockCipher* E,const byte* inputBlock,int nb,const byte* counterValues){
46 sizeBlock=E->blockSize();
50 // allocate memory for the output blocks
51 outputBlocks=(byte *)malloc(sizeBlock*nbOutputBlocks);
52 if(outputBlocks==NULL){
57 pskConvertHex((char *)K, (char *)&hexstr, sizeBlock);
58 DEBUG2("SOBMMO::initialize: K=");
59 DEBUG2((char *)&hexstr);
61 pskConvertHex((char *)inputBlock, (char *)&hexstr, sizeBlock);
62 DEBUG2("SOBMMO::initialize: inputBlock=");
63 DEBUG2((char *)&hexstr);
65 pskConvertHex((char *)counterValues, (char *)&hexstr, sizeBlock*nbOutputBlocks);
66 DEBUG2("SOBMMO::initialize: counterValues=");
67 DEBUG2((char *)&hexstr);
69 E->makeKey(K,sizeBlock,DIR_ENCRYPT);
70 E->encrypt(inputBlock,outputBlocks);
72 // duplicate the first result
73 for(i=1;i<nbOutputBlocks;i++)
75 memcpy(outputBlocks+i*sizeBlock,outputBlocks,sizeBlock);
78 pskConvertHex((char *)outputBlocks, (char *)&hexstr, nbOutputBlocks*sizeBlock);
79 DEBUG2("SOBMMO::initialize: outputBlocks before XOR=");
80 DEBUG2((char *)&hexstr);
83 for(i=0;i<(nbOutputBlocks*sizeBlock);i++)
85 *(outputBlocks+i)=(*(outputBlocks+i))^(*(counterValues+i));
88 pskConvertHex((char *)outputBlocks, (char *)&hexstr, nbOutputBlocks*sizeBlock);
89 DEBUG2("SOBMMO::initialize: outputBlocks after XOR=");
90 DEBUG2((char *)&hexstr);
92 // in order to check that AES(K,M) is valid
93 E->encrypt(outputBlocks,buf);
94 pskConvertHex((char *)buf, (char *)&hexstr, 16);
95 DEBUG2("SOBMMO::initialize: buf=");
96 DEBUG2((char *)&hexstr);
98 // produce each output block
99 for(i=0;i<nbOutputBlocks;i++)
101 E->encrypt(outputBlocks+i*sizeBlock,outputBlocks+i*sizeBlock); // Be careful, pt=ct !!! TBTested
104 pskConvertHex((char *)outputBlocks, (char *)&hexstr, nbOutputBlocks*sizeBlock);
105 DEBUG2("SOBMMO::initialize: produced output blocks=");
106 DEBUG2((char *)&hexstr);
114 *@memo this function returns an output block
115 *@param id, the number of the wanted output block (the numerotation begins at 1 !!)
117 byte* SOBMMO::getOutputBlock(int id){
120 if(id<1 || id>nbOutputBlocks) {
124 output=(byte*)malloc(sizeBlock);
128 memcpy(output,outputBlocks+(id-1)*sizeBlock,sizeBlock);