Codebase list libcryptx-perl / 112e890
compatibility with Crypt::Rabbit Karel Miko 6 years ago
3 changed file(s) with 30 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
2525 rv = rabbit_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
2626 }
2727 else {
28 rv = rabbit_setiv(&RETVAL->state, NULL, 0);
28 /* rv = rabbit_setiv(&RETVAL->state, NULL, 0); */
29 rv = CRYPT_OK; /* XXX-FIXME this is a hack - skip rabbit_setiv if undefined nonce */
2930 }
3031 if (rv != CRYPT_OK) {
3132 Safefree(RETVAL);
222222 unsigned char tmpiv[8] = {0};
223223
224224 LTC_ARGCHK(st != NULL);
225 LTC_ARGCHK(iv != NULL);
226225 LTC_ARGCHK(ivlen <= 8);
226 LTC_ARGCHK(iv != NULL || ivlen == 0);
227227
228228 /* pad iv in tmpiv */
229 XMEMCPY(tmpiv, iv, ivlen);
229 if (ivlen > 0) XMEMCPY(tmpiv, iv, ivlen);
230230
231231 /* Generate four subvectors */
232232 LOAD32L(i0, tmpiv+0);
193193 /* ======================================================================== */
194194
195195 /*
196 * Key schedule: initialize the key context structure with the provided
197 * secret key. The secret key is an array of 1 to 32 bytes.
196 * Initialize Sosemanuk's state by providing a key. The key is an array of
197 * 1 to 32 bytes.
198198 * @param ss The Sosemanuk state
199199 * @param key Key
200 * @param keylen Length of key
200 * @param keylen Length of key in bytes
201201 * @return CRYPT_OK on success
202202 */
203203 int sosemanuk_setup(sosemanuk_state *ss, unsigned char *key, unsigned long keylen)
330330
331331
332332 /*
333 * Cipher initialization: the cipher internal state is initialized, using
334 * the provided key context and IV. The IV length is up to 16 bytes. If
335 * "ivlen" is 0 (no IV), then the "iv" parameter can be NULL.
333 * Initialization continues by setting the IV. The IV length is up to 16 bytes.
334 * If "ivlen" is 0 (no IV), then the "iv" parameter can be NULL. If multiple
335 * encryptions/decryptions are to be performed with the same key and
336 * sosemanuk_done() has not been called, only sosemanuk_setiv() need be called
337 * to set the state.
336338 * @param ss The Sosemanuk state
337339 * @param iv Initialization vector
338 * @param ivlen Length of iv
340 * @param ivlen Length of iv in bytes
339341 * @return CRYPT_OK on success
340342 */
341343 int sosemanuk_setiv(sosemanuk_state *ss, unsigned char *iv, unsigned long ivlen)
379381 unsigned char ivtmp[16] = {0};
380382
381383 LTC_ARGCHK(ss != NULL);
382 LTC_ARGCHK(ivlen >= 0 && ivlen <= 16);
384 LTC_ARGCHK(ivlen <= 16);
383385 LTC_ARGCHK(iv != NULL || ivlen == 0);
384386
385387 if (ivlen > 0) XMEMCPY(ivtmp, iv, ivlen);
447449 /*
448450 * Multiplication by alpha: alpha * x = T32(x << 8) ^ mul_a[x >> 24]
449451 */
450 static ulong32 mul_a[] = {
452 static const ulong32 mul_a[] = {
451453 0x00000000, 0xE19FCF13, 0x6B973726, 0x8A08F835,
452454 0xD6876E4C, 0x3718A15F, 0xBD10596A, 0x5C8F9679,
453455 0x05A7DC98, 0xE438138B, 0x6E30EBBE, 0x8FAF24AD,
517519 /*
518520 * Multiplication by 1/alpha: 1/alpha * x = (x >> 8) ^ mul_ia[x & 0xFF]
519521 */
520 static ulong32 mul_ia[] = {
522 static const ulong32 mul_ia[] = {
521523 0x00000000, 0x180F40CD, 0x301E8033, 0x2811C0FE,
522524 0x603CA966, 0x7833E9AB, 0x50222955, 0x482D6998,
523525 0xC078FBCC, 0xD877BB01, 0xF0667BFF, 0xE8693B32,
742744 * reference distinct buffers (no partial overlap is allowed).
743745 * @param ss The Sosemanuk state
744746 * @param in Data in
747 * @param inlen Length of data in bytes
745748 * @param out Data out
746 * @param datalen Length of data
747749 * @return CRYPT_OK on success
748750 */
749751 int sosemanuk_crypt(sosemanuk_state *ss,
750 const unsigned char *in, unsigned long datalen, unsigned char *out)
752 const unsigned char *in, unsigned long inlen, unsigned char *out)
751753 {
752754 LTC_ARGCHK(ss != NULL);
753755 LTC_ARGCHK(in != NULL);
756758 if (ss->ptr < (sizeof(ss->buf))) {
757759 unsigned long rlen = (sizeof(ss->buf)) - ss->ptr;
758760
759 if (rlen > datalen)
760 rlen = datalen;
761 if (rlen > inlen)
762 rlen = inlen;
761763 _xorbuf(ss->buf + ss->ptr, in, out, rlen);
762764 in += rlen;
763765 out += rlen;
764 datalen -= rlen;
766 inlen -= rlen;
765767 ss->ptr += rlen;
766768 }
767 while (datalen > 0) {
769 while (inlen > 0) {
768770 _sosemanuk_internal(ss);
769 if (datalen >= sizeof(ss->buf)) {
771 if (inlen >= sizeof(ss->buf)) {
770772 _xorbuf(ss->buf, in, out, sizeof(ss->buf));
771773 in += sizeof(ss->buf);
772774 out += sizeof(ss->buf);
773 datalen -= sizeof(ss->buf);
775 inlen -= sizeof(ss->buf);
774776 } else {
775 _xorbuf(ss->buf, in, out, datalen);
776 ss->ptr = datalen;
777 datalen = 0;
777 _xorbuf(ss->buf, in, out, inlen);
778 ss->ptr = inlen;
779 inlen = 0;
778780 }
779781 }
780782 return CRYPT_OK;
781783 }
782784
783785
786
784787 /*
785788 * Cipher operation, as a PRNG: the provided output buffer is filled with
786789 * pseudo-random bytes as output from the stream cipher.
787790 * @param ss The Sosemanuk state
788791 * @param out Data out
789 * @param outlen Length of output
792 * @param outlen Length of output in bytes
790793 * @return CRYPT_OK on success
791794 */
792795 int sosemanuk_keystream(sosemanuk_state *ss, unsigned char *out, unsigned long outlen)
800803
801804 /*
802805 * Terminate and clear Sosemanuk key context
803 * @param kc The Sosemanuk key context
806 * @param ss The Sosemanuk state
804807 * @return CRYPT_OK on success
805808 */
806809 int sosemanuk_done(sosemanuk_state *ss)