FreeBSD/sys/mips/rmi/dev/sec/rmisec.c
/*
 * Allocate a new 'session' and return an encoded session id.  'sidp'
 * contains our registration id, and should contain an encoded session
 * id on successful allocation.
 */
Allocate a new 'session' and return an encoded session id.  'sidp'
contains our registration id, and should contain an encoded session
id on successful allocation.
Allocate a new ' session ' and return an encoded session id . ' sidp ' contains our registration id and should contain an encoded session id on successful allocation .
static int
xlr_sec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
{
	struct cryptoini *c;
	struct xlr_sec_softc *sc = device_get_softc(dev);
	int mac = 0, cry = 0, sesn;
	struct xlr_sec_session *ses = NULL;
	if (sidp == NULL || cri == NULL || sc == NULL)
		return (EINVAL);
	if (sc->sc_sessions == NULL) {
		ses = sc->sc_sessions = (struct xlr_sec_session *)malloc(
		    sizeof(struct xlr_sec_session), M_DEVBUF, M_NOWAIT);
		if (ses == NULL)
			return (ENOMEM);
		sesn = 0;
		sc->sc_nsessions = 1;
	} else {
		for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
			if (!sc->sc_sessions[sesn].hs_used) {
				ses = &sc->sc_sessions[sesn];
				break;
			}
		}
		if (ses == NULL) {
			sesn = sc->sc_nsessions;
			ses = (struct xlr_sec_session *)malloc((sesn + 1) *
			    sizeof(struct xlr_sec_session), M_DEVBUF, M_NOWAIT);
			if (ses == NULL)
				return (ENOMEM);
			bcopy(sc->sc_sessions, ses, sesn * sizeof(*ses));
			bzero(sc->sc_sessions, sesn * sizeof(*ses));
			free(sc->sc_sessions, M_DEVBUF);
			sc->sc_sessions = ses;
			ses = &sc->sc_sessions[sesn];
			sc->sc_nsessions++;
		}
	}
	bzero(ses, sizeof(*ses));
	ses->sessionid = sesn;
	ses->desc_ptr = xlr_sec_allocate_desc(ses);
	if (ses->desc_ptr == NULL)
		return (ENOMEM);
	ses->hs_used = 1;
	for (c = cri; c != NULL; c = c->cri_next) {
		switch (c->cri_alg) {
		case CRYPTO_MD5:
		case CRYPTO_SHA1:
		case CRYPTO_MD5_HMAC:
		case CRYPTO_SHA1_HMAC:
			if (mac)
				return (EINVAL);
			mac = 1;
			ses->hs_mlen = c->cri_mlen;
			if (ses->hs_mlen == 0) {
				switch (c->cri_alg) {
				case CRYPTO_MD5:
				case CRYPTO_MD5_HMAC:
					ses->hs_mlen = 16;
					break;
				case CRYPTO_SHA1:
				case CRYPTO_SHA1_HMAC:
					ses->hs_mlen = 20;
					break;
				}
			}
			break;
		case CRYPTO_DES_CBC:
		case CRYPTO_3DES_CBC:
		case CRYPTO_AES_CBC:
		case CRYPTO_ARC4:
			if (cry)
				return (EINVAL);
			cry = 1;
			break;
		default:
			return (EINVAL);
		}
	}
	if (mac == 0 && cry == 0)
		return (EINVAL);
	*sidp = XLR_SEC_SID(device_get_unit(sc->sc_dev), sesn);
	return (0);
}
static int xlr_sec_newsession ( device_t dev , u_int32_t * sidp , struct cryptoini * cri ) { struct cryptoini * c ; struct xlr_sec_softc * sc = device_get_softc ( dev ) ; int mac = 0 , cry = 0 , sesn ; struct xlr_sec_session * ses = NULL ; if ( sidp == NULL || cri == NULL || sc == NULL ) return ( EINVAL ) ; if ( sc -> sc_sessions == NULL ) { ses = sc -> sc_sessions = ( struct xlr_sec_session * ) malloc ( sizeof ( struct xlr_sec_session ) , M_DEVBUF , M_NOWAIT ) ; if ( ses == NULL ) return ( ENOMEM ) ; sesn = 0 ; sc -> sc_nsessions = 1 ; } else { for ( sesn = 0 ; sesn < sc -> sc_nsessions ; sesn ++ ) { if ( ! sc -> sc_sessions [ sesn ] . hs_used ) { ses = & sc -> sc_sessions [ sesn ] ; break ; } } if ( ses == NULL ) { sesn = sc -> sc_nsessions ; ses = ( struct xlr_sec_session * ) malloc ( ( sesn + 1 ) * sizeof ( struct xlr_sec_session ) , M_DEVBUF , M_NOWAIT ) ; if ( ses == NULL ) return ( ENOMEM ) ; bcopy ( sc -> sc_sessions , ses , sesn * sizeof ( * ses ) ) ; bzero ( sc -> sc_sessions , sesn * sizeof ( * ses ) ) ; free ( sc -> sc_sessions , M_DEVBUF ) ; sc -> sc_sessions = ses ; ses = & sc -> sc_sessions [ sesn ] ; sc -> sc_nsessions ++ ; } } bzero ( ses , sizeof ( * ses ) ) ; ses -> sessionid = sesn ; ses -> desc_ptr = xlr_sec_allocate_desc ( ses ) ; if ( ses -> desc_ptr == NULL ) return ( ENOMEM ) ; ses -> hs_used = 1 ; for ( c = cri ; c != NULL ; c = c -> cri_next ) { switch ( c -> cri_alg ) { case CRYPTO_MD5 : case CRYPTO_SHA1 : case CRYPTO_MD5_HMAC : case CRYPTO_SHA1_HMAC : if ( mac ) return ( EINVAL ) ; mac = 1 ; ses -> hs_mlen = c -> cri_mlen ; if ( ses -> hs_mlen == 0 ) { switch ( c -> cri_alg ) { case CRYPTO_MD5 : case CRYPTO_MD5_HMAC : ses -> hs_mlen = 16 ; break ; case CRYPTO_SHA1 : case CRYPTO_SHA1_HMAC : ses -> hs_mlen = 20 ; break ; } } break ; case CRYPTO_DES_CBC : case CRYPTO_3DES_CBC : case CRYPTO_AES_CBC : case CRYPTO_ARC4 : if ( cry ) return ( EINVAL ) ; cry = 1 ; break ; default : return ( EINVAL ) ; } } if ( mac == 0 && cry == 0 ) return ( EINVAL ) ; * sidp = XLR_SEC_SID ( device_get_unit ( sc -> sc_dev ) , sesn ) ; return ( 0 ) ; }
Allocate a new 'session' and return an encoded session id.
Allocate a new ' session ' and return an encoded session id .
/* XXX this may read fewer, does it matter? */ /*
			 * read_random(ses->hs_iv, c->cri_alg ==
			 * CRYPTO_AES_CBC ? XLR_SEC_AES_IV_LENGTH :
			 * XLR_SEC_IV_LENGTH);
			 */ /* FALLTHROUGH */