xcrypt.c revision 175965
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 175965 2008-02-04 07:56:36Z matteo $"); 3784220Sdillon 3826213Swpaul#include <stdio.h> 3926213Swpaul#include <stdlib.h> 4026213Swpaul#include <string.h> 4126213Swpaul#include <rpc/des_crypt.h> 4226213Swpaul 43146841Sstefanfstatic char hex[16] = { 44146841Sstefanf '0', '1', '2', '3', '4', '5', '6', '7', 45146841Sstefanf '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 46146841Sstefanf}; 47146841Sstefanf 4892917Sobrienstatic char hexval( char ); 4992917Sobrienstatic void bin2hex( int, unsigned char *, char * ); 5092917Sobrienstatic void hex2bin( int, char *, char * ); 5192917Sobrienvoid passwd2des( char *, char * ); 5226213Swpaul 5326213Swpaul/* 5426213Swpaul * Encrypt a secret key given passwd 5526213Swpaul * The secret key is passed and returned in hex notation. 5626213Swpaul * Its length must be a multiple of 16 hex digits (64 bits). 5726213Swpaul */ 5826213Swpaulint 5926213Swpaulxencrypt(secret, passwd) 6026213Swpaul char *secret; 6126213Swpaul char *passwd; 6226213Swpaul{ 6326213Swpaul char key[8]; 6426213Swpaul char ivec[8]; 6526213Swpaul char *buf; 6626213Swpaul int err; 6726213Swpaul int len; 6826213Swpaul 6926213Swpaul len = strlen(secret) / 2; 70175965Smatteo if ((buf = malloc((unsigned)len)) == NULL) { 71175965Smatteo return(0); 72175965Smatteo } 7326213Swpaul 7426213Swpaul hex2bin(len, secret, buf); 7526213Swpaul passwd2des(passwd, key); 7626213Swpaul bzero(ivec, 8); 7726213Swpaul 7826213Swpaul err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec); 7926213Swpaul if (DES_FAILED(err)) { 8026213Swpaul free(buf); 8126213Swpaul return (0); 8226213Swpaul } 8326213Swpaul bin2hex(len, (unsigned char *) buf, secret); 8426213Swpaul free(buf); 8526213Swpaul return (1); 8626213Swpaul} 8726213Swpaul 8826213Swpaul/* 8926213Swpaul * Decrypt secret key using passwd 9026213Swpaul * The secret key is passed and returned in hex notation. 9126213Swpaul * Once again, the length is a multiple of 16 hex digits 9226213Swpaul */ 9326213Swpaulint 9426213Swpaulxdecrypt(secret, passwd) 9526213Swpaul char *secret; 9626213Swpaul char *passwd; 9726213Swpaul{ 9826213Swpaul char key[8]; 9926213Swpaul char ivec[8]; 10026213Swpaul char *buf; 10126213Swpaul int err; 10226213Swpaul int len; 10326213Swpaul 10426213Swpaul len = strlen(secret) / 2; 105175965Smatteo if ((buf = malloc((unsigned)len)) == NULL) { 106175965Smatteo return(0); 107175965Smatteo } 10826213Swpaul 10926213Swpaul hex2bin(len, secret, buf); 11026213Swpaul passwd2des(passwd, key); 11126213Swpaul bzero(ivec, 8); 11226213Swpaul 11326213Swpaul err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec); 11426213Swpaul if (DES_FAILED(err)) { 11526213Swpaul free(buf); 11626213Swpaul return (0); 11726213Swpaul } 11826213Swpaul bin2hex(len, (unsigned char *) buf, secret); 11926213Swpaul free(buf); 12026213Swpaul return (1); 12126213Swpaul} 12226213Swpaul 12326213Swpaul 12426213Swpaul/* 12526213Swpaul * Turn password into DES key 12626213Swpaul */ 12726213Swpaulvoid 12826213Swpaulpasswd2des(pw, key) 12926213Swpaul char *pw; 13026213Swpaul char *key; 13126213Swpaul{ 13226213Swpaul int i; 13326213Swpaul 13426213Swpaul bzero(key, 8); 13526213Swpaul for (i = 0; *pw; i = (i+1)%8) { 13626213Swpaul key[i] ^= *pw++ << 1; 13726213Swpaul } 13826213Swpaul des_setparity(key); 13926213Swpaul} 14026213Swpaul 14126213Swpaul 14226213Swpaul 14326213Swpaul/* 14426213Swpaul * Hex to binary conversion 14526213Swpaul */ 14626213Swpaulstatic void 14726213Swpaulhex2bin(len, hexnum, binnum) 14826213Swpaul int len; 14926213Swpaul char *hexnum; 15026213Swpaul char *binnum; 15126213Swpaul{ 15226213Swpaul int i; 15326213Swpaul 15426213Swpaul for (i = 0; i < len; i++) { 15526213Swpaul *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]); 15626213Swpaul } 15726213Swpaul} 15826213Swpaul 15926213Swpaul/* 16026213Swpaul * Binary to hex conversion 16126213Swpaul */ 16226213Swpaulstatic void 16326213Swpaulbin2hex(len, binnum, hexnum) 16426213Swpaul int len; 16526213Swpaul unsigned char *binnum; 16626213Swpaul char *hexnum; 16726213Swpaul{ 16826213Swpaul int i; 16926213Swpaul unsigned val; 17026213Swpaul 17126213Swpaul for (i = 0; i < len; i++) { 17226213Swpaul val = binnum[i]; 17326213Swpaul hexnum[i*2] = hex[val >> 4]; 17426213Swpaul hexnum[i*2+1] = hex[val & 0xf]; 17526213Swpaul } 17626213Swpaul hexnum[len*2] = 0; 17726213Swpaul} 17826213Swpaul 17926213Swpaulstatic char 18026213Swpaulhexval(c) 18126213Swpaul char c; 18226213Swpaul{ 18326213Swpaul if (c >= '0' && c <= '9') { 18426213Swpaul return (c - '0'); 18526213Swpaul } else if (c >= 'a' && c <= 'z') { 18626213Swpaul return (c - 'a' + 10); 18726213Swpaul } else if (c >= 'A' && c <= 'Z') { 18826213Swpaul return (c - 'A' + 10); 18926213Swpaul } else { 19026213Swpaul return (-1); 19126213Swpaul } 19226213Swpaul} 193