avr-crypto-lib/arcfour/arcfour-asm.S

139 lines
2.8 KiB
ArmAsm
Raw Normal View History

2008-05-26 19:13:21 +00:00
/* arcfour-asm.S */
/*
This file is part of the AVR-Crypto-Lib.
2015-02-06 02:43:31 +00:00
Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
2008-05-26 19:13:21 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2008-08-02 04:38:17 +00:00
/*
2008-07-03 04:11:34 +00:00
* File: arcfour-asm.S
* Author: Daniel Otte
* Date: 2006-07-06
* License: GPLv3 or later
* Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
*
*/
2008-08-06 18:04:23 +00:00
#include <avr/io.h>
2008-08-19 12:18:25 +00:00
#include "avr-asm-macros.S"
2008-08-06 18:04:23 +00:00
/* +---+---+---------------------+
* | i | j | ......<256>........ |
* +---+---+---------------------+
*/
.global arcfour_init
2008-08-06 18:04:23 +00:00
/*
*== arcfour_init ==
* this function initialises the context
* param1: 16-bit pointer to the key
* given in r24:r25
2011-01-21 23:29:18 +00:00
* param2: 8-bit integer indicating keylength in bits
* given in r22:r23
2008-08-06 18:04:23 +00:00
* param3: 16-bit pointer to a ctx struct
* given in r20:r21
*/
arcfour_init:
2008-08-07 09:45:50 +00:00
push_ r28, r29
2008-08-06 18:04:23 +00:00
movw r26, r20 /* X points to ctx */
movw r30, r24 /* Z points to key */
st X+, r1
2008-08-06 18:04:23 +00:00
st X+, r1 /* X points to S */
movw r20, r26 /* store pointer to S in r21:r20 */
2011-01-21 23:29:18 +00:00
lsr r23
ror r22
lsr r23
ror r22
lsr r23
ror r22
1:
st X+, r1
inc r1
brne 1b
2008-08-06 18:04:23 +00:00
movw r26, r20
2008-08-07 09:45:50 +00:00
add r22, r30 /* r18 is keyindex counter */
clr r0
2008-08-06 18:04:23 +00:00
clr r19
2:
2008-08-06 18:04:23 +00:00
ld r23, X
2008-08-07 09:45:50 +00:00
ld r18, Z+
add r19, r18
2008-08-06 18:04:23 +00:00
add r19, r23
movw r28, r20 /* load pointer to S in Y */
add r28, r19
adc r29, r1
2008-08-07 09:45:50 +00:00
ld r18, Y
2008-08-06 18:04:23 +00:00
st Y, r23
2008-08-07 09:45:50 +00:00
st X+, r18
cp r30, r22
2008-08-06 18:04:23 +00:00
brne 3f
movw r30, r24
3:
inc r0
brne 2b
2008-08-07 09:45:50 +00:00
pop_ r29, r28
ret
/*
uint8_t arcfour_gen(arcfour_ctx_t *c){
uint8_t t;
c->i++;
c->j += c->s[c->i];
t = c->s[c->j];
c->s[c->j] = c->s[c->i];
c->s[c->i] = t;
return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
}
*/
.global arcfour_gen
;== arcfour_gen ==
; this function generates a keystream byte
; param1: 16-bit pointer to a ctx struct
; given in r25,r24
arcfour_gen:
movw r26, r24
ld r18, X
inc r18
st X+, r18
movw r30, r26
ld r19, X+
add r26, r18
adc r27, r1
ld r20, X
add r19, r20
st Z+, r19 /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
add r30, r19
adc r31, r1
2013-09-20 00:10:15 +00:00
ld r21, Z /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j] */
st Z, r20
st X, r21
add r20, r21
adiw r24, 2
movw r26, r24 /* X and Z point to S */
add r26, r20
adc r27, r1
ld r24, X
clr r25
ret