5 * Cryptographic challenges
7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Static variables --------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @c_genkey@ --- *
47 * Use: Generates a new challenge key.
50 static void c_genkey(void)
52 if (mac
&& GM_CLASS(mac
) == algs
.m
&& oseq
< 0x07ffffff) return;
53 if (mac
) GM_DESTROY(mac
);
54 assert(algs
.mksz
< sizeof(buf_t
));
55 rand_get(RAND_GLOBAL
, buf_t
, algs
.mksz
);
56 mac
= GM_KEY(algs
.m
, buf_t
, algs
.mksz
);
60 trace(T_CHAL
, "chal: generated new challenge key");
61 trace_block(T_CRYPTO
, "chal: new key", buf_t
, algs
.mksz
);
67 * Arguments: @buf *b@ = where to put the challenge
69 * Returns: Zero if OK, nonzero on error.
71 * Use: Issues a new challenge.
81 if (buf_putu32(b
, oseq
++)) return (-1);
83 GH_HASH(h
, p
, BCUR(b
) - p
);
84 buf_put(b
, GH_DONE(h
, 0), algs
.tagsz
);
86 if (BBAD(b
)) return (-1);
88 trace(T_CHAL
, "chal: issuing challenge %lu", (unsigned long)(oseq
- 1));
89 trace_block(T_CRYPTO
, "chal: challenge block", p
, BCUR(b
) - p
);
94 /* --- @c_check@ --- *
96 * Arguments: @buf *b@ = where to find the challenge
98 * Returns: Zero if OK, nonzero if it didn't work.
100 * Use: Checks a challenge. On failure, the buffer is broken.
106 size_t sz
= 4 + algs
.tagsz
;
111 if ((p
= buf_get(b
, sz
)) == 0) {
112 a_warn("CHAL", "invalid-challenge", A_END
);
115 IF_TRACING(T_CHAL
, trace_block(T_CRYPTO
, "chal: check challenge", p
, sz
); )
117 a_warn("CHAL", "impossible-challenge", A_END
);
122 ok
= (memcmp(GH_DONE(h
, 0), p
+ 4, algs
.tagsz
) == 0);
125 a_warn("CHAL", "incorrect-tag", A_END
);
129 if (seq_check(&iseq
, LOAD32(p
), "CHAL"))
131 T( trace(T_CHAL
, "chal: checked challenge %lu", (unsigned long)seq
); )
139 /*----- That's all, folks -------------------------------------------------*/