Back to index

lightning-sunbird  0.9+nobinonly
desblapi.c
Go to the documentation of this file.
00001 /*
00002  *  desblapi.c
00003  *
00004  *  core source file for DES-150 library
00005  *  Implement DES Modes of Operation and Triple-DES.
00006  *  Adapt DES-150 to blapi API.
00007  *
00008  * ***** BEGIN LICENSE BLOCK *****
00009  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00010  *
00011  * The contents of this file are subject to the Mozilla Public License Version
00012  * 1.1 (the "License"); you may not use this file except in compliance with
00013  * the License. You may obtain a copy of the License at
00014  * http://www.mozilla.org/MPL/
00015  *
00016  * Software distributed under the License is distributed on an "AS IS" basis,
00017  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00018  * for the specific language governing rights and limitations under the
00019  * License.
00020  *
00021  * The Original Code is the DES-150 library.
00022  *
00023  * The Initial Developer of the Original Code is
00024  * Nelson B. Bolyard, nelsonb@iname.com.
00025  * Portions created by the Initial Developer are Copyright (C) 1990
00026  * the Initial Developer. All Rights Reserved.
00027  *
00028  * Contributor(s):
00029  *
00030  * Alternatively, the contents of this file may be used under the terms of
00031  * either the GNU General Public License Version 2 or later (the "GPL"), or
00032  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00033  * in which case the provisions of the GPL or the LGPL are applicable instead
00034  * of those above. If you wish to allow use of your version of this file only
00035  * under the terms of either the GPL or the LGPL, and not to allow others to
00036  * use your version of this file under the terms of the MPL, indicate your
00037  * decision by deleting the provisions above and replace them with the notice
00038  * and other provisions required by the GPL or the LGPL. If you do not delete
00039  * the provisions above, a recipient may use your version of this file under
00040  * the terms of any one of the MPL, the GPL or the LGPL.
00041  *
00042  * ***** END LICENSE BLOCK ***** */
00043 
00044 #include "des.h"
00045 #include <stddef.h>
00046 #include "secerr.h"
00047 
00048 #if defined(_X86_)
00049 /* Intel X86 CPUs do unaligned loads and stores without complaint. */
00050 #define COPY8B(to, from, ptr) \
00051        HALFPTR(to)[0] = HALFPTR(from)[0]; \
00052        HALFPTR(to)[1] = HALFPTR(from)[1]; 
00053 #elif defined(USE_MEMCPY)
00054 #define COPY8B(to, from, ptr) memcpy(to, from, 8)
00055 #else
00056 #define COPY8B(to, from, ptr) \
00057     if (((ptrdiff_t)(ptr) & 0x3) == 0) { \
00058        HALFPTR(to)[0] = HALFPTR(from)[0]; \
00059        HALFPTR(to)[1] = HALFPTR(from)[1]; \
00060     } else if (((ptrdiff_t)(ptr) & 0x1) == 0) { \
00061        SHORTPTR(to)[0] = SHORTPTR(from)[0]; \
00062        SHORTPTR(to)[1] = SHORTPTR(from)[1]; \
00063        SHORTPTR(to)[2] = SHORTPTR(from)[2]; \
00064        SHORTPTR(to)[3] = SHORTPTR(from)[3]; \
00065     } else { \
00066        BYTEPTR(to)[0] = BYTEPTR(from)[0]; \
00067        BYTEPTR(to)[1] = BYTEPTR(from)[1]; \
00068        BYTEPTR(to)[2] = BYTEPTR(from)[2]; \
00069        BYTEPTR(to)[3] = BYTEPTR(from)[3]; \
00070        BYTEPTR(to)[4] = BYTEPTR(from)[4]; \
00071        BYTEPTR(to)[5] = BYTEPTR(from)[5]; \
00072        BYTEPTR(to)[6] = BYTEPTR(from)[6]; \
00073        BYTEPTR(to)[7] = BYTEPTR(from)[7]; \
00074     } 
00075 #endif
00076 #define COPY8BTOHALF(to, from) COPY8B(to, from, from)
00077 #define COPY8BFROMHALF(to, from) COPY8B(to, from, to)
00078 
00079 static void 
00080 DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
00081 {
00082     while (len) {
00083        DES_Do1Block(cx->ks0, in, out);
00084        len -= 8;
00085        in  += 8;
00086        out += 8;
00087     }
00088 }
00089 
00090 static void 
00091 DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
00092 {
00093     while (len) {
00094        DES_Do1Block(cx->ks0,  in, out);
00095        len -= 8;
00096        in  += 8;
00097        DES_Do1Block(cx->ks1, out, out);
00098        DES_Do1Block(cx->ks2, out, out);
00099        out += 8;
00100     }
00101 }
00102 
00103 static void 
00104 DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
00105 {
00106     const BYTE * bufend = in + len;
00107     HALF  vec[2];
00108 
00109     while (in != bufend) {
00110        COPY8BTOHALF(vec, in);
00111        in += 8;
00112        vec[0] ^= cx->iv[0];
00113        vec[1] ^= cx->iv[1];
00114        DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
00115        COPY8BFROMHALF(out, cx->iv);
00116        out += 8;
00117     }
00118 }
00119 
00120 static void 
00121 DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
00122 {
00123     const BYTE * bufend;
00124     HALF oldciphertext[2];
00125     HALF plaintext    [2];
00126 
00127     for (bufend = in + len; in != bufend; ) {
00128        oldciphertext[0] = cx->iv[0];
00129        oldciphertext[1] = cx->iv[1];
00130        COPY8BTOHALF(cx->iv, in);
00131        in += 8;
00132        DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
00133        plaintext[0] ^= oldciphertext[0];
00134        plaintext[1] ^= oldciphertext[1];
00135        COPY8BFROMHALF(out, plaintext);
00136        out += 8;
00137     }
00138 }
00139 
00140 static void 
00141 DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
00142 {
00143     const BYTE * bufend = in + len;
00144     HALF  vec[2];
00145 
00146     while (in != bufend) {
00147        COPY8BTOHALF(vec, in);
00148        in += 8;
00149        vec[0] ^= cx->iv[0];
00150        vec[1] ^= cx->iv[1];
00151        DES_Do1Block( cx->ks0, (BYTE *)vec,    (BYTE *)cx->iv);
00152        DES_Do1Block( cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv);
00153        DES_Do1Block( cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv);
00154        COPY8BFROMHALF(out, cx->iv);
00155        out += 8;
00156     }
00157 }
00158 
00159 static void 
00160 DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
00161 {
00162     const BYTE * bufend;
00163     HALF oldciphertext[2];
00164     HALF plaintext    [2];
00165 
00166     for (bufend = in + len; in != bufend; ) {
00167        oldciphertext[0] = cx->iv[0];
00168        oldciphertext[1] = cx->iv[1];
00169        COPY8BTOHALF(cx->iv, in);
00170        in += 8;
00171        DES_Do1Block(cx->ks0, (BYTE *)cx->iv,    (BYTE *)plaintext);
00172        DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext);
00173        DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext);
00174        plaintext[0] ^= oldciphertext[0];
00175        plaintext[1] ^= oldciphertext[1];
00176        COPY8BFROMHALF(out, plaintext);
00177        out += 8;
00178     }
00179 }
00180 
00181 DESContext *
00182 DES_AllocateContext(void)
00183 {
00184     return PORT_ZNew(DESContext);
00185 }
00186 
00187 SECStatus   
00188 DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen,
00189                const unsigned char *iv, int mode, unsigned int encrypt,
00190                unsigned int unused)
00191 {
00192     DESDirection opposite;
00193     if (!cx) {
00194        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00195        return SECFailure;
00196     }
00197     cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT;
00198     opposite      = encrypt ? DES_DECRYPT : DES_ENCRYPT;
00199     switch (mode) {
00200     case NSS_DES:    /* DES ECB */
00201        DES_MakeSchedule( cx->ks0, key, cx->direction);
00202        cx->worker = &DES_ECB;
00203        break;
00204 
00205     case NSS_DES_EDE3:      /* DES EDE ECB */
00206        cx->worker = &DES_EDE3_ECB;
00207        if (encrypt) {
00208            DES_MakeSchedule(cx->ks0, key,      cx->direction);
00209            DES_MakeSchedule(cx->ks1, key +  8, opposite);
00210            DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
00211        } else {
00212            DES_MakeSchedule(cx->ks2, key,      cx->direction);
00213            DES_MakeSchedule(cx->ks1, key +  8, opposite);
00214            DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
00215        }
00216        break;
00217 
00218     case NSS_DES_CBC:       /* DES CBC */
00219        COPY8BTOHALF(cx->iv, iv);
00220        cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe;
00221        DES_MakeSchedule(cx->ks0, key, cx->direction);
00222        break;
00223 
00224     case NSS_DES_EDE3_CBC:  /* DES EDE CBC */
00225        COPY8BTOHALF(cx->iv, iv);
00226        if (encrypt) {
00227            cx->worker = &DES_EDE3CBCEn;
00228            DES_MakeSchedule(cx->ks0, key,      cx->direction);
00229            DES_MakeSchedule(cx->ks1, key +  8, opposite);
00230            DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
00231        } else {
00232            cx->worker = &DES_EDE3CBCDe;
00233            DES_MakeSchedule(cx->ks2, key,      cx->direction);
00234            DES_MakeSchedule(cx->ks1, key +  8, opposite);
00235            DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
00236        }
00237        break;
00238 
00239     default:
00240        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00241        return SECFailure;
00242     }
00243     return SECSuccess;
00244 }
00245 
00246 DESContext *
00247 DES_CreateContext(const BYTE * key, const BYTE *iv, int mode, PRBool encrypt)
00248 {
00249     DESContext *cx = PORT_ZNew(DESContext);
00250     SECStatus rv   = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0);
00251 
00252     if (rv != SECSuccess) {
00253        PORT_ZFree(cx, sizeof *cx);
00254        cx = NULL;
00255     }
00256     return cx;
00257 }
00258 
00259 void
00260 DES_DestroyContext(DESContext *cx, PRBool freeit)
00261 {
00262     if (cx) {
00263        memset(cx, 0, sizeof *cx);
00264        if (freeit)
00265            PORT_Free(cx);
00266     }
00267 }
00268 
00269 SECStatus
00270 DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
00271             unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
00272 {
00273 
00274     if (inLen < 0 || (inLen % 8) != 0 || maxOutLen < inLen || !cx || 
00275         cx->direction != DES_ENCRYPT) {
00276        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00277        return SECFailure;
00278     }
00279 
00280     cx->worker(cx, out, in, inLen);
00281     if (outLen)
00282        *outLen = inLen;
00283     return SECSuccess;
00284 }
00285 
00286 SECStatus
00287 DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
00288             unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
00289 {
00290 
00291     if (inLen < 0 || (inLen % 8) != 0 || maxOutLen < inLen || !cx || 
00292         cx->direction != DES_DECRYPT) {
00293        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00294        return SECFailure;
00295     }
00296 
00297     cx->worker(cx, out, in, inLen);
00298     if (outLen)
00299        *outLen = inLen;
00300     return SECSuccess;
00301 }