/* * This encrypts using the Cipher Block Chaining mode of DES */ int cbc_encode(char *msgbuf, int n, FILE *fp) { int inverse = 0; /* 0 to encrypt, 1 to decrypt */ /* * do the transformation */ if (n == 8) { for (n = 0; n < 8; n++) msgbuf[n] ^= ivec[n]; DES_XFORM((DES_cblock *)msgbuf); MEMCPY(ivec, msgbuf, 8); return WRITE(msgbuf, 8, fp); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ /* MEMZERO(msgbuf + n, 8 - n); */ /* * Pad the last block randomly */ MEMCPY(msgbuf + n, pvec, 8 - n); msgbuf[7] = n; for (n = 0; n < 8; n++) msgbuf[n] ^= ivec[n]; DES_XFORM((DES_cblock *)msgbuf); return WRITE(msgbuf, 8, fp); }
/* * This encrypts using the Cipher Block Chaining mode of DES */ static void cbcenc(void) { int n; /* number of bytes actually read */ int bn; /* block number */ DES_cblock msgbuf; /* I/O buffer */ /* * do the transformation */ for (bn = 1; (n = READ(msgbuf, 8)) == 8; bn++) { for (n = 0; n < 8; n++) msgbuf[n] ^= ivec[n]; DES_XFORM(&msgbuf); MEMCPY(ivec, msgbuf, 8); WRITE(msgbuf, 8); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&msgbuf[n], 8 - n); msgbuf[7] = n; for (n = 0; n < 8; n++) msgbuf[n] ^= ivec[n]; DES_XFORM(&msgbuf); WRITE(msgbuf, 8); }
/* * This encrypts using the Cipher Block Chaining mode of DES */ int cbc_encode(char *msgbuf, int n, FILE *fp) { int inverse = 0; /* 0 to encrypt, 1 to decrypt */ /* * do the transformation */ if (n == 8) { for (n = 0; n < 8; n++) CHAR(msgbuf, n) ^= CHAR(ivec, n); DES_XFORM(UBUFFER(msgbuf)); MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8); return WRITE(BUFFER(msgbuf), 8, fp); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ /* MEMZERO(msgbuf + n, 8 - n); */ /* * Pad the last block randomly */ (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n); CHAR(msgbuf, 7) = n; for (n = 0; n < 8; n++) CHAR(msgbuf, n) ^= CHAR(ivec, n); DES_XFORM(UBUFFER(msgbuf)); return WRITE(BUFFER(msgbuf), 8, fp); }
/* * This decrypts using the Cipher Block Chaining mode of DES */ static void cbcdec(void) { int n; /* number of bytes actually read */ DES_cblock msgbuf; /* I/O buffer */ DES_cblock ibuf; /* temp buffer for initialization vector */ int c; /* used to test for EOF */ int bn; /* block number */ for (bn = 0; (n = READ(msgbuf, 8)) == 8; bn++) { /* * do the transformation */ MEMCPY(ibuf, msgbuf, 8); DES_XFORM(&msgbuf); for (c = 0; c < 8; c++) msgbuf[c] ^= ivec[c]; MEMCPY(ivec, ibuf, 8); /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = msgbuf[7]; if (n < 0 || n > 7) warnx("decryption failed (block corrupt) at %d", bn); } else (void)ungetc(c, stdin); WRITE(msgbuf, n); } if (n > 0) warnx("decryption failed (incomplete block) at %d", bn); }
/* * This decrypts using the Electronic Code Book mode of DES */ static void ecbdec(void) { int n; /* number of bytes actually read */ int c; /* used to test for EOF */ int bn; /* block number */ DES_cblock msgbuf; /* I/O buffer */ for (bn = 1; (n = READ(msgbuf, 8)) == 8; bn++) { /* * do the transformation */ DES_XFORM(&msgbuf); /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = msgbuf[7]; if (n < 0 || n > 7) warnx("decryption failed (block corrupt) at %d", bn); } else (void)ungetc(c, stdin); WRITE(msgbuf, n); } if (n > 0) warnx("decryption failed (incomplete block) at %d", bn); }
/* * This authenticates using the Cipher FeedBack mode of DES */ static void cfbauth(void) { int n, j; /* number of bytes actually read */ int nbytes; /* number of bytes to read */ char ibuf[8]; /* input buffer */ DES_cblock msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ while ((n = READ(ibuf, nbytes)) == nbytes) { MEMCPY(msgbuf, ivec, 8); DES_XFORM(&msgbuf); for (n = 0; n < 8 - nbytes; n++) ivec[n] = ivec[n + nbytes]; for (n = 0; n < nbytes; n++) ivec[8 - nbytes + n] = ibuf[n] ^ msgbuf[n]; } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ MEMZERO(&ibuf[n], nbytes - n); ibuf[nbytes - 1] = '0' + n; MEMCPY(msgbuf, ivec, 8); DES_XFORM(&msgbuf); for (n = 0; n < nbytes; n++) ibuf[n] ^= msgbuf[n]; /* * drop the bits * we write chars until fewer than 7 bits, * and then pad the last one with 0 bits */ for (n = 0; macbits > 7; n++, macbits -= 8) (void)putchar(msgbuf[n]); if (macbits > 0) { msgbuf[0] = 0x00; for (j = 0; j < macbits; j++) msgbuf[0] |= msgbuf[n] & bits[j]; (void)putchar(msgbuf[0]); } }
/* * This encrypts using the Output FeedBack mode of DES */ static void ofbenc(void) { int n; /* number of bytes actually read */ int c; /* used to test for EOF */ int nbytes; /* number of bytes to read */ int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ DES_cblock msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(msgbuf, ivec, 8); DES_XFORM(&msgbuf); for (n = 0; n < 8 - nbytes; n++) ivec[n] = ivec[n + nbytes]; for (n = 0; n < nbytes; n++) { ivec[8 - nbytes + n] = msgbuf[n]; obuf[n] = ibuf[n] ^ msgbuf[n]; } WRITE(obuf, nbytes); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&ibuf[n], nbytes - n); ibuf[nbytes - 1] = n; MEMCPY(msgbuf, ivec, 8); DES_XFORM(&msgbuf); for (c = 0; c < nbytes; c++) ibuf[c] ^= msgbuf[c]; WRITE(ibuf, nbytes); }
/* * This authenticates using the Cipher Block Chaining mode of DES */ static void cbcauth(void) { int n, j; /* number of bytes actually read */ DES_cblock msgbuf; /* I/O buffer */ DES_cblock encbuf; /* encryption buffer */ /* * do the transformation * note we DISCARD the encrypted block; * we only care about the last one */ while ((n = READ(msgbuf, 8)) == 8) { for (n = 0; n < 8; n++) encbuf[n] = msgbuf[n] ^ ivec[n]; DES_XFORM(&encbuf); MEMCPY(ivec, encbuf, 8); } /* * now compute the last one, right padding with '\0' if need be */ if (n > 0) { MEMZERO(&msgbuf[n], 8 - n); for (n = 0; n < 8; n++) encbuf[n] = msgbuf[n] ^ ivec[n]; DES_XFORM(&encbuf); } /* * drop the bits * we write chars until fewer than 7 bits, * and then pad the last one with 0 bits */ for (n = 0; macbits > 7; n++, macbits -= 8) (void)putchar(encbuf[n]); if (macbits > 0) { msgbuf[0] = 0x00; for (j = 0; j < macbits; j++) msgbuf[0] |= encbuf[n] & bits[j]; (void)putchar(msgbuf[0]); } }
/* * This decrypts using the Output Block Chaining mode of DES */ static void ofbdec(void) { int n; /* number of bytes actually read */ int c; /* used to test for EOF */ int nbytes; /* number of bytes to read */ int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ DES_cblock msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(msgbuf, ivec, 8); DES_XFORM(&msgbuf); for (c = 0; c < 8 - nbytes; c++) ivec[c] = ivec[c + nbytes]; for (c = 0; c < nbytes; c++) { ivec[8 - nbytes + c] = msgbuf[c]; obuf[c] = ibuf[c] ^ msgbuf[c]; } /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = obuf[nbytes-1]; if (n < 0 || n > nbytes-1) warnx("decryption failed (block corrupt) at %d", bn); } else (void)ungetc(c, stdin); /* * dump it */ WRITE(obuf, n); } if (n > 0) warnx("decryption failed (incomplete block) at %d", bn); }
/* * This decrypts using the Cipher Block Chaining mode of DES * msgbuf I/O buffer * fp input file descriptor */ int cbc_decode(char *msgbuf, FILE *fp) { DES_cblock inbuf; /* temp buffer for initialization vector */ int n; /* number of bytes actually read */ int c; /* used to test for EOF */ int inverse = 1; /* 0 to encrypt, 1 to decrypt */ if ((n = READ(msgbuf, 8, fp)) == 8) { /* * do the transformation */ MEMCPY(inbuf, msgbuf, 8); DES_XFORM((DES_cblock *)msgbuf); for (c = 0; c < 8; c++) msgbuf[c] ^= ivec[c]; MEMCPY(ivec, inbuf, 8); /* * if the last one, handle it specially */ if ((c = fgetc(fp)) == EOF) { n = msgbuf[7]; if (n < 0 || n > 7) { des_error("decryption failed (block corrupted)"); return EOF; } } else ungetc(c, fp); return n; } if (n > 0) des_error("decryption failed (incomplete block)"); else if (n < 0) des_error("cannot read file"); return EOF; }
/* * This decrypts using the Cipher Block Chaining mode of DES * msgbuf I/O buffer * fp input file descriptor */ int cbc_decode(char *msgbuf, FILE *fp) { Desbuf tbuf; /* temp buffer for initialization vector */ int n; /* number of bytes actually read */ int c; /* used to test for EOF */ int inverse = 1; /* 0 to encrypt, 1 to decrypt */ if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) { /* * do the transformation */ MEMCPY(BUFFER(tbuf), BUFFER(msgbuf), 8); DES_XFORM(UBUFFER(msgbuf)); for (c = 0; c < 8; c++) UCHAR(msgbuf, c) ^= UCHAR(ivec, c); MEMCPY(BUFFER(ivec), BUFFER(tbuf), 8); /* * if the last one, handle it specially */ if ((c = fgetc(fp)) == EOF) { n = CHAR(msgbuf, 7); if (n < 0 || n > 7) { des_error("decryption failed (block corrupted)"); return EOF; } } else (void)ungetc(c, fp); return n; } if (n > 0) des_error("decryption failed (incomplete block)"); else if (n < 0) des_error("cannot read file"); return EOF; }