xcrypt.c revision 84220
126213Swpaul/* 226213Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 326213Swpaul * unrestricted use provided that this legend is included on all tape 426213Swpaul * media and as a part of the software program in whole or part. Users 526213Swpaul * may copy or modify Sun RPC without charge, but are not authorized 626213Swpaul * to license or distribute it to anyone else except as part of a product or 726213Swpaul * program developed by the user. 826213Swpaul * 926213Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1026213Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1126213Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1226213Swpaul * 1326213Swpaul * Sun RPC is provided with no support and without any obligation on the 1426213Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction, 1526213Swpaul * modification or enhancement. 1626213Swpaul * 1726213Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 1826213Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 1926213Swpaul * OR ANY PART THEREOF. 2026213Swpaul * 2126213Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2226213Swpaul * or profits or other special, indirect and consequential damages, even if 2326213Swpaul * Sun has been advised of the possibility of such damages. 2426213Swpaul * 2526213Swpaul * Sun Microsystems, Inc. 2626213Swpaul * 2550 Garcia Avenue 2726213Swpaul * Mountain View, California 94043 2826213Swpaul */ 2926213Swpaul/* 3026213Swpaul * Hex encryption/decryption and utility routines 3126213Swpaul * 3226213Swpaul * Copyright (C) 1986, Sun Microsystems, Inc. 3326213Swpaul */ 3426213Swpaul 3584220Sdillon#include <sys/cdefs.h> 3684220Sdillon__FBSDID("$FreeBSD: head/lib/librpcsvc/xcrypt.c 84220 2001-09-30 22:15:15Z dillon $"); 3784220Sdillon 3826213Swpaul#include <stdio.h> 3926213Swpaul#include <stdlib.h> 4026213Swpaul#include <string.h> 4126213Swpaul#include <rpc/des_crypt.h> 4226213Swpaul 4326213Swpaulstatic char hex[]; /* forward */ 4426213Swpaulstatic char hexval __P(( char )); 4526213Swpaulstatic void bin2hex __P(( int, unsigned char *, char * )); 4626213Swpaulstatic void hex2bin __P(( int, char *, char * )); 4726213Swpaulvoid passwd2des __P(( char *, char * )); 4826213Swpaul 4926213Swpaul/* 5026213Swpaul * Encrypt a secret key given passwd 5126213Swpaul * The secret key is passed and returned in hex notation. 5226213Swpaul * Its length must be a multiple of 16 hex digits (64 bits). 5326213Swpaul */ 5426213Swpaulint 5526213Swpaulxencrypt(secret, passwd) 5626213Swpaul char *secret; 5726213Swpaul char *passwd; 5826213Swpaul{ 5926213Swpaul char key[8]; 6026213Swpaul char ivec[8]; 6126213Swpaul char *buf; 6226213Swpaul int err; 6326213Swpaul int len; 6426213Swpaul 6526213Swpaul len = strlen(secret) / 2; 6626213Swpaul buf = malloc((unsigned)len); 6726213Swpaul 6826213Swpaul hex2bin(len, secret, buf); 6926213Swpaul passwd2des(passwd, key); 7026213Swpaul bzero(ivec, 8); 7126213Swpaul 7226213Swpaul err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec); 7326213Swpaul if (DES_FAILED(err)) { 7426213Swpaul free(buf); 7526213Swpaul return (0); 7626213Swpaul } 7726213Swpaul bin2hex(len, (unsigned char *) buf, secret); 7826213Swpaul free(buf); 7926213Swpaul return (1); 8026213Swpaul} 8126213Swpaul 8226213Swpaul/* 8326213Swpaul * Decrypt secret key using passwd 8426213Swpaul * The secret key is passed and returned in hex notation. 8526213Swpaul * Once again, the length is a multiple of 16 hex digits 8626213Swpaul */ 8726213Swpaulint 8826213Swpaulxdecrypt(secret, passwd) 8926213Swpaul char *secret; 9026213Swpaul char *passwd; 9126213Swpaul{ 9226213Swpaul char key[8]; 9326213Swpaul char ivec[8]; 9426213Swpaul char *buf; 9526213Swpaul int err; 9626213Swpaul int len; 9726213Swpaul 9826213Swpaul len = strlen(secret) / 2; 9926213Swpaul buf = malloc((unsigned)len); 10026213Swpaul 10126213Swpaul hex2bin(len, secret, buf); 10226213Swpaul passwd2des(passwd, key); 10326213Swpaul bzero(ivec, 8); 10426213Swpaul 10526213Swpaul err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec); 10626213Swpaul if (DES_FAILED(err)) { 10726213Swpaul free(buf); 10826213Swpaul return (0); 10926213Swpaul } 11026213Swpaul bin2hex(len, (unsigned char *) buf, secret); 11126213Swpaul free(buf); 11226213Swpaul return (1); 11326213Swpaul} 11426213Swpaul 11526213Swpaul 11626213Swpaul/* 11726213Swpaul * Turn password into DES key 11826213Swpaul */ 11926213Swpaulvoid 12026213Swpaulpasswd2des(pw, key) 12126213Swpaul char *pw; 12226213Swpaul char *key; 12326213Swpaul{ 12426213Swpaul int i; 12526213Swpaul 12626213Swpaul bzero(key, 8); 12726213Swpaul for (i = 0; *pw; i = (i+1)%8) { 12826213Swpaul key[i] ^= *pw++ << 1; 12926213Swpaul } 13026213Swpaul des_setparity(key); 13126213Swpaul} 13226213Swpaul 13326213Swpaul 13426213Swpaul 13526213Swpaul/* 13626213Swpaul * Hex to binary conversion 13726213Swpaul */ 13826213Swpaulstatic void 13926213Swpaulhex2bin(len, hexnum, binnum) 14026213Swpaul int len; 14126213Swpaul char *hexnum; 14226213Swpaul char *binnum; 14326213Swpaul{ 14426213Swpaul int i; 14526213Swpaul 14626213Swpaul for (i = 0; i < len; i++) { 14726213Swpaul *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]); 14826213Swpaul } 14926213Swpaul} 15026213Swpaul 15126213Swpaul/* 15226213Swpaul * Binary to hex conversion 15326213Swpaul */ 15426213Swpaulstatic void 15526213Swpaulbin2hex(len, binnum, hexnum) 15626213Swpaul int len; 15726213Swpaul unsigned char *binnum; 15826213Swpaul char *hexnum; 15926213Swpaul{ 16026213Swpaul int i; 16126213Swpaul unsigned val; 16226213Swpaul 16326213Swpaul for (i = 0; i < len; i++) { 16426213Swpaul val = binnum[i]; 16526213Swpaul hexnum[i*2] = hex[val >> 4]; 16626213Swpaul hexnum[i*2+1] = hex[val & 0xf]; 16726213Swpaul } 16826213Swpaul hexnum[len*2] = 0; 16926213Swpaul} 17026213Swpaul 17126213Swpaulstatic char hex[16] = { 17226213Swpaul '0', '1', '2', '3', '4', '5', '6', '7', 17326213Swpaul '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 17426213Swpaul}; 17526213Swpaul 17626213Swpaulstatic char 17726213Swpaulhexval(c) 17826213Swpaul char c; 17926213Swpaul{ 18026213Swpaul if (c >= '0' && c <= '9') { 18126213Swpaul return (c - '0'); 18226213Swpaul } else if (c >= 'a' && c <= 'z') { 18326213Swpaul return (c - 'a' + 10); 18426213Swpaul } else if (c >= 'A' && c <= 'Z') { 18526213Swpaul return (c - 'A' + 10); 18626213Swpaul } else { 18726213Swpaul return (-1); 18826213Swpaul } 18926213Swpaul} 190