ck_crp.c 164 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623
  1. char *ckcrpv = "Encryption Engine, 9.0.117, 19 Mar 2010";
  2. /*
  3. C K _ C R P . C - Cryptography for C-Kermit"
  4. Copyright (C) 1998, 2010,
  5. Trustees of Columbia University in the City of New York.
  6. All rights reserved. See the C-Kermit COPYING.TXT file or the
  7. copyright text in the ckcmai.c module for disclaimer and permissions.
  8. Author:
  9. Jeffrey E Altman (jaltman@secure-endpoints.com)
  10. Secure Endpoints Inc., New York City
  11. */
  12. #define CK_CRP_C
  13. #ifdef CK_DES
  14. #ifdef CK_SSL
  15. #ifndef LIBDES
  16. #define LIBDES
  17. #endif /* LIBDES */
  18. #endif /* CK_SSL */
  19. #endif /* CK_DES */
  20. #ifdef CRYPT_DLL
  21. #define CK_AUTHENTICATION
  22. #define CK_ENCRYPTION
  23. #define CK_DES
  24. #define CK_CAST
  25. #ifndef LIBDES
  26. #define LIBDES
  27. #endif /* LIBDES */
  28. #define TELCMDS /* to define name array */
  29. #define TELOPTS /* to define name array */
  30. #define ENCRYPT_NAMES
  31. #endif /* CRYPT_DLL */
  32. #include "ckcsym.h"
  33. #include "ckcdeb.h"
  34. #include "ckcnet.h"
  35. #ifdef DEBUG
  36. #undef DEBUG
  37. #endif /* DEBUG */
  38. #ifdef CK_AUTHENTICATION
  39. #ifdef CK_ENCRYPTION
  40. #define ENCRYPTION
  41. #ifdef CK_DES
  42. #define DES_ENCRYPTION
  43. #endif /* CK_DES */
  44. #ifdef CK_CAST
  45. #define CAST_ENCRYPTION
  46. #endif /* CK_CAST */
  47. #ifdef COMMENT
  48. #define CAST_EXPORT_ENCRYPTION
  49. #endif /* COMMENT */
  50. #endif /* CK_ENCRYPTION */
  51. #endif /* CK_AUTHENTICATION */
  52. #ifdef CK_ENCRYPTION
  53. #include "ckucmd.h" /* For struct keytab definition */
  54. #include "ckuath.h"
  55. #include "ckuat2.h"
  56. #ifdef MIT_CURRENT
  57. #include <krb5.h>
  58. #endif /* MIT_CURRENT */
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #ifdef OS2
  62. #include <stdarg.h>
  63. #ifdef OS2ONLY
  64. #include <os2.h>
  65. #endif /* OS2ONLY */
  66. #include "ckosyn.h"
  67. #else /* OS2 */
  68. static char * tmpstring = NULL;
  69. #endif /* OS2 */
  70. #ifndef CAST_OR_EXPORT
  71. #ifdef CAST_ENCRYPTION
  72. #define CAST_OR_EXPORT
  73. #endif /* CAST_ENCRYPTION */
  74. #ifdef CAST_EXPORT_ENCRYPTION
  75. #define CAST_OR_EXPORT
  76. #endif /* CAST_EXPORT_ENCRYPTION */
  77. #endif /* CAST_OR_EXPORT */
  78. #ifdef MACOSX
  79. #undef LIBDES
  80. #endif /* MACOSX */
  81. #ifdef CRYPT_DLL
  82. int cmd_quoting = 0;
  83. #ifndef TELOPT_MACRO
  84. int
  85. telopt_index(opt) int opt; {
  86. if ( opt >= 0 && opt <= TELOPT_SEND_URL )
  87. return(opt);
  88. else if ( opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT )
  89. return(opt-89);
  90. else
  91. return(NTELOPTS);
  92. }
  93. int
  94. telopt_ok(opt) int opt; {
  95. return((opt >= TELOPT_BINARY && opt <= TELOPT_SEND_URL) ||
  96. (opt >= TELOPT_PRAGMA_LOGON && opt <= TELOPT_PRAGMA_HEARTBEAT));
  97. }
  98. CHAR *
  99. telopt(opt) int opt; {
  100. if ( telopt_ok(opt) )
  101. return(telopts[telopt_index(opt)]);
  102. else
  103. return("UNKNOWN");
  104. }
  105. #endif /* TELOPT_MACRO */
  106. static int (*p_ttol)(char *,int)=NULL;
  107. static int (*p_dodebug)(int,char *,char *,CK_OFF_T)=NULL;
  108. static int (*p_dohexdump)(char *,char *,int)=NULL;
  109. static void (*p_tn_debug)(char *)=NULL;
  110. static int (*p_vscrnprintf)(char *, ...)=NULL;
  111. static void * p_k5_context=NULL;
  112. static unsigned long (*p_reqtelmutex)(unsigned long)=NULL;
  113. static unsigned long (*p_reltelmutex)(void)=NULL;
  114. unsigned long
  115. RequestTelnetMutex(unsigned long x)
  116. {
  117. if ( p_reqtelmutex )
  118. return p_reqtelmutex(x);
  119. return 0;
  120. }
  121. unsigned long
  122. ReleaseTelnetMutex(void)
  123. {
  124. if ( p_reltelmutex )
  125. return p_reltelmutex();
  126. return 0;
  127. }
  128. int
  129. ttol(char * s, int n)
  130. {
  131. if ( p_ttol )
  132. return(p_ttol(s,n));
  133. else
  134. return(-1);
  135. }
  136. int
  137. dodebug(int flag, char * s1, char * s2, CK_OFF_T n)
  138. {
  139. if ( p_dodebug )
  140. return(p_dodebug(flag,s1,s2,n));
  141. else
  142. return(-1);
  143. }
  144. int
  145. dohexdump( char * s1, char * s2, int n )
  146. {
  147. if ( p_dohexdump )
  148. p_dohexdump(s1,s2,n);
  149. return(0);
  150. }
  151. void
  152. tn_debug( char * s )
  153. {
  154. if ( p_tn_debug )
  155. p_tn_debug(s);
  156. }
  157. static char myprtfstr[4096];
  158. int
  159. Vscrnprintf(const char * format, ...) {
  160. int i, len, rc=0;
  161. char *cp;
  162. va_list ap;
  163. va_start(ap, format);
  164. #ifdef NT
  165. rc = _vsnprintf(myprtfstr, sizeof(myprtfstr)-1, format, ap);
  166. #else /* NT */
  167. rc = vsprintf(myprtfstr, format, ap);
  168. #endif /* NT */
  169. va_end(ap);
  170. if ( p_vscrnprintf )
  171. return(p_vscrnprintf(myprtfstr));
  172. else
  173. return(-1);
  174. }
  175. int
  176. #ifdef CK_ANSIC
  177. tn_hex(CHAR * buf, int buflen, CHAR * data, int datalen)
  178. #else /* CK_ANSIC */
  179. tn_hex(buf, buflen, data, datalen)
  180. CHAR * buf;
  181. int buflen;
  182. CHAR * data;
  183. int datalen;
  184. #endif /* CK_ANSIC */
  185. {
  186. int i = 0, j = 0, k = 0;
  187. CHAR tmp[8];
  188. #ifdef COMMENT
  189. int was_hex = 1;
  190. for (k=0; k < datalen; k++) {
  191. if (data[k] < 32 || data[k] >= 127) {
  192. sprintf(tmp,"%s%02X ",was_hex?"":"\" ",data[k]);
  193. was_hex = 1;
  194. } else {
  195. sprintf(tmp,"%s%c",was_hex?"\"":"",data[k]);
  196. was_hex = 0;
  197. }
  198. ckstrncat(buf,tmp,buflen);
  199. }
  200. if (!was_hex)
  201. ckstrncat(buf,"\" ",buflen);
  202. #else /* COMMENT */
  203. if (datalen <= 0 || data == NULL)
  204. return(0);
  205. for (i = 0; i < datalen; i++) {
  206. ckstrncat(buf,"\r\n ",buflen);
  207. for (j = 0 ; (j < 16); j++) {
  208. if ((i + j) < datalen)
  209. sprintf(tmp,
  210. "%s%02x ",
  211. (j == 8 ? "| " : ""),
  212. (CHAR) data[i + j]
  213. );
  214. else
  215. sprintf(tmp,
  216. "%s ",
  217. (j == 8 ? "| " : "")
  218. );
  219. ckstrncat(buf,tmp,buflen);
  220. }
  221. ckstrncat(buf," ",buflen);
  222. for (k = 0; (k < 16) && ((i + k) < datalen); k++) {
  223. sprintf(tmp,
  224. "%s%c",
  225. (k == 8 ? " " : ""),
  226. isprint(data[i + k]) ? data[i + k] : '.'
  227. );
  228. ckstrncat(buf,tmp,buflen);
  229. }
  230. i += j - 1;
  231. } /* end for */
  232. ckstrncat(buf,"\r\n ",buflen);
  233. #endif /* COMMENT */
  234. return(strlen(buf));
  235. }
  236. #ifdef COMMENT
  237. #define ttol dll_ttol
  238. #define dodebug dll_dodebug
  239. #define dohexdump dll_dohexdump
  240. #define tn_debug dll_tn_debug
  241. #define Vscrnprintf dll_vscrnprintf
  242. #endif /* COMMENT */
  243. char tn_msg[TN_MSG_LEN], hexbuf[TN_MSG_LEN]; /* from ckcnet.c */
  244. int deblog=1, debses=1, tn_deb=1;
  245. #else /* CRYPT_DLL */
  246. extern char tn_msg[], hexbuf[]; /* from ckcnet.c */
  247. extern int deblog, debses, tn_deb;
  248. #ifdef MIT_CURRENT
  249. extern krb5_context k5_context;
  250. #endif /* MIT_CURRENT */
  251. #endif /* CRYPT_DLL */
  252. #ifdef LIBDES
  253. #ifdef MACOSX
  254. #define des_new_random_key ck_des_new_random_key
  255. #define des_set_random_generator_seed ck_des_set_random_generator_seed
  256. #define des_key_sched ck_des_key_sched
  257. #define des_ecb_encrypt ck_des_ecb_encrypt
  258. #define des_string_to_key ck_des_string_to_key
  259. #define des_fixup_key_parity ck_des_fixup_key_parity
  260. #endif /* MACOSX */
  261. #ifndef UNIX
  262. #define des_new_random_key des_random_key
  263. #define des_set_random_generator_seed des_random_seed
  264. #endif /* UNIX */
  265. #define des_fixup_key_parity des_set_odd_parity
  266. #ifdef OPENSSL_097
  267. #define OPENSSL_ENABLE_OLD_DES_SUPPORT
  268. #include <openssl/des.h>
  269. #endif /* OPENSSL_097 */
  270. #else /* LIBDES */
  271. #ifdef UNIX
  272. #define des_set_random_generator_seed(x) des_init_random_number_generator(x)
  273. #endif /* UNIX */
  274. #ifdef OS2
  275. #define des_new_random_key ck_des_new_random_key
  276. #define des_set_random_generator_seed ck_des_set_random_generator_seed
  277. #define des_key_sched ck_des_key_sched
  278. #define des_ecb_encrypt ck_des_ecb_encrypt
  279. #define des_string_to_key ck_des_string_to_key
  280. #define des_fixup_key_parity ck_des_fixup_key_parity
  281. #endif /* OS2 */
  282. #endif /* LIBDES */
  283. #ifdef CK_DES
  284. /* This code comes from Eric Young's libdes package and is not part */
  285. /* of the standard MIT DES library that is part of Kerberos. However, */
  286. /* it is extremely useful. So we add it here. */
  287. /* Weak and semi week keys as take from
  288. * %A D.W. Davies
  289. * %A W.L. Price
  290. * %T Security for Computer Networks
  291. * %I John Wiley & Sons
  292. * %D 1984
  293. * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
  294. * (and actual cblock values).
  295. */
  296. #define NUM_WEAK_KEY 16
  297. static Block weak_keys[NUM_WEAK_KEY]={
  298. /* weak keys */
  299. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  300. {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
  301. {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
  302. {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
  303. /* semi-weak keys */
  304. {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
  305. {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
  306. {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
  307. {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
  308. {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
  309. {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
  310. {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
  311. {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
  312. {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
  313. {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
  314. {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  315. {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
  316. int
  317. ck_des_is_weak_key(key)
  318. Block key;
  319. {
  320. int i;
  321. for (i=0; i<NUM_WEAK_KEY; i++) {
  322. /* Added == 0 to comparision, I obviously don't run
  323. * this section very often :-(, thanks to
  324. * engineering@MorningStar.Com for the fix
  325. * eay 93/06/29
  326. * Another problem, I was comparing only the first 4
  327. * bytes, 97/03/18 */
  328. if (memcmp(weak_keys[i],key,sizeof(Block)) == 0)
  329. return(1);
  330. }
  331. return(0);
  332. }
  333. #ifdef UNIX
  334. #ifdef LIBDES
  335. #ifndef MACOSX
  336. /* These functions are not part of Eric Young's DES library */
  337. /* _unix_time_gmt_unixsec */
  338. /* _des_set_random_generator_seed */
  339. /* _des_fixup_key_parity (added in 0.9.5) */
  340. /* _des_new_random_key */
  341. #include <sys/time.h>
  342. unsigned long
  343. unix_time_gmt_unixsec (usecptr)
  344. unsigned long *usecptr;
  345. {
  346. struct timeval now;
  347. (void) gettimeofday (&now, (struct timezone *)0);
  348. if (usecptr)
  349. *usecptr = now.tv_usec;
  350. return now.tv_sec;
  351. }
  352. void
  353. des_set_random_generator_seed(Block B)
  354. {
  355. des_random_seed(B);
  356. return;
  357. }
  358. #ifdef COMMENT
  359. /* added to openssl in 0.9.5 */
  360. void
  361. des_fixup_key_parity(Block B)
  362. {
  363. des_set_odd_parity(B);
  364. return;
  365. }
  366. #endif /* COMMENT */
  367. int
  368. des_new_random_key(Block B)
  369. {
  370. int rc=0;
  371. /* WARNING:
  372. This might need to have the "rc = " removed because this
  373. is VOID in later, and maybe even all, versions.
  374. */
  375. rc = des_random_key(B);
  376. return(rc);
  377. }
  378. #endif /* MACOSX */
  379. #endif /* LIBDES */
  380. #endif /* UNIX */
  381. #endif /* CK_DES */
  382. /*
  383. * Copyright (c) 1991, 1993
  384. * The Regents of the University of California. All rights reserved.
  385. *
  386. * Redistribution and use in source and binary forms, with or without
  387. * modification, are permitted provided that the following conditions
  388. * are met:
  389. * 1. Redistributions of source code must retain the above copyright
  390. * notice, this list of conditions and the following disclaimer.
  391. * 2. Redistributions in binary form must reproduce the above copyright
  392. * notice, this list of conditions and the following disclaimer in the
  393. * documentation and/or other materials provided with the distribution.
  394. * 3. All advertising materials mentioning features or use of this software
  395. * must display the following acknowledgement:
  396. * This product includes software developed by the University of
  397. * California, Berkeley and its contributors.
  398. * 4. Neither the name of the University nor the names of its contributors
  399. * may be used to endorse or promote products derived from this software
  400. * without specific prior written permission.
  401. *
  402. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  403. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  404. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  405. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  406. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  407. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  408. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  409. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  410. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  411. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  412. * SUCH DAMAGE.
  413. */
  414. /* based on @(#)encrypt.c 8.1 (Berkeley) 6/4/93 */
  415. /*
  416. * Copyright (C) 1990 by the Massachusetts Institute of Technology
  417. *
  418. * Export of this software from the United States of America may
  419. * require a specific license from the United States Government.
  420. * It is the responsibility of any person or organization contemplating
  421. * export to obtain such a license before exporting.
  422. *
  423. * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  424. * distribute this software and its documentation for any purpose and
  425. * without fee is hereby granted, provided that the above copyright
  426. * notice appear in all copies and that both that copyright notice and
  427. * this permission notice appear in supporting documentation, and that
  428. * the name of M.I.T. not be used in advertising or publicity pertaining
  429. * to distribution of the software without specific, written prior
  430. * permission. M.I.T. makes no representations about the suitability of
  431. * this software for any purpose. It is provided "as is" without express
  432. * or implied warranty.
  433. */
  434. #include <stdio.h>
  435. /*
  436. * These function pointers point to the current routines
  437. * for encrypting and decrypting data.
  438. */
  439. /* NOTE: These next two might need to have the "static " removed */
  440. static VOID (*encrypt_output) P((unsigned char *, int));
  441. static int (*decrypt_input) P((int));
  442. #ifdef DEBUG
  443. static int encrypt_debug_mode = 1;
  444. static int encrypt_verbose = 1;
  445. #else
  446. static int encrypt_verbose = 1;
  447. static int encrypt_debug_mode = 0;
  448. #endif
  449. static char dbgbuf [16384];
  450. static int decrypt_mode = 0;
  451. static int encrypt_mode = 0;
  452. static int autoencrypt = 1;
  453. static int autodecrypt = 1;
  454. static int havesessionkey = 0;
  455. static kstream EncryptKSGlobalHack = NULL;
  456. static int EncryptType = ENCTYPE_ANY;
  457. #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
  458. static long i_support_encrypt =
  459. typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
  460. static long i_support_decrypt =
  461. typemask(ENCTYPE_DES_CFB64) | typemask(ENCTYPE_DES_OFB64);
  462. static long i_wont_support_encrypt = 0;
  463. static long i_wont_support_decrypt = 0;
  464. #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
  465. #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
  466. static long remote_supports_encrypt = 0;
  467. static long remote_supports_decrypt = 0;
  468. /* Make sure that this list is in order of algorithm strength */
  469. /* as it determines the search order for selecting specific */
  470. /* encryption choices. All CFB modes must come before OFB modes. */
  471. static Encryptions encryptions[] = {
  472. #ifdef DES_ENCRYPTION
  473. { "DES3_CFB64",
  474. ENCTYPE_DES3_CFB64,
  475. des3_cfb64_encrypt,
  476. des3_cfb64_decrypt,
  477. des3_cfb64_init,
  478. des3_cfb64_start,
  479. des3_cfb64_is,
  480. des3_cfb64_reply,
  481. des3_cfb64_session,
  482. des3_cfb64_keyid,
  483. NULL },
  484. #endif /* DES_ENCRYPTION */
  485. #ifdef CAST_ENCRYPTION
  486. #ifndef CAST_EXPORT_ENCRYPTION
  487. { "CAST128_CFB64", ENCTYPE_CAST128_CFB64,
  488. cast_cfb64_encrypt,
  489. cast_cfb64_decrypt,
  490. cast_cfb64_init,
  491. cast_cfb64_start,
  492. cast_cfb64_is,
  493. cast_cfb64_reply,
  494. cast_cfb64_session,
  495. cast_cfb64_keyid,
  496. NULL },
  497. #endif
  498. #endif
  499. #ifdef DES_ENCRYPTION
  500. { "DES_CFB64",
  501. ENCTYPE_DES_CFB64,
  502. cfb64_encrypt,
  503. cfb64_decrypt,
  504. cfb64_init,
  505. cfb64_start,
  506. cfb64_is,
  507. cfb64_reply,
  508. cfb64_session,
  509. cfb64_keyid,
  510. NULL },
  511. #endif /* DES_ENCRYPTION */
  512. #if defined (CAST_EXPORT_ENCRYPTION) || defined(CAST_ENCRYPTION)
  513. { "CAST5_40_CFB64", ENCTYPE_CAST5_40_CFB64,
  514. castexp_cfb64_encrypt,
  515. castexp_cfb64_decrypt,
  516. castexp_cfb64_init,
  517. castexp_cfb64_start,
  518. castexp_cfb64_is,
  519. castexp_cfb64_reply,
  520. castexp_cfb64_session,
  521. castexp_cfb64_keyid,
  522. NULL },
  523. #endif /* CAST_ENCRYPTION */
  524. #ifdef DES_ENCRYPTION
  525. { "DES3_OFB64",
  526. ENCTYPE_DES3_OFB64,
  527. des3_ofb64_encrypt,
  528. des3_ofb64_decrypt,
  529. des3_ofb64_init,
  530. des3_ofb64_start,
  531. des3_ofb64_is,
  532. des3_ofb64_reply,
  533. des3_ofb64_session,
  534. des3_ofb64_keyid,
  535. NULL },
  536. #endif /* DES_ENCRYPTION */
  537. #ifdef CAST_ENCRYPTION
  538. #ifndef CAST_EXPORT_ENCRYPTION
  539. { "CAST128_OFB64", ENCTYPE_CAST128_OFB64,
  540. cast_ofb64_encrypt,
  541. cast_ofb64_decrypt,
  542. cast_ofb64_init,
  543. cast_ofb64_start,
  544. cast_ofb64_is,
  545. cast_ofb64_reply,
  546. cast_ofb64_session,
  547. cast_ofb64_keyid,
  548. NULL },
  549. #endif
  550. #endif
  551. #ifdef DES_ENCRYPTION
  552. { "DES_OFB64",
  553. ENCTYPE_DES_OFB64,
  554. ofb64_encrypt,
  555. ofb64_decrypt,
  556. ofb64_init,
  557. ofb64_start,
  558. ofb64_is,
  559. ofb64_reply,
  560. ofb64_session,
  561. ofb64_keyid,
  562. NULL },
  563. #endif /* DES_ENCRYPTION */
  564. #if defined (CAST_EXPORT_ENCRYPTION) || defined(CAST_ENCRYPTION)
  565. { "CAST5_40_OFB64", ENCTYPE_CAST5_40_OFB64,
  566. castexp_ofb64_encrypt,
  567. castexp_ofb64_decrypt,
  568. castexp_ofb64_init,
  569. castexp_ofb64_start,
  570. castexp_ofb64_is,
  571. castexp_ofb64_reply,
  572. castexp_ofb64_session,
  573. castexp_ofb64_keyid,
  574. NULL },
  575. #endif /* CAST_ENCRYPTION */
  576. { 0,0,0,0,0,0,0,0,0,0,0 }
  577. };
  578. int
  579. get_crypt_table( struct keytab ** pTable, int * pN )
  580. {
  581. int i=0,n=0;
  582. if ( *pTable )
  583. {
  584. for ( i=0 ; i < *pN ; i++ )
  585. free( (*pTable)[i].kwd ) ;
  586. free ( *pTable ) ;
  587. }
  588. *pTable = NULL;
  589. *pN = 0;
  590. /* How many encryption types do we have? */
  591. while ( encryptions[n].name )
  592. n++;
  593. if ( n )
  594. {
  595. *pTable = malloc( sizeof(struct keytab) * (n+2) ) ;
  596. if ( !(*pTable) )
  597. return(0);
  598. #ifdef OS2
  599. (*pTable)[0].kwd =strdup("automatic");
  600. #else /* OS2 */
  601. makestr(&tmpstring,"automatic");
  602. (*pTable)[0].kwd = tmpstring;
  603. tmpstring = NULL;
  604. #endif /* OS2 */
  605. (*pTable)[0].kwval = ENCTYPE_ANY;
  606. (*pTable)[0].flgs = 0;
  607. #ifdef OS2
  608. (*pTable)[1].kwd =strdup("none");
  609. #else /* OS2 */
  610. makestr(&tmpstring,"none");
  611. (*pTable)[1].kwd = tmpstring;
  612. tmpstring = NULL;
  613. #endif /* OS2 */
  614. (*pTable)[1].kwval = 999;
  615. (*pTable)[1].flgs = 0;
  616. (*pN) = 2;
  617. for ( i=0 ; i < n ; i++ ) {
  618. char * newstr = NULL, * p;
  619. int newval = encryptions[i].type;
  620. int j = 0, len = 0;
  621. #ifdef OS2
  622. newstr = strdup(encryptions[i].name);
  623. strlwr(newstr);
  624. #else /* OS2 */
  625. makestr(&tmpstring,encryptions[i].name);
  626. newstr = tmpstring;
  627. tmpstring = NULL;
  628. for (p = newstr; *p; p++) if (isupper(*p)) *p = tolower(*p);
  629. #endif /* OS2 */
  630. for (j = 0; j < (*pN); j++) {
  631. int tempval = 0;
  632. char * tempstr = NULL;
  633. if ( strcmp( (*pTable)[j].kwd, newstr ) > 0 )
  634. {
  635. tempval = (*pTable)[j].kwval;
  636. tempstr = (*pTable)[j].kwd;
  637. (*pTable)[j].kwd = newstr ;
  638. (*pTable)[j].kwval = newval;
  639. newval = tempval;
  640. newstr = tempstr;
  641. (*pTable)[j].flgs = 0;
  642. }
  643. }
  644. (*pTable)[*pN].kwd = newstr ;
  645. (*pTable)[*pN].kwval = newval;
  646. (*pTable)[*pN].flgs = 0 ;
  647. (*pN)++ ;
  648. }
  649. } else {
  650. *pTable = malloc( sizeof(struct keytab) * 2 ) ;
  651. if ( !(*pTable) )
  652. return(0);
  653. #ifdef OS2
  654. (*pTable)[0].kwd =strdup("automatic");
  655. #else /* OS2 */
  656. makestr(&tmpstring,"automatic");
  657. (*pTable)[0].kwd = tmpstring;
  658. tmpstring = NULL;
  659. #endif /* OS2 */
  660. (*pTable)[0].kwval = ENCTYPE_ANY;
  661. (*pTable)[0].flgs = 0;
  662. #ifdef OS2
  663. (*pTable)[1].kwd =strdup("none");
  664. #else /* OS2 */
  665. makestr(&tmpstring,"none");
  666. (*pTable)[1].kwd = tmpstring;
  667. tmpstring = NULL;
  668. #endif /* OS2 */
  669. (*pTable)[1].kwval = 999;
  670. (*pTable)[1].flgs = 0;
  671. (*pN) = 2;
  672. }
  673. return(*pN);
  674. }
  675. static unsigned char str_send[64] = { IAC, SB, TELOPT_ENCRYPTION,
  676. ENCRYPT_SUPPORT };
  677. static unsigned char str_suplen = 0;
  678. static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPTION };
  679. static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPTION, 0, IAC, SE };
  680. _PROTOTYP(int encrypt_request_end, (VOID));
  681. _PROTOTYP(int encrypt_request_start, (VOID));
  682. _PROTOTYP(int encrypt_enc_keyid, (unsigned char *, int));
  683. _PROTOTYP(int encrypt_dec_keyid, (unsigned char *, int));
  684. _PROTOTYP(int encrypt_support, (unsigned char *, int));
  685. _PROTOTYP(int encrypt_start, (unsigned char *, int));
  686. _PROTOTYP(int encrypt_end, (VOID));
  687. _PROTOTYP(int encrypt_ks_stream,(struct kstream_data_block *, /* output */
  688. struct kstream_data_block *)); /* input */
  689. _PROTOTYP(int decrypt_ks_stream,(struct kstream_data_block *, /* output */
  690. struct kstream_data_block *)); /* input */
  691. int
  692. #ifdef CK_ANSIC
  693. encrypt_ks_stream(struct kstream_data_block *i,
  694. struct kstream_data_block *o)
  695. #else
  696. encrypt_ks_stream(i,o)
  697. struct kstream_data_block *i; struct kstream_data_block *o;
  698. #endif
  699. {
  700. /*
  701. * this is really quite bogus, since it does an in-place encryption...
  702. */
  703. if (encrypt_output) {
  704. encrypt_output(i->ptr, i->length);
  705. return 1;
  706. }
  707. return 0;
  708. }
  709. int
  710. #ifdef CK_ANSIC
  711. decrypt_ks_stream(struct kstream_data_block *i,
  712. struct kstream_data_block *o)
  713. #else
  714. decrypt_ks_stream(i,o)
  715. struct kstream_data_block *i; struct kstream_data_block *o;
  716. #endif
  717. {
  718. unsigned int len;
  719. /*
  720. * this is really quite bogus, since it does an in-place decryption...
  721. */
  722. if (decrypt_input) {
  723. for (len = 0 ; len < i->length ; len++)
  724. ((unsigned char *)i->ptr)[len]
  725. = decrypt_input(((unsigned char *)i->ptr)[len]);
  726. return 1;
  727. }
  728. return 0;
  729. }
  730. int
  731. #ifdef CK_ANSIC
  732. decrypt_ks_hack(unsigned char *buf, int cnt)
  733. #else
  734. decrypt_ks_hack(buf,cnt) unsigned char *buf; int cnt;
  735. #endif
  736. {
  737. int len;
  738. /*
  739. * this is really quite bogus, since it does an in-place decryption...
  740. */
  741. for (len = 0 ; len < cnt ; len++)
  742. buf[len] = decrypt_input(buf[len]);
  743. #ifdef DEBUG
  744. ckhexdump("decrypt ks hack", buf, cnt);
  745. #endif
  746. return 1;
  747. }
  748. /*
  749. * parsedat[0] == the suboption we might be negotiating,
  750. */
  751. int
  752. #ifdef CK_ANSIC
  753. encrypt_parse(unsigned char *parsedat, int end_sub)
  754. #else
  755. encrypt_parse(parsedat,end_sub) unsigned char *parsedat; int end_sub;
  756. #endif
  757. {
  758. int rc = 0;
  759. switch(parsedat[1]) {
  760. case ENCRYPT_START:
  761. rc = encrypt_start(parsedat + 2, end_sub - 2);
  762. break;
  763. case ENCRYPT_END:
  764. rc = encrypt_end();
  765. break;
  766. case ENCRYPT_SUPPORT:
  767. rc = encrypt_support(parsedat + 2, end_sub - 2);
  768. break;
  769. case ENCRYPT_REQSTART:
  770. rc = encrypt_request_start();
  771. break;
  772. case ENCRYPT_REQEND:
  773. /*
  774. * We can always send an REQEND so that we cannot
  775. * get stuck encrypting. We should only get this
  776. * if we have been able to get in the correct mode
  777. * anyhow.
  778. */
  779. rc = encrypt_request_end();
  780. break;
  781. case ENCRYPT_IS:
  782. rc = encrypt_is(parsedat + 2, end_sub - 2);
  783. break;
  784. case ENCRYPT_REPLY:
  785. rc = encrypt_reply(parsedat + 2, end_sub - 2);
  786. break;
  787. case ENCRYPT_ENC_KEYID:
  788. rc = encrypt_enc_keyid(parsedat + 2, end_sub - 2);
  789. break;
  790. case ENCRYPT_DEC_KEYID:
  791. rc = encrypt_dec_keyid(parsedat + 2, end_sub - 2);
  792. break;
  793. default:
  794. rc = -1;
  795. break;
  796. }
  797. return(rc);
  798. }
  799. /* XXX */
  800. Encryptions *
  801. #ifdef CK_ANSIC
  802. findencryption(int type)
  803. #else
  804. findencryption(type) int type;
  805. #endif
  806. {
  807. Encryptions *ep = encryptions;
  808. if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask(type)))
  809. return(0);
  810. while (ep->type && ep->type != type)
  811. ++ep;
  812. return(ep->type ? ep : 0);
  813. }
  814. Encryptions *
  815. #ifdef CK_ANSIC
  816. finddecryption(int type)
  817. #else
  818. finddecryption(type) int type;
  819. #endif
  820. {
  821. Encryptions *ep = encryptions;
  822. if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask(type)))
  823. return(0);
  824. while (ep->type && ep->type != type)
  825. ++ep;
  826. return(ep->type ? ep : 0);
  827. }
  828. #define MAXKEYLEN 64
  829. static struct key_info {
  830. unsigned char keyid[MAXKEYLEN];
  831. int keylen;
  832. int dir;
  833. int *modep;
  834. Encryptions *(*getcrypt)();
  835. } ki[2] = {
  836. { { 0 }, 0, DIR_ENCRYPT, &encrypt_mode, findencryption },
  837. { { 0 }, 0, DIR_DECRYPT, &decrypt_mode, finddecryption },
  838. };
  839. VOID
  840. #ifdef CK_ANSIC
  841. encrypt_init(kstream iks, int type)
  842. #else
  843. encrypt_init(iks, type) kstream iks; int type;
  844. #endif
  845. {
  846. Encryptions *ep = encryptions;
  847. i_support_encrypt = i_support_decrypt = 0;
  848. remote_supports_encrypt = remote_supports_decrypt = 0;
  849. i_wont_support_encrypt = i_wont_support_decrypt = 0;
  850. encrypt_mode = 0;
  851. decrypt_mode = 0;
  852. encrypt_output = NULL;
  853. decrypt_input = NULL;
  854. ki[0].keylen = 0;
  855. memset(ki[0].keyid,0,MAXKEYLEN);
  856. ki[1].keylen = 0;
  857. memset(ki[1].keyid,0,MAXKEYLEN);
  858. havesessionkey = 0;
  859. autoencrypt = 1;
  860. autodecrypt = 1;
  861. EncryptKSGlobalHack = iks;
  862. EncryptType = type;
  863. str_send[0] = IAC;
  864. str_send[1] = SB;
  865. str_send[2] = TELOPT_ENCRYPTION;
  866. str_send[3] = ENCRYPT_SUPPORT;
  867. str_suplen = 4;
  868. while (ep->type) {
  869. if ( EncryptType == ENCTYPE_ANY ||
  870. EncryptType == ep->type ) {
  871. #ifdef DEBUG
  872. if (encrypt_debug_mode) {
  873. sprintf(dbgbuf, ">>>I will support %s\n",
  874. ENCTYPE_NAME(ep->type)); /* safe */
  875. debug(F110,"encrypt_init",dbgbuf,0);
  876. }
  877. #endif
  878. i_support_encrypt |= typemask(ep->type);
  879. i_support_decrypt |= typemask(ep->type);
  880. if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
  881. if ((str_send[str_suplen++] = ep->type) == IAC)
  882. str_send[str_suplen++] = IAC;
  883. }
  884. if (ep->init)
  885. (*ep->init)(0);
  886. ++ep;
  887. }
  888. str_send[str_suplen++] = IAC;
  889. str_send[str_suplen++] = SE;
  890. }
  891. VOID
  892. #ifdef CK_ANSIC
  893. encrypt_send_support(VOID)
  894. #else
  895. encrypt_send_support()
  896. #endif
  897. {
  898. Encryptions *ep = encryptions;
  899. #ifdef CK_SSL
  900. if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  901. return;
  902. #endif /* CK_SSL */
  903. str_send[0] = IAC;
  904. str_send[1] = SB;
  905. str_send[2] = TELOPT_ENCRYPTION;
  906. str_send[3] = ENCRYPT_SUPPORT;
  907. str_suplen = 4;
  908. while (ep->type) {
  909. if ( EncryptType == ENCTYPE_ANY ||
  910. EncryptType == ep->type ) {
  911. #ifdef DEBUG
  912. if (encrypt_debug_mode) {
  913. sprintf(dbgbuf, ">>>I will support %s\n",
  914. ENCTYPE_NAME(ep->type)); /* safe */
  915. debug(F110,"encrypt_send_support",dbgbuf,0);
  916. }
  917. #endif
  918. if ((i_wont_support_decrypt & typemask(ep->type)) == 0)
  919. if ((str_send[str_suplen++] = ep->type) == IAC)
  920. str_send[str_suplen++] = IAC;
  921. }
  922. ++ep;
  923. }
  924. str_send[str_suplen++] = IAC;
  925. str_send[str_suplen++] = SE;
  926. /*
  927. * If the user has requested that decryption start
  928. * immediatly, then send a "REQUEST START" before
  929. * we negotiate the type.
  930. */
  931. if (autodecrypt)
  932. encrypt_send_request_start();
  933. if (deblog || tn_deb || debses) {
  934. int i;
  935. sprintf(tn_msg,"TELNET SENT SB %s SUPPORT ",
  936. TELOPT(TELOPT_ENCRYPTION)); /* safe */
  937. for ( i=4;i<str_suplen-2;i++ ) {
  938. if ( str_send[i] == IAC ) {
  939. ckstrncat(tn_msg,"IAC ",TN_MSG_LEN);
  940. i++;
  941. }
  942. ckstrncat(tn_msg,ENCTYPE_NAME(str_send[i]),TN_MSG_LEN);
  943. ckstrncat(tn_msg," ",TN_MSG_LEN);
  944. }
  945. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  946. debug(F100,tn_msg,"",0);
  947. if (tn_deb || debses) tn_debug(tn_msg);
  948. }
  949. #ifdef OS2
  950. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  951. #endif
  952. ttol(str_send, str_suplen);
  953. #ifdef OS2
  954. ReleaseTelnetMutex();
  955. #endif
  956. str_suplen = 0;
  957. }
  958. /*
  959. * Called when ENCRYPT SUPPORT is received.
  960. */
  961. int
  962. #ifdef CK_ANSIC
  963. encrypt_support(unsigned char *_typelist, int _cnt)
  964. #else
  965. encrypt_support(_typelist, _cnt) unsigned char * _typelist; int _cnt;
  966. #endif
  967. {
  968. register int type, use_type = 0;
  969. unsigned char * typelist = _typelist;
  970. int cnt = _cnt;
  971. Encryptions *ep;
  972. debug(F111,"encrypt_support","cnt",cnt);
  973. /*
  974. * Forget anything the other side has previously told us.
  975. */
  976. remote_supports_decrypt = 0;
  977. while (cnt-- > 0) {
  978. debug(F101,"XXX cnt","",cnt);
  979. type = *typelist++;
  980. debug(F101,"XXX type","",type);
  981. debug(F101,"XXX ENCTYPE_ANY","",ENCTYPE_ANY);
  982. if ( EncryptType == ENCTYPE_ANY ||
  983. EncryptType == type ) {
  984. #ifdef DEBUG
  985. if (encrypt_debug_mode) {
  986. sprintf(dbgbuf, ">>>Remote supports %s (%d)\n",
  987. ENCTYPE_NAME(type), type); /* safe */
  988. debug(F110,"encrypt_support",dbgbuf,0);
  989. }
  990. #endif
  991. if ((type < ENCTYPE_CNT) &&
  992. (I_SUPPORT_ENCRYPT & typemask(type))) {
  993. remote_supports_decrypt |= typemask(type);
  994. if (use_type == 0)
  995. use_type = type;
  996. }
  997. }
  998. }
  999. debug(F101,"XXX use_type","",use_type);
  1000. if (use_type) {
  1001. ep = findencryption(use_type);
  1002. if (!ep) {
  1003. debug(F111,"encrypt_support","findencryption == NULL",use_type);
  1004. return(-1);
  1005. }
  1006. debug(F100,"XXX ep not NULL","",0);
  1007. type = ep->start ? (*ep->start)(DIR_ENCRYPT, 0) : 0;
  1008. debug(F101,"XXX new type","",type);
  1009. #ifdef DEBUG
  1010. if (encrypt_debug_mode) {
  1011. sprintf(dbgbuf, ">>>(*ep->start)() %s returned %d (%s)\n",
  1012. ENCTYPE_NAME(use_type), type,
  1013. ENCRYPT_NAME(type)); /* safe */
  1014. debug(F110,"encrypt_support",dbgbuf,0);
  1015. }
  1016. #endif
  1017. if (type < 0) {
  1018. debug(F111,"encrypt_support","type < 0",type);
  1019. return(-1);
  1020. }
  1021. encrypt_mode = use_type;
  1022. if (type == 0)
  1023. encrypt_start_output(use_type);
  1024. debug(F111,"encrypt_support","success",type);
  1025. return(0);
  1026. }
  1027. debug(F111,"encrypt_support","failed",use_type);
  1028. return(-1);
  1029. }
  1030. int
  1031. #ifdef CK_ANSIC
  1032. encrypt_is(unsigned char *data, int cnt)
  1033. #else
  1034. encrypt_is(data, cnt) unsigned char *data; int cnt;
  1035. #endif /* CK_ANSIC */
  1036. {
  1037. Encryptions *ep;
  1038. register int type, ret;
  1039. if (--cnt < 0)
  1040. return(-1);
  1041. type = *data++;
  1042. if (type < ENCTYPE_CNT)
  1043. remote_supports_encrypt |= typemask(type);
  1044. if (!(ep = finddecryption(type))) {
  1045. #ifdef DEBUG
  1046. if (encrypt_debug_mode) {
  1047. sprintf(dbgbuf, ">>>encrypt_is: "
  1048. "Can't find type %s (%d) for initial negotiation\n",
  1049. ENCTYPE_NAME_OK(type)
  1050. ? ENCTYPE_NAME(type) : "(unknown)",
  1051. type); /* safe */
  1052. debug(F110,"encrypt_is",dbgbuf,0);
  1053. }
  1054. #endif
  1055. return(-1);
  1056. }
  1057. if (!ep->is) {
  1058. #ifdef DEBUG
  1059. if (encrypt_debug_mode) {
  1060. sprintf(dbgbuf, ">>>encrypt_is: "
  1061. "No initial negotiation needed for type %s (%d)\n",
  1062. ENCTYPE_NAME_OK(type)
  1063. ? ENCTYPE_NAME(type) : "(unknown)",
  1064. type); /* safe */
  1065. debug(F110,"encrypt_is",dbgbuf,0);
  1066. }
  1067. #endif
  1068. ret = 0;
  1069. } else {
  1070. ret = (*ep->is)(data, cnt);
  1071. #ifdef DEBUG
  1072. if (encrypt_debug_mode) {
  1073. sprintf(dbgbuf, "encrypt_is: "
  1074. "(*ep->is)(%x, %d) returned %s(%d)\n", data, cnt,
  1075. (ret < 0) ? "FAIL " :
  1076. (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret); /* safe */
  1077. debug(F110,"encrypt_is",dbgbuf,0);
  1078. }
  1079. #endif
  1080. }
  1081. if (ret < 0) {
  1082. autodecrypt = 0;
  1083. return(-1);
  1084. } else {
  1085. decrypt_mode = type;
  1086. if (ret == 0 && autodecrypt) {
  1087. encrypt_send_request_start();
  1088. }
  1089. }
  1090. return(0);
  1091. }
  1092. int
  1093. #ifdef CK_ANSIC
  1094. encrypt_reply(unsigned char *data, int cnt)
  1095. #else
  1096. encrypt_reply(data, cnt) unsigned char *data; int cnt;
  1097. #endif
  1098. {
  1099. Encryptions *ep;
  1100. register int ret, type;
  1101. if (--cnt < 0)
  1102. return(-1);
  1103. type = *data++;
  1104. if (!(ep = findencryption(type))) {
  1105. #ifdef DEBUG
  1106. if (encrypt_debug_mode) {
  1107. sprintf(dbgbuf,
  1108. ">>>Can't find type %s (%d) for initial negotiation\n",
  1109. ENCTYPE_NAME_OK(type)
  1110. ? ENCTYPE_NAME(type) : "(unknown)",
  1111. type); /* safe */
  1112. debug(F110,"encrypt_reply",dbgbuf,0);
  1113. }
  1114. #endif
  1115. return(-1);
  1116. }
  1117. if (!ep->reply) {
  1118. #ifdef DEBUG
  1119. if (encrypt_debug_mode) {
  1120. sprintf(dbgbuf, ">>>No initial negotiation needed for type %s (%d)\n",
  1121. ENCTYPE_NAME_OK(type)
  1122. ? ENCTYPE_NAME(type) : "(unknown)",
  1123. type); /* safe */
  1124. debug(F110,"encrypt_reply",dbgbuf,0);
  1125. }
  1126. #endif
  1127. ret = 0;
  1128. } else {
  1129. ret = (*ep->reply)(data, cnt);
  1130. #ifdef DEBUG
  1131. if (encrypt_debug_mode) {
  1132. sprintf(dbgbuf, "(*ep->reply)(%x, %d) returned %s(%d)\n",
  1133. data, cnt,
  1134. (ret < 0) ? "FAIL " :
  1135. (ret == 0) ? "SUCCESS " : "MORE_TO_DO ", ret); /* safe */
  1136. debug(F110,"encrypt_reply",dbgbuf,0);
  1137. }
  1138. #endif
  1139. }
  1140. #ifdef DEBUG
  1141. if (encrypt_debug_mode) {
  1142. sprintf(dbgbuf, ">>>encrypt_reply returned %d\n", ret); /* safe */
  1143. debug(F110,"encrypt_reply",dbgbuf,0);
  1144. }
  1145. #endif
  1146. if (ret < 0) {
  1147. autoencrypt = 0;
  1148. return(-1);
  1149. } else {
  1150. encrypt_mode = type;
  1151. if (ret == 0 && autoencrypt)
  1152. encrypt_start_output(type);
  1153. }
  1154. return(0);
  1155. }
  1156. /*
  1157. * Called when a ENCRYPT START command is received.
  1158. */
  1159. int
  1160. #ifdef CK_ANSIC
  1161. encrypt_start(unsigned char *data, int cnt)
  1162. #else
  1163. encrypt_start(data, cnt) unsigned char *data; int cnt;
  1164. #endif
  1165. {
  1166. Encryptions *ep;
  1167. if (!decrypt_mode) {
  1168. /*
  1169. * Something is wrong. We should not get a START
  1170. * command without having already picked our
  1171. * decryption scheme. Send a REQUEST-END to
  1172. * attempt to clear the channel...
  1173. */
  1174. encrypt_send_request_end();
  1175. printf("Authentication error!\n%s\n",
  1176. "Warning, Cannot decrypt input stream!!!");
  1177. return(-1);
  1178. }
  1179. if (ep = finddecryption(decrypt_mode)) {
  1180. if ( decrypt_input != ep->input ) {
  1181. decrypt_input = ep->input;
  1182. EncryptKSGlobalHack->decrypt = decrypt_ks_stream;
  1183. EncryptKSGlobalHack->decrypt_type = ep->type;
  1184. if (encrypt_verbose) {
  1185. sprintf(dbgbuf, "Input is now decrypted with type %s",
  1186. ENCTYPE_NAME(decrypt_mode)); /* safe */
  1187. debug(F110,"encrypt_start",dbgbuf,0);
  1188. printf("%s\n",dbgbuf);
  1189. }
  1190. #ifdef DEBUG
  1191. if (encrypt_debug_mode) {
  1192. sprintf(dbgbuf, ">>>Start to decrypt input with type %s",
  1193. ENCTYPE_NAME(decrypt_mode)); /* safe */
  1194. debug(F110,"ck_crp",dbgbuf,0);
  1195. }
  1196. #endif
  1197. }
  1198. } else {
  1199. char buf[1024];
  1200. sprintf(buf, "Warning, Cannot decrypt type %s (%d)!!!",
  1201. ENCTYPE_NAME_OK(decrypt_mode)
  1202. ? ENCTYPE_NAME(decrypt_mode) : "(unknown)",
  1203. decrypt_mode); /* safe */
  1204. printf("Authentication error!\n%s\n",buf);
  1205. encrypt_send_request_end();
  1206. return(-1);
  1207. }
  1208. return(0);
  1209. }
  1210. int
  1211. #ifdef CK_ANSIC
  1212. encrypt_dont_support(int type)
  1213. #else
  1214. encrypt_dont_support(type) int type;
  1215. #endif
  1216. {
  1217. i_wont_support_encrypt |= typemask(type);
  1218. i_wont_support_decrypt |= typemask(type);
  1219. return(0);
  1220. }
  1221. int
  1222. #ifdef CK_ANSIC
  1223. encrypt_session_key(Session_Key *key, int server)
  1224. #else
  1225. encrypt_session_key(key, server) Session_Key *key; int server;
  1226. #endif
  1227. {
  1228. Encryptions *ep = encryptions;
  1229. if (havesessionkey)
  1230. return(0);
  1231. havesessionkey = 1;
  1232. while (ep->type) {
  1233. debug(F111,"encrypt_session_key",ep->name,ep->type);
  1234. if (ep->session) {
  1235. if ((*ep->session)(key, server) < 0) {
  1236. i_wont_support_encrypt |= typemask(ep->type);
  1237. i_wont_support_decrypt |= typemask(ep->type);
  1238. }
  1239. }
  1240. ++ep;
  1241. }
  1242. debug(F111,"encrypt_session_key (done)",ep->name,ep->type);
  1243. return(0);
  1244. }
  1245. /*
  1246. * Called when ENCRYPT END is received.
  1247. */
  1248. int
  1249. #ifdef CK_ANSIC
  1250. encrypt_end(VOID)
  1251. #else
  1252. encrypt_end()
  1253. #endif
  1254. {
  1255. decrypt_input = NULL;
  1256. EncryptKSGlobalHack->decrypt = NULL;
  1257. EncryptKSGlobalHack->decrypt_type = ENCTYPE_ANY;
  1258. #ifdef DEBUG
  1259. if (encrypt_debug_mode) {
  1260. sprintf(dbgbuf, ">>>Input is back to clear text"); /* safe */
  1261. debug(F110,"encrypt_end",dbgbuf,0);
  1262. }
  1263. #endif
  1264. if (encrypt_verbose) {
  1265. sprintf(dbgbuf, "Input is now clear text"); /* safe */
  1266. debug(F110,"encrypt_end",dbgbuf,0);
  1267. printf("%s\n",dbgbuf);
  1268. }
  1269. return(0);
  1270. }
  1271. /*
  1272. * Called when ENCRYPT REQUEST-END is received.
  1273. */
  1274. int
  1275. #ifdef CK_ANSIC
  1276. encrypt_request_end(VOID)
  1277. #else
  1278. encrypt_request_end()
  1279. #endif
  1280. {
  1281. encrypt_send_end();
  1282. return(0);
  1283. }
  1284. /*
  1285. * Called when ENCRYPT REQUEST-START is received. If we receive
  1286. * this before a type is picked, then that indicates that the
  1287. * other side wants us to start encrypting data as soon as we
  1288. * can.
  1289. */
  1290. int
  1291. #ifdef CK_ANSIC
  1292. encrypt_request_start(VOID)
  1293. #else
  1294. encrypt_request_start()
  1295. #endif
  1296. {
  1297. if (encrypt_mode != 0)
  1298. encrypt_start_output(encrypt_mode);
  1299. return(0);
  1300. }
  1301. static unsigned char str_keyid[(MAXKEYLEN*2)+5] = {
  1302. IAC, SB, TELOPT_ENCRYPTION
  1303. };
  1304. _PROTOTYP(int encrypt_keyid,(struct key_info *,unsigned char *,int));
  1305. int
  1306. #ifdef CK_ANSIC
  1307. encrypt_enc_keyid(unsigned char *keyid, int len)
  1308. #else
  1309. encrypt_enc_keyid(keyid, len) unsigned char *keyid; int len;
  1310. #endif
  1311. {
  1312. return(encrypt_keyid(&ki[1], keyid, len));
  1313. }
  1314. int
  1315. #ifdef CK_ANSIC
  1316. encrypt_dec_keyid(unsigned char *keyid, int len)
  1317. #else
  1318. encrypt_dec_keyid(keyid, len) unsigned char *keyid; int len;
  1319. #endif /* CK_ANSIC */
  1320. {
  1321. return(encrypt_keyid(&ki[0], keyid, len));
  1322. }
  1323. int
  1324. #ifdef CK_ANSIC
  1325. encrypt_keyid(struct key_info *kp, unsigned char *keyid, int len)
  1326. #else
  1327. encrypt_keyid(kp, keyid, len)
  1328. struct key_info *kp; unsigned char *keyid; int len;
  1329. #endif
  1330. {
  1331. Encryptions *ep;
  1332. int dir = kp->dir;
  1333. register int ret = 0;
  1334. if (!(ep = (*kp->getcrypt)(*kp->modep))) {
  1335. if (len == 0)
  1336. return(-1);
  1337. kp->keylen = 0;
  1338. } else if (len == 0 || len > MAXKEYLEN) {
  1339. /*
  1340. * Empty option or Key too long, indicates a failure.
  1341. */
  1342. if (kp->keylen == 0)
  1343. return(-1);
  1344. kp->keylen = 0;
  1345. if (ep->keyid)
  1346. (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
  1347. } else if ((len != kp->keylen) || (memcmp(keyid, kp->keyid, len) != 0)) {
  1348. /*
  1349. * Length or contents are different
  1350. */
  1351. kp->keylen = len;
  1352. memcpy(kp->keyid, keyid, len); /* length < MAXKEYLEN */
  1353. if (ep->keyid)
  1354. (void)(*ep->keyid)(dir, kp->keyid, &kp->keylen);
  1355. } else {
  1356. if (ep->keyid)
  1357. ret = (*ep->keyid)(dir, kp->keyid, &kp->keylen);
  1358. if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt)
  1359. encrypt_start_output(*kp->modep);
  1360. return(0);
  1361. }
  1362. encrypt_send_keyid(dir, kp->keyid, kp->keylen, 0);
  1363. return(0);
  1364. }
  1365. int
  1366. #ifdef CK_ANSIC
  1367. encrypt_send_keyid(int dir, unsigned char *keyid, int keylen, int saveit)
  1368. #else
  1369. encrypt_send_keyid(dir, keyid, keylen, saveit)
  1370. int dir; unsigned char *keyid; int keylen; int saveit;
  1371. #endif
  1372. {
  1373. unsigned char *strp;
  1374. #ifdef CK_SSL
  1375. if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1376. return(0);
  1377. #endif /* CK_SSL */
  1378. str_keyid[3] = (dir == DIR_ENCRYPT)
  1379. ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID;
  1380. if (saveit && keylen <= MAXKEYLEN) {
  1381. struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1];
  1382. memcpy(kp->keyid, keyid, keylen);
  1383. kp->keylen = keylen;
  1384. }
  1385. for (strp = &str_keyid[4]; keylen > 0; --keylen) {
  1386. if ((*strp++ = *keyid++) == IAC)
  1387. *strp++ = IAC;
  1388. }
  1389. *strp++ = IAC;
  1390. *strp++ = SE;
  1391. if (deblog || tn_deb || debses) {
  1392. int i;
  1393. sprintf(tn_msg,"TELNET SENT SB %s %s ",
  1394. TELOPT(TELOPT_ENCRYPTION),
  1395. (dir == DIR_ENCRYPT) ? "ENC-KEYID" : "DEC-KEYID"); /* safe */
  1396. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_keyid[4],strp-str_keyid-2-4);
  1397. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1398. debug(F100,tn_msg,"",0);
  1399. if (tn_deb || debses) tn_debug(tn_msg);
  1400. }
  1401. #ifdef OS2
  1402. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1403. #endif
  1404. ttol(str_keyid, strp - str_keyid);
  1405. #ifdef OS2
  1406. ReleaseTelnetMutex();
  1407. #endif
  1408. return(0);
  1409. }
  1410. VOID
  1411. #ifdef CK_ANSIC
  1412. encrypt_auto(int on)
  1413. #else
  1414. encrypt_auto(on) int on;
  1415. #endif
  1416. {
  1417. if (on < 0)
  1418. autoencrypt ^= 1;
  1419. else
  1420. autoencrypt = on ? 1 : 0;
  1421. }
  1422. VOID
  1423. #ifdef CK_ANSIC
  1424. decrypt_auto(int on)
  1425. #else
  1426. decrypt_auto(on) int on;
  1427. #endif
  1428. {
  1429. if (on < 0)
  1430. autodecrypt ^= 1;
  1431. else
  1432. autodecrypt = on ? 1 : 0;
  1433. }
  1434. VOID
  1435. #ifdef CK_ANSIC
  1436. encrypt_start_output(int type)
  1437. #else
  1438. encrypt_start_output(type) int type;
  1439. #endif
  1440. {
  1441. Encryptions *ep;
  1442. register unsigned char *p;
  1443. register int i;
  1444. #ifdef CK_SSL
  1445. if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1446. return;
  1447. #endif /* CK_SSL */
  1448. if (!(ep = findencryption(type))) {
  1449. #ifdef DEBUG
  1450. if (encrypt_debug_mode) {
  1451. sprintf(dbgbuf, ">>>Can't encrypt with type %s (%d)\n",
  1452. ENCTYPE_NAME_OK(type)
  1453. ? ENCTYPE_NAME(type) : "(unknown)",
  1454. type); /* safe */
  1455. debug(F110,"encrypt_start_output",dbgbuf,0);
  1456. }
  1457. #endif
  1458. return;
  1459. }
  1460. if (ep->start) {
  1461. i = (*ep->start)(DIR_ENCRYPT, 0);
  1462. #ifdef DEBUG
  1463. if (encrypt_debug_mode) {
  1464. sprintf(dbgbuf, ">>>Encrypt start: %s (%d) %s\n",
  1465. (i < 0) ? "failed" :
  1466. "initial negotiation in progress",
  1467. i, ENCTYPE_NAME(type)); /* safe */
  1468. debug(F110,"encrypt_start_output",dbgbuf,0);
  1469. }
  1470. #endif
  1471. if (i)
  1472. return;
  1473. }
  1474. if ( encrypt_output != ep->output ) {
  1475. p = str_start;
  1476. *p++ = IAC;
  1477. *p++ = SB;
  1478. *p++ = TELOPT_ENCRYPTION;
  1479. *p++ = ENCRYPT_START;
  1480. for (i = 0; i < ki[0].keylen; ++i) {
  1481. if (( *p++ = ki[0].keyid[i]) == IAC)
  1482. *p++ = IAC;
  1483. }
  1484. *p++ = IAC;
  1485. *p++ = SE;
  1486. if (deblog || tn_deb || debses) {
  1487. int i;
  1488. sprintf(tn_msg,"TELNET SENT SB %s START ",
  1489. TELOPT(TELOPT_ENCRYPTION)); /* safe */
  1490. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_start[4],p-str_start-2-4);
  1491. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1492. debug(F100,tn_msg,"",0);
  1493. if (tn_deb || debses) tn_debug(tn_msg);
  1494. }
  1495. #ifdef OS2
  1496. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1497. #endif
  1498. ttol(str_start, p - str_start);
  1499. #ifdef OS2
  1500. ReleaseTelnetMutex();
  1501. #endif
  1502. /*
  1503. * If we are already encrypting in some mode, then
  1504. * encrypt the ring (which includes our request) in
  1505. * the old mode, mark it all as "clear text" and then
  1506. * switch to the new mode.
  1507. */
  1508. encrypt_output = ep->output;
  1509. EncryptKSGlobalHack->encrypt = encrypt_ks_stream;
  1510. EncryptKSGlobalHack->encrypt_type = type;
  1511. encrypt_mode = type;
  1512. #ifdef DEBUG
  1513. if (encrypt_debug_mode) {
  1514. sprintf(dbgbuf, ">>>Started to encrypt output with type %s",
  1515. ENCTYPE_NAME(type)); /* safe */
  1516. debug(F110,"encrypt_start_output",dbgbuf,0);
  1517. }
  1518. #endif
  1519. if (encrypt_verbose) {
  1520. sprintf(dbgbuf, "Output is now encrypted with type %s",
  1521. ENCTYPE_NAME(type)); /* safe */
  1522. debug(F110,"encrypt_start_output",dbgbuf,0);
  1523. printf("%s\n",dbgbuf);
  1524. }
  1525. }
  1526. }
  1527. VOID
  1528. #ifdef CK_ANSIC
  1529. encrypt_send_end(VOID)
  1530. #else
  1531. encrypt_send_end()
  1532. #endif
  1533. {
  1534. #ifdef CK_SSL
  1535. if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1536. return;
  1537. #endif /* CK_SSL */
  1538. if (!encrypt_output)
  1539. return;
  1540. str_end[0] = IAC;
  1541. str_end[1] = SB;
  1542. str_end[2] = TELOPT_ENCRYPTION;
  1543. str_end[3] = ENCRYPT_END;
  1544. str_end[4] = IAC;
  1545. str_end[5] = SE;
  1546. if (deblog || tn_deb || debses) {
  1547. int i;
  1548. sprintf(tn_msg,"TELNET SENT SB %s END IAC SE",
  1549. TELOPT(TELOPT_ENCRYPTION)); /* safe */
  1550. debug(F100,tn_msg,"",0);
  1551. if (tn_deb || debses) tn_debug(tn_msg);
  1552. }
  1553. #ifdef OS2
  1554. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1555. #endif
  1556. ttol(str_end, sizeof(str_end));
  1557. #ifdef OS2
  1558. ReleaseTelnetMutex();
  1559. #endif
  1560. encrypt_output = 0;
  1561. EncryptKSGlobalHack->encrypt = NULL;
  1562. EncryptKSGlobalHack->encrypt_type = ENCTYPE_ANY;
  1563. #ifdef DEBUG
  1564. if (encrypt_debug_mode) {
  1565. sprintf(dbgbuf, ">>>Output is back to clear text"); /* safe */
  1566. debug(F110,"encrypt_send_end",dbgbuf,0);
  1567. }
  1568. #endif
  1569. if (encrypt_verbose) {
  1570. sprintf(dbgbuf, "Output is now clear text"); /* safe */
  1571. debug(F110,"encrypt_send_end",dbgbuf,0);
  1572. printf("%s\n",dbgbuf);
  1573. }
  1574. }
  1575. VOID
  1576. #ifdef CK_ANSIC
  1577. encrypt_send_request_start(VOID)
  1578. #else
  1579. encrypt_send_request_start()
  1580. #endif
  1581. {
  1582. register unsigned char *p;
  1583. register int i;
  1584. #ifdef CK_SSL
  1585. if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1586. return;
  1587. #endif /* CK_SSL */
  1588. p = str_start;
  1589. *p++ = IAC;
  1590. *p++ = SB;
  1591. *p++ = TELOPT_ENCRYPTION;
  1592. *p++ = ENCRYPT_REQSTART;
  1593. for (i = 0; i < ki[1].keylen; ++i) {
  1594. if (( *p++ = ki[1].keyid[i]) == IAC)
  1595. *p++ = IAC;
  1596. }
  1597. *p++ = IAC;
  1598. *p++ = SE;
  1599. if (deblog || tn_deb || debses) {
  1600. int i;
  1601. sprintf(tn_msg,"TELNET SENT SB %s REQUEST-START ",
  1602. TELOPT(TELOPT_ENCRYPTION)); /* safe */
  1603. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&str_start[4],p-str_start-2-4);
  1604. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1605. debug(F100,tn_msg,"",0);
  1606. if (tn_deb || debses) tn_debug(tn_msg);
  1607. }
  1608. #ifdef OS2
  1609. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1610. #endif
  1611. ttol(str_start, p - str_start);
  1612. #ifdef OS2
  1613. ReleaseTelnetMutex();
  1614. #endif
  1615. if (encrypt_debug_mode) {
  1616. sprintf(dbgbuf, ">>>Request input to be encrypted\n"); /* safe */
  1617. debug(F110,"encrypt_send_request_start",dbgbuf,0);
  1618. }
  1619. }
  1620. VOID
  1621. #ifdef CK_ANSIC
  1622. encrypt_send_request_end(VOID)
  1623. #else
  1624. encrypt_send_request_end()
  1625. #endif
  1626. {
  1627. #ifdef CK_SSL
  1628. if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  1629. return;
  1630. #endif /* CK_SSL */
  1631. str_end[0] = IAC;
  1632. str_end[1] = SB;
  1633. str_end[2] = TELOPT_ENCRYPTION;
  1634. str_end[3] = ENCRYPT_REQEND;
  1635. str_end[4] = IAC;
  1636. str_end[5] = SE;
  1637. if (deblog || tn_deb || debses) {
  1638. int i;
  1639. sprintf(tn_msg,"TELNET SENT SB %s REQEND IAC SE",
  1640. TELOPT(TELOPT_ENCRYPTION)); /* safe */
  1641. debug(F100,tn_msg,"",0);
  1642. if (tn_deb || debses) tn_debug(tn_msg);
  1643. }
  1644. #ifdef OS2
  1645. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1646. #endif
  1647. ttol(str_end, sizeof(str_end));
  1648. #ifdef OS2
  1649. ReleaseTelnetMutex();
  1650. #endif
  1651. if (encrypt_debug_mode) {
  1652. sprintf(dbgbuf, ">>>Request input to be clear text\n"); /* safe */
  1653. debug(F110,"encrypt_send_request_end",dbgbuf,0);
  1654. }
  1655. }
  1656. int
  1657. #ifdef CK_ANSIC
  1658. encrypt_is_encrypting(VOID)
  1659. #else
  1660. encrypt_is_encrypting()
  1661. #endif
  1662. {
  1663. if (encrypt_output)
  1664. return 1;
  1665. return 0;
  1666. }
  1667. int
  1668. #ifdef CK_ANSIC
  1669. encrypt_is_decrypting(VOID)
  1670. #else
  1671. encrypt_is_decrypting()
  1672. #endif
  1673. {
  1674. if (decrypt_input)
  1675. return 1;
  1676. return 0;
  1677. }
  1678. #ifdef DEBUG
  1679. void
  1680. encrypt_debug(mode)
  1681. int mode;
  1682. {
  1683. encrypt_debug_mode = mode;
  1684. }
  1685. #endif
  1686. #ifdef CK_DES
  1687. /*-
  1688. * Copyright (c) 1991, 1993
  1689. * The Regents of the University of California. All rights reserved.
  1690. *
  1691. * Redistribution and use in source and binary forms, with or without
  1692. * modification, are permitted provided that the following conditions
  1693. * are met:
  1694. * 1. Redistributions of source code must retain the above copyright
  1695. * notice, this list of conditions and the following disclaimer.
  1696. * 2. Redistributions in binary form must reproduce the above copyright
  1697. * notice, this list of conditions and the following disclaimer in the
  1698. * documentation and/or other materials provided with the distribution.
  1699. * 3. All advertising materials mentioning features or use of this software
  1700. * must display the following acknowledgement:
  1701. * This product includes software developed by the University of
  1702. * California, Berkeley and its contributors.
  1703. * 4. Neither the name of the University nor the names of its contributors
  1704. * may be used to endorse or promote products derived from this software
  1705. * without specific prior written permission.
  1706. *
  1707. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1708. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1709. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1710. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1711. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1712. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1713. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1714. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1715. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1716. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1717. * SUCH DAMAGE.
  1718. */
  1719. /* based on @(#)enc_des.c 8.1 (Berkeley) 6/4/93 */
  1720. #define CFB 0
  1721. #define OFB 1
  1722. #define NO_SEND_IV 1
  1723. #define NO_RECV_IV 2
  1724. #define NO_KEYID 4
  1725. #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
  1726. #define SUCCESS 0
  1727. #define xFAILED -1
  1728. Schedule test_sched;
  1729. struct des_stinfo {
  1730. Block str_output;
  1731. Block str_feed;
  1732. Block str_iv;
  1733. Block str_ikey;
  1734. #ifdef MIT_CURRENT
  1735. unsigned char str_keybytes[8];
  1736. krb5_keyblock str_key;
  1737. #else /* MIT_CURRENT */
  1738. Schedule str_sched;
  1739. int str_index;
  1740. #endif /* MIT_CURRENT */
  1741. int str_flagshift;
  1742. };
  1743. struct des_fb {
  1744. #ifndef MIT_CURRENT
  1745. Block krbdes_key;
  1746. Schedule krbdes_sched;
  1747. #endif /* MIT_CURRENT */
  1748. Block temp_feed;
  1749. unsigned char fb_feed[64];
  1750. int need_start;
  1751. int state[2];
  1752. int keyid[2];
  1753. int once;
  1754. #ifdef MIT_CURRENT
  1755. int validkey;
  1756. #endif /* MIT_CURRENT */
  1757. struct des_stinfo streams[2];
  1758. };
  1759. static struct des_fb des_fb[2];
  1760. struct des3_stinfo {
  1761. Block str_output;
  1762. Block str_feed;
  1763. Block str_iv;
  1764. Block str_ikey[3];
  1765. Schedule str_sched[3];
  1766. int str_index;
  1767. int str_flagshift;
  1768. };
  1769. struct des3_fb {
  1770. #ifndef MIT_CURRENT
  1771. Block krbdes_key[3];
  1772. Schedule krbdes_sched[3];
  1773. #endif /* MIT_CURRENT */
  1774. Block temp_feed;
  1775. unsigned char fb_feed[64];
  1776. int need_start;
  1777. int state[2];
  1778. int keyid[2];
  1779. int once;
  1780. #ifdef MIT_CURRENT
  1781. int validkey;
  1782. #endif /* MIT_CURRENT */
  1783. struct des3_stinfo streams[2];
  1784. };
  1785. static struct des3_fb des3_fb[2];
  1786. struct keyidlist {
  1787. char *keyid;
  1788. int keyidlen;
  1789. char *key;
  1790. int keylen;
  1791. int flags;
  1792. } keyidlist [] = {
  1793. { "\0", 1, 0, 0, 0 }, /* default key of zero */
  1794. { 0, 0, 0, 0, 0 }
  1795. };
  1796. #define KEYFLAG_MASK 03
  1797. #define KEYFLAG_NOINIT 00
  1798. #define KEYFLAG_INIT 01
  1799. #define KEYFLAG_OK 02
  1800. #define KEYFLAG_BAD 03
  1801. #define KEYFLAG_SHIFT 2
  1802. #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
  1803. #define FB64_IV 1
  1804. #define FB64_IV_OK 2
  1805. #define FB64_IV_BAD 3
  1806. #define FB64_CHALLENGE 4
  1807. #define FB64_RESPONSE 5
  1808. void fb64_stream_iv P((Block, struct des_stinfo *));
  1809. void fb64_init P((struct des_fb *));
  1810. static int fb64_start P((struct des_fb *, int, int));
  1811. int fb64_is P((unsigned char *, int, struct des_fb *));
  1812. int fb64_reply P((unsigned char *, int, struct des_fb *));
  1813. static int fb64_session P((Session_Key *, int, struct des_fb *));
  1814. void fb64_stream_key P((Block, struct des_stinfo *));
  1815. int fb64_keyid P((int, unsigned char *, int *, struct des_fb *));
  1816. #ifdef MIT_CURRENT
  1817. static void
  1818. #ifdef CK_ANSIC
  1819. ecb_encrypt(struct des_stinfo *stp, Block in, Block out)
  1820. #else /* CKANSIC */
  1821. ecb_encrypt(stp, in, out)
  1822. struct des_stinfo *stp;
  1823. Block in;
  1824. Block out;
  1825. #endif /* CK_ANSIC */
  1826. {
  1827. krb5_error_code code;
  1828. krb5_data din;
  1829. krb5_enc_data dout;
  1830. din.length = 8;
  1831. din.data = in;
  1832. dout.ciphertext.length = 8;
  1833. dout.ciphertext.data = out;
  1834. dout.enctype = ENCTYPE_UNKNOWN;
  1835. #ifdef CRYPT_DLL
  1836. code = krb5_c_encrypt(*p_k5_context, &stp->str_key, 0, 0,
  1837. &din, &dout);
  1838. #else /* CRYPT_DLL */
  1839. code = krb5_c_encrypt(k5_context, &stp->str_key, 0, 0,
  1840. &din, &dout);
  1841. #endif /* CRYPT_DLL */
  1842. /* XXX I'm not sure what to do if this fails */
  1843. if (code)
  1844. com_err("libtelnet", code, "encrypting stream data");
  1845. }
  1846. #endif /* MIT_CURRENT */
  1847. void
  1848. cfb64_init(server)
  1849. int server;
  1850. {
  1851. fb64_init(&des_fb[CFB]);
  1852. des_fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
  1853. des_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
  1854. des_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
  1855. }
  1856. void
  1857. ofb64_init(server)
  1858. int server;
  1859. {
  1860. fb64_init(&des_fb[OFB]);
  1861. des_fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
  1862. des_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
  1863. des_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
  1864. }
  1865. void
  1866. fb64_init(fbp)
  1867. register struct des_fb *fbp;
  1868. {
  1869. memset((void *)fbp, 0, sizeof(*fbp));
  1870. fbp->state[0] = fbp->state[1] = xFAILED;
  1871. fbp->fb_feed[0] = IAC;
  1872. fbp->fb_feed[1] = SB;
  1873. fbp->fb_feed[2] = TELOPT_ENCRYPTION;
  1874. fbp->fb_feed[3] = ENCRYPT_IS;
  1875. }
  1876. /*
  1877. * Returns:
  1878. * -1: some error. Negotiation is done, encryption not ready.
  1879. * 0: Successful, initial negotiation all done.
  1880. * 1: successful, negotiation not done yet.
  1881. * 2: Not yet. Other things (like getting the key from
  1882. * Kerberos) have to happen before we can continue.
  1883. */
  1884. int
  1885. cfb64_start(dir, server)
  1886. int dir;
  1887. int server;
  1888. {
  1889. return(fb64_start(&des_fb[CFB], dir, server));
  1890. }
  1891. int
  1892. ofb64_start(dir, server)
  1893. int dir;
  1894. int server;
  1895. {
  1896. return(fb64_start(&des_fb[OFB], dir, server));
  1897. }
  1898. static int
  1899. fb64_start(fbp, dir, server)
  1900. struct des_fb *fbp;
  1901. int dir;
  1902. int server;
  1903. {
  1904. int x;
  1905. unsigned char *p;
  1906. register int state;
  1907. switch (dir) {
  1908. case DIR_DECRYPT:
  1909. /*
  1910. * This is simply a request to have the other side
  1911. * start output (our input). He will negotiate an
  1912. * IV so we need not look for it.
  1913. */
  1914. state = fbp->state[dir-1];
  1915. if (state == xFAILED)
  1916. state = IN_PROGRESS;
  1917. break;
  1918. case DIR_ENCRYPT:
  1919. state = fbp->state[dir-1];
  1920. if (state == xFAILED)
  1921. state = IN_PROGRESS;
  1922. else if ((state & NO_SEND_IV) == 0)
  1923. break;
  1924. #ifdef MIT_CURRENT
  1925. if (!fbp->validkey) {
  1926. fbp->need_start = 1;
  1927. break;
  1928. }
  1929. #else /* MIT_CURRENT */
  1930. if (!VALIDKEY(fbp->krbdes_key)) {
  1931. fbp->need_start = 1;
  1932. break;
  1933. }
  1934. #endif /* MIT_CURRENT */
  1935. state &= ~NO_SEND_IV;
  1936. state |= NO_RECV_IV;
  1937. /*
  1938. * Create a random feed and send it over.
  1939. */
  1940. #ifdef MIT_CURRENT
  1941. {
  1942. krb5_data d;
  1943. krb5_error_code code;
  1944. d.data = fbp->temp_feed;
  1945. d.length = sizeof(fbp->temp_feed);
  1946. #ifdef CRYPT_DLL
  1947. if (code = krb5_c_random_make_octets(*p_k5_context,&d))
  1948. return(xFAILED);
  1949. #else /* CRYPT_DLL */
  1950. if (code = krb5_c_random_make_octets(k5_context,&d))
  1951. return(xFAILED);
  1952. #endif /* CRYPT_DLL */
  1953. }
  1954. #else /* MIT_CURRENT */
  1955. des_new_random_key(fbp->temp_feed);
  1956. des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  1957. fbp->krbdes_sched, 1);
  1958. #endif /* MIT_CURRENT */
  1959. p = fbp->fb_feed + 3;
  1960. *p++ = ENCRYPT_IS;
  1961. p++;
  1962. *p++ = FB64_IV;
  1963. for (x = 0; x < sizeof(Block); ++x) {
  1964. if (( *p++ = fbp->temp_feed[x]) == IAC)
  1965. *p++ = IAC;
  1966. }
  1967. *p++ = IAC;
  1968. *p++ = SE;
  1969. if (deblog || tn_deb || debses) {
  1970. int i;
  1971. sprintf(tn_msg,
  1972. "TELNET SENT SB %s IS %s FB64_IV ",
  1973. TELOPT(fbp->fb_feed[2]),
  1974. enctype_names[fbp->fb_feed[4]]); /* safe */
  1975. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],
  1976. (p-fbp->fb_feed)-2-6);
  1977. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  1978. debug(F100,tn_msg,"",0);
  1979. if (tn_deb || debses) tn_debug(tn_msg);
  1980. }
  1981. #ifdef OS2
  1982. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  1983. #endif
  1984. ttol(fbp->fb_feed, p - fbp->fb_feed);
  1985. #ifdef OS2
  1986. ReleaseTelnetMutex();
  1987. #endif
  1988. break;
  1989. default:
  1990. return(xFAILED);
  1991. }
  1992. return(fbp->state[dir-1] = state);
  1993. }
  1994. /*
  1995. * Returns:
  1996. * -1: some error. Negotiation is done, encryption not ready.
  1997. * 0: Successful, initial negotiation all done.
  1998. * 1: successful, negotiation not done yet.
  1999. */
  2000. int
  2001. cfb64_is(data, cnt)
  2002. unsigned char *data;
  2003. int cnt;
  2004. {
  2005. return(fb64_is(data, cnt, &des_fb[CFB]));
  2006. }
  2007. int
  2008. ofb64_is(data, cnt)
  2009. unsigned char *data;
  2010. int cnt;
  2011. {
  2012. return(fb64_is(data, cnt, &des_fb[OFB]));
  2013. }
  2014. int
  2015. fb64_is(data, cnt, fbp)
  2016. unsigned char *data;
  2017. int cnt;
  2018. struct des_fb *fbp;
  2019. {
  2020. unsigned char *p;
  2021. register int state = fbp->state[DIR_DECRYPT-1];
  2022. if (cnt-- < 1)
  2023. goto failure;
  2024. #ifdef CK_SSL
  2025. if (!TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2026. #endif /* CK_SSL */
  2027. switch (*data++) {
  2028. case FB64_IV:
  2029. if (cnt != sizeof(Block)) {
  2030. #ifdef DEBUG
  2031. if (encrypt_debug_mode)
  2032. printf("CFB64: initial vector failed on size\r\n");
  2033. #endif
  2034. state = xFAILED;
  2035. goto failure;
  2036. }
  2037. #ifdef DEBUG
  2038. if (encrypt_debug_mode) {
  2039. printf("CFB64: initial vector received\r\n");
  2040. printf("Initializing Decrypt stream\r\n");
  2041. }
  2042. #endif
  2043. fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
  2044. p = fbp->fb_feed + 3;
  2045. *p++ = ENCRYPT_REPLY;
  2046. p++;
  2047. *p++ = FB64_IV_OK;
  2048. *p++ = IAC;
  2049. *p++ = SE;
  2050. if (deblog || tn_deb || debses) {
  2051. int i;
  2052. sprintf(tn_msg,
  2053. "TELNET SENT SB %s REPLY %s FB64_IV_OK ",
  2054. TELOPT(fbp->fb_feed[2]),
  2055. enctype_names[fbp->fb_feed[4]]); /* safe */
  2056. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],
  2057. (p-fbp->fb_feed)-2-6);
  2058. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2059. debug(F100,tn_msg,"",0);
  2060. if (tn_deb || debses) tn_debug(tn_msg);
  2061. }
  2062. #ifdef OS2
  2063. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2064. #endif
  2065. ttol(fbp->fb_feed, p - fbp->fb_feed);
  2066. #ifdef OS2
  2067. ReleaseTelnetMutex();
  2068. #endif
  2069. state = IN_PROGRESS;
  2070. break;
  2071. default:
  2072. #if 0
  2073. if (encrypt_debug_mode) {
  2074. printf("Unknown option type: %d\r\n", *(data-1));
  2075. printf("\r\n");
  2076. }
  2077. #endif
  2078. /* FALL THROUGH */
  2079. failure:
  2080. /*
  2081. * We failed. Send an FB64_IV_BAD option
  2082. * to the other side so it will know that
  2083. * things failed.
  2084. */
  2085. p = fbp->fb_feed + 3;
  2086. *p++ = ENCRYPT_REPLY;
  2087. p++;
  2088. *p++ = FB64_IV_BAD;
  2089. *p++ = IAC;
  2090. *p++ = SE;
  2091. if (deblog || tn_deb || debses) {
  2092. int i;
  2093. sprintf(tn_msg,
  2094. "TELNET SENT SB %s REPLY %s FB64_IV_BAD ",
  2095. TELOPT(fbp->fb_feed[2]),
  2096. enctype_names[fbp->fb_feed[4]]); /* safe */
  2097. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],
  2098. (p-fbp->fb_feed)-2-6);
  2099. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2100. debug(F100,tn_msg,"",0);
  2101. if (tn_deb || debses) tn_debug(tn_msg);
  2102. }
  2103. #ifdef OS2
  2104. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2105. #endif
  2106. ttol(fbp->fb_feed, p - fbp->fb_feed);
  2107. #ifdef OS2
  2108. ReleaseTelnetMutex();
  2109. #endif
  2110. break;
  2111. }
  2112. return(fbp->state[DIR_DECRYPT-1] = state);
  2113. }
  2114. /*
  2115. * Returns:
  2116. * -1: some error. Negotiation is done, encryption not ready.
  2117. * 0: Successful, initial negotiation all done.
  2118. * 1: successful, negotiation not done yet.
  2119. */
  2120. int
  2121. cfb64_reply(data, cnt)
  2122. unsigned char *data;
  2123. int cnt;
  2124. {
  2125. return(fb64_reply(data, cnt, &des_fb[CFB]));
  2126. }
  2127. int
  2128. ofb64_reply(data, cnt)
  2129. unsigned char *data;
  2130. int cnt;
  2131. {
  2132. return(fb64_reply(data, cnt, &des_fb[OFB]));
  2133. }
  2134. int
  2135. fb64_reply(data, cnt, fbp)
  2136. unsigned char *data;
  2137. int cnt;
  2138. struct des_fb *fbp;
  2139. {
  2140. register int state = fbp->state[DIR_ENCRYPT-1];
  2141. if (cnt-- < 1)
  2142. goto failure;
  2143. switch (*data++) {
  2144. case FB64_IV_OK:
  2145. fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  2146. if (state == xFAILED)
  2147. state = IN_PROGRESS;
  2148. state &= ~NO_RECV_IV;
  2149. encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
  2150. break;
  2151. case FB64_IV_BAD:
  2152. memset(fbp->temp_feed, 0, sizeof(Block));
  2153. fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  2154. state = xFAILED;
  2155. break;
  2156. default:
  2157. #if 0
  2158. if (encrypt_debug_mode) {
  2159. printf("Unknown option type: %d\r\n", data[-1]);
  2160. printf("\r\n");
  2161. }
  2162. #endif
  2163. /* FALL THROUGH */
  2164. failure:
  2165. state = xFAILED;
  2166. break;
  2167. }
  2168. return(fbp->state[DIR_ENCRYPT-1] = state);
  2169. }
  2170. int
  2171. cfb64_session(key, server)
  2172. Session_Key *key;
  2173. int server;
  2174. {
  2175. return(fb64_session(key, server, &des_fb[CFB]));
  2176. }
  2177. int
  2178. ofb64_session(key, server)
  2179. Session_Key *key;
  2180. int server;
  2181. {
  2182. return(fb64_session(key, server, &des_fb[OFB]));
  2183. }
  2184. static int
  2185. fb64_session(key, server, fbp)
  2186. Session_Key *key;
  2187. int server;
  2188. struct des_fb *fbp;
  2189. {
  2190. int rc=0;
  2191. int use2keys;
  2192. struct des_stinfo * s_stream;
  2193. struct des_stinfo * c_stream;
  2194. if(server) {
  2195. s_stream = &fbp->streams[DIR_ENCRYPT-1];
  2196. c_stream = &fbp->streams[DIR_DECRYPT-1];
  2197. }
  2198. else {
  2199. s_stream = &fbp->streams[DIR_DECRYPT-1];
  2200. c_stream = &fbp->streams[DIR_ENCRYPT-1];
  2201. }
  2202. if (!key || key->length < sizeof(Block)) {
  2203. CHAR buf[80];
  2204. sprintf((char *)buf,"Can't set DES session key (%d < %d)",
  2205. key ? key->length : 0, sizeof(Block)); /* safe */
  2206. #ifdef DEBUG
  2207. if (encrypt_debug_mode)
  2208. printf("%s\r\n",buf);
  2209. #endif
  2210. debug(F110,"fb64_session",buf,0);
  2211. return(-1);
  2212. }
  2213. use2keys = (key->type == SK_DES ||
  2214. key->length < 2 * sizeof(Block)) ? 0 : 1;
  2215. #ifdef MIT_CURRENT
  2216. if(use2keys) {
  2217. memcpy((void *) fbp->keybytes,
  2218. (void *) (key->data + sizeof(Block)), sizeof(Block));
  2219. des_fixup_key_parity(fbp->);
  2220. fb64_stream_key(fbp->krbdes_key, s_stream);
  2221. }
  2222. memcpy((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
  2223. if (key->type != SK_DES)
  2224. des_fixup_key_parity(fbp->krbdes_key);
  2225. if(!use2keys)
  2226. fb64_stream_key(fbp->krbdes_key, s_stream);
  2227. fb64_stream_key(fbp->krbdes_key, c_stream);
  2228. fbp->validkey = 1;
  2229. fb64_stream_key(key->data, &fbp->streams[DIR_ENCRYPT-1]);
  2230. fb64_stream_key(key->data, &fbp->streams[DIR_DECRYPT-1]);
  2231. #else /* MIT_CURRENT */
  2232. if(use2keys) {
  2233. memcpy((void *) fbp->krbdes_key,
  2234. (void *) (key->data + sizeof(Block)), sizeof(Block));
  2235. des_fixup_key_parity(fbp->krbdes_key);
  2236. fb64_stream_key(fbp->krbdes_key, s_stream);
  2237. }
  2238. memcpy((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
  2239. if (key->type != SK_DES)
  2240. des_fixup_key_parity(fbp->krbdes_key);
  2241. if(!use2keys)
  2242. fb64_stream_key(fbp->krbdes_key, s_stream);
  2243. fb64_stream_key(fbp->krbdes_key, c_stream);
  2244. if (fbp->once == 0) {
  2245. des_set_random_generator_seed(fbp->krbdes_key);
  2246. fbp->once = 1;
  2247. }
  2248. memset(fbp->krbdes_sched,0,sizeof(Schedule));
  2249. ckhexdump("fb64_session_key",fbp->krbdes_key,8);
  2250. rc = des_key_sched(fbp->krbdes_key, fbp->krbdes_sched);
  2251. if ( rc == -1 ) {
  2252. printf("?Invalid DES key specified for encryption\n");
  2253. debug(F110,"fb64_session_key",
  2254. "invalid DES Key specified for encryption",0);
  2255. } else if ( rc == -2 ) {
  2256. printf("?Weak DES key specified for encryption\n");
  2257. debug(F110,"fb64_session_key",
  2258. "weak DES Key specified for encryption",0);
  2259. } else if ( rc != 0 ) {
  2260. printf("?Key Schedule not created by encryption\n");
  2261. debug(F110,"fb64_session_key",
  2262. "Key Schedule not created by encryption",0);
  2263. }
  2264. ckhexdump("fb64_session_key schedule",fbp->krbdes_sched,8*16);
  2265. #endif /* MIT_CURRENT */
  2266. /*
  2267. * Now look to see if krbdes_start() was was waiting for
  2268. * the key to show up. If so, go ahead an call it now
  2269. * that we have the key.
  2270. */
  2271. if (fbp->need_start) {
  2272. fbp->need_start = 0;
  2273. fb64_start(fbp, DIR_ENCRYPT, server);
  2274. }
  2275. return(0);
  2276. }
  2277. /*
  2278. * We only accept a keyid of 0. If we get a keyid of
  2279. * 0, then mark the state as SUCCESS.
  2280. */
  2281. int
  2282. cfb64_keyid(dir, kp, lenp)
  2283. int dir, *lenp;
  2284. unsigned char *kp;
  2285. {
  2286. return(fb64_keyid(dir, kp, lenp, &des_fb[CFB]));
  2287. }
  2288. int
  2289. ofb64_keyid(dir, kp, lenp)
  2290. int dir, *lenp;
  2291. unsigned char *kp;
  2292. {
  2293. return(fb64_keyid(dir, kp, lenp, &des_fb[OFB]));
  2294. }
  2295. int
  2296. fb64_keyid(dir, kp, lenp, fbp)
  2297. int dir, *lenp;
  2298. unsigned char *kp;
  2299. struct des_fb *fbp;
  2300. {
  2301. register int state = fbp->state[dir-1];
  2302. if (*lenp != 1 || (*kp != '\0')) {
  2303. *lenp = 0;
  2304. return(state);
  2305. }
  2306. if (state == xFAILED)
  2307. state = IN_PROGRESS;
  2308. state &= ~NO_KEYID;
  2309. return(fbp->state[dir-1] = state);
  2310. }
  2311. #if 0
  2312. void
  2313. fb64_printsub(data, cnt, buf, buflen, type)
  2314. unsigned char *data, *buf, *type;
  2315. int cnt, buflen;
  2316. {
  2317. char lbuf[64];
  2318. register int i;
  2319. char *cp;
  2320. buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
  2321. buflen -= 1;
  2322. switch(data[2]) {
  2323. case FB64_IV:
  2324. sprintf(lbuf, "%s_IV", type);
  2325. cp = lbuf;
  2326. goto common;
  2327. case FB64_IV_OK:
  2328. sprintf(lbuf, "%s_IV_OK", type);
  2329. cp = lbuf;
  2330. goto common;
  2331. case FB64_IV_BAD:
  2332. sprintf(lbuf, "%s_IV_BAD", type);
  2333. cp = lbuf;
  2334. goto common;
  2335. case FB64_CHALLENGE:
  2336. sprintf(lbuf, "%s_CHALLENGE", type);
  2337. cp = lbuf;
  2338. goto common;
  2339. case FB64_RESPONSE:
  2340. sprintf(lbuf, "%s_RESPONSE", type);
  2341. cp = lbuf;
  2342. goto common;
  2343. default:
  2344. sprintf(lbuf, " %d (unknown)", data[2]);
  2345. cp = lbuf;
  2346. common:
  2347. for (; (buflen > 0) && (*buf = *cp++); buf++)
  2348. buflen--;
  2349. for (i = 3; i < cnt; i++) {
  2350. sprintf(lbuf, " %d", data[i]);
  2351. for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
  2352. buflen--;
  2353. }
  2354. break;
  2355. }
  2356. }
  2357. void
  2358. cfb64_printsub(data, cnt, buf, buflen)
  2359. unsigned char *data, *buf;
  2360. int cnt, buflen;
  2361. {
  2362. fb64_printsub(data, cnt, buf, buflen, "CFB64");
  2363. }
  2364. void
  2365. ofb64_printsub(data, cnt, buf, buflen)
  2366. unsigned char *data, *buf;
  2367. int cnt, buflen;
  2368. {
  2369. fb64_printsub(data, cnt, buf, buflen, "OFB64");
  2370. }
  2371. #endif
  2372. void
  2373. fb64_stream_iv(seed, stp)
  2374. Block seed;
  2375. register struct des_stinfo *stp;
  2376. {
  2377. int rc=0;
  2378. memcpy(stp->str_iv, seed, sizeof(Block));
  2379. memcpy(stp->str_output, seed, sizeof(Block));
  2380. memset(stp->str_sched,0,sizeof(Schedule));
  2381. ckhexdump("fb64_stream_iv",stp->str_ikey,8);
  2382. #ifndef MIT_CURRENT
  2383. rc = des_key_sched(stp->str_ikey, stp->str_sched);
  2384. if ( rc == -1 ) {
  2385. printf("?Invalid DES key specified for encryption\r\n");
  2386. debug(F110,"fb64_stream_iv",
  2387. "invalid DES Key specified for encryption",0);
  2388. } else if ( rc == -2 ) {
  2389. printf("?Weak DES key specified for encryption\r\n");
  2390. debug(F110,"fb64_stream_iv",
  2391. "weak DES Key specified for encryption",0);
  2392. } else if ( rc != 0 ) {
  2393. printf("?Key Schedule not created by encryption\r\n");
  2394. debug(F110,"fb64_stream_iv",
  2395. "Key Schedule not created by encryption",0);
  2396. }
  2397. ckhexdump("fb64_stream_iv schedule",stp->str_sched,8*16);
  2398. #endif /* MIT_CURRENT */
  2399. stp->str_index = sizeof(Block);
  2400. }
  2401. void
  2402. fb64_stream_key(key, stp)
  2403. Block key;
  2404. register struct des_stinfo *stp;
  2405. {
  2406. int rc = 0;
  2407. #ifdef MIT_CURRENT
  2408. memcpy(stp->str_keybytes, key, sizeof(Block));
  2409. stp->str_key.length = 8;
  2410. stp->str_key.contents = stp->str_keybytes;
  2411. /* the original version of this code uses des ecb mode, but
  2412. it only ever does one block at a time. cbc with a zero iv
  2413. is identical */
  2414. stp->str_key.enctype = ENCTYPE_DES_CBC_RAW;
  2415. #else /* MIT_CURRENT */
  2416. memcpy(stp->str_ikey, key, sizeof(Block));
  2417. memset(stp->str_sched,0,sizeof(Schedule));
  2418. ckhexdump("fb64_stream_key",key,8);
  2419. rc = des_key_sched(key, stp->str_sched);
  2420. if ( rc == -1 ) {
  2421. printf("?Invalid DES key specified for encryption\r\n");
  2422. debug(F110,"fb64_stream_key",
  2423. "invalid DES Key specified for encryption",0);
  2424. } else if ( rc == -2 ) {
  2425. printf("?Weak DES key specified for encryption\r\n");
  2426. debug(F110,"fb64_stream_key",
  2427. "weak DES Key specified for encryption",0);
  2428. } else if ( rc != 0 ) {
  2429. printf("?Key Schedule not created by encryption\r\n");
  2430. debug(F110,"fb64_stream_key",
  2431. "Key Schedule not created by encryption",0);
  2432. }
  2433. ckhexdump("fb64_stream_key schedule",stp->str_sched,8*16);
  2434. #endif /* MIT_CURRENT */
  2435. memcpy(stp->str_output, stp->str_iv, sizeof(Block));
  2436. stp->str_index = sizeof(Block);
  2437. }
  2438. /*
  2439. * DES 64 bit Cipher Feedback
  2440. *
  2441. * key --->+-----+
  2442. * +->| DES |--+
  2443. * | +-----+ |
  2444. * | v
  2445. * INPUT --(--------->(+)+---> DATA
  2446. * | |
  2447. * +-------------+
  2448. *
  2449. *
  2450. * Given:
  2451. * iV: Initial vector, 64 bits (8 bytes) long.
  2452. * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  2453. * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  2454. *
  2455. * V0 = DES(iV, key)
  2456. * On = Dn ^ Vn
  2457. * V(n+1) = DES(On, key)
  2458. */
  2459. void
  2460. cfb64_encrypt(s, c)
  2461. register unsigned char *s;
  2462. int c;
  2463. {
  2464. register struct des_stinfo *stp = &des_fb[CFB].streams[DIR_ENCRYPT-1];
  2465. register int index;
  2466. index = stp->str_index;
  2467. while (c-- > 0) {
  2468. if (index == sizeof(Block)) {
  2469. Block b;
  2470. #ifdef MIT_CURRENT
  2471. ecb_encrypt(stp, stp->str_output, b);
  2472. #else /* MIT_CURRENT */
  2473. des_ecb_encrypt(stp->str_output, b, stp->str_sched, 1);
  2474. #endif /* MIT_CURRENT */
  2475. memcpy(stp->str_feed,b,sizeof(Block));
  2476. index = 0;
  2477. }
  2478. /* On encryption, we store (feed ^ data) which is cypher */
  2479. *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
  2480. s++;
  2481. index++;
  2482. }
  2483. stp->str_index = index;
  2484. }
  2485. int
  2486. cfb64_decrypt(data)
  2487. int data;
  2488. {
  2489. register struct des_stinfo *stp = &des_fb[CFB].streams[DIR_DECRYPT-1];
  2490. int index;
  2491. if (data == -1) {
  2492. /*
  2493. * Back up one byte. It is assumed that we will
  2494. * never back up more than one byte. If we do, this
  2495. * may or may not work.
  2496. */
  2497. if (stp->str_index)
  2498. --stp->str_index;
  2499. return(0);
  2500. }
  2501. index = stp->str_index++;
  2502. if (index == sizeof(Block)) {
  2503. Block b;
  2504. #ifdef MIT_CURRENT
  2505. ecb_encrypt(stp, stp->str_output, b);
  2506. #else /* MIT_CURRENT */
  2507. des_ecb_encrypt(stp->str_output, b, stp->str_sched, 1);
  2508. #endif /* MIT_CURRENT */
  2509. memcpy(stp->str_feed, b, sizeof(Block));
  2510. stp->str_index = 1; /* Next time will be 1 */
  2511. index = 0; /* But now use 0 */
  2512. }
  2513. /* On decryption we store (data) which is cypher. */
  2514. stp->str_output[index] = data;
  2515. return(data ^ stp->str_feed[index]);
  2516. }
  2517. /*
  2518. * DES 64 bit Output Feedback
  2519. *
  2520. * key --->+-----+
  2521. * +->| DES |--+
  2522. * | +-----+ |
  2523. * +-----------+
  2524. * v
  2525. * INPUT -------->(+) ----> DATA
  2526. *
  2527. * Given:
  2528. * iV: Initial vector, 64 bits (8 bytes) long.
  2529. * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  2530. * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  2531. *
  2532. * V0 = DES(iV, key)
  2533. * V(n+1) = DES(Vn, key)
  2534. * On = Dn ^ Vn
  2535. */
  2536. void
  2537. ofb64_encrypt(s, c)
  2538. register unsigned char *s;
  2539. int c;
  2540. {
  2541. register struct des_stinfo *stp = &des_fb[OFB].streams[DIR_ENCRYPT-1];
  2542. register int index;
  2543. index = stp->str_index;
  2544. while (c-- > 0) {
  2545. if (index == sizeof(Block)) {
  2546. Block b;
  2547. #ifdef MIT_CURRENT
  2548. ecb_encrypt(stp, stp->str_feed, b);
  2549. #else /* MIT_CURRENT */
  2550. des_ecb_encrypt(stp->str_feed, b, stp->str_sched, 1);
  2551. #endif /* MIT_CURRENT */
  2552. memcpy(stp->str_feed,b,sizeof(Block));
  2553. index = 0;
  2554. }
  2555. *s++ ^= stp->str_feed[index];
  2556. index++;
  2557. }
  2558. stp->str_index = index;
  2559. }
  2560. int
  2561. ofb64_decrypt(data)
  2562. int data;
  2563. {
  2564. register struct des_stinfo *stp = &des_fb[OFB].streams[DIR_DECRYPT-1];
  2565. int index;
  2566. if (data == -1) {
  2567. /*
  2568. * Back up one byte. It is assumed that we will
  2569. * never back up more than one byte. If we do, this
  2570. * may or may not work.
  2571. */
  2572. if (stp->str_index)
  2573. --stp->str_index;
  2574. return(0);
  2575. }
  2576. index = stp->str_index++;
  2577. if (index == sizeof(Block)) {
  2578. Block b;
  2579. #ifdef MIT_CURRENT
  2580. ecb_encrypt(stp, stp->str_feed, b);
  2581. #else /* MIT_CURRENT */
  2582. des_ecb_encrypt(stp->str_feed, b, stp->str_sched, 1);
  2583. #endif /* MIT_CURRENT */
  2584. memcpy(stp->str_feed, b, sizeof(Block));
  2585. stp->str_index = 1; /* Next time will be 1 */
  2586. index = 0; /* But now use 0 */
  2587. }
  2588. return(data ^ stp->str_feed[index]);
  2589. }
  2590. void des3_fb64_stream_iv P((Block, struct des3_stinfo *));
  2591. void des3_fb64_init P((struct des3_fb *));
  2592. static int des3_fb64_start P((struct des3_fb *, int, int));
  2593. int des3_fb64_is P((unsigned char *, int, struct des3_fb *));
  2594. int des3_fb64_reply P((unsigned char *, int, struct des3_fb *));
  2595. static int des3_fb64_session P((Session_Key *, int, struct des3_fb *));
  2596. void des3_fb64_stream_key P((Block *, struct des3_stinfo *));
  2597. int des3_fb64_keyid P((int, unsigned char *, int *, struct des3_fb *));
  2598. void
  2599. des3_cfb64_init(server)
  2600. int server;
  2601. {
  2602. des3_fb64_init(&des3_fb[CFB]);
  2603. des3_fb[CFB].fb_feed[4] = ENCTYPE_DES3_CFB64;
  2604. des3_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
  2605. des3_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
  2606. }
  2607. void
  2608. des3_ofb64_init(server)
  2609. int server;
  2610. {
  2611. des3_fb64_init(&des3_fb[OFB]);
  2612. des3_fb[OFB].fb_feed[4] = ENCTYPE_DES3_OFB64;
  2613. des3_fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
  2614. des3_fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
  2615. }
  2616. void
  2617. des3_fb64_init(fbp)
  2618. register struct des3_fb *fbp;
  2619. {
  2620. memset((void *)fbp, 0, sizeof(*fbp));
  2621. fbp->state[0] = fbp->state[1] = xFAILED;
  2622. fbp->fb_feed[0] = IAC;
  2623. fbp->fb_feed[1] = SB;
  2624. fbp->fb_feed[2] = TELOPT_ENCRYPTION;
  2625. fbp->fb_feed[3] = ENCRYPT_IS;
  2626. }
  2627. /*
  2628. * Returns:
  2629. * -1: some error. Negotiation is done, encryption not ready.
  2630. * 0: Successful, initial negotiation all done.
  2631. * 1: successful, negotiation not done yet.
  2632. * 2: Not yet. Other things (like getting the key from
  2633. * Kerberos) have to happen before we can continue.
  2634. */
  2635. int
  2636. des3_cfb64_start(dir, server)
  2637. int dir;
  2638. int server;
  2639. {
  2640. return(des3_fb64_start(&des3_fb[CFB], dir, server));
  2641. }
  2642. int
  2643. des3_ofb64_start(dir, server)
  2644. int dir;
  2645. int server;
  2646. {
  2647. return(des3_fb64_start(&des3_fb[OFB], dir, server));
  2648. }
  2649. static int
  2650. des3_fb64_start(fbp, dir, server)
  2651. struct des3_fb *fbp;
  2652. int dir;
  2653. int server;
  2654. {
  2655. int x;
  2656. unsigned char *p;
  2657. register int state;
  2658. switch (dir) {
  2659. case DIR_DECRYPT:
  2660. /*
  2661. * This is simply a request to have the other side
  2662. * start output (our input). He will negotiate an
  2663. * IV so we need not look for it.
  2664. */
  2665. state = fbp->state[dir-1];
  2666. if (state == xFAILED)
  2667. state = IN_PROGRESS;
  2668. break;
  2669. case DIR_ENCRYPT:
  2670. state = fbp->state[dir-1];
  2671. if (state == xFAILED)
  2672. state = IN_PROGRESS;
  2673. else if ((state & NO_SEND_IV) == 0)
  2674. break;
  2675. if (!VALIDKEY(fbp->krbdes_key[0]) ||
  2676. !VALIDKEY(fbp->krbdes_key[1]) ||
  2677. !VALIDKEY(fbp->krbdes_key[2]) ) {
  2678. fbp->need_start = 1;
  2679. break;
  2680. }
  2681. state &= ~NO_SEND_IV;
  2682. state |= NO_RECV_IV;
  2683. /*
  2684. * Create a random feed and send it over.
  2685. */
  2686. des_new_random_key(fbp->temp_feed);
  2687. #ifdef LIBDES
  2688. des_ecb3_encrypt(fbp->temp_feed, fbp->temp_feed,
  2689. fbp->krbdes_sched[0],
  2690. fbp->krbdes_sched[1],
  2691. fbp->krbdes_sched[2],
  2692. 1);
  2693. #else /* LIBDES */
  2694. des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2695. fbp->krbdes_sched[0], 1);
  2696. des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2697. fbp->krbdes_sched[1], 0);
  2698. des_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  2699. fbp->krbdes_sched[2], 1);
  2700. #endif /* LIBDES */
  2701. p = fbp->fb_feed + 3;
  2702. *p++ = ENCRYPT_IS;
  2703. p++;
  2704. *p++ = FB64_IV;
  2705. for (x = 0; x < sizeof(Block); ++x) {
  2706. if (( *p++ = fbp->temp_feed[x]) == IAC)
  2707. *p++ = IAC;
  2708. }
  2709. *p++ = IAC;
  2710. *p++ = SE;
  2711. if (deblog || tn_deb || debses) {
  2712. int i;
  2713. sprintf(tn_msg,
  2714. "TELNET SENT SB %s IS %s FB64_IV ",
  2715. TELOPT(fbp->fb_feed[2]),
  2716. enctype_names[fbp->fb_feed[4]]); /* safe */
  2717. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],
  2718. (p-fbp->fb_feed)-2-6);
  2719. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2720. debug(F100,tn_msg,"",0);
  2721. if (tn_deb || debses) tn_debug(tn_msg);
  2722. }
  2723. #ifdef OS2
  2724. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2725. #endif
  2726. ttol(fbp->fb_feed, p - fbp->fb_feed);
  2727. #ifdef OS2
  2728. ReleaseTelnetMutex();
  2729. #endif
  2730. break;
  2731. default:
  2732. return(xFAILED);
  2733. }
  2734. return(fbp->state[dir-1] = state);
  2735. }
  2736. /*
  2737. * Returns:
  2738. * -1: some error. Negotiation is done, encryption not ready.
  2739. * 0: Successful, initial negotiation all done.
  2740. * 1: successful, negotiation not done yet.
  2741. */
  2742. int
  2743. des3_cfb64_is(data, cnt)
  2744. unsigned char *data;
  2745. int cnt;
  2746. {
  2747. return(des3_fb64_is(data, cnt, &des3_fb[CFB]));
  2748. }
  2749. int
  2750. des3_ofb64_is(data, cnt)
  2751. unsigned char *data;
  2752. int cnt;
  2753. {
  2754. return(des3_fb64_is(data, cnt, &des3_fb[OFB]));
  2755. }
  2756. int
  2757. des3_fb64_is(data, cnt, fbp)
  2758. unsigned char *data;
  2759. int cnt;
  2760. struct des3_fb *fbp;
  2761. {
  2762. unsigned char *p;
  2763. register int state = fbp->state[DIR_DECRYPT-1];
  2764. if (cnt-- < 1)
  2765. goto failure;
  2766. #ifdef CK_SSL
  2767. if (!TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  2768. #endif /* CK_SSL */
  2769. switch (*data++) {
  2770. case FB64_IV:
  2771. if (cnt != sizeof(Block)) {
  2772. #ifdef DEBUG
  2773. if (encrypt_debug_mode)
  2774. printf("DES3_FB64: initial vector failed on size\r\n");
  2775. #endif
  2776. state = xFAILED;
  2777. goto failure;
  2778. }
  2779. #ifdef DEBUG
  2780. if (encrypt_debug_mode) {
  2781. printf("DES3_FB64: initial vector received\r\n");
  2782. printf("Initializing Decrypt stream\r\n");
  2783. }
  2784. #endif
  2785. des3_fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
  2786. p = fbp->fb_feed + 3;
  2787. *p++ = ENCRYPT_REPLY;
  2788. p++;
  2789. *p++ = FB64_IV_OK;
  2790. *p++ = IAC;
  2791. *p++ = SE;
  2792. if (deblog || tn_deb || debses) {
  2793. int i;
  2794. sprintf(tn_msg,
  2795. "TELNET SENT SB %s REPLY %s FB64_IV_OK ",
  2796. TELOPT(fbp->fb_feed[2]),
  2797. enctype_names[fbp->fb_feed[4]]); /* safe */
  2798. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],
  2799. (p-fbp->fb_feed)-2-6);
  2800. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2801. debug(F100,tn_msg,"",0);
  2802. if (tn_deb || debses) tn_debug(tn_msg);
  2803. }
  2804. #ifdef OS2
  2805. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2806. #endif
  2807. ttol(fbp->fb_feed, p - fbp->fb_feed);
  2808. #ifdef OS2
  2809. ReleaseTelnetMutex();
  2810. #endif
  2811. state = IN_PROGRESS;
  2812. break;
  2813. default:
  2814. #if 0
  2815. if (encrypt_debug_mode) {
  2816. printf("Unknown option type: %d\r\n", *(data-1));
  2817. printf("\r\n");
  2818. }
  2819. #endif
  2820. /* FALL THROUGH */
  2821. failure:
  2822. /*
  2823. * We failed. Send an FB64_IV_BAD option
  2824. * to the other side so it will know that
  2825. * things failed.
  2826. */
  2827. p = fbp->fb_feed + 3;
  2828. *p++ = ENCRYPT_REPLY;
  2829. p++;
  2830. *p++ = FB64_IV_BAD;
  2831. *p++ = IAC;
  2832. *p++ = SE;
  2833. if (deblog || tn_deb || debses) {
  2834. int i;
  2835. sprintf(tn_msg,
  2836. "TELNET SENT SB %s REPLY %s FB64_IV_BAD ",
  2837. TELOPT(fbp->fb_feed[2]),
  2838. enctype_names[fbp->fb_feed[4]]); /* safe */
  2839. tn_hex((CHAR *)tn_msg,TN_MSG_LEN,&fbp->fb_feed[6],
  2840. (p-fbp->fb_feed)-2-6);
  2841. ckstrncat(tn_msg,"IAC SE",TN_MSG_LEN);
  2842. debug(F100,tn_msg,"",0);
  2843. if (tn_deb || debses) tn_debug(tn_msg);
  2844. }
  2845. #ifdef OS2
  2846. RequestTelnetMutex( SEM_INDEFINITE_WAIT );
  2847. #endif
  2848. ttol(fbp->fb_feed, p - fbp->fb_feed);
  2849. #ifdef OS2
  2850. ReleaseTelnetMutex();
  2851. #endif
  2852. break;
  2853. }
  2854. return(fbp->state[DIR_DECRYPT-1] = state);
  2855. }
  2856. /*
  2857. * Returns:
  2858. * -1: some error. Negotiation is done, encryption not ready.
  2859. * 0: Successful, initial negotiation all done.
  2860. * 1: successful, negotiation not done yet.
  2861. */
  2862. int
  2863. des3_cfb64_reply(data, cnt)
  2864. unsigned char *data;
  2865. int cnt;
  2866. {
  2867. return(des3_fb64_reply(data, cnt, &des3_fb[CFB]));
  2868. }
  2869. int
  2870. des3_ofb64_reply(data, cnt)
  2871. unsigned char *data;
  2872. int cnt;
  2873. {
  2874. return(des3_fb64_reply(data, cnt, &des3_fb[OFB]));
  2875. }
  2876. int
  2877. des3_fb64_reply(data, cnt, fbp)
  2878. unsigned char *data;
  2879. int cnt;
  2880. struct des3_fb *fbp;
  2881. {
  2882. register int state = fbp->state[DIR_ENCRYPT-1];
  2883. if (cnt-- < 1)
  2884. goto failure;
  2885. switch (*data++) {
  2886. case FB64_IV_OK:
  2887. des3_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  2888. if (state == xFAILED)
  2889. state = IN_PROGRESS;
  2890. state &= ~NO_RECV_IV;
  2891. encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
  2892. break;
  2893. case FB64_IV_BAD:
  2894. memset(fbp->temp_feed, 0, sizeof(Block));
  2895. des3_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  2896. state = xFAILED;
  2897. break;
  2898. default:
  2899. #if 0
  2900. if (encrypt_debug_mode) {
  2901. printf("Unknown option type: %d\r\n", data[-1]);
  2902. printf("\r\n");
  2903. }
  2904. #endif
  2905. /* FALL THROUGH */
  2906. failure:
  2907. state = xFAILED;
  2908. break;
  2909. }
  2910. return(fbp->state[DIR_ENCRYPT-1] = state);
  2911. }
  2912. int
  2913. des3_cfb64_session(key, server)
  2914. Session_Key *key;
  2915. int server;
  2916. {
  2917. return(des3_fb64_session(key, server, &des3_fb[CFB]));
  2918. }
  2919. int
  2920. des3_ofb64_session(key, server)
  2921. Session_Key *key;
  2922. int server;
  2923. {
  2924. return(des3_fb64_session(key, server, &des3_fb[OFB]));
  2925. }
  2926. static int
  2927. des3_fb64_session(key, server, fbp)
  2928. Session_Key *key;
  2929. int server;
  2930. struct des3_fb *fbp;
  2931. {
  2932. int rc=0,i=0;
  2933. int keys2use=0;
  2934. struct des3_stinfo * s_stream;
  2935. struct des3_stinfo * c_stream;
  2936. if(server) {
  2937. s_stream = &fbp->streams[DIR_ENCRYPT-1];
  2938. c_stream = &fbp->streams[DIR_DECRYPT-1];
  2939. }
  2940. else {
  2941. s_stream = &fbp->streams[DIR_DECRYPT-1];
  2942. c_stream = &fbp->streams[DIR_ENCRYPT-1];
  2943. }
  2944. keys2use = key->length / sizeof(Block);
  2945. if (!key || (key->type == SK_DES) || (keys2use < 2)) {
  2946. CHAR buf[80];
  2947. sprintf((char *)buf,"Can't set 3DES session key (%d < %d)",
  2948. key ? key->length : 0, 2 * (int)sizeof(Block)); /* safe */
  2949. #ifdef DEBUG
  2950. if (encrypt_debug_mode)
  2951. printf("%s\r\n",buf);
  2952. #endif
  2953. debug(F110,"des3_fb64_session",buf,0);
  2954. return(-1);
  2955. }
  2956. debug(F111,"des3_fb64_session","keys2use",keys2use);
  2957. /* Compute the first set of keys / key order */
  2958. switch ( keys2use ) {
  2959. case 2:
  2960. memcpy((void *)fbp->krbdes_key[0], (void *)key->data, sizeof(Block));
  2961. memcpy((void *) fbp->krbdes_key[1],(void *)(key->data + sizeof(Block)),
  2962. sizeof(Block));
  2963. memcpy((void *)fbp->krbdes_key[2], (void *)key->data, sizeof(Block));
  2964. break;
  2965. case 3:
  2966. default:
  2967. memcpy((void *)fbp->krbdes_key[0], (void *)key->data, sizeof(Block));
  2968. memcpy((void *) fbp->krbdes_key[1],(void *)(key->data + sizeof(Block)),
  2969. sizeof(Block));
  2970. memcpy((void *) fbp->krbdes_key[2],
  2971. (void *) (key->data + 2*sizeof(Block)), sizeof(Block));
  2972. break;
  2973. }
  2974. ckhexdump("des3_session_key key->data",key->data,sizeof(Block));
  2975. ckhexdump("des3_session_key fbp->krbdes_key[0]",
  2976. fbp->krbdes_key[0],
  2977. sizeof(Block)
  2978. );
  2979. if (fbp->once == 0) {
  2980. des_set_random_generator_seed(fbp->krbdes_key[0]);
  2981. fbp->once = 1;
  2982. }
  2983. for ( i=0;i<3;i++ )
  2984. des_fixup_key_parity(fbp->krbdes_key[i]);
  2985. des3_fb64_stream_key(fbp->krbdes_key, s_stream);
  2986. /* Compute the second set of keys / key order */
  2987. switch ( keys2use ) {
  2988. case 2:
  2989. memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  2990. sizeof(Block));
  2991. memcpy((void *)fbp->krbdes_key[1], (void *)key->data, sizeof(Block));
  2992. memcpy((void *) fbp->krbdes_key[2],(void *)(key->data + sizeof(Block)),
  2993. sizeof(Block));
  2994. break;
  2995. case 3:
  2996. memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  2997. sizeof(Block));
  2998. memcpy((void *) fbp->krbdes_key[1],
  2999. (void *) (key->data + 2*sizeof(Block)), sizeof(Block));
  3000. memcpy((void *)fbp->krbdes_key[2], (void *)key->data, sizeof(Block));
  3001. break;
  3002. case 4:
  3003. memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  3004. sizeof(Block));
  3005. memcpy((void *) fbp->krbdes_key[1],
  3006. (void *) (key->data + 3*sizeof(Block)), sizeof(Block));
  3007. memcpy((void *)fbp->krbdes_key[2], (void *)key->data, sizeof(Block));
  3008. break;
  3009. case 5:
  3010. memcpy((void *) fbp->krbdes_key[0],(void *)(key->data + sizeof(Block)),
  3011. sizeof(Block));
  3012. memcpy((void *) fbp->krbdes_key[1],
  3013. (void *) (key->data + 3*sizeof(Block)), sizeof(Block));
  3014. memcpy((void *)fbp->krbdes_key[2],
  3015. (void *)(key->data + 4*sizeof(Block)), sizeof(Block));
  3016. break;
  3017. case 6:
  3018. memcpy((void *) fbp->krbdes_key[0],
  3019. (void *) (key->data + 3*sizeof(Block)), sizeof(Block));
  3020. memcpy((void *)fbp->krbdes_key[1],
  3021. (void *)(key->data + 4*sizeof(Block)), sizeof(Block));
  3022. memcpy((void *) fbp->krbdes_key[2],
  3023. (void *) (key->data + 5 *sizeof(Block)), sizeof(Block));
  3024. break;
  3025. }
  3026. for ( i=0;i<3;i++ )
  3027. des_fixup_key_parity(fbp->krbdes_key[i]);
  3028. des3_fb64_stream_key(fbp->krbdes_key, c_stream);
  3029. /* now use the second set of keys to build the default Key Schedule */
  3030. /* which is used for generating the IV. */
  3031. for ( i=0;i<3;i++ ) {
  3032. memset(fbp->krbdes_sched[i],0,sizeof(Schedule));
  3033. rc = des_key_sched(fbp->krbdes_key[i], fbp->krbdes_sched[i]);
  3034. if ( rc == -1 ) {
  3035. printf("?Invalid DES key specified for encryption [DES3,%s]\r\n",
  3036. server?"server":"client");
  3037. debug(F110,"des3_fb64_stream_iv",
  3038. "invalid DES Key specified for encryption",0);
  3039. } else if ( rc == -2 ) {
  3040. printf("?Weak DES key specified for encryption\r\n");
  3041. debug(F110,"des3_fb64_stream_iv",
  3042. "weak DES Key specified for encryption",0);
  3043. } else if ( rc != 0 ) {
  3044. printf("?Key Schedule not created by encryption\r\n");
  3045. debug(F110,"des3_fb64_stream_iv",
  3046. "Key Schedule not created by encryption",0);
  3047. }
  3048. ckhexdump("des3_fb64_session_key schedule",fbp->krbdes_sched[i],8*16);
  3049. }
  3050. /*
  3051. * Now look to see if krbdes_start() was was waiting for
  3052. * the key to show up. If so, go ahead an call it now
  3053. * that we have the key.
  3054. */
  3055. if (fbp->need_start) {
  3056. fbp->need_start = 0;
  3057. des3_fb64_start(fbp, DIR_ENCRYPT, server);
  3058. }
  3059. return(0);
  3060. }
  3061. /*
  3062. * We only accept a keyid of 0. If we get a keyid of
  3063. * 0, then mark the state as SUCCESS.
  3064. */
  3065. int
  3066. des3_cfb64_keyid(dir, kp, lenp)
  3067. int dir, *lenp;
  3068. unsigned char *kp;
  3069. {
  3070. return(des3_fb64_keyid(dir, kp, lenp, &des3_fb[CFB]));
  3071. }
  3072. int
  3073. des3_ofb64_keyid(dir, kp, lenp)
  3074. int dir, *lenp;
  3075. unsigned char *kp;
  3076. {
  3077. return(des3_fb64_keyid(dir, kp, lenp, &des3_fb[OFB]));
  3078. }
  3079. int
  3080. des3_fb64_keyid(dir, kp, lenp, fbp)
  3081. int dir, *lenp;
  3082. unsigned char *kp;
  3083. struct des3_fb *fbp;
  3084. {
  3085. register int state = fbp->state[dir-1];
  3086. if (*lenp != 1 || (*kp != '\0')) {
  3087. *lenp = 0;
  3088. return(state);
  3089. }
  3090. if (state == xFAILED)
  3091. state = IN_PROGRESS;
  3092. state &= ~NO_KEYID;
  3093. return(fbp->state[dir-1] = state);
  3094. }
  3095. #if 0
  3096. void
  3097. des3_fb64_printsub(data, cnt, buf, buflen, type)
  3098. unsigned char *data, *buf, *type;
  3099. int cnt, buflen;
  3100. {
  3101. char lbuf[64];
  3102. register int i;
  3103. char *cp;
  3104. buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
  3105. buflen -= 1;
  3106. switch(data[2]) {
  3107. case FB64_IV:
  3108. sprintf(lbuf, "%s_IV", type);
  3109. cp = lbuf;
  3110. goto common;
  3111. case FB64_IV_OK:
  3112. sprintf(lbuf, "%s_IV_OK", type);
  3113. cp = lbuf;
  3114. goto common;
  3115. case FB64_IV_BAD:
  3116. sprintf(lbuf, "%s_IV_BAD", type);
  3117. cp = lbuf;
  3118. goto common;
  3119. case FB64_CHALLENGE:
  3120. sprintf(lbuf, "%s_CHALLENGE", type);
  3121. cp = lbuf;
  3122. goto common;
  3123. case FB64_RESPONSE:
  3124. sprintf(lbuf, "%s_RESPONSE", type);
  3125. cp = lbuf;
  3126. goto common;
  3127. default:
  3128. sprintf(lbuf, " %d (unknown)", data[2]);
  3129. cp = lbuf;
  3130. common:
  3131. for (; (buflen > 0) && (*buf = *cp++); buf++)
  3132. buflen--;
  3133. for (i = 3; i < cnt; i++) {
  3134. sprintf(lbuf, " %d", data[i]);
  3135. for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
  3136. buflen--;
  3137. }
  3138. break;
  3139. }
  3140. }
  3141. void
  3142. des3_cfb64_printsub(data, cnt, buf, buflen)
  3143. unsigned char *data, *buf;
  3144. int cnt, buflen;
  3145. {
  3146. des3_fb64_printsub(data, cnt, buf, buflen, "CFB64");
  3147. }
  3148. void
  3149. des3_ofb64_printsub(data, cnt, buf, buflen)
  3150. unsigned char *data, *buf;
  3151. int cnt, buflen;
  3152. {
  3153. des3_fb64_printsub(data, cnt, buf, buflen, "OFB64");
  3154. }
  3155. #endif
  3156. void
  3157. des3_fb64_stream_iv(seed, stp)
  3158. Block seed;
  3159. register struct des3_stinfo *stp;
  3160. {
  3161. int rc=0, i = 0;;
  3162. memcpy(stp->str_iv, seed, sizeof(Block));
  3163. memcpy(stp->str_output, seed, sizeof(Block));
  3164. for ( i=0;i<3;i++ ) {
  3165. memset(stp->str_sched[i],0,sizeof(Schedule));
  3166. ckhexdump("des3_fb64_stream_iv",stp->str_ikey[i],8);
  3167. rc = des_key_sched(stp->str_ikey[i], stp->str_sched[i]);
  3168. if ( rc == -1 ) {
  3169. printf("?Invalid DES key specified for encryption [DES3 iv]\r\n");
  3170. debug(F110,"des3_fb64_stream_iv",
  3171. "invalid DES Key specified for encryption",0);
  3172. } else if ( rc == -2 ) {
  3173. printf("?Weak DES key specified for encryption\r\n");
  3174. debug(F110,"des3_fb64_stream_iv",
  3175. "weak DES Key specified for encryption",0);
  3176. } else if ( rc != 0 ) {
  3177. printf("?Key Schedule not created by encryption\r\n");
  3178. debug(F110,"des3_fb64_stream_iv",
  3179. "Key Schedule not created by encryption",0);
  3180. }
  3181. ckhexdump("des3_fb64_stream_iv schedule",stp->str_sched[i],8*16);
  3182. }
  3183. stp->str_index = sizeof(Block);
  3184. }
  3185. void
  3186. des3_fb64_stream_key(key, stp)
  3187. Block * key;
  3188. register struct des3_stinfo *stp;
  3189. {
  3190. int rc = 0, i = 0;
  3191. for ( i=0;i<3;i++ ) {
  3192. memcpy(stp->str_ikey[i], key[i], sizeof(Block));
  3193. memset(stp->str_sched[i],0,sizeof(Schedule));
  3194. ckhexdump("des3_fb64_stream_key",key[i],8);
  3195. rc = des_key_sched(key[i], stp->str_sched[i]);
  3196. if ( rc == -1 ) {
  3197. printf("?Invalid DES key specified for encryption [DES3 key]\r\n");
  3198. debug(F110,"des3_fb64_stream_key",
  3199. "invalid DES Key specified for encryption",0);
  3200. } else if ( rc == -2 ) {
  3201. printf("?Weak DES key specified for encryption\r\n");
  3202. debug(F110,"des3_fb64_stream_key",
  3203. "weak DES Key specified for encryption",0);
  3204. } else if ( rc != 0 ) {
  3205. printf("?Key Schedule not created by encryption\r\n");
  3206. debug(F110,"des3_fb64_stream_key",
  3207. "Key Schedule not created by encryption",0);
  3208. }
  3209. ckhexdump("des3_fb64_stream_key schedule",stp->str_sched[i],8*16);
  3210. }
  3211. memcpy(stp->str_output, stp->str_iv, sizeof(Block));
  3212. stp->str_index = sizeof(Block);
  3213. }
  3214. /*
  3215. * DES3 64 bit Cipher Feedback
  3216. *
  3217. * key1 key2 key3
  3218. * | | |
  3219. * v v v
  3220. * +-------+ +-------+ +-------+
  3221. * +->| DES-e |->| DES-d |->| DES-e |-- +
  3222. * | +-------+ +-------+ +-------+ |
  3223. * | v
  3224. * INPUT --(-------------------------------->(+)+---> DATA
  3225. * | |
  3226. * +------------------------------------+
  3227. *
  3228. *
  3229. * Given:
  3230. * iV: Initial vector, 64 bits (8 bytes) long.
  3231. * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  3232. * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  3233. *
  3234. * V0 = DES-e(DES-d(DES-e(iV, key1),key2),key3)
  3235. * On = Dn ^ Vn
  3236. * V(n+1) = DES-e(DES-d(DES-e(On, key1),key2),key3)
  3237. */
  3238. void
  3239. des3_cfb64_encrypt(s, c)
  3240. register unsigned char *s;
  3241. int c;
  3242. {
  3243. register struct des3_stinfo *stp = &des3_fb[CFB].streams[DIR_ENCRYPT-1];
  3244. register int index;
  3245. index = stp->str_index;
  3246. while (c-- > 0) {
  3247. if (index == sizeof(Block)) {
  3248. Block b;
  3249. #ifdef LIBDES
  3250. des_ecb3_encrypt(stp->str_output, b, stp->str_sched[0],
  3251. stp->str_sched[1], stp->str_sched[2], 1);
  3252. #else /* LIBDES */
  3253. des_ecb_encrypt(stp->str_output, b,
  3254. stp->str_sched[0], 1);
  3255. des_ecb_encrypt(stp->str_output, b,
  3256. stp->str_sched[1], 0);
  3257. des_ecb_encrypt(stp->str_output, b,
  3258. stp->str_sched[2], 1);
  3259. #endif /* LIBDES */
  3260. memcpy(stp->str_feed,b,sizeof(Block));
  3261. index = 0;
  3262. }
  3263. /* On encryption, we store (feed ^ data) which is cypher */
  3264. *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
  3265. s++;
  3266. index++;
  3267. }
  3268. stp->str_index = index;
  3269. }
  3270. int
  3271. des3_cfb64_decrypt(data)
  3272. int data;
  3273. {
  3274. register struct des3_stinfo *stp = &des3_fb[CFB].streams[DIR_DECRYPT-1];
  3275. int index;
  3276. if (data == -1) {
  3277. /*
  3278. * Back up one byte. It is assumed that we will
  3279. * never back up more than one byte. If we do, this
  3280. * may or may not work.
  3281. */
  3282. if (stp->str_index)
  3283. --stp->str_index;
  3284. return(0);
  3285. }
  3286. index = stp->str_index++;
  3287. if (index == sizeof(Block)) {
  3288. Block b;
  3289. #ifdef LIBDES
  3290. des_ecb3_encrypt(stp->str_output, b, stp->str_sched[0],
  3291. stp->str_sched[1], stp->str_sched[2], 1);
  3292. #else /* LIBDES */
  3293. des_ecb_encrypt(stp->str_output, b,
  3294. stp->str_sched[0], 1);
  3295. des_ecb_encrypt(stp->str_output, b,
  3296. stp->str_sched[1], 0);
  3297. des_ecb_encrypt(stp->str_output, b,
  3298. stp->str_sched[2], 1);
  3299. #endif /* LIBDES */
  3300. memcpy(stp->str_feed, b, sizeof(Block));
  3301. stp->str_index = 1; /* Next time will be 1 */
  3302. index = 0; /* But now use 0 */
  3303. }
  3304. /* On decryption we store (data) which is cypher. */
  3305. stp->str_output[index] = data;
  3306. return(data ^ stp->str_feed[index]);
  3307. }
  3308. /*
  3309. * DES3 64 bit Output Feedback
  3310. *
  3311. *
  3312. * key1 key2 key3
  3313. * | | |
  3314. * v v v
  3315. * +-------+ +-------+ +-------+
  3316. * +->| DES-e |->| DES-d |->| DES-e |-- +
  3317. * | +-------+ +-------+ +-------+ |
  3318. * +------------------------------------+
  3319. * v
  3320. * INPUT ------------------------------------->(+) ----> DATA
  3321. *
  3322. * Given:
  3323. * iV: Initial vector, 64 bits (8 bytes) long.
  3324. * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  3325. * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  3326. *
  3327. * V0 = DES-e(DES-d(DES-e(iV, key1),key2),key3)
  3328. * V(n+1) = DES-e(DES-d(DES-e(Vn, key1),key2),key3)
  3329. * On = Dn ^ Vn
  3330. */
  3331. void
  3332. des3_ofb64_encrypt(s, c)
  3333. register unsigned char *s;
  3334. int c;
  3335. {
  3336. register struct des3_stinfo *stp = &des3_fb[OFB].streams[DIR_ENCRYPT-1];
  3337. register int index;
  3338. index = stp->str_index;
  3339. while (c-- > 0) {
  3340. if (index == sizeof(Block)) {
  3341. Block b;
  3342. #ifdef LIBDES
  3343. des_ecb3_encrypt(stp->str_feed, b, stp->str_sched[0],
  3344. stp->str_sched[1], stp->str_sched[2], 1);
  3345. #else /* LIBDES */
  3346. des_ecb_encrypt(stp->str_output, b,
  3347. stp->str_sched[0], 1);
  3348. des_ecb_encrypt(stp->str_output, b,
  3349. stp->str_sched[1], 0);
  3350. des_ecb_encrypt(stp->str_output, b,
  3351. stp->str_sched[2], 1);
  3352. #endif /* LIBDES */
  3353. memcpy(stp->str_feed,b,sizeof(Block));
  3354. index = 0;
  3355. }
  3356. *s++ ^= stp->str_feed[index];
  3357. index++;
  3358. }
  3359. stp->str_index = index;
  3360. }
  3361. int
  3362. des3_ofb64_decrypt(data)
  3363. int data;
  3364. {
  3365. register struct des3_stinfo *stp = &des3_fb[OFB].streams[DIR_DECRYPT-1];
  3366. int index;
  3367. if (data == -1) {
  3368. /*
  3369. * Back up one byte. It is assumed that we will
  3370. * never back up more than one byte. If we do, this
  3371. * may or may not work.
  3372. */
  3373. if (stp->str_index)
  3374. --stp->str_index;
  3375. return(0);
  3376. }
  3377. index = stp->str_index++;
  3378. if (index == sizeof(Block)) {
  3379. Block b;
  3380. #ifdef LIBDES
  3381. des_ecb3_encrypt(stp->str_feed, b, stp->str_sched[0],
  3382. stp->str_sched[1], stp->str_sched[2], 1);
  3383. #else /* LIBDES */
  3384. des_ecb_encrypt(stp->str_output, b,
  3385. stp->str_sched[0], 1);
  3386. des_ecb_encrypt(stp->str_output, b,
  3387. stp->str_sched[1], 0);
  3388. des_ecb_encrypt(stp->str_output, b,
  3389. stp->str_sched[2], 1);
  3390. #endif /* LIBDES */
  3391. memcpy(stp->str_feed, b, sizeof(Block));
  3392. stp->str_index = 1; /* Next time will be 1 */
  3393. index = 0; /* But now use 0 */
  3394. }
  3395. return(data ^ stp->str_feed[index]);
  3396. }
  3397. #endif /* CK_DES */
  3398. #ifdef CK_CAST
  3399. /*-
  3400. * Copyright (c) 1991, 1993
  3401. * The Regents of the University of California. All rights reserved.
  3402. *
  3403. * Redistribution and use in source and binary forms, with or without
  3404. * modification, are permitted provided that the following conditions
  3405. * are met:
  3406. * 1. Redistributions of source code must retain the above copyright
  3407. * notice, this list of conditions and the following disclaimer.
  3408. * 2. Redistributions in binary form must reproduce the above copyright
  3409. * notice, this list of conditions and the following disclaimer in the
  3410. * documentation and/or other materials provided with the distribution.
  3411. * 3. All advertising materials mentioning features or use of this software
  3412. * must display the following acknowledgement:
  3413. * This product includes software developed by the University of
  3414. * California, Berkeley and its contributors.
  3415. * 4. Neither the name of the University nor the names of its contributors
  3416. * may be used to endorse or promote products derived from this software
  3417. * without specific prior written permission.
  3418. *
  3419. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  3420. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  3421. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  3422. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  3423. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  3424. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  3425. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  3426. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  3427. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  3428. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  3429. * SUCH DAMAGE.
  3430. */
  3431. /*
  3432. * Copyright (c) 1997 Stanford University
  3433. *
  3434. * Permission to use, copy, modify, distribute, and sell this software and
  3435. * its documentation for any purpose is hereby granted without fee, provided
  3436. * that the above copyright notices and this permission notice appear in
  3437. * all copies of the software and related documentation.
  3438. *
  3439. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  3440. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  3441. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  3442. *
  3443. * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  3444. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  3445. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  3446. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  3447. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  3448. */
  3449. #include <stdio.h>
  3450. #ifdef __STDC__
  3451. #include <stdlib.h>
  3452. #endif
  3453. /*
  3454. * cast.h
  3455. * Author: Tom Wu
  3456. *
  3457. * Type and function declarations for CAST.
  3458. */
  3459. #ifndef _CAST_H_
  3460. #define _CAST_H_
  3461. #ifndef P
  3462. #ifdef __STDC__
  3463. #define P(x) x
  3464. #else
  3465. #define P(x) ()
  3466. #endif /* __STDC__ */
  3467. #endif /* P */
  3468. #ifndef LITTLE_ENDIAN
  3469. #ifndef BIG_ENDIAN
  3470. #ifndef WORDS_BIGENDIAN
  3471. #define LITTLE_ENDIAN 1
  3472. #endif /* WORDS_BIGENDIAN */
  3473. #endif /* BIG_ENDIAN */
  3474. #endif /* LITTLE_ENDIAN */
  3475. typedef unsigned int uint32; /* Must be 32 bits */
  3476. typedef uint32 * uint32p;
  3477. typedef unsigned char uint8;
  3478. typedef uint8 * uint8p;
  3479. typedef struct {
  3480. struct CastSubkeyPair {
  3481. uint32 Km;
  3482. uint32 Kr;
  3483. } K[16];
  3484. int ksize;
  3485. } CastKeySched;
  3486. /*
  3487. * cast*_key_sched(schedule, key)
  3488. *
  3489. * Initializes the CAST key schedule "schedule" according to the given key.
  3490. * The different setup routines accept different length keys:
  3491. *
  3492. * ck_cast5_40_key_sched: 40-bit/5-byte (12 round) keys
  3493. * ck_cast5_64_key_sched: 64-bit/8-byte (12 round) keys
  3494. * ck_cast5_80_key_sched: 80-bit/10-byte (12 round) keys
  3495. * ck_cast128_key_sched: 128-bit/16-byte (16 round) keys
  3496. */
  3497. extern void ck_cast5_40_key_sched P((CastKeySched *, uint8 *));
  3498. extern void ck_cast5_64_key_sched P((CastKeySched *, uint8 *));
  3499. extern void ck_cast5_80_key_sched P((CastKeySched *, uint8 *));
  3500. extern void ck_cast128_key_sched P((CastKeySched *, uint8 *));
  3501. /*
  3502. * ck_cast_ecb_encrypt(output, input, schedule, mode)
  3503. * ck_cast_ecb_crypt(data, schedule, mode)
  3504. *
  3505. * Encrypts the 64-bit "input" according to the CAST key schedule
  3506. * "schedule" and places the result in "output". If "mode" is 0,
  3507. * ck_cast_ecb_encrypt will encrypt, otherwise it will decrypt.
  3508. * "Output" and "input" can point to the same memory, in which case
  3509. * en/decryption will be performed in place.
  3510. *
  3511. * ck_cast_ecb_crypt accepts input in the form of an array of two
  3512. * 32-bit words and performs encryption/decryption in place.
  3513. */
  3514. extern void ck_cast_ecb_encrypt P((uint8 *, uint8 *, CastKeySched *, int));
  3515. extern void ck_cast_ecb_crypt P((uint32 *, CastKeySched *, int));
  3516. #endif /* CAST_H */
  3517. extern encrypt_debug_mode;
  3518. #define CFB_40 0
  3519. #define OFB_40 1
  3520. #ifdef CAST_EXPORT_ENCRYPTION
  3521. #define FB_CNT 2
  3522. #else
  3523. #define CFB_128 2
  3524. #define OFB_128 3
  3525. #define FB_CNT 4
  3526. #endif
  3527. #define NO_SEND_IV 1
  3528. #define NO_RECV_IV 2
  3529. #define NO_KEYID 4
  3530. #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
  3531. #define SUCCESS 0
  3532. #define cFAILED -1
  3533. struct cast_fb {
  3534. Block temp_feed;
  3535. unsigned char fb_feed[64];
  3536. int key_isset;
  3537. int need_start;
  3538. int state[2];
  3539. struct cast_stinfo {
  3540. Block str_output;
  3541. Block str_feed;
  3542. Block str_iv;
  3543. CastKeySched str_sched;
  3544. int str_index;
  3545. } streams[2];
  3546. };
  3547. static struct cast_fb cast_fb[FB_CNT];
  3548. #define FB64_IV 1
  3549. #define FB64_IV_OK 2
  3550. #define FB64_IV_BAD 3
  3551. static void cast_fb64_stream_iv P((Block, struct cast_stinfo *));
  3552. static void cast_fb64_init P((struct cast_fb *));
  3553. static int cast_fb64_start P((struct cast_fb *, int, int));
  3554. static int cast_fb64_is P((unsigned char *, int, struct cast_fb *));
  3555. static int cast_fb64_reply P((unsigned char *, int, struct cast_fb *));
  3556. static int cast_fb64_session P((Session_Key *, int, struct cast_fb *, int));
  3557. static void cast_fb64_stream_key P((Block, struct cast_stinfo *, int));
  3558. static int cast_fb64_keyid P((int, unsigned char *, int *, struct cast_fb *));
  3559. static void _cast_cfb64_encrypt P((unsigned char *,int, struct cast_stinfo *));
  3560. static int _cast_cfb64_decrypt P((int, struct cast_stinfo *));
  3561. static void _cast_ofb64_encrypt P((unsigned char *,int, struct cast_stinfo *));
  3562. static int _cast_ofb64_decrypt P((int, struct cast_stinfo *));
  3563. #ifndef CAST_EXPORT_ENCRYPTION
  3564. void
  3565. cast_cfb64_init(server)
  3566. int server;
  3567. {
  3568. cast_fb64_init(&cast_fb[CFB_128]);
  3569. cast_fb[CFB_128].fb_feed[4] = ENCTYPE_CAST128_CFB64;
  3570. }
  3571. void
  3572. cast_ofb64_init(server)
  3573. int server;
  3574. {
  3575. cast_fb64_init(&cast_fb[OFB_128]);
  3576. cast_fb[OFB_128].fb_feed[4] = ENCTYPE_CAST128_OFB64;
  3577. }
  3578. #endif
  3579. void
  3580. castexp_cfb64_init(server)
  3581. int server;
  3582. {
  3583. cast_fb64_init(&cast_fb[CFB_40]);
  3584. cast_fb[CFB_40].fb_feed[4] = ENCTYPE_CAST5_40_CFB64;
  3585. }
  3586. void
  3587. castexp_ofb64_init(server)
  3588. int server;
  3589. {
  3590. cast_fb64_init(&cast_fb[OFB_40]);
  3591. cast_fb[OFB_40].fb_feed[4] = ENCTYPE_CAST5_40_OFB64;
  3592. }
  3593. static void
  3594. cast_fb64_init(fbp)
  3595. register struct cast_fb *fbp;
  3596. {
  3597. memset((void *)fbp, 0, sizeof(*fbp));
  3598. fbp->key_isset = 0;
  3599. fbp->state[0] = fbp->state[1] = cFAILED;
  3600. fbp->fb_feed[0] = IAC;
  3601. fbp->fb_feed[1] = SB;
  3602. fbp->fb_feed[2] = TELOPT_ENCRYPTION;
  3603. fbp->fb_feed[3] = ENCRYPT_IS;
  3604. }
  3605. /*
  3606. * Returns:
  3607. * -1: some error. Negotiation is done, encryption not ready.
  3608. * 0: Successful, initial negotiation all done.
  3609. * 1: successful, negotiation not done yet.
  3610. * 2: Not yet. Other things (like getting the key from
  3611. * Kerberos) have to happen before we can continue.
  3612. */
  3613. #ifndef CAST_EXPORT_ENCRYPTION
  3614. int
  3615. cast_cfb64_start(dir, server)
  3616. int dir;
  3617. int server;
  3618. {
  3619. return(cast_fb64_start(&cast_fb[CFB_128], dir, server));
  3620. }
  3621. int
  3622. cast_ofb64_start(dir, server)
  3623. int dir;
  3624. int server;
  3625. {
  3626. return(cast_fb64_start(&cast_fb[OFB_128], dir, server));
  3627. }
  3628. #endif
  3629. int
  3630. castexp_cfb64_start(dir, server)
  3631. int dir;
  3632. int server;
  3633. {
  3634. return(cast_fb64_start(&cast_fb[CFB_40], dir, server));
  3635. }
  3636. int
  3637. castexp_ofb64_start(dir, server)
  3638. int dir;
  3639. int server;
  3640. {
  3641. return(cast_fb64_start(&cast_fb[OFB_40], dir, server));
  3642. }
  3643. static int
  3644. cast_fb64_start(fbp, dir, server)
  3645. struct cast_fb *fbp;
  3646. int dir;
  3647. int server;
  3648. {
  3649. Block b;
  3650. int x;
  3651. unsigned char *p;
  3652. register int state;
  3653. switch (dir) {
  3654. case DIR_DECRYPT:
  3655. /*
  3656. * This is simply a request to have the other side
  3657. * start output (our input). He will negotiate an
  3658. * IV so we need not look for it.
  3659. */
  3660. state = fbp->state[dir-1];
  3661. if (state == cFAILED)
  3662. state = IN_PROGRESS;
  3663. break;
  3664. case DIR_ENCRYPT:
  3665. state = fbp->state[dir-1];
  3666. if (state == cFAILED)
  3667. state = IN_PROGRESS;
  3668. else if ((state & NO_SEND_IV) == 0)
  3669. break;
  3670. if (!fbp->key_isset) {
  3671. fbp->need_start = 1;
  3672. break;
  3673. }
  3674. state &= ~NO_SEND_IV;
  3675. state |= NO_RECV_IV;
  3676. #ifdef DEBUG
  3677. if (encrypt_debug_mode)
  3678. printf("Creating new feed\r\n");
  3679. #endif
  3680. /*
  3681. * Create a random feed and send it over.
  3682. */
  3683. ck_cast_ecb_encrypt(fbp->temp_feed, fbp->temp_feed,
  3684. &fbp->streams[dir-1].str_sched, 0);
  3685. p = fbp->fb_feed + 3;
  3686. *p++ = ENCRYPT_IS;
  3687. p++;
  3688. *p++ = FB64_IV;
  3689. for (x = 0; x < sizeof(Block); ++x) {
  3690. if ((*p++ = fbp->temp_feed[x]) == IAC)
  3691. *p++ = IAC;
  3692. }
  3693. *p++ = IAC;
  3694. *p++ = SE;
  3695. ttol(fbp->fb_feed, p - fbp->fb_feed);
  3696. break;
  3697. default:
  3698. return(cFAILED);
  3699. }
  3700. return(fbp->state[dir-1] = state);
  3701. }
  3702. /*
  3703. * Returns:
  3704. * -1: some error. Negotiation is done, encryption not ready.
  3705. * 0: Successful, initial negotiation all done.
  3706. * 1: successful, negotiation not done yet.
  3707. */
  3708. #ifndef CAST_EXPORT_ENCRYPTION
  3709. int
  3710. cast_cfb64_is(data, cnt)
  3711. unsigned char *data;
  3712. int cnt;
  3713. {
  3714. return(cast_fb64_is(data, cnt, &cast_fb[CFB_128]));
  3715. }
  3716. int
  3717. cast_ofb64_is(data, cnt)
  3718. unsigned char *data;
  3719. int cnt;
  3720. {
  3721. return(cast_fb64_is(data, cnt, &cast_fb[OFB_128]));
  3722. }
  3723. #endif
  3724. int
  3725. castexp_cfb64_is(data, cnt)
  3726. unsigned char *data;
  3727. int cnt;
  3728. {
  3729. return(cast_fb64_is(data, cnt, &cast_fb[CFB_40]));
  3730. }
  3731. int
  3732. castexp_ofb64_is(data, cnt)
  3733. unsigned char *data;
  3734. int cnt;
  3735. {
  3736. return(cast_fb64_is(data, cnt, &cast_fb[OFB_40]));
  3737. }
  3738. static int
  3739. cast_fb64_is(data, cnt, fbp)
  3740. unsigned char *data;
  3741. int cnt;
  3742. struct cast_fb *fbp;
  3743. {
  3744. int x;
  3745. unsigned char *p;
  3746. Block b;
  3747. register int state = fbp->state[DIR_DECRYPT-1];
  3748. if (cnt-- < 1)
  3749. goto failure;
  3750. #ifdef CK_SSL
  3751. if (!TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows)
  3752. #endif /* CK_SSL */
  3753. switch (*data++) {
  3754. case FB64_IV:
  3755. if (cnt != sizeof(Block)) {
  3756. #ifdef DEBUG
  3757. if (encrypt_debug_mode)
  3758. printf("FB64: initial vector failed on size\r\n");
  3759. #endif
  3760. state = cFAILED;
  3761. goto failure;
  3762. }
  3763. #ifdef DEBUG
  3764. if (encrypt_debug_mode)
  3765. printf("FB64: initial vector received\r\n");
  3766. if (encrypt_debug_mode)
  3767. printf("Initializing Decrypt stream\r\n");
  3768. #endif
  3769. cast_fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
  3770. p = fbp->fb_feed + 3;
  3771. *p++ = ENCRYPT_REPLY;
  3772. p++;
  3773. *p++ = FB64_IV_OK;
  3774. *p++ = IAC;
  3775. *p++ = SE;
  3776. ttol(fbp->fb_feed, p - fbp->fb_feed);
  3777. state = IN_PROGRESS;
  3778. break;
  3779. default:
  3780. /* unknown option type */
  3781. /* FALL THROUGH */
  3782. failure:
  3783. /*
  3784. * We failed. Send an FB64_IV_BAD option
  3785. * to the other side so it will know that
  3786. * things failed.
  3787. */
  3788. p = fbp->fb_feed + 3;
  3789. *p++ = ENCRYPT_REPLY;
  3790. p++;
  3791. *p++ = FB64_IV_BAD;
  3792. *p++ = IAC;
  3793. *p++ = SE;
  3794. ttol(fbp->fb_feed, p - fbp->fb_feed);
  3795. break;
  3796. }
  3797. return(fbp->state[DIR_DECRYPT-1] = state);
  3798. }
  3799. /*
  3800. * Returns:
  3801. * -1: some error. Negotiation is done, encryption not ready.
  3802. * 0: Successful, initial negotiation all done.
  3803. * 1: successful, negotiation not done yet.
  3804. */
  3805. #ifndef CAST_EXPORT_ENCRYPTION
  3806. int
  3807. cast_cfb64_reply(data, cnt)
  3808. unsigned char *data;
  3809. int cnt;
  3810. {
  3811. return(cast_fb64_reply(data, cnt, &cast_fb[CFB_128]));
  3812. }
  3813. int
  3814. cast_ofb64_reply(data, cnt)
  3815. unsigned char *data;
  3816. int cnt;
  3817. {
  3818. return(cast_fb64_reply(data, cnt, &cast_fb[OFB_128]));
  3819. }
  3820. #endif
  3821. int
  3822. castexp_cfb64_reply(data, cnt)
  3823. unsigned char *data;
  3824. int cnt;
  3825. {
  3826. return(cast_fb64_reply(data, cnt, &cast_fb[CFB_40]));
  3827. }
  3828. int
  3829. castexp_ofb64_reply(data, cnt)
  3830. unsigned char *data;
  3831. int cnt;
  3832. {
  3833. return(cast_fb64_reply(data, cnt, &cast_fb[OFB_40]));
  3834. }
  3835. static int
  3836. cast_fb64_reply(data, cnt, fbp)
  3837. unsigned char *data;
  3838. int cnt;
  3839. struct cast_fb *fbp;
  3840. {
  3841. int x;
  3842. unsigned char *p;
  3843. Block b;
  3844. register int state = fbp->state[DIR_ENCRYPT-1];
  3845. if (cnt-- < 1)
  3846. goto failure;
  3847. switch (*data++) {
  3848. case FB64_IV_OK:
  3849. cast_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  3850. if (state == cFAILED)
  3851. state = IN_PROGRESS;
  3852. state &= ~NO_RECV_IV;
  3853. encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
  3854. break;
  3855. case FB64_IV_BAD:
  3856. memset(fbp->temp_feed, 0, sizeof(Block));
  3857. cast_fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
  3858. state = cFAILED;
  3859. break;
  3860. default:
  3861. #if 0
  3862. if (encrypt_debug_mode) {
  3863. printf("Unknown option type: %d\r\n", data[-1]);
  3864. printd(data, cnt);
  3865. printf("\r\n");
  3866. }
  3867. #endif
  3868. /* FALL THROUGH */
  3869. failure:
  3870. state = cFAILED;
  3871. break;
  3872. }
  3873. return(fbp->state[DIR_ENCRYPT-1] = state);
  3874. }
  3875. #ifndef CAST_EXPORT_ENCRYPTION
  3876. int
  3877. cast_cfb64_session(key, server)
  3878. Session_Key *key;
  3879. int server;
  3880. {
  3881. return(cast_fb64_session(key, server, &cast_fb[CFB_128], 1));
  3882. }
  3883. int
  3884. cast_ofb64_session(key, server)
  3885. Session_Key *key;
  3886. int server;
  3887. {
  3888. return(cast_fb64_session(key, server, &cast_fb[OFB_128], 1));
  3889. }
  3890. #endif
  3891. int
  3892. castexp_cfb64_session(key, server)
  3893. Session_Key *key;
  3894. int server;
  3895. {
  3896. return(cast_fb64_session(key, server, &cast_fb[CFB_40], 0));
  3897. }
  3898. int
  3899. castexp_ofb64_session(key, server)
  3900. Session_Key *key;
  3901. int server;
  3902. {
  3903. return(cast_fb64_session(key, server, &cast_fb[OFB_40], 0));
  3904. }
  3905. #define CAST128_KEYLEN 16 /* 128 bits */
  3906. #define CAST5_40_KEYLEN 5 /* 40 bits */
  3907. static int
  3908. cast_fb64_session(key, server, fbp, fs)
  3909. Session_Key *key;
  3910. int server;
  3911. struct cast_fb *fbp;
  3912. int fs;
  3913. {
  3914. int klen;
  3915. unsigned char * kptr;
  3916. if(fs)
  3917. klen = CAST128_KEYLEN;
  3918. else
  3919. klen = CAST5_40_KEYLEN;
  3920. if (!key || key->length < klen) {
  3921. CHAR buf[80];
  3922. sprintf((char *)buf,"Can't set CAST session key (%d < %d)",
  3923. key ? key->length : 0, klen); /* safe */
  3924. #ifdef DEBUG
  3925. if (encrypt_debug_mode)
  3926. printf("%s\r\n",buf);
  3927. #endif
  3928. debug(F110,"cast_fb64_session",buf,0);
  3929. return(cFAILED);
  3930. }
  3931. if(key->length < 2 * klen)
  3932. kptr = key->data;
  3933. else
  3934. kptr = key->data + klen;
  3935. if(server) {
  3936. cast_fb64_stream_key(kptr, &fbp->streams[DIR_ENCRYPT-1], fs);
  3937. cast_fb64_stream_key(key->data, &fbp->streams[DIR_DECRYPT-1], fs);
  3938. }
  3939. else {
  3940. cast_fb64_stream_key(kptr, &fbp->streams[DIR_DECRYPT-1], fs);
  3941. cast_fb64_stream_key(key->data, &fbp->streams[DIR_ENCRYPT-1], fs);
  3942. }
  3943. /* Stuff leftovers into the feed */
  3944. if(key->length >= 2 * klen + sizeof(Block))
  3945. memcpy(fbp->temp_feed, key->data + 2 * klen, sizeof(Block));
  3946. else {
  3947. #ifdef COMMENT
  3948. /* This is a better way of erasing the password */
  3949. /* but we do not want to link in libsrp */
  3950. t_random(fbp->temp_feed, sizeof(Block));
  3951. #else
  3952. memset(fbp->temp_feed, 0, sizeof(Block));
  3953. #endif
  3954. }
  3955. fbp->key_isset = 1;
  3956. /*
  3957. * Now look to see if cast_fb64_start() was was waiting for
  3958. * the key to show up. If so, go ahead an call it now
  3959. * that we have the key.
  3960. */
  3961. if (fbp->need_start) {
  3962. fbp->need_start = 0;
  3963. cast_fb64_start(fbp, DIR_ENCRYPT, server);
  3964. }
  3965. return(0);
  3966. }
  3967. /*
  3968. * We only accept a keyid of 0. If we get a keyid of
  3969. * 0, then mark the state as SUCCESS.
  3970. */
  3971. #ifndef CAST_EXPORT_ENCRYPTION
  3972. int
  3973. cast_cfb64_keyid(dir, kp, lenp)
  3974. int dir, *lenp;
  3975. unsigned char *kp;
  3976. {
  3977. return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[CFB_128]));
  3978. }
  3979. int
  3980. cast_ofb64_keyid(dir, kp, lenp)
  3981. int dir, *lenp;
  3982. unsigned char *kp;
  3983. {
  3984. return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[OFB_128]));
  3985. }
  3986. #endif
  3987. int
  3988. castexp_cfb64_keyid(dir, kp, lenp)
  3989. int dir, *lenp;
  3990. unsigned char *kp;
  3991. {
  3992. return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[CFB_40]));
  3993. }
  3994. int
  3995. castexp_ofb64_keyid(dir, kp, lenp)
  3996. int dir, *lenp;
  3997. unsigned char *kp;
  3998. {
  3999. return(cast_fb64_keyid(dir, kp, lenp, &cast_fb[OFB_40]));
  4000. }
  4001. static int
  4002. cast_fb64_keyid(dir, kp, lenp, fbp)
  4003. int dir, *lenp;
  4004. unsigned char *kp;
  4005. struct cast_fb *fbp;
  4006. {
  4007. register int state = fbp->state[dir-1];
  4008. if (*lenp != 1 || (*kp != '\0')) {
  4009. *lenp = 0;
  4010. return(state);
  4011. }
  4012. if (state == cFAILED)
  4013. state = IN_PROGRESS;
  4014. state &= ~NO_KEYID;
  4015. return(fbp->state[dir-1] = state);
  4016. }
  4017. static void
  4018. cast_fb64_printsub(data, cnt, buf, buflen, type)
  4019. unsigned char *data, *buf, *type;
  4020. int cnt, buflen;
  4021. {
  4022. char lbuf[64];
  4023. register int i;
  4024. char *cp;
  4025. buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
  4026. buflen -= 1;
  4027. switch(data[2]) {
  4028. case FB64_IV:
  4029. sprintf(lbuf, "%s_IV", type);
  4030. cp = lbuf;
  4031. goto common;
  4032. case FB64_IV_OK:
  4033. sprintf(lbuf, "%s_IV_OK", type);
  4034. cp = lbuf;
  4035. goto common;
  4036. case FB64_IV_BAD:
  4037. sprintf(lbuf, "%s_IV_BAD", type);
  4038. cp = lbuf;
  4039. goto common;
  4040. default:
  4041. sprintf(lbuf, " %d (unknown)", data[2]);
  4042. cp = lbuf;
  4043. common:
  4044. for (; (buflen > 0) && (*buf = *cp++); buf++)
  4045. buflen--;
  4046. for (i = 3; i < cnt; i++) {
  4047. sprintf(lbuf, " %d", data[i]);
  4048. for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
  4049. buflen--;
  4050. }
  4051. break;
  4052. }
  4053. }
  4054. void
  4055. cast_cfb64_printsub(data, cnt, buf, buflen)
  4056. unsigned char *data, *buf;
  4057. int cnt, buflen;
  4058. {
  4059. cast_fb64_printsub(data, cnt, buf, buflen, "CFB64");
  4060. }
  4061. void
  4062. cast_ofb64_printsub(data, cnt, buf, buflen)
  4063. unsigned char *data, *buf;
  4064. int cnt, buflen;
  4065. {
  4066. cast_fb64_printsub(data, cnt, buf, buflen, "OFB64");
  4067. }
  4068. static void
  4069. cast_fb64_stream_iv(seed, stp)
  4070. Block seed;
  4071. register struct cast_stinfo *stp;
  4072. {
  4073. memcpy((void *)stp->str_iv, (void *)seed, sizeof(Block));
  4074. memcpy((void *)stp->str_output, (void *)seed, sizeof(Block));
  4075. stp->str_index = sizeof(Block);
  4076. }
  4077. static void
  4078. cast_fb64_stream_key(key, stp, fs)
  4079. unsigned char * key;
  4080. register struct cast_stinfo *stp;
  4081. int fs;
  4082. {
  4083. #ifndef CAST_EXPORT_ENCRYPTION
  4084. if(fs)
  4085. ck_cast128_key_sched(&stp->str_sched, key);
  4086. else
  4087. #endif
  4088. ck_cast5_40_key_sched(&stp->str_sched, key);
  4089. memcpy((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
  4090. stp->str_index = sizeof(Block);
  4091. }
  4092. /*
  4093. * CAST 64 bit Cipher Feedback
  4094. *
  4095. * key --->+------+
  4096. * +->| CAST |--+
  4097. * | +------+ |
  4098. * | v
  4099. * INPUT --(---------->(+)+---> DATA
  4100. * | |
  4101. * +--------------+
  4102. *
  4103. *
  4104. * Given:
  4105. * iV: Initial vector, 64 bits (8 bytes) long.
  4106. * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  4107. * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  4108. *
  4109. * V0 = CAST(iV, key)
  4110. * On = Dn ^ Vn
  4111. * V(n+1) = CAST(On, key)
  4112. */
  4113. #ifndef CAST_EXPORT_ENCRYPTION
  4114. void
  4115. cast_cfb64_encrypt(s, c)
  4116. register unsigned char *s;
  4117. int c;
  4118. {
  4119. _cast_cfb64_encrypt(s, c, &cast_fb[CFB_128].streams[DIR_ENCRYPT-1]);
  4120. }
  4121. #endif
  4122. void
  4123. castexp_cfb64_encrypt(s, c)
  4124. register unsigned char *s;
  4125. int c;
  4126. {
  4127. _cast_cfb64_encrypt(s, c, &cast_fb[CFB_40].streams[DIR_ENCRYPT-1]);
  4128. }
  4129. static void
  4130. _cast_cfb64_encrypt(s, c, stp)
  4131. register unsigned char *s;
  4132. int c;
  4133. register struct cast_stinfo *stp;
  4134. {
  4135. register int index;
  4136. index = stp->str_index;
  4137. while (c-- > 0) {
  4138. if (index == sizeof(Block)) {
  4139. Block b;
  4140. ck_cast_ecb_encrypt(b, stp->str_output, &stp->str_sched, 0);
  4141. memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4142. index = 0;
  4143. }
  4144. /* On encryption, we store (feed ^ data) which is cypher */
  4145. *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
  4146. s++;
  4147. index++;
  4148. }
  4149. stp->str_index = index;
  4150. }
  4151. #ifndef CAST_EXPORT_ENCRYPTION
  4152. int
  4153. cast_cfb64_decrypt(data)
  4154. int data;
  4155. {
  4156. return _cast_cfb64_decrypt(data, &cast_fb[CFB_128].streams[DIR_DECRYPT-1]);
  4157. }
  4158. #endif
  4159. int
  4160. castexp_cfb64_decrypt(data)
  4161. int data;
  4162. {
  4163. return _cast_cfb64_decrypt(data, &cast_fb[CFB_40].streams[DIR_DECRYPT-1]);
  4164. }
  4165. static int
  4166. _cast_cfb64_decrypt(data, stp)
  4167. int data;
  4168. register struct cast_stinfo *stp;
  4169. {
  4170. int index;
  4171. if (data == -1) {
  4172. /*
  4173. * Back up one byte. It is assumed that we will
  4174. * never back up more than one byte. If we do, this
  4175. * may or may not work.
  4176. */
  4177. if (stp->str_index)
  4178. --stp->str_index;
  4179. return(0);
  4180. }
  4181. index = stp->str_index++;
  4182. if (index == sizeof(Block)) {
  4183. Block b;
  4184. ck_cast_ecb_encrypt(b, stp->str_output, &stp->str_sched, 0);
  4185. memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4186. stp->str_index = 1; /* Next time will be 1 */
  4187. index = 0; /* But now use 0 */
  4188. }
  4189. /* On decryption we store (data) which is cypher. */
  4190. stp->str_output[index] = data;
  4191. return(data ^ stp->str_feed[index]);
  4192. }
  4193. /*
  4194. * CAST 64 bit Output Feedback
  4195. *
  4196. * key --->+------+
  4197. * +->| CAST |--+
  4198. * | +------+ |
  4199. * +------------+
  4200. * v
  4201. * INPUT --------->(+) ----> DATA
  4202. *
  4203. * Given:
  4204. * iV: Initial vector, 64 bits (8 bytes) long.
  4205. * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
  4206. * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
  4207. *
  4208. * V0 = CAST(iV, key)
  4209. * V(n+1) = CAST(Vn, key)
  4210. * On = Dn ^ Vn
  4211. */
  4212. #ifndef CAST_EXPORT_ENCRYPTION
  4213. void
  4214. cast_ofb64_encrypt(s, c)
  4215. register unsigned char *s;
  4216. int c;
  4217. {
  4218. _cast_ofb64_encrypt(s, c, &cast_fb[OFB_128].streams[DIR_ENCRYPT-1]);
  4219. }
  4220. #endif
  4221. void
  4222. castexp_ofb64_encrypt(s, c)
  4223. register unsigned char *s;
  4224. int c;
  4225. {
  4226. _cast_ofb64_encrypt(s, c, &cast_fb[OFB_40].streams[DIR_ENCRYPT-1]);
  4227. }
  4228. static void
  4229. _cast_ofb64_encrypt(s, c, stp)
  4230. register unsigned char *s;
  4231. int c;
  4232. register struct cast_stinfo *stp;
  4233. {
  4234. register int index;
  4235. index = stp->str_index;
  4236. while (c-- > 0) {
  4237. if (index == sizeof(Block)) {
  4238. Block b;
  4239. ck_cast_ecb_encrypt(b, stp->str_feed, &stp->str_sched, 0);
  4240. memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4241. index = 0;
  4242. }
  4243. *s++ ^= stp->str_feed[index];
  4244. index++;
  4245. }
  4246. stp->str_index = index;
  4247. }
  4248. #ifndef CAST_EXPORT_ENCRYPTION
  4249. int
  4250. cast_ofb64_decrypt(data)
  4251. int data;
  4252. {
  4253. return _cast_ofb64_decrypt(data, &cast_fb[OFB_128].streams[DIR_DECRYPT-1]);
  4254. }
  4255. #endif
  4256. int
  4257. castexp_ofb64_decrypt(data)
  4258. int data;
  4259. {
  4260. return _cast_ofb64_decrypt(data, &cast_fb[OFB_40].streams[DIR_DECRYPT-1]);
  4261. }
  4262. static int
  4263. _cast_ofb64_decrypt(data, stp)
  4264. int data;
  4265. register struct cast_stinfo *stp;
  4266. {
  4267. int index;
  4268. if (data == -1) {
  4269. /*
  4270. * Back up one byte. It is assumed that we will
  4271. * never back up more than one byte. If we do, this
  4272. * may or may not work.
  4273. */
  4274. if (stp->str_index)
  4275. --stp->str_index;
  4276. return(0);
  4277. }
  4278. index = stp->str_index++;
  4279. if (index == sizeof(Block)) {
  4280. Block b;
  4281. ck_cast_ecb_encrypt(b, stp->str_feed, &stp->str_sched, 0);
  4282. memcpy((void *)stp->str_feed, (void *)b, sizeof(Block));
  4283. stp->str_index = 1; /* Next time will be 1 */
  4284. index = 0; /* But now use 0 */
  4285. }
  4286. return(data ^ stp->str_feed[index]);
  4287. }
  4288. /*
  4289. * Copyright (c) 1997 Stanford University
  4290. *
  4291. * Permission to use, copy, modify, distribute, and sell this software and
  4292. * its documentation for any purpose is hereby granted without fee, provided
  4293. * that the above copyright notices and this permission notice appear in
  4294. * all copies of the software and related documentation.
  4295. *
  4296. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  4297. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  4298. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  4299. *
  4300. * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  4301. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  4302. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  4303. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  4304. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  4305. */
  4306. /*
  4307. * cast.c
  4308. * Author: Tom Wu
  4309. *
  4310. * An implementation of the CAST-128 encryption algorithm, as
  4311. * specified in RFC 2144.
  4312. */
  4313. /* The first four S-boxes are for encryption/decryption */
  4314. static uint32 S1[] = {
  4315. 0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 0x9c004dd3,
  4316. 0x6003e540, 0xcf9fc949, 0xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675,
  4317. 0x6e63a0e0, 0x15c361d2, 0xc2e7661d, 0x22d4ff8e, 0x28683b6f, 0xc07fd059,
  4318. 0xff2379c8, 0x775f50e2, 0x43c340d3, 0xdf2f8656, 0x887ca41a, 0xa2d2bd2d,
  4319. 0xa1c9e0d6, 0x346c4819, 0x61b76d87, 0x22540f2f, 0x2abe32e1, 0xaa54166b,
  4320. 0x22568e3a, 0xa2d341d0, 0x66db40c8, 0xa784392f, 0x004dff2f, 0x2db9d2de,
  4321. 0x97943fac, 0x4a97c1d8, 0x527644b7, 0xb5f437a7, 0xb82cbaef, 0xd751d159,
  4322. 0x6ff7f0ed, 0x5a097a1f, 0x827b68d0, 0x90ecf52e, 0x22b0c054, 0xbc8e5935,
  4323. 0x4b6d2f7f, 0x50bb64a2, 0xd2664910, 0xbee5812d, 0xb7332290, 0xe93b159f,
  4324. 0xb48ee411, 0x4bff345d, 0xfd45c240, 0xad31973f, 0xc4f6d02e, 0x55fc8165,
  4325. 0xd5b1caad, 0xa1ac2dae, 0xa2d4b76d, 0xc19b0c50, 0x882240f2, 0x0c6e4f38,
  4326. 0xa4e4bfd7, 0x4f5ba272, 0x564c1d2f, 0xc59c5319, 0xb949e354, 0xb04669fe,
  4327. 0xb1b6ab8a, 0xc71358dd, 0x6385c545, 0x110f935d, 0x57538ad5, 0x6a390493,
  4328. 0xe63d37e0, 0x2a54f6b3, 0x3a787d5f, 0x6276a0b5, 0x19a6fcdf, 0x7a42206a,
  4329. 0x29f9d4d5, 0xf61b1891, 0xbb72275e, 0xaa508167, 0x38901091, 0xc6b505eb,
  4330. 0x84c7cb8c, 0x2ad75a0f, 0x874a1427, 0xa2d1936b, 0x2ad286af, 0xaa56d291,
  4331. 0xd7894360, 0x425c750d, 0x93b39e26, 0x187184c9, 0x6c00b32d, 0x73e2bb14,
  4332. 0xa0bebc3c, 0x54623779, 0x64459eab, 0x3f328b82, 0x7718cf82, 0x59a2cea6,
  4333. 0x04ee002e, 0x89fe78e6, 0x3fab0950, 0x325ff6c2, 0x81383f05, 0x6963c5c8,
  4334. 0x76cb5ad6, 0xd49974c9, 0xca180dcf, 0x380782d5, 0xc7fa5cf6, 0x8ac31511,
  4335. 0x35e79e13, 0x47da91d0, 0xf40f9086, 0xa7e2419e, 0x31366241, 0x051ef495,
  4336. 0xaa573b04, 0x4a805d8d, 0x548300d0, 0x00322a3c, 0xbf64cddf, 0xba57a68e,
  4337. 0x75c6372b, 0x50afd341, 0xa7c13275, 0x915a0bf5, 0x6b54bfab, 0x2b0b1426,
  4338. 0xab4cc9d7, 0x449ccd82, 0xf7fbf265, 0xab85c5f3, 0x1b55db94, 0xaad4e324,
  4339. 0xcfa4bd3f, 0x2deaa3e2, 0x9e204d02, 0xc8bd25ac, 0xeadf55b3, 0xd5bd9e98,
  4340. 0xe31231b2, 0x2ad5ad6c, 0x954329de, 0xadbe4528, 0xd8710f69, 0xaa51c90f,
  4341. 0xaa786bf6, 0x22513f1e, 0xaa51a79b, 0x2ad344cc,0x7b5a41f0, 0xd37cfbad,
  4342. 0x1b069505, 0x41ece491, 0xb4c332e6, 0x032268d4, 0xc9600acc, 0xce387e6d,
  4343. 0xbf6bb16c, 0x6a70fb78, 0x0d03d9c9, 0xd4df39de, 0xe01063da, 0x4736f464,
  4344. 0x5ad328d8, 0xb347cc96, 0x75bb0fc3, 0x98511bfb, 0x4ffbcc35, 0xb58bcf6a,
  4345. 0xe11f0abc, 0xbfc5fe4a, 0xa70aec10, 0xac39570a,0x3f04442f, 0x6188b153,
  4346. 0xe0397a2e, 0x5727cb79, 0x9ceb418f, 0x1cacd68d, 0x2ad37c96, 0x0175cb9d,
  4347. 0xc69dff09, 0xc75b65f0, 0xd9db40d8, 0xec0e7779, 0x4744ead4, 0xb11c3274,
  4348. 0xdd24cb9e, 0x7e1c54bd, 0xf01144f9, 0xd2240eb1, 0x9675b3fd, 0xa3ac3755,
  4349. 0xd47c27af, 0x51c85f4d, 0x56907596, 0xa5bb15e6,0x580304f0, 0xca042cf1,
  4350. 0x011a37ea, 0x8dbfaadb, 0x35ba3e4a, 0x3526ffa0, 0xc37b4d09, 0xbc306ed9,
  4351. 0x98a52666, 0x5648f725, 0xff5e569d, 0x0ced63d0, 0x7c63b2cf, 0x700b45e1,
  4352. 0xd5ea50f1, 0x85a92872, 0xaf1fbda7, 0xd4234870, 0xa7870bf3, 0x2d3b4d79,
  4353. 0x42e04198, 0x0cd0ede7, 0x26470db8, 0xf881814c,0x474d6ad7, 0x7c0c5e5c,
  4354. 0xd1231959, 0x381b7298, 0xf5d2f4db, 0xab838653, 0x6e2f1e23, 0x83719c9e,
  4355. 0xbd91e046, 0x9a56456e, 0xdc39200c, 0x20c8c571, 0x962bda1c, 0xe1e696ff,
  4356. 0xb141ab08, 0x7cca89b9, 0x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d,
  4357. 0x427b169c, 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf
  4358. };
  4359. static uint32 S2[] = {
  4360. 0x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a, 0xeec5207a,
  4361. 0x55889c94, 0x72fc0651, 0xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba,
  4362. 0x99c430ef, 0x5f0c0794, 0x18dcdb7d, 0xa1d6eff3, 0xa0b52f7b, 0x59e83605,
  4363. 0xee15b094, 0xe9ffd909, 0xdc440086, 0xef944459, 0xba83ccb3, 0xe0c3cdfb,
  4364. 0xd1da4181, 0x3b092ab1, 0xf997f1c1, 0xa5e6cf7b, 0x01420ddb, 0xe4e7ef5b,
  4365. 0x25a1ff41, 0xe180f806, 0x1fc41080, 0x179bee7a, 0xd37ac6a9, 0xfe5830a4,
  4366. 0x98de8b7f, 0x77e83f4e, 0x79929269, 0x24fa9f7b, 0xe113c85b, 0xacc40083,
  4367. 0xd7503525, 0xf7ea615f, 0x62143154, 0x0d554b63, 0x5d681121, 0xc866c359,
  4368. 0x3d63cf73, 0xcee234c0, 0xd4d87e87, 0x5c672b21, 0x071f6181, 0x39f7627f,
  4369. 0x361e3084, 0xe4eb573b, 0x602f64a4, 0xd63acd9c, 0x1bbc4635, 0x9e81032d,
  4370. 0x2701f50c, 0x99847ab4, 0xa0e3df79, 0xba6cf38c, 0x10843094, 0x2537a95e,
  4371. 0xf46f6ffe, 0xa1ff3b1f, 0x208cfb6a, 0x8f458c74, 0xd9e0a227, 0x4ec73a34,
  4372. 0xfc884f69, 0x3e4de8df, 0xef0e0088, 0x3559648d, 0x8a45388c, 0x1d804366,
  4373. 0x721d9bfd, 0xa58684bb, 0xe8256333, 0x844e8212, 0x128d8098, 0xfed33fb4,
  4374. 0xce280ae1, 0x27e19ba5, 0xd5a6c252, 0xe49754bd, 0xc5d655dd, 0xeb667064,
  4375. 0x77840b4d, 0xa1b6a801, 0x84db26a9, 0xe0b56714, 0x21f043b7, 0xe5d05860,
  4376. 0x54f03084, 0x066ff472, 0xa31aa153, 0xdadc4755, 0xb5625dbf, 0x68561be6,
  4377. 0x83ca6b94, 0x2d6ed23b, 0xeccf01db, 0xa6d3d0ba, 0xb6803d5c, 0xaf77a709,
  4378. 0x33b4a34c, 0x397bc8d6, 0x5ee22b95, 0x5f0e5304, 0x81ed6f61, 0x20e74364,
  4379. 0xb45e1378, 0xde18639b, 0x881ca122, 0xb96726d1, 0x8049a7e8, 0x22b7da7b,
  4380. 0x5e552d25, 0x5272d237, 0x79d2951c, 0xc60d894c, 0x488cb402, 0x1ba4fe5b,
  4381. 0xa4b09f6b, 0x1ca815cf, 0xa20c3005, 0x8871df63, 0xb9de2fcb, 0x0cc6c9e9,
  4382. 0x0beeff53, 0xe3214517, 0xb4542835, 0x9f63293c, 0xee41e729, 0x6e1d2d7c,
  4383. 0x50045286, 0x1e6685f3, 0xf33401c6, 0x30a22c95, 0x31a70850, 0x60930f13,
  4384. 0x73f98417, 0xa1269859, 0xec645c44, 0x52c877a9, 0xcdff33a6, 0xa02b1741,
  4385. 0x7cbad9a2, 0x2180036f, 0x50d99c08, 0xcb3f4861, 0xc26bd765, 0x64a3f6ab,
  4386. 0x80342676, 0x25a75e7b, 0xe4e6d1fc, 0x20c710e6, 0xcdf0b680, 0x17844d3b,
  4387. 0x31eef84d, 0x7e0824e4, 0x2ccb49eb, 0x846a3bae, 0x8ff77888, 0xee5d60f6,
  4388. 0x7af75673, 0x2fdd5cdb, 0xa11631c1, 0x30f66f43, 0xb3faec54, 0x157fd7fa,
  4389. 0xef8579cc, 0xd152de58, 0xdb2ffd5e, 0x8f32ce19, 0x306af97a, 0x02f03ef8,
  4390. 0x99319ad5, 0xc242fa0f, 0xa7e3ebb0, 0xc68e4906, 0xb8da230c, 0x80823028,
  4391. 0xdcdef3c8, 0xd35fb171, 0x088a1bc8, 0xbec0c560, 0x61a3c9e8, 0xbca8f54d,
  4392. 0xc72feffa, 0x22822e99, 0x82c570b4, 0xd8d94e89, 0x8b1c34bc, 0x301e16e6,
  4393. 0x273be979, 0xb0ffeaa6, 0x61d9b8c6, 0x00b24869, 0xb7ffce3f, 0x08dc283b,
  4394. 0x43daf65a, 0xf7e19798, 0x7619b72f, 0x8f1c9ba4, 0xdc8637a0, 0x16a7d3b1,
  4395. 0x9fc393b7, 0xa7136eeb, 0xc6bcc63e, 0x1a513742, 0xef6828bc, 0x520365d6,
  4396. 0x2d6a77ab, 0x3527ed4b, 0x821fd216, 0x095c6e2e, 0xdb92f2fb, 0x5eea29cb,
  4397. 0x145892f5, 0x91584f7f, 0x5483697b, 0x2667a8cc, 0x85196048, 0x8c4bacea,
  4398. 0x833860d4, 0x0d23e0f9, 0x6c387e8a, 0x0ae6d249, 0xb284600c, 0xd835731d,
  4399. 0xdcb1c647, 0xac4c56ea, 0x3ebd81b3, 0x230eabb0, 0x6438bc87, 0xf0b5b1fa,
  4400. 0x8f5ea2b3, 0xfc184642, 0x0a036b7a, 0x4fb089bd, 0x649da589, 0xa345415e,
  4401. 0x5c038323, 0x3e5d3bb9, 0x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef,
  4402. 0x7160a539, 0x73bfbe70, 0x83877605, 0x4523ecf1
  4403. };
  4404. static uint32 S3[] = {
  4405. 0x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff, 0x369fe44b,
  4406. 0x8c1fc644, 0xaececa90, 0xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae,
  4407. 0x920e8806, 0xf0ad0548, 0xe13c8d83, 0x927010d5, 0x11107d9f, 0x07647db9,
  4408. 0xb2e3e4d4, 0x3d4f285e, 0xb9afa820, 0xfade82e0, 0xa067268b, 0x8272792e,
  4409. 0x553fb2c0, 0x489ae22b, 0xd4ef9794, 0x125e3fbc, 0x21fffcee, 0x825b1bfd,
  4410. 0x9255c5ed, 0x1257a240, 0x4e1a8302, 0xbae07fff, 0x528246e7, 0x8e57140e,
  4411. 0x3373f7bf, 0x8c9f8188, 0xa6fc4ee8, 0xc982b5a5, 0xa8c01db7, 0x579fc264,
  4412. 0x67094f31, 0xf2bd3f5f, 0x40fff7c1, 0x1fb78dfc, 0x8e6bd2c1, 0x437be59b,
  4413. 0x99b03dbf, 0xb5dbc64b, 0x638dc0e6, 0x55819d99, 0xa197c81c, 0x4a012d6e,
  4414. 0xc5884a28, 0xccc36f71, 0xb843c213, 0x6c0743f1, 0x8309893c, 0x0feddd5f,
  4415. 0x2f7fe850, 0xd7c07f7e, 0x02507fbf, 0x5afb9a04, 0xa747d2d0, 0x1651192e,
  4416. 0xaf70bf3e, 0x58c31380, 0x5f98302e, 0x727cc3c4, 0x0a0fb402, 0x0f7fef82,
  4417. 0x8c96fdad, 0x5d2c2aae, 0x8ee99a49, 0x50da88b8, 0x8427f4a0, 0x1eac5790,
  4418. 0x796fb449, 0x8252dc15, 0xefbd7d9b, 0xa672597d, 0xada840d8, 0x45f54504,
  4419. 0xfa5d7403, 0xe83ec305, 0x4f91751a, 0x925669c2, 0x23efe941, 0xa903f12e,
  4420. 0x60270df2, 0x0276e4b6, 0x94fd6574, 0x927985b2, 0x8276dbcb, 0x02778176,
  4421. 0xf8af918d, 0x4e48f79e, 0x8f616ddf, 0xe29d840e, 0x842f7d83, 0x340ce5c8,
  4422. 0x96bbb682, 0x93b4b148, 0xef303cab, 0x984faf28, 0x779faf9b, 0x92dc560d,
  4423. 0x224d1e20, 0x8437aa88, 0x7d29dc96, 0x2756d3dc, 0x8b907cee, 0xb51fd240,
  4424. 0xe7c07ce3, 0xe566b4a1, 0xc3e9615e, 0x3cf8209d, 0x6094d1e3, 0xcd9ca341,
  4425. 0x5c76460e, 0x00ea983b, 0xd4d67881, 0xfd47572c, 0xf76cedd9, 0xbda8229c,
  4426. 0x127dadaa, 0x438a074e, 0x1f97c090, 0x081bdb8a, 0x93a07ebe, 0xb938ca15,
  4427. 0x97b03cff, 0x3dc2c0f8, 0x8d1ab2ec, 0x64380e51, 0x68cc7bfb, 0xd90f2788,
  4428. 0x12490181, 0x5de5ffd4, 0xdd7ef86a, 0x76a2e214, 0xb9a40368, 0x925d958f,
  4429. 0x4b39fffa, 0xba39aee9, 0xa4ffd30b, 0xfaf7933b, 0x6d498623, 0x193cbcfa,
  4430. 0x27627545, 0x825cf47a, 0x61bd8ba0, 0xd11e42d1, 0xcead04f4, 0x127ea392,
  4431. 0x10428db7, 0x8272a972, 0x9270c4a8, 0x127de50b, 0x285ba1c8, 0x3c62f44f,
  4432. 0x35c0eaa5, 0xe805d231, 0x428929fb, 0xb4fcdf82, 0x4fb66a53, 0x0e7dc15b,
  4433. 0x1f081fab, 0x108618ae, 0xfcfd086d, 0xf9ff2889, 0x694bcc11, 0x236a5cae,
  4434. 0x12deca4d, 0x2c3f8cc5, 0xd2d02dfe, 0xf8ef5896, 0xe4cf52da, 0x95155b67,
  4435. 0x494a488c, 0xb9b6a80c, 0x5c8f82bc, 0x89d36b45, 0x3a609437, 0xec00c9a9,
  4436. 0x44715253, 0x0a874b49, 0xd773bc40, 0x7c34671c, 0x02717ef6, 0x4feb5536,
  4437. 0xa2d02fff, 0xd2bf60c4, 0xd43f03c0, 0x50b4ef6d, 0x07478cd1, 0x006e1888,
  4438. 0xa2e53f55, 0xb9e6d4bc, 0xa2048016, 0x97573833, 0xd7207d67, 0xde0f8f3d,
  4439. 0x72f87b33, 0xabcc4f33, 0x7688c55d, 0x7b00a6b0, 0x947b0001, 0x570075d2,
  4440. 0xf9bb88f8, 0x8942019e, 0x4264a5ff, 0x856302e0, 0x72dbd92b, 0xee971b69,
  4441. 0x6ea22fde, 0x5f08ae2b, 0xaf7a616d, 0xe5c98767, 0xcf1febd2, 0x61efc8c2,
  4442. 0xf1ac2571, 0xcc8239c2, 0x67214cb8, 0xb1e583d1, 0xb7dc3e62, 0x7f10bdce,
  4443. 0xf90a5c38, 0x0ff0443d, 0x606e6dc6, 0x60543a49, 0x5727c148, 0x2be98a1d,
  4444. 0x8ab41738, 0x20e1be24, 0xaf96da0f, 0x68458425, 0x99833be5, 0x600d457d,
  4445. 0x282f9350, 0x8334b362, 0xd91d1120, 0x2b6d8da0, 0x642b1e31, 0x9c305a00,
  4446. 0x52bce688, 0x1b03588a, 0xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5,
  4447. 0xdfef4636, 0xa133c501, 0xe9d3531c, 0xee353783
  4448. };
  4449. static uint32 S4[] = {
  4450. 0x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb, 0x64ad8c57,
  4451. 0x85510443, 0xfa020ed1, 0x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120,
  4452. 0xfd059d43, 0x6497b7b1, 0xf3641f63, 0x241e4adf, 0x28147f5f, 0x4fa2b8cd,
  4453. 0xc9430040, 0x0cc32220, 0xfdd30b30, 0xc0a5374f, 0x1d2d00d9, 0x24147b15,
  4454. 0xee4d111a, 0x0fca5167, 0x71ff904c, 0x2d195ffe, 0x1a05645f, 0x0c13fefe,
  4455. 0x081b08ca, 0x05170121, 0x80530100, 0xe83e5efe, 0xac9af4f8, 0x7fe72701,
  4456. 0xd2b8ee5f, 0x06df4261, 0xbb9e9b8a, 0x7293ea25, 0xce84ffdf, 0xf5718801,
  4457. 0x3dd64b04, 0xa26f263b, 0x7ed48400, 0x547eebe6, 0x446d4ca0, 0x6cf3d6f5,
  4458. 0x2649abdf, 0xaea0c7f5, 0x36338cc1, 0x503f7e93, 0xd3772061, 0x11b638e1,
  4459. 0x72500e03, 0xf80eb2bb, 0xabe0502e, 0xec8d77de, 0x57971e81, 0xe14f6746,
  4460. 0xc9335400, 0x6920318f, 0x081dbb99, 0xffc304a5, 0x4d351805, 0x7f3d5ce3,
  4461. 0xa6c866c6, 0x5d5bcca9, 0xdaec6fea, 0x9f926f91, 0x9f46222f, 0x3991467d,
  4462. 0xa5bf6d8e, 0x1143c44f, 0x43958302, 0xd0214eeb, 0x022083b8, 0x3fb6180c,
  4463. 0x18f8931e, 0x281658e6, 0x26486e3e, 0x8bd78a70, 0x7477e4c1, 0xb506e07c,
  4464. 0xf32d0a25, 0x79098b02, 0xe4eabb81, 0x28123b23, 0x69dead38, 0x1574ca16,
  4465. 0xdf871b62, 0x211c40b7, 0xa51a9ef9, 0x0014377b, 0x041e8ac8, 0x09114003,
  4466. 0xbd59e4d2, 0xe3d156d5, 0x4fe876d5, 0x2f91a340, 0x557be8de, 0x00eae4a7,
  4467. 0x0ce5c2ec, 0x4db4bba6, 0xe756bdff, 0xdd3369ac, 0xec17b035, 0x06572327,
  4468. 0x99afc8b0, 0x56c8c391, 0x6b65811c, 0x5e146119, 0x6e85cb75, 0xbe07c002,
  4469. 0xc2325577, 0x893ff4ec, 0x5bbfc92d, 0xd0ec3b25, 0xb7801ab7, 0x8d6d3b24,
  4470. 0x20c763ef, 0xc366a5fc, 0x9c382880, 0x0ace3205, 0xaac9548a, 0xeca1d7c7,
  4471. 0x041afa32, 0x1d16625a, 0x6701902c, 0x9b757a54, 0x31d477f7, 0x9126b031,
  4472. 0x36cc6fdb, 0xc70b8b46, 0xd9e66a48, 0x56e55a79, 0x026a4ceb, 0x52437eff,
  4473. 0x2f8f76b4, 0x0df980a5, 0x8674cde3, 0xedda04eb, 0x17a9be04, 0x2c18f4df,
  4474. 0xb7747f9d, 0xab2af7b4, 0xefc34d20, 0x2e096b7c, 0x1741a254, 0xe5b6a035,
  4475. 0x213d42f6, 0x2c1c7c26, 0x61c2f50f, 0x6552daf9, 0xd2c231f8, 0x25130f69,
  4476. 0xd8167fa2, 0x0418f2c8, 0x001a96a6, 0x0d1526ab, 0x63315c21, 0x5e0a72ec,
  4477. 0x49bafefd, 0x187908d9, 0x8d0dbd86, 0x311170a7, 0x3e9b640c, 0xcc3e10d7,
  4478. 0xd5cad3b6, 0x0caec388, 0xf73001e1, 0x6c728aff, 0x71eae2a1, 0x1f9af36e,
  4479. 0xcfcbd12f, 0xc1de8417, 0xac07be6b, 0xcb44a1d8, 0x8b9b0f56, 0x013988c3,
  4480. 0xb1c52fca, 0xb4be31cd, 0xd8782806, 0x12a3a4e2, 0x6f7de532, 0x58fd7eb6,
  4481. 0xd01ee900, 0x24adffc2, 0xf4990fc5, 0x9711aac5, 0x001d7b95, 0x82e5e7d2,
  4482. 0x109873f6, 0x00613096, 0xc32d9521, 0xada121ff, 0x29908415, 0x7fbb977f,
  4483. 0xaf9eb3db, 0x29c9ed2a, 0x5ce2a465, 0xa730f32c, 0xd0aa3fe8, 0x8a5cc091,
  4484. 0xd49e2ce7, 0x0ce454a9, 0xd60acd86, 0x015f1919, 0x77079103, 0xdea03af6,
  4485. 0x78a8565e, 0xdee356df, 0x21f05cbe, 0x8b75e387, 0xb3c50651, 0xb8a5c3ef,
  4486. 0xd8eeb6d2, 0xe523be77, 0xc2154529, 0x2f69efdf, 0xafe67afb, 0xf470c4b2,
  4487. 0xf3e0eb5b, 0xd6cc9876, 0x39e4460c, 0x1fda8538, 0x1987832f, 0xca007367,
  4488. 0xa99144f8, 0x296b299e, 0x492fc295, 0x9266beab, 0xb5676e69, 0x9bd3ddda,
  4489. 0xdf7e052f, 0xdb25701c, 0x1b5e51ee, 0xf65324e6, 0x6afce36c, 0x0316cc04,
  4490. 0x8644213e, 0xb7dc59d0, 0x7965291f, 0xccd6fd43, 0x41823979, 0x932bcdf6,
  4491. 0xb657c34d, 0x4edfd282, 0x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e,
  4492. 0x13ecf0b0, 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2
  4493. };
  4494. /* Encrypt/decrypt one 64-bit block of data */
  4495. void
  4496. ck_cast_ecb_encrypt(out, in, sched, mode)
  4497. uint8p out;
  4498. uint8p in;
  4499. CastKeySched * sched;
  4500. int mode; /* zero means encrypt */
  4501. {
  4502. uint32 t[2];
  4503. #ifdef LITTLE_ENDIAN
  4504. t[0] = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
  4505. t[1] = (in[4] << 24) | (in[5] << 16) | (in[6] << 8) | in[7];
  4506. #else
  4507. t[0] = *(uint32p) in;
  4508. t[1] = *(uint32p) (in + 4);
  4509. #endif
  4510. ck_cast_ecb_crypt(t, sched, mode);
  4511. #ifdef LITTLE_ENDIAN
  4512. out[0] = (t[0] >> 24) & 0xff;
  4513. out[1] = (t[0] >> 16) & 0xff;
  4514. out[2] = (t[0] >> 8) & 0xff;
  4515. out[3] = t[0] & 0xff;
  4516. out[4] = (t[1] >> 24) & 0xff;
  4517. out[5] = (t[1] >> 16) & 0xff;
  4518. out[6] = (t[1] >> 8) & 0xff;
  4519. out[7] = t[1] & 0xff;
  4520. #else
  4521. *(uint32p) out = t[0];
  4522. *(uint32p) (out + 4) = t[1];
  4523. #endif
  4524. }
  4525. void
  4526. ck_cast_ecb_crypt(data, sched, mode)
  4527. uint32p data;
  4528. CastKeySched * sched;
  4529. int mode;
  4530. {
  4531. register uint32 L, R, temp;
  4532. register struct CastSubkeyPair * kp;
  4533. register uint8p Ia, Ib, Ic, Id;
  4534. uint32 I;
  4535. #ifdef LITTLE_ENDIAN
  4536. Id = (uint8p) &I;
  4537. Ic = Id + 1;
  4538. Ib = Ic + 1;
  4539. Ia = Ib + 1;
  4540. #else
  4541. Ia = (uint8p) &I;
  4542. Ib = Ia + 1;
  4543. Ic = Ib + 1;
  4544. Id = Ic + 1;
  4545. #endif
  4546. L = data[0];
  4547. R = data[1];
  4548. #define type0(left,right) \
  4549. temp = kp->Km + right;\
  4550. I = (temp << kp->Kr) | (temp >> (32 - kp->Kr));\
  4551. left ^= ((S1[*Ia] ^ S2[*Ib]) - S3[*Ic]) + S4[*Id];
  4552. #define type1(left,right) \
  4553. temp = kp->Km ^ right;\
  4554. I = (temp << kp->Kr) | (temp >> (32 - kp->Kr));\
  4555. left ^= ((S1[*Ia] - S2[*Ib]) + S3[*Ic]) ^ S4[*Id];
  4556. #define type2(left,right) \
  4557. temp = kp->Km - right;\
  4558. I = (temp << kp->Kr) | (temp >> (32 - kp->Kr));\
  4559. left ^= ((S1[*Ia] + S2[*Ib]) ^ S3[*Ic]) - S4[*Id];
  4560. if(mode) {
  4561. #ifndef CAST_EXPORT_ENCRYPTION
  4562. if(sched->ksize > 10) {
  4563. kp = &sched->K[15];
  4564. type0(L, R); --kp;
  4565. type2(R, L); --kp;
  4566. type1(L, R); --kp;
  4567. type0(R, L); --kp;
  4568. }
  4569. else
  4570. #endif
  4571. kp = &sched->K[11];
  4572. type2(L, R); --kp;
  4573. type1(R, L); --kp;
  4574. type0(L, R); --kp;
  4575. type2(R, L); --kp;
  4576. type1(L, R); --kp;
  4577. type0(R, L); --kp;
  4578. type2(L, R); --kp;
  4579. type1(R, L); --kp;
  4580. type0(L, R); --kp;
  4581. type2(R, L); --kp;
  4582. type1(L, R); --kp;
  4583. type0(R, L);
  4584. }
  4585. else {
  4586. kp = &sched->K[0];
  4587. type0(L, R); ++kp;
  4588. type1(R, L); ++kp;
  4589. type2(L, R); ++kp;
  4590. type0(R, L); ++kp;
  4591. type1(L, R); ++kp;
  4592. type2(R, L); ++kp;
  4593. type0(L, R); ++kp;
  4594. type1(R, L); ++kp;
  4595. type2(L, R); ++kp;
  4596. type0(R, L); ++kp;
  4597. type1(L, R); ++kp;
  4598. type2(R, L); ++kp;
  4599. #ifndef CAST_EXPORT_ENCRYPTION
  4600. if(sched->ksize > 10) {
  4601. type0(L, R); ++kp;
  4602. type1(R, L); ++kp;
  4603. type2(L, R); ++kp;
  4604. type0(R, L);
  4605. }
  4606. #endif
  4607. }
  4608. data[0] = R;
  4609. data[1] = L;
  4610. }
  4611. /* The last four S-boxes are for key schedule setup */
  4612. static uint32 S5[] = {
  4613. 0x7ec90c04, 0x2c6e74b9, 0x9b0e66df, 0xa6337911, 0xb86a7fff, 0x1dd358f5,
  4614. 0x44dd9d44, 0x1731167f, 0x08fbf1fa, 0xe7f511cc, 0xd2051b00, 0x735aba00,
  4615. 0x2ab722d8, 0x386381cb, 0xacf6243a, 0x69befd7a, 0xe6a2e77f, 0xf0c720cd,
  4616. 0xc4494816, 0xccf5c180, 0x38851640, 0x15b0a848, 0xe68b18cb, 0x4caadeff,
  4617. 0x5f480a01, 0x0412b2aa, 0x259814fc, 0x41d0efe2, 0x4e40b48d, 0x248eb6fb,
  4618. 0x8dba1cfe, 0x41a99b02, 0x1a550a04, 0xba8f65cb, 0x7251f4e7, 0x95a51725,
  4619. 0xc106ecd7, 0x97a5980a, 0xc539b9aa, 0x4d79fe6a, 0xf2f3f763, 0x68af8040,
  4620. 0xed0c9e56, 0x11b4958b, 0xe1eb5a88, 0x8709e6b0, 0xd7e07156, 0x4e29fea7,
  4621. 0x6366e52d, 0x02d1c000, 0xc4ac8e05, 0x9377f571, 0x0c05372a, 0x578535f2,
  4622. 0x2261be02, 0xd642a0c9, 0xdf13a280, 0x74b55bd2, 0x682199c0, 0xd421e5ec,
  4623. 0x53fb3ce8, 0xc8adedb3, 0x28a87fc9, 0x3d959981, 0x5c1ff900, 0xfe38d399,
  4624. 0x0c4eff0b, 0x062407ea, 0xaa2f4fb1, 0x4fb96976, 0x90c79505, 0xb0a8a774,
  4625. 0xef55a1ff, 0xe59ca2c2, 0xa6b62d27, 0xe66a4263, 0xdf65001f, 0x0ec50966,
  4626. 0xdfdd55bc, 0x29de0655, 0x911e739a, 0x17af8975, 0x32c7911c, 0x89f89468,
  4627. 0x0d01e980, 0x524755f4, 0x03b63cc9, 0x0cc844b2, 0xbcf3f0aa, 0x87ac36e9,
  4628. 0xe53a7426, 0x01b3d82b, 0x1a9e7449, 0x64ee2d7e, 0xcddbb1da, 0x01c94910,
  4629. 0xb868bf80, 0x0d26f3fd, 0x9342ede7, 0x04a5c284, 0x636737b6, 0x50f5b616,
  4630. 0xf24766e3, 0x8eca36c1, 0x136e05db, 0xfef18391, 0xfb887a37, 0xd6e7f7d4,
  4631. 0xc7fb7dc9, 0x3063fcdf, 0xb6f589de, 0xec2941da, 0x26e46695, 0xb7566419,
  4632. 0xf654efc5, 0xd08d58b7, 0x48925401, 0xc1bacb7f, 0xe5ff550f, 0xb6083049,
  4633. 0x5bb5d0e8, 0x87d72e5a, 0xab6a6ee1, 0x223a66ce, 0xc62bf3cd, 0x9e0885f9,
  4634. 0x68cb3e47, 0x086c010f, 0xa21de820, 0xd18b69de, 0xf3f65777, 0xfa02c3f6,
  4635. 0x407edac3, 0xcbb3d550, 0x1793084d, 0xb0d70eba, 0x0ab378d5, 0xd951fb0c,
  4636. 0xded7da56, 0x4124bbe4, 0x94ca0b56, 0x0f5755d1, 0xe0e1e56e, 0x6184b5be,
  4637. 0x580a249f, 0x94f74bc0, 0xe327888e, 0x9f7b5561, 0xc3dc0280, 0x05687715,
  4638. 0x646c6bd7, 0x44904db3, 0x66b4f0a3, 0xc0f1648a, 0x697ed5af, 0x49e92ff6,
  4639. 0x309e374f, 0x2cb6356a, 0x85808573, 0x4991f840, 0x76f0ae02, 0x083be84d,
  4640. 0x28421c9a, 0x44489406, 0x736e4cb8, 0xc1092910, 0x8bc95fc6, 0x7d869cf4,
  4641. 0x134f616f, 0x2e77118d, 0xb31b2be1, 0xaa90b472, 0x3ca5d717, 0x7d161bba,
  4642. 0x9cad9010, 0xaf462ba2, 0x9fe459d2, 0x45d34559, 0xd9f2da13, 0xdbc65487,
  4643. 0xf3e4f94e, 0x176d486f, 0x097c13ea, 0x631da5c7, 0x445f7382, 0x175683f4,
  4644. 0xcdc66a97, 0x70be0288, 0xb3cdcf72, 0x6e5dd2f3, 0x20936079, 0x459b80a5,
  4645. 0xbe60e2db, 0xa9c23101, 0xeba5315c, 0x224e42f2, 0x1c5c1572, 0xf6721b2c,
  4646. 0x1ad2fff3, 0x8c25404e, 0x324ed72f, 0x4067b7fd, 0x0523138e, 0x5ca3bc78,
  4647. 0xdc0fd66e, 0x75922283, 0x784d6b17, 0x58ebb16e, 0x44094f85, 0x3f481d87,
  4648. 0xfcfeae7b, 0x77b5ff76, 0x8c2302bf, 0xaaf47556, 0x5f46b02a, 0x2b092801,
  4649. 0x3d38f5f7, 0x0ca81f36, 0x52af4a8a, 0x66d5e7c0, 0xdf3b0874, 0x95055110,
  4650. 0x1b5ad7a8, 0xf61ed5ad, 0x6cf6e479, 0x20758184, 0xd0cefa65, 0x88f7be58,
  4651. 0x4a046826, 0x0ff6f8f3, 0xa09c7f70, 0x5346aba0, 0x5ce96c28, 0xe176eda3,
  4652. 0x6bac307f, 0x376829d2, 0x85360fa9, 0x17e3fe2a, 0x24b79767, 0xf5a96b20,
  4653. 0xd6cd2595, 0x68ff1ebf, 0x7555442c, 0xf19f06be, 0xf9e0659a, 0xeeb9491d,
  4654. 0x34010718, 0xbb30cab8, 0xe822fe15, 0x88570983, 0x750e6249, 0xda627e55,
  4655. 0x5e76ffa8, 0xb1534546, 0x6d47de08, 0xefe9e7d4
  4656. };
  4657. static uint32 S6[] = {
  4658. 0xf6fa8f9d, 0x2cac6ce1, 0x4ca34867, 0xe2337f7c, 0x95db08e7, 0x016843b4,
  4659. 0xeced5cbc, 0x325553ac, 0xbf9f0960, 0xdfa1e2ed, 0x83f0579d, 0x63ed86b9,
  4660. 0x1ab6a6b8, 0xde5ebe39, 0xf38ff732, 0x8989b138, 0x33f14961, 0xc01937bd,
  4661. 0xf506c6da, 0xe4625e7e, 0xa308ea99, 0x4e23e33c, 0x79cbd7cc, 0x48a14367,
  4662. 0xa3149619, 0xfec94bd5, 0xa114174a, 0xeaa01866, 0xa084db2d, 0x09a8486f,
  4663. 0xa888614a, 0x2900af98, 0x01665991, 0xe1992863, 0xc8f30c60, 0x2e78ef3c,
  4664. 0xd0d51932, 0xcf0fec14, 0xf7ca07d2, 0xd0a82072, 0xfd41197e, 0x9305a6b0,
  4665. 0xe86be3da, 0x74bed3cd, 0x372da53c, 0x4c7f4448, 0xdab5d440, 0x6dba0ec3,
  4666. 0x083919a7, 0x9fbaeed9, 0x49dbcfb0, 0x4e670c53, 0x5c3d9c01, 0x64bdb941,
  4667. 0x2c0e636a, 0xba7dd9cd, 0xea6f7388, 0xe70bc762, 0x35f29adb, 0x5c4cdd8d,
  4668. 0xf0d48d8c, 0xb88153e2, 0x08a19866, 0x1ae2eac8, 0x284caf89, 0xaa928223,
  4669. 0x9334be53, 0x3b3a21bf, 0x16434be3, 0x9aea3906, 0xefe8c36e, 0xf890cdd9,
  4670. 0x80226dae, 0xc340a4a3, 0xdf7e9c09, 0xa694a807, 0x5b7c5ecc, 0x221db3a6,
  4671. 0x9a69a02f, 0x68818a54, 0xceb2296f, 0x53c0843a, 0xfe893655, 0x25bfe68a,
  4672. 0xb4628abc, 0xcf222ebf, 0x25ac6f48, 0xa9a99387, 0x53bddb65, 0xe76ffbe7,
  4673. 0xe967fd78, 0x0ba93563, 0x8e342bc1, 0xe8a11be9, 0x4980740d, 0xc8087dfc,
  4674. 0x8de4bf99, 0xa11101a0, 0x7fd37975, 0xda5a26c0, 0xe81f994f, 0x9528cd89,
  4675. 0xfd339fed, 0xb87834bf, 0x5f04456d, 0x22258698, 0xc9c4c83b, 0x2dc156be,
  4676. 0x4f628daa, 0x57f55ec5, 0xe2220abe, 0xd2916ebf, 0x4ec75b95, 0x24f2c3c0,
  4677. 0x42d15d99, 0xcd0d7fa0, 0x7b6e27ff, 0xa8dc8af0, 0x7345c106, 0xf41e232f,
  4678. 0x35162386, 0xe6ea8926, 0x3333b094, 0x157ec6f2, 0x372b74af, 0x692573e4,
  4679. 0xe9a9d848, 0xf3160289, 0x3a62ef1d, 0xa787e238, 0xf3a5f676, 0x74364853,
  4680. 0x20951063, 0x4576698d, 0xb6fad407, 0x592af950, 0x36f73523, 0x4cfb6e87,
  4681. 0x7da4cec0, 0x6c152daa, 0xcb0396a8, 0xc50dfe5d, 0xfcd707ab, 0x0921c42f,
  4682. 0x89dff0bb, 0x5fe2be78, 0x448f4f33, 0x754613c9, 0x2b05d08d, 0x48b9d585,
  4683. 0xdc049441, 0xc8098f9b, 0x7dede786, 0xc39a3373, 0x42410005, 0x6a091751,
  4684. 0x0ef3c8a6, 0x890072d6, 0x28207682, 0xa9a9f7be, 0xbf32679d, 0xd45b5b75,
  4685. 0xb353fd00, 0xcbb0e358, 0x830f220a, 0x1f8fb214, 0xd372cf08, 0xcc3c4a13,
  4686. 0x8cf63166, 0x061c87be, 0x88c98f88, 0x6062e397, 0x47cf8e7a, 0xb6c85283,
  4687. 0x3cc2acfb, 0x3fc06976, 0x4e8f0252, 0x64d8314d, 0xda3870e3, 0x1e665459,
  4688. 0xc10908f0, 0x513021a5, 0x6c5b68b7, 0x822f8aa0, 0x3007cd3e, 0x74719eef,
  4689. 0xdc872681, 0x073340d4, 0x7e432fd9, 0x0c5ec241, 0x8809286c, 0xf592d891,
  4690. 0x08a930f6, 0x957ef305, 0xb7fbffbd, 0xc266e96f, 0x6fe4ac98, 0xb173ecc0,
  4691. 0xbc60b42a, 0x953498da, 0xfba1ae12, 0x2d4bd736, 0x0f25faab, 0xa4f3fceb,
  4692. 0xe2969123, 0x257f0c3d, 0x9348af49, 0x361400bc, 0xe8816f4a, 0x3814f200,
  4693. 0xa3f94043, 0x9c7a54c2, 0xbc704f57, 0xda41e7f9, 0xc25ad33a, 0x54f4a084,
  4694. 0xb17f5505, 0x59357cbe, 0xedbd15c8, 0x7f97c5ab, 0xba5ac7b5, 0xb6f6deaf,
  4695. 0x3a479c3a, 0x5302da25, 0x653d7e6a, 0x54268d49, 0x51a477ea, 0x5017d55b,
  4696. 0xd7d25d88, 0x44136c76, 0x0404a8c8, 0xb8e5a121, 0xb81a928a, 0x60ed5869,
  4697. 0x97c55b96, 0xeaec991b, 0x29935913, 0x01fdb7f1, 0x088e8dfa, 0x9ab6f6f5,
  4698. 0x3b4cbf9f, 0x4a5de3ab, 0xe6051d35, 0xa0e1d855, 0xd36b4cf1, 0xf544edeb,
  4699. 0xb0e93524, 0xbebb8fbd, 0xa2d762cf, 0x49c92f54, 0x38b5f331, 0x7128a454,
  4700. 0x48392905, 0xa65b1db8, 0x851c97bd, 0xd675cf2f
  4701. };
  4702. static uint32 S7[] = {
  4703. 0x85e04019, 0x332bf567, 0x662dbfff, 0xcfc65693, 0x2a8d7f6f, 0xab9bc912,
  4704. 0xde6008a1, 0x2028da1f, 0x0227bce7, 0x4d642916, 0x18fac300, 0x50f18b82,
  4705. 0x2cb2cb11, 0xb232e75c, 0x4b3695f2, 0xb28707de, 0xa05fbcf6, 0xcd4181e9,
  4706. 0xe150210c, 0xe24ef1bd, 0xb168c381, 0xfde4e789, 0x5c79b0d8, 0x1e8bfd43,
  4707. 0x4d495001, 0x38be4341, 0x913cee1d, 0x92a79c3f, 0x089766be, 0xbaeeadf4,
  4708. 0x1286becf, 0xb6eacb19, 0x2660c200, 0x7565bde4, 0x64241f7a, 0x8248dca9,
  4709. 0xc3b3ad66, 0x28136086, 0x0bd8dfa8, 0x356d1cf2, 0x107789be, 0xb3b2e9ce,
  4710. 0x0502aa8f, 0x0bc0351e, 0x166bf52a, 0xeb12ff82, 0xe3486911, 0xd34d7516,
  4711. 0x4e7b3aff, 0x5f43671b, 0x9cf6e037, 0x4981ac83, 0x334266ce, 0x8c9341b7,
  4712. 0xd0d854c0, 0xcb3a6c88, 0x47bc2829, 0x4725ba37, 0xa66ad22b, 0x7ad61f1e,
  4713. 0x0c5cbafa, 0x4437f107, 0xb6e79962, 0x42d2d816, 0x0a961288, 0xe1a5c06e,
  4714. 0x13749e67, 0x72fc081a, 0xb1d139f7, 0xf9583745, 0xcf19df58, 0xbec3f756,
  4715. 0xc06eba30, 0x07211b24, 0x45c28829, 0xc95e317f, 0xbc8ec511, 0x38bc46e9,
  4716. 0xc6e6fa14, 0xbae8584a, 0xad4ebc46, 0x468f508b, 0x7829435f, 0xf124183b,
  4717. 0x821dba9f, 0xaff60ff4, 0xea2c4e6d, 0x16e39264, 0x92544a8b, 0x009b4fc3,
  4718. 0xaba68ced, 0x9ac96f78, 0x06a5b79a, 0xb2856e6e, 0x1aec3ca9, 0xbe838688,
  4719. 0x0e0804e9, 0x55f1be56, 0xe7e5363b, 0xb3a1f25d, 0xf7debb85, 0x61fe033c,
  4720. 0x16746233, 0x3c034c28, 0xda6d0c74, 0x79aac56c, 0x3ce4e1ad, 0x51f0c802,
  4721. 0x98f8f35a, 0x1626a49f, 0xeed82b29, 0x1d382fe3, 0x0c4fb99a, 0xbb325778,
  4722. 0x3ec6d97b, 0x6e77a6a9, 0xcb658b5c, 0xd45230c7, 0x2bd1408b, 0x60c03eb7,
  4723. 0xb9068d78, 0xa33754f4, 0xf430c87d, 0xc8a71302, 0xb96d8c32, 0xebd4e7be,
  4724. 0xbe8b9d2d, 0x7979fb06, 0xe7225308, 0x8b75cf77, 0x11ef8da4, 0xe083c858,
  4725. 0x8d6b786f, 0x5a6317a6, 0xfa5cf7a0, 0x5dda0033, 0xf28ebfb0, 0xf5b9c310,
  4726. 0xa0eac280, 0x08b9767a, 0xa3d9d2b0, 0x79d34217, 0x021a718d, 0x9ac6336a,
  4727. 0x2711fd60, 0x438050e3, 0x069908a8, 0x3d7fedc4, 0x826d2bef, 0x4eeb8476,
  4728. 0x488dcf25, 0x36c9d566, 0x28e74e41, 0xc2610aca, 0x3d49a9cf, 0xbae3b9df,
  4729. 0xb65f8de6, 0x92aeaf64, 0x3ac7d5e6, 0x9ea80509, 0xf22b017d, 0xa4173f70,
  4730. 0xdd1e16c3, 0x15e0d7f9, 0x50b1b887, 0x2b9f4fd5, 0x625aba82, 0x6a017962,
  4731. 0x2ec01b9c, 0x15488aa9, 0xd716e740, 0x40055a2c, 0x93d29a22, 0xe32dbf9a,
  4732. 0x058745b9, 0x3453dc1e, 0xd699296e, 0x496cff6f, 0x1c9f4986, 0xdfe2ed07,
  4733. 0xb87242d1, 0x19de7eae, 0x053e561a, 0x15ad6f8c, 0x66626c1c, 0x7154c24c,
  4734. 0xea082b2a, 0x93eb2939, 0x17dcb0f0, 0x58d4f2ae, 0x9ea294fb, 0x52cf564c,
  4735. 0x9883fe66, 0x2ec40581, 0x763953c3, 0x01d6692e, 0xd3a0c108, 0xa1e7160e,
  4736. 0xe4f2dfa6, 0x693ed285, 0x74904698, 0x4c2b0edd, 0x4f757656, 0x5d393378,
  4737. 0xa132234f, 0x3d321c5d, 0xc3f5e194, 0x4b269301, 0xc79f022f, 0x3c997e7e,
  4738. 0x5e4f9504, 0x3ffafbbd, 0x76f7ad0e, 0x296693f4, 0x3d1fce6f, 0xc61e45be,
  4739. 0xd3b5ab34, 0xf72bf9b7, 0x1b0434c0, 0x4e72b567, 0x5592a33d, 0xb5229301,
  4740. 0xcfd2a87f, 0x60aeb767, 0x1814386b, 0x30bcc33d, 0x38a0c07d, 0xfd1606f2,
  4741. 0xc363519b, 0x589dd390, 0x5479f8e6, 0x1cb8d647, 0x97fd61a9, 0xea7759f4,
  4742. 0x2d57539d, 0x569a58cf, 0xe84e63ad, 0x462e1b78, 0x6580f87e, 0xf3817914,
  4743. 0x91da55f4, 0x40a230f3, 0xd1988f35, 0xb6e318d2, 0x3ffa50bc, 0x3d40f021,
  4744. 0xc3c0bdae, 0x4958c24c, 0x518f36b2, 0x84b1d370, 0x0fedce83, 0x878ddada,
  4745. 0xf2a279c7, 0x94e01be8, 0x90716f4b, 0x954b8aa3
  4746. };
  4747. static uint32 S8[] = {
  4748. 0xe216300d, 0xbbddfffc, 0xa7ebdabd, 0x35648095, 0x7789f8b7, 0xe6c1121b,
  4749. 0x0e241600, 0x052ce8b5, 0x11a9cfb0, 0xe5952f11, 0xece7990a, 0x9386d174,
  4750. 0x2a42931c, 0x76e38111, 0xb12def3a, 0x37ddddfc, 0xde9adeb1, 0x0a0cc32c,
  4751. 0xbe197029, 0x84a00940, 0xbb243a0f, 0xb4d137cf, 0xb44e79f0, 0x049eedfd,
  4752. 0x0b15a15d, 0x480d3168, 0x8bbbde5a, 0x669ded42, 0xc7ece831, 0x3f8f95e7,
  4753. 0x72df191b, 0x7580330d, 0x94074251, 0x5c7dcdfa, 0xabbe6d63, 0xaa402164,
  4754. 0xb301d40a, 0x02e7d1ca, 0x53571dae, 0x7a3182a2, 0x12a8ddec, 0xfdaa335d,
  4755. 0x176f43e8, 0x71fb46d4, 0x38129022, 0xce949ad4, 0xb84769ad, 0x965bd862,
  4756. 0x82f3d055, 0x66fb9767, 0x15b80b4e, 0x1d5b47a0, 0x4cfde06f, 0xc28ec4b8,
  4757. 0x57e8726e, 0x647a78fc, 0x99865d44, 0x608bd593, 0x6c200e03, 0x39dc5ff6,
  4758. 0x5d0b00a3, 0xae63aff2, 0x7e8bd632, 0x70108c0c, 0xbbd35049, 0x2998df04,
  4759. 0x980cf42a, 0x9b6df491, 0x9e7edd53, 0x06918548, 0x58cb7e07, 0x3b74ef2e,
  4760. 0x522fffb1, 0xd24708cc, 0x1c7e27cd, 0xa4eb215b, 0x3cf1d2e2, 0x19b47a38,
  4761. 0x424f7618, 0x35856039, 0x9d17dee7, 0x27eb35e6, 0xc9aff67b, 0x36baf5b8,
  4762. 0x09c467cd, 0xc18910b1, 0xe11dbf7b, 0x06cd1af8, 0x7170c608, 0x2d5e3354,
  4763. 0xd4de495a, 0x64c6d006, 0xbcc0c62c, 0x3dd00db3, 0x708f8f34, 0x77d51b42,
  4764. 0x264f620f, 0x24b8d2bf, 0x15c1b79e, 0x46a52564, 0xf8d7e54e, 0x3e378160,
  4765. 0x7895cda5, 0x859c15a5, 0xe6459788, 0xc37bc75f, 0xdb07ba0c, 0x0676a3ab,
  4766. 0x7f229b1e, 0x31842e7b, 0x24259fd7, 0xf8bef472, 0x835ffcb8, 0x6df4c1f2,
  4767. 0x96f5b195, 0xfd0af0fc, 0xb0fe134c, 0xe2506d3d, 0x4f9b12ea, 0xf215f225,
  4768. 0xa223736f, 0x9fb4c428, 0x25d04979, 0x34c713f8, 0xc4618187, 0xea7a6e98,
  4769. 0x7cd16efc, 0x1436876c, 0xf1544107, 0xbedeee14, 0x56e9af27, 0xa04aa441,
  4770. 0x3cf7c899, 0x92ecbae6, 0xdd67016d, 0x151682eb, 0xa842eedf, 0xfdba60b4,
  4771. 0xf1907b75, 0x20e3030f, 0x24d8c29e, 0xe139673b, 0xefa63fb8, 0x71873054,
  4772. 0xb6f2cf3b, 0x9f326442, 0xcb15a4cc, 0xb01a4504, 0xf1e47d8d, 0x844a1be5,
  4773. 0xbae7dfdc, 0x42cbda70, 0xcd7dae0a, 0x57e85b7a, 0xd53f5af6, 0x20cf4d8c,
  4774. 0xcea4d428, 0x79d130a4, 0x3486ebfb, 0x33d3cddc, 0x77853b53, 0x37effcb5,
  4775. 0xc5068778, 0xe580b3e6, 0x4e68b8f4, 0xc5c8b37e, 0x0d809ea2, 0x398feb7c,
  4776. 0x132a4f94, 0x43b7950e, 0x2fee7d1c, 0x223613bd, 0xdd06caa2, 0x37df932b,
  4777. 0xc4248289, 0xacf3ebc3, 0x5715f6b7, 0xef3478dd, 0xf267616f, 0xc148cbe4,
  4778. 0x9052815e, 0x5e410fab, 0xb48a2465, 0x2eda7fa4, 0xe87b40e4, 0xe98ea084,
  4779. 0x5889e9e1, 0xefd390fc, 0xdd07d35b, 0xdb485694, 0x38d7e5b2, 0x57720101,
  4780. 0x730edebc, 0x5b643113, 0x94917e4f, 0x503c2fba, 0x646f1282, 0x7523d24a,
  4781. 0xe0779695, 0xf9c17a8f, 0x7a5b2121, 0xd187b896, 0x29263a4d, 0xba510cdf,
  4782. 0x81f47c9f, 0xad1163ed, 0xea7b5965, 0x1a00726e, 0x11403092, 0x00da6d77,
  4783. 0x4a0cdd61, 0xad1f4603, 0x605bdfb0, 0x9eedc364, 0x22ebe6a8, 0xcee7d28a,
  4784. 0xa0e736a0, 0x5564a6b9, 0x10853209, 0xc7eb8f37, 0x2de705ca, 0x8951570f,
  4785. 0xdf09822b, 0xbd691a6c, 0xaa12e4f2, 0x87451c0f, 0xe0f6a27a, 0x3ada4819,
  4786. 0x4cf1764f, 0x0d771c2b, 0x67cdb156, 0x350d8384, 0x5938fa0f, 0x42399ef3,
  4787. 0x36997b07, 0x0e84093d, 0x4aa93e61, 0x8360d87b, 0x1fa98b0c, 0x1149382c,
  4788. 0xe97625a5, 0x0614d1b7, 0x0e25244b, 0x0c768347, 0x589e8d82, 0x0d2059d1,
  4789. 0xa466bb1e, 0xf8da0a82, 0x04f19130, 0xba6e4ec0, 0x99265164, 0x1ee7230d,
  4790. 0x50b2ad80, 0xeaee6801, 0x8db2a283, 0xea8bf59e
  4791. };
  4792. /* Initialize a key schedule from a 128-bit key */
  4793. static void
  4794. cast_key_sched(sched, key)
  4795. CastKeySched * sched;
  4796. uint8p key;
  4797. {
  4798. uint8p x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xA, xB, xC, xD, xE, xF;
  4799. uint8p z0, z1, z2, z3, z4, z5, z6, z7, z8, z9, zA, zB, zC, zD, zE, zF;
  4800. uint32 X03, X47, X8B, XCF, Z03, Z47, Z8B, ZCF;
  4801. #ifdef LITTLE_ENDIAN
  4802. x3 = (uint8p) &X03; x2 = x3 + 1; x1 = x2 + 1; x0 = x1 + 1;
  4803. x7 = (uint8p) &X47; x6 = x7 + 1; x5 = x6 + 1; x4 = x5 + 1;
  4804. xB = (uint8p) &X8B; xA = xB + 1; x9 = xA + 1; x8 = x9 + 1;
  4805. xF = (uint8p) &XCF; xE = xF + 1; xD = xE + 1; xC = xD + 1;
  4806. z3 = (uint8p) &Z03; z2 = z3 + 1; z1 = z2 + 1; z0 = z1 + 1;
  4807. z7 = (uint8p) &Z47; z6 = z7 + 1; z5 = z6 + 1; z4 = z5 + 1;
  4808. zB = (uint8p) &Z8B; zA = zB + 1; z9 = zA + 1; z8 = z9 + 1;
  4809. zF = (uint8p) &ZCF; zE = zF + 1; zD = zE + 1; zC = zD + 1;
  4810. #else
  4811. x0 = (uint8p) &X03; x1 = x0 + 1; x2 = x1 + 1; x3 = x2 + 1;
  4812. x4 = (uint8p) &X47; x5 = x4 + 1; x6 = x5 + 1; x7 = x6 + 1;
  4813. x8 = (uint8p) &X8B; x9 = x8 + 1; xA = x9 + 1; xB = xA + 1;
  4814. xC = (uint8p) &XCF; xD = xC + 1; xE = xD + 1; xF = xE + 1;
  4815. z0 = (uint8p) &Z03; z1 = z0 + 1; z2 = z1 + 1; z3 = z2 + 1;
  4816. z4 = (uint8p) &Z47; z5 = z4 + 1; z6 = z5 + 1; z7 = z6 + 1;
  4817. z8 = (uint8p) &Z8B; z9 = z8 + 1; zA = z9 + 1; zB = zA + 1;
  4818. zC = (uint8p) &ZCF; zD = zC + 1; zE = zD + 1; zF = zE + 1;
  4819. #endif
  4820. #ifdef LITTLE_ENDIAN
  4821. *x0 = key[0];
  4822. *x1 = key[1];
  4823. *x2 = key[2];
  4824. *x3 = key[3];
  4825. *x4 = key[4];
  4826. *x5 = key[5];
  4827. *x6 = key[6];
  4828. *x7 = key[7];
  4829. *x8 = key[8];
  4830. *x9 = key[9];
  4831. *xA = key[10];
  4832. *xB = key[11];
  4833. *xC = key[12];
  4834. *xD = key[13];
  4835. *xE = key[14];
  4836. *xF = key[15];
  4837. #else
  4838. X03 = *(uint32p) key;
  4839. X47 = *(uint32p) (key + 4);
  4840. X8B = *(uint32p) (key + 8);
  4841. XCF = *(uint32p) (key + 12);
  4842. #endif
  4843. /* First half of key schedule */
  4844. Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  4845. Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  4846. Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  4847. ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  4848. sched->K[0].Km = S5[*z8] ^ S6[*z9] ^ S7[*z7] ^ S8[*z6] ^ S5[*z2];
  4849. sched->K[1].Km = S5[*zA] ^ S6[*zB] ^ S7[*z5] ^ S8[*z4] ^ S6[*z6];
  4850. sched->K[2].Km = S5[*zC] ^ S6[*zD] ^ S7[*z3] ^ S8[*z2] ^ S7[*z9];
  4851. sched->K[3].Km = S5[*zE] ^ S6[*zF] ^ S7[*z1] ^ S8[*z0] ^ S8[*zC];
  4852. X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  4853. X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  4854. X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  4855. XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  4856. sched->K[4].Km = S5[*x3] ^ S6[*x2] ^ S7[*xC] ^ S8[*xD] ^ S5[*x8];
  4857. sched->K[5].Km = S5[*x1] ^ S6[*x0] ^ S7[*xE] ^ S8[*xF] ^ S6[*xD];
  4858. sched->K[6].Km = S5[*x7] ^ S6[*x6] ^ S7[*x8] ^ S8[*x9] ^ S7[*x3];
  4859. sched->K[7].Km = S5[*x5] ^ S6[*x4] ^ S7[*xA] ^ S8[*xB] ^ S8[*x7];
  4860. Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  4861. Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  4862. Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  4863. ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  4864. sched->K[8].Km = S5[*z3] ^ S6[*z2] ^ S7[*zC] ^ S8[*zD] ^ S5[*z9];
  4865. sched->K[9].Km = S5[*z1] ^ S6[*z0] ^ S7[*zE] ^ S8[*zF] ^ S6[*zC];
  4866. sched->K[10].Km = S5[*z7] ^ S6[*z6] ^ S7[*z8] ^ S8[*z9] ^ S7[*z2];
  4867. sched->K[11].Km = S5[*z5] ^ S6[*z4] ^ S7[*zA] ^ S8[*zB] ^ S8[*z6];
  4868. X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  4869. X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  4870. X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  4871. XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  4872. sched->K[12].Km = S5[*x8] ^ S6[*x9] ^ S7[*x7] ^ S8[*x6] ^ S5[*x3];
  4873. sched->K[13].Km = S5[*xA] ^ S6[*xB] ^ S7[*x5] ^ S8[*x4] ^ S6[*x7];
  4874. sched->K[14].Km = S5[*xC] ^ S6[*xD] ^ S7[*x3] ^ S8[*x2] ^ S7[*x8];
  4875. sched->K[15].Km = S5[*xE] ^ S6[*xF] ^ S7[*x1] ^ S8[*x0] ^ S8[*xD];
  4876. /* Second half of key schedule - just like first half */
  4877. Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  4878. Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  4879. Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  4880. ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  4881. sched->K[0].Kr = (S5[*z8] ^ S6[*z9] ^ S7[*z7] ^ S8[*z6] ^ S5[*z2]) & 0x1f;
  4882. sched->K[1].Kr = (S5[*zA] ^ S6[*zB] ^ S7[*z5] ^ S8[*z4] ^ S6[*z6]) & 0x1f;
  4883. sched->K[2].Kr = (S5[*zC] ^ S6[*zD] ^ S7[*z3] ^ S8[*z2] ^ S7[*z9]) & 0x1f;
  4884. sched->K[3].Kr = (S5[*zE] ^ S6[*zF] ^ S7[*z1] ^ S8[*z0] ^ S8[*zC]) & 0x1f;
  4885. X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  4886. X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  4887. X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  4888. XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  4889. sched->K[4].Kr = (S5[*x3] ^ S6[*x2] ^ S7[*xC] ^ S8[*xD] ^ S5[*x8]) & 0x1f;
  4890. sched->K[5].Kr = (S5[*x1] ^ S6[*x0] ^ S7[*xE] ^ S8[*xF] ^ S6[*xD]) & 0x1f;
  4891. sched->K[6].Kr = (S5[*x7] ^ S6[*x6] ^ S7[*x8] ^ S8[*x9] ^ S7[*x3]) & 0x1f;
  4892. sched->K[7].Kr = (S5[*x5] ^ S6[*x4] ^ S7[*xA] ^ S8[*xB] ^ S8[*x7]) & 0x1f;
  4893. Z03 = X03 ^ S5[*xD] ^ S6[*xF] ^ S7[*xC] ^ S8[*xE] ^ S7[*x8];
  4894. Z47 = X8B ^ S5[*z0] ^ S6[*z2] ^ S7[*z1] ^ S8[*z3] ^ S8[*xA];
  4895. Z8B = XCF ^ S5[*z7] ^ S6[*z6] ^ S7[*z5] ^ S8[*z4] ^ S5[*x9];
  4896. ZCF = X47 ^ S5[*zA] ^ S6[*z9] ^ S7[*zB] ^ S8[*z8] ^ S6[*xB];
  4897. sched->K[8].Kr = (S5[*z3] ^ S6[*z2] ^ S7[*zC] ^ S8[*zD] ^ S5[*z9]) & 0x1f;
  4898. sched->K[9].Kr = (S5[*z1] ^ S6[*z0] ^ S7[*zE] ^ S8[*zF] ^ S6[*zC]) & 0x1f;
  4899. sched->K[10].Kr = (S5[*z7] ^ S6[*z6] ^ S7[*z8] ^ S8[*z9] ^ S7[*z2]) & 0x1f;
  4900. sched->K[11].Kr = (S5[*z5] ^ S6[*z4] ^ S7[*zA] ^ S8[*zB] ^ S8[*z6]) & 0x1f;
  4901. X03 = Z8B ^ S5[*z5] ^ S6[*z7] ^ S7[*z4] ^ S8[*z6] ^ S7[*z0];
  4902. X47 = Z03 ^ S5[*x0] ^ S6[*x2] ^ S7[*x1] ^ S8[*x3] ^ S8[*z2];
  4903. X8B = Z47 ^ S5[*x7] ^ S6[*x6] ^ S7[*x5] ^ S8[*x4] ^ S5[*z1];
  4904. XCF = ZCF ^ S5[*xA] ^ S6[*x9] ^ S7[*xB] ^ S8[*x8] ^ S6[*z3];
  4905. sched->K[12].Kr = (S5[*x8] ^ S6[*x9] ^ S7[*x7] ^ S8[*x6] ^ S5[*x3]) & 0x1f;
  4906. sched->K[13].Kr = (S5[*xA] ^ S6[*xB] ^ S7[*x5] ^ S8[*x4] ^ S6[*x7]) & 0x1f;
  4907. sched->K[14].Kr = (S5[*xC] ^ S6[*xD] ^ S7[*x3] ^ S8[*x2] ^ S7[*x8]) & 0x1f;
  4908. sched->K[15].Kr = (S5[*xE] ^ S6[*xF] ^ S7[*x1] ^ S8[*x0] ^ S8[*xD]) & 0x1f;
  4909. }
  4910. /* Initialize with a full-strength 128-bit key */
  4911. #ifndef CAST_EXPORT_ENCRYPTION
  4912. void
  4913. ck_cast128_key_sched(sched, key)
  4914. CastKeySched * sched;
  4915. uint8 * key;
  4916. {
  4917. sched->ksize = 16;
  4918. cast_key_sched(sched, key);
  4919. }
  4920. #endif
  4921. /* Handle reduced-keysize variants */
  4922. static void
  4923. cast5_key_sched(sched, key, sz)
  4924. CastKeySched * sched;
  4925. uint8 * key;
  4926. int sz;
  4927. {
  4928. uint8 buf[16];
  4929. sched->ksize = sz;
  4930. memset(buf, 0, sizeof(buf));
  4931. memcpy(buf, key, sz);
  4932. cast_key_sched(sched, buf);
  4933. }
  4934. /* 40, 64, and 80-bit keys - all use 12 rounds */
  4935. void
  4936. ck_cast5_40_key_sched(sched, key)
  4937. CastKeySched * sched;
  4938. uint8 * key;
  4939. {
  4940. cast5_key_sched(sched, key, 5);
  4941. }
  4942. #ifndef CAST_EXPORT_ENCRYPTION
  4943. void
  4944. ck_cast5_64_key_sched(sched, key)
  4945. CastKeySched * sched;
  4946. uint8 * key;
  4947. {
  4948. cast5_key_sched(sched, key, 8);
  4949. }
  4950. void
  4951. ck_cast5_80_key_sched(sched, key)
  4952. CastKeySched * sched;
  4953. uint8 * key;
  4954. {
  4955. cast5_key_sched(sched, key, 10);
  4956. }
  4957. #endif /* CAST_EXPORT_ENCRYPTION */
  4958. #endif /* CK_CAST */
  4959. #ifdef CRYPT_DLL
  4960. static char *
  4961. ck_crypt_dll_version()
  4962. {
  4963. return(ckcrpv);
  4964. }
  4965. int
  4966. crypt_dll_init( struct _crypt_dll_init * init )
  4967. {
  4968. #ifdef LIBDES
  4969. extern int des_check_key;
  4970. extern void libdes_dll_init(struct _crypt_dll_init *);
  4971. des_check_key = 1;
  4972. #endif /* LIBDES */
  4973. if ( init->version >= 1 ) {
  4974. p_ttol = init->p_ttol;
  4975. p_dodebug = init->p_dodebug;
  4976. p_dohexdump = init->p_dohexdump;
  4977. p_tn_debug = init->p_tn_debug;
  4978. p_vscrnprintf = init->p_vscrnprintf;
  4979. if ( init->version == 1 )
  4980. return(1);
  4981. }
  4982. if ( init->version >= 2 ) {
  4983. /* This is a k5_context but we don't want to include krb5.h */
  4984. p_k5_context = (void *) init->p_k5_context;
  4985. if ( init->version == 2 )
  4986. return(1);
  4987. }
  4988. if ( init->version >= 3 ) {
  4989. init->p_install_funcs("encrypt_parse",encrypt_parse);
  4990. init->p_install_funcs("encrypt_init",encrypt_init);
  4991. init->p_install_funcs("encrypt_session_key",encrypt_session_key);
  4992. init->p_install_funcs("encrypt_send_request_start",
  4993. encrypt_send_request_start
  4994. );
  4995. init->p_install_funcs("encrypt_request_start",encrypt_request_start);
  4996. init->p_install_funcs("encrypt_send_request_end",
  4997. encrypt_send_request_end
  4998. );
  4999. init->p_install_funcs("encrypt_request_end",encrypt_request_end);
  5000. init->p_install_funcs("encrypt_send_end",encrypt_send_end);
  5001. init->p_install_funcs("encrypt_send_support",encrypt_send_support);
  5002. init->p_install_funcs("encrypt_is_encrypting",encrypt_is_encrypting);
  5003. init->p_install_funcs("encrypt_is_decrypting",encrypt_is_decrypting);
  5004. init->p_install_funcs("get_crypt_table",get_crypt_table);
  5005. init->p_install_funcs("des_is_weak_key",ck_des_is_weak_key);
  5006. libdes_dll_init(init);
  5007. if (init->version == 3)
  5008. return(1);
  5009. }
  5010. if ( init->version >= 4 ) {
  5011. init->p_install_funcs("crypt_dll_version",ck_crypt_dll_version);
  5012. if (init->version == 4)
  5013. return(1);
  5014. }
  5015. if ( init->version >= 5 ) {
  5016. p_reqtelmutex = init->p_reqtelmutex;
  5017. p_reltelmutex = init->p_reltelmutex;
  5018. if (init->version == 5)
  5019. return(1);
  5020. }
  5021. if ( init->version >= 6 ) {
  5022. init->p_install_funcs("encrypt_dont_support",encrypt_dont_support);
  5023. if ( init->version == 6 )
  5024. return(1);
  5025. /* when adding new versions; migrate the next two lines */
  5026. init->version = 6;
  5027. return(1);
  5028. }
  5029. return(0);
  5030. }
  5031. #undef malloc
  5032. #undef realloc
  5033. #undef free
  5034. #undef strdup
  5035. static void
  5036. fatal(char *msg) {
  5037. if (!msg) msg = "";
  5038. printf(msg);
  5039. exit(1); /* Exit indicating failure */
  5040. }
  5041. void *
  5042. kmalloc(size_t size)
  5043. {
  5044. void *ptr;
  5045. if (size == 0) {
  5046. fatal("kmalloc: zero size");
  5047. }
  5048. ptr = malloc(size);
  5049. if (ptr == NULL) {
  5050. fatal("kmalloc: out of memory");
  5051. }
  5052. return ptr;
  5053. }
  5054. void *
  5055. krealloc(void *ptr, size_t new_size)
  5056. {
  5057. void *new_ptr;
  5058. if (new_size == 0) {
  5059. fatal("krealloc: zero size");
  5060. }
  5061. if (ptr == NULL)
  5062. new_ptr = malloc(new_size);
  5063. else
  5064. new_ptr = realloc(ptr, new_size);
  5065. if (new_ptr == NULL) {
  5066. fatal("krealloc: out of memory");
  5067. }
  5068. return new_ptr;
  5069. }
  5070. void
  5071. kfree(void *ptr)
  5072. {
  5073. if (ptr == NULL) {
  5074. printf("kfree: NULL pointer given as argument");
  5075. return;
  5076. }
  5077. free(ptr);
  5078. }
  5079. char *
  5080. kstrdup(const char *str)
  5081. {
  5082. size_t len;
  5083. char *cp;
  5084. if (str == NULL) {
  5085. fatal("kstrdup: NULL pointer given as argument");
  5086. }
  5087. len = strlen(str) + 1;
  5088. cp = kmalloc(len);
  5089. if (cp)
  5090. memcpy(cp, str, len);
  5091. return cp;
  5092. }
  5093. #endif /* CRYPT_DLL */
  5094. #endif /* CK_ENCRYPTION */