md.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /* md.c - message digest dispatcher
  2. * Copyright (C) 1998, 1999, 2002, 2003, 2006,
  3. * 2008 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt.
  6. *
  7. * Libgcrypt is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser general Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Libgcrypt is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "g10lib.h"
  26. #include "cipher.h"
  27. #include "ath.h"
  28. #include "rmd.h"
  29. /* A dummy extraspec so that we do not need to tests the extraspec
  30. field from the module specification against NULL and instead
  31. directly test the respective fields of extraspecs. */
  32. static md_extra_spec_t dummy_extra_spec;
  33. /* This is the list of the digest implementations included in
  34. libgcrypt. */
  35. static struct digest_table_entry
  36. {
  37. gcry_md_spec_t *digest;
  38. md_extra_spec_t *extraspec;
  39. unsigned int algorithm;
  40. int fips_allowed;
  41. } digest_table[] =
  42. {
  43. #if USE_CRC
  44. /* We allow the CRC algorithms even in FIPS mode because they are
  45. actually no cryptographic primitives. */
  46. { &_gcry_digest_spec_crc32,
  47. &dummy_extra_spec, GCRY_MD_CRC32, 1 },
  48. { &_gcry_digest_spec_crc32_rfc1510,
  49. &dummy_extra_spec, GCRY_MD_CRC32_RFC1510, 1 },
  50. { &_gcry_digest_spec_crc24_rfc2440,
  51. &dummy_extra_spec, GCRY_MD_CRC24_RFC2440, 1 },
  52. #endif
  53. #if USE_MD4
  54. { &_gcry_digest_spec_md4,
  55. &dummy_extra_spec, GCRY_MD_MD4 },
  56. #endif
  57. #if USE_MD5
  58. { &_gcry_digest_spec_md5,
  59. &dummy_extra_spec, GCRY_MD_MD5, 1 },
  60. #endif
  61. #if USE_RMD160
  62. { &_gcry_digest_spec_rmd160,
  63. &dummy_extra_spec, GCRY_MD_RMD160 },
  64. #endif
  65. #if USE_SHA1
  66. { &_gcry_digest_spec_sha1,
  67. &_gcry_digest_extraspec_sha1, GCRY_MD_SHA1, 1 },
  68. #endif
  69. #if USE_SHA256
  70. { &_gcry_digest_spec_sha256,
  71. &_gcry_digest_extraspec_sha256, GCRY_MD_SHA256, 1 },
  72. { &_gcry_digest_spec_sha224,
  73. &_gcry_digest_extraspec_sha224, GCRY_MD_SHA224, 1 },
  74. #endif
  75. #if USE_SHA512
  76. { &_gcry_digest_spec_sha512,
  77. &_gcry_digest_extraspec_sha512, GCRY_MD_SHA512, 1 },
  78. { &_gcry_digest_spec_sha384,
  79. &_gcry_digest_extraspec_sha384, GCRY_MD_SHA384, 1 },
  80. #endif
  81. #if USE_TIGER
  82. { &_gcry_digest_spec_tiger,
  83. &dummy_extra_spec, GCRY_MD_TIGER },
  84. { &_gcry_digest_spec_tiger1,
  85. &dummy_extra_spec, GCRY_MD_TIGER1 },
  86. { &_gcry_digest_spec_tiger2,
  87. &dummy_extra_spec, GCRY_MD_TIGER2 },
  88. #endif
  89. #if USE_WHIRLPOOL
  90. { &_gcry_digest_spec_whirlpool,
  91. &dummy_extra_spec, GCRY_MD_WHIRLPOOL },
  92. #endif
  93. { NULL },
  94. };
  95. /* List of registered digests. */
  96. static gcry_module_t digests_registered;
  97. /* This is the lock protecting DIGESTS_REGISTERED. */
  98. static ath_mutex_t digests_registered_lock = ATH_MUTEX_INITIALIZER;
  99. /* Flag to check whether the default ciphers have already been
  100. registered. */
  101. static int default_digests_registered;
  102. typedef struct gcry_md_list
  103. {
  104. gcry_md_spec_t *digest;
  105. gcry_module_t module;
  106. struct gcry_md_list *next;
  107. size_t actual_struct_size; /* Allocated size of this structure. */
  108. PROPERLY_ALIGNED_TYPE context;
  109. } GcryDigestEntry;
  110. /* this structure is put right after the gcry_md_hd_t buffer, so that
  111. * only one memory block is needed. */
  112. struct gcry_md_context
  113. {
  114. int magic;
  115. size_t actual_handle_size; /* Allocated size of this handle. */
  116. int secure;
  117. FILE *debug;
  118. int finalized;
  119. GcryDigestEntry *list;
  120. byte *macpads;
  121. int macpads_Bsize; /* Blocksize as used for the HMAC pads. */
  122. };
  123. #define CTX_MAGIC_NORMAL 0x11071961
  124. #define CTX_MAGIC_SECURE 0x16917011
  125. /* Convenient macro for registering the default digests. */
  126. #define REGISTER_DEFAULT_DIGESTS \
  127. do \
  128. { \
  129. ath_mutex_lock (&digests_registered_lock); \
  130. if (! default_digests_registered) \
  131. { \
  132. md_register_default (); \
  133. default_digests_registered = 1; \
  134. } \
  135. ath_mutex_unlock (&digests_registered_lock); \
  136. } \
  137. while (0)
  138. static const char * digest_algo_to_string( int algo );
  139. static gcry_err_code_t check_digest_algo (int algo);
  140. static gcry_err_code_t md_open (gcry_md_hd_t *h, int algo,
  141. int secure, int hmac);
  142. static gcry_err_code_t md_enable (gcry_md_hd_t hd, int algo);
  143. static gcry_err_code_t md_copy (gcry_md_hd_t a, gcry_md_hd_t *b);
  144. static void md_close (gcry_md_hd_t a);
  145. static void md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen);
  146. static void md_final(gcry_md_hd_t a);
  147. static byte *md_read( gcry_md_hd_t a, int algo );
  148. static int md_get_algo( gcry_md_hd_t a );
  149. static int md_digest_length( int algo );
  150. static const byte *md_asn_oid( int algo, size_t *asnlen, size_t *mdlen );
  151. static void md_start_debug ( gcry_md_hd_t a, const char *suffix );
  152. static void md_stop_debug ( gcry_md_hd_t a );
  153. /* Internal function. Register all the ciphers included in
  154. CIPHER_TABLE. Returns zero on success or an error code. */
  155. static void
  156. md_register_default (void)
  157. {
  158. gcry_err_code_t err = 0;
  159. int i;
  160. for (i = 0; !err && digest_table[i].digest; i++)
  161. {
  162. if ( fips_mode ())
  163. {
  164. if (!digest_table[i].fips_allowed)
  165. continue;
  166. if (digest_table[i].algorithm == GCRY_MD_MD5
  167. && _gcry_enforced_fips_mode () )
  168. continue; /* Do not register in enforced fips mode. */
  169. }
  170. err = _gcry_module_add (&digests_registered,
  171. digest_table[i].algorithm,
  172. (void *) digest_table[i].digest,
  173. (void *) digest_table[i].extraspec,
  174. NULL);
  175. }
  176. if (err)
  177. BUG ();
  178. }
  179. /* Internal callback function. */
  180. static int
  181. gcry_md_lookup_func_name (void *spec, void *data)
  182. {
  183. gcry_md_spec_t *digest = (gcry_md_spec_t *) spec;
  184. char *name = (char *) data;
  185. return (! stricmp (digest->name, name));
  186. }
  187. /* Internal callback function. Used via _gcry_module_lookup. */
  188. static int
  189. gcry_md_lookup_func_oid (void *spec, void *data)
  190. {
  191. gcry_md_spec_t *digest = (gcry_md_spec_t *) spec;
  192. char *oid = (char *) data;
  193. gcry_md_oid_spec_t *oid_specs = digest->oids;
  194. int ret = 0, i;
  195. if (oid_specs)
  196. {
  197. for (i = 0; oid_specs[i].oidstring && (! ret); i++)
  198. if (! stricmp (oid, oid_specs[i].oidstring))
  199. ret = 1;
  200. }
  201. return ret;
  202. }
  203. /* Internal function. Lookup a digest entry by it's name. */
  204. static gcry_module_t
  205. gcry_md_lookup_name (const char *name)
  206. {
  207. gcry_module_t digest;
  208. digest = _gcry_module_lookup (digests_registered, (void *) name,
  209. gcry_md_lookup_func_name);
  210. return digest;
  211. }
  212. /* Internal function. Lookup a cipher entry by it's oid. */
  213. static gcry_module_t
  214. gcry_md_lookup_oid (const char *oid)
  215. {
  216. gcry_module_t digest;
  217. digest = _gcry_module_lookup (digests_registered, (void *) oid,
  218. gcry_md_lookup_func_oid);
  219. return digest;
  220. }
  221. /* Register a new digest module whose specification can be found in
  222. DIGEST. On success, a new algorithm ID is stored in ALGORITHM_ID
  223. and a pointer representhing this module is stored in MODULE. */
  224. gcry_error_t
  225. _gcry_md_register (gcry_md_spec_t *digest,
  226. md_extra_spec_t *extraspec,
  227. unsigned int *algorithm_id,
  228. gcry_module_t *module)
  229. {
  230. gcry_err_code_t err = 0;
  231. gcry_module_t mod;
  232. /* We do not support module loading in fips mode. */
  233. if (fips_mode ())
  234. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  235. ath_mutex_lock (&digests_registered_lock);
  236. err = _gcry_module_add (&digests_registered, 0,
  237. (void *) digest,
  238. (void *)(extraspec? extraspec : &dummy_extra_spec),
  239. &mod);
  240. ath_mutex_unlock (&digests_registered_lock);
  241. if (! err)
  242. {
  243. *module = mod;
  244. *algorithm_id = mod->mod_id;
  245. }
  246. return gcry_error (err);
  247. }
  248. /* Unregister the digest identified by ID, which must have been
  249. registered with gcry_digest_register. */
  250. void
  251. gcry_md_unregister (gcry_module_t module)
  252. {
  253. ath_mutex_lock (&digests_registered_lock);
  254. _gcry_module_release (module);
  255. ath_mutex_unlock (&digests_registered_lock);
  256. }
  257. static int
  258. search_oid (const char *oid, int *algorithm, gcry_md_oid_spec_t *oid_spec)
  259. {
  260. gcry_module_t module;
  261. int ret = 0;
  262. if (oid && ((! strncmp (oid, "oid.", 4))
  263. || (! strncmp (oid, "OID.", 4))))
  264. oid += 4;
  265. module = gcry_md_lookup_oid (oid);
  266. if (module)
  267. {
  268. gcry_md_spec_t *digest = module->spec;
  269. int i;
  270. for (i = 0; digest->oids[i].oidstring && !ret; i++)
  271. if (! stricmp (oid, digest->oids[i].oidstring))
  272. {
  273. if (algorithm)
  274. *algorithm = module->mod_id;
  275. if (oid_spec)
  276. *oid_spec = digest->oids[i];
  277. ret = 1;
  278. }
  279. _gcry_module_release (module);
  280. }
  281. return ret;
  282. }
  283. /****************
  284. * Map a string to the digest algo
  285. */
  286. int
  287. gcry_md_map_name (const char *string)
  288. {
  289. gcry_module_t digest;
  290. int ret, algorithm = 0;
  291. if (! string)
  292. return 0;
  293. REGISTER_DEFAULT_DIGESTS;
  294. /* If the string starts with a digit (optionally prefixed with
  295. either "OID." or "oid."), we first look into our table of ASN.1
  296. object identifiers to figure out the algorithm */
  297. ath_mutex_lock (&digests_registered_lock);
  298. ret = search_oid (string, &algorithm, NULL);
  299. if (! ret)
  300. {
  301. /* Not found, search a matching digest name. */
  302. digest = gcry_md_lookup_name (string);
  303. if (digest)
  304. {
  305. algorithm = digest->mod_id;
  306. _gcry_module_release (digest);
  307. }
  308. }
  309. ath_mutex_unlock (&digests_registered_lock);
  310. return algorithm;
  311. }
  312. /****************
  313. * Map a digest algo to a string
  314. */
  315. static const char *
  316. digest_algo_to_string (int algorithm)
  317. {
  318. const char *name = NULL;
  319. gcry_module_t digest;
  320. REGISTER_DEFAULT_DIGESTS;
  321. ath_mutex_lock (&digests_registered_lock);
  322. digest = _gcry_module_lookup_id (digests_registered, algorithm);
  323. if (digest)
  324. {
  325. name = ((gcry_md_spec_t *) digest->spec)->name;
  326. _gcry_module_release (digest);
  327. }
  328. ath_mutex_unlock (&digests_registered_lock);
  329. return name;
  330. }
  331. /****************
  332. * This function simply returns the name of the algorithm or some constant
  333. * string when there is no algo. It will never return NULL.
  334. * Use the macro gcry_md_test_algo() to check whether the algorithm
  335. * is valid.
  336. */
  337. const char *
  338. gcry_md_algo_name (int algorithm)
  339. {
  340. const char *s = digest_algo_to_string (algorithm);
  341. return s ? s : "?";
  342. }
  343. static gcry_err_code_t
  344. check_digest_algo (int algorithm)
  345. {
  346. gcry_err_code_t rc = 0;
  347. gcry_module_t digest;
  348. REGISTER_DEFAULT_DIGESTS;
  349. ath_mutex_lock (&digests_registered_lock);
  350. digest = _gcry_module_lookup_id (digests_registered, algorithm);
  351. if (digest)
  352. _gcry_module_release (digest);
  353. else
  354. rc = GPG_ERR_DIGEST_ALGO;
  355. ath_mutex_unlock (&digests_registered_lock);
  356. return rc;
  357. }
  358. /****************
  359. * Open a message digest handle for use with algorithm ALGO.
  360. * More algorithms may be added by md_enable(). The initial algorithm
  361. * may be 0.
  362. */
  363. static gcry_err_code_t
  364. md_open (gcry_md_hd_t *h, int algo, int secure, int hmac)
  365. {
  366. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  367. int bufsize = secure ? 512 : 1024;
  368. struct gcry_md_context *ctx;
  369. gcry_md_hd_t hd;
  370. size_t n;
  371. /* Allocate a memory area to hold the caller visible buffer with it's
  372. * control information and the data required by this module. Set the
  373. * context pointer at the beginning to this area.
  374. * We have to use this strange scheme because we want to hide the
  375. * internal data but have a variable sized buffer.
  376. *
  377. * +---+------+---........------+-------------+
  378. * !ctx! bctl ! buffer ! private !
  379. * +---+------+---........------+-------------+
  380. * ! ^
  381. * !---------------------------!
  382. *
  383. * We have to make sure that private is well aligned.
  384. */
  385. n = sizeof (struct gcry_md_handle) + bufsize;
  386. n = ((n + sizeof (PROPERLY_ALIGNED_TYPE) - 1)
  387. / sizeof (PROPERLY_ALIGNED_TYPE)) * sizeof (PROPERLY_ALIGNED_TYPE);
  388. /* Allocate and set the Context pointer to the private data */
  389. if (secure)
  390. hd = gcry_malloc_secure (n + sizeof (struct gcry_md_context));
  391. else
  392. hd = gcry_malloc (n + sizeof (struct gcry_md_context));
  393. if (! hd)
  394. err = gpg_err_code_from_errno (errno);
  395. if (! err)
  396. {
  397. hd->ctx = ctx = (struct gcry_md_context *) ((char *) hd + n);
  398. /* Setup the globally visible data (bctl in the diagram).*/
  399. hd->bufsize = n - sizeof (struct gcry_md_handle) + 1;
  400. hd->bufpos = 0;
  401. /* Initialize the private data. */
  402. memset (hd->ctx, 0, sizeof *hd->ctx);
  403. ctx->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
  404. ctx->actual_handle_size = n + sizeof (struct gcry_md_context);
  405. ctx->secure = secure;
  406. if (hmac)
  407. {
  408. switch (algo)
  409. {
  410. case GCRY_MD_SHA384:
  411. case GCRY_MD_SHA512:
  412. ctx->macpads_Bsize = 128;
  413. break;
  414. default:
  415. ctx->macpads_Bsize = 64;
  416. break;
  417. }
  418. ctx->macpads = gcry_malloc_secure (2*(ctx->macpads_Bsize));
  419. if (!ctx->macpads)
  420. {
  421. err = gpg_err_code_from_errno (errno);
  422. md_close (hd);
  423. }
  424. }
  425. }
  426. if (! err)
  427. {
  428. /* Hmmm, should we really do that? - yes [-wk] */
  429. _gcry_fast_random_poll ();
  430. if (algo)
  431. {
  432. err = md_enable (hd, algo);
  433. if (err)
  434. md_close (hd);
  435. }
  436. }
  437. if (! err)
  438. *h = hd;
  439. return err;
  440. }
  441. /* Create a message digest object for algorithm ALGO. FLAGS may be
  442. given as an bitwise OR of the gcry_md_flags values. ALGO may be
  443. given as 0 if the algorithms to be used are later set using
  444. gcry_md_enable. H is guaranteed to be a valid handle or NULL on
  445. error. */
  446. gcry_error_t
  447. gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags)
  448. {
  449. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  450. gcry_md_hd_t hd;
  451. if ((flags & ~(GCRY_MD_FLAG_SECURE | GCRY_MD_FLAG_HMAC)))
  452. err = GPG_ERR_INV_ARG;
  453. else
  454. {
  455. err = md_open (&hd, algo, (flags & GCRY_MD_FLAG_SECURE),
  456. (flags & GCRY_MD_FLAG_HMAC));
  457. }
  458. *h = err? NULL : hd;
  459. return gcry_error (err);
  460. }
  461. static gcry_err_code_t
  462. md_enable (gcry_md_hd_t hd, int algorithm)
  463. {
  464. struct gcry_md_context *h = hd->ctx;
  465. gcry_md_spec_t *digest = NULL;
  466. GcryDigestEntry *entry;
  467. gcry_module_t module;
  468. gcry_err_code_t err = 0;
  469. for (entry = h->list; entry; entry = entry->next)
  470. if (entry->module->mod_id == algorithm)
  471. return err; /* already enabled */
  472. REGISTER_DEFAULT_DIGESTS;
  473. ath_mutex_lock (&digests_registered_lock);
  474. module = _gcry_module_lookup_id (digests_registered, algorithm);
  475. ath_mutex_unlock (&digests_registered_lock);
  476. if (! module)
  477. {
  478. log_debug ("md_enable: algorithm %d not available\n", algorithm);
  479. err = GPG_ERR_DIGEST_ALGO;
  480. }
  481. else
  482. digest = (gcry_md_spec_t *) module->spec;
  483. if (!err && algorithm == GCRY_MD_MD5 && fips_mode ())
  484. {
  485. _gcry_inactivate_fips_mode ("MD5 used");
  486. if (_gcry_enforced_fips_mode () )
  487. {
  488. /* We should never get to here because we do not register
  489. MD5 in enforced fips mode. But better throw an error. */
  490. err = GPG_ERR_DIGEST_ALGO;
  491. }
  492. }
  493. if (!err)
  494. {
  495. size_t size = (sizeof (*entry)
  496. + digest->contextsize
  497. - sizeof (entry->context));
  498. /* And allocate a new list entry. */
  499. if (h->secure)
  500. entry = gcry_malloc_secure (size);
  501. else
  502. entry = gcry_malloc (size);
  503. if (! entry)
  504. err = gpg_err_code_from_errno (errno);
  505. else
  506. {
  507. entry->digest = digest;
  508. entry->module = module;
  509. entry->next = h->list;
  510. entry->actual_struct_size = size;
  511. h->list = entry;
  512. /* And init this instance. */
  513. entry->digest->init (&entry->context.c);
  514. }
  515. }
  516. if (err)
  517. {
  518. if (module)
  519. {
  520. ath_mutex_lock (&digests_registered_lock);
  521. _gcry_module_release (module);
  522. ath_mutex_unlock (&digests_registered_lock);
  523. }
  524. }
  525. return err;
  526. }
  527. gcry_error_t
  528. gcry_md_enable (gcry_md_hd_t hd, int algorithm)
  529. {
  530. return gcry_error (md_enable (hd, algorithm));
  531. }
  532. static gcry_err_code_t
  533. md_copy (gcry_md_hd_t ahd, gcry_md_hd_t *b_hd)
  534. {
  535. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  536. struct gcry_md_context *a = ahd->ctx;
  537. struct gcry_md_context *b;
  538. GcryDigestEntry *ar, *br;
  539. gcry_md_hd_t bhd;
  540. size_t n;
  541. if (ahd->bufpos)
  542. md_write (ahd, NULL, 0);
  543. n = (char *) ahd->ctx - (char *) ahd;
  544. if (a->secure)
  545. bhd = gcry_malloc_secure (n + sizeof (struct gcry_md_context));
  546. else
  547. bhd = gcry_malloc (n + sizeof (struct gcry_md_context));
  548. if (! bhd)
  549. err = gpg_err_code_from_errno (errno);
  550. if (! err)
  551. {
  552. bhd->ctx = b = (struct gcry_md_context *) ((char *) bhd + n);
  553. /* No need to copy the buffer due to the write above. */
  554. gcry_assert (ahd->bufsize == (n - sizeof (struct gcry_md_handle) + 1));
  555. bhd->bufsize = ahd->bufsize;
  556. bhd->bufpos = 0;
  557. gcry_assert (! ahd->bufpos);
  558. memcpy (b, a, sizeof *a);
  559. b->list = NULL;
  560. b->debug = NULL;
  561. if (a->macpads)
  562. {
  563. b->macpads = gcry_malloc_secure (2*(a->macpads_Bsize));
  564. if (! b->macpads)
  565. {
  566. err = gpg_err_code_from_errno (errno);
  567. md_close (bhd);
  568. }
  569. else
  570. memcpy (b->macpads, a->macpads, (2*(a->macpads_Bsize)));
  571. }
  572. }
  573. /* Copy the complete list of algorithms. The copied list is
  574. reversed, but that doesn't matter. */
  575. if (!err)
  576. {
  577. for (ar = a->list; ar; ar = ar->next)
  578. {
  579. if (a->secure)
  580. br = gcry_malloc_secure (sizeof *br
  581. + ar->digest->contextsize
  582. - sizeof(ar->context));
  583. else
  584. br = gcry_malloc (sizeof *br
  585. + ar->digest->contextsize
  586. - sizeof (ar->context));
  587. if (!br)
  588. {
  589. err = gpg_err_code_from_errno (errno);
  590. md_close (bhd);
  591. break;
  592. }
  593. memcpy (br, ar, (sizeof (*br) + ar->digest->contextsize
  594. - sizeof (ar->context)));
  595. br->next = b->list;
  596. b->list = br;
  597. /* Add a reference to the module. */
  598. ath_mutex_lock (&digests_registered_lock);
  599. _gcry_module_use (br->module);
  600. ath_mutex_unlock (&digests_registered_lock);
  601. }
  602. }
  603. if (a->debug && !err)
  604. md_start_debug (bhd, "unknown");
  605. if (!err)
  606. *b_hd = bhd;
  607. return err;
  608. }
  609. gcry_error_t
  610. gcry_md_copy (gcry_md_hd_t *handle, gcry_md_hd_t hd)
  611. {
  612. gcry_err_code_t err;
  613. err = md_copy (hd, handle);
  614. if (err)
  615. *handle = NULL;
  616. return gcry_error (err);
  617. }
  618. /*
  619. * Reset all contexts and discard any buffered stuff. This may be used
  620. * instead of a md_close(); md_open().
  621. */
  622. void
  623. gcry_md_reset (gcry_md_hd_t a)
  624. {
  625. GcryDigestEntry *r;
  626. /* Note: We allow this even in fips non operational mode. */
  627. a->bufpos = a->ctx->finalized = 0;
  628. for (r = a->ctx->list; r; r = r->next)
  629. {
  630. memset (r->context.c, 0, r->digest->contextsize);
  631. (*r->digest->init) (&r->context.c);
  632. }
  633. if (a->ctx->macpads)
  634. md_write (a, a->ctx->macpads, a->ctx->macpads_Bsize); /* inner pad */
  635. }
  636. static void
  637. md_close (gcry_md_hd_t a)
  638. {
  639. GcryDigestEntry *r, *r2;
  640. if (! a)
  641. return;
  642. if (a->ctx->debug)
  643. md_stop_debug (a);
  644. for (r = a->ctx->list; r; r = r2)
  645. {
  646. r2 = r->next;
  647. ath_mutex_lock (&digests_registered_lock);
  648. _gcry_module_release (r->module);
  649. ath_mutex_unlock (&digests_registered_lock);
  650. wipememory (r, r->actual_struct_size);
  651. gcry_free (r);
  652. }
  653. if (a->ctx->macpads)
  654. {
  655. wipememory (a->ctx->macpads, 2*(a->ctx->macpads_Bsize));
  656. gcry_free(a->ctx->macpads);
  657. }
  658. wipememory (a, a->ctx->actual_handle_size);
  659. gcry_free(a);
  660. }
  661. void
  662. gcry_md_close (gcry_md_hd_t hd)
  663. {
  664. /* Note: We allow this even in fips non operational mode. */
  665. md_close (hd);
  666. }
  667. static void
  668. md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen)
  669. {
  670. GcryDigestEntry *r;
  671. if (a->ctx->debug)
  672. {
  673. if (a->bufpos && fwrite (a->buf, a->bufpos, 1, a->ctx->debug) != 1)
  674. BUG();
  675. if (inlen && fwrite (inbuf, inlen, 1, a->ctx->debug) != 1)
  676. BUG();
  677. }
  678. for (r = a->ctx->list; r; r = r->next)
  679. {
  680. if (a->bufpos)
  681. (*r->digest->write) (&r->context.c, a->buf, a->bufpos);
  682. (*r->digest->write) (&r->context.c, inbuf, inlen);
  683. }
  684. a->bufpos = 0;
  685. }
  686. void
  687. gcry_md_write (gcry_md_hd_t hd, const void *inbuf, size_t inlen)
  688. {
  689. md_write (hd, inbuf, inlen);
  690. }
  691. static void
  692. md_final (gcry_md_hd_t a)
  693. {
  694. GcryDigestEntry *r;
  695. if (a->ctx->finalized)
  696. return;
  697. if (a->bufpos)
  698. md_write (a, NULL, 0);
  699. for (r = a->ctx->list; r; r = r->next)
  700. (*r->digest->final) (&r->context.c);
  701. a->ctx->finalized = 1;
  702. if (a->ctx->macpads)
  703. {
  704. /* Finish the hmac. */
  705. int algo = md_get_algo (a);
  706. byte *p = md_read (a, algo);
  707. size_t dlen = md_digest_length (algo);
  708. gcry_md_hd_t om;
  709. gcry_err_code_t err = md_open (&om, algo, a->ctx->secure, 0);
  710. if (err)
  711. _gcry_fatal_error (err, NULL);
  712. md_write (om,
  713. (a->ctx->macpads)+(a->ctx->macpads_Bsize),
  714. a->ctx->macpads_Bsize);
  715. md_write (om, p, dlen);
  716. md_final (om);
  717. /* Replace our digest with the mac (they have the same size). */
  718. memcpy (p, md_read (om, algo), dlen);
  719. md_close (om);
  720. }
  721. }
  722. static gcry_err_code_t
  723. prepare_macpads (gcry_md_hd_t hd, const unsigned char *key, size_t keylen)
  724. {
  725. int i;
  726. int algo = md_get_algo (hd);
  727. unsigned char *helpkey = NULL;
  728. unsigned char *ipad, *opad;
  729. if (!algo)
  730. return GPG_ERR_DIGEST_ALGO; /* Might happen if no algo is enabled. */
  731. if ( keylen > hd->ctx->macpads_Bsize )
  732. {
  733. helpkey = gcry_malloc_secure (md_digest_length (algo));
  734. if (!helpkey)
  735. return gpg_err_code_from_errno (errno);
  736. gcry_md_hash_buffer (algo, helpkey, key, keylen);
  737. key = helpkey;
  738. keylen = md_digest_length (algo);
  739. gcry_assert ( keylen <= hd->ctx->macpads_Bsize );
  740. }
  741. memset ( hd->ctx->macpads, 0, 2*(hd->ctx->macpads_Bsize) );
  742. ipad = hd->ctx->macpads;
  743. opad = (hd->ctx->macpads)+(hd->ctx->macpads_Bsize);
  744. memcpy ( ipad, key, keylen );
  745. memcpy ( opad, key, keylen );
  746. for (i=0; i < hd->ctx->macpads_Bsize; i++ )
  747. {
  748. ipad[i] ^= 0x36;
  749. opad[i] ^= 0x5c;
  750. }
  751. gcry_free (helpkey);
  752. return GPG_ERR_NO_ERROR;
  753. }
  754. gcry_error_t
  755. gcry_md_ctl (gcry_md_hd_t hd, int cmd, void *buffer, size_t buflen)
  756. {
  757. gcry_err_code_t rc = 0;
  758. switch (cmd)
  759. {
  760. case GCRYCTL_FINALIZE:
  761. md_final (hd);
  762. break;
  763. case GCRYCTL_SET_KEY:
  764. rc = gcry_err_code (gcry_md_setkey (hd, buffer, buflen));
  765. break;
  766. case GCRYCTL_START_DUMP:
  767. md_start_debug (hd, buffer);
  768. break;
  769. case GCRYCTL_STOP_DUMP:
  770. md_stop_debug ( hd );
  771. break;
  772. default:
  773. rc = GPG_ERR_INV_OP;
  774. }
  775. return gcry_error (rc);
  776. }
  777. gcry_error_t
  778. gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen)
  779. {
  780. gcry_err_code_t rc = GPG_ERR_NO_ERROR;
  781. if (!hd->ctx->macpads)
  782. rc = GPG_ERR_CONFLICT;
  783. else
  784. {
  785. rc = prepare_macpads (hd, key, keylen);
  786. if (! rc)
  787. gcry_md_reset (hd);
  788. }
  789. return gcry_error (rc);
  790. }
  791. /* The new debug interface. If SUFFIX is a string it creates an debug
  792. file for the context HD. IF suffix is NULL, the file is closed and
  793. debugging is stopped. */
  794. void
  795. gcry_md_debug (gcry_md_hd_t hd, const char *suffix)
  796. {
  797. if (suffix)
  798. md_start_debug (hd, suffix);
  799. else
  800. md_stop_debug (hd);
  801. }
  802. /****************
  803. * if ALGO is null get the digest for the used algo (which should be only one)
  804. */
  805. static byte *
  806. md_read( gcry_md_hd_t a, int algo )
  807. {
  808. GcryDigestEntry *r = a->ctx->list;
  809. if (! algo)
  810. {
  811. /* Return the first algorithm */
  812. if (r)
  813. {
  814. if (r->next)
  815. log_debug ("more than one algorithm in md_read(0)\n");
  816. return r->digest->read (&r->context.c);
  817. }
  818. }
  819. else
  820. {
  821. for (r = a->ctx->list; r; r = r->next)
  822. if (r->module->mod_id == algo)
  823. return r->digest->read (&r->context.c);
  824. }
  825. BUG();
  826. return NULL;
  827. }
  828. /*
  829. * Read out the complete digest, this function implictly finalizes
  830. * the hash.
  831. */
  832. byte *
  833. gcry_md_read (gcry_md_hd_t hd, int algo)
  834. {
  835. /* This function is expected to always return a digest, thus we
  836. can't return an error which we actually should do in
  837. non-operational state. */
  838. gcry_md_ctl (hd, GCRYCTL_FINALIZE, NULL, 0);
  839. return md_read (hd, algo);
  840. }
  841. /*
  842. * Read out an intermediate digest. Not yet functional.
  843. */
  844. gcry_err_code_t
  845. gcry_md_get (gcry_md_hd_t hd, int algo, byte *buffer, int buflen)
  846. {
  847. (void)hd;
  848. (void)algo;
  849. (void)buffer;
  850. (void)buflen;
  851. /*md_digest ... */
  852. fips_signal_error ("unimplemented function called");
  853. return GPG_ERR_INTERNAL;
  854. }
  855. /*
  856. * Shortcut function to hash a buffer with a given algo. The only
  857. * guaranteed supported algorithms are RIPE-MD160 and SHA-1. The
  858. * supplied digest buffer must be large enough to store the resulting
  859. * hash. No error is returned, the function will abort on an invalid
  860. * algo. DISABLED_ALGOS are ignored here. */
  861. void
  862. gcry_md_hash_buffer (int algo, void *digest,
  863. const void *buffer, size_t length)
  864. {
  865. if (algo == GCRY_MD_SHA1)
  866. _gcry_sha1_hash_buffer (digest, buffer, length);
  867. else if (algo == GCRY_MD_RMD160 && !fips_mode () )
  868. _gcry_rmd160_hash_buffer (digest, buffer, length);
  869. else
  870. {
  871. /* For the others we do not have a fast function, so we use the
  872. normal functions. */
  873. gcry_md_hd_t h;
  874. gpg_err_code_t err;
  875. if (algo == GCRY_MD_MD5 && fips_mode ())
  876. {
  877. _gcry_inactivate_fips_mode ("MD5 used");
  878. if (_gcry_enforced_fips_mode () )
  879. {
  880. /* We should never get to here because we do not register
  881. MD5 in enforced fips mode. */
  882. _gcry_fips_noreturn ();
  883. }
  884. }
  885. err = md_open (&h, algo, 0, 0);
  886. if (err)
  887. log_bug ("gcry_md_open failed for algo %d: %s",
  888. algo, gpg_strerror (gcry_error(err)));
  889. md_write (h, (byte *) buffer, length);
  890. md_final (h);
  891. memcpy (digest, md_read (h, algo), md_digest_length (algo));
  892. md_close (h);
  893. }
  894. }
  895. static int
  896. md_get_algo (gcry_md_hd_t a)
  897. {
  898. GcryDigestEntry *r = a->ctx->list;
  899. if (r && r->next)
  900. {
  901. fips_signal_error ("possible usage error");
  902. log_error ("WARNING: more than one algorithm in md_get_algo()\n");
  903. }
  904. return r ? r->module->mod_id : 0;
  905. }
  906. int
  907. gcry_md_get_algo (gcry_md_hd_t hd)
  908. {
  909. return md_get_algo (hd);
  910. }
  911. /****************
  912. * Return the length of the digest
  913. */
  914. static int
  915. md_digest_length (int algorithm)
  916. {
  917. gcry_module_t digest;
  918. int mdlen = 0;
  919. REGISTER_DEFAULT_DIGESTS;
  920. ath_mutex_lock (&digests_registered_lock);
  921. digest = _gcry_module_lookup_id (digests_registered, algorithm);
  922. if (digest)
  923. {
  924. mdlen = ((gcry_md_spec_t *) digest->spec)->mdlen;
  925. _gcry_module_release (digest);
  926. }
  927. ath_mutex_unlock (&digests_registered_lock);
  928. return mdlen;
  929. }
  930. /****************
  931. * Return the length of the digest in bytes.
  932. * This function will return 0 in case of errors.
  933. */
  934. unsigned int
  935. gcry_md_get_algo_dlen (int algorithm)
  936. {
  937. return md_digest_length (algorithm);
  938. }
  939. /* Hmmm: add a mode to enumerate the OIDs
  940. * to make g10/sig-check.c more portable */
  941. static const byte *
  942. md_asn_oid (int algorithm, size_t *asnlen, size_t *mdlen)
  943. {
  944. const byte *asnoid = NULL;
  945. gcry_module_t digest;
  946. REGISTER_DEFAULT_DIGESTS;
  947. ath_mutex_lock (&digests_registered_lock);
  948. digest = _gcry_module_lookup_id (digests_registered, algorithm);
  949. if (digest)
  950. {
  951. if (asnlen)
  952. *asnlen = ((gcry_md_spec_t *) digest->spec)->asnlen;
  953. if (mdlen)
  954. *mdlen = ((gcry_md_spec_t *) digest->spec)->mdlen;
  955. asnoid = ((gcry_md_spec_t *) digest->spec)->asnoid;
  956. _gcry_module_release (digest);
  957. }
  958. else
  959. log_bug ("no ASN.1 OID for md algo %d\n", algorithm);
  960. ath_mutex_unlock (&digests_registered_lock);
  961. return asnoid;
  962. }
  963. /****************
  964. * Return information about the given cipher algorithm
  965. * WHAT select the kind of information returned:
  966. * GCRYCTL_TEST_ALGO:
  967. * Returns 0 when the specified algorithm is available for use.
  968. * buffer and nbytes must be zero.
  969. * GCRYCTL_GET_ASNOID:
  970. * Return the ASNOID of the algorithm in buffer. if buffer is NULL, only
  971. * the required length is returned.
  972. *
  973. * Note: Because this function is in most cases used to return an
  974. * integer value, we can make it easier for the caller to just look at
  975. * the return value. The caller will in all cases consult the value
  976. * and thereby detecting whether a error occurred or not (i.e. while checking
  977. * the block size)
  978. */
  979. gcry_error_t
  980. gcry_md_algo_info (int algo, int what, void *buffer, size_t *nbytes)
  981. {
  982. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  983. switch (what)
  984. {
  985. case GCRYCTL_TEST_ALGO:
  986. if (buffer || nbytes)
  987. err = GPG_ERR_INV_ARG;
  988. else
  989. err = check_digest_algo (algo);
  990. break;
  991. case GCRYCTL_GET_ASNOID:
  992. /* We need to check that the algo is available because
  993. md_asn_oid would otherwise raise an assertion. */
  994. err = check_digest_algo (algo);
  995. if (!err)
  996. {
  997. const char unsigned *asn;
  998. size_t asnlen;
  999. asn = md_asn_oid (algo, &asnlen, NULL);
  1000. if (buffer && (*nbytes >= asnlen))
  1001. {
  1002. memcpy (buffer, asn, asnlen);
  1003. *nbytes = asnlen;
  1004. }
  1005. else if (!buffer && nbytes)
  1006. *nbytes = asnlen;
  1007. else
  1008. {
  1009. if (buffer)
  1010. err = GPG_ERR_TOO_SHORT;
  1011. else
  1012. err = GPG_ERR_INV_ARG;
  1013. }
  1014. }
  1015. break;
  1016. default:
  1017. err = GPG_ERR_INV_OP;
  1018. }
  1019. return gcry_error (err);
  1020. }
  1021. static void
  1022. md_start_debug ( gcry_md_hd_t md, const char *suffix )
  1023. {
  1024. static int idx=0;
  1025. char buf[50];
  1026. if (fips_mode ())
  1027. return;
  1028. if ( md->ctx->debug )
  1029. {
  1030. log_debug("Oops: md debug already started\n");
  1031. return;
  1032. }
  1033. idx++;
  1034. snprintf (buf, DIM(buf)-1, "dbgmd-%05d.%.10s", idx, suffix );
  1035. md->ctx->debug = fopen(buf, "w");
  1036. if ( !md->ctx->debug )
  1037. log_debug("md debug: can't open %s\n", buf );
  1038. }
  1039. static void
  1040. md_stop_debug( gcry_md_hd_t md )
  1041. {
  1042. if ( md->ctx->debug )
  1043. {
  1044. if ( md->bufpos )
  1045. md_write ( md, NULL, 0 );
  1046. fclose (md->ctx->debug);
  1047. md->ctx->debug = NULL;
  1048. }
  1049. #ifdef HAVE_U64_TYPEDEF
  1050. { /* a kludge to pull in the __muldi3 for Solaris */
  1051. volatile u32 a = (u32)(ulong)md;
  1052. volatile u64 b = 42;
  1053. volatile u64 c;
  1054. c = a * b;
  1055. (void)c;
  1056. }
  1057. #endif
  1058. }
  1059. /*
  1060. * Return information about the digest handle.
  1061. * GCRYCTL_IS_SECURE:
  1062. * Returns 1 when the handle works on secured memory
  1063. * otherwise 0 is returned. There is no error return.
  1064. * GCRYCTL_IS_ALGO_ENABLED:
  1065. * Returns 1 if the algo is enabled for that handle.
  1066. * The algo must be passed as the address of an int.
  1067. */
  1068. gcry_error_t
  1069. gcry_md_info (gcry_md_hd_t h, int cmd, void *buffer, size_t *nbytes)
  1070. {
  1071. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  1072. switch (cmd)
  1073. {
  1074. case GCRYCTL_IS_SECURE:
  1075. *nbytes = h->ctx->secure;
  1076. break;
  1077. case GCRYCTL_IS_ALGO_ENABLED:
  1078. {
  1079. GcryDigestEntry *r;
  1080. int algo;
  1081. if ( !buffer || (nbytes && (*nbytes != sizeof (int))))
  1082. err = GPG_ERR_INV_ARG;
  1083. else
  1084. {
  1085. algo = *(int*)buffer;
  1086. *nbytes = 0;
  1087. for(r=h->ctx->list; r; r = r->next ) {
  1088. if (r->module->mod_id == algo)
  1089. {
  1090. *nbytes = 1;
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. break;
  1096. }
  1097. default:
  1098. err = GPG_ERR_INV_OP;
  1099. }
  1100. return gcry_error (err);
  1101. }
  1102. /* Explicitly initialize this module. */
  1103. gcry_err_code_t
  1104. _gcry_md_init (void)
  1105. {
  1106. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  1107. REGISTER_DEFAULT_DIGESTS;
  1108. return err;
  1109. }
  1110. int
  1111. gcry_md_is_secure (gcry_md_hd_t a)
  1112. {
  1113. size_t value;
  1114. if (gcry_md_info (a, GCRYCTL_IS_SECURE, NULL, &value))
  1115. value = 1; /* It seems to be better to assume secure memory on
  1116. error. */
  1117. return value;
  1118. }
  1119. int
  1120. gcry_md_is_enabled (gcry_md_hd_t a, int algo)
  1121. {
  1122. size_t value;
  1123. value = sizeof algo;
  1124. if (gcry_md_info (a, GCRYCTL_IS_ALGO_ENABLED, &algo, &value))
  1125. value = 0;
  1126. return value;
  1127. }
  1128. /* Get a list consisting of the IDs of the loaded message digest
  1129. modules. If LIST is zero, write the number of loaded message
  1130. digest modules to LIST_LENGTH and return. If LIST is non-zero, the
  1131. first *LIST_LENGTH algorithm IDs are stored in LIST, which must be
  1132. of according size. In case there are less message digest modules
  1133. than *LIST_LENGTH, *LIST_LENGTH is updated to the correct
  1134. number. */
  1135. gcry_error_t
  1136. gcry_md_list (int *list, int *list_length)
  1137. {
  1138. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  1139. ath_mutex_lock (&digests_registered_lock);
  1140. err = _gcry_module_list (digests_registered, list, list_length);
  1141. ath_mutex_unlock (&digests_registered_lock);
  1142. return err;
  1143. }
  1144. /* Run the selftests for digest algorithm ALGO with optional reporting
  1145. function REPORT. */
  1146. gpg_error_t
  1147. _gcry_md_selftest (int algo, int extended, selftest_report_func_t report)
  1148. {
  1149. gcry_module_t module = NULL;
  1150. cipher_extra_spec_t *extraspec = NULL;
  1151. gcry_err_code_t ec = 0;
  1152. REGISTER_DEFAULT_DIGESTS;
  1153. ath_mutex_lock (&digests_registered_lock);
  1154. module = _gcry_module_lookup_id (digests_registered, algo);
  1155. if (module && !(module->flags & FLAG_MODULE_DISABLED))
  1156. extraspec = module->extraspec;
  1157. ath_mutex_unlock (&digests_registered_lock);
  1158. if (extraspec && extraspec->selftest)
  1159. ec = extraspec->selftest (algo, extended, report);
  1160. else
  1161. {
  1162. ec = GPG_ERR_DIGEST_ALGO;
  1163. if (report)
  1164. report ("digest", algo, "module",
  1165. module && !(module->flags & FLAG_MODULE_DISABLED)?
  1166. "no selftest available" :
  1167. module? "algorithm disabled" : "algorithm not found");
  1168. }
  1169. if (module)
  1170. {
  1171. ath_mutex_lock (&digests_registered_lock);
  1172. _gcry_module_release (module);
  1173. ath_mutex_unlock (&digests_registered_lock);
  1174. }
  1175. return gpg_error (ec);
  1176. }